Fix function grouping skipped for lowercase 'as' in CREATE TABLE AS SELECT#867
Open
Osamaali313 wants to merge 1 commit into
Open
Conversation
…ELECT group_functions compared `tmp_token.value == 'AS'` case-sensitively, while the sibling CREATE and TABLE checks in the same loop use `.value.upper()`. SQL keywords are case-insensitive, so a lowercase `as` in a CREATE TABLE ... AS SELECT statement left `has_as` False, tripping the guard and skipping all function grouping for that statement (e.g. `count(x)` in the SELECT body was returned as a bare Identifier + Parenthesis instead of a Function). Compare case-insensitively, matching the sibling checks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pytest)ruff)In
group_functions(sqlparse/engine/grouping.py), the guard that suppresses function grouping forCREATE TABLE name (...)checks three tokens, but only two are case-insensitive:Since SQL keywords are case-insensitive, a lowercase
asin aCREATE TABLE ... AS SELECT(CTAS) statement leaveshas_asFalse, so the guard fires and grouping returns early, skipping all function grouping for the statement.With uppercase
ASthe same statement groupscount(x)as aFunction; only the keyword casing changes the parse tree. The existingtest_grouping_alias_ctascovers the uppercase form.The fix compares
ascase-insensitively like its sibling checks, and I added a lowercase regression test. The original guard behavior is preserved:create table foo (a int, b int)(noAS) still does not groupfoo (...)as a function in either case.