forked from apache/cloudberry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdbpath.c
More file actions
3891 lines (3513 loc) · 124 KB
/
cdbpath.c
File metadata and controls
3891 lines (3513 loc) · 124 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* cdbpath.c
*
* Portions Copyright (c) 2005-2008, Greenplum inc
* Portions Copyright (c) 2012-Present VMware, Inc. or its affiliates.
* Portions Copyright (c) 2023, HashData Technology Limited.
*
* IDENTIFICATION
* src/backend/cdb/cdbpath.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/hash.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_trigger.h"
#include "commands/trigger.h"
#include "nodes/makefuncs.h" /* makeFuncExpr() */
#include "nodes/nodeFuncs.h" /* exprType() */
#include "nodes/pathnodes.h" /* PlannerInfo, RelOptInfo */
#include "optimizer/cost.h" /* set_rel_width() */
#include "optimizer/optimizer.h" /* cpu_tuple_cost */
#include "optimizer/pathnode.h" /* Path, pathnode_walker() */
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/tlist.h"
#include "parser/parsetree.h"
#include "parser/parse_expr.h" /* exprType() */
#include "parser/parse_oper.h"
#include "utils/catcache.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "cdb/cdbdef.h" /* CdbSwap() */
#include "cdb/cdbhash.h"
#include "cdb/cdbpath.h" /* me */
#include "cdb/cdbutil.h"
#include "cdb/cdbvars.h"
typedef struct
{
CdbPathLocus locus;
CdbPathLocus move_to;
double bytes;
Path *path;
List *pathkeys;
bool ok_to_replicate;
bool require_existing_order;
bool has_wts; /* Does the rel have WorkTableScan? */
bool isouter; /* Is at outer table side? */
} CdbpathMfjRel;
static bool try_redistribute(PlannerInfo *root, CdbpathMfjRel *g,
CdbpathMfjRel *o, List *redistribution_clauses, bool parallel_aware);
static SplitUpdatePath *make_splitupdate_path(PlannerInfo *root, Path *subpath, Index rti);
static bool can_elide_explicit_motion(PlannerInfo *root, Index rti, Path *subpath, GpPolicy *policy);
/*
* cdbpath_cost_motion
* Fills in the cost estimate fields in a MotionPath node.
*/
void
cdbpath_cost_motion(PlannerInfo *root, CdbMotionPath *motionpath)
{
Path *subpath = motionpath->subpath;
Cost cost_per_row;
Cost motioncost;
double recvrows;
double sendrows;
double send_segments = 1;
double recv_segments = 1;
double total_rows;
CdbPathLocus sublocus = subpath->locus;
CdbPathLocus motionlocus = motionpath->path.locus;
int mot_parallel = motionlocus.parallel_workers;
int sub_parallel = sublocus.parallel_workers;
if (CdbPathLocus_IsPartitioned(motionlocus))
{
recv_segments = CdbPathLocus_NumSegments(motionlocus);
if (mot_parallel > 0)
recv_segments *= mot_parallel;
}
else if (mot_parallel > 0 && CdbPathLocus_IsReplicatedWorkers(motionlocus))
recv_segments *= mot_parallel;
if (CdbPathLocus_IsPartitioned(sublocus))
{
send_segments = CdbPathLocus_NumSegments(sublocus);
if (sub_parallel > 0)
send_segments *= sub_parallel;
}
else if (sub_parallel > 0 && CdbPathLocus_IsReplicatedWorkers(sublocus))
send_segments *= sub_parallel;
/*
* Estimate the total number of rows being sent.
* The base estimate is computed by multiplying the subpath's rows with
* the number of sending segments. But in some cases, that leads to too
* large estimates, if the subpath's estimate was "clamped" to 1 row. The
* typical example is a single-row select like "SELECT * FROM table WHERE
* key = 123. The Scan on the table returns only one row, on one segment,
* and the estimate on the Scan node is 1 row. If you have e.g. 3
* segments, and we just multiplied the subpath's row estimate by 3, we
* would estimate that the Gather returns 3 rows, even though there is
* only one matching row in the table. Using the 'rows' estimate on the
* RelOptInfo is more accurate in such cases. To correct that, if the
* subpath's estimate is 1 row, but the underlying relation's estimate is
* smaller, use the underlying relation's estimate.
*
* We don't always use the relation's estimate, because there might be
* nodes like ProjectSet or Limit in the subpath, in which case the
* subpath's estimate is more accurate. Also, the relation might not have
* a valid 'rows' estimate; upper rels, for example, do not. So check for
* that too.
*/
total_rows = subpath->rows * send_segments;
if (subpath->rows == 1.0 &&
motionpath->path.parent->rows > 0 &&
motionpath->path.parent->rows < total_rows)
{
/* use the RelOptInfo's estimate */
total_rows = motionpath->path.parent->rows;
}
motionpath->path.rows = clamp_row_est(total_rows / recv_segments);
cost_per_row = (gp_motion_cost_per_row > 0.0)
? gp_motion_cost_per_row
: 2.0 * cpu_tuple_cost;
sendrows = subpath->rows;
recvrows = motionpath->path.rows;
motioncost = cost_per_row * 0.5 * (sendrows + recvrows);
/*
* CBDB_PARALLEL_FIXME:
* Motioncost may be higher than sendrows + recvrows.
* ex: Broadcast Motion 3:6
* Broadcast to prallel workers, each worker's has a rel's all rows(recvrows),
* but the transfered cost will double as we will broadcast to 6 workers.
*/
if(CdbPathLocus_IsReplicated(motionlocus) && mot_parallel > 0)
motioncost *= mot_parallel;
motionpath->path.total_cost = motioncost + subpath->total_cost;
motionpath->path.startup_cost = subpath->startup_cost;
motionpath->path.memory = subpath->memory;
} /* cdbpath_cost_motion */
/*
* cdbpath_create_motion_path
* Returns a Path that delivers the subpath result to the
* given locus, or NULL if it can't be done.
*
* 'pathkeys' must specify an ordering equal to or weaker than the
* subpath's existing ordering.
*
* If no motion is needed, the caller's subpath is returned unchanged.
* Else if require_existing_order is true, NULL is returned if the
* motion would not preserve an ordering at least as strong as the
* specified ordering; also NULL is returned if pathkeys is NIL
* meaning the caller is just checking and doesn't want to add motion.
* Else a CdbMotionPath is returned having either the specified pathkeys
* (if given and the motion uses Merge Receive), or the pathkeys
* of the original subpath (if the motion is order-preserving), or no
* pathkeys otherwise (the usual case).
*/
Path *
cdbpath_create_motion_path(PlannerInfo *root,
Path *subpath,
List *pathkeys,
bool require_existing_order,
CdbPathLocus locus)
{
CdbMotionPath *pathnode;
Assert(cdbpathlocus_is_valid(locus) &&
cdbpathlocus_is_valid(subpath->locus));
/*
* ISTM, subpath of ReplicatedWorkers only happened if general join with broadcast.
* And that only happened if we're doing some updating. Such as:
* `explain update rt3 set b = rt2.b from rt2 where rt3.b = rt2.b;`
* where rt3 and rt2 should have different numsegments.
* However, we don't support parallel update yet, so it will never happen.
*/
Assert(!CdbPathLocus_IsReplicatedWorkers(subpath->locus));
/*
* Motion is to change path's locus, if target locus is the
* same as the subpath's, there is no need to add motion.
*/
if (cdbpathlocus_equal(subpath->locus, locus))
return subpath;
/* Moving subpath output to a single executor process (qDisp or qExec)? */
if (CdbPathLocus_IsOuterQuery(locus))
{
/* Outer -> Outer is a no-op */
if (CdbPathLocus_IsOuterQuery(subpath->locus))
{
return subpath;
}
if (CdbPathLocus_IsGeneral(subpath->locus))
{
/* XXX: this is a bit bogus. We just change the subpath's locus. */
subpath->locus = locus;
return subpath;
}
if (CdbPathLocus_IsEntry(subpath->locus) ||
CdbPathLocus_IsSingleQE(subpath->locus))
{
/*
* XXX: this is a bit bogus. We just change the subpath's locus.
*
* This is also bogus, because the outer query might need to run
* in segments.
*/
subpath->locus = locus;
return subpath;
}
}
else if (CdbPathLocus_IsBottleneck(locus))
{
/* entry-->entry? No motion needed. */
if (CdbPathLocus_IsEntry(subpath->locus) &&
CdbPathLocus_IsEntry(locus))
{
return subpath;
}
/* singleQE-->singleQE? No motion needed. */
if (CdbPathLocus_IsSingleQE(subpath->locus) &&
CdbPathLocus_IsSingleQE(locus))
{
subpath->locus.numsegments = CdbPathLocus_CommonSegments(subpath->locus, locus);
return subpath;
}
/* entry-->singleQE? Don't move. Slice's QE will run on entry db. */
if (CdbPathLocus_IsEntry(subpath->locus))
{
return subpath;
}
/* outerquery-->entry? No motion needed. */
if (CdbPathLocus_IsOuterQuery(subpath->locus) &&
CdbPathLocus_IsEntry(locus))
{
return subpath;
}
/* singleQE-->entry? Don't move. Slice's QE will run on entry db. */
if (CdbPathLocus_IsSingleQE(subpath->locus))
{
/*
* If the subpath requires parameters, we cannot generate Motion atop of it.
*/
if (!bms_is_empty(PATH_REQ_OUTER(subpath)))
return NULL;
/*
* Create CdbMotionPath node to indicate that the slice must be
* dispatched to a singleton gang running on the entry db. We
* merely use this node to note that the path has 'Entry' locus;
* no corresponding Motion node will be created in the Plan tree.
*/
Assert(CdbPathLocus_IsEntry(locus));
pathnode = makeNode(CdbMotionPath);
pathnode->path.pathtype = T_Motion;
pathnode->path.parent = subpath->parent;
/* Motion doesn't project, so use source path's pathtarget */
pathnode->path.pathtarget = subpath->pathtarget;
pathnode->path.locus = locus;
pathnode->path.rows = subpath->rows;
/* GPDB_96_MERGE_FIXME: When is a Motion path parallel-safe? I tried
* setting this to 'false' initially, to play it safe, but somehow
* the Paths with motions ended up in gather plans anyway, and tripped
* assertion failures.
*/
pathnode->path.parallel_aware = false;
pathnode->path.parallel_safe = subpath->parallel_safe;
pathnode->path.parallel_workers = locus.parallel_workers;
pathnode->path.pathkeys = pathkeys;
pathnode->subpath = subpath;
/* Costs, etc, are same as subpath. */
pathnode->path.startup_cost = subpath->total_cost;
pathnode->path.total_cost = subpath->total_cost;
pathnode->path.memory = subpath->memory;
pathnode->path.motionHazard = subpath->motionHazard;
pathnode->path.barrierHazard = subpath->barrierHazard;
/* Motion nodes are never rescannable. */
pathnode->path.rescannable = false;
return (Path *) pathnode;
}
if (CdbPathLocus_IsSegmentGeneral(subpath->locus) ||
CdbPathLocus_IsSegmentGeneralWorkers(subpath->locus) ||
CdbPathLocus_IsReplicated(subpath->locus))
{
/*
* If the subpath requires parameters, we cannot generate Motion atop of it.
*/
if (!bms_is_empty(PATH_REQ_OUTER(subpath)))
return NULL;
/*
* Data is only available on segments, to distingush it with
* CdbLocusType_General, adding a motion to indicated this
* slice must be executed on a singleton gang.
*
* This motion may be redundant for segmentGeneral --> singleQE
* if the singleQE is not promoted to executed on qDisp in the
* end, so in cdbllize_fix_outer_query_motions(), we will omit it.
*/
pathnode = makeNode(CdbMotionPath);
pathnode->path.pathtype = T_Motion;
pathnode->path.parent = subpath->parent;
/* Motion doesn't project, so use source path's pathtarget */
pathnode->path.pathtarget = subpath->pathtarget;
pathnode->path.locus = locus;
pathnode->path.rows = subpath->rows;
pathnode->path.pathkeys = pathkeys;
/* GPDB_96_MERGE_FIXME: When is a Motion path parallel-safe? I tried
* setting this to 'false' initially, to play it safe, but somehow
* the Paths with motions ended up in gather plans anyway, and tripped
* assertion failures.
*/
pathnode->path.parallel_aware = false;
pathnode->path.parallel_safe = subpath->parallel_safe;
pathnode->path.parallel_workers = locus.parallel_workers;
pathnode->subpath = subpath;
/* Costs, etc, are same as subpath. */
pathnode->path.startup_cost = subpath->total_cost;
pathnode->path.total_cost = subpath->total_cost;
pathnode->path.memory = subpath->memory;
pathnode->path.motionHazard = subpath->motionHazard;
pathnode->path.barrierHazard = subpath->barrierHazard;
/* Motion nodes are never rescannable. */
pathnode->path.rescannable = false;
return (Path *) pathnode;
}
/* No motion needed if subpath can run anywhere giving same output. */
if (CdbPathLocus_IsGeneral(subpath->locus))
{
/*
* general-->(entry|singleqe), no motion is needed, can run
* directly on any of the common segments
*/
return subpath;
}
/* Fail if caller refuses motion. */
if (require_existing_order &&
!pathkeys)
return NULL;
/*
* Must be partitioned-->singleton. If caller gave pathkeys, they'll
* be used for Merge Receive. If no pathkeys, Union Receive will
* arbitrarily interleave the rows from the subpath partitions in no
* special order.
*/
if (!CdbPathLocus_IsPartitioned(subpath->locus))
goto invalid_motion_request;
}
/* Output from a single process to be distributed over a gang? */
else if (CdbPathLocus_IsBottleneck(subpath->locus))
{
/* Must be bottleneck-->partitioned or bottleneck-->replicated */
if (!CdbPathLocus_IsPartitioned(locus) &&
!CdbPathLocus_IsReplicated(locus))
goto invalid_motion_request;
/* Fail if caller disallows motion. */
if (require_existing_order &&
!pathkeys)
return NULL;
/* Each qExec receives a subset of the rows, with ordering preserved. */
pathkeys = subpath->pathkeys;
}
/* Redistributing partitioned subpath output from one gang to another? */
else if (CdbPathLocus_IsPartitioned(subpath->locus))
{
/* partitioned-->partitioned? */
if (CdbPathLocus_IsPartitioned(locus))
{
/* No motion if subpath partitioning matches caller's request. */
if (cdbpathlocus_equal(subpath->locus, locus))
return subpath;
}
/* Must be partitioned-->replicated */
else if (!CdbPathLocus_IsReplicated(locus) && !CdbPathLocus_IsHashedWorkers(locus) && !CdbPathLocus_IsReplicatedWorkers(locus))
goto invalid_motion_request;
/* Fail if caller insists on ordered result or no motion. */
if (require_existing_order)
return NULL;
/*
* Output streams lose any ordering they had. Only a qDisp or
* singleton qExec can merge sorted streams (for now).
*/
pathkeys = NIL;
}
/* If subplan uses no tables, it can run on qDisp or a singleton qExec. */
else if (CdbPathLocus_IsGeneral(subpath->locus))
{
/*
* Parallel replicating is now only happening if both sides are not general.
*/
Assert(!CdbPathLocus_IsReplicatedWorkers(locus));
/*
* No motion needed if general-->general or general-->replicated or
* general-->segmentGeneral
*/
if (CdbPathLocus_IsGeneral(locus) ||
CdbPathLocus_IsReplicated(locus) ||
CdbPathLocus_IsSegmentGeneral(locus))
{
return subpath;
}
if (CdbPathLocus_IsSegmentGeneralWorkers(subpath->locus))
goto invalid_motion_request;
/* Must be general-->partitioned. */
if (!CdbPathLocus_IsPartitioned(locus))
goto invalid_motion_request;
/* Fail if caller wants no motion. */
if (require_existing_order &&
!pathkeys)
return NULL;
/* Since the motion is 1-to-many, the rows remain in the same order. */
pathkeys = subpath->pathkeys;
}
/* Does subpath produce same multiset of rows on every qExec of its gang? */
else if (CdbPathLocus_IsReplicated(subpath->locus))
{
/* No-op if replicated-->replicated. */
if (CdbPathLocus_IsReplicated(locus))
{
Assert(CdbPathLocus_NumSegments(locus) <=
CdbPathLocus_NumSegments(subpath->locus));
subpath->locus.numsegments = CdbPathLocus_NumSegments(locus);
return subpath;
}
/* Other destinations aren't used or supported at present. */
goto invalid_motion_request;
}
/* Most motions from SegmentGeneral (replicated table) are disallowed */
else if (CdbPathLocus_IsSegmentGeneral(subpath->locus) || CdbPathLocus_IsSegmentGeneralWorkers(subpath->locus))
{
/*
* The only allowed case is a SegmentGeneral to Hashed motion,
* and SegmentGeneral's numsegments is smaller than Hashed's.
* In such a case we redistribute SegmentGeneral to Hashed.
*
* FIXME: HashedOJ?
*/
if (CdbPathLocus_IsPartitioned(locus))
{
pathkeys = subpath->pathkeys;
}
else if (CdbPathLocus_IsReplicated(locus))
{
if (CdbPathLocus_NumSegments(locus) <= CdbPathLocus_NumSegments(subpath->locus))
{
subpath->locus.numsegments = CdbPathLocus_NumSegments(locus);
return subpath;
}
else
{
pathkeys = subpath->pathkeys;
}
}
else if (CdbPathLocus_IsSegmentGeneral(locus))
{
subpath->locus.numsegments = Min(subpath->locus.numsegments, locus.numsegments);
return subpath;
}
else
goto invalid_motion_request;
}
else
goto invalid_motion_request;
/* Don't materialize before motion. */
if (IsA(subpath, MaterialPath))
subpath = ((MaterialPath *) subpath)->subpath;
/*
* MPP-3300: materialize *before* motion can never help us, motion pushes
* data. other nodes pull. We relieve motion deadlocks by adding
* materialize nodes on top of motion nodes
*/
if(IsA(subpath,SubqueryScanPath)
&& CdbPathLocus_IsBottleneck(locus)
&& !subpath->pathkeys
&& ((SubqueryScanPath *)subpath)->subpath->pathkeys)
{
/*
* In gpdb, when there is a Gather Motion on top of a SubqueryScan,
* it is hard to keep the sort order information. The subquery's
* path key cannot be pulled up, if the parent query doesn't have
* an equivalence class corresponding to the subquery's sort order.
*
* e.g. create a view with an ORDER BY:
*
* CREATE VIEW v AS SELECT va, vn FROM sourcetable ORDER BY vn;
*
* and query it:
*
* SELECT va FROM v_sourcetable;
*
* So, push down the Gather Motion if the SubqueryScan dose not
* have pathkey but the SubqueryScan's subpath does.
*
*/
SubqueryScanPath *subqueryScanPath = (SubqueryScanPath *)subpath;
SubqueryScanPath *newSubqueryScanPath = NULL;
Path *motionPath = NULL;
subpath = subqueryScanPath->subpath;
motionPath = cdbpath_create_motion_path(root,
subpath,
subpath->pathkeys,
true,
locus);
newSubqueryScanPath = create_subqueryscan_path(root,
subqueryScanPath->path.parent,
motionPath,
subqueryScanPath->path.pathkeys,
locus,
subqueryScanPath->required_outer);
/* apply final path target */
newSubqueryScanPath = (SubqueryScanPath *)apply_projection_to_path(root,
subqueryScanPath->path.parent,
(Path *) newSubqueryScanPath,
subqueryScanPath->path.pathtarget);
return (Path *) newSubqueryScanPath;
}
/*
* If the subpath requires parameters, we cannot generate Motion atop of it.
*/
if (!bms_is_empty(PATH_REQ_OUTER(subpath)))
return NULL;
/* Create CdbMotionPath node. */
pathnode = makeNode(CdbMotionPath);
pathnode->path.pathtype = T_Motion;
pathnode->path.parent = subpath->parent;
/* Motion doesn't project, so use source path's pathtarget */
pathnode->path.pathtarget = subpath->pathtarget;
pathnode->path.locus = locus;
pathnode->path.rows = subpath->rows;
pathnode->path.pathkeys = pathkeys;
/* GPDB_96_MERGE_FIXME: When is a Motion path parallel-safe? I tried
* setting this to 'false' initially, to play it safe, but somehow
* the Paths with motions ended up in gather plans anyway, and tripped
* assertion failures.
*/
pathnode->path.parallel_aware = false;
/*
* CBDB_PARALLEL_FIXME:
* We once set parallel_safe by locus type, but almost all locus are
* parallel safe nowadays.
* In principle, we should set parallel_safe = true if we are in a parallel join.
* TODO: Set parallel_safe to true for all locus.
*/
pathnode->path.parallel_safe = (locus.parallel_workers > 0 ||
CdbPathLocus_IsHashedWorkers(locus) ||
CdbPathLocus_IsSingleQE(locus) ||
CdbPathLocus_IsEntry(locus) ||
CdbPathLocus_IsReplicatedWorkers(locus) ||
CdbPathLocus_IsReplicated(locus) || /* CTAS replicated table */
CdbPathLocus_IsHashed(locus));
if (!subpath->parallel_safe)
pathnode->path.parallel_safe = false;
pathnode->path.parallel_workers = locus.parallel_workers;
pathnode->subpath = subpath;
pathnode->is_explicit_motion = false;
/* Cost of motion */
cdbpath_cost_motion(root, pathnode);
/* Tell operators above us that slack may be needed for deadlock safety. */
pathnode->path.motionHazard = true;
/*
* If parallel workers > 0, which means barrier hazard exits for parallel
* hash join.
*/
pathnode->path.barrierHazard = (locus.parallel_workers > 0);
pathnode->path.rescannable = false;
/*
* A motion to bring data to the outer query's locus needs a Material node
* on top, to shield the Motion node from rescanning, when the SubPlan
* is rescanned.
*/
if (CdbPathLocus_IsOuterQuery(locus))
{
return (Path *) create_material_path(subpath->parent, &pathnode->path);
}
return (Path *) pathnode;
/* Unexpected source or destination locus. */
invalid_motion_request:
elog(ERROR, "could not build Motion path");
return NULL;
} /* cdbpath_create_motion_path */
Path *
cdbpath_create_explicit_motion_path(PlannerInfo *root,
Path *subpath,
CdbPathLocus locus)
{
CdbMotionPath *pathnode;
/* Create CdbMotionPath node. */
pathnode = makeNode(CdbMotionPath);
pathnode->path.pathtype = T_Motion;
pathnode->path.parent = subpath->parent;
/* Motion doesn't project, so use source path's pathtarget */
pathnode->path.pathtarget = subpath->pathtarget;
pathnode->path.locus = locus;
pathnode->path.rows = subpath->rows;
pathnode->path.pathkeys = NIL;
/* GPDB_96_MERGE_FIXME: When is a Motion path parallel-safe? I tried
* setting this to 'false' initially, to play it safe, but somehow
* the Paths with motions ended up in gather plans anyway, and tripped
* assertion failures.
*/
pathnode->path.parallel_aware = false;
pathnode->path.parallel_safe = false;
pathnode->path.parallel_workers = subpath->parallel_workers;
pathnode->subpath = subpath;
pathnode->is_explicit_motion = true;
/* Cost of motion */
cdbpath_cost_motion(root, pathnode);
/* Tell operators above us that slack may be needed for deadlock safety. */
pathnode->path.motionHazard = true;
pathnode->path.barrierHazard = false;
pathnode->path.rescannable = false;
return (Path *) pathnode;
}
Path *
cdbpath_create_broadcast_motion_path(PlannerInfo *root,
Path *subpath,
int numsegments)
{
CdbMotionPath *pathnode;
/* Create CdbMotionPath node. */
pathnode = makeNode(CdbMotionPath);
pathnode->path.pathtype = T_Motion;
pathnode->path.parent = subpath->parent;
/* Motion doesn't project, so use source path's pathtarget */
pathnode->path.pathtarget = subpath->pathtarget;
CdbPathLocus_MakeReplicated(&pathnode->path.locus, numsegments, subpath->parallel_workers);
pathnode->path.rows = subpath->rows;
pathnode->path.pathkeys = NIL;
/* GPDB_96_MERGE_FIXME: When is a Motion path parallel-safe? I tried
* setting this to 'false' initially, to play it safe, but somehow
* the Paths with motions ended up in gather plans anyway, and tripped
* assertion failures.
*/
pathnode->path.parallel_aware = false;
pathnode->path.parallel_safe = false;
pathnode->path.parallel_workers = subpath->parallel_workers;
pathnode->subpath = subpath;
pathnode->is_explicit_motion = false;
/* Cost of motion */
cdbpath_cost_motion(root, pathnode);
/* Tell operators above us that slack may be needed for deadlock safety. */
pathnode->path.motionHazard = true;
pathnode->path.barrierHazard = false;
pathnode->path.rescannable = false;
return (Path *) pathnode;
}
/*
*/
static CdbMotionPath *
make_motion_path(PlannerInfo *root, Path *subpath,
CdbPathLocus locus,
bool is_explicit_motion,
GpPolicy *policy)
{
CdbMotionPath *pathnode;
/* Create CdbMotionPath node. */
pathnode = makeNode(CdbMotionPath);
pathnode->path.pathtype = T_Motion;
pathnode->path.parent = subpath->parent;
/* Motion doesn't project, so use source path's pathtarget */
pathnode->path.pathtarget = subpath->pathtarget;
pathnode->path.locus = locus;
pathnode->path.rows = subpath->rows;
pathnode->path.pathkeys = NIL;
/* GPDB_96_MERGE_FIXME: When is a Motion path parallel-safe? I tried
* setting this to 'false' initially, to play it safe, but somehow
* the Paths with motions ended up in gather plans anyway, and tripped
* assertion failures.
*/
pathnode->path.parallel_aware = false;
pathnode->path.parallel_safe = false;
pathnode->path.parallel_workers = subpath->parallel_workers;
pathnode->subpath = subpath;
pathnode->is_explicit_motion = is_explicit_motion;
pathnode->policy = policy;
/* Cost of motion */
cdbpath_cost_motion(root, pathnode);
/* Tell operators above us that slack may be needed for deadlock safety. */
pathnode->path.motionHazard = true;
pathnode->path.barrierHazard = false;
pathnode->path.rescannable = false;
return pathnode;
}
/*
* cdbpath_match_preds_to_partkey_tail
*
* Returns true if all of the locus's partitioning key expressions are
* found as comparands of equijoin predicates in the mergeclause_list.
*
* NB: for mergeclause_list and pathkey structure assumptions, see:
* select_mergejoin_clauses() in joinpath.c
* find_mergeclauses_for_pathkeys() in pathkeys.c
*/
typedef struct
{
PlannerInfo *root;
List *mergeclause_list;
Path *path;
CdbPathLocus locus;
CdbPathLocus otherlocus;
CdbPathLocus *colocus;
bool colocus_eq_locus;
} CdbpathMatchPredsContext;
/*
* A helper function to create a DistributionKey for an EquivalenceClass.
*/
static DistributionKey *
makeDistributionKeyForEC(EquivalenceClass *eclass, Oid opfamily)
{
DistributionKey *dk = makeNode(DistributionKey);
Assert(OidIsValid(opfamily));
dk->dk_eclasses = list_make1(eclass);
dk->dk_opfamily = opfamily;
return dk;
}
/*
* cdbpath_eclass_constant_is_hashable
*
* Iterates through a list of equivalence class members and determines if
* expression in pseudoconstant is hashable under the given hash opfamily.
*
* If there are multiple constants in the equivalence class, it is sufficient
* that one of them is usable.
*/
static bool
cdbpath_eclass_constant_is_hashable(EquivalenceClass *ec, Oid hashOpFamily)
{
ListCell *j;
foreach(j, ec->ec_members)
{
EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
if (!em->em_is_const)
continue;
if (get_opfamily_member(hashOpFamily, em->em_datatype, em->em_datatype,
HTEqualStrategyNumber))
return true;
}
return false;
}
static bool
cdbpath_match_preds_to_distkey_tail(CdbpathMatchPredsContext *ctx,
List *list, ListCell *distkeycell, bool parallel_aware)
{
DistributionKey *distkey = (DistributionKey *) lfirst(distkeycell);
DistributionKey *codistkey;
ListCell *cell;
ListCell *rcell;
Assert(CdbPathLocus_IsHashed(ctx->locus) ||
CdbPathLocus_IsHashedWorkers(ctx->locus) ||
CdbPathLocus_IsHashedOJ(ctx->locus));
/*
* Try ro redistributed one to match another.
* non-parallel_aware
* HashedWorkers can only work with replica, can't redistributed one to match
*/
if (!parallel_aware && CdbPathLocus_IsHashedWorkers(ctx->locus))
return false;
/*----------------
* Is there a "<distkey item> = <constant expr>" predicate?
*
* If table T is distributed on cols (C,D,E) and query contains preds
* T.C = U.A AND T.D = <constant expr> AND T.E = U.B
* then we would like to report a match and return the colocus
* (U.A, <constant expr>, U.B)
* so the caller can join T and U by redistributing only U.
* (Note that "T.D = <constant expr>" won't be in the mergeclause_list
* because it isn't a join pred.)
*----------------
*/
codistkey = NULL;
foreach(cell, distkey->dk_eclasses)
{
EquivalenceClass *ec = (EquivalenceClass *) lfirst(cell);
if (CdbEquivClassIsConstant(ec) &&
cdbpath_eclass_constant_is_hashable(ec, distkey->dk_opfamily))
{
codistkey = distkey;
break;
}
}
/* Look for an equijoin comparison to the distkey item. */
if (!codistkey)
{
foreach(rcell, ctx->mergeclause_list)
{
EquivalenceClass *a_ec; /* Corresponding to ctx->path. */
EquivalenceClass *b_ec;
ListCell *i;
RestrictInfo *rinfo = (RestrictInfo *) lfirst(rcell);
update_mergeclause_eclasses(ctx->root, rinfo);
if (bms_is_subset(rinfo->right_relids, ctx->path->parent->relids))
{
a_ec = rinfo->right_ec;
b_ec = rinfo->left_ec;
}
else
{
a_ec = rinfo->left_ec;
b_ec = rinfo->right_ec;
Assert(bms_is_subset(rinfo->left_relids, ctx->path->parent->relids));
}
foreach(i, distkey->dk_eclasses)
{
EquivalenceClass *dk_eclass = (EquivalenceClass *) lfirst(i);
if (dk_eclass == a_ec)
codistkey = makeDistributionKeyForEC(b_ec, distkey->dk_opfamily); /* break earlier? */
}
if (codistkey)
break;
}
}
/* Fail if didn't find a match for this distkey item. */
if (!codistkey)
return false;
/* Might need to build co-locus if locus is outer join source or result. */
if (codistkey != lfirst(distkeycell))
ctx->colocus_eq_locus = false;
/* Match remaining partkey items. */
distkeycell = lnext(list, distkeycell);
if (distkeycell)
{
if (!cdbpath_match_preds_to_distkey_tail(ctx, list, distkeycell, parallel_aware))
return false;
}
/* Success! Matched all items. Return co-locus if requested. */
if (ctx->colocus)
{
if (ctx->colocus_eq_locus)
*ctx->colocus = ctx->locus;
else if (!distkeycell)
CdbPathLocus_MakeHashed(ctx->colocus, list_make1(codistkey),
CdbPathLocus_NumSegments(ctx->locus),
ctx->locus.parallel_workers);
else
{
ctx->colocus->distkey = lcons(codistkey, ctx->colocus->distkey);
Assert(cdbpathlocus_is_valid(*ctx->colocus));
}
}
return true;
} /* cdbpath_match_preds_to_partkey_tail */
/*
* cdbpath_match_preds_to_partkey
*
* Returns true if an equijoin predicate is found in the mergeclause_list
* for each item of the locus's partitioning key.
*
* (Also, a partkey item that is equal to a constant is treated as a match.)
*
* Readers may refer also to these related functions:
* select_mergejoin_clauses() in joinpath.c
* find_mergeclauses_for_pathkeys() in pathkeys.c
*/
static bool
cdbpath_match_preds_to_distkey(PlannerInfo *root,
List *mergeclause_list,
Path *path,
CdbPathLocus locus,
CdbPathLocus otherlocus,
bool parallel_aware,
CdbPathLocus *colocus) /* OUT */
{
CdbpathMatchPredsContext ctx;
if (!CdbPathLocus_IsHashed(locus) &&
!CdbPathLocus_IsHashedOJ(locus) &&
!CdbPathLocus_IsHashedWorkers(locus))
return false;
/*
* Don't bother to redistribute to non-parallel locus if parallel_aware is true.
* We should already consider non-parallel join of the same two path before.
*/