perf(query): replace Coalesce with OR IS NULL in func_in (#38336)
This commit is contained in:
parent
e6180c4ab4
commit
51cfc8181e
2 changed files with 8 additions and 6 deletions
|
|
@ -8,7 +8,6 @@ import frappe
|
||||||
from frappe.database.utils import NestedSetHierarchy
|
from frappe.database.utils import NestedSetHierarchy
|
||||||
from frappe.model.db_query import get_timespan_date_range
|
from frappe.model.db_query import get_timespan_date_range
|
||||||
from frappe.query_builder import Field
|
from frappe.query_builder import Field
|
||||||
from frappe.query_builder.functions import Coalesce
|
|
||||||
from frappe.utils import cstr
|
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]
|
value = ["" if v is None else v for v in value]
|
||||||
if "" in value:
|
if "" in value:
|
||||||
return Coalesce(key, "").isin(value)
|
return key.isin(value) | key.isnull()
|
||||||
return key.isin(value)
|
return key.isin(value)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -525,15 +525,17 @@ class TestOperatorIn(IntegrationTestCase):
|
||||||
query = func_in(note.name, [None, "user1"])
|
query = func_in(note.name, [None, "user1"])
|
||||||
sql_str = str(query).lower()
|
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)
|
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")
|
note = frappe.qb.DocType("Note")
|
||||||
query = func_in(note.name, ["", "user1"])
|
query = func_in(note.name, ["", "user1"])
|
||||||
sql_str = str(query).lower()
|
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)
|
self.assertIn("''", sql_str)
|
||||||
|
|
||||||
def test_func_in_with_mixed_none_and_values(self):
|
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"])
|
query = func_in(note.name, ["val1", None, "val2"])
|
||||||
sql_str = str(query).lower()
|
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):
|
def test_in_filter_matches_null_and_empty_columns(self):
|
||||||
test_doctype = new_doctype(
|
test_doctype = new_doctype(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue