Skip to content

Commit

Permalink
Fix(mysql): generate JSON_OBJECT properly (#2139)
Browse files Browse the repository at this point in the history
* Fix(mysql): generate JSON_OBJECT properly

* Move common function to dialect and use TRANSFORM
  • Loading branch information
middagj authored Aug 31, 2023
1 parent 7a27931 commit 2ad559d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
datestrtodate_sql,
format_time_lambda,
inline_array_sql,
json_keyvalue_comma_sql,
max_or_greatest,
min_or_least,
no_ilike_sql,
Expand Down Expand Up @@ -417,6 +418,7 @@ class Generator(generator.Generator):
exp.ILike: no_ilike_sql,
exp.IntDiv: rename_func("DIV"),
exp.JSONFormat: rename_func("TO_JSON_STRING"),
exp.JSONKeyValue: json_keyvalue_comma_sql,
exp.Max: max_or_greatest,
exp.MD5: lambda self, e: self.func("TO_HEX", self.func("MD5", e.this)),
exp.MD5Digest: rename_func("MD5"),
Expand Down Expand Up @@ -651,6 +653,3 @@ def version_sql(self, expression: exp.Version) -> str:
expression = expression.copy()
expression.set("this", "SYSTEM_TIME")
return super().version_sql(expression)

def jsonkeyvalue_sql(self, expression: exp.JSONKeyValue) -> str:
return f"{self.sql(expression, 'this')}, {self.sql(expression, 'expression')}"
5 changes: 5 additions & 0 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,8 @@ def parse_timestamp_trunc(args: t.List) -> exp.TimestampTrunc:

def any_value_to_max_sql(self: Generator, expression: exp.AnyValue) -> str:
return self.func("MAX", expression.this)


# Used to generate JSON_OBJECT with a comma in BigQuery and MySQL instead of colon
def json_keyvalue_comma_sql(self, expression: exp.JSONKeyValue) -> str:
return f"{self.sql(expression, 'this')}, {self.sql(expression, 'expression')}"
2 changes: 2 additions & 0 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
arrow_json_extract_scalar_sql,
datestrtodate_sql,
format_time_lambda,
json_keyvalue_comma_sql,
locate_to_strposition,
max_or_greatest,
min_or_least,
Expand Down Expand Up @@ -531,6 +532,7 @@ class Generator(generator.Generator):
exp.GroupConcat: lambda self, e: f"""GROUP_CONCAT({self.sql(e, "this")} SEPARATOR {self.sql(e, "separator") or "','"})""",
exp.ILike: no_ilike_sql,
exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
exp.JSONKeyValue: json_keyvalue_comma_sql,
exp.Max: max_or_greatest,
exp.Min: min_or_least,
exp.NullSafeEQ: lambda self, e: self.binary(e, "<=>"),
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,6 @@ def test_set_variable(self):

cmd = self.parse_one("SET x = 1, y = 2")
self.assertEqual(len(cmd.expressions), 2)

def test_json_object(self):
self.validate_identity("SELECT JSON_OBJECT('id', 87, 'name', 'carrot')")

0 comments on commit 2ad559d

Please sign in to comment.