Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Bug Fixes

* Fix statement splitting (issue845).
* Fix a late-binding closure bug in `TokenList.token_not_matching`.
* Fix function grouping being skipped in ``CREATE TABLE ... AS SELECT``
statements when the ``as`` keyword is lowercase.


Release 0.5.5 (Dec 19, 2025)
Expand Down
2 changes: 1 addition & 1 deletion sqlparse/engine/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def group_functions(tlist):
has_create = True
if tmp_token.value.upper() == 'TABLE':
has_table = True
if tmp_token.value == 'AS':
if tmp_token.value.upper() == 'AS':
has_as = True
if has_create and has_table and not has_as:
return
Expand Down
8 changes: 8 additions & 0 deletions tests/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ def test_grouping_alias_ctas():
assert p.tokens[10].get_alias() == 'col1'
assert isinstance(p.tokens[10].tokens[0], sql.Function)


def test_grouping_alias_ctas_lowercase_as():
# 'as' is case-insensitive; a lowercase keyword in CREATE TABLE ... AS
# SELECT must not disable function grouping in the SELECT body.
p = sqlparse.parse('create table tbl1 as select coalesce(t1.col1, 0) as col1 from t1')[0]
assert p.tokens[10].get_alias() == 'col1'
assert isinstance(p.tokens[10].tokens[0], sql.Function)

def test_grouping_subquery_no_parens():
# Not totally sure if this is the right approach...
# When a THEN clause contains a subquery w/o parenthesis around it *and*
Expand Down