From 7a9c4c51b43353c5ea260c7f899946347f297119 Mon Sep 17 00:00:00 2001
From: DeWei Dai <daidewei1970@163.com>
Date: Wed, 18 Nov 2026 10:38:31 +0800
Subject: [PATCH v2] Reject duplicate SQL/JSON PASSING argument names

SQL/JSON query functions (JSON_EXISTS, JSON_QUERY, and JSON_VALUE) and
JSON_TABLE allow a PASSING clause to bind variables used in the path
expression.  The SQL standard requires the variable names in a PASSING
clause to be unique, but transformJsonPassingArgs() did not enforce that.
GetJsonPathVar() then selected the first matching variable, so later
values with the same name were silently ignored.

Reject duplicate names during parse analysis using
ERRCODE_DUPLICATE_ALIAS, document the requirement, and add regression
coverage for JSON_EXISTS, JSON_QUERY, JSON_VALUE, and JSON_TABLE.

For example, this previously returned only "comedy", depending on which
value was listed first:

    PASSING 'comedy' AS k1, 'drama' AS k1
---
 doc/src/sgml/func/func-json.sgml               |  4 ++++
 src/backend/parser/parse_expr.c                | 18 ++++++++++++++----
 .../regress/expected/sqljson_jsontable.out     | 11 +++++++++++
 .../regress/expected/sqljson_queryfuncs.out    | 17 +++++++++++++++++
 src/test/regress/sql/sqljson_jsontable.sql     |  9 +++++++++
 src/test/regress/sql/sqljson_queryfuncs.sql    |  6 ++++++
 6 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/func/func-json.sgml b/doc/src/sgml/func/func-json.sgml
index ede39e7d024..9b2e3f66399 100644
--- a/doc/src/sgml/func/func-json.sgml
+++ b/doc/src/sgml/func/func-json.sgml
@@ -3326,6 +3326,8 @@ $.* ? (@ like_regex "^\\d+$")
    <replaceable>path_expression</replaceable> can also reference variables,
    whose values are specified with their respective names in the
    <literal>PASSING</literal> clause that is supported by each function.
+   The names of the variables in the <literal>PASSING</literal> clause must
+   be unique.
    <replaceable>context_item</replaceable> can be a <type>jsonb</type> value
    or a character string that can be successfully cast to <type>jsonb</type>.
   </para>
@@ -3713,6 +3715,8 @@ where <replaceable class="parameter">json_table_column</replaceable> is:
      is an optional name for the <replaceable>path_expression</replaceable>.
      The optional <literal>PASSING</literal> clause provides data values for
      the variables mentioned in the <replaceable>path_expression</replaceable>.
+     The names of the variables in the <literal>PASSING</literal> clause must
+     be unique.
      The result of the input data evaluation using the aforementioned elements
      is called the <firstterm>row pattern</firstterm>, which is used as the
      source for row values in the constructed view.
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index e207b7a1978..dd81d30c266 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -4790,12 +4790,22 @@ transformJsonPassingArgs(ParseState *pstate, const char *constructName,
 	foreach(lc, args)
 	{
 		JsonArgument *arg = castNode(JsonArgument, lfirst(lc));
-		Node	   *expr = transformJsonValueExpr(pstate, constructName,
-												  arg->val, format,
-												  InvalidOid, true);
+		String	   *name = makeString(arg->name);
+		Node	   *expr;
+
+		if (list_member(*passing_names, name))
+			ereport(ERROR,
+					errcode(ERRCODE_DUPLICATE_ALIAS),
+					errmsg("PASSING argument name \"%s\" is not unique", arg->name),
+					parser_errposition(pstate,
+									   exprLocation((Node *) arg->val->raw_expr)));
+
+		expr = transformJsonValueExpr(pstate, constructName,
+									  arg->val, format,
+									  InvalidOid, true);
 
 		*passing_values = lappend(*passing_values, expr);
-		*passing_names = lappend(*passing_names, makeString(arg->name));
+		*passing_names = lappend(*passing_names, name);
 	}
 }
 
