MCPcopy Create free account
hub / github.com/apache/cloudberry / create_motion_path_for_insert

Function create_motion_path_for_insert

src/backend/cdb/cdbpath.c:2509–2647  ·  view source on GitHub ↗

* Add a suitable Motion Path so that the input tuples from 'subpath' are * distributed correctly for insertion into target table. */

Source from the content-addressed store, hash-verified

2507 * distributed correctly for insertion into target table.
2508 */
2509Path *
2510create_motion_path_for_insert(PlannerInfo *root, GpPolicy *policy,
2511 Path *subpath)
2512{
2513 GpPolicyType policyType = policy->ptype;
2514 CdbPathLocus targetLocus;
2515
2516 if (policyType == POLICYTYPE_PARTITIONED)
2517 {
2518 /*
2519 * A query to reach here: INSERT INTO t1 VALUES(1).
2520 * There is no need to add a motion from General, we could
2521 * simply put General on the same segments with target table.
2522 */
2523 /* FIXME: also do this for other targetPolicyType? */
2524 /* FIXME: also do this for all the subplans */
2525 if (CdbPathLocus_IsGeneral(subpath->locus))
2526 {
2527 subpath->locus.numsegments = policy->numsegments;
2528 }
2529
2530 targetLocus = cdbpathlocus_for_insert(root, policy, subpath->pathtarget);
2531
2532 if (policy->nattrs == 0 && CdbPathLocus_IsPartitioned(subpath->locus))
2533 {
2534 /*
2535 * If the target table is DISTRIBUTED RANDOMLY, we can insert the
2536 * rows anywhere. So if the input path is already partitioned, let
2537 * the insertions happen where they are. Unless the GUC gp_force_random_redistribution
2538 * tells us to force the redistribution.
2539 *
2540 * If you `explain` the query insert into tab_random select * from tab_partition
2541 * there is not Motion node in plan. However, it is not means that the query only
2542 * execute in entry db. It is dispatched to QE and do everything well as we expect.
2543 *
2544 * But, we need to grant a Motion node if target locus' segnumber is different with
2545 * subpath.
2546 */
2547 if (gp_force_random_redistribution || targetLocus.numsegments != subpath->locus.numsegments)
2548 {
2549 CdbPathLocus_MakeStrewn(&targetLocus, policy->numsegments, 0);
2550 subpath = cdbpath_create_motion_path(root, subpath, NIL, false, targetLocus);
2551 }
2552 }
2553 else if (CdbPathLocus_IsNull(targetLocus))
2554 {
2555 /* could not create DistributionKeys to represent the distribution keys. */
2556 CdbPathLocus_MakeStrewn(&targetLocus, policy->numsegments, 0);
2557
2558 subpath = (Path *) make_motion_path(root, subpath, targetLocus, false, policy);
2559 }
2560 else
2561 {
2562 subpath = cdbpath_create_motion_path(root, subpath, NIL, false, targetLocus);
2563 }
2564 }
2565 else if (policyType == POLICYTYPE_ENTRY)
2566 {

Callers 3

cdbllize_adjust_top_pathFunction · 0.85

Calls 5

cdbpathlocus_for_insertFunction · 0.85
make_motion_pathFunction · 0.85

Tested by

no test coverage detected