pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #18852: Unexpected expression in subquery output
3+ messages / 3 participants
[nested] [flat]

* BUG #18852: Unexpected expression in subquery output
@ 2025-03-16 13:39 PG Bug reporting form <noreply@postgresql.org>
  2025-03-17 15:21 ` Re: BUG #18852: Unexpected expression in subquery output Tender Wang <tndrwang@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: PG Bug reporting form @ 2025-03-16 13:39 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: tharakan@gmail.com

The following bug has been logged on the website:

Bug reference:      18852
Logged by:          Robins Tharakan
Email address:      tharakan@gmail.com
PostgreSQL version: Unsupported/Unknown
Operating system:   Ubuntu
Description:        

Hi,

The following SQL returns this error:

$ psql -f repro.sql >/dev/null
psql:repro.sql:11: ERROR:  unexpected expression in subquery output

$ cat repro.sql
CREATE SCHEMA a;
CREATE SCHEMA b;
CREATE EXTENSION postgres_fdw ;
CREATE SERVER d FOREIGN DATA WRAPPER postgres_fdw;
CREATE FOREIGN TABLE a.e () SERVER d;
CREATE FOREIGN TABLE a.f (g text) SERVER d;
CREATE FOREIGN TABLE b.h () SERVER d;
SELECT FROM a.f RIGHT JOIN b.h ON EXISTS (SELECT FROM a.e WHERE (SELECT
NULL) > g) LIMIT 9;

- The issue surfaced after commit 824dbea3e41efa3b35094163c834988dea7eb139
[1]
Testing for start crashing
Checking (682c5be25c2~0) - 682c5be25c - fail
Checking (682c5be25c2~10) - 2b73a8cd33 - fail
Checking (682c5be25c2~30) - 3691edfab9 - fail
.
.
Checking (682c5be25c2~3470) - 278eb13c48 - pass
Checking (682c5be25c2~3450) - 7db01fbcef - fail
Checking (682c5be25c2~3460) - 719b342d36 - fail
Checking (682c5be25c2~3465) - c2a465b2c9 - fail
Checking (682c5be25c2~3467) - 7636725b92 - fail
Checking (682c5be25c2~3468) - bfc677c3bc - fail
Checking (682c5be25c2~3469) - 824dbea3e4 - fail
Surfacing Commit is 824dbea3e41efa3b35094163c834988dea7eb139

-
robins

Reference:
1.
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=824dbea3e41efa3b35094163c834988de...



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #18852: Unexpected expression in subquery output
  2025-03-16 13:39 BUG #18852: Unexpected expression in subquery output PG Bug reporting form <noreply@postgresql.org>
@ 2025-03-17 15:21 ` Tender Wang <tndrwang@gmail.com>
  2025-03-19 01:01   ` Re: BUG #18852: Unexpected expression in subquery output Robins Tharakan <tharakan@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tender Wang @ 2025-03-17 15:21 UTC (permalink / raw)
  To: tharakan@gmail.com; pgsql-bugs@lists.postgresql.org

PG Bug reporting form <noreply@postgresql.org> 于2025年3月17日周一 16:35写道:

> The following bug has been logged on the website:
>
> Bug reference:      18852
> Logged by:          Robins Tharakan
> Email address:      tharakan@gmail.com
> PostgreSQL version: Unsupported/Unknown
> Operating system:   Ubuntu
> Description:
>
> Hi,
>
> The following SQL returns this error:
>
> $ psql -f repro.sql >/dev/null
> psql:repro.sql:11: ERROR:  unexpected expression in subquery output
>
> $ cat repro.sql
> CREATE SCHEMA a;
> CREATE SCHEMA b;
> CREATE EXTENSION postgres_fdw ;
> CREATE SERVER d FOREIGN DATA WRAPPER postgres_fdw;
> CREATE FOREIGN TABLE a.e () SERVER d;
> CREATE FOREIGN TABLE a.f (g text) SERVER d;
> CREATE FOREIGN TABLE b.h () SERVER d;
> SELECT FROM a.f RIGHT JOIN b.h ON EXISTS (SELECT FROM a.e WHERE (SELECT
> NULL) > g) LIMIT 9;
>

I can reproduce this issue on HEAD.
SELECT FROM a.f RIGHT JOIN b.h ON EXISTS (SELECT FROM a.e WHERE (SELECT
NULL) > g) LIMIT 9;

The join order of the above query is b.h left join (a.f semi join a.e). The
NULL > g is pushdown to a.f rel.
When we process semijoin, the (NULL > g) is added into
fpinfo->remote_conds, then it is added into the finfo->joinclauses
when we process the left join.

So in deparseFromExprForRel(), we should append the fpinfo->joinclauses to
buf, then we will enter get_relation_column_alias_ids(node, innerrel,
relno, colon)
in is_subquery_var(). The innerrel is joinrel of a.f and a.e, the node is
var (g), but innerrel->reltarget->expr is NIL. So the error triggers.

The (null > g) should not be the leftjoin joinclauses, if I understand
correctly. And I see the comments in foreign_join_ok():
...
else if (jointype == JOIN_LEFT || jointype == JOIN_RIGHT || jointype ==
JOIN_FULL)
{
/*
* Conditions, generated from semi-joins, should be evaluated before
* LEFT/RIGHT/FULL join.
*/
...

I add below code in foreign_join_ok() if jointype is LEFT:
case JOIN_LEFT:
if (bms_is_empty(fpinfo_i->hidden_subquery_rels))
            fpinfo->joinclauses = list_concat(fpinfo->joinclauses,

fpinfo_i->remote_conds);

Then I get the plan like below:
postgres=# explain verbose SELECT FROM a.f RIGHT JOIN b.h ON EXISTS (SELECT
FROM a.e WHERE (SELECT
NULL) > g) limit 1;

   QUERY PLAN

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan  (cost=100.01..100.21 rows=1 width=0)
   Relations: (b.h) LEFT JOIN ((a.f) SEMI JOIN (a.e))
   Remote SQL: SELECT NULL FROM (b.h r2 LEFT JOIN (SELECT NULL FROM a.f r1
WHERE (($1::text > r1.g)) AND EXISTS (SELECT NULL FROM a.e r4)) s5 ON
(TRUE)) LIMIT 1::bigint
   InitPlan 1
     ->  Result  (cost=0.00..0.01 rows=1 width=32)
           Output: NULL::text
(6 rows)

I'm not very familiar with fdw codes. Any thoughts?
-- 
Thanks,
Tender Wang

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #18852: Unexpected expression in subquery output
  2025-03-16 13:39 BUG #18852: Unexpected expression in subquery output PG Bug reporting form <noreply@postgresql.org>
  2025-03-17 15:21 ` Re: BUG #18852: Unexpected expression in subquery output Tender Wang <tndrwang@gmail.com>
