Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTZDK-00000000PD1-1UGm for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Nov 2026 06:32:14 +0000 Received: from localhost ([127.0.0.1]:54056 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xTZDI-00000009Wxv-1x8I for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Nov 2026 06:32:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:39878) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTZDH-00000009Wxn-3it2 for pgsql-hackers@lists.postgresql.org; Wed, 18 Nov 2026 06:32:12 +0000 Received: from mail-dy1-x1333.google.com ([2607:f8b0:4864:20::1333]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1xTZDE-00000000GmX-2wRn for pgsql-hackers@lists.postgresql.org; Wed, 18 Nov 2026 06:32:11 +0000 Received: by mail-dy1-x1333.google.com with SMTP id 5a478bee46e88-38318d69173so289424eec.0 for ; Tue, 17 Nov 2026 22:32:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794983528; x=1795588328; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:content-type:from:to:cc :subject:date:message-id:reply-to:content-type; bh=jQ+jv1NdpQ5U3ZPXsQ8u8r8q1RkQ1oITZ6Pc/NKn2Mw=; b=wXldYe+f2DeyGkRcWoFyqD7pROO90wIKB35zogCg7NejZDSf3Vtbs8ADYYUxHhIdZb BJEdsZ4BQi6497fB1UxhJWDuqdgAT53DhpYR9IL57ftwFk6B/acmhj1QJsJMw2ueJ3rx +et8jjZwc2syEMYkm3aLZguWJwSgWci9WyEf4UeUz53dKnJQCKsj8f6q8t02B1I5Nu7N f0MEEh5K+zaV26/C184bO7r75qw+8Pilz5O6pG5ET7C2XaEiApjJ/OQJYAY9NYxt1qWN 0bjRlFeDOlVeOyos+eG7jTFmj9bJBHztJ2m+teBn0gTmyh2WLn6lJmUUQYzIHWXn8JHx cvhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794983528; x=1795588328; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jQ+jv1NdpQ5U3ZPXsQ8u8r8q1RkQ1oITZ6Pc/NKn2Mw=; b=xgxCgooKfvQZ0+MEtjBMjS+pYBfyzgfSqLks74J6bRGWVR8l5x6b6Ren8ojJ0eQ0lB 99UKFhEdqr15vGG6az6KEd4DMq5tq9Bp+hHzlaFX9nSipvK4jIHtikjtCFJhb/igAUY9 KRoiELCK9OFE1FOctLm6p5ymFqJX/I2rqMneY8Lw5ASwaO0u29qBxlJ87rPSuozcz0vD mUzdO1PfHTh4W/oq8wIVF/DliOEgA+uWisM+YQVRCuyHjNTeRP07xa075If0RIh7wjuy pH4mre9so6c/4J1+3c/PrSnwSBRfFBEat869BYQZPebOSi/1Gk0bhN4aVjo96s4PEHap p9Kg== X-Gm-Message-State: APuU4Z8exbdOUSY4zy+LCXoOq3FJ/wnlx9xWJlrgKmTp0DEk7I3o8zsS pbS3EuEUTiT1005AdopaFeD8IZ1x8AC9btha+/xjXnsU4DAiux6+ZF4D X-Gm-Gg: AZFdTZfyiXGz4V7hbglNMsjgwYGhkRn6KisCezb1Yebhp7uPyvFyAj2iVF9FCJjytte JVqpFuzxRq4QKufhqyvRxpR4ZThGfDhibXSRgH/5j/9GtfTuoZm4YLc4BT9Gq+6pnuWN/u26EkG FGfsaK7agaik3x9jNq3lwuAdd+KAyUOYmrMwVx3C17Q+oiGTaTYftX3EpNBRRehUSYTxs80GGc3 j16q3mSq91NNSSbG4UmeANeP9lsNtYCtKm1LAFqQemNVyYcxn/SGJhSW0QrhjGEWN2qKthTA1XM qfMl9DCthkXWmIy7HtXuxKsaZluPmvDZzWRlqC9Y1bC4N+RqATSvxhE8ZvR3lPIjyIJJtTCUYxr IFAaTn3fPwo7BPWI3ZLTGP6iV6tnaoUiJGo2/hpTn9vdbSHgScPMaaGffl6sdcr0dnVi8GFUwNy VgR+IJaPzinsae8urgfLv5CyjYSsx81W4RoMZRpt58UPoVGdJrVxUfGRYqK2OT0qhiDERMGzW9X zCXWG820miHNsRD+g4ZPTdCeO4RLD2G11NEZmDVrXGCPAwWmiwkigfMKXtPWenJk0jUrv5mYKlK xsVJBcNGHnIX9BBoBp5PLPGov7bmnEx6MKbLfQ== X-Received: by 2002:a05:7300:b197:b0:380:c3d7:c263 with SMTP id 5a478bee46e88-3827dd04b3bmr1854878eec.5.1794983527722; Tue, 17 Nov 2026 22:32:07 -0800 (PST) Received: from smtpclient.apple ([185.135.79.54]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-382a80bc19bsm4237780eec.29.2026.11.17.22.32.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Nov 2026 22:32:07 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.700.51.1.1\)) Subject: Re: [PATCH] Reject duplicate SQL/JSON PASSING argument names From: Chao Li X-Priority: 3 In-Reply-To: <202611181055551263943@163.com> Date: Wed, 18 Nov 2026 14:31:33 +0800 Cc: Pgsql Hackers Content-Transfer-Encoding: quoted-printable Message-Id: <1C45CA29-5CEA-463B-A895-3969DDF0B78A@gmail.com> References: <202611181055551263943@163.com> To: Dewei Dai X-Mailer: Apple Mail (2.3864.700.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Nov 18, 2026, at 10:55, Dewei Dai wrote: >=20 > Hi, >=20 > The attached patch makes SQL/JSON PASSING clauses reject duplicate > variable names instead of silently using the first matching value. >=20 > For example, this previously returned only "comedy": >=20 > SELECT jt.kind > FROM JSON_TABLE( > '{ "favorites":[{ "kind" : "comedy" },{ "kind" : "drama" }] }', > '$.favorites[*]?(@.kind =3D=3D $k1)' > PASSING 'comedy' AS k1, 'drama' AS k1 > COLUMNS (kind text PATH '$.kind') > ) AS jt; >=20 > With the patch it raises: >=20 > ERROR: PASSING argument name "k1" is not unique >=20 > 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. >=20 > 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?(@ =3D=3D $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 ``` --- 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 =3D castNode(JsonArgument, = lfirst(lc)); + String *name =3D makeString(arg->name); Node *expr =3D transformJsonValueExpr(pstate, = constructName, = arg->val, format, = InvalidOid, true); =20 + 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 PASSING clause provides data values = for the variables mentioned in the = path_expression. The result of the input data evaluation using the aforementioned = elements is called the row pattern, 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/