diff options
author | Likai Liu <liulk@likai.org> | 2020-01-07 18:22:32 -0500 |
---|---|---|
committer | Andi Albrecht <albrecht.andi@gmail.com> | 2020-02-02 21:21:09 +0100 |
commit | a301b79c042c5c4e8677ad6e44905903ea9375c3 (patch) | |
tree | ca40612759532ba231b57f1388eb446dc71b67fc | |
parent | 44eacf2e2f4a4255829109a5e67e0c1d2af542da (diff) | |
download | sqlparse-a301b79c042c5c4e8677ad6e44905903ea9375c3.tar.gz |
[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.
-rw-r--r-- | sqlparse/engine/grouping.py | 2 | ||||
-rw-r--r-- | tests/test_grouping.py | 8 |
2 files changed, 9 insertions, 1 deletions
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' |