pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chao Li <li.evan.chao@gmail.com>
To: Dewei Dai <daidewei1970@163.com>
Cc: Pgsql Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: [PATCH] Reject duplicate SQL/JSON PASSING argument names
Date: Wed, 18 Nov 2026 14:31:33 +0800
Message-ID: <1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com> (raw)
In-Reply-To: <202611181055551263943@163.com>
References: <202611181055551263943@163.com>



> 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] 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
```
inline patch 1: src/backend/parser/parse_expr.c (+8/-0 lines) download inline-1.patch--- 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] https://www.postgresql.org/message-id/DC2D9FA5-45C4-494F-99FC-F9B95AD4FF55%40gmail.com Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/

view thread (5+ messages)  latest in thread

Message-ID: <1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com>
Permalink:  ../1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com/
Also on:    postgresql.org/message-id/1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: li.evan.chao@gmail.com, daidewei1970@163.com
  Subject: Re: [PATCH] Reject duplicate SQL/JSON PASSING argument names
  In-Reply-To: <1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com>
  References: <202611181055551263943@163.com> <1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox