Skip to content

Commit

Permalink
CREATE TABLE tbl AS SELECT should return get_alias() for its column
Browse files Browse the repository at this point in the history
  • Loading branch information
chezou authored and andialbrecht committed Aug 24, 2022
1 parent 4862d68 commit 7de1999
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sqlparse/engine/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down

0 comments on commit 7de1999

Please sign in to comment.