From 91fb78bcbd67a1195eb4299d412e4f7a99bacac4 Mon Sep 17 00:00:00 2001 From: Fredrik Hyyrynen Date: Thu, 2 May 2024 16:06:27 +0200 Subject: [PATCH 1/6] Corrected documentation of attribute allows_alias_to_source_column The value is true if the engine is able to pick the source column for aggregation clauses used in ORDER BY when a column in SELECT has an alias that is the same as a source column. --- superset/db_engine_specs/README.md | 4 ++-- superset/db_engine_specs/base.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/superset/db_engine_specs/README.md b/superset/db_engine_specs/README.md index 88362f6b02d3f..6c3eea1edee33 100644 --- a/superset/db_engine_specs/README.md +++ b/superset/db_engine_specs/README.md @@ -207,7 +207,7 @@ In practice, the attribute doesn't seem to be working as of 2023-07-27. ### `allows_alias_to_source_column = True` -When this is true the database allows queries where alias can overshadow existing column names. For example, in this query: +When this is true the database allows ORDER BY statements where an alias can have the same name as the selected source column. For example, in this query: ```sql SELECT @@ -215,7 +215,7 @@ SELECT FROM some_table ORDER BY - foo -- references the alias `foo + 1`, not the column `foo` + foo -- references the source column `foo`, not the column alias `foo + 1` ``` ### `allows_hidden_orderby_agg = True` diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index 159c510fe9656..1254c6d2adfa1 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -347,7 +347,7 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods allows_sql_comments = True allows_escaped_colons = True - # Whether ORDER BY clause can use aliases created in SELECT + # Whether ORDER BY clause can handle aliases created in SELECT # that are the same as a source column allows_alias_to_source_column = True From 2281a738113d990d250a703938cd2a40cbb15fc0 Mon Sep 17 00:00:00 2001 From: Fredrik Hyyrynen Date: Thu, 2 May 2024 16:07:30 +0200 Subject: [PATCH 2/6] Corrected documentation of attribute allows_alias_in_orderby --- superset/db_engine_specs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/superset/db_engine_specs/README.md b/superset/db_engine_specs/README.md index 6c3eea1edee33..7abd5cc4ca8c5 100644 --- a/superset/db_engine_specs/README.md +++ b/superset/db_engine_specs/README.md @@ -152,7 +152,7 @@ Superset will try to use aliases whenever possible, in order to give friendly na ### `allows_alias_in_orderby = True` -Does the DB support referencing alias in the `GROUP BY`, eg: +Does the DB support referencing alias in the `ORDER BY`, eg: ```sql SELECT @@ -160,7 +160,7 @@ SELECT COUNT(*) AS cnt FROM some_table -GROUP BY +ORDER BY country ``` From 94dc526b993c9ecec2b6fab99ac0195340563c60 Mon Sep 17 00:00:00 2001 From: Fredrik Hyyrynen Date: Thu, 2 May 2024 16:15:25 +0200 Subject: [PATCH 3/6] Rename aliases if used in GROUP BY in engines engine without support * Renamed attribute allows_alias_to_source_column to order_by_allows_alias_to_source_column * Added attribute group_by_allows_alias_to_source_column * Rename aliases for source columns if used in GROUP BY and if group_by_allows_alias_to_source_column is false --- superset/connectors/sqla/models.py | 32 +++++++++++++++++++++++++++++- superset/db_engine_specs/README.md | 15 +++++++++++++- superset/db_engine_specs/base.py | 29 ++++++++++++++++++--------- superset/db_engine_specs/drill.py | 2 ++ superset/db_engine_specs/hive.py | 2 +- superset/db_engine_specs/lib.py | 3 ++- superset/db_engine_specs/presto.py | 3 ++- superset/db_engine_specs/trino.py | 3 ++- superset/models/helpers.py | 32 +++++++++++++++++++++++++++++- 9 files changed, 105 insertions(+), 16 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index c38a0085a534b..354e2a67330f8 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -1585,7 +1585,7 @@ def make_orderby_compatible( the same as a source column. In this case, we update the SELECT alias to another name to avoid the conflict. """ - if self.db_engine_spec.allows_alias_to_source_column: + if self.db_engine_spec.order_by_allows_alias_to_source_column: return def is_alias_used_in_orderby(col: ColumnElement) -> bool: @@ -1602,6 +1602,36 @@ def is_alias_used_in_orderby(col: ColumnElement) -> bool: if is_alias_used_in_orderby(col): col.name = f"{col.name}__" + def make_groupby_compatible( + self, select_exprs: list[ColumnElement], groupby_exprs: list[ColumnElement] + ) -> None: + """ + If needed, make sure aliases for selected columns are not used in + `GROUP BY`. + + In some databases (e.g. Drill), `GROUP BY` clause is not able to + automatically pick the source column if a `SELECT` clause alias is named + the same as a source column. In this case, we update the SELECT alias to + another name to avoid the conflict. + """ + if self.db_engine_spec.group_by_allows_alias_to_source_column: + return + + def is_alias_used_in_groupby(col: ColumnElement) -> bool: + if not isinstance(col, Label): + return False + regexp = re.compile(f"\\(.*\\b{re.escape(col.name)}\\b.*\\)", re.IGNORECASE) + return any(regexp.search(str(x)) for x in groupby_exprs) + + # Iterate through selected columns, if column alias appears in groupby + # use another `alias`. The final output columns will still use the + # original names, because they are updated by `labels_expected` after + # querying. + for col in select_exprs: + if is_alias_used_in_groupby(col): + col.name = f"{col.name}__" + + def text(self, clause: str) -> TextClause: return self.db_engine_spec.get_text_clause(clause) diff --git a/superset/db_engine_specs/README.md b/superset/db_engine_specs/README.md index 7abd5cc4ca8c5..bd2a1f8aeedeb 100644 --- a/superset/db_engine_specs/README.md +++ b/superset/db_engine_specs/README.md @@ -205,7 +205,7 @@ LIMIT 10000; In practice, the attribute doesn't seem to be working as of 2023-07-27. -### `allows_alias_to_source_column = True` +### `order_by_allows_alias_to_source_column = True` When this is true the database allows ORDER BY statements where an alias can have the same name as the selected source column. For example, in this query: @@ -218,6 +218,19 @@ ORDER BY foo -- references the source column `foo`, not the column alias `foo + 1` ``` +### `group_by_allows_alias_to_source_column = True` + +When this is true the database allows GROUP BY statements where an alias can have the same name as the selected source column. For example, in this query: + +```sql +SELECT + foo + 1 AS foo +FROM + some_table +GROUP BY + foo -- references the source column `foo`, not the column alias `foo + 1` +``` + ### `allows_hidden_orderby_agg = True` If set to true the database allows expressions in the `GROUP BY` that are not present in the projection (`SELECT`), eg: diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index 1254c6d2adfa1..3576f60fc9ecf 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -185,14 +185,21 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods """Abstract class for database engine specific configurations Attributes: - allows_alias_to_source_column: Whether the engine is able to pick the - source column for aggregation clauses - used in ORDER BY when a column in SELECT - has an alias that is the same as a source - column. - allows_hidden_orderby_agg: Whether the engine allows ORDER BY to - directly use aggregation clauses, without - having to add the same aggregation in SELECT. + order_by_allows_alias_to_source_column: Whether the engine is able to pick the + source column for aggregation clauses + used in ORDER BY when a column in SELECT + has an alias that is the same as a source + column. + + group_by_allows_alias_to_source_column: Whether the engine is able to pick the + source column for aggregation clauses + used in GROUP BY when a column in SELECT + has an alias that is the same as a source + column. + + allows_hidden_orderby_agg: Whether the engine allows ORDER BY to + directly use aggregation clauses, without + having to add the same aggregation in SELECT. """ engine_name: str | None = None # for user messages, overridden in child classes @@ -349,7 +356,11 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods # Whether ORDER BY clause can handle aliases created in SELECT # that are the same as a source column - allows_alias_to_source_column = True + order_by_allows_alias_to_source_column = True + + # Whether GROUP BY clause can handle aliases created in SELECT + # that are the same as a source column + group_by_allows_alias_to_source_column = True # Whether ORDER BY clause must appear in SELECT # if True, then it doesn't have to. diff --git a/superset/db_engine_specs/drill.py b/superset/db_engine_specs/drill.py index e99d4a27f4a6e..8583e2dee0c24 100644 --- a/superset/db_engine_specs/drill.py +++ b/superset/db_engine_specs/drill.py @@ -35,6 +35,8 @@ class DrillEngineSpec(BaseEngineSpec): engine = "drill" engine_name = "Apache Drill" default_driver = "sadrill" + order_by_allows_alias_to_source_column = True + group_by_allows_alias_to_source_column = False supports_dynamic_schema = True diff --git a/superset/db_engine_specs/hive.py b/superset/db_engine_specs/hive.py index e3cf128b7a2c6..3ad34c36651e7 100644 --- a/superset/db_engine_specs/hive.py +++ b/superset/db_engine_specs/hive.py @@ -101,7 +101,7 @@ class HiveEngineSpec(PrestoEngineSpec): engine = "hive" engine_name = "Apache Hive" max_column_name_length = 767 - allows_alias_to_source_column = True + order_by_allows_alias_to_source_column = True allows_hidden_orderby_agg = False supports_dynamic_schema = True diff --git a/superset/db_engine_specs/lib.py b/superset/db_engine_specs/lib.py index 398d5af64741c..85cc2e82926c4 100644 --- a/superset/db_engine_specs/lib.py +++ b/superset/db_engine_specs/lib.py @@ -125,7 +125,8 @@ def diagnose(spec: type[BaseEngineSpec]) -> dict[str, Any]: "alias_in_select": spec.allows_alias_in_select, "alias_in_orderby": spec.allows_alias_in_orderby, "time_groupby_inline": spec.time_groupby_inline, - "alias_to_source_column": not spec.allows_alias_to_source_column, + "order_by_alias_to_source_column": not spec.order_by_allows_alias_to_source_column, + "group_by_alias_to_source_column": not spec.group_by_allows_alias_to_source_column, "order_by_not_in_select": spec.allows_hidden_orderby_agg, "expressions_in_orderby": spec.allows_hidden_cc_in_orderby, "cte_in_subquery": spec.allows_cte_in_subquery, diff --git a/superset/db_engine_specs/presto.py b/superset/db_engine_specs/presto.py index fbd0eff484474..a28ca2d47765b 100644 --- a/superset/db_engine_specs/presto.py +++ b/superset/db_engine_specs/presto.py @@ -676,7 +676,8 @@ def latest_sub_partition( class PrestoEngineSpec(PrestoBaseEngineSpec): engine = "presto" engine_name = "Presto" - allows_alias_to_source_column = False + order_by_allows_alias_to_source_column = False + group_by_allows_alias_to_source_column = False custom_errors: dict[Pattern[str], tuple[str, SupersetErrorType, dict[str, Any]]] = { COLUMN_DOES_NOT_EXIST_REGEX: ( diff --git a/superset/db_engine_specs/trino.py b/superset/db_engine_specs/trino.py index 143276bdc3dca..6639ee237d8bb 100644 --- a/superset/db_engine_specs/trino.py +++ b/superset/db_engine_specs/trino.py @@ -63,7 +63,8 @@ class TrinoEngineSpec(PrestoBaseEngineSpec): engine = "trino" engine_name = "Trino" - allows_alias_to_source_column = False + order_by_allows_alias_to_source_column = False + group_by_allows_alias_to_source_column = False @classmethod def get_extra_table_metadata( diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 7b211f98b11aa..ec51c25d8c13b 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -984,7 +984,7 @@ def make_orderby_compatible( the same as a source column. In this case, we update the SELECT alias to another name to avoid the conflict. """ - if self.db_engine_spec.allows_alias_to_source_column: + if self.db_engine_spec.order_by_allows_alias_to_source_column: return def is_alias_used_in_orderby(col: ColumnElement) -> bool: @@ -1001,6 +1001,35 @@ def is_alias_used_in_orderby(col: ColumnElement) -> bool: if is_alias_used_in_orderby(col): col.name = f"{col.name}__" + def make_groupby_compatible( + self, select_exprs: list[ColumnElement], groupby_exprs: list[ColumnElement] + ) -> None: + """ + If needed, make sure aliases for selected columns are not used in + `GROUP BY`. + + In some databases (e.g. Drill), `GROUP BY` clause is not able to + automatically pick the source column if a `SELECT` clause alias is named + the same as a source column. In this case, we update the SELECT alias to + another name to avoid the conflict. + """ + if self.db_engine_spec.group_by_allows_alias_to_source_column: + return + + def is_alias_used_in_groupby(col: ColumnElement) -> bool: + if not isinstance(col, Label): + return False + regexp = re.compile(f"\\(.*\\b{re.escape(col.name)}\\b.*\\)", re.IGNORECASE) + return any(regexp.search(str(x)) for x in groupby_exprs) + + # Iterate through selected columns, if column alias appears in groupby + # use another `alias`. The final output columns will still use the + # original names, because they are updated by `labels_expected` after + # querying. + for col in select_exprs: + if is_alias_used_in_groupby(col): + col.name = f"{col.name}__" + def exc_query(self, qry: Any) -> QueryResult: qry_start_dttm = datetime.now() query_str_ext = self.get_query_str_extended(qry) @@ -1753,6 +1782,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma tbl, cte = self.get_from_clause(template_processor) if groupby_all_columns: + self.make_groupby_compatible(select_exprs, groupby_all_columns.values()) qry = qry.group_by(*groupby_all_columns.values()) where_clause_and = [] From 3dd2ec8f84d1cea4aec51c13b2359595f7117a1e Mon Sep 17 00:00:00 2001 From: Fredrik Hyyrynen Date: Sun, 30 Jun 2024 16:13:29 +0200 Subject: [PATCH 4/6] Removed trailing whitespace --- superset/db_engine_specs/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index 3576f60fc9ecf..018992149eedd 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -196,7 +196,7 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods used in GROUP BY when a column in SELECT has an alias that is the same as a source column. - + allows_hidden_orderby_agg: Whether the engine allows ORDER BY to directly use aggregation clauses, without having to add the same aggregation in SELECT. From aaf72cdb47dc18b393a9aa82f7838d8b54d6f6db Mon Sep 17 00:00:00 2001 From: Fredrik Hyyrynen Date: Mon, 1 Jul 2024 11:22:52 +0200 Subject: [PATCH 5/6] Corrected type of input for groupby expressions --- superset/models/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset/models/helpers.py b/superset/models/helpers.py index ec51c25d8c13b..43de9fa9c5576 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -1782,7 +1782,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma tbl, cte = self.get_from_clause(template_processor) if groupby_all_columns: - self.make_groupby_compatible(select_exprs, groupby_all_columns.values()) + self.make_groupby_compatible(select_exprs, list(groupby_all_columns.values())) qry = qry.group_by(*groupby_all_columns.values()) where_clause_and = [] From 627c2ee35ee1c11e40e872d465dcd5f1cb4db80f Mon Sep 17 00:00:00 2001 From: Fredrik Hyyrynen Date: Mon, 1 Jul 2024 12:01:08 +0200 Subject: [PATCH 6/6] Shortened the name and inverted meaning of config parameters. The new name is shorter but also more true to the actual usage. Updated documentation and implementation. --- superset/connectors/sqla/models.py | 5 ++-- superset/db_engine_specs/README.md | 8 +++--- superset/db_engine_specs/base.py | 40 ++++++++++++++---------------- superset/db_engine_specs/drill.py | 4 +-- superset/db_engine_specs/hive.py | 2 +- superset/db_engine_specs/lib.py | 4 +-- superset/db_engine_specs/presto.py | 4 +-- superset/db_engine_specs/trino.py | 4 +-- superset/models/helpers.py | 8 +++--- 9 files changed, 39 insertions(+), 40 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 354e2a67330f8..e40752b11de79 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -1585,7 +1585,7 @@ def make_orderby_compatible( the same as a source column. In this case, we update the SELECT alias to another name to avoid the conflict. """ - if self.db_engine_spec.order_by_allows_alias_to_source_column: + if not self.db_engine_spec.order_by_require_unique_alias: return def is_alias_used_in_orderby(col: ColumnElement) -> bool: @@ -1614,7 +1614,7 @@ def make_groupby_compatible( the same as a source column. In this case, we update the SELECT alias to another name to avoid the conflict. """ - if self.db_engine_spec.group_by_allows_alias_to_source_column: + if not self.db_engine_spec.group_by_require_unique_alias: return def is_alias_used_in_groupby(col: ColumnElement) -> bool: @@ -1631,7 +1631,6 @@ def is_alias_used_in_groupby(col: ColumnElement) -> bool: if is_alias_used_in_groupby(col): col.name = f"{col.name}__" - def text(self, clause: str) -> TextClause: return self.db_engine_spec.get_text_clause(clause) diff --git a/superset/db_engine_specs/README.md b/superset/db_engine_specs/README.md index bd2a1f8aeedeb..9fa038c35d759 100644 --- a/superset/db_engine_specs/README.md +++ b/superset/db_engine_specs/README.md @@ -205,9 +205,9 @@ LIMIT 10000; In practice, the attribute doesn't seem to be working as of 2023-07-27. -### `order_by_allows_alias_to_source_column = True` +### `order_by_require_unique_alias = True` -When this is true the database allows ORDER BY statements where an alias can have the same name as the selected source column. For example, in this query: +When this is true the database requires aliases to have a different name than the selected source column when used in ORDER BY statements. This example query will not work as the alias has the same name: ```sql SELECT @@ -218,9 +218,9 @@ ORDER BY foo -- references the source column `foo`, not the column alias `foo + 1` ``` -### `group_by_allows_alias_to_source_column = True` +### `group_by_require_unique_alias = True` -When this is true the database allows GROUP BY statements where an alias can have the same name as the selected source column. For example, in this query: +When this is true the database requires aliases to have a different name than the selected source column when used in GROUP BY statements. This example query will not work as the alias has the same name: ```sql SELECT diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index 018992149eedd..571f77a4619f0 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -185,21 +185,19 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods """Abstract class for database engine specific configurations Attributes: - order_by_allows_alias_to_source_column: Whether the engine is able to pick the - source column for aggregation clauses - used in ORDER BY when a column in SELECT - has an alias that is the same as a source - column. - - group_by_allows_alias_to_source_column: Whether the engine is able to pick the - source column for aggregation clauses - used in GROUP BY when a column in SELECT - has an alias that is the same as a source - column. - - allows_hidden_orderby_agg: Whether the engine allows ORDER BY to - directly use aggregation clauses, without - having to add the same aggregation in SELECT. + order_by_require_unique_alias: Whether the engine requires an alias of a + column in SELECT to be unique as it otherwise + is not able to pick the source column for + aggregation clauses in ORDER BY. + + group_by_require_unique_alias: Whether the engine requires an alias of a + column in SELECT to be unique as it otheriwse + is not able to pick the source column for + aggregation clauses in GROUP BY. + + allows_hidden_orderby_agg: Whether the engine allows ORDER BY to + directly use aggregation clauses, without + having to add the same aggregation in SELECT. """ engine_name: str | None = None # for user messages, overridden in child classes @@ -354,13 +352,13 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods allows_sql_comments = True allows_escaped_colons = True - # Whether ORDER BY clause can handle aliases created in SELECT - # that are the same as a source column - order_by_allows_alias_to_source_column = True + # Whether ORDER BY clause requires aliases create in SELECT + # to have a unique name. + order_by_require_unique_alias = False - # Whether GROUP BY clause can handle aliases created in SELECT - # that are the same as a source column - group_by_allows_alias_to_source_column = True + # Whether GROUP BY clause requires aliases create in SELECT + # to have a unique name. + group_by_require_unique_alias = False # Whether ORDER BY clause must appear in SELECT # if True, then it doesn't have to. diff --git a/superset/db_engine_specs/drill.py b/superset/db_engine_specs/drill.py index 8583e2dee0c24..46ba4775564b9 100644 --- a/superset/db_engine_specs/drill.py +++ b/superset/db_engine_specs/drill.py @@ -35,8 +35,8 @@ class DrillEngineSpec(BaseEngineSpec): engine = "drill" engine_name = "Apache Drill" default_driver = "sadrill" - order_by_allows_alias_to_source_column = True - group_by_allows_alias_to_source_column = False + order_by_require_unique_alias = False + group_by_require_unique_alias = True supports_dynamic_schema = True diff --git a/superset/db_engine_specs/hive.py b/superset/db_engine_specs/hive.py index 3ad34c36651e7..bc1346b182ce9 100644 --- a/superset/db_engine_specs/hive.py +++ b/superset/db_engine_specs/hive.py @@ -101,7 +101,7 @@ class HiveEngineSpec(PrestoEngineSpec): engine = "hive" engine_name = "Apache Hive" max_column_name_length = 767 - order_by_allows_alias_to_source_column = True + order_by_require_unique_alias = False allows_hidden_orderby_agg = False supports_dynamic_schema = True diff --git a/superset/db_engine_specs/lib.py b/superset/db_engine_specs/lib.py index 85cc2e82926c4..2c8fcb7d93e58 100644 --- a/superset/db_engine_specs/lib.py +++ b/superset/db_engine_specs/lib.py @@ -125,8 +125,8 @@ def diagnose(spec: type[BaseEngineSpec]) -> dict[str, Any]: "alias_in_select": spec.allows_alias_in_select, "alias_in_orderby": spec.allows_alias_in_orderby, "time_groupby_inline": spec.time_groupby_inline, - "order_by_alias_to_source_column": not spec.order_by_allows_alias_to_source_column, - "group_by_alias_to_source_column": not spec.group_by_allows_alias_to_source_column, + "order_by_require_unique_alias": not spec.order_by_require_unique_alias, + "group_by_require_unique_alias": not spec.group_by_require_unique_alias, "order_by_not_in_select": spec.allows_hidden_orderby_agg, "expressions_in_orderby": spec.allows_hidden_cc_in_orderby, "cte_in_subquery": spec.allows_cte_in_subquery, diff --git a/superset/db_engine_specs/presto.py b/superset/db_engine_specs/presto.py index a28ca2d47765b..4e201280cc54c 100644 --- a/superset/db_engine_specs/presto.py +++ b/superset/db_engine_specs/presto.py @@ -676,8 +676,8 @@ def latest_sub_partition( class PrestoEngineSpec(PrestoBaseEngineSpec): engine = "presto" engine_name = "Presto" - order_by_allows_alias_to_source_column = False - group_by_allows_alias_to_source_column = False + order_by_require_unique_alias = True + group_by_require_unique_alias = True custom_errors: dict[Pattern[str], tuple[str, SupersetErrorType, dict[str, Any]]] = { COLUMN_DOES_NOT_EXIST_REGEX: ( diff --git a/superset/db_engine_specs/trino.py b/superset/db_engine_specs/trino.py index 6639ee237d8bb..d825b0b3e9369 100644 --- a/superset/db_engine_specs/trino.py +++ b/superset/db_engine_specs/trino.py @@ -63,8 +63,8 @@ class TrinoEngineSpec(PrestoBaseEngineSpec): engine = "trino" engine_name = "Trino" - order_by_allows_alias_to_source_column = False - group_by_allows_alias_to_source_column = False + order_by_require_unique_alias = True + group_by_require_unique_alias = True @classmethod def get_extra_table_metadata( diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 43de9fa9c5576..ecf73ee42bad2 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -984,7 +984,7 @@ def make_orderby_compatible( the same as a source column. In this case, we update the SELECT alias to another name to avoid the conflict. """ - if self.db_engine_spec.order_by_allows_alias_to_source_column: + if not self.db_engine_spec.order_by_require_unique_alias: return def is_alias_used_in_orderby(col: ColumnElement) -> bool: @@ -1013,7 +1013,7 @@ def make_groupby_compatible( the same as a source column. In this case, we update the SELECT alias to another name to avoid the conflict. """ - if self.db_engine_spec.group_by_allows_alias_to_source_column: + if not self.db_engine_spec.group_by_require_unique_alias: return def is_alias_used_in_groupby(col: ColumnElement) -> bool: @@ -1782,7 +1782,9 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma tbl, cte = self.get_from_clause(template_processor) if groupby_all_columns: - self.make_groupby_compatible(select_exprs, list(groupby_all_columns.values())) + self.make_groupby_compatible( + select_exprs, list(groupby_all_columns.values()) + ) qry = qry.group_by(*groupby_all_columns.values()) where_clause_and = []