From 8afce2ad34095d2a4f864b982c59a57602e5773f Mon Sep 17 00:00:00 2001 From: Narayanan Venkateswaran Date: Thu, 26 Nov 2026 15:34:57 +0000 Subject: [PATCH v4] Allow NULL and UNIQUE clauses in empty JSON_OBJECT() and JSON_ARRAY() Commit 7081ac46ace introduced SQL/JSON constructors JSON_OBJECT() and JSON_ARRAY() with documentation presenting the argument list, ON NULL clause, UNIQUE clause, and RETURNING clause as independent optional elements. While ISO/IEC 9075-2 nests the ON NULL and UNIQUE clauses inside the optional group requiring at least one argument expression, PostgreSQL's documentation has always shown them as independent optional clauses (matching other implementations such as Oracle). Previously, gram.y only allowed empty calls via: JSON_OBJECT '(' json_returning_clause_opt ')' JSON_ARRAY '(' json_returning_clause_opt ')' Specifying ON NULL or UNIQUE clauses without key/value or element expressions (for example JSON_OBJECT(ABSENT ON NULL) or JSON_ARRAY(NULL ON NULL)) therefore failed with a syntax error. Factor the non-empty null and uniqueness clauses into json_constructor_null_clause and json_key_uniqueness_constraint, and add conflict-free productions for empty JSON_OBJECT() and JSON_ARRAY() calls with those clauses. Document that specifying ON NULL (and UNIQUE for JSON_OBJECT) without arguments is an extension to the SQL standard, and add regression tests in sqljson covering empty clause combinations, unreserved keyword column names, and EXPLAIN/view deparsing of empty JSON_OBJECT() and JSON_ARRAY() forms. Reported-by: Shallow Discussion: https://postgr.es/m/19737-b44cf2e08708b613@postgresql.org --- doc/src/sgml/func/func-json.sgml | 10 ++- src/backend/parser/gram.y | 61 +++++++++++++-- src/test/regress/expected/sqljson.out | 107 ++++++++++++++++++++++++++ src/test/regress/sql/sqljson.sql | 34 ++++++++ 4 files changed, 204 insertions(+), 8 deletions(-) diff --git a/doc/src/sgml/func/func-json.sgml b/doc/src/sgml/func/func-json.sgml index ede39e7d024..363c6aaf0b4 100644 --- a/doc/src/sgml/func/func-json.sgml +++ b/doc/src/sgml/func/func-json.sgml @@ -617,8 +617,11 @@ Constructs a JSON array from either a series of value_expression parameters or from the results of query_expression, - which must be a SELECT query returning a single column. If - ABSENT ON NULL is specified, NULL values are ignored. + which must be a SELECT query returning a single column. + An empty JSON array is returned if no parameters are given. + As an extension to the SQL standard, the ON NULL + clause can also be specified when no parameters are given. + If ABSENT ON NULL is specified, NULL values are ignored. This is always the case if a query_expression is used. If the query returns no rows, an empty JSON array is returned. @@ -719,6 +722,9 @@ Constructs a JSON object of all the key/value pairs given, or an empty object if none are given. + As an extension to the SQL standard, the ON NULL + and UNIQUE clauses can also be specified when + constructing an empty object. key_expression is a scalar expression defining the JSON key, which is converted to the text type. diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 0563453fe24..6d68d86760a 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -689,6 +689,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); json_table_default_plan_union_cross json_wrapper_behavior %type json_key_uniqueness_constraint_opt + json_key_uniqueness_constraint + json_constructor_null_clause json_object_constructor_null_clause_opt json_array_constructor_null_clause_opt @@ -16484,6 +16486,33 @@ func_expr_common_subexpr: n->location = @1; $$ = (Node *) n; } + | JSON_OBJECT '(' + json_constructor_null_clause + json_key_uniqueness_constraint_opt + json_returning_clause_opt ')' + { + JsonObjectConstructor *n = makeNode(JsonObjectConstructor); + + n->exprs = NIL; + n->absent_on_null = $3; + n->unique = $4; + n->output = (JsonOutput *) $5; + n->location = @1; + $$ = (Node *) n; + } + | JSON_OBJECT '(' + json_key_uniqueness_constraint + json_returning_clause_opt ')' + { + JsonObjectConstructor *n = makeNode(JsonObjectConstructor); + + n->exprs = NIL; + n->absent_on_null = false; + n->unique = $3; + n->output = (JsonOutput *) $4; + n->location = @1; + $$ = (Node *) n; + } | JSON_OBJECT '(' json_returning_clause_opt ')' { JsonObjectConstructor *n = makeNode(JsonObjectConstructor); @@ -16525,6 +16554,19 @@ func_expr_common_subexpr: n->location = @1; $$ = (Node *) n; } + | JSON_ARRAY '(' + json_constructor_null_clause + json_returning_clause_opt + ')' + { + JsonArrayConstructor *n = makeNode(JsonArrayConstructor); + + n->exprs = NIL; + n->absent_on_null = $3; + n->output = (JsonOutput *) $4; + n->location = @1; + $$ = (Node *) n; + } | JSON_ARRAY '(' json_returning_clause_opt ')' @@ -17531,11 +17573,15 @@ json_predicate_type_constraint: * KEYS-less productions a precedence less than IDENT (i.e., less than KEYS). * This prevents reducing them when the next token is KEYS. */ -json_key_uniqueness_constraint_opt: +json_key_uniqueness_constraint: WITH UNIQUE KEYS { $$ = true; } | WITH UNIQUE %prec UNBOUNDED { $$ = true; } | WITHOUT UNIQUE KEYS { $$ = false; } | WITHOUT UNIQUE %prec UNBOUNDED { $$ = false; } + ; + +json_key_uniqueness_constraint_opt: + json_key_uniqueness_constraint { $$ = $1; } | /* EMPTY */ %prec UNBOUNDED { $$ = false; } ; @@ -17559,16 +17605,19 @@ json_name_and_value: { $$ = makeJsonKeyValue($1, $3); } ; +json_constructor_null_clause: + NULL_P ON NULL_P { $$ = false; } + | ABSENT ON NULL_P { $$ = true; } + ; + /* empty means false for objects, true for arrays */ json_object_constructor_null_clause_opt: - NULL_P ON NULL_P { $$ = false; } - | ABSENT ON NULL_P { $$ = true; } - | /* EMPTY */ { $$ = false; } + json_constructor_null_clause { $$ = $1; } + | /* EMPTY */ { $$ = false; } ; json_array_constructor_null_clause_opt: - NULL_P ON NULL_P { $$ = false; } - | ABSENT ON NULL_P { $$ = true; } + json_constructor_null_clause { $$ = $1; } | /* EMPTY */ { $$ = true; } ; diff --git a/src/test/regress/expected/sqljson.out b/src/test/regress/expected/sqljson.out index 1085475541b..8289d9bb315 100644 --- a/src/test/regress/expected/sqljson.out +++ b/src/test/regress/expected/sqljson.out @@ -295,6 +295,54 @@ SELECT JSON_OBJECT(); {} (1 row) +SELECT JSON_OBJECT(NULL ON NULL); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(ABSENT ON NULL); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(WITH UNIQUE KEYS); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(WITH UNIQUE RETURNING jsonb); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(WITHOUT UNIQUE); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(WITHOUT UNIQUE KEYS); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(NULL ON NULL WITH UNIQUE KEYS); + json_object +------------- + {} +(1 row) + +SELECT JSON_OBJECT(ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb); + json_object +------------- + {} +(1 row) + SELECT JSON_OBJECT(RETURNING json); json_object ------------- @@ -617,6 +665,37 @@ SELECT JSON_ARRAY(); [] (1 row) +SELECT JSON_ARRAY(NULL ON NULL); + json_array +------------ + [] +(1 row) + +SELECT JSON_ARRAY(ABSENT ON NULL); + json_array +------------ + [] +(1 row) + +SELECT JSON_ARRAY(NULL ON NULL RETURNING jsonb); + json_array +------------ + [] +(1 row) + +SELECT JSON_ARRAY(ABSENT ON NULL RETURNING jsonb); + json_array +------------ + [] +(1 row) + +SELECT JSON_OBJECT(absent: 1), JSON_OBJECT(without: 2), JSON_ARRAY(absent) +FROM (VALUES ('a', 'b')) t(absent, without); + json_object | json_object | json_array +-------------+-------------+------------ + {"a" : 1} | {"b" : 2} | ["a"] +(1 row) + SELECT JSON_ARRAY(RETURNING json); json_array ------------ @@ -1033,6 +1112,20 @@ SELECT JSON_OBJECT('foo' : '1' FORMAT JSON, 'bar' : 'baz' RETURNING json); CREATE OR REPLACE VIEW public.json_object_view AS SELECT JSON_OBJECT('foo' : '1'::text FORMAT JSON, 'bar' : 'baz'::text RETURNING json) AS "json_object" DROP VIEW json_object_view; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT JSON_OBJECT(ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb); + QUERY PLAN +------------------------------------------------------------------------- + Result + Output: JSON_OBJECT( ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb) +(2 rows) + +CREATE VIEW json_object_view AS +SELECT JSON_OBJECT(ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb); +\sv json_object_view +CREATE OR REPLACE VIEW public.json_object_view AS + SELECT JSON_OBJECT( ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb) AS "json_object" +DROP VIEW json_object_view; SELECT to_json(a) AS a, JSON_OBJECTAGG(k : v WITH UNIQUE KEYS) OVER (ORDER BY k) FROM (VALUES (1,1), (2,2)) a(k,v); a | json_objectagg @@ -1083,6 +1176,20 @@ SELECT JSON_ARRAY('1' FORMAT JSON, 2 RETURNING json); CREATE OR REPLACE VIEW public.json_array_view AS SELECT JSON_ARRAY('1'::text FORMAT JSON, 2 RETURNING json) AS "json_array" DROP VIEW json_array_view; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT JSON_ARRAY(NULL ON NULL RETURNING jsonb); + QUERY PLAN +----------------------------------------------------- + Result + Output: JSON_ARRAY( NULL ON NULL RETURNING jsonb) +(2 rows) + +CREATE VIEW json_array_view AS +SELECT JSON_ARRAY(NULL ON NULL RETURNING jsonb); +\sv json_array_view +CREATE OR REPLACE VIEW public.json_array_view AS + SELECT JSON_ARRAY( NULL ON NULL RETURNING jsonb) AS "json_array" +DROP VIEW json_array_view; -- Test JSON_OBJECTAGG deparsing EXPLAIN (VERBOSE, COSTS OFF) SELECT JSON_OBJECTAGG(i: ('111' || i)::bytea FORMAT JSON WITH UNIQUE RETURNING text) FILTER (WHERE i > 3) diff --git a/src/test/regress/sql/sqljson.sql b/src/test/regress/sql/sqljson.sql index 43106fdda96..3d2214a502a 100644 --- a/src/test/regress/sql/sqljson.sql +++ b/src/test/regress/sql/sqljson.sql @@ -64,6 +64,14 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT JSON_SERIALIZE('{}' RETURNING bytea); -- JSON_OBJECT() SELECT JSON_OBJECT(); +SELECT JSON_OBJECT(NULL ON NULL); +SELECT JSON_OBJECT(ABSENT ON NULL); +SELECT JSON_OBJECT(WITH UNIQUE KEYS); +SELECT JSON_OBJECT(WITH UNIQUE RETURNING jsonb); +SELECT JSON_OBJECT(WITHOUT UNIQUE); +SELECT JSON_OBJECT(WITHOUT UNIQUE KEYS); +SELECT JSON_OBJECT(NULL ON NULL WITH UNIQUE KEYS); +SELECT JSON_OBJECT(ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb); SELECT JSON_OBJECT(RETURNING json); SELECT JSON_OBJECT(RETURNING json FORMAT JSON); SELECT JSON_OBJECT(RETURNING jsonb); @@ -177,6 +185,12 @@ DROP TYPE mood; -- JSON_ARRAY() SELECT JSON_ARRAY(); +SELECT JSON_ARRAY(NULL ON NULL); +SELECT JSON_ARRAY(ABSENT ON NULL); +SELECT JSON_ARRAY(NULL ON NULL RETURNING jsonb); +SELECT JSON_ARRAY(ABSENT ON NULL RETURNING jsonb); +SELECT JSON_OBJECT(absent: 1), JSON_OBJECT(without: 2), JSON_ARRAY(absent) +FROM (VALUES ('a', 'b')) t(absent, without); SELECT JSON_ARRAY(RETURNING json); SELECT JSON_ARRAY(RETURNING json FORMAT JSON); SELECT JSON_ARRAY(RETURNING jsonb); @@ -343,6 +357,16 @@ SELECT JSON_OBJECT('foo' : '1' FORMAT JSON, 'bar' : 'baz' RETURNING json); DROP VIEW json_object_view; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT JSON_OBJECT(ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb); + +CREATE VIEW json_object_view AS +SELECT JSON_OBJECT(ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb); + +\sv json_object_view + +DROP VIEW json_object_view; + SELECT to_json(a) AS a, JSON_OBJECTAGG(k : v WITH UNIQUE KEYS) OVER (ORDER BY k) FROM (VALUES (1,1), (2,2)) a(k,v); @@ -372,6 +396,16 @@ SELECT JSON_ARRAY('1' FORMAT JSON, 2 RETURNING json); DROP VIEW json_array_view; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT JSON_ARRAY(NULL ON NULL RETURNING jsonb); + +CREATE VIEW json_array_view AS +SELECT JSON_ARRAY(NULL ON NULL RETURNING jsonb); + +\sv json_array_view + +DROP VIEW json_array_view; + -- Test JSON_OBJECTAGG deparsing EXPLAIN (VERBOSE, COSTS OFF) SELECT JSON_OBJECTAGG(i: ('111' || i)::bytea FORMAT JSON WITH UNIQUE RETURNING text) FILTER (WHERE i > 3) -- 2.56.1.839.g22a8290db9-goog