From b5e9fad11a2146c471c7f27d88cc425d928f94dd Mon Sep 17 00:00:00 2001 From: Yongjie Zhao Date: Fri, 18 Mar 2022 14:05:30 +0800 Subject: [PATCH] fix: adhoc column in legacy chart (#19234) --- superset/connectors/base/models.py | 9 ++++--- tests/integration_tests/model_tests.py | 34 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/superset/connectors/base/models.py b/superset/connectors/base/models.py index 5cf2a8719bf9..939a1fc1c7c2 100644 --- a/superset/connectors/base/models.py +++ b/superset/connectors/base/models.py @@ -339,11 +339,14 @@ def data_for_slices( # pylint: disable=too-many-locals or [] ) else: - column_names.update( - column + _columns = [ + utils.get_column_name(column) + if utils.is_adhoc_column(column) + else column for column_param in COLUMN_FORM_DATA_PARAMS for column in utils.get_iterable(form_data.get(column_param) or []) - ) + ] + column_names.update(_columns) filtered_metrics = [ metric diff --git a/tests/integration_tests/model_tests.py b/tests/integration_tests/model_tests.py index 6371a06123ae..5ffa65e583ea 100644 --- a/tests/integration_tests/model_tests.py +++ b/tests/integration_tests/model_tests.py @@ -15,10 +15,12 @@ # specific language governing permissions and limitations # under the License. # isort:skip_file +import json import textwrap import unittest from unittest import mock +from superset.connectors.sqla.models import SqlaTable from superset.exceptions import SupersetException from tests.integration_tests.fixtures.birth_names_dashboard import ( load_birth_names_dashboard_with_slices, @@ -578,6 +580,38 @@ def test_data_for_slices_with_query_context(self): "state", } + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") + def test_data_for_slices_with_adhoc_column(self): + # should perform sqla.model.BaseDatasource.data_for_slices() with adhoc + # column and legacy chart + tbl = self.get_table(name="birth_names") + dashboard = self.get_dash_by_slug("births") + slc = Slice( + slice_name="slice with adhoc column", + datasource_type="table", + viz_type="table", + params=json.dumps( + { + "adhoc_filters": [], + "granularity_sqla": "ds", + "groupby": [ + "name", + {"label": "adhoc_column", "sqlExpression": "name"}, + ], + "metrics": ["sum__num"], + "time_range": "No filter", + "viz_type": "table", + } + ), + datasource_id=tbl.id, + ) + dashboard.slices.append(slc) + datasource_info = slc.datasource.data_for_slices([slc]) + assert "database" in datasource_info + + # clean up and auto commit + metadata_db.session.delete(slc) + def test_literal_dttm_type_factory(): orig_type = DateTime()