This is an automated email from the ASF dual-hosted git repository. beto pushed a commit to branch robust_has_table_query in repository https://gitbox.apache.org/repos/asf/superset.git
commit be6f7dfa99836486edbb9ebb268f517e9a02243f Author: Beto Dealmeida <[email protected]> AuthorDate: Thu May 11 17:26:22 2023 -0700 Fix lint and tests --- superset/connectors/sqla/models.py | 4 +--- superset/models/helpers.py | 10 +++++----- superset/sql_parse.py | 2 +- tests/unit_tests/sql_parse_tests.py | 4 ---- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index b0dc302920..0323fee995 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -82,10 +82,8 @@ from superset.db_engine_specs.base import BaseEngineSpec, TimestampExpression from superset.exceptions import ( ColumnNotFoundException, DatasetInvalidPermissionEvaluationException, - QueryClauseValidationException, QueryObjectValidationError, SupersetGenericDBErrorException, - SupersetSecurityException, ) from superset.jinja_context import ( BaseTemplateProcessor, @@ -102,7 +100,7 @@ from superset.models.helpers import ( QueryResult, QueryStringExtended, ) -from superset.sql_parse import ParsedQuery, sanitize_clause +from superset.sql_parse import ParsedQuery from superset.superset_typing import ( AdhocColumn, AdhocMetric, diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 7de361bad3..ac0528bd98 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -118,12 +118,12 @@ def validate_adhoc_subquery( :raise SupersetSecurityException if sql contains sub-queries or nested sub-queries with table """ - # build a proper SQL query from the expression - sql = f"SELECT {expression}" - statements = [] - for statement in sqlparse.parse(sql): - if has_table_query(str(statement), sqla_dialect): + for statement in sqlparse.parse(expression): + # build a proper SQL query from the expression for sqloxide + sql = f"SELECT {expression}" + + if has_table_query(sql, sqla_dialect): if not is_feature_enabled("ALLOW_ADHOC_SUBQUERY"): raise SupersetSecurityException( SupersetError( diff --git a/superset/sql_parse.py b/superset/sql_parse.py index e7fec3756f..4d1e9d71a0 100644 --- a/superset/sql_parse.py +++ b/superset/sql_parse.py @@ -791,7 +791,7 @@ def extract_table_references( sql_text = RE_JINJA_VAR.sub("abc", sql_text) try: tree = parse_sql(sql_text, dialect=sqloxide_dialect) - except Exception as ex: # pylint: disable=broad-except + except Exception as ex: if show_warning: logger.warning( "\nUnable to parse query with sqloxide:\n%s\n%s", sql_text, ex diff --git a/tests/unit_tests/sql_parse_tests.py b/tests/unit_tests/sql_parse_tests.py index 69440319ba..9ed919ddf0 100644 --- a/tests/unit_tests/sql_parse_tests.py +++ b/tests/unit_tests/sql_parse_tests.py @@ -1618,8 +1618,6 @@ def test_extract_table_references(mocker: MockerFixture) -> None: Table(table="table", schema=None, catalog=None), Table(table="other_table", schema=None, catalog=None), } -<<<<<<< HEAD - logger.warning.assert_not_called() def test_is_select() -> None: @@ -1637,5 +1635,3 @@ WITH t AS ( ) SELECT * FROM t""" ).is_select() -======= ->>>>>>> 748876648e (feat: robust(er) adhoc query validation)
