-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathplanner.c
More file actions
418 lines (374 loc) · 11.9 KB
/
Copy pathplanner.c
File metadata and controls
418 lines (374 loc) · 11.9 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
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <catalog/pg_namespace.h>
#include <nodes/extensible.h>
#include <nodes/makefuncs.h>
#include <nodes/nodeFuncs.h>
#include <optimizer/appendinfo.h>
#include <optimizer/optimizer.h>
#include <optimizer/pathnode.h>
#include <optimizer/paths.h>
#include <optimizer/placeholder.h>
#include <optimizer/planmain.h>
#include <optimizer/prep.h>
#include <optimizer/subselect.h>
#include <optimizer/tlist.h>
#include <parser/parsetree.h>
#include "guc.h"
#include "import/planner.h"
#include "nodes/chunk_append/chunk_append.h"
#include "nodes/chunk_append/transform.h"
#include "nodes/modify_hypertable.h"
#include "nodes/vector_agg.h"
static Sort *make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators,
Oid *collations, bool *nullsFirst);
static Plan *adjust_childscan(PlannerInfo *root, Plan *plan, Path *path, List *pathkeys,
List *tlist, AttrNumber *sortColIdx);
static CustomScanMethods chunk_append_plan_methods = {
.CustomName = "ChunkAppend",
.CreateCustomScanState = ts_chunk_append_state_create,
};
bool
ts_is_chunk_append_plan(Plan *plan)
{
return IsA(plan, CustomScan) &&
castNode(CustomScan, plan)->methods == &chunk_append_plan_methods;
}
void
_chunk_append_init(void)
{
TryRegisterCustomScanMethods(&chunk_append_plan_methods);
}
static Plan *
adjust_childscan(PlannerInfo *root, Plan *plan, Path *path, List *pathkeys, List *tlist,
AttrNumber *sortColIdx)
{
int childSortCols;
Oid *sortOperators;
Oid *collations;
bool *nullsFirst;
AttrNumber *childColIdx;
/* Compute sort column info, and adjust subplan's tlist as needed */
plan = ts_prepare_sort_from_pathkeys(plan,
pathkeys,
path->parent->relids,
sortColIdx,
true,
&childSortCols,
&childColIdx,
&sortOperators,
&collations,
&nullsFirst);
/* inject sort node if child sort order does not match desired order */
if (!pathkeys_contained_in(pathkeys, path->pathkeys))
{
Assert(!IsA(plan, Sort));
plan = (Plan *)
make_sort(plan, childSortCols, childColIdx, sortOperators, collations, nullsFirst);
}
return plan;
}
Plan *
ts_chunk_append_plan_create(PlannerInfo *root, RelOptInfo *rel, CustomPath *path, List *tlist,
List *clauses, List *custom_plans)
{
ListCell *lc_child;
List *parent_clauses = NIL;
List *chunk_ri_clauses = NIL;
List *chunk_rt_indexes = NIL;
List *sort_options = NIL;
List *custom_private = NIL;
uint32 limit = 0;
List *orig_tlist = NIL;
ChunkAppendPath *capath = (ChunkAppendPath *) path;
CustomScan *cscan = makeNode(CustomScan);
cscan->flags = path->flags;
cscan->methods = &chunk_append_plan_methods;
cscan->scan.scanrelid = rel->relid;
orig_tlist = ts_build_path_tlist(root, (Path *) path);
tlist = orig_tlist;
/*
* If this is a child of ModifyHypertable we need to adjust
* targetlists to not have any ROWID_VAR references as postgres
* asserts that scan targetlists do not have them in setrefs.c
*
* We keep orig_tlist unaltered to let adjust_appendrel_attrs()
* replace ROWID_VARs for chunks' targetlists (it would assert
* trying to modify a "wholerow" target entry that has already
* been adjusted by ts_replace_rowid_vars(); we see these in
* foreign tables).
*/
if (root->parse->commandType != CMD_SELECT)
{
tlist = ts_replace_rowid_vars(root, tlist, rel->relid);
}
cscan->scan.plan.targetlist = tlist;
/*
* For parameterized paths (e.g., LATERAL joins), transform outer-relation
* Vars to NestLoop Params. We store clauses in custom_private rather than
* custom_exprs because chunk_ri_clauses reference chunk relations (after
* adjust_appendrel_attrs), but setrefs.c processing of custom_exprs expects
* Vars to reference the parent relation (scanrelid). Since custom_private
* bypasses setrefs, we must transform outer Vars to Params ourselves.
*/
if (path->path.param_info && root->curOuterRels)
{
List *transformed_clauses = NIL;
ListCell *lc;
foreach (lc, clauses)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
RestrictInfo *newrinfo = makeNode(RestrictInfo);
memcpy(newrinfo, rinfo, sizeof(RestrictInfo));
newrinfo->clause = (Expr *) ts_replace_nestloop_params(root, (Node *) rinfo->clause);
transformed_clauses = lappend(transformed_clauses, newrinfo);
}
clauses = transformed_clauses;
}
ListCell *lc_plan, *lc_path;
forboth (lc_path, path->custom_paths, lc_plan, custom_plans)
{
Plan *child_plan = lfirst(lc_plan);
Path *child_path = lfirst(lc_path);
/* push down targetlist to children */
if (child_path->parent->reloptkind == RELOPT_OTHER_MEMBER_REL)
{
/* if this is an append child we need to adjust targetlist references */
AppendRelInfo *appinfo = ts_get_appendrelinfo(root, child_path->parent->relid, false);
child_plan->targetlist =
castNode(List, adjust_appendrel_attrs(root, (Node *) orig_tlist, 1, &appinfo));
}
else
{
/*
* This can also be a MergeAppend path building the entire
* hypertable, in case we have a single partial chunk.
*/
child_plan->targetlist = tlist;
}
}
if (path->path.pathkeys != NIL)
{
/*
* If this is an ordered append node we need to ensure the columns
* required for sorting are present in the targetlist and all children
* return sorted output. Children not returning sorted output will be
* wrapped in a sort node.
*/
int numCols;
AttrNumber *sortColIdx;
Oid *sortOperators;
Oid *collations;
bool *nullsFirst;
List *pathkeys = path->path.pathkeys;
List *sort_indexes = NIL;
List *sort_ops = NIL;
List *sort_collations = NIL;
List *sort_nulls = NIL;
int i;
/* Compute sort column info, and adjust MergeAppend's tlist as needed */
ts_prepare_sort_from_pathkeys(&cscan->scan.plan,
pathkeys,
path->path.parent->relids,
NULL,
true,
&numCols,
&sortColIdx,
&sortOperators,
&collations,
&nullsFirst);
/*
* collect sort information to make available to explain
*/
for (i = 0; i < numCols; i++)
{
sort_indexes = lappend_oid(sort_indexes, sortColIdx[i]);
sort_ops = lappend_oid(sort_ops, sortOperators[i]);
sort_collations = lappend_oid(sort_collations, collations[i]);
sort_nulls = lappend_oid(sort_nulls, nullsFirst[i]);
}
sort_options = list_make4(sort_indexes, sort_ops, sort_collations, sort_nulls);
forboth (lc_path, path->custom_paths, lc_plan, custom_plans)
{
/*
* If the planner injected a Result node to do projection
* we can safely remove the Result node if it does not have
* a one-time filter because ChunkAppend can do projection.
*/
if (IsA(lfirst(lc_plan), Result) &&
castNode(Result, lfirst(lc_plan))->resconstantqual == NULL)
{
lfirst(lc_plan) = ((Plan *) lfirst(lc_plan))->lefttree;
}
/*
* This could be a MergeAppend due to space partitioning, or
* due to partially compressed chunks. The MergeAppend plan adds
* sort to it children, and has the proper sorting itself, so no
* need to do anything for it.
* We can also have plain chunk scans here which might require a
* Sort.
*/
if (!IsA(lfirst(lc_plan), MergeAppend))
{
lfirst(lc_plan) = adjust_childscan(root,
lfirst(lc_plan),
lfirst(lc_path),
path->path.pathkeys,
orig_tlist,
sortColIdx);
}
}
}
/* decouple input tlist from output tlist in case output tlist gets modified later */
cscan->custom_scan_tlist = list_copy(tlist);
cscan->custom_plans = custom_plans;
/*
* Always pass per-chunk restrictinfo clauses and RT indexes into the
* executor so that the three per-chunk lists (subplans, ri_clauses,
* constraints) are uniformly populated. This is required for parallel
* workers that index into these lists, and keeps startup/runtime
* exclusion from having to cope with missing data.
*/
{
foreach (lc_child, cscan->custom_plans)
{
Scan *scan = ts_chunk_append_get_scan_plan(lfirst(lc_child));
if (scan == NULL || scan->scanrelid == 0)
{
chunk_ri_clauses = lappend(chunk_ri_clauses, NIL);
chunk_rt_indexes = lappend_oid(chunk_rt_indexes, 0);
}
else
{
List *chunk_clauses = NIL;
ListCell *lc;
AppendRelInfo *appinfo = ts_get_appendrelinfo(root, scan->scanrelid, false);
foreach (lc, clauses)
{
Node *clause = (Node *) ts_transform_cross_datatype_comparison(
castNode(RestrictInfo, lfirst(lc))->clause);
clause = adjust_appendrel_attrs(root, clause, 1, &appinfo);
chunk_clauses = lappend(chunk_clauses, clause);
}
chunk_ri_clauses = lappend(chunk_ri_clauses, chunk_clauses);
chunk_rt_indexes = lappend_oid(chunk_rt_indexes, scan->scanrelid);
}
}
Assert(list_length(cscan->custom_plans) == list_length(chunk_ri_clauses));
Assert(list_length(chunk_ri_clauses) == list_length(chunk_rt_indexes));
}
/* pass down the parent clauses if doing parent exclusion */
if (capath->runtime_exclusion_parent)
{
ListCell *lc;
foreach (lc, clauses)
{
parent_clauses = lappend(parent_clauses, castNode(RestrictInfo, lfirst(lc))->clause);
}
}
if (capath->pushdown_limit && capath->limit_tuples > 0)
{
limit = capath->limit_tuples;
}
custom_private = list_make1(list_make5_int(capath->startup_exclusion,
capath->runtime_exclusion_parent,
capath->runtime_exclusion_children,
limit,
capath->first_partial_path));
custom_private = lappend(custom_private, chunk_ri_clauses);
custom_private = lappend(custom_private, chunk_rt_indexes);
custom_private = lappend(custom_private, sort_options);
custom_private = lappend(custom_private, parent_clauses);
cscan->custom_private = custom_private;
return &cscan->scan.plan;
}
/*
* make_sort --- basic routine to build a Sort plan node
*
* Caller must have built the sortColIdx, sortOperators, collations, and
* nullsFirst arrays already.
*/
static Sort *
make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations,
bool *nullsFirst)
{
Sort *node = makeNode(Sort);
Plan *plan = &node->plan;
plan->targetlist = lefttree->targetlist;
plan->qual = NIL;
plan->lefttree = lefttree;
plan->righttree = NULL;
node->numCols = numCols;
node->sortColIdx = sortColIdx;
node->sortOperators = sortOperators;
node->collations = collations;
node->nullsFirst = nullsFirst;
return node;
}
Scan *
ts_chunk_append_get_scan_plan(Plan *plan)
{
if (plan == NULL)
{
return NULL;
}
switch (nodeTag(plan))
{
case T_BitmapHeapScan:
case T_BitmapIndexScan:
case T_CteScan:
case T_ForeignScan:
case T_FunctionScan:
case T_IndexOnlyScan:
case T_IndexScan:
case T_SampleScan:
case T_SeqScan:
case T_SubqueryScan:
case T_TidScan:
case T_ValuesScan:
case T_WorkTableScan:
case T_TidRangeScan:
return (Scan *) plan;
case T_CustomScan:
{
CustomScan *custom = castNode(CustomScan, plan);
if (custom->scan.scanrelid > 0)
{
/*
* The custom plan node is a scan itself. This handles the
* ColumnarScan node.
*/
return (Scan *) plan;
}
if (strcmp(custom->methods->CustomName, VECTOR_AGG_NODE_NAME) == 0)
{
/*
* This is a vectorized aggregation node, we have to recurse
* into its child, similar to the normal aggregation node.
*
* Unfortunately we have to hardcode the node name here, because
* we can't depend on the TSL library.
*/
return ts_chunk_append_get_scan_plan(linitial(custom->custom_plans));
}
break;
}
case T_Sort:
case T_Result:
case T_Agg:
if (plan->lefttree != NULL)
{
Assert(plan->righttree == NULL);
/* Let ts_chunk_append_get_scan_plan handle the subplan */
return ts_chunk_append_get_scan_plan(plan->lefttree);
}
break;
default:
break;
}
return NULL;
}