From 773bd9d0827fa78f86814e7bd948bc94cc9ba604 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Sat, 29 Apr 2023 18:57:45 -0700 Subject: [PATCH] fix: handle comments in has_table_query --- superset/sql_parse.py | 4 ++++ tests/unit_tests/sql_parse_tests.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/superset/sql_parse.py b/superset/sql_parse.py index 03c9926a44a55..323fabb3d3c1f 100644 --- a/superset/sql_parse.py +++ b/superset/sql_parse.py @@ -509,6 +509,10 @@ def has_table_query(token_list: TokenList) -> bool: """ state = InsertRLSState.SCANNING for token in token_list.tokens: + # Ignore comments + if isinstance(token, sqlparse.sql.Comment): + continue + # Recurse into child token list if isinstance(token, TokenList) and has_table_query(token): return True diff --git a/tests/unit_tests/sql_parse_tests.py b/tests/unit_tests/sql_parse_tests.py index f6d63cf465236..cfe6e213b297e 100644 --- a/tests/unit_tests/sql_parse_tests.py +++ b/tests/unit_tests/sql_parse_tests.py @@ -1217,6 +1217,14 @@ def test_sqlparse_issue_652(): ("extract(HOUR from from_unixtime(hour_ts)", False), ("(SELECT * FROM table)", True), ("(SELECT COUNT(DISTINCT name) from birth_names)", True), + ( + "(SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%user%' LIMIT 1)", + True, + ), + ( + "(SELECT table_name FROM /**/ information_schema.tables WHERE table_name LIKE '%user%' LIMIT 1)", + True, + ), ], ) def test_has_table_query(sql: str, expected: bool) -> None: