Skip to content

Commit eda80f0

Browse files
committed
Repair interaction between IN-join processing and subselect pullup that
I inadvertently broke a few days ago (per report from Sean Thomas). Add regression test case to try to catch any similar breakage in future.
1 parent c02036b commit eda80f0

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

src/backend/optimizer/plan/subselect.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.82 2003/08/08 21:41:51 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.83 2003/10/18 16:52:15 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -493,8 +493,8 @@ make_subplan(SubLink *slink, List *lefthand, bool isTopQual)
493493
* If rtindex is 0, we build Params to represent the sub-select outputs.
494494
* The paramids of the Params created are returned in the *righthandIds list.
495495
*
496-
* If rtindex is not 0, we build Vars using that rtindex as varno. The
497-
* Vars themselves are returned in *righthandIds (this is a bit of a type
496+
* If rtindex is not 0, we build Vars using that rtindex as varno. Copies
497+
* of the Var nodes are returned in *righthandIds (this is a bit of a type
498498
* cheat, but we can get away with it).
499499
*/
500500
static List *
@@ -525,8 +525,11 @@ convert_sublink_opers(List *lefthand, List *operOids,
525525
te->resdom->restype,
526526
te->resdom->restypmod,
527527
0);
528-
/* Record it for caller */
529-
*righthandIds = lappend(*righthandIds, rightop);
528+
/*
529+
* Copy it for caller. NB: we need a copy to avoid having
530+
* doubly-linked substructure in the modified parse tree.
531+
*/
532+
*righthandIds = lappend(*righthandIds, copyObject(rightop));
530533
}
531534
else
532535
{
@@ -735,7 +738,7 @@ convert_IN_to_join(Query *parse, SubLink *sublink)
735738

736739
/*
737740
* Build the result qual expressions. As a side effect,
738-
* ininfo->sub_targetlist is filled with a list of the Vars
741+
* ininfo->sub_targetlist is filled with a list of Vars
739742
* representing the subselect outputs.
740743
*/
741744
exprs = convert_sublink_opers(sublink->lefthand,

src/test/regress/expected/subselect.out

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,39 @@ from int8_tbl group by q1 order by q1;
165165
4567890123456789 | 0.6
166166
(2 rows)
167167

168+
--
169+
-- Test cases to catch unpleasant interactions between IN-join processing
170+
-- and subquery pullup.
171+
--
172+
select count(*) from
173+
(select 1 from tenk1 a
174+
where unique1 IN (select hundred from tenk1 b)) ss;
175+
count
176+
-------
177+
100
178+
(1 row)
179+
180+
select count(distinct ss.ten) from
181+
(select ten from tenk1 a
182+
where unique1 IN (select hundred from tenk1 b)) ss;
183+
count
184+
-------
185+
10
186+
(1 row)
187+
188+
select count(*) from
189+
(select 1 from tenk1 a
190+
where unique1 IN (select distinct hundred from tenk1 b)) ss;
191+
count
192+
-------
193+
100
194+
(1 row)
195+
196+
select count(distinct ss.ten) from
197+
(select ten from tenk1 a
198+
where unique1 IN (select distinct hundred from tenk1 b)) ss;
199+
count
200+
-------
201+
10
202+
(1 row)
203+

src/test/regress/sql/subselect.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,21 @@ SELECT '' AS eight, ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
7575

7676
select q1, float8(count(*)) / (select count(*) from int8_tbl)
7777
from int8_tbl group by q1 order by q1;
78+
79+
--
80+
-- Test cases to catch unpleasant interactions between IN-join processing
81+
-- and subquery pullup.
82+
--
83+
84+
select count(*) from
85+
(select 1 from tenk1 a
86+
where unique1 IN (select hundred from tenk1 b)) ss;
87+
select count(distinct ss.ten) from
88+
(select ten from tenk1 a
89+
where unique1 IN (select hundred from tenk1 b)) ss;
90+
select count(*) from
91+
(select 1 from tenk1 a
92+
where unique1 IN (select distinct hundred from tenk1 b)) ss;
93+
select count(distinct ss.ten) from
94+
(select ten from tenk1 a
95+
where unique1 IN (select distinct hundred from tenk1 b)) ss;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy