Skip to content

Commit

Permalink
Fix(snowflake): treat $ as part of the json path key identifier (#4604)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jan 13, 2025
1 parent 2bea466 commit 199508a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import typing as t

from sqlglot import exp, generator, parser, tokens, transforms
from sqlglot import exp, generator, jsonpath, parser, tokens, transforms
from sqlglot.dialects.dialect import (
Dialect,
NormalizationStrategy,
Expand Down Expand Up @@ -375,6 +375,10 @@ def quote_identifier(self, expression: E, identify: bool = True) -> E:

return super().quote_identifier(expression, identify=identify)

class JSONPathTokenizer(jsonpath.JSONPathTokenizer):
SINGLE_TOKENS = jsonpath.JSONPathTokenizer.SINGLE_TOKENS.copy()
SINGLE_TOKENS.pop("$")

class Parser(parser.Parser):
IDENTIFY_PIVOT_STRINGS = True
DEFAULT_SAMPLING_METHOD = "BERNOULLI"
Expand Down
6 changes: 5 additions & 1 deletion tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_snowflake(self):
self.validate_identity("SELECT MATCH_CONDITION")
self.validate_identity("SELECT * REPLACE (CAST(col AS TEXT) AS scol) FROM t")
self.validate_identity("1 /* /* */")
self.validate_identity("TO_TIMESTAMP(col, fmt)")
self.validate_identity(
"SELECT * FROM table AT (TIMESTAMP => '2024-07-24') UNPIVOT(a FOR b IN (c)) AS pivot_table"
)
Expand Down Expand Up @@ -104,7 +105,10 @@ def test_snowflake(self):
self.validate_identity(
"SELECT * FROM DATA AS DATA_L ASOF JOIN DATA AS DATA_R MATCH_CONDITION (DATA_L.VAL > DATA_R.VAL) ON DATA_L.ID = DATA_R.ID"
)
self.validate_identity("TO_TIMESTAMP(col, fmt)")
self.validate_identity(
"GET_PATH(json_data, '$id')",
"""GET_PATH(json_data, '["$id"]')""",
)
self.validate_identity(
"CAST(x AS GEOGRAPHY)",
"TO_GEOGRAPHY(x)",
Expand Down

0 comments on commit 199508a

Please sign in to comment.