Skip to content

Commit

Permalink
fix(bigquery): calculated column cannot orderby in BigQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie committed Oct 22, 2021
1 parent 824e62b commit 9a3d729
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,13 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
# if engine does not allow using SELECT alias in ORDER BY
# revert to the underlying column
col = col.element

if (
db_engine_spec.allows_alias_in_select
and db_engine_spec.allows_hidden_orderby_calculated_column
and col.name in [select_col.name for select_col in select_exprs]
):
col = literal_column(col.name)
direction = asc if ascending else desc
qry = qry.order_by(direction(col))

Expand Down
6 changes: 6 additions & 0 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
# if TRUE, then it doesn't have to.
allows_hidden_ordeby_agg = True

# Whether ORDER BY clause can use sql caculated expression
# if True, use alias of select column for `order by`
# the True is safe for most database
# But for backward compatibility, set to False by default
allows_hidden_orderby_calculated_column = False

force_column_alias_quotes = False
arraysize = 0
max_column_name_length = 0
Expand Down
2 changes: 2 additions & 0 deletions superset/db_engine_specs/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class BigQueryEngineSpec(BaseEngineSpec):
# same cursor, so we need to run all statements at once
run_multiple_statements_as_one = True

allows_hidden_orderby_calculated_column = True

"""
https://www.python.org/dev/peps/pep-0249/#arraysize
raw_connections bypass the pybigquery query execution context and deal with
Expand Down

0 comments on commit 9a3d729

Please sign in to comment.