Skip to content

Commit

Permalink
fix: adhoc column in legacy chart (apache#19234)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie authored Mar 18, 2022
1 parent f629154 commit b5e9fad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
9 changes: 6 additions & 3 deletions superset/connectors/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/integration_tests/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b5e9fad

Please sign in to comment.