diff --git a/src/test/regress/expected/sqljson_jsontable.out b/src/test/regress/expected/sqljson_jsontable.out
index 1dcb9479de3..021eef7f562 100644
--- a/src/test/regress/expected/sqljson_jsontable.out
+++ b/src/test/regress/expected/sqljson_jsontable.out
@@ -755,6 +755,17 @@ FROM JSON_TABLE(
  2
 (2 rows)
 
+-- Duplicate PASSING argument names are not allowed
+SELECT *
+FROM JSON_TABLE(
+	'{ "favorites":[{ "kind":"comedy" },{ "kind":"drama" }] }',
+	'$.favorites[*]?(@.kind == $k1)'
+		PASSING 'comedy' AS k1, 'drama' AS k1
+		COLUMNS (kind text PATH '$.kind')
+	) jt;
+ERROR:  PASSING argument name "k1" is not unique
+LINE 5:   PASSING 'comedy' AS k1, 'drama' AS k1
+                                  ^
 -- PASSING arguments are also passed to column paths
 SELECT *
 FROM JSON_TABLE(
diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out
index 19db825c8ab..7712e15337c 100644
--- a/src/test/regress/expected/sqljson_queryfuncs.out
+++ b/src/test/regress/expected/sqljson_queryfuncs.out
@@ -1546,6 +1546,23 @@ SELECT JSON_QUERY(jsonb 'null', '$"Xyz"' PASSING 1 AS "Xyz");
  1
 (1 row)
 
+-- Duplicate PASSING argument names are not allowed
+SELECT JSON_QUERY(jsonb 'null', '$x' PASSING 1 AS x, 2 AS x);
+ERROR:  PASSING argument name "x" is not unique
+LINE 1: ...ELECT JSON_QUERY(jsonb 'null', '$x' PASSING 1 AS x, 2 AS x);
+                                                               ^
+SELECT JSON_QUERY(jsonb 'null', '$x' PASSING 1 AS x, 2 AS "x");
+ERROR:  PASSING argument name "x" is not unique
+LINE 1: ...ECT JSON_QUERY(jsonb 'null', '$x' PASSING 1 AS x, 2 AS "x");
+                                                             ^
+SELECT JSON_EXISTS(jsonb 'null', '$x' PASSING 1 AS x, 2 AS x);
+ERROR:  PASSING argument name "x" is not unique
+LINE 1: ...LECT JSON_EXISTS(jsonb 'null', '$x' PASSING 1 AS x, 2 AS x);
+                                                               ^
+SELECT JSON_VALUE(jsonb 'null', '$x' PASSING 1 AS x, 2 AS "x");
+ERROR:  PASSING argument name "x" is not unique
+LINE 1: ...ECT JSON_VALUE(jsonb 'null', '$x' PASSING 1 AS x, 2 AS "x");
+                                                             ^
 -- Test PASSING and toasted values
 CREATE TABLE test_passing_toast (t text);
 ALTER TABLE test_passing_toast ALTER COLUMN t SET STORAGE EXTENDED;
diff --git a/src/test/regress/sql/sqljson_jsontable.sql b/src/test/regress/sql/sqljson_jsontable.sql
index 73e60411df6..1957f2c7c23 100644
--- a/src/test/regress/sql/sqljson_jsontable.sql
+++ b/src/test/regress/sql/sqljson_jsontable.sql
@@ -342,6 +342,15 @@ FROM JSON_TABLE(
 		COLUMNS (y text FORMAT JSON PATH '$')
 	) jt;
 
+-- Duplicate PASSING argument names are not allowed
+SELECT *
+FROM JSON_TABLE(
+	'{ "favorites":[{ "kind":"comedy" },{ "kind":"drama" }] }',
+	'$.favorites[*]?(@.kind == $k1)'
+		PASSING 'comedy' AS k1, 'drama' AS k1
+		COLUMNS (kind text PATH '$.kind')
+	) jt;
+
 -- PASSING arguments are also passed to column paths
 SELECT *
 FROM JSON_TABLE(
diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql
index 5c675d98235..1e435f323e6 100644
--- a/src/test/regress/sql/sqljson_queryfuncs.sql
+++ b/src/test/regress/sql/sqljson_queryfuncs.sql
@@ -517,6 +517,12 @@ SELECT JSON_QUERY(jsonb 'null', '$Xyz' PASSING 1 AS Xyz);
 SELECT JSON_QUERY(jsonb 'null', '$Xyz' PASSING 1 AS "Xyz");
 SELECT JSON_QUERY(jsonb 'null', '$"Xyz"' PASSING 1 AS "Xyz");
 
+-- Duplicate PASSING argument names are not allowed
+SELECT JSON_QUERY(jsonb 'null', '$x' PASSING 1 AS x, 2 AS x);
+SELECT JSON_QUERY(jsonb 'null', '$x' PASSING 1 AS x, 2 AS "x");
+SELECT JSON_EXISTS(jsonb 'null', '$x' PASSING 1 AS x, 2 AS x);
+SELECT JSON_VALUE(jsonb 'null', '$x' PASSING 1 AS x, 2 AS "x");
+
 -- Test PASSING and toasted values
 CREATE TABLE test_passing_toast (t text);
 ALTER TABLE test_passing_toast ALTER COLUMN t SET STORAGE EXTENDED;
-- 
2.43.0

