Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(duckdb): support postgres JSON/JSONB_OBJECT_AGG to duckdb JSON_GROUP_OBJECT #4677

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sqlglot/dialects/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ class Generator(generator.Generator):
exp.Levenshtein: unsupported_args("ins_cost", "del_cost", "sub_cost", "max_dist")(
rename_func("LEVENSHTEIN")
),
exp.JSONObjectAgg: rename_func("JSON_GROUP_OBJECT"),
exp.JSONBObjectAgg: rename_func("JSON_GROUP_OBJECT"),
}

SUPPORTED_JSON_PATH_PARTS = {
Expand Down
6 changes: 6 additions & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ class Parser(parser.Parser):
"SHA384": lambda args: exp.SHA2(this=seq_get(args, 0), length=exp.Literal.number(384)),
"SHA512": lambda args: exp.SHA2(this=seq_get(args, 0), length=exp.Literal.number(512)),
"LEVENSHTEIN_LESS_EQUAL": _build_levenshtein_less_equal,
"JSON_OBJECT_AGG": lambda args: exp.JSONObjectAgg(
expressions=[seq_get(args, 0), seq_get(args, 1)]
VaggelisD marked this conversation as resolved.
Show resolved Hide resolved
),
"JSONB_OBJECT_AGG": exp.JSONBObjectAgg.from_arg_list,
}

NO_PAREN_FUNCTIONS = {
Expand Down Expand Up @@ -617,6 +621,8 @@ class Generator(generator.Generator):
exp.Unicode: rename_func("ASCII"),
exp.UnixToTime: _unix_to_time_sql,
exp.Levenshtein: _levenshtein_sql,
exp.JSONObjectAgg: rename_func("JSON_OBJECT_AGG"),
exp.JSONBObjectAgg: rename_func("JSONB_OBJECT_AGG"),
}

TRANSFORMS.pop(exp.CommentColumnConstraint)
Expand Down
8 changes: 8 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6068,6 +6068,14 @@ class JSONObjectAgg(AggFunc):
}


# https://www.postgresql.org/docs/9.5/functions-aggregate.html
class JSONBObjectAgg(AggFunc):
arg_types = {
"key": True,
"value": True,
}
VaggelisD marked this conversation as resolved.
Show resolved Hide resolved


# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAY.html
class JSONArray(Func):
arg_types = {
Expand Down
20 changes: 20 additions & 0 deletions tests/dialects/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,26 @@ def test_duckdb(self):
},
)

self.validate_all(
"SELECT JSON_GROUP_OBJECT(k, v) FROM t",
read={
"postgres": "SELECT JSON_OBJECT_AGG(k, v) FROM t",
},
write={
"duckdb": "SELECT JSON_GROUP_OBJECT(k, v) FROM t",
},
)

self.validate_all(
"SELECT JSON_GROUP_OBJECT(k, v) FROM t",
read={
"postgres": "SELECT JSONB_OBJECT_AGG(k, v) FROM t",
},
write={
"duckdb": "SELECT JSON_GROUP_OBJECT(k, v) FROM t",
},
)

def test_array_index(self):
with self.assertLogs(helper_logger) as cm:
self.validate_all(
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,9 @@ def test_postgres(self):
"SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY a) FILTER(WHERE CAST(b AS BOOLEAN)) AS mean_value FROM (VALUES (0, 't')) AS fake_data(a, b)"
)

self.validate_identity("SELECT JSON_OBJECT_AGG(k, v) FROM t")
self.validate_identity("SELECT JSONB_OBJECT_AGG(k, v) FROM t")
VaggelisD marked this conversation as resolved.
Show resolved Hide resolved

def test_ddl(self):
# Checks that user-defined types are parsed into DataType instead of Identifier
self.parse_one("CREATE TABLE t (a udt)").this.expressions[0].args["kind"].assert_is(
Expand Down