From 349d69789a25ee842a08d8b09b872f4fb8210d6f Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Sun, 15 Nov 2026 21:03:11 +0100
Subject: [PATCH v1 05/11] Test SQL/JSON domain error fallbacks

Check JSON_VALUE and JSON_QUERY with NULL-rejecting RETURNING domains
and non-NULL ON ERROR values. Require SQL NULL after outer-join
null-extension and for NULL arguments of STRICT SQL functions.

A NULL context item of JSON_VALUE() / JSON_QUERY() is still coerced to
a domain RETURNING type.  If the domain rejects NULL, ON ERROR applies,
so with a non-NULL ON ERROR value the expression is not strict, but the
planner currently treats it as strict.

Add tests for subquery pull-up and inlining of STRICT SQL functions,
which produce wrong results.  The expected output records the correct
results, so these tests fail until fixed by the following commit.
---
 .../regress/expected/sqljson_queryfuncs.out   | 57 +++++++++++++++++++
 src/test/regress/sql/sqljson_queryfuncs.sql   | 35 ++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out
index 16eb84837ad..2acb2253ce9 100644
--- a/src/test/regress/expected/sqljson_queryfuncs.out
+++ b/src/test/regress/expected/sqljson_queryfuncs.out
@@ -2260,3 +2260,60 @@ FROM (VALUES (1), (NULL)) v(x);
 DROP FUNCTION queryfuncs_strict_passing_query, queryfuncs_strict_passing_exists,
   queryfuncs_strict_default_query;
 DROP FUNCTION queryfuncs_strict_passing, queryfuncs_strict_default;
+-- A NULL context item is still coerced to a domain RETURNING type, and if the
+-- domain rejects NULL, ON ERROR applies.  So with a non-NULL ON ERROR value,
+-- the JsonExpr is not strict even in its context item.
+CREATE DOMAIN queryfuncs_int_notnull AS int NOT NULL;
+CREATE DOMAIN queryfuncs_jsonb_notnull AS jsonb NOT NULL;
+-- the ON ERROR values are returned for a NULL context item
+SELECT JSON_VALUE(NULL::jsonb, '$' RETURNING queryfuncs_int_notnull DEFAULT 0 ON ERROR),
+       JSON_QUERY(NULL::jsonb, '$' RETURNING queryfuncs_jsonb_notnull EMPTY ARRAY ON ERROR);
+ json_value | json_query 
+------------+------------
+          0 | []
+(1 row)
+
+-- so it must not be treated as strict when pulling up a subquery: jv and jq
+-- must be NULL for the unmatched x = 1
+SELECT x, y, jv, jq
+FROM (VALUES (1), (2)) a(x)
+     LEFT JOIN (SELECT y,
+                       JSON_VALUE(j, '$' RETURNING queryfuncs_int_notnull
+                                  DEFAULT 0 ON ERROR) AS jv,
+                       JSON_QUERY(j, '$' RETURNING queryfuncs_jsonb_notnull
+                                  EMPTY ARRAY ON ERROR) AS jq
+                FROM (VALUES (2, jsonb '2'), (3, jsonb '3')) b(y, j)) ss ON x = y
+ORDER BY x;
+ x | y | jv | jq 
+---+---+----+----
+ 1 |   |    | 
+ 2 | 2 |  2 | 2
+(2 rows)
+
+-- nor when inlining a STRICT SQL function: it must return NULL for NULL input
+CREATE FUNCTION queryfuncs_strict_domain(jsonb) RETURNS int
+  LANGUAGE sql IMMUTABLE STRICT
+  AS $$ SELECT JSON_VALUE($1, '$' RETURNING queryfuncs_int_notnull
+                          DEFAULT 0 ON ERROR) $$;
+SELECT x, queryfuncs_strict_domain(x) FROM (VALUES ('1'::jsonb), (NULL)) v(x);
+ x | queryfuncs_strict_domain 
+---+--------------------------
+ 1 |                        1
+   |                         
+(2 rows)
+
+-- the same for JSON_QUERY()
+CREATE FUNCTION queryfuncs_strict_domain_query(jsonb) RETURNS jsonb
+  LANGUAGE sql IMMUTABLE STRICT
+  AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_notnull
+                          EMPTY ARRAY ON ERROR) $$;
+SELECT x, queryfuncs_strict_domain_query(x)
+FROM (VALUES ('[1]'::jsonb), (NULL)) v(x);
+  x  | queryfuncs_strict_domain_query 
+-----+--------------------------------
+ [1] | [1]
+     | 
+(2 rows)
+
+DROP FUNCTION queryfuncs_strict_domain, queryfuncs_strict_domain_query;
+DROP DOMAIN queryfuncs_int_notnull, queryfuncs_jsonb_notnull;
diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql
index c732c92e353..0d4eb899419 100644
--- a/src/test/regress/sql/sqljson_queryfuncs.sql
+++ b/src/test/regress/sql/sqljson_queryfuncs.sql
@@ -868,3 +868,38 @@ FROM (VALUES (1), (NULL)) v(x);
 DROP FUNCTION queryfuncs_strict_passing_query, queryfuncs_strict_passing_exists,
   queryfuncs_strict_default_query;
 DROP FUNCTION queryfuncs_strict_passing, queryfuncs_strict_default;
