Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e2f2d7

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 odditiesinvolved 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 parent53166fe commit9e2f2d7

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

‎src/backend/optimizer/util/pathnode.c

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

14741474
Assert(list_length(colnos)==list_length(opids));
14751475

1476+
/*
1477+
* A set-returning function in the query's targetlist can result in
1478+
* returning duplicate rows, if the SRF is evaluated after the
1479+
* de-duplication step; so we play it safe and say "no" if there are any
1480+
* SRFs. (We could be certain that it's okay if SRFs appear only in the
1481+
* specified columns, since those must be evaluated before de-duplication;
1482+
* but it doesn't presently seem worth the complication to check that.)
1483+
*/
1484+
if (expression_returns_set((Node*)query->targetList))
1485+
return false;
1486+
14761487
/*
14771488
* DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
14781489
* columns in the DISTINCT clause appear in colnos and operator semantics

‎src/test/regress/expected/subselect.out

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,41 @@ select * from int4_tbl where
739739
0
740740
(1 row)
741741

742+
--
743+
-- Check for incorrect optimization when IN subquery contains a SRF
744+
--
745+
explain (verbose, costs off)
746+
select * from int4_tbl o where (f1, f1) in
747+
(select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1);
748+
QUERY PLAN
749+
----------------------------------------------------------------------
750+
Hash Join
751+
Output: o.f1
752+
Hash Cond: (o.f1 = "ANY_subquery".f1)
753+
-> Seq Scan on public.int4_tbl o
754+
Output: o.f1
755+
-> Hash
756+
Output: "ANY_subquery".f1, "ANY_subquery".g
757+
-> HashAggregate
758+
Output: "ANY_subquery".f1, "ANY_subquery".g
759+
Group Key: "ANY_subquery".f1, "ANY_subquery".g
760+
-> Subquery Scan on "ANY_subquery"
761+
Output: "ANY_subquery".f1, "ANY_subquery".g
762+
Filter: ("ANY_subquery".f1 = "ANY_subquery".g)
763+
-> HashAggregate
764+
Output: i.f1, (generate_series(1, 2) / 10)
765+
Group Key: i.f1
766+
-> Seq Scan on public.int4_tbl i
767+
Output: i.f1
768+
(18 rows)
769+
770+
select * from int4_tbl o where (f1, f1) in
771+
(select f1, generate_series(1,2) / 10 g from int4_tbl i group by f1);
772+
f1
773+
----
774+
0
775+
(1 row)
776+
742777
--
743778
-- Check that volatile quals aren't pushed down past a DISTINCT:
744779
-- nextval() should not be called more than the nominal number of times

‎src/test/regress/sql/subselect.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ select * from int4_tbl where
423423
(case when f1in (select unique1from tenk1 a) then f1 elsenull end)in
424424
(select tenfrom tenk1 b);
425425

426+
--
427+
-- Check for incorrect optimization when IN subquery contains a SRF
428+
--
429+
explain (verbose, costs off)
430+
select*from int4_tbl owhere (f1, f1)in
431+
(select f1, generate_series(1,2)/10 gfrom int4_tbl igroup by f1);
432+
select*from int4_tbl owhere (f1, f1)in
433+
(select f1, generate_series(1,2)/10 gfrom int4_tbl igroup by f1);
434+
426435
--
427436
-- Check that volatile quals aren't pushed down past a DISTINCT:
428437
-- nextval() should not be called more than the nominal number of times

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp