summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki Ariga <chezou+github@gmail.com>2022-05-03 00:33:01 -0700
committerAndi Albrecht <albrecht.andi@gmail.com>2022-08-24 21:08:17 +0200
commit7de1999a0af9dbbd09f0afb3a50383ad91c22c4c (patch)
tree631532bb4b3d5d2af10260bdfaac17a16e03d7de
parent4862d68ef15617266c8cc8174beb705fb388ec20 (diff)
downloadsqlparse-7de1999a0af9dbbd09f0afb3a50383ad91c22c4c.tar.gz
CREATE TABLE tbl AS SELECT should return get_alias() for its column
-rw-r--r--sqlparse/engine/grouping.py5
-rw-r--r--tests/test_grouping.py5
2 files changed, 9 insertions, 1 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index 2fb0a4c..d250e18 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -341,12 +341,15 @@ def group_aliased(tlist):
def group_functions(tlist):
has_create = False
has_table = False
+ has_as = False
for tmp_token in tlist.tokens:
if tmp_token.value == 'CREATE':
has_create = True
if tmp_token.value == 'TABLE':
has_table = True
- if has_create and has_table:
+ if tmp_token.value == 'AS':
+ has_as = True
+ if has_create and has_table and not has_as:
return
tidx, token = tlist.token_next_by(t=T.Name)
diff --git a/tests/test_grouping.py b/tests/test_grouping.py
index cf629e9..8c034f9 100644
--- a/tests/test_grouping.py
+++ b/tests/test_grouping.py
@@ -324,6 +324,11 @@ def test_grouping_alias_case():
assert p.tokens[0].get_alias() == 'foo'
+def test_grouping_alias_ctas():
+ 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*