forked from heterodb/pg-strom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_join.c
2902 lines (2655 loc) · 78.7 KB
/
gpu_join.c
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
/*
* gpu_join.c
*
* Multi-relations join accelerated with GPU processors
* ----
* Copyright 2011-2023 (C) KaiGai Kohei <[email protected]>
* Copyright 2014-2023 (C) PG-Strom Developers Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the PostgreSQL License.
*/
#include "pg_strom.h"
#include "cuda_common.h"
/* static variables */
static set_join_pathlist_hook_type set_join_pathlist_next = NULL;
static CustomPathMethods gpujoin_path_methods;
static CustomScanMethods gpujoin_plan_methods;
static CustomExecMethods gpujoin_exec_methods;
static bool pgstrom_enable_gpujoin = false; /* GUC */
static bool pgstrom_enable_gpuhashjoin = false; /* GUC */
static bool pgstrom_enable_gpugistindex = false;/* GUC */
static bool pgstrom_enable_partitionwise_gpujoin = false;
static CustomPathMethods dpujoin_path_methods;
static CustomScanMethods dpujoin_plan_methods;
static CustomExecMethods dpujoin_exec_methods;
static bool pgstrom_enable_dpujoin = false; /* GUC */
static bool pgstrom_enable_dpuhashjoin = false; /* GUC */
static bool pgstrom_enable_dpugistindex = false;/* GUC */
static bool pgstrom_enable_partitionwise_dpujoin = false;
static bool pgstrom_debug_xpujoinpath = false;
/*
* DEBUG_XpuJoinPath
*/
static inline void
__appendRangeTblEntry(StringInfo buf,
Index rtindex,
RangeTblEntry *rte)
{
if (rte->rtekind == RTE_RELATION)
{
char *relname = get_rel_name(rte->relid);
appendStringInfo(buf, "%s", relname);
if (rte->eref &&
rte->eref->aliasname &&
strcmp(relname, rte->eref->aliasname) != 0)
appendStringInfo(buf, "[%s]", rte->eref->aliasname);
}
else
{
const char *label;
switch (rte->rtekind)
{
case RTE_SUBQUERY: label = "subquery"; break;
case RTE_JOIN: label = "join"; break;
case RTE_FUNCTION: label = "function"; break;
case RTE_TABLEFUNC: label = "table-function"; break;
case RTE_VALUES: label = "values-list"; break;
case RTE_CTE: label = "cte"; break;
case RTE_NAMEDTUPLESTORE: label = "tuplestore"; break;
case RTE_RESULT: label = "result"; break;
default: label = "unknown"; break;
}
if (rte->eref &&
rte->eref->aliasname)
appendStringInfo(buf, "[%s:%s]", label, rte->eref->aliasname);
else
appendStringInfo(buf, "[%s:%u]", label, rtindex);
}
}
static void
DEBUG_XpuJoinPathPrint(PlannerInfo *root,
const char *custom_name,
const Path *path,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel)
{
if (pgstrom_debug_xpujoinpath)
{
StringInfoData buf;
int i, count;
initStringInfo(&buf);
appendStringInfo(&buf, "%s: outer=(", custom_name);
for (i = bms_next_member(outer_rel->relids, -1), count=0;
i >= 0;
i = bms_next_member(outer_rel->relids, i), count++)
{
RangeTblEntry *rte = root->simple_rte_array[i];
if (count > 0)
appendStringInfo(&buf, ", ");
__appendRangeTblEntry(&buf, i, rte);
}
appendStringInfo(&buf, ") inner=(");
for (i = bms_next_member(outer_rel->relids, -1), count=0;
i >= 0;
i = bms_next_member(outer_rel->relids, i), count++)
{
RangeTblEntry *rte = root->simple_rte_array[i];
if (count > 0)
appendStringInfo(&buf, ", ");
__appendRangeTblEntry(&buf, i, rte);
}
appendStringInfo(&buf, ") parallel=%d cost=%.2f nrows=%.0f",
(int)path->parallel_aware,
path->total_cost,
path->rows);
elog(NOTICE, "%s", buf.data);
pfree(buf.data);
}
}
/*
* try_fetch_xpujoin_planinfo
*/
pgstromPlanInfo *
try_fetch_xpujoin_planinfo(const Path *path)
{
const CustomPath *cpath = (const CustomPath *)path;
if (IsA(cpath, CustomPath) &&
(cpath->methods == &gpujoin_path_methods ||
cpath->methods == &dpujoin_path_methods))
return (pgstromPlanInfo *)linitial(cpath->custom_private);
return NULL;
}
/*
* __buildXpuJoinPlanInfo
*/
static pgstromPlanInfo *
__buildXpuJoinPlanInfo(PlannerInfo *root,
RelOptInfo *joinrel,
JoinType join_type,
List *restrict_clauses,
pgstromOuterPathLeafInfo *op_prev,
List *inner_paths_list,
int sibling_param_id,
double inner_discount_ratio)
{
pgstromPlanInfo *pp_prev = op_prev->pp_info;
pgstromPlanInfo *pp_info;
pgstromPlanInnerInfo *pp_inner;
Path *inner_path = llast(inner_paths_list);
RelOptInfo *inner_rel = inner_path->parent;
RelOptInfo *outer_rel = op_prev->leaf_rel;
Cardinality outer_nrows;
Cardinality inner_nrows;
Cost startup_cost;
Cost inner_cost;
Cost run_cost;
Cost final_cost;
Cost comp_cost = 0.0;
bool enable_xpuhashjoin;
bool enable_xpugistindex;
double xpu_tuple_cost;
Cost xpu_ratio;
QualCost join_quals_cost;
List *join_quals = NIL;
List *other_quals = NIL;
List *hash_outer_keys = NIL;
List *hash_inner_keys = NIL;
List *inner_target_list = NIL;
ListCell *lc;
bool clauses_are_immutable = true;
/* cross join is not welcome */
if (!restrict_clauses)
return NULL;
/*
* device specific parameters
*/
if ((pp_prev->xpu_task_flags & DEVKIND__ANY) == DEVKIND__NVIDIA_GPU)
{
enable_xpuhashjoin = pgstrom_enable_gpuhashjoin;
enable_xpugistindex = pgstrom_enable_gpugistindex;
xpu_tuple_cost = pgstrom_gpu_tuple_cost;
xpu_ratio = pgstrom_gpu_operator_ratio();
}
else if ((pp_prev->xpu_task_flags & DEVKIND__ANY) == DEVKIND__NVIDIA_DPU)
{
enable_xpuhashjoin = pgstrom_enable_dpuhashjoin;
enable_xpugistindex = pgstrom_enable_dpugistindex;
xpu_tuple_cost = pgstrom_dpu_tuple_cost;
xpu_ratio = pgstrom_dpu_operator_ratio();
}
else
{
elog(ERROR, "Bug? unexpected xpu_task_flags: %08x",
pp_prev->xpu_task_flags);
}
/* setup inner_target_list */
foreach (lc, inner_paths_list)
{
Path *i_path = lfirst(lc);
inner_target_list = lappend(inner_target_list, i_path->pathtarget);
}
/*
* All the join-clauses must be executable on GPU device.
* Even though older version supports HostQuals to be
* applied post device join, it leads undesirable (often
* unacceptable) growth of the result rows in device join.
* So, we simply reject any join that contains host-only
* qualifiers.
*/
foreach (lc, restrict_clauses)
{
RestrictInfo *rinfo = lfirst(lc);
/*
* In case when neither outer-vars nor inner-vars are not referenced,
* this join always works as a cross-join or an empty-join.
* They are not welcome for xPU-Join workloads regardless of the cost
* estimation.
*/
if (contain_var_clause((Node *)rinfo->clause))
clauses_are_immutable = false;
/*
* Is the JOIN-clause executable on the target device?
*/
if (!pgstrom_xpu_expression(rinfo->clause,
pp_prev->xpu_task_flags,
pp_prev->scan_relid,
inner_target_list,
NULL))
{
return NULL;
}
/*
* See the logic in extract_actual_join_clauses()
* We need to distinguish between the join-qualifiers explicitly described
* in the OUTER JOIN ... ON clause from other qualifiers come from WHERE-
* clause then pushed down to the joinrel.
*/
if (IS_OUTER_JOIN(join_type) &&
RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
{
if (!rinfo->pseudoconstant)
other_quals = lappend(other_quals, rinfo->clause);
continue;
}
else
{
Assert(!rinfo->pseudoconstant);
join_quals = lappend(join_quals, rinfo->clause);
}
/* Is the hash-join enabled? */
if (!enable_xpuhashjoin)
continue;
/* Is it hash-joinable clause? */
if (!rinfo->can_join || !OidIsValid(rinfo->hashjoinoperator))
continue;
Assert(is_opclause(rinfo->clause));
/*
* Check if clause has the form "outer op inner" or
* "inner op outer". If suitable, we may be able to choose
* GpuHashJoin logic. See clause_sides_match_join also.
*/
if ((bms_is_subset(rinfo->left_relids, outer_rel->relids) &&
bms_is_subset(rinfo->right_relids, inner_rel->relids)) ||
(bms_is_subset(rinfo->left_relids, inner_rel->relids) &&
bms_is_subset(rinfo->right_relids, outer_rel->relids)))
{
OpExpr *op = (OpExpr *)rinfo->clause;
Node *arg1 = linitial(op->args);
Node *arg2 = lsecond(op->args);
Relids relids1 = pull_varnos(root, arg1);
Relids relids2 = pull_varnos(root, arg2);
devtype_info *dtype;
/* hash-join key must support device hash function */
dtype = pgstrom_devtype_lookup(exprType(arg1));
if (!dtype || !dtype->type_hashfunc)
continue;
dtype = pgstrom_devtype_lookup(exprType(arg2));
if (!dtype || !dtype->type_hashfunc)
continue;
if (bms_is_subset(relids1, outer_rel->relids) &&
bms_is_subset(relids2, inner_rel->relids))
{
hash_outer_keys = lappend(hash_outer_keys, arg1);
hash_inner_keys = lappend(hash_inner_keys, arg2);
}
else if (bms_is_subset(relids1, inner_rel->relids) &&
bms_is_subset(relids2, outer_rel->relids))
{
hash_inner_keys = lappend(hash_inner_keys, arg1);
hash_outer_keys = lappend(hash_outer_keys, arg2);
}
bms_free(relids1);
bms_free(relids2);
}
}
if (clauses_are_immutable)
{
elog(DEBUG2, "immutable join-clause is not supported: %s",
nodeToString(restrict_clauses));
return NULL;
}
/*
* Setup pgstromPlanInfo
*/
pp_info = copy_pgstrom_plan_info(pp_prev);
pp_info->xpu_task_flags &= ~DEVTASK__MASK;
pp_info->xpu_task_flags |= DEVTASK__JOIN;
pp_info->sibling_param_id = sibling_param_id;
pp_inner = &pp_info->inners[pp_info->num_rels++];
pp_inner->join_type = join_type;
pp_inner->join_nrows = joinrel->rows;
pp_inner->hash_outer_keys = hash_outer_keys;
pp_inner->hash_inner_keys = hash_inner_keys;
pp_inner->join_quals = join_quals;
pp_inner->other_quals = other_quals;
/* GiST-Index availability checks */
if (enable_xpugistindex &&
hash_outer_keys == NIL &&
hash_inner_keys == NIL)
{
Path *orig_inner_path = llast(inner_paths_list);
Path *gist_inner_path
= pgstromTryFindGistIndex(root,
orig_inner_path,
restrict_clauses,
pp_prev->xpu_task_flags,
pp_prev->scan_relid,
inner_target_list,
pp_inner);
if (gist_inner_path)
llast(inner_paths_list) = gist_inner_path;
}
/*
* Cost estimation
*/
startup_cost = pp_prev->startup_cost;
inner_cost = pp_prev->inner_cost;
run_cost = pp_prev->run_cost;
final_cost = 0.0;
outer_nrows = PP_INFO_NUM_ROWS(pp_prev);
/*
* Cost for inner-setup
*/
inner_nrows = inner_path->rows;
if (inner_path->parallel_aware)
{
double divisor = inner_path->parallel_workers;
double leader_contribution;
if (parallel_leader_participation)
{
leader_contribution = 1.0 - (0.3 * inner_path->parallel_workers);
if (leader_contribution > 0.0)
divisor += leader_contribution;
}
inner_nrows *= divisor;
}
inner_cost += (inner_path->total_cost +
inner_nrows * cpu_tuple_cost) * inner_discount_ratio;
/*
* Cost for join_quals
*/
cost_qual_eval(&join_quals_cost, join_quals, root);
startup_cost += join_quals_cost.startup;
if (hash_outer_keys != NIL && hash_inner_keys != NIL)
{
/*
* GpuHashJoin - It computes hash-value of inner tuples by CPU,
* but outer tuples by GPU, then it evaluates join-qualifiers
* for each items on inner hash table by GPU.
*/
int num_hashkeys = list_length(hash_outer_keys);
/* cost to compute inner hash value by CPU */
startup_cost += cpu_operator_cost * num_hashkeys * inner_path->rows;
/* cost to comput hash value by GPU */
comp_cost += (cpu_operator_cost * xpu_ratio *
num_hashkeys *
outer_nrows);
/* cost to evaluate join qualifiers */
comp_cost += join_quals_cost.per_tuple * xpu_ratio * outer_nrows;
}
else if (OidIsValid(pp_inner->gist_index_oid))
{
/*
* GpuNestLoop+GiST-Index
*/
Expr *gist_clause = pp_inner->gist_clause;
double gist_selectivity = pp_inner->gist_selectivity;
QualCost gist_clause_cost;
/* cost to preload inner heap tuples by CPU */
startup_cost += cpu_tuple_cost * inner_path->rows;
/* cost to preload the entire index pages once */
startup_cost += seq_page_cost * pp_inner->gist_npages;
/* cost to evaluate GiST index by GPU */
cost_qual_eval_node(&gist_clause_cost, (Node *)gist_clause, root);
comp_cost += gist_clause_cost.per_tuple * xpu_ratio * outer_nrows;
/* cost to evaluate join qualifiers by GPU */
comp_cost += (join_quals_cost.per_tuple * xpu_ratio *
outer_nrows *
gist_selectivity *
inner_path->rows);
}
else
{
/*
* GpuNestLoop - It evaluates join-qual for each pair of outer
* and inner tuples. So, its run_cost is usually higher than
* GpuHashJoin.
*/
/* cost to preload inner heap tuples by CPU */
startup_cost += cpu_tuple_cost * inner_path->rows;
/* cost to evaluate join qualifiers by GPU */
comp_cost += (join_quals_cost.per_tuple * xpu_ratio *
inner_path->rows *
outer_nrows);
}
/* discount if CPU parallel is enabled */
run_cost += (comp_cost / pp_info->parallel_divisor);
/* cost for DMA receive (xPU --> Host) */
final_cost += (xpu_tuple_cost * joinrel->rows) / pp_info->parallel_divisor;
/* cost for host projection */
final_cost += (joinrel->reltarget->cost.per_tuple *
joinrel->rows / pp_info->parallel_divisor);
pp_info->startup_cost = startup_cost;
pp_info->inner_cost = inner_cost;
pp_info->run_cost = run_cost;
pp_info->final_cost = final_cost;
pp_inner->join_nrows = (joinrel->rows / pp_info->parallel_divisor);
return pp_info;
}
/*
* __build_simple_xpujoin_path
*/
static CustomPath *
__build_simple_xpujoin_path(PlannerInfo *root,
RelOptInfo *join_rel,
Path *outer_path, /* may be pseudo outer-path */
Path *inner_path,
JoinType join_type,
pgstromOuterPathLeafInfo *op_prev,
pgstromOuterPathLeafInfo **p_op_leaf,
List *restrict_clauses,
SpecialJoinInfo *sjinfo,
Relids param_source_rels,
bool try_parallel_path,
int sibling_param_id,
double inner_discount_ratio,
uint32_t xpu_task_flags,
const CustomPathMethods *xpujoin_path_methods)
{
pgstromPlanInfo *pp_info;
Relids required_outer;
ParamPathInfo *param_info;
CustomPath *cpath;
List *inner_paths_list;
required_outer = calc_non_nestloop_required_outer(outer_path,
inner_path);
if (required_outer &&
!bms_overlap(required_outer, param_source_rels))
return NULL;
/*
* Get param info
*/
param_info = get_joinrel_parampathinfo(root,
join_rel,
outer_path,
inner_path,
sjinfo,
required_outer,
&restrict_clauses);
if (restrict_clauses == NIL)
return NULL; /* cross join is not welcome */
/*
* Build a new pgstromPlanInfo
*/
inner_paths_list = list_copy(op_prev->inner_paths_list);
inner_paths_list = lappend(inner_paths_list, inner_path);
pp_info = __buildXpuJoinPlanInfo(root,
join_rel,
join_type,
restrict_clauses,
op_prev,
inner_paths_list,
sibling_param_id,
inner_discount_ratio);
if (!pp_info)
return NULL;
/*
* Build a new CustomPath
*/
cpath = makeNode(CustomPath);
cpath->path.pathtype = T_CustomScan;
cpath->path.parent = join_rel;
cpath->path.pathtarget = join_rel->reltarget;
cpath->path.param_info = param_info;
cpath->path.parallel_aware = try_parallel_path;
cpath->path.parallel_safe = join_rel->consider_parallel;
cpath->path.parallel_workers = pp_info->parallel_nworkers;
cpath->path.pathkeys = NIL;
cpath->path.rows = PP_INFO_NUM_ROWS(pp_info);
cpath->path.startup_cost = (pp_info->startup_cost +
pp_info->inner_cost);
cpath->path.total_cost = (pp_info->startup_cost +
pp_info->inner_cost +
pp_info->run_cost +
pp_info->final_cost);
cpath->flags = CUSTOMPATH_SUPPORT_PROJECTION;
cpath->methods = xpujoin_path_methods;
Assert(list_length(inner_paths_list) == pp_info->num_rels);
cpath->custom_paths = inner_paths_list;
cpath->custom_private = list_make1(pp_info);
if (p_op_leaf)
{
pgstromOuterPathLeafInfo *op_leaf;
op_leaf = palloc0(sizeof(pgstromOuterPathLeafInfo));
op_leaf->pp_info = pp_info;
op_leaf->leaf_rel = join_rel;
op_leaf->leaf_param = param_info;
op_leaf->leaf_nrows = cpath->path.rows;
op_leaf->leaf_cost = cpath->path.total_cost;
op_leaf->inner_paths_list = cpath->custom_paths;
*p_op_leaf = op_leaf;
}
return cpath;
}
/*
* try_add_xpujoin_simple_path
*/
static void
try_add_xpujoin_simple_path(PlannerInfo *root,
RelOptInfo *join_rel,
RelOptInfo *outer_rel,
Path *inner_path,
JoinType join_type,
JoinPathExtraData *extra,
bool try_parallel_path,
uint32_t xpu_task_flags,
const CustomPathMethods *xpujoin_path_methods)
{
pgstromOuterPathLeafInfo *op_prev;
pgstromOuterPathLeafInfo *op_leaf;
Path __outer_path; /* pseudo outer-path */
CustomPath *cpath;
op_prev = pgstrom_find_op_normal(root, outer_rel, try_parallel_path);
if (!op_prev)
return;
memset(&__outer_path, 0, sizeof(Path));
__outer_path.parent = outer_rel;
__outer_path.param_info = op_prev->leaf_param;
__outer_path.rows = op_prev->leaf_nrows;
cpath = __build_simple_xpujoin_path(root,
join_rel,
&__outer_path,
inner_path,
join_type,
op_prev,
&op_leaf,
extra->restrictlist,
extra->sjinfo,
extra->param_source_rels,
try_parallel_path,
-1, /* sibling_param_id */
1.0, /* inner discount ratio */
xpu_task_flags,
xpujoin_path_methods);
if (!cpath)
return;
/* register the XpuJoinPath */
DEBUG_XpuJoinPathPrint(root,
xpujoin_path_methods->CustomName,
&cpath->path,
outer_rel,
inner_path->parent);
pgstrom_remember_op_normal(root,
join_rel,
op_leaf,
try_parallel_path);
if (!try_parallel_path)
add_path(join_rel, &cpath->path);
else
add_partial_path(join_rel, &cpath->path);
}
/*
* __build_child_join_sjinfo
*/
static SpecialJoinInfo *
__build_child_join_sjinfo(PlannerInfo *root,
Relids leaf_join_relids,
SpecialJoinInfo *parent_sjinfo)
{
SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
sjinfo->min_lefthand = fixup_relids_by_partition_leaf(root,
leaf_join_relids,
sjinfo->min_lefthand);
sjinfo->min_righthand = fixup_relids_by_partition_leaf(root,
leaf_join_relids,
sjinfo->min_righthand);
sjinfo->syn_lefthand = fixup_relids_by_partition_leaf(root,
leaf_join_relids,
sjinfo->syn_lefthand);
sjinfo->syn_righthand = fixup_relids_by_partition_leaf(root,
leaf_join_relids,
sjinfo->syn_righthand);
return sjinfo;
}
/*
* __lookup_or_build_leaf_joinrel
*/
static AppendRelInfo *
__build_fake_apinfo_non_relations(PlannerInfo *root, Index rtindex)
{
AppendRelInfo *apinfo = makeNode(AppendRelInfo);
RelOptInfo *rel = root->simple_rel_array[rtindex];
PathTarget *reltarget = rel->reltarget;
List *trans_vars = NIL;
ListCell *lc;
AttrNumber attno = 1;
AttrNumber *colnos
= palloc0(sizeof(AttrNumber) * list_length(reltarget->exprs));
foreach (lc, reltarget->exprs)
{
Node *node = lfirst(lc);
Var *var;
var = makeVar(rtindex,
attno++,
exprType(node),
exprTypmod(node),
exprCollation(node),
0);
trans_vars = lappend(trans_vars, var);
colnos[attno-1] = attno;
}
apinfo->translated_vars = trans_vars;
apinfo->num_child_cols = attno;
apinfo->parent_colnos = colnos;
return apinfo;
}
static AppendRelInfo **
__make_fake_apinfo_array(PlannerInfo *root,
RelOptInfo *parent_joinrel,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel)
{
AppendRelInfo **ap_info_array;
Relids __relids;
int i;
ap_info_array = palloc0(sizeof(AppendRelInfo *) *
root->simple_rel_array_size);
__relids = bms_union(outer_rel->relids,
inner_rel->relids);
for (i = bms_next_member(__relids, -1);
i >= 0;
i = bms_next_member(__relids, i))
{
AppendRelInfo *ap_info = root->append_rel_array[i];
if (ap_info)
{
bool rebuild = false;
while (!bms_is_member(ap_info->parent_relid,
parent_joinrel->relids))
{
Index curr_child = ap_info->parent_relid;
ap_info = NULL;
for (int j=0; j < root->simple_rel_array_size; j++)
{
AppendRelInfo *__ap_info = root->append_rel_array[j];
if (__ap_info &&
__ap_info->child_relid == curr_child)
{
ap_info = root->append_rel_array[j];
break;
}
}
if (!ap_info)
elog(ERROR, "Bug? AppendRelInfo chain is not linked");
rebuild = true;
}
if (rebuild)
{
Index parent_relid = ap_info->parent_relid;
RangeTblEntry *rte_child = root->simple_rte_array[i];
RangeTblEntry *rte_parent = root->simple_rte_array[parent_relid];
Relation rel_child;
Relation rel_parent;
if (rte_child->rtekind != RTE_RELATION ||
rte_parent->rtekind != RTE_RELATION)
elog(ERROR, "Bug? not a relation has partition leaf");
rel_child = relation_open(rte_child->relid, NoLock);
rel_parent = relation_open(rte_parent->relid, NoLock);
ap_info = make_append_rel_info(rel_parent,
rel_child,
parent_relid, i);
relation_close(rel_child, NoLock);
relation_close(rel_parent, NoLock);
}
}
else
{
RangeTblEntry *rte = root->simple_rte_array[i];
if (rte->rtekind != RTE_RELATION)
ap_info = __build_fake_apinfo_non_relations(root, i);
else
{
Relation rel = relation_open(rte->relid, NoLock);
ap_info = make_append_rel_info(rel, rel, i, i);
relation_close(rel, NoLock);
}
}
ap_info_array[i] = ap_info;
}
return ap_info_array;
}
static RelOptInfo *
__lookup_or_build_leaf_joinrel(PlannerInfo *root,
RelOptInfo *parent_joinrel,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel,
List *restrictlist,
SpecialJoinInfo *sjinfo,
JoinType jointype)
{
RelOptInfo *leaf_joinrel;
Relids relids;
relids = bms_union(outer_rel->relids,
inner_rel->relids);
leaf_joinrel = find_join_rel(root, relids);
if (!leaf_joinrel)
{
RelOptKind reloptkind_saved = inner_rel->reloptkind;
bool partitionwise_saved = parent_joinrel->consider_partitionwise_join;
AppendRelInfo **ap_array_saved = root->append_rel_array;
/* a small hack to avoid assert() */
inner_rel->reloptkind = RELOPT_OTHER_MEMBER_REL;
PG_TRY();
{
inner_rel->reloptkind = RELOPT_OTHER_MEMBER_REL;
parent_joinrel->consider_partitionwise_join = true;
root->append_rel_array = __make_fake_apinfo_array(root,
parent_joinrel,
outer_rel,
inner_rel);
leaf_joinrel = build_child_join_rel(root,
outer_rel,
inner_rel,
parent_joinrel,
restrictlist,
sjinfo);
}
PG_CATCH();
{
inner_rel->reloptkind = reloptkind_saved;
parent_joinrel->consider_partitionwise_join = partitionwise_saved;
root->append_rel_array = ap_array_saved;
PG_RE_THROW();
}
PG_END_TRY();
inner_rel->reloptkind = reloptkind_saved;
parent_joinrel->consider_partitionwise_join = partitionwise_saved;
root->append_rel_array = ap_array_saved;
}
return leaf_joinrel;
}
/*
* try_add_xpujoin_partition_path
*/
static void
try_add_xpujoin_partition_path(PlannerInfo *root,
RelOptInfo *join_rel,
RelOptInfo *outer_rel,
Path *inner_path,
JoinType join_type,
JoinPathExtraData *extra,
bool try_parallel_path,
uint32_t xpu_task_flags,
const CustomPathMethods *xpujoin_path_methods)
{
List *op_prev_list = NIL;
List *op_leaf_list = NIL;
List *cpaths_list = NIL;
Path *append_path;
Relids required_outer = NULL;
int parallel_nworkers = 0;
double total_nrows = 0.0;
bool identical_inners;
int sibling_param_id = -1;
double inner_discount_ratio = 1.0;
ListCell *lc;
op_prev_list = pgstrom_find_op_leafs(root,
outer_rel,
try_parallel_path,
&identical_inners);
if (identical_inners)
{
PlannerGlobal *glob = root->glob;
sibling_param_id = list_length(glob->paramExecTypes);
glob->paramExecTypes = lappend_oid(glob->paramExecTypes,
INTERNALOID);
if (list_length(op_prev_list) > 1)
inner_discount_ratio = 1.0 / (double)list_length(op_prev_list);
}
Assert(inner_discount_ratio >= 0.0 && inner_discount_ratio <= 1.0);
foreach (lc, op_prev_list)
{
pgstromOuterPathLeafInfo *op_prev = lfirst(lc);
pgstromOuterPathLeafInfo *op_leaf;
Path __outer_path; //pseudo outer-path
Path *__inner_path = inner_path;
Relids __join_relids;
Relids __required_outer;
List *__restrict_clauses = NIL;
SpecialJoinInfo *__sjinfo;
RelOptInfo *__join_rel;
CustomPath *cpath;
/* setup pseudo outer-path */
memset(&__outer_path, 0, sizeof(Path));
__outer_path.parent = op_prev->leaf_rel;
__outer_path.param_info = op_prev->leaf_param;
__outer_path.rows = op_prev->leaf_nrows;
__required_outer = calc_non_nestloop_required_outer(&__outer_path,
__inner_path);
if (__required_outer &&
!bms_overlap(__required_outer, extra->param_source_rels))
return;
/*
* setup SpecialJoinInfo for inner-leaf join
*/
__join_relids = bms_union(op_prev->leaf_rel->relids,
__inner_path->parent->relids);
__sjinfo = __build_child_join_sjinfo(root,
__join_relids,
extra->sjinfo);
/*
* fixup restrict_clauses
*/
__restrict_clauses =
fixup_expression_by_partition_leaf(root,
op_prev->leaf_rel->relids,
extra->restrictlist);
/*
* lookup or construct a new join_rel
*/
__join_rel = __lookup_or_build_leaf_joinrel(root,
join_rel,
op_prev->leaf_rel,
inner_path->parent,
__restrict_clauses,
__sjinfo,
join_type);
/*
* build XpuJoinPath
*/
cpath = __build_simple_xpujoin_path(root,
__join_rel,
&__outer_path,
inner_path,
join_type,
op_prev,
&op_leaf,
__restrict_clauses,
__sjinfo,
extra->param_source_rels,
try_parallel_path,
sibling_param_id,
inner_discount_ratio,
xpu_task_flags,
xpujoin_path_methods);
if (!cpath)
return;
parallel_nworkers += cpath->path.parallel_workers;
total_nrows += cpath->path.rows;
cpaths_list = lappend(cpaths_list, cpath);
op_leaf_list = lappend(op_leaf_list, op_leaf);
}
if (list_length(cpaths_list) == 0)
return;
if (try_parallel_path)
{
if (parallel_nworkers > max_parallel_workers_per_gather)
parallel_nworkers = max_parallel_workers_per_gather;
if (parallel_nworkers == 0)
return;
}
append_path = (Path *)
create_append_path(root,
join_rel,
(try_parallel_path ? NIL : cpaths_list),
(try_parallel_path ? cpaths_list : NIL),
NIL,
required_outer,
(try_parallel_path ? parallel_nworkers : 0),
try_parallel_path,
total_nrows);
DEBUG_XpuJoinPathPrint(root,
xpujoin_path_methods->CustomName,
append_path,
outer_rel,
inner_path->parent);
pgstrom_remember_op_leafs(root,
join_rel,
op_leaf_list,
try_parallel_path);
if (!try_parallel_path)
add_path(join_rel, append_path);
else
add_partial_path(join_rel, append_path);
}
/*
* __xpuJoinAddCustomPathCommon
*/
static void
__xpuJoinAddCustomPathCommon(PlannerInfo *root,
RelOptInfo *joinrel,
RelOptInfo *outerrel,
RelOptInfo *innerrel,
JoinType join_type,
JoinPathExtraData *extra,
uint32_t xpu_task_flags,
const CustomPathMethods *xpujoin_path_methods,
bool consider_partition)
{
List *inner_pathlist;
ListCell *lc;
/* quick bailout if unsupported join type */
if (join_type != JOIN_INNER &&
join_type != JOIN_FULL &&
join_type != JOIN_RIGHT &&
join_type != JOIN_LEFT)
return;
//TODO: JOIN_SEMI and JOIN_ANTI
inner_pathlist = innerrel->pathlist;
for (int try_parallel=0; try_parallel < 2; try_parallel++)