From 776ae4a629d59b5de507d24a1ac4d51344697296 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Tue, 26 Sep 2023 15:22:08 -0700 Subject: [PATCH] Clean up code --- superset/connectors/sqla/utils.py | 37 +------------------------------ superset/models/helpers.py | 37 ------------------------------- superset/sql_lab.py | 2 +- superset/sql_parse.py | 1 - 4 files changed, 2 insertions(+), 75 deletions(-) diff --git a/superset/connectors/sqla/utils.py b/superset/connectors/sqla/utils.py index fdcc2da757be6..66594084c82d5 100644 --- a/superset/connectors/sqla/utils.py +++ b/superset/connectors/sqla/utils.py @@ -38,7 +38,7 @@ ) from superset.models.core import Database from superset.result_set import SupersetResultSet -from superset.sql_parse import has_table_query, insert_rls_in_predicate, ParsedQuery +from superset.sql_parse import ParsedQuery from superset.superset_typing import ResultSetColumnType if TYPE_CHECKING: @@ -153,41 +153,6 @@ def get_columns_description( raise SupersetGenericDBErrorException(message=str(ex)) from ex -def validate_adhoc_subquery( - sql: str, - database_id: int, - default_schema: str, -) -> str: - """ - Check if adhoc SQL contains sub-queries or nested sub-queries with table. - - If sub-queries are allowed, the adhoc SQL is modified to insert any applicable RLS - predicates to it. - - :param sql: adhoc sql expression - :raise SupersetSecurityException if sql contains sub-queries or - nested sub-queries with table - """ - # pylint: disable=import-outside-toplevel - from superset import is_feature_enabled - - statements = [] - for statement in sqlparse.parse(sql): - if has_table_query(statement): - if not is_feature_enabled("ALLOW_ADHOC_SUBQUERY"): - raise SupersetSecurityException( - SupersetError( - error_type=SupersetErrorType.ADHOC_SUBQUERY_NOT_ALLOWED_ERROR, - message=_("Custom SQL fields cannot contain sub-queries."), - level=ErrorLevel.ERROR, - ) - ) - statement = insert_rls_in_predicate(statement, database_id, default_schema) - statements.append(statement) - - return ";\n".join(str(statement) for statement in statements) - - @lru_cache(maxsize=LRU_CACHE_MAX_SIZE) def get_dialect_name(drivername: str) -> str: return SqlaURL.create(drivername).get_dialect().name diff --git a/superset/models/helpers.py b/superset/models/helpers.py index caf47b6667c0e..f846c19ec8ddb 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -901,43 +901,6 @@ def _apply_cte(sql: str, cte: Optional[str]) -> str: sql = f"{cte}\n{sql}" return sql - @staticmethod - def validate_adhoc_subquery( - sql: str, - database_id: int, - default_schema: str, - ) -> str: - """ - Check if adhoc SQL contains sub-queries or nested sub-queries with table. - - If sub-queries are allowed, the adhoc SQL is modified to insert any applicable RLS - predicates to it. - - :param sql: adhoc sql expression - :raise SupersetSecurityException if sql contains sub-queries or - nested sub-queries with table - """ - - statements = [] - for statement in sqlparse.parse(sql): - if has_table_query(statement): - if not is_feature_enabled("ALLOW_ADHOC_SUBQUERY"): - raise SupersetSecurityException( - SupersetError( - error_type=SupersetErrorType.ADHOC_SUBQUERY_NOT_ALLOWED_ERROR, - message=_("Custom SQL fields cannot contain sub-queries."), - level=ErrorLevel.ERROR, - ) - ) - statement = insert_rls_in_predicate( - statement, - database_id, - default_schema, - ) - statements.append(statement) - - return ";\n".join(str(statement) for statement in statements) - def get_query_str_extended( self, query_obj: QueryObjectDict, mutate: bool = True ) -> QueryStringExtended: diff --git a/superset/sql_lab.py b/superset/sql_lab.py index 6db97d45d9960..efbef6560a366 100644 --- a/superset/sql_lab.py +++ b/superset/sql_lab.py @@ -196,7 +196,7 @@ def get_sql_results( # pylint: disable=too-many-arguments return handle_query_error(ex, query, session) -def execute_sql_statement( # pylint: disable=too-many-arguments +def execute_sql_statement( # pylint: disable=too-many-arguments, too-many-locals sql_statement: str, query: Query, session: Session, diff --git a/superset/sql_parse.py b/superset/sql_parse.py index 767f075490308..cecd673276976 100644 --- a/superset/sql_parse.py +++ b/superset/sql_parse.py @@ -775,7 +775,6 @@ def insert_rls_in_predicate( rls: Optional[TokenList] = None state = InsertRLSState.SCANNING for token in token_list.tokens: - # Recurse into child token list if isinstance(token, TokenList): i = token_list.tokens.index(token)