Skip to content

Commit

Permalink
fix: is_select check for lowercase select with "WITH" clauses (#22370)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcomuniz authored Apr 18, 2023
1 parent 37a78b1 commit e9b4022
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def is_select(self) -> bool:
# 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 parsed[0]) or any(
token.ttype == DML and token.value != "SELECT" for token in parsed[0]
token.ttype == DML and token.normalized != "SELECT" for token in parsed[0]
):
return False

Expand All @@ -237,7 +237,7 @@ def is_select(self) -> bool:
return False

return any(
token.ttype == DML and token.value == "SELECT" for token in parsed[0]
token.ttype == DML and token.normalized == "SELECT" for token in parsed[0]
)

def is_valid_ctas(self) -> bool:
Expand Down
22 changes: 22 additions & 0 deletions tests/unit_tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,28 @@ def test_cte_is_select() -> None:
assert sql.is_select()


def test_cte_is_select_lowercase() -> None:
"""
Some CTEs with lowercase select are not correctly identified as SELECTS.
"""
sql = ParsedQuery(
"""WITH foo AS(
select
FLOOR(__time TO WEEK) AS "week",
name,
COUNT(DISTINCT user_id) AS "unique_users"
FROM "druid"."my_table"
GROUP BY 1,2
)
select
f.week,
f.name,
f.unique_users
FROM foo f"""
)
assert sql.is_select()


def test_unknown_select() -> None:
"""
Test that `is_select` works when sqlparse fails to identify the type.
Expand Down

0 comments on commit e9b4022

Please sign in to comment.