Skip to content

Commit dd5aa20

Browse files
tenderwgyjhjstz
authored andcommitted
Fix "unexpected gang size" issue.
In postgres planner, after top_plan having been produced, we have to adjust the Motion type of subplans, which call cdbllize_decorate_subplans_with_motions(). If subquery has another subquery, for example, in this test case, the second subquery's motion type is set to Gather but should be Broadcast. The root cause is that we forget to change local subFlow when its subplan has changed.
1 parent 319b1e0 commit dd5aa20

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

src/backend/cdb/cdbllize.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ fix_subplan_motion(PlannerInfo *root, Plan *subplan, Flow *outer_query_flow)
10251025
{
10261026
initPlans = list_concat(initPlans, subplan->initPlan);
10271027
subplan = subplan->lefttree;
1028+
subFlow = subplan->flow;
10281029
}
10291030

10301031
/*
@@ -1041,6 +1042,7 @@ fix_subplan_motion(PlannerInfo *root, Plan *subplan, Flow *outer_query_flow)
10411042

10421043
sendSlice = strippedMotion->senderSliceInfo;
10431044
subplan = subplan->lefttree;
1045+
subFlow = subplan->flow;
10441046
initPlans = list_concat(initPlans, strippedMotion->plan.initPlan);
10451047
}
10461048
else

src/test/regress/expected/subselect_gp.out

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,51 @@ select * from mrs_u1 join mrs_u2 on mrs_u1.a=mrs_u2.a where mrs_u1.a in (1,11) o
298298
drop table if exists mrs_u1;
299299
drop table if exists mrs_u2;
300300
--
301+
-- Set right motion type to subquery
302+
--
303+
drop table if exists gs_tab;
304+
NOTICE: table "gs_tab" does not exist, skipping
305+
create table gs_tab(a int, b int, c int) distributed by (a);
306+
insert into gs_tab values (1,1,1),(2,2,2);
307+
explain(costs off)
308+
select a from gs_tab t1 where b in
309+
(select b from gs_tab t2 where c in
310+
(select c from gs_tab t3)
311+
or (c >= 2))
312+
or (b <= 3)
313+
order by a;
314+
QUERY PLAN
315+
-------------------------------------------------------------------------------
316+
Gather Motion 3:1 (slice1; segments: 3)
317+
Merge Key: t1.a
318+
-> Sort
319+
Sort Key: t1.a
320+
-> Seq Scan on gs_tab t1
321+
Filter: ((hashed SubPlan 2) OR (b <= 3))
322+
SubPlan 2
323+
-> Broadcast Motion 3:3 (slice2; segments: 3)
324+
-> Seq Scan on gs_tab t2
325+
Filter: ((hashed SubPlan 1) OR (c >= 2))
326+
SubPlan 1
327+
-> Broadcast Motion 3:3 (slice3; segments: 3)
328+
-> Seq Scan on gs_tab t3
329+
Optimizer: Postgres query optimizer
330+
(14 rows)
331+
332+
select a from gs_tab t1 where b in
333+
(select b from gs_tab t2 where c in
334+
(select c from gs_tab t3)
335+
or (c >= 2))
336+
or (b <= 3)
337+
order by a;
338+
a
339+
---
340+
1
341+
2
342+
(2 rows)
343+
344+
drop table if exists gs_tab;
345+
--
301346
-- MPP-13758
302347
--
303348
drop table if exists csq_m1;

src/test/regress/expected/subselect_gp_optimizer.out

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,67 @@ select * from mrs_u1 join mrs_u2 on mrs_u1.a=mrs_u2.a where mrs_u1.a in (1,11) o
275275
drop table if exists mrs_u1;
276276
drop table if exists mrs_u2;
277277
--
278+
-- Set right motion type to subquery
279+
--
280+
drop table if exists gs_tab;
281+
NOTICE: table "gs_tab" does not exist, skipping
282+
create table gs_tab(a int, b int, c int) distributed by (a);
283+
insert into gs_tab values (1,1,1),(2,2,2);
284+
explain(costs off)
285+
select a from gs_tab t1 where b in
286+
(select b from gs_tab t2 where c in
287+
(select c from gs_tab t3)
288+
or (c >= 2))
289+
or (b <= 3)
290+
order by a;
291+
QUERY PLAN
292+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
293+
Gather Motion 3:1 (slice1; segments: 3)
294+
Merge Key: gs_tab_2.a
295+
-> Sort
296+
Sort Key: gs_tab_2.a
297+
-> Result
298+
Filter: (CASE WHEN ((count((true))) > '0'::bigint) THEN CASE WHEN ((sum((CASE WHEN ((gs_tab_2.b = gs_tab_1.b) IS NULL) THEN 1 ELSE 0 END))) = (count((true)))) THEN NULL::boolean ELSE true END ELSE false END OR (gs_tab_2.b <= 3))
299+
-> GroupAggregate
300+
Group Key: gs_tab_2.a, gs_tab_2.b, gs_tab_2.ctid, gs_tab_2.gp_segment_id
301+
-> Sort
302+
Sort Key: gs_tab_2.a, gs_tab_2.b, gs_tab_2.ctid, gs_tab_2.gp_segment_id
303+
-> Nested Loop Left Join
304+
Join Filter: ((gs_tab_2.b = gs_tab_1.b) IS NOT FALSE)
305+
-> Seq Scan on gs_tab gs_tab_2
306+
-> Materialize
307+
-> Result
308+
-> Broadcast Motion 3:3 (slice2; segments: 3)
309+
-> Result
310+
Filter: (CASE WHEN ((count((true))) > '0'::bigint) THEN CASE WHEN ((sum((CASE WHEN ((gs_tab_1.c = gs_tab.c) IS NULL) THEN 1 ELSE 0 END))) = (count((true)))) THEN NULL::boolean ELSE true END ELSE false END OR (gs_tab_1.c >= 2))
311+
-> GroupAggregate
312+
Group Key: gs_tab_1.a, gs_tab_1.b, gs_tab_1.c, gs_tab_1.ctid, gs_tab_1.gp_segment_id
313+
-> Sort
314+
Sort Key: gs_tab_1.a, gs_tab_1.b, gs_tab_1.c, gs_tab_1.ctid, gs_tab_1.gp_segment_id
315+
-> Nested Loop Left Join
316+
Join Filter: ((gs_tab_1.c = gs_tab.c) IS NOT FALSE)
317+
-> Seq Scan on gs_tab gs_tab_1
318+
-> Materialize
319+
-> Result
320+
-> Broadcast Motion 3:3 (slice3; segments: 3)
321+
-> Seq Scan on gs_tab
322+
Optimizer: Pivotal Optimizer (GPORCA)
323+
(30 rows)
324+
325+
select a from gs_tab t1 where b in
326+
(select b from gs_tab t2 where c in
327+
(select c from gs_tab t3)
328+
or (c >= 2))
329+
or (b <= 3)
330+
order by a;
331+
a
332+
---
333+
1
334+
2
335+
(2 rows)
336+
337+
drop table if exists gs_tab;
338+
--
278339
-- MPP-13758
279340
--
280341
drop table if exists csq_m1;

src/test/regress/sql/subselect_gp.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ select * from mrs_u1 join mrs_u2 on mrs_u1.a=mrs_u2.a where mrs_u1.a in (1,11) o
135135
drop table if exists mrs_u1;
136136
drop table if exists mrs_u2;
137137

138+
--
139+
-- Set right motion type to subquery
140+
--
141+
142+
drop table if exists gs_tab;
143+
144+
create table gs_tab(a int, b int, c int) distributed by (a);
145+
insert into gs_tab values (1,1,1),(2,2,2);
146+
explain(costs off)
147+
select a from gs_tab t1 where b in
148+
(select b from gs_tab t2 where c in
149+
(select c from gs_tab t3)
150+
or (c >= 2))
151+
or (b <= 3)
152+
order by a;
153+
154+
select a from gs_tab t1 where b in
155+
(select b from gs_tab t2 where c in
156+
(select c from gs_tab t3)
157+
or (c >= 2))
158+
or (b <= 3)
159+
order by a;
160+
161+
drop table if exists gs_tab;
162+
138163
--
139164
-- MPP-13758
140165
--

0 commit comments

Comments
 (0)