From a301b79c042c5c4e8677ad6e44905903ea9375c3 Mon Sep 17 00:00:00 2001 From: Likai Liu Date: Tue, 7 Jan 2020 18:22:32 -0500 Subject: [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. --- sqlparse/engine/grouping.py | 2 +- tests/test_grouping.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index b634128..daaffb0 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -164,7 +164,7 @@ def group_as(tlist): return token.normalized == 'NULL' or not token.is_keyword def valid_next(token): - ttypes = T.DML, T.DDL + ttypes = T.DML, T.DDL, T.CTE return not imt(token, t=ttypes) and token is not None def post(tlist, pidx, tidx, nidx): diff --git a/tests/test_grouping.py b/tests/test_grouping.py index 6b5bb68..a147063 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -632,3 +632,11 @@ def test_aliased_literal_without_as(): p = sqlparse.parse('1 foo')[0].tokens assert len(p) == 1 assert p[0].get_alias() == 'foo' + + +def test_grouping_as_cte(): + p = sqlparse.parse('foo AS WITH apple AS 1, banana AS 2')[0].tokens + assert len(p) > 4 + assert p[0].get_alias() is None + assert p[2].value == 'AS' + assert p[4].value == 'WITH' -- cgit v1.2.1