Skip to content

Commit a301b79

Browse files
liulkandialbrecht
authored andcommitted
[grouping] group_as() no longer groups AS CTE
This patch changes the grouping of AS so that: Foo AS WITH bar AS 1 SELECT 2 with no longer be grouped as: [Identifier[Foo, AS, WITH, Identifier[Bar AS 1]], SELECT, 2] but will be grouped as: [Identifier[Foo], AS, WITH, Identifier[Bar AS 1], SELECT, 2] This fixes the parsing of CREATE TABLE new_table AS WITH ... so the rest of the tokens after AS are parsed the same as a bare WITH.
1 parent 44eacf2 commit a301b79

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

sqlparse/engine/grouping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def valid_prev(token):
164164
return token.normalized == 'NULL' or not token.is_keyword
165165

166166
def valid_next(token):
167-
ttypes = T.DML, T.DDL
167+
ttypes = T.DML, T.DDL, T.CTE
168168
return not imt(token, t=ttypes) and token is not None
169169

170170
def post(tlist, pidx, tidx, nidx):

tests/test_grouping.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,11 @@ def test_aliased_literal_without_as():
632632
p = sqlparse.parse('1 foo')[0].tokens
633633
assert len(p) == 1
634634
assert p[0].get_alias() == 'foo'
635+
636+
637+
def test_grouping_as_cte():
638+
p = sqlparse.parse('foo AS WITH apple AS 1, banana AS 2')[0].tokens
639+
assert len(p) > 4
640+
assert p[0].get_alias() is None
641+
assert p[2].value == 'AS'
642+
assert p[4].value == 'WITH'

0 commit comments

Comments
 (0)