Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4886dc9

Browse files
committed
Fix set_subquery_pathlist() to copy the RTE's subquery before it gets mangled
by the planning process. This prevents the "failed to locate grouping columns" error recently reported by Dickson Guedes. That happens because planning replaces SubLinks by SubPlans in the subquery's targetlist, and exprTypmod() is smarter about the former than the latter, causing the apparent type of the subquery's output columns to change. This seems to be a deficiency we should fix in exprTypmod(), but that will be a much more invasive patch with possible side-effects elsewhere, so I'll do that only in HEAD. Back-patch to 8.3. Arguably the lack of a copying step is broken/dangerous all the way back, but in the absence of known problems I'll refrain from making the older branches pay the extra cost. (The reason this particular symptom didn't appear before is that exprTypmod() wasn't smart about SubLinks either, until 8.3.)
1 parent 4283172 commit 4886dc9

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/backend/optimizer/path/allpaths.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.180 2009/02/15 20:16:21 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.181 2009/03/10 20:58:26 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -523,6 +523,13 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
523523
PlannerInfo *subroot;
524524
List *pathkeys;
525525

526+
/*
527+
* Must copy the Query so that planning doesn't mess up the RTE contents
528+
* (really really need to fix the planner to not scribble on its input,
529+
* someday).
530+
*/
531+
subquery = copyObject(subquery);
532+
526533
/* We need a workspace for keeping track of set-op type coercions */
527534
differentTypes = (bool *)
528535
palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));

src/test/regress/expected/subselect.out

+12
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,15 @@ from tc;
465465
3
466466
(2 rows)
467467

468+
--
469+
-- Test case for 8.3 "failed to locate grouping columns" bug
470+
--
471+
create temp table t1 (f1 numeric(14,0), f2 varchar(30));
472+
select * from
473+
(select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
474+
from t1 up) ss
475+
group by f1,f2,fs;
476+
f1 | f2 | fs
477+
----+----+----
478+
(0 rows)
479+

src/test/regress/sql/subselect.sql

+11
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,14 @@ select
298298
( select min(tb.id) from tb
299299
where tb.aval = (select ta.val from ta where ta.id = tc.aid) ) as min_tb_id
300300
from tc;
301+
302+
--
303+
-- Test case for 8.3 "failed to locate grouping columns" bug
304+
--
305+
306+
create temp table t1 (f1 numeric(14,0), f2 varchar(30));
307+
308+
select * from
309+
(select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
310+
from t1 up) ss
311+
group by f1,f2,fs;

0 commit comments

Comments
 (0)