Skip to content

Commit

Permalink
Fix: double json spark closes #1547
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 5, 2023
1 parent dfae784 commit aef9cfa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sqlglot/dialects/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ def _date_diff_sql(self: generator.Generator, expression: exp.DateDiff) -> str:
return f"{diff_sql}{multiplier_sql}"


def _json_format_sql(self: generator.Generator, expression: exp.JSONFormat) -> str:
this = expression.this

if not this.type:
from sqlglot.optimizer.annotate_types import annotate_types

annotate_types(this)

if this.type.is_type(exp.DataType.Type.JSON):
return self.sql(this)
return self.func("TO_JSON", this, expression.args.get("options"))


def _array_sort_sql(self: generator.Generator, expression: exp.ArraySort) -> str:
if expression.expression:
self.unsupported("Hive SORT_ARRAY does not support a comparator")
Expand Down Expand Up @@ -306,7 +319,7 @@ class Generator(generator.Generator):
exp.ILike: no_ilike_sql,
exp.JSONExtract: rename_func("GET_JSON_OBJECT"),
exp.JSONExtractScalar: rename_func("GET_JSON_OBJECT"),
exp.JSONFormat: rename_func("TO_JSON"),
exp.JSONFormat: _json_format_sql,
exp.Map: var_map_sql,
exp.Max: max_or_greatest,
exp.Min: min_or_least,
Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,14 @@ def test_presto(self):
},
)

self.validate_all(
"JSON_FORMAT(JSON 'x')",
write={
"presto": "JSON_FORMAT(CAST('x' AS JSON))",
"spark": "TO_JSON('x')",
},
)

def test_encode_decode(self):
self.validate_all(
"TO_UTF8(x)",
Expand Down

0 comments on commit aef9cfa

Please sign in to comment.