Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: is_select check for lowercase select with "WITH" clauses #22370

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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