fix: Allow "EXPLAIN" queries when "Allow DML" setting is False (#11348)

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
This commit is contained in:
Hugh A. Miles II 2020-10-21 12:58:57 -07:00 committed by GitHub
parent 54c2ad4308
commit dfbcbcce67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -111,7 +111,13 @@ class ParsedQuery:
return self._parsed[0].get_type() == "SELECT"
def is_explain(self) -> bool:
return self.stripped().upper().startswith("EXPLAIN")
# Remove comments
statements_without_comments = sqlparse.format(
self.stripped(), strip_comments=True
)
# Explain statements will only be the first statement
return statements_without_comments.startswith("EXPLAIN")
def is_unknown(self) -> bool:
return self._parsed[0].get_type() == "UNKNOWN"

View File

@ -579,3 +579,51 @@ class TestSupersetSqlParse(unittest.TestCase):
reindent=True,
),
)
def test_is_explain(self):
query = """
-- comment
EXPLAIN select * from table
-- comment 2
"""
parsed = ParsedQuery(query)
self.assertEqual(parsed.is_explain(), True)
query = """
-- comment
EXPLAIN select * from table
where col1 = 'something'
-- comment 2
-- comment 3
EXPLAIN select * from table
where col1 = 'something'
-- comment 4
"""
parsed = ParsedQuery(query)
self.assertEqual(parsed.is_explain(), True)
query = """
-- This is a comment
-- this is another comment but with a space in the front
EXPLAIN SELECT * FROM TABLE
"""
parsed = ParsedQuery(query)
self.assertEqual(parsed.is_explain(), True)
query = """
/* This is a comment
with stars instead */
EXPLAIN SELECT * FROM TABLE
"""
parsed = ParsedQuery(query)
self.assertEqual(parsed.is_explain(), True)
query = """
-- comment
select * from table
where col1 = 'something'
-- comment 2
"""
parsed = ParsedQuery(query)
self.assertEqual(parsed.is_explain(), False)