diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/optimizer/prep/prepunion.c | 12 | ||||
-rw-r--r-- | src/test/regress/expected/union.out | 25 | ||||
-rw-r--r-- | src/test/regress/sql/union.sql | 6 |
3 files changed, 42 insertions, 1 deletions
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index b7147832e0f..7d0279ec3ef 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -338,6 +338,16 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, * Estimate number of groups if caller wants it. If the subquery used * grouping or aggregation, its output is probably mostly unique * anyway; otherwise do statistical estimation. + * + * XXX you don't really want to know about this: we do the estimation + * using the subquery's original targetlist expressions, not the + * subroot->processed_tlist which might seem more appropriate. The + * reason is that if the subquery is itself a setop, it may return a + * processed_tlist containing "varno 0" Vars generated by + * generate_append_tlist, and those would confuse estimate_num_groups + * mightily. We ought to get rid of the "varno 0" hack, but that + * requires a redesign of the parsetree representation of setops, so + * that there can be an RTE corresponding to each setop's output. */ if (pNumGroups) { @@ -347,7 +357,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, *pNumGroups = subpath->rows; else *pNumGroups = estimate_num_groups(subroot, - get_tlist_exprs(subroot->processed_tlist, false), + get_tlist_exprs(subquery->targetList, false), subpath->rows, NULL); } diff --git a/src/test/regress/expected/union.out b/src/test/regress/expected/union.out index 016571bd4a9..042ee8a060f 100644 --- a/src/test/regress/expected/union.out +++ b/src/test/regress/expected/union.out @@ -319,6 +319,31 @@ SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl; SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE; ERROR: FOR NO KEY UPDATE is not allowed with UNION/INTERSECT/EXCEPT +-- nested cases +(SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6; + ?column? | ?column? | ?column? +----------+----------+---------- + 4 | 5 | 6 +(1 row) + +(SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6; + ?column? | ?column? | ?column? +----------+----------+---------- + 4 | 5 | 6 +(1 row) + +(SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6; + ?column? | ?column? | ?column? +----------+----------+---------- + 1 | 2 | 3 +(1 row) + +(SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6; + ?column? | ?column? | ?column? +----------+----------+---------- + 1 | 2 | 3 +(1 row) + -- -- Mixed types -- diff --git a/src/test/regress/sql/union.sql b/src/test/regress/sql/union.sql index 9ff1551e5de..7baacba6d67 100644 --- a/src/test/regress/sql/union.sql +++ b/src/test/regress/sql/union.sql @@ -111,6 +111,12 @@ SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl; SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE; +-- nested cases +(SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6; +(SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6; +(SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6; +(SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6; + -- -- Mixed types -- |