@ 2025-03-19 01:01   ` Robins Tharakan <tharakan@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Robins Tharakan @ 2025-03-19 01:01 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: Tender Wang <tndrwang@gmail.com>

On Tue, 18 Mar 2025 at 01:51, Tender Wang <tndrwang@gmail.com> wrote:

>
> PG Bug reporting form <noreply@postgresql.org> 于2025年3月17日周一 16:35写道:
>
>> The following bug has been logged on the website:
>>
>> Bug reference:      18852
>>
>>
> I can reproduce this issue on HEAD.
> SELECT FROM a.f RIGHT JOIN b.h ON EXISTS (SELECT FROM a.e WHERE (SELECT
> NULL) > g) LIMIT 9;
>


FWIW, there's additional discussion happening in the original thread -

https://www.postgresql.org/message-id/flat/CAPpHfdsf_9OBXS1KzPQi%2BTAnNWPZGAzZ67xKn4-5uWhbg2dj-Q%40m...


-
robins
https://robins.in

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2025-03-19 01:01 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-16 13:39 BUG #18852: Unexpected expression in subquery output PG Bug reporting form <noreply@postgresql.org>
2025-03-17 15:21 ` Tender Wang <tndrwang@gmail.com>
2025-03-19 01:01   ` Robins Tharakan <tharakan@gmail.com>

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