Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adhoc column in legacy chart #19234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [
ktmud marked this conversation as resolved.
Show resolved Hide resolved
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,
)
zhaoyongjie marked this conversation as resolved.
Show resolved Hide resolved
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