+
+-- A NULL context item is still coerced to a domain RETURNING type, and if the
+-- domain rejects NULL, ON ERROR applies.  So with a non-NULL ON ERROR value,
+-- the JsonExpr is not strict even in its context item.
+CREATE DOMAIN queryfuncs_int_notnull AS int NOT NULL;
+CREATE DOMAIN queryfuncs_jsonb_notnull AS jsonb NOT NULL;
+-- the ON ERROR values are returned for a NULL context item
+SELECT JSON_VALUE(NULL::jsonb, '$' RETURNING queryfuncs_int_notnull DEFAULT 0 ON ERROR),
+       JSON_QUERY(NULL::jsonb, '$' RETURNING queryfuncs_jsonb_notnull EMPTY ARRAY ON ERROR);
+-- so it must not be treated as strict when pulling up a subquery: jv and jq
+-- must be NULL for the unmatched x = 1
+SELECT x, y, jv, jq
+FROM (VALUES (1), (2)) a(x)
+     LEFT JOIN (SELECT y,
+                       JSON_VALUE(j, '$' RETURNING queryfuncs_int_notnull
+                                  DEFAULT 0 ON ERROR) AS jv,
+                       JSON_QUERY(j, '$' RETURNING queryfuncs_jsonb_notnull
+                                  EMPTY ARRAY ON ERROR) AS jq
+                FROM (VALUES (2, jsonb '2'), (3, jsonb '3')) b(y, j)) ss ON x = y
+ORDER BY x;
+-- nor when inlining a STRICT SQL function: it must return NULL for NULL input
+CREATE FUNCTION queryfuncs_strict_domain(jsonb) RETURNS int
+  LANGUAGE sql IMMUTABLE STRICT
+  AS $$ SELECT JSON_VALUE($1, '$' RETURNING queryfuncs_int_notnull
+                          DEFAULT 0 ON ERROR) $$;
+SELECT x, queryfuncs_strict_domain(x) FROM (VALUES ('1'::jsonb), (NULL)) v(x);
+-- the same for JSON_QUERY()
+CREATE FUNCTION queryfuncs_strict_domain_query(jsonb) RETURNS jsonb
+  LANGUAGE sql IMMUTABLE STRICT
+  AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_notnull
+                          EMPTY ARRAY ON ERROR) $$;
+SELECT x, queryfuncs_strict_domain_query(x)
+FROM (VALUES ('[1]'::jsonb), (NULL)) v(x);
+DROP FUNCTION queryfuncs_strict_domain, queryfuncs_strict_domain_query;
+DROP DOMAIN queryfuncs_int_notnull, queryfuncs_jsonb_notnull;
-- 
2.56.0

