From b6d17cb3149c9882b9677878f1e04151ecd5dccb Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sun, 29 Nov 2026 23:42:44 -0500 Subject: [PATCH 1/3] JSON_TABLE: don't merge the collations of PASSING arguments Commit e405a46c45b made different explicit collations fail, even when the only column is FOR ORDINALITY. Bug: #19872 Reported-by: chunling qin <303677365@qq.com> Discussion: https://postgr.es/m/19872-fe8b70fdefd5d598@postgresql.org Backpatch-through: 17 --- src/backend/parser/parse_jsontable.c | 11 ++++++++--- src/test/regress/expected/sqljson_jsontable.out | 9 +++++++++ src/test/regress/sql/sqljson_jsontable.sql | 5 +++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/backend/parser/parse_jsontable.c b/src/backend/parser/parse_jsontable.c index 3ecbb5ee6da..c94e34df159 100644 --- a/src/backend/parser/parse_jsontable.c +++ b/src/backend/parser/parse_jsontable.c @@ -166,9 +166,15 @@ transformJsonTable(ParseState *pstate, JsonTable *jt) * Assign collations now, because assign_query_collations() doesn't look * into the range table. This must happen before the PASSING arguments * are copied into tf->passingvalexprs below, so that the copies carry the - * collations too. + * collations too. The PASSING arguments are independent of each other, + * so process them separately rather than through the JsonExpr, which + * would complain if they had different explicit collations. */ - assign_expr_collations(pstate, tf->docexpr); + je = castNode(JsonExpr, tf->docexpr); + assign_expr_collations(pstate, je->formatted_expr); + assign_expr_collations(pstate, je->path_spec); + assign_list_collations(pstate, je->passing_values); + assign_expr_collations(pstate, (Node *) je->on_error); /* * Create a JsonTablePlan that will generate row pattern that becomes @@ -187,7 +193,6 @@ transformJsonTable(ParseState *pstate, JsonTable *jt) * TableFunc.docexpr. JsonExpr.passing_values is still kept around for * get_json_table(). */ - je = (JsonExpr *) tf->docexpr; tf->passingvalexprs = copyObject(je->passing_values); tf->ordinalitycol = -1; /* undefine ordinality column number */ diff --git a/src/test/regress/expected/sqljson_jsontable.out b/src/test/regress/expected/sqljson_jsontable.out index 1dcb9479de3..c434fc381d1 100644 --- a/src/test/regress/expected/sqljson_jsontable.out +++ b/src/test/regress/expected/sqljson_jsontable.out @@ -2049,3 +2049,12 @@ SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING ROW(1, 2) < ROW(3, 4) AS x t (1 row) +-- PASSING arguments needn't agree on their collations (bug #19872) +SELECT * FROM JSON_TABLE(jsonb '{}', '$' + PASSING 'b' COLLATE "POSIX" AS v1, 'c' COLLATE "C" AS v2 + COLUMNS (n FOR ORDINALITY)); + n +--- + 1 +(1 row) + diff --git a/src/test/regress/sql/sqljson_jsontable.sql b/src/test/regress/sql/sqljson_jsontable.sql index 73e60411df6..255f955298a 100644 --- a/src/test/regress/sql/sqljson_jsontable.sql +++ b/src/test/regress/sql/sqljson_jsontable.sql @@ -1117,3 +1117,8 @@ SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING 'a' < 'b' COLLATE "C" AS x COLUMNS (a bool PATH '$x')); SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING ROW(1, 2) < ROW(3, 4) AS x COLUMNS (a bool PATH '$x')); + +-- PASSING arguments needn't agree on their collations (bug #19872) +SELECT * FROM JSON_TABLE(jsonb '{}', '$' + PASSING 'b' COLLATE "POSIX" AS v1, 'c' COLLATE "C" AS v2 + COLUMNS (n FOR ORDINALITY)); -- 2.37.1 (Apple Git-137.1)