Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Blob syntax fails when including a typecast or || concat in Snowflake #3068

Closed
seve-martinez opened this issue Mar 1, 2024 · 0 comments
Assignees

Comments

@seve-martinez
Copy link

seve-martinez commented Mar 1, 2024

This is actually two bugs, but are very closely related.

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

First issue

from sqlglot import parse_one, exp
from sqlglot.dialects import Snowflake
from sqlglot.optimizer import build_scope, find_all_in_scope
from sqlglot.optimizer.qualify import qualify


qry = """
  with a as (
    select '{"from": 12345, "to": 67890}' as json_blob
  )
  
  -- or ::INTEGER
  select try_parse_json(json_blob):from::STRING from a
"""

ast = parse_one(qry, dialect=Snowflake)
q_ast = qualify(ast, dialect=Snowflake)

root = build_scope(q_ast)

for scope in root.traverse():
    for column in set(scope.columns):
        print(column)

Produces

ParseError: Invalid expression / Unexpected token. Line 6, Col: 41.
     select '{"from": 12345, "to": 67890}' as json_blob
  )

  select try_parse_json(json_blob):from::STRING from a

Alternatively:

Invalid JSON path syntax. Unexpected TokenType.L_PAREN at index 1: CAST(from AS TEXT)

Second issue

The other example uses the following query:

ParseError: Invalid expression / Unexpected token. Line 6, Col: 42.
    select '{"from": 12345, "to": 67890}' as json_blob
  )

  -- NOTE: The space preceding the `||` is optional
  select try_parse_json(json_blob):from || ' test' from a

Will fail with:

Unexpected TokenType.STRING at index 2: from || ' test '

Official Documentation
Please include links to official SQL documentation related to your issue.

@seve-martinez seve-martinez changed the title JSON Blob syntax fails when including a typecast or || concat JSON Blob syntax fails when including a typecast or || concat in Snowflake Mar 1, 2024
@tobymao tobymao self-assigned this Mar 1, 2024
@tobymao tobymao closed this as completed in c51b64f Mar 2, 2024
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

No branches or pull requests

2 participants