[PATCH] Reject duplicate SQL/JSON PASSING argument names

Started by Dewei Dai7 days ago6 messageshackers
Jump to latest
#1Dewei Dai
daidewei1970@163.com

Hi,

The attached patch makes SQL/JSON PASSING clauses reject duplicate
variable names instead of silently using the first matching value.

For example, this previously returned only "comedy":

SELECT jt.kind
FROM JSON_TABLE(
'{ "favorites":[{ "kind" : "comedy" },{ "kind" : "drama" }] }',
'$.favorites[*]?(@.kind == $k1)'
PASSING 'comedy' AS k1, 'drama' AS k1
COLUMNS (kind text PATH '$.kind')
) AS jt;

With the patch it raises:

ERROR: PASSING argument name "k1" is not unique

The same check applies to JSON_EXISTS, JSON_QUERY, JSON_VALUE, and
JSON_TABLE because they all use transformJsonPassingArgs(). The patch
also adds regression tests for JSON_QUERY and JSON_TABLE.

The patch was tested with the full regression suite on current master.

Thanks,
DeWei Dai
HighGo Software Co., Ltd.
https://www.highgo.com/

daidewei1970@163.com

Attachments:

t259308_1
0001-Reject-duplicate-PASSING-argument-names.patchapplication/octet-stream; name=0001-Reject-duplicate-PASSING-argument-names.patchDownload+43-1
#2Chao Li
li.evan.chao@gmail.com
In reply to: Dewei Dai (#1)
Re: [PATCH] Reject duplicate SQL/JSON PASSING argument names

On Nov 18, 2026, at 10:55, Dewei Dai <daidewei1970@163.com> wrote:

Hi,

The attached patch makes SQL/JSON PASSING clauses reject duplicate
variable names instead of silently using the first matching value.

For example, this previously returned only "comedy":

SELECT jt.kind
FROM JSON_TABLE(
'{ "favorites":[{ "kind" : "comedy" },{ "kind" : "drama" }] }',
'$.favorites[*]?(@.kind == $k1)'
PASSING 'comedy' AS k1, 'drama' AS k1
COLUMNS (kind text PATH '$.kind')
) AS jt;

With the patch it raises:

ERROR: PASSING argument name "k1" is not unique

The same check applies to JSON_EXISTS, JSON_QUERY, JSON_VALUE, and
JSON_TABLE because they all use transformJsonPassingArgs(). The patch
also adds regression tests for JSON_QUERY and JSON_TABLE.

The patch was tested with the full regression suite on current master.

I think it's reasonable to reject duplicate PASSING argument names.

Oracle also rejects duplicates:
```
SELECT JSON_VALUE(
'{"a":1}',
'$.a?(@ == $x)'
PASSING 1 AS "x", 2 AS "x"
) FROM dual;

ORA-40464: JSON PASSING clause contained duplicate variable name 'x'.
```

I'm not trying to say PG has to follow Oracle, but explicitly rejecting duplicates is better than silently making a decision for users. I happen to have an ongoing patch [1]/messages/by-id/DC2D9FA5-45C4-494F-99FC-F9B95AD4FF55@gmail.com that is under discussion between two approaches: 1) silently discarding empty path elements or 2) rejecting empty path elements, and people seem to be more keen on explicitly rejecting them. I think that's in the same spirit as this rejection.

Also, when there are duplicate names, users may be confused about whether the earlier one or the later one should win. And the current doc doesn't have an explanation for that.

Going back to the patch code changes, I have a few comments.

1
```
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -4792,12 +4792,21 @@ transformJsonPassingArgs(ParseState *pstate, const char *constructName,
 	foreach(lc, args)
 	{
 		JsonArgument *arg = castNode(JsonArgument, lfirst(lc));
+		String	   *name = makeString(arg->name);
 		Node	   *expr = transformJsonValueExpr(pstate, constructName,
 												  arg->val, format,
 												  InvalidOid, true);
+		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))));
```

The result of transformJsonValueExpr() doesn't matter for the dup name check, so we can do the check before calling transformJsonValueExpr().

2
```
+			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))));
```

Modern ereport() style doesn't require the extra parentheses around errcode(), errmsg(), etc. Since this file already has a mix of old and new styles, I think we should use the modern style for new code.

3. The current doc in func-json.sgml doesn't explicitly say anything about duplicate PASSING argument names:
```
The optional <literal>PASSING</literal> clause provides data values for
the variables mentioned in the <replaceable>path_expression</replaceable>.
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.
```

So, I think this patch should update the doc accordingly, for example, adding a simple sentence saying that duplicate PASSING argument names are not allowed.

4. The tests cover JSON_QUERY and JSON_TABLE, but as you explained in the patch email, JSON_EXISTS and JSON_VALUE are also affected. Maybe add test cases for JSON_EXISTS and JSON_VALUE as well.

5. Maybe also mention JSON_EXISTS, JSON_QUERY, and JSON_VALUE in the commit message, as they are fixed by this patch as well.

[1]: /messages/by-id/DC2D9FA5-45C4-494F-99FC-F9B95AD4FF55@gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Dewei Dai
daidewei1970@163.com
In reply to: Chao Li (#2)
回复: Re: [PATCH] Reject duplicate SQL/JSON PASSING argument names

Thanks for the review.

On 2026-11-18 14:31:33, Chao Li wrote:

1
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
...
The result of transformJsonValueExpr() doesn't matter for the dup name check, so we can do the check before calling transformJsonValueExpr().

Fixed. I moved the duplicate name check before calling transformJsonValueExpr().

2
...
Modern ereport() style doesn't require the extra parentheses around errcode(), errmsg(), etc. Since this file already has a mix of old and new styles, I think we should use the modern style for new code.

Fixed. I updated the new ereport() call to use the modern style without extra parentheses.

3. The current doc in func-json.sgml doesn't explicitly say anything about duplicate PASSING argument names:
...
So, I think this patch should update the doc accordingly, for example, adding a simple sentence saying that duplicate PASSING argument names are not allowed.

Fixed. I added a sentence to func-json.sgml stating that the names of the variables in the PASSING clause must be unique.

4. The tests cover JSON_QUERY and JSON_TABLE, but as you explained in the patch email, JSON_EXISTS and JSON_VALUE are also affected. Maybe add test cases for JSON_EXISTS and JSON_VALUE as well.

Fixed. I added regression tests for JSON_EXISTS and JSON_VALUE.

5. Maybe also mention JSON_EXISTS, JSON_QUERY, and JSON_VALUE in the commit message, as they are fixed by this patch as well.

Fixed. I updated the commit message to mention all affected functions.

Best regards,
Dewei Dai

HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t259308_3
v2-0001-Reject-duplicate-SQL-JSON-PASSING-argument-names.patchapplication/octet-stream; name=v2-0001-Reject-duplicate-SQL-JSON-PASSING-argument-names.patchDownload+61-5
#4Chao Li
li.evan.chao@gmail.com
In reply to: Dewei Dai (#3)
Re: [PATCH] Reject duplicate SQL/JSON PASSING argument names

On Nov 18, 2026, at 17:50, Dewei Dai <daidewei1970@163.com> wrote:

Thanks for the review.

On 2026-11-18 14:31:33, Chao Li wrote:

1
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
...
The result of transformJsonValueExpr() doesn't matter for the dup name check, so we can do the check before calling transformJsonValueExpr().

Fixed. I moved the duplicate name check before calling transformJsonValueExpr().

2
...
Modern ereport() style doesn't require the extra parentheses around errcode(), errmsg(), etc. Since this file already has a mix of old and new styles, I think we should use the modern style for new code.

Fixed. I updated the new ereport() call to use the modern style without extra parentheses.

3. The current doc in func-json.sgml doesn't explicitly say anything about duplicate PASSING argument names:
...
So, I think this patch should update the doc accordingly, for example, adding a simple sentence saying that duplicate PASSING argument names are not allowed.

Fixed. I added a sentence to func-json.sgml stating that the names of the variables in the PASSING clause must be unique.

4. The tests cover JSON_QUERY and JSON_TABLE, but as you explained in the patch email, JSON_EXISTS and JSON_VALUE are also affected. Maybe add test cases for JSON_EXISTS and JSON_VALUE as well.

Fixed. I added regression tests for JSON_EXISTS and JSON_VALUE.

5. Maybe also mention JSON_EXISTS, JSON_QUERY, and JSON_VALUE in the commit message, as they are fixed by this patch as well.

Fixed. I updated the commit message to mention all affected functions.

Best regards,
Dewei Dai

HighGo Software Co., Ltd.
https://www.highgo.com/

<v2-0001-Reject-duplicate-SQL-JSON-PASSING-argument-names.patch>

Thanks for updating the patch. V2 looks good to me.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#5Dewei Dai
daidewei1970@163.com
In reply to: Chao Li (#4)
Re: [PATCH v2] Reject duplicate SQL/JSON PASSING argument names

Hi,

I've registered this patch in the current CommitFest:
https://commitfest.postgresql.org/patch/7671/

Best regards,
Dewei Dai

HighGo Software Co., Ltd.
https://www.highgo.com/

#6Chao Li
li.evan.chao@gmail.com
In reply to: Dewei Dai (#5)
Re: [PATCH v2] Reject duplicate SQL/JSON PASSING argument names

On Nov 20, 2026, at 13:59, Dewei Dai <daidewei1970@163.com> wrote:

Hi,

I've registered this patch in the current CommitFest:
https://commitfest.postgresql.org/patch/7671/

Best regards,
Dewei Dai

HighGo Software Co., Ltd.
https://www.highgo.com/

Thanks. I have marked it as ready for committer.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/