Skip to content

Commit 6b96824

Browse files
committed
Fix CTAS crash.
If we create a replicated table when selecting project volatile functions from a randomly distributed table, we will try to add a Motion on a Motion node that will cause a crash. Create table t1(id int) distributed randomly; Create table t2 as select random() from t1 distributed replicated; planner may have add a top Motion earlier if we know the query's final locus. Some codes handle volatile functions lead us to create_motion_path_for_insert(), and we should consider the case to avoid Assertion failure when create_plan(). Authored-by: Zhang Mingli avamingli@gmail.com
1 parent a402666 commit 6b96824

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/backend/cdb/cdbpath.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,15 @@ create_motion_path_for_insert(PlannerInfo *root, GpPolicy *policy,
26222622
}
26232623

26242624
}
2625-
subpath = cdbpath_create_broadcast_motion_path(root, subpath, policy->numsegments);
2625+
2626+
/*
2627+
* planner may have add a top Motion eariler.
2628+
* Create table t1(id int) distributed randomly;
2629+
* Create table t2 as select random() from t1 distributed replicated;
2630+
* Avoid Motion if there was already one.
2631+
*/
2632+
if (!CdbPathLocus_IsReplicated(subpath->locus))
2633+
subpath = cdbpath_create_broadcast_motion_path(root, subpath, policy->numsegments);
26262634
}
26272635
else
26282636
elog(ERROR, "unrecognized policy type %u", policyType);

src/test/regress/expected/bfv_planner.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,8 @@ select count(distinct nextval) from gp_dist_random('t_rep4');
663663
(1 row)
664664

665665
drop table rep_tbl, t_rep4;
666+
create table t_issue_1218_random(id int) distributed randomly;
667+
create table t_issue_1218_replicated as select random() from t_issue_1218_random distributed replicated;
666668
--
667669
-- Test append different numsegments tables work well
668670
-- See Github issue: https://github.com/greenplum-db/gpdb/issues/12146

src/test/regress/expected/bfv_planner_optimizer.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,8 @@ select count(distinct nextval) from gp_dist_random('t_rep4');
681681
(1 row)
682682

683683
drop table rep_tbl, t_rep4;
684+
create table t_issue_1218_random(id int) distributed randomly;
685+
create table t_issue_1218_replicated as select random() from t_issue_1218_random distributed replicated;
684686
--
685687
-- Test append different numsegments tables work well
686688
-- See Github issue: https://github.com/greenplum-db/gpdb/issues/12146

src/test/regress/sql/bfv_planner.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ select count(*) from gp_dist_random('t_rep4');
345345
select count(distinct nextval) from gp_dist_random('t_rep4');
346346
drop table rep_tbl, t_rep4;
347347

348+
create table t_issue_1218_random(id int) distributed randomly;
349+
create table t_issue_1218_replicated as select random() from t_issue_1218_random distributed replicated;
350+
348351
--
349352
-- Test append different numsegments tables work well
350353
-- See Github issue: https://github.com/greenplum-db/gpdb/issues/12146

0 commit comments

Comments
 (0)