zhaoyongjie commented on a change in pull request #13739:
URL: https://github.com/apache/superset/pull/13739#discussion_r605344811
##########
File path: superset/connectors/sqla/models.py
##########
@@ -882,6 +867,51 @@ def adhoc_metric_to_sqla(
return self.make_sqla_column_compatible(sqla_metric, label)
+ def make_sqla_column_compatible(
+ self, sqla_col: Column, label: Optional[str] = None
+ ) -> Column:
+ """Takes a sqlalchemy column object and adds label info if supported
by engine.
+ :param sqla_col: sqlalchemy column instance
+ :param label: alias/label that column is expected to have
+ :return: either a sql alchemy column or label instance if supported by
engine
+ """
+ label_expected = label or sqla_col.name
+ db_engine_spec = self.database.db_engine_spec
+ # add quotes to tables
+ if db_engine_spec.allows_alias_in_select:
+ label = db_engine_spec.make_label_compatible(label_expected)
+ sqla_col = sqla_col.label(label)
+ return sqla_col
+
+ def make_orderby_compatible(
+ self, select_exprs: List[ColumnElement], orderby_exprs:
List[ColumnElement]
+ ) -> None:
+ """
+ If needed, make sure aliases for selected columns are not used in
+ `ORDER BY`.
+
+ In some databases (e.g. Presto), `ORDER 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.database.db_engine_spec.allows_alias_to_source_column:
Review comment:
I recommend making this change by default.
for instance, this SQL in Postgres. the 'ORDER BY' clause still refer to the
original `score` column. We should try to avoid the same `Column Name` and
`Column Alias`
```
SELECT name, sum(score) as score
FROM (
SELECT 'a' as name, 4 as score
UNION ALL
SELECT 'b', 5
UNION ALL
SELECT 'a', 4
) t
GROUP BY name
ORDER by max(score) desc
```

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]