Re: BUG #19046: Incorrect result when using json_array() with column reference in subquery combined with RIGHT JOIN

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: runyuan(at)berkeley(dot)edu, pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: BUG #19046: Incorrect result when using json_array() with column reference in subquery combined with RIGHT JOIN
Date: 2025-09-10 12:30:59
Message-ID: CAHewXNnLLO1VuP3OJGRy2sc34xA0nWYSthCM1eeMD=bhkvMfYg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Tender Wang <tndrwang(at)gmail(dot)com> 于2025年9月10日周三 19:36写道:

>
>
> PG Bug reporting form <noreply(at)postgresql(dot)org> 于2025年9月10日周三 18:22写道:
>
>> The following bug has been logged on the website:
>>
>> Bug reference: 19046
>> Logged by: Runyuan He
>> Email address: runyuan(at)berkeley(dot)edu
>> PostgreSQL version: 18rc1
>> Operating system: Linux (x86)
>> Description:
>>
>> Bug Description:
>> When using json_array() function with a column reference from table t
>> inside
>> a subquery, and then performing a RIGHT JOIN with condition FALSE, the
>> result incorrectly returns [3, 2] instead of the expected NULL value.
>>
>> Reproducible Example:
>> CREATE TABLE t(c INT);
>> INSERT INTO t VALUES (1);
>>
>> SELECT sub.c FROM
>> (SELECT json_array(3, 2, t.c) AS c FROM t) AS sub
>> RIGHT JOIN t ON FALSE;
>> -- PostgreSQL 16.x: Returns NULL (CORRECT)
>> -- PostgreSQL 17.6, 17.rc1: Returns [3, 2] (INCORRECT)
>> -- PostgreSQL 18rc1: Returns [3, 2] (INCORRECT)
>>
>> SELECT sub.c FROM
>> (SELECT json_array(3, 2, t.c) AS c FROM t) AS sub;
>> -- Returns [3, 2, 1] (CORRECT)
>>
>> SELECT sub.c FROM
>> (SELECT json_array(3, 2, 1) AS c FROM t) AS sub
>> RIGHT JOIN t ON FALSE;
>> -- Returns Null (CORRECT)
>>
>>
> I can reproduce this on HEAD. The following commit introduced this
> incorrect result:
>
> commit cb8e50a4a09fe541e32cd54ea90a97f2924121a1
> Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
> Date: Fri Aug 30 12:42:12 2024 -0400
>
> Avoid inserting PlaceHolderVars in cases where pre-v16 PG did not.
>
>
The calling contain_nonstrict_functions() returned false when
checking JsonConstructorExpr.
postgres=# SELECT json_array(3, 2, NULL);
json_array
------------
[3, 2]
(1 row)

The JsonConstructorExpr seems non-strict function.

diff --git a/src/backend/optimizer/util/clauses.c
b/src/backend/optimizer/util/clauses.c
index 6f0b338d2cd..5ef364b7f7c 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1115,6 +1115,8 @@ contain_nonstrict_functions_walker(Node *node, void
*context)
return true;
if (IsA(node, BooleanTest))
return true;
+ if (IsA(node, JsonConstructorExpr))
+ return true;

I added the above codes, then the query returned the correct result.
I didn't dig more the details. Any thought?
--
Thanks,
Tender Wang

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Richard Guo 2025-09-10 13:29:41 Re: BUG #19046: Incorrect result when using json_array() with column reference in subquery combined with RIGHT JOIN
Previous Message Tender Wang 2025-09-10 11:36:52 Re: BUG #19046: Incorrect result when using json_array() with column reference in subquery combined with RIGHT JOIN