From a2c78c149521e0db3cbb73191790cd59c8b83ad5 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) (cherry picked from commit 4592dd13fa7fdae6d8d8c153f42d47447f5319ef) --- 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 b22768bfd67..bf33451e34c 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -1965,7 +1965,9 @@ class ExploreMixin: # pylint: disable=too-many-public-methods 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 c4f04584fa2..3523e71b0b0 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 @@ class TestBigQueryDbEngineSpec(TestDbEngineSpec): "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