sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

Snowflake Alter Table ... Add Column ... WITH TAG (...) doesn't parse correctly

Open barino86 opened this issue 1 year ago • 1 comments

Before you file an issue

  • Make sure you specify the "read" dialect eg. parse_one(sql, read="spark"): snowflake
  • Make sure you specify the "write" dialect eg. ast.sql(dialect="duckdb"): snowflake
  • Check if the issue still exists on main

Fully reproducible code snippet Please include a fully reproducible code snippet or the input sql, dialect, and expected output.

Snowflake syntax allows an optional WITH token to add a column with a tag, however, the parser doesn't like the WITH token in this position. If the WITH token is removed the parser doesn't know how to parse the TAG syntax for added columns.

This example doesn't understand the WITH token as part of the Add Column query:

from sqlglot import parse_one

query = """
    ALTER TABLE db_name.schmaName.tblName add column COLUMN_1 varchar not null WITH TAG (key1 = 'value_1');
"""
parsed = parse_one(query, read='snowflake')
print(repr(parsed))

...
sqlglot.errors.ParseError: Expecting (. Line 2, Col: 84.
            ALTER TABLE db_name.schmaName.tblName add column COLUMN_1 varchar not null TAG (key1 = 'value_1');

This example doesn't raise an exception but still doesn't know how to parse the query to add a column with a tag.

from sqlglot import parse_one

query = """
    ALTER TABLE db_name.schmaName.tblName add column COLUMN_1 varchar not null TAG (key1 = 'value_1');
"""
parsed = parse_one(query, read='snowflake')
print(repr(parsed))

...
'ALTER TABLE db_name.schmaName.tblName add column COLUMN_1 varchar not null TAG (key1 = 'value_1')' contains unsupported syntax. Falling back to parsing as a 'Command'.
Command(this=ALTER, expression=TABLE db_name.schmaName.tblName add column COLUMN_1 varchar not null TAG (key1 = 'value_1'))
...

Official Documentation Please include links to official SQL documentation related to your issue. https://docs.snowflake.com/en/sql-reference/sql/alter-table#table-column-actions-tablecolumnaction

barino86 avatar May 23 '24 04:05 barino86

Will this also take care of the case for ALTER TABLE ... ALTER COLUMN ... SET TAG tag1 = 'value1' ?

barino86 avatar May 25 '24 18:05 barino86