From a28664944b151f824b43500192a14f5872c65a04 Mon Sep 17 00:00:00 2001 From: Vitor Avila <96086495+Vitor-Avila@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:26:53 -0300 Subject: [PATCH] fix(BigQuery): Support special characters in column/metric names used in ORDER BY (#26461) --- superset/models/helpers.py | 4 +++- tests/integration_tests/db_engine_specs/bigquery_tests.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) 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