betodealmeida commented on code in PR #30202: URL: https://github.com/apache/superset/pull/30202#discussion_r1751842269
########## superset/sql_parse.py: ########## @@ -1177,46 +1106,31 @@ class InsertRLSState(StrEnum): FOUND_TABLE = "FOUND_TABLE" -def has_table_query(token_list: TokenList) -> bool: +def has_table_query(expression: str, engine: str) -> bool: """ Return if a statement has a query reading from a table. - >>> has_table_query(sqlparse.parse("COUNT(*)")[0]) + >>> has_table_query("COUNT(*)", "postgresql") False - >>> has_table_query(sqlparse.parse("SELECT * FROM table")[0]) + >>> has_table_query("SELECT * FROM table", "postgresql") True Note that queries reading from constant values return false: - >>> has_table_query(sqlparse.parse("SELECT * FROM (SELECT 1)")[0]) + >>> has_table_query("SELECT * FROM (SELECT 1)", "postgresql") False """ - state = InsertRLSState.SCANNING - for token in token_list.tokens: - # Ignore comments - if isinstance(token, sqlparse.sql.Comment): - continue + # Remove trailing semicolon. + expression = expression.strip().rstrip(";") - # Recurse into child token list - if isinstance(token, TokenList) and has_table_query(token): - return True - - # Found a source keyword (FROM/JOIN) - if imt(token, m=[(Keyword, "FROM"), (Keyword, "JOIN")]): - state = InsertRLSState.SEEN_SOURCE - - # Found identifier/keyword after FROM/JOIN - elif state == InsertRLSState.SEEN_SOURCE and ( - isinstance(token, sqlparse.sql.Identifier) or token.ttype == Keyword - ): - return True - - # Found nothing, leaving source - elif state == InsertRLSState.SEEN_SOURCE and token.ttype != Whitespace: - state = InsertRLSState.SCANNING Review Comment: No, confusingly here we're just using the `InsertRLSState` enum do to the table scan. (I also have a PR almost ready where I move all the RLS functions to `sqlglot`.) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org