-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.c
567 lines (501 loc) · 15.7 KB
/
condition.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
/*
* Copyright 2023 Scalar, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include "c.h"
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "nodes/primnodes.h"
#include "nodes/pg_list.h"
#include "nodes/nodes.h"
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/planmain.h"
#include "utils/syscache.h"
#include "condition.h"
#include "column_metadata.h"
#define UnsupportedConitionType -1
/*
* Type of the condition target column
*/
typedef enum {
SCALARDB_PARTITION_KEY,
SCALARDB_CLUSTERING_KEY,
SCALARDB_SECONDARY_INDEX,
} ScalarDbFdwConditionKeyType;
typedef enum {
SCALARDB_OP_EQ,
SCALARDB_OP_LE,
SCALARDB_OP_LT,
SCALARDB_OP_GE,
SCALARDB_OP_GT,
} ScalarDbFdwOperator;
/*
* Represents a shippable condition in the planner phase.
*/
typedef struct {
/* type of condition. See the above definition. */
ScalarDbFdwConditionKeyType key;
/* key index in the partition key, clustering key, or secondary index */
int key_index;
/* type of operator. */
ScalarDbFdwOperator op;
/* Target column of the condition */
Var *column;
/* Name of column*/
#if PG_VERSION_NUM >= 150000
String *name;
#else
Value *name;
#endif
/* Expression that is compared with the column.
* This expression will be evaluated in the executor phase.
* This must be a pseudo constant */
Expr *expr;
} ScalarDbFdwShippableCondition;
static void determine_clustering_key_boundary(
List *clustering_key_conds, List *clustering_key_shippable_conds,
int num_clustering_keys, ScalarDbFdwClusteringKeyBoundary *boundary);
static ScalarDbFdwShippableCondition *
is_shippable_condition(RelOptInfo *baserel,
ScalarDbFdwColumnMetadata *column_metadata, Expr *expr);
static ScalarDbFdwOperator get_operator_type(OpExpr *op);
static Const *make_boolean_const(bool val);
/*
* Determine which conditions can be pushed down to the foreign server (referred to as shippable).
*
* remote_conds must contain one of the partition key or secondary index conditions
* These conditions must be equal operations.
*
* The number of partition key conditions must be equal to the number of partition keys to be appended to remote_conds.
* In other words, all partition keys must be specified in WHERE clause[].
*
* If remote_conds contain the partition key conditions, remote_conds may contain zero or more the clustring key conditions.
*
* If input_conds contains multiple secondary index condtiions, only the first condition found is appended to remote_conds.
*/
extern void determine_remote_conds(RelOptInfo *baserel, List *input_conds,
ScalarDbFdwColumnMetadata *column_metadata,
List **remote_conds, List **local_conds,
ScalarDbFdwClusteringKeyBoundary *boundary,
ScalarDbFdwScanType *scan_type)
{
ListCell *lc;
ScalarDbFdwShippableCondition *shippable_condition;
List *partition_key_conds = NIL;
List *clustering_key_conds = NIL;
List *clustering_key_shippable_conds = NIL;
RestrictInfo *secondary_index_cond = NULL;
ereport(DEBUG3, errmsg("entering function %s", __func__));
foreach(lc, input_conds) {
RestrictInfo *ri = lfirst_node(RestrictInfo, lc);
shippable_condition = is_shippable_condition(
baserel, column_metadata, ri->clause);
if (shippable_condition != NULL) {
switch (shippable_condition->key) {
case SCALARDB_PARTITION_KEY: {
partition_key_conds =
lappend(partition_key_conds, ri);
pfree(shippable_condition);
break;
}
case SCALARDB_CLUSTERING_KEY: {
clustering_key_conds =
lappend(clustering_key_conds, ri);
/* Not that the type of the element is ScalarDbFdwShippableCondition here.
* This type is not a serilizable Node of List, but it would not be problematic
* because this is used only with in this function */
clustering_key_shippable_conds =
lappend(clustering_key_shippable_conds,
shippable_condition);
break;
}
case SCALARDB_SECONDARY_INDEX: {
/* Use only the first condition on the secondary index */
if (secondary_index_cond == NULL) {
secondary_index_cond = ri;
}
pfree(shippable_condition);
break;
}
}
}
}
if (list_length(partition_key_conds) ==
list_length(column_metadata->partition_key_attnums)) {
*scan_type = SCALARDB_SCAN_PARTITION_KEY;
*remote_conds = partition_key_conds;
*local_conds = list_difference(input_conds, *remote_conds);
determine_clustering_key_boundary(
clustering_key_conds, clustering_key_shippable_conds,
list_length(column_metadata->clustering_key_attnums),
boundary);
if (list_length(boundary->conds) > 0) {
*local_conds =
list_difference(*local_conds, boundary->conds);
}
} else if (secondary_index_cond != NULL) {
*scan_type = SCALARDB_SCAN_SECONDARY_INDEX;
*remote_conds = list_make1(secondary_index_cond);
*local_conds = list_difference(input_conds, *remote_conds);
} else {
*scan_type = SCALARDB_SCAN_ALL;
*remote_conds = NIL;
*local_conds = input_conds;
}
}
/*
* Split the given condition expression the left Var that indicates the condition variable
* and the right Expr that indicates the condition value.
*
* It is caller's responsibility to ensure that the given condition expression is shippable
* by calling determine_remote_conds.
*/
extern void split_condition_expr(RelOptInfo *baserel,
ScalarDbFdwColumnMetadata *column_metadata,
Expr *expr, Var **left, String **left_name,
Expr **right)
{
ScalarDbFdwShippableCondition *cond;
cond = is_shippable_condition(baserel, column_metadata, expr);
if (cond == NULL)
ereport(ERROR, errmsg("condition is not shippable"));
*left = cond->column;
*left_name = cond->name;
*right = cond->expr;
}
/*
* Return true if the given expr is Var belonging to the given baserel.
*/
extern bool is_foreign_table_var(Expr *expr, RelOptInfo *baserel)
{
Var *var;
if (!IsA(expr, Var))
return false;
var = (Var *)expr;
return bms_is_member(var->varno, baserel->relids) &&
var->varlevelsup == 0;
}
static void determine_clustering_key_boundary(
List *clustering_key_conds, List *clustering_key_shippable_conds,
int num_clustering_keys, ScalarDbFdwClusteringKeyBoundary *boundary)
{
ListCell *lc, *lc2;
bool start_boundary_is_set = false;
bool end_boundary_is_set = false;
/* Check for keys in order to determine the boundary
* until the first non-EQ condition (i.e., range condition) is found */
for (int i = 0; i < num_clustering_keys; i++) {
forboth(lc, clustering_key_conds, lc2,
clustering_key_shippable_conds)
{
RestrictInfo *ri = lfirst(lc);
ScalarDbFdwShippableCondition *cond = lfirst(lc2);
if (cond->key_index == i) {
switch (cond->op) {
case SCALARDB_OP_EQ:
/* Skip if range condition was found eariler */
if (start_boundary_is_set ||
end_boundary_is_set)
continue;
boundary->names = lappend(
boundary->names, cond->name);
boundary->start_exprs =
lappend(boundary->start_exprs,
cond->expr);
boundary->end_exprs =
lappend(boundary->end_exprs,
cond->expr);
boundary->conds =
lappend(boundary->conds, ri);
boundary->is_equals =
lappend(boundary->is_equals,
makeBoolean(true));
boundary->start_inclusive = true;
boundary->end_inclusive = true;
/* Check the next clustering key */
goto NEXT_CLUSTERING_KEY;
case SCALARDB_OP_LE:
case SCALARDB_OP_LT:
/* Skip if end boundary is already set*/
if (end_boundary_is_set)
continue;
boundary->end_exprs =
lappend(boundary->end_exprs,
cond->expr);
boundary->end_inclusive =
cond->op == SCALARDB_OP_LE;
boundary->conds =
lappend(boundary->conds, ri);
if (start_boundary_is_set) {
/* Stop the boundary check because
* both start and end for the current clustering key is set */
goto FINISH;
} else {
boundary->names =
lappend(boundary->names,
cond->name);
boundary->is_equals = lappend(
boundary->is_equals,
makeBoolean(false));
end_boundary_is_set = true;
/* Try to find start boundary condition */
continue;
}
case SCALARDB_OP_GE:
case SCALARDB_OP_GT:
/* Skip if start boundary is already set*/
if (start_boundary_is_set)
continue;
boundary->start_exprs =
lappend(boundary->start_exprs,
cond->expr);
boundary->start_inclusive =
cond->op == SCALARDB_OP_GE;
boundary->conds =
lappend(boundary->conds, ri);
if (end_boundary_is_set) {
/* Stop the boundary check because
* both start and end for the current clustering key is set */
goto FINISH;
} else {
boundary->names =
lappend(boundary->names,
cond->name);
boundary->is_equals = lappend(
boundary->is_equals,
makeBoolean(false));
start_boundary_is_set = true;
/* Try to find end boundary condition */
continue;
}
}
}
}
/* In the case that one of the start or end boudary is detected for the key;
* - if it is the first key in the list, use it as an one-side boundary
* - if the list already has other keys, use only them
* (i.e., use only conditions of EQ operator) */
if (start_boundary_is_set && !end_boundary_is_set) {
if (list_length(boundary->start_exprs) > 1) {
boundary->names =
list_delete_last(boundary->names);
boundary->is_equals =
list_delete_last(boundary->is_equals);
boundary->conds =
list_delete_last(boundary->conds);
boundary->start_exprs =
list_delete_last(boundary->start_exprs);
boundary->start_inclusive = true;
}
}
if (end_boundary_is_set && !start_boundary_is_set) {
if (list_length(boundary->end_exprs) > 1) {
boundary->names =
list_delete_last(boundary->names);
boundary->is_equals =
list_delete_last(boundary->is_equals);
boundary->conds =
list_delete_last(boundary->conds);
boundary->end_exprs =
list_delete_last(boundary->end_exprs);
boundary->end_inclusive = true;
}
}
/* Stop the boundary check if no condition is found for the current clustering key */
goto FINISH;
NEXT_CLUSTERING_KEY:
continue;
}
FINISH:
return;
}
static ScalarDbFdwShippableCondition *
check_keys_for_var(Var *var, List *key_attnums, List *key_names,
ScalarDbFdwConditionKeyType key_type, ScalarDbFdwOperator op,
Expr *expr)
{
ListCell *lc;
int i = 0;
ScalarDbFdwShippableCondition *cond;
foreach(lc, key_attnums) {
int attnum = lfirst_int(lc);
if (attnum == var->varattno) {
cond = palloc0(sizeof(ScalarDbFdwShippableCondition));
cond->key = key_type;
cond->key_index = i;
cond->op = op;
cond->column = var;
cond->name = list_nth(key_names, i);
cond->expr = expr;
return cond;
}
i++;
}
return NULL;
}
static ScalarDbFdwShippableCondition *
is_shippable_condition(RelOptInfo *baserel,
ScalarDbFdwColumnMetadata *column_metadata, Expr *expr)
{
ScalarDbFdwShippableCondition *cond;
switch (nodeTag(expr)) {
case T_Var: {
Var *var = (Var *)expr;
if (var->vartype != BOOLOID)
return NULL;
if (!is_foreign_table_var((Expr *)var, baserel))
return NULL;
cond = check_keys_for_var(
var, column_metadata->partition_key_attnums,
column_metadata->partition_key_names,
SCALARDB_PARTITION_KEY, SCALARDB_OP_EQ,
(Expr *)make_boolean_const(true));
if (cond != NULL)
return cond;
cond = check_keys_for_var(
var, column_metadata->clustering_key_attnums,
column_metadata->clustering_key_names,
SCALARDB_CLUSTERING_KEY, SCALARDB_OP_EQ,
(Expr *)make_boolean_const(true));
if (cond != NULL)
return cond;
cond = check_keys_for_var(
var, column_metadata->secondary_index_attnums,
column_metadata->secondary_index_names,
SCALARDB_SECONDARY_INDEX, SCALARDB_OP_EQ,
(Expr *)make_boolean_const(true));
if (cond != NULL)
return cond;
return NULL;
}
case T_BoolExpr: {
BoolExpr *bool_expr = (BoolExpr *)expr;
Expr *expr;
Var *var;
/* Consider only NOT operator */
if (bool_expr->boolop != NOT_EXPR)
return NULL;
expr = linitial_node(Expr, bool_expr->args);
if (!is_foreign_table_var(expr, baserel))
return NULL;
var = (Var *)expr;
cond = check_keys_for_var(
var, column_metadata->partition_key_attnums,
column_metadata->partition_key_names,
SCALARDB_PARTITION_KEY, SCALARDB_OP_EQ,
(Expr *)make_boolean_const(false));
if (cond != NULL)
return cond;
cond = check_keys_for_var(
var, column_metadata->clustering_key_attnums,
column_metadata->clustering_key_names,
SCALARDB_CLUSTERING_KEY, SCALARDB_OP_EQ,
(Expr *)make_boolean_const(false));
if (cond != NULL)
return cond;
cond = check_keys_for_var(
var, column_metadata->secondary_index_attnums,
column_metadata->secondary_index_names,
SCALARDB_SECONDARY_INDEX, SCALARDB_OP_EQ,
(Expr *)make_boolean_const(false));
if (cond != NULL)
return cond;
return NULL;
}
case T_OpExpr: {
Expr *left_expr;
Var *left;
Node *right;
OpExpr *op = (OpExpr *)expr;
ScalarDbFdwOperator op_type = get_operator_type(op);
if (op_type == UnsupportedConitionType)
return NULL;
if (list_length(op->args) != 2)
return NULL;
left_expr = linitial_node(Expr, op->args);
right = lsecond(op->args);
if (!is_foreign_table_var(left_expr, baserel))
return NULL;
left = (Var *)left_expr;
if (!is_pseudo_constant_clause(right))
return NULL;
if (op_type == SCALARDB_OP_EQ) {
cond = check_keys_for_var(
left, column_metadata->partition_key_attnums,
column_metadata->partition_key_names,
SCALARDB_PARTITION_KEY, SCALARDB_OP_EQ,
(Expr *)right);
if (cond != NULL)
return cond;
}
if (op_type == SCALARDB_OP_EQ || op_type == SCALARDB_OP_LE ||
op_type == SCALARDB_OP_LT || op_type == SCALARDB_OP_GE ||
op_type == SCALARDB_OP_GT) {
cond = check_keys_for_var(
left, column_metadata->clustering_key_attnums,
column_metadata->clustering_key_names,
SCALARDB_CLUSTERING_KEY, op_type,
(Expr *)right);
if (cond != NULL)
return cond;
}
if (op_type == SCALARDB_OP_EQ) {
cond = check_keys_for_var(
left, column_metadata->secondary_index_attnums,
column_metadata->secondary_index_names,
SCALARDB_SECONDARY_INDEX, SCALARDB_OP_EQ,
(Expr *)right);
if (cond != NULL)
return cond;
}
return NULL;
}
default:
return NULL;
}
}
static ScalarDbFdwOperator get_operator_type(OpExpr *op)
{
HeapTuple tuple;
Form_pg_operator form;
ScalarDbFdwOperator ret = UnsupportedConitionType;
if (list_length(op->args) != 2)
return ret;
/* Retrieve information about the operator from system catalog. */
tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(op->opno));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for operator %u", op->opno);
form = (Form_pg_operator)GETSTRUCT(tuple);
if (strcmp(NameStr(form->oprname), "=") == 0)
ret = SCALARDB_OP_EQ;
else if (strcmp(NameStr(form->oprname), "<=") == 0)
ret = SCALARDB_OP_LE;
else if (strcmp(NameStr(form->oprname), "<") == 0)
ret = SCALARDB_OP_LT;
else if (strcmp(NameStr(form->oprname), ">=") == 0)
ret = SCALARDB_OP_GE;
else if (strcmp(NameStr(form->oprname), ">") == 0)
ret = SCALARDB_OP_GT;
ReleaseSysCache(tuple);
return ret;
}
static Const *make_boolean_const(bool val)
{
return makeConst(BOOLOID, -1, InvalidOid, 1, BoolGetDatum(val), false,
true);
}