Skip to content

Commit

Permalink
Fix: json_object(*) closes #1757
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 12, 2023
1 parent 46abf16 commit 2b46782
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
17 changes: 13 additions & 4 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,6 @@ def jsonkeyvalue_sql(self, expression: exp.JSONKeyValue) -> str:
return f"{self.sql(expression, 'this')}: {self.sql(expression, 'expression')}"

def jsonobject_sql(self, expression: exp.JSONObject) -> str:
expressions = self.expressions(expression)
null_handling = expression.args.get("null_handling")
null_handling = f" {null_handling}" if null_handling else ""
unique_keys = expression.args.get("unique_keys")
Expand All @@ -1792,7 +1791,11 @@ def jsonobject_sql(self, expression: exp.JSONObject) -> str:
format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
encoding = self.sql(expression, "encoding")
encoding = f" ENCODING {encoding}" if encoding else ""
return f"JSON_OBJECT({expressions}{null_handling}{unique_keys}{return_type}{format_json}{encoding})"
return self.func(
"JSON_OBJECT",
*expression.expressions,
suffix=f"{null_handling}{unique_keys}{return_type}{format_json}{encoding})",
)

def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
this = self.sql(expression, "this")
Expand Down Expand Up @@ -2183,8 +2186,14 @@ def function_fallback_sql(self, expression: exp.Func) -> str:

return self.func(expression.sql_name(), *args)

def func(self, name: str, *args: t.Optional[exp.Expression | str]) -> str:
return f"{self.normalize_func(name)}({self.format_args(*args)})"
def func(
self,
name: str,
*args: t.Optional[exp.Expression | str],
prefix: str = "(",
suffix: str = ")",
) -> str:
return f"{self.normalize_func(name)}{prefix}{self.format_args(*args)}{suffix}"

def format_args(self, *args: t.Optional[str | exp.Expression]) -> str:
arg_sqls = tuple(self.sql(arg) for arg in args if arg is not None)
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3750,7 +3750,8 @@ def _parse_json_key_value(self) -> t.Optional[exp.JSONKeyValue]:
return self.expression(exp.JSONKeyValue, this=key, expression=value)

def _parse_json_object(self) -> exp.JSONObject:
expressions = self._parse_csv(self._parse_json_key_value)
star = self._parse_star()
expressions = [star] if star else self._parse_csv(self._parse_json_key_value)

null_handling = None
if self._match_text_seq("NULL", "ON", "NULL"):
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ PRAGMA schema.synchronous = 2
PRAGMA schema.synchronous = FULL
PRAGMA schema.memory_limit = '1GB'
JSON_OBJECT()
JSON_OBJECT(*)
JSON_OBJECT('key1': 1, 'key2': TRUE)
JSON_OBJECT('id': '5', 'fld1': 'bla', 'fld2': 'bar')
JSON_OBJECT('x': NULL, 'y': 1 NULL ON NULL)
Expand Down

0 comments on commit 2b46782

Please sign in to comment.