Skip to content

Fix function grouping skipped for lowercase 'as' in CREATE TABLE AS SELECT#867

Open
Osamaali313 wants to merge 1 commit into
andialbrecht:masterfrom
Osamaali313:fix-ctas-lowercase-as-function-grouping
Open

Fix function grouping skipped for lowercase 'as' in CREATE TABLE AS SELECT#867
Osamaali313 wants to merge 1 commit into
andialbrecht:masterfrom
Osamaali313:fix-ctas-lowercase-as-function-grouping

Conversation

@Osamaali313

Copy link
Copy Markdown
  • ran the tests (pytest)
  • all style issues addressed (ruff)
  • your changes are covered by tests
  • your changes are documented, if needed

In group_functions (sqlparse/engine/grouping.py), the guard that suppresses function grouping for CREATE TABLE name (...) checks three tokens, but only two are case-insensitive:

if tmp_token.value.upper() == 'CREATE':
    has_create = True
if tmp_token.value.upper() == 'TABLE':
    has_table = True
if tmp_token.value == 'AS':        # case-sensitive
    has_as = True

Since SQL keywords are case-insensitive, a lowercase as in a CREATE TABLE ... AS SELECT (CTAS) statement leaves has_as False, so the guard fires and grouping returns early, skipping all function grouping for the statement.

import sqlparse
from sqlparse import sql

p = sqlparse.parse("CREATE TABLE foo as SELECT count(x) FROM bar")[0]
# count(x) is left as a bare Name + Parenthesis instead of a sql.Function

With uppercase AS the same statement groups count(x) as a Function; only the keyword casing changes the parse tree. The existing test_grouping_alias_ctas covers the uppercase form.

The fix compares as case-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) (no AS) still does not group foo (...) as a function in either case.

…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.
Copilot AI review requested due to automatic review settings July 22, 2026 19:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants