perf: optimize != operator when field can be null

This commit is contained in:
Ankush Menat 2025-05-02 11:51:11 +05:30
parent e0f63a928f
commit 6d32ffcc6c
2 changed files with 11 additions and 0 deletions

View file

@ -924,6 +924,9 @@ from {tables}
# intersection instead of full table scans.
if fallback == value and f.operator == "=":
condition = f"( {column_name} is NULL OR {column_name} {f.operator} {value} )"
elif fallback == value and f.operator == "!=":
# NULL != anything is always NULL, so won't match
condition = f"{column_name} {f.operator} {value}"
else:
condition = f"ifnull({column_name}, {fallback}) {f.operator} {value}"

View file

@ -1200,6 +1200,14 @@ class TestDBQuery(IntegrationTestCase):
query = frappe.get_all("DocField", {"fieldname": None}, run=0)
self.assertIn("''", query)
self.assertNotIn("\\'", query)
self.assertNotIn("ifnull", query)
def test_ifnull_fallback_types(self):
query = frappe.get_all("DocField", {"fieldname": ("!=", None)}, run=0)
# Fallbacks should always be of correct type
self.assertIn("''", query)
self.assertNotIn("0", query)
self.assertNotIn("ifnull", query)
class TestReportView(IntegrationTestCase):