fix: handle distinct for fieldname (#25511)

`distinct count(fieldname)` is supported well but `count(distinct fieldname)` fails if fieldname contains full field with table name included. This PR just adds basic handling for it.

Needs to be rewritten entirely in QB __some day__.
This commit is contained in:
Ankush Menat 2024-03-18 19:08:29 +05:30 committed by GitHub
parent 41a7b42f16
commit 8a7beebf30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 1 deletions

View file

@ -471,6 +471,8 @@ class DatabaseQuery:
if table_name.lower().startswith("group_concat("):
table_name = table_name[13:]
if table_name.lower().startswith("distinct"):
table_name = table_name[8:].strip()
if table_name[0] != "`":
table_name = f"`{table_name}`"
if (

View file

@ -987,7 +987,7 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList {
if (this.count_without_children) {
str = __("{0} of {1} ({2} rows with children)", [
count_without_children,
this.total_count,
count_str,
current_count,
]);
}

View file

@ -14,6 +14,7 @@ from frappe.handler import execute_cmd
from frappe.model.db_query import DatabaseQuery, get_between_date_filter
from frappe.permissions import add_user_permission, clear_user_permissions_for_doctype
from frappe.query_builder import Column
from frappe.tests.test_query_builder import db_type_is, run_only_if
from frappe.tests.utils import FrappeTestCase
from frappe.utils.testutils import add_custom_field, clear_custom_fields
@ -1174,6 +1175,7 @@ class TestDBQuery(FrappeTestCase):
class TestReportView(FrappeTestCase):
@run_only_if(db_type_is.MARIADB) # TODO: postgres name casting is messed up
def test_get_count(self):
frappe.local.request = frappe._dict()
frappe.local.request.method = "GET"
@ -1233,6 +1235,20 @@ class TestReportView(FrappeTestCase):
self.assertIsInstance(count, int)
self.assertLessEqual(count, limit)
# test with distinct
limit = 2
frappe.local.form_dict = frappe._dict(
{
"doctype": "DocType",
"fields": [],
"distinct": "true",
"limit": limit,
}
)
count = execute_cmd("frappe.desk.reportview.get_count")
self.assertIsInstance(count, int)
self.assertLessEqual(count, limit)
def test_reportview_get(self):
user = frappe.get_doc("User", "test@example.com")
add_child_table_to_blog_post()