-
Notifications
You must be signed in to change notification settings - Fork 13.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve adhoc SQL validation #19454
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -899,7 +899,11 @@ def adhoc_metric_to_sqla( | |
elif expression_type == utils.AdhocMetricExpressionType.SQL: | ||
tp = self.get_template_processor() | ||
expression = tp.process_template(cast(str, metric["sqlExpression"])) | ||
validate_adhoc_subquery(expression) | ||
expression = validate_adhoc_subquery( | ||
expression, | ||
self.database_id, | ||
self.schema, | ||
) | ||
try: | ||
expression = sanitize_clause(expression) | ||
except QueryClauseValidationException as ex: | ||
|
@@ -928,7 +932,11 @@ def adhoc_column_to_sqla( | |
if template_processor and expression: | ||
expression = template_processor.process_template(expression) | ||
if expression: | ||
validate_adhoc_subquery(expression) | ||
expression = validate_adhoc_subquery( | ||
expression, | ||
self.database_id, | ||
self.schema, | ||
) | ||
try: | ||
expression = sanitize_clause(expression) | ||
except QueryClauseValidationException as ex: | ||
|
@@ -984,15 +992,14 @@ def is_alias_used_in_orderby(col: ColumnElement) -> bool: | |
|
||
def _get_sqla_row_level_filters( | ||
self, template_processor: BaseTemplateProcessor | ||
) -> List[str]: | ||
) -> List[TextClause]: | ||
""" | ||
Return the appropriate row level security filters for | ||
this table and the current user. | ||
|
||
:param BaseTemplateProcessor template_processor: The template | ||
processor to apply to the filters. | ||
:returns: A list of SQL clauses to be ANDed together. | ||
:rtype: List[str] | ||
""" | ||
all_filters: List[TextClause] = [] | ||
filter_groups: Dict[Union[int, str], List[TextClause]] = defaultdict(list) | ||
|
@@ -1145,6 +1152,12 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma | |
col: Union[AdhocMetric, ColumnElement] = orig_col | ||
if isinstance(col, dict): | ||
col = cast(AdhocMetric, col) | ||
if col.get("sqlExpression"): | ||
col["sqlExpression"] = validate_adhoc_subquery( | ||
col["sqlExpression"], | ||
self.database_id, | ||
self.schema, | ||
) | ||
if utils.is_adhoc_metric(col): | ||
# add adhoc sort by column to columns_by_name if not exists | ||
col = self.adhoc_metric_to_sqla(col, columns_by_name) | ||
|
@@ -1194,7 +1207,11 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma | |
elif selected in columns_by_name: | ||
outer = columns_by_name[selected].get_sqla_col() | ||
else: | ||
validate_adhoc_subquery(selected) | ||
selected = validate_adhoc_subquery( | ||
selected, | ||
self.database_id, | ||
self.schema, | ||
) | ||
outer = literal_column(f"({selected})") | ||
outer = self.make_sqla_column_compatible(outer, selected) | ||
else: | ||
|
@@ -1207,7 +1224,11 @@ 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) | ||
selected = validate_adhoc_subquery( | ||
selected, | ||
self.database_id, | ||
self.schema, | ||
) | ||
select_exprs.append( | ||
columns_by_name[selected].get_sqla_col() | ||
if selected in columns_by_name | ||
|
@@ -1420,7 +1441,6 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma | |
and db_engine_spec.allows_hidden_cc_in_orderby | ||
and col.name in [select_col.name for select_col in select_exprs] | ||
): | ||
validate_adhoc_subquery(str(col.expression)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this up, where it's still a string. |
||
col = literal_column(col.name) | ||
direction = asc if ascending else desc | ||
qry = qry.order_by(direction(col)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why
mypy
didn't catch this.