in superset/sql_parse.py [0:0]
def is_select(self) -> bool: # noqa: C901
# make sure we strip comments; prevents a bug with comments in the CTE
parsed = sqlparse.parse(self.strip_comments())
seen_select = False
for statement in parsed:
# Check if this is a CTE
if statement.is_group and statement[0].ttype == Keyword.CTE:
if sqloxide_parse is not None:
try:
if not self._check_cte_is_select(
sqloxide_parse(self.strip_comments(), dialect="ansi")
):
return False
except ValueError:
# sqloxide was not able to parse the query, so let's continue with # noqa: E501
# sqlparse
pass
inner_cte = self.get_inner_cte_expression(statement.tokens) or []
# Check if the inner CTE is a not a SELECT
if any(token.ttype == DDL for token in inner_cte) or any(
token.ttype == DML and token.normalized != "SELECT"
for token in inner_cte
):
return False
if statement.get_type() == "SELECT":
seen_select = True
continue
if statement.get_type() != "UNKNOWN":
return False
# for `UNKNOWN`, check all DDL/DML explicitly: only `SELECT` DML is allowed,
# and no DDL is allowed
if any(token.ttype == DDL for token in statement) or any(
token.ttype == DML and token.normalized != "SELECT"
for token in statement
):
return False
if imt(statement.tokens[0], m=(Keyword, "USE")):
continue
# return false on `EXPLAIN`, `SET`, `SHOW`, etc.
if imt(statement.tokens[0], t=Keyword):
return False
if not any(
token.ttype == DML and token.normalized == "SELECT"
for token in statement
):
return False
return seen_select