From 8a7beebf30f30cc955d67218b1ee87bc69f26a54 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 18 Mar 2024 19:08:29 +0530 Subject: [PATCH] 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__. --- frappe/model/db_query.py | 2 ++ frappe/public/js/frappe/list/list_view.js | 2 +- frappe/tests/test_db_query.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/frappe/model/db_query.py b/frappe/model/db_query.py index 6107431ba0..18c3d2782c 100644 --- a/frappe/model/db_query.py +++ b/frappe/model/db_query.py @@ -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 ( diff --git a/frappe/public/js/frappe/list/list_view.js b/frappe/public/js/frappe/list/list_view.js index 42ccc0e1f7..f20e2d351a 100644 --- a/frappe/public/js/frappe/list/list_view.js +++ b/frappe/public/js/frappe/list/list_view.js @@ -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, ]); } diff --git a/frappe/tests/test_db_query.py b/frappe/tests/test_db_query.py index f80f73c410..8cd233431a 100644 --- a/frappe/tests/test_db_query.py +++ b/frappe/tests/test_db_query.py @@ -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()