diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 9c8e83147e9ed..fa2c9b8102136 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -1972,7 +1972,9 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma and db_engine_spec.allows_hidden_cc_in_orderby and col.name in [select_col.name for select_col in select_exprs] ): - col = literal_column(col.name) + with self.database.get_sqla_engine_with_context() as engine: + quote = engine.dialect.identifier_preparer.quote + col = literal_column(quote(col.name)) direction = sa.asc if ascending else sa.desc qry = qry.order_by(direction(col)) diff --git a/tests/integration_tests/db_engine_specs/bigquery_tests.py b/tests/integration_tests/db_engine_specs/bigquery_tests.py index c4f04584fa2bb..3523e71b0b022 100644 --- a/tests/integration_tests/db_engine_specs/bigquery_tests.py +++ b/tests/integration_tests/db_engine_specs/bigquery_tests.py @@ -381,4 +381,4 @@ def test_calculated_column_in_order_by(self): "orderby": [["gender_cc", True]], } sql = table.get_query_str(query_obj) - assert "ORDER BY gender_cc ASC" in sql + assert "ORDER BY `gender_cc` ASC" in sql