Skip to content

Commit

Permalink
update validate adhoc subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
Lily Kuang committed Mar 18, 2022
1 parent a0b16d7 commit 16f5f3d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,8 @@ def adhoc_metric_to_sqla(
elif expression_type == utils.AdhocMetricExpressionType.SQL:
tp = self.get_template_processor()
expression = tp.process_template(cast(str, metric["sqlExpression"]))
if validate_adhoc_subquery(expression):
sqla_metric = literal_column(expression)
validate_adhoc_subquery(expression)
sqla_metric = literal_column(expression)
else:
raise QueryObjectValidationError("Adhoc metric expressionType is invalid")

Expand All @@ -908,10 +908,10 @@ def adhoc_column_to_sqla(
"""
label = utils.get_column_name(col)
expression = col["sqlExpression"]
sqla_metric = None
if template_processor and expression:
expression = template_processor.process_template(expression)
validate_adhoc_subquery(expression)
if expression:
validate_adhoc_subquery(expression)
sqla_metric = literal_column(expression)
return self.make_sqla_column_compatible(sqla_metric, label)

Expand Down Expand Up @@ -1183,9 +1183,10 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
select_exprs.append(outer)
elif columns:
for selected in columns:
validate_adhoc_subquery(selected)
select_exprs.append(
columns_by_name[selected].get_sqla_col()
if selected in columns_by_name and validate_adhoc_subquery(selected)
if selected in columns_by_name
else self.make_sqla_column_compatible(literal_column(selected))
)
metrics_exprs = []
Expand Down Expand Up @@ -1393,8 +1394,8 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
db_engine_spec.allows_alias_in_select
and db_engine_spec.allows_hidden_cc_in_orderby
and col.name in [select_col.name for select_col in select_exprs]
and validate_adhoc_subquery(str(col.expression))
):
validate_adhoc_subquery(str(col.expression))
col = literal_column(col.name)
direction = asc if ascending else desc
qry = qry.order_by(direction(col))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/sqla_models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def test_adhoc_metrics_and_calc_columns(self):
{
"expressionType": AdhocMetricExpressionType.SQL,
"sqlExpression": "(SELECT (SELECT * from birth_names) "
"from test_validate_adhoc_sql)",
"from test_validate_adhoc_sql)",
"label": "adhoc_metrics",
}
],
Expand Down

0 comments on commit 16f5f3d

Please sign in to comment.