perf(query): replace Coalesce with OR IS NULL in func_in (#38336)

This commit is contained in:
Kaushal Shriwas 2026-04-08 10:57:07 +05:30 committed by GitHub
parent e6180c4ab4
commit 51cfc8181e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 6 deletions

View file

@ -8,7 +8,6 @@ import frappe
from frappe.database.utils import NestedSetHierarchy
from frappe.model.db_query import get_timespan_date_range
from frappe.query_builder import Field
from frappe.query_builder.functions import Coalesce
from frappe.utils import cstr
@ -51,7 +50,7 @@ def func_in(key: Field, value: list | tuple) -> frappe.qb:
value = ["" if v is None else v for v in value]
if "" in value:
return Coalesce(key, "").isin(value)
return key.isin(value) | key.isnull()
return key.isin(value)

View file

@ -525,15 +525,17 @@ class TestOperatorIn(IntegrationTestCase):
query = func_in(note.name, [None, "user1"])
sql_str = str(query).lower()
self.assertIn("coalesce", sql_str)
self.assertNotIn("coalesce", sql_str)
self.assertIn("is null", sql_str)
self.assertIn("''", sql_str)
def test_func_in_with_empty_string_uses_coalesce(self):
def test_func_in_with_empty_string_uses_or_is_null(self):
note = frappe.qb.DocType("Note")
query = func_in(note.name, ["", "user1"])
sql_str = str(query).lower()
self.assertIn("coalesce", sql_str)
self.assertNotIn("coalesce", sql_str)
self.assertIn("is null", sql_str)
self.assertIn("''", sql_str)
def test_func_in_with_mixed_none_and_values(self):
@ -541,7 +543,8 @@ class TestOperatorIn(IntegrationTestCase):
query = func_in(note.name, ["val1", None, "val2"])
sql_str = str(query).lower()
self.assertIn("coalesce", sql_str)
self.assertNotIn("coalesce", sql_str)
self.assertIn("is null", sql_str)
def test_in_filter_matches_null_and_empty_columns(self):
test_doctype = new_doctype(