Skip to content

Commit 189bd09

Browse files
committed
Don't assume a subquery's output is unique if there's a SRF in its tlist.
While the x output of "select x from t group by x" can be presumed unique, this does not hold for "select x, generate_series(1,10) from t group by x", because we may expand the set-returning function after the grouping step. (Perhaps that should be re-thought; but considering all the other oddities involved with SRFs in targetlists, it seems unlikely we'll change it.) Put a check in query_is_distinct_for() so it's not fooled by such cases. Back-patch to all supported branches. David Rowley
1 parent 759c9fb commit 189bd09

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/backend/optimizer/util/pathnode.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,17 @@ query_is_distinct_for(Query *query, List *colnos, List *opids)
15081508

15091509
Assert(list_length(colnos) == list_length(opids));
15101510

1511+
/*
1512+
* A set-returning function in the query's targetlist can result in
1513+
* returning duplicate rows, if the SRF is evaluated after the
1514+
* de-duplication step; so we play it safe and say "no" if there are any
1515+
* SRFs. (We could be certain that it's okay if SRFs appear only in the
1516+
* specified columns, since those must be evaluated before de-duplication;
1517+
* but it doesn't presently seem worth the complication to check that.)
1518+
*/
1519+
if (expression_returns_set((Node *) query->targetList))
1520+
return false;
1521+
15111522
/*
15121523
* DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
15131524
* columns in the DISTINCT clause appear in colnos and operator semantics

src/test/regress/expected/subselect.out

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,36 @@ select * from int4_tbl where
742742
0
743743
(1 row)
744744

745+
--
746+
-- Check for incorrect optimization when IN subquery contains a SRF
747+
--
748+
explain (verbose, costs off)
749+
select * from int4_tbl o where (f1, f1) in
750+
(select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1);
751+
QUERY PLAN
752+
----------------------------------------------------------------------
753+
Hash Join
754+
Output: o.f1
755+
Hash Cond: (o.f1 = "ANY_subquery".f1)
756+
-> Seq Scan on public.int4_tbl o
757+
Output: o.f1
758+
-> Hash
759+
Output: "ANY_subquery".f1, "ANY_subquery".g
760+
-> HashAggregate
761+
Output: "ANY_subquery".f1, "ANY_subquery".g
762+
-> Subquery Scan on "ANY_subquery"
763+
Output: "ANY_subquery".f1, "ANY_subquery".g
764+
Filter: ("ANY_subquery".f1 = "ANY_subquery".g)
765+
-> HashAggregate
766+
Output: i.f1, (generate_series(1, 2) / 10)
767+
-> Seq Scan on public.int4_tbl i
768+
Output: i.f1
769+
(16 rows)
770+
771+
select * from int4_tbl o where (f1, f1) in
772+
(select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1);
773+
f1
774+
----
775+
0
776+
(1 row)
777+

src/test/regress/sql/subselect.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,12 @@ select * from int4_tbl where
422422
select * from int4_tbl where
423423
(case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
424424
(select ten from tenk1 b);
425+
426+
--
427+
-- Check for incorrect optimization when IN subquery contains a SRF
428+
--
429+
explain (verbose, costs off)
430+
select * from int4_tbl o where (f1, f1) in
431+
(select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1);
432+
select * from int4_tbl o where (f1, f1) in
433+
(select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1);

0 commit comments

Comments
 (0)