Skip to content

Commit 52e068f

Browse files
authored
fix(databricks): Preserve colon operators in TRY_CAST (#5028)
1 parent 65ac8d0 commit 52e068f

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

sqlglot/dialects/spark2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ def struct_sql(self, expression: exp.Struct) -> str:
334334

335335
def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
336336
arg = expression.this
337-
is_json_extract = isinstance(arg, (exp.JSONExtract, exp.JSONExtractScalar))
337+
is_json_extract = isinstance(
338+
arg, (exp.JSONExtract, exp.JSONExtractScalar)
339+
) and not arg.args.get("variant_extract")
338340

339341
# We can't use a non-nested type (eg. STRING) as a schema
340342
if expression.to.args.get("nested") and (is_parse_json(arg) or is_json_extract):

tests/dialects/test_databricks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ def test_databricks(self):
165165
# https://docs.databricks.com/sql/language-manual/functions/colonsign.html
166166
def test_json(self):
167167
self.validate_identity("SELECT c1:price, c1:price.foo, c1:price.bar[1]")
168+
self.validate_identity("SELECT TRY_CAST(c1:price AS ARRAY<VARIANT>)")
169+
self.validate_identity("""SELECT TRY_CAST(c1:["foo bar"]["baz qux"] AS ARRAY<VARIANT>)""")
168170
self.validate_identity(
169171
"""SELECT c1:item[1].price FROM VALUES ('{ "item": [ { "model" : "basic", "price" : 6.12 }, { "model" : "medium", "price" : 9.24 } ] }') AS T(c1)"""
170172
)

0 commit comments

Comments
 (0)