fix: adhoc column in legacy chart (#19234)

This commit is contained in:
Yongjie Zhao 2022-03-18 14:05:30 +08:00 committed by GitHub
parent f6291545fb
commit b5e9fad11a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 3 deletions

View File

@ -339,11 +339,14 @@ class BaseDatasource(
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

View File

@ -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 @@ class TestSqlaTableModel(SupersetTestCase):
"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()