From 36eceb08e0f7c8b99ba8083ead6b5f31f184c9f6 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt Date: Fri, 4 Feb 2022 10:09:25 +0200 Subject: [PATCH] fix(sqla): avoid unnecessary groupby for when no metrics --- superset/charts/schemas.py | 5 ++++- superset/common/query_actions.py | 2 +- tests/integration_tests/charts/data/api_tests.py | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/superset/charts/schemas.py b/superset/charts/schemas.py index 225410eb4c9ab..b25f9712dbbd2 100644 --- a/superset/charts/schemas.py +++ b/superset/charts/schemas.py @@ -598,7 +598,10 @@ class ChartDataBoxplotOptionsSchema(ChartDataPostProcessingOperationOptionsSchem description="Aggregate expressions. Metrics can be passed as both " "references to datasource metrics (strings), or ad-hoc metrics" "which are defined only within the query object. See " - "`ChartDataAdhocMetricSchema` for the structure of ad-hoc metrics.", + "`ChartDataAdhocMetricSchema` for the structure of ad-hoc metrics. " + "When metrics is undefined or null, the query is executed without a groupby. " + "However, when metrics is an array (length >= 0), a groupby clause is added to " + "the query.", allow_none=True, ) diff --git a/superset/common/query_actions.py b/superset/common/query_actions.py index 6ed18d1958208..ecdf00d084d82 100644 --- a/superset/common/query_actions.py +++ b/superset/common/query_actions.py @@ -140,7 +140,7 @@ def _get_samples( query_obj = copy.copy(query_obj) query_obj.is_timeseries = False query_obj.orderby = [] - query_obj.metrics = [] + query_obj.metrics = None query_obj.post_processing = [] query_obj.columns = [o.column_name for o in datasource.columns] query_obj.from_dttm = None diff --git a/tests/integration_tests/charts/data/api_tests.py b/tests/integration_tests/charts/data/api_tests.py index 8d9797f67ebb2..d6ccd6aadfe54 100644 --- a/tests/integration_tests/charts/data/api_tests.py +++ b/tests/integration_tests/charts/data/api_tests.py @@ -154,6 +154,7 @@ def test_as_samples_without_row_limit__row_count_as_default_samples_row_limit(se # assert self.assert_row_count(rv, expected_row_count) + assert "GROUP BY" not in rv.json["result"][0]["query"] @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") @mock.patch( @@ -184,6 +185,7 @@ def test_as_samples_with_row_limit_bigger_then_sql_max_row__rowcount_as_sql_max_ # assert self.assert_row_count(rv, expected_row_count) + assert "GROUP BY" not in rv.json["result"][0]["query"] @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") @mock.patch( @@ -200,6 +202,7 @@ def test_with_row_limit_as_samples__rowcount_as_row_limit(self): # assert self.assert_row_count(rv, expected_row_count) + assert "GROUP BY" not in rv.json["result"][0]["query"] def test_with_incorrect_result_type__400(self): self.query_context_payload["result_type"] = "qwerty"