Skip to content

CREATE TABLE tbl AS SELECT should return get_alias() for its column#662

Merged
andialbrecht merged 1 commit into
andialbrecht:masterfrom
chezou:ctas
Aug 24, 2022
Merged

CREATE TABLE tbl AS SELECT should return get_alias() for its column#662
andialbrecht merged 1 commit into
andialbrecht:masterfrom
chezou:ctas

Conversation

@chezou
Copy link
Copy Markdown
Contributor

@chezou chezou commented May 3, 2022

Currently, CREATE TABLE AS SELECT a.k.a. CTAS doesn't parse column aliases with functions e.g., coalesce, if, appropriately.

In the following example, coalesce is parsed as Name while it should be Function to get get_alias() appropriately.

Before

>>> import sqlparse
>>> p = sqlparse.parse('CREATE TABLE tbl1 AS SELECT coalesce(t1.col1, 0) AS col1 FROM t1')[0]
>>> p._pprint_tree()
|- 0 DDL 'CREATE'
|- 1 Whitespace ' '
|- 2 Keyword 'TABLE'
|- 3 Whitespace ' '
|- 4 Identifier 'tbl1'
|  `- 0 Name 'tbl1'
|- 5 Whitespace ' '
|- 6 Keyword 'AS'
|- 7 Whitespace ' '
|- 8 DML 'SELECT'
|- 9 Whitespace ' '
|- 10 Identifier 'coales...'
|  |- 0 Name 'coales...'
|  `- 1 Identifier '(t1.co...'
|     |- 0 Parenthesis '(t1.co...'
|     |  |- 0 Punctuation '('
|     |  |- 1 IdentifierList 't1.col...'
|     |  |  |- 0 Identifier 't1.col1'
|     |  |  |  |- 0 Name 't1'
|     |  |  |  |- 1 Punctuation '.'
|     |  |  |  `- 2 Name 'col1'
|     |  |  |- 1 Punctuation ','
|     |  |  |- 2 Whitespace ' '
|     |  |  `- 3 Integer '0'
|     |  `- 2 Punctuation ')'
|     |- 1 Whitespace ' '
|     |- 2 Keyword 'AS'
|     |- 3 Whitespace ' '
|     `- 4 Identifier 'col1'
|        `- 0 Name 'col1'
|- 11 Whitespace ' '
|- 12 Keyword 'FROM'
|- 13 Whitespace ' '
`- 14 Identifier 't1'
   `- 0 Name 't1'
>>> p.tokens[10].get_alias()
# Should return 'col1'

After

>>> p = sqlparse.parse('CREATE TABLE tbl1 AS SELECT coalesce(t1.col1, 0) AS col1 FROM t1')[0]
>>> p._pprint_tree()
|- 0 DDL 'CREATE'
|- 1 Whitespace ' '
|- 2 Keyword 'TABLE'
|- 3 Whitespace ' '
|- 4 Identifier 'tbl1'
|  `- 0 Name 'tbl1'
|- 5 Whitespace ' '
|- 6 Keyword 'AS'
|- 7 Whitespace ' '
|- 8 DML 'SELECT'
|- 9 Whitespace ' '
|- 10 Identifier 'coales...'
|  |- 0 Function 'coales...'
|  |  |- 0 Identifier 'coales...'
|  |  |  `- 0 Name 'coales...'
|  |  `- 1 Parenthesis '(t1.co...'
|  |     |- 0 Punctuation '('
|  |     |- 1 IdentifierList 't1.col...'
|  |     |  |- 0 Identifier 't1.col1'
|  |     |  |  |- 0 Name 't1'
|  |     |  |  |- 1 Punctuation '.'
|  |     |  |  `- 2 Name 'col1'
|  |     |  |- 1 Punctuation ','
|  |     |  |- 2 Whitespace ' '
|  |     |  `- 3 Integer '0'
|  |     `- 2 Punctuation ')'
|  |- 1 Whitespace ' '
|  |- 2 Keyword 'AS'
|  |- 3 Whitespace ' '
|  `- 4 Identifier 'col1'
|     `- 0 Name 'col1'
|- 11 Whitespace ' '
|- 12 Keyword 'FROM'
|- 13 Whitespace ' '
`- 14 Identifier 't1'
   `- 0 Name 't1'
>>> p.tokens[10].get_alias()
'col1'

@andialbrecht
Copy link
Copy Markdown
Owner

Thanks a lot!

@chezou chezou deleted the ctas branch August 24, 2022 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants