Skip to content

Commit 7de1999

Browse files
chezouandialbrecht
authored andcommitted
CREATE TABLE tbl AS SELECT should return get_alias() for its column
1 parent 4862d68 commit 7de1999

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

sqlparse/engine/grouping.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,15 @@ def group_aliased(tlist):
341341
def group_functions(tlist):
342342
has_create = False
343343
has_table = False
344+
has_as = False
344345
for tmp_token in tlist.tokens:
345346
if tmp_token.value == 'CREATE':
346347
has_create = True
347348
if tmp_token.value == 'TABLE':
348349
has_table = True
349-
if has_create and has_table:
350+
if tmp_token.value == 'AS':
351+
has_as = True
352+
if has_create and has_table and not has_as:
350353
return
351354

352355
tidx, token = tlist.token_next_by(t=T.Name)

tests/test_grouping.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ def test_grouping_alias_case():
324324
assert p.tokens[0].get_alias() == 'foo'
325325

326326

327+
def test_grouping_alias_ctas():
328+
p = sqlparse.parse('CREATE TABLE tbl1 AS SELECT coalesce(t1.col1, 0) AS col1 FROM t1')[0]
329+
assert p.tokens[10].get_alias() == 'col1'
330+
assert isinstance(p.tokens[10].tokens[0], sql.Function)
331+
327332
def test_grouping_subquery_no_parens():
328333
# Not totally sure if this is the right approach...
329334
# When a THEN clause contains a subquery w/o parenthesis around it *and*

0 commit comments

Comments
 (0)