sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

Required keyword: 'this' missing for <class 'sqlglot.expressions.CommentColumnConstraint'> with Snowflake

Open braunreyes opened this issue 3 years ago • 2 comments

Here is the code to reproduce:

from sqlglot import parse_one

print(parse_one("""create or replace  temporary view test_view
   
(
  
    "EVENT_ID" COMMENT $$A UUID for each event$$, 
  
    "TITLE" COMMENT $$The title of the shareable list item.$$, 
  
   
)

   as (

    select "EVENT_ID", "TITLE"
    from test
    
)
;""", dialect='snowflake', ).dump())

results in the following stacktrace:

Traceback (most recent call last):
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/src/query.py", line 40, in <module>
    print(parse_one("""create or replace  temporary view test_view
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/__init__.py", line 126, in parse_one
    result = dialect.parse(sql, **opts)
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/dialects/dialect.py", line 320, in parse
    return self.parser(**opts).parse(self.tokenize(sql), sql)
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1004, in parse
    return self._parse(
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1070, in _parse
    expressions.append(parse_method(self))
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1254, in _parse_statement
    return self.STATEMENT_PARSERS[self._prev.token_type](self)
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 555, in <lambda>
    TokenType.CREATE: lambda self: self._parse_create(),
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1367, in _parse_create
    this = self._parse_schema(this=table_parts)
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 3891, in _parse_schema
    args = self._parse_csv(lambda: self._parse_constraint() or self._parse_field_def())
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 4955, in _parse_csv
    parse_result = parse_method()
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 3891, in <lambda>
    args = self._parse_csv(lambda: self._parse_constraint() or self._parse_field_def())
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 3897, in _parse_field_def
    return self._parse_column_def(self._parse_field(any_token=True))
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 3922, in _parse_column_def
    constraint = self._parse_column_constraint()
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 4038, in _parse_column_constraint
    kind=self.CONSTRAINT_PARSERS[self._prev.text.upper()](self),
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 733, in <lambda>
    "COMMENT": lambda self: self.expression(
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1134, in expression
    return self.validate_expression(instance)
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1154, in validate_expression
    self.raise_error(error_message)
  File "/Users/mozilla/projects/moso-data-ops-poc/data-flows/.venv/lib/python3.10/site-packages/sqlglot/parser.py", line 1114, in raise_error
    raise error
sqlglot.errors.ParseError: Required keyword: 'this' missing for <class 'sqlglot.expressions.CommentColumnConstraint'>. Line 5, Col: 48.
  create or replace  temporary view test_view
   
(
  
    "EVENT_ID" COMMENT $$A UUID for each event$$, 
  
    "TITLE" COMMENT $$The title of the shareable list item.$$, 
  
   
)

   as (

    select 

While this gets figured out, is there a way to disable or skip this parsing? These comments are not crucial to my use case at the moment. Also please let me know how I can help.

braunreyes avatar Nov 09 '23 16:11 braunreyes

You can parse it with

print(parse_one("""create or replace  temporary view test_view
   
(
  
    "EVENT_ID" COMMENT $$A UUID for each event$$, 
  
    "TITLE" COMMENT $$The title of the shareable list item.$$, 
  
   
)

   as (

    select "EVENT_ID", "TITLE"
    from test
    
)
;""", dialect='snowflake', error_level=ErrorLevel.IGNORE ).dump())

Just that the output is then incomplete.

sweigert avatar Nov 09 '23 16:11 sweigert

ah I see, so it works but then stuff is missing....like the source table idenfier

{
    "class": "Create",
    "args": {
        "this": {
            "class": "Schema",
            "args": {
                "this": {
                    "class": "Table",
                    "args": {
                        "this": {
                            "class": "Identifier",
                            "args": {"this": "test_view", "quoted": False},
                        }
                    },
                },
                "expressions": [
                    {
                        "class": "ColumnDef",
                        "args": {
                            "this": {
                                "class": "Identifier",
                                "args": {"this": "EVENT_ID", "quoted": True},
                            },
                            "constraints": [
                                {
                                    "class": "ColumnConstraint",
                                    "args": {
                                        "kind": {
                                            "class": "CommentColumnConstraint",
                                            "args": {},
                                        }
                                    },
                                }
                            ],
                        },
                    }
                ],
            },
        },
        "kind": "view",
        "replace": True,
        "exists": False,
        "properties": {
            "class": "Properties",
            "args": {"expressions": [{"class": "TemporaryProperty", "args": {}}]},
        },
    },
}

braunreyes avatar Nov 09 '23 16:11 braunreyes