diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 0a7df8a195355..a2ae1f8330fed 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -1672,7 +1672,9 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma select_exprs.append( self.convert_tbl_column_to_sqla_col( - columns_by_name[selected], template_processor=template_processor + columns_by_name[selected], + template_processor=template_processor, + label=_column_label, ) if isinstance(selected, str) and selected in columns_by_name else self.make_sqla_column_compatible( diff --git a/tests/integration_tests/model_tests.py b/tests/integration_tests/model_tests.py index 5222c1cb34ef1..653bacd2387db 100644 --- a/tests/integration_tests/model_tests.py +++ b/tests/integration_tests/model_tests.py @@ -592,6 +592,29 @@ def test_query_with_non_existent_metrics(self): self.assertTrue("Metric 'invalid' does not exist", context.exception) + def test_query_label_without_group_by(self): + tbl = self.get_table(name="birth_names") + query_obj = dict( + groupby=[], + columns=[ + "gender", + { + "label": "Given Name", + "sqlExpression": "name", + "expressionType": "SQL", + }, + ], + filter=[], + is_timeseries=False, + granularity=None, + from_dttm=None, + to_dttm=None, + extras={}, + ) + + sql = tbl.get_query_str(query_obj) + self.assertRegex(sql, r'name AS ["`]?Given Name["`]?') + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_data_for_slices_with_no_query_context(self): tbl = self.get_table(name="birth_names")