forked from citusdata/pg_shard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprune_shard_list.c
592 lines (491 loc) · 16.4 KB
/
prune_shard_list.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
/*-------------------------------------------------------------------------
*
* prune_shard_list.c
*
* This file contains functions to examine lists of shards and remove those not
* required to execute a given query. Functions contained here are borrowed from
* CitusDB.
*
* Copyright (c) 2014, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "c.h"
#include "fmgr.h"
#include "pg_config.h"
#include "postgres_ext.h"
#include "distribution_metadata.h"
#include "prune_shard_list.h"
#include <stddef.h>
#include "access/skey.h"
#include "catalog/pg_am.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/nodes.h"
#include "nodes/primnodes.h"
#include "nodes/relation.h"
#include "optimizer/clauses.h"
#include "optimizer/predtest.h"
#include "optimizer/restrictinfo.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/errcodes.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
#include "utils/memutils.h"
/*
* OperatorIdCache is used for caching operator identifiers for given typeId,
* accessMethodId and strategyNumber. It is initialized to empty list as
* there are no items in the cache.
*/
static List *OperatorIdCache = NIL;
/* local function forward declarations */
static Oid LookupOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber);
static bool SimpleOpExpression(Expr *clause);
static Node * HashableClauseMutator(Node *originalNode, Var *partitionColumn);
static bool OpExpressionContainsColumn(OpExpr *operatorExpression, Var *partitionColumn);
static Var * MakeInt4Column(void);
static Const * MakeInt4Constant(Datum constantValue);
static OpExpr * MakeHashedOperatorExpression(OpExpr *operatorExpression);
static OpExpr * MakeOpExpressionWithZeroConst(void);
static List * BuildRestrictInfoList(List *qualList);
static Node * BuildBaseConstraint(Var *column);
static void UpdateConstraint(Node *baseConstraint, ShardInterval *shardInterval);
/*
* PruneShardList prunes shards from given list based on the selection criteria,
* and returns remaining shards in another list.
*/
List *
PruneShardList(Oid relationId, List *whereClauseList, List *shardIntervalList)
{
List *remainingShardList = NIL;
ListCell *shardIntervalCell = NULL;
List *restrictInfoList = NIL;
Node *baseConstraint = NULL;
Var *partitionColumn = PartitionColumn(relationId);
char partitionMethod = PartitionType(relationId);
/* build the filter clause list for the partition method */
if (partitionMethod == DISTRIBUTE_BY_HASH)
{
Node *hashedNode = HashableClauseMutator((Node *) whereClauseList,
partitionColumn);
List *hashedClauseList = (List *) hashedNode;
restrictInfoList = BuildRestrictInfoList(hashedClauseList);
}
else
{
restrictInfoList = BuildRestrictInfoList(whereClauseList);
}
/* override the partition column for hash partitioning */
if (partitionMethod == DISTRIBUTE_BY_HASH)
{
partitionColumn = MakeInt4Column();
}
/* build the base expression for constraint */
baseConstraint = BuildBaseConstraint(partitionColumn);
/* walk over shard list and check if shards can be pruned */
foreach(shardIntervalCell, shardIntervalList)
{
ShardInterval *shardInterval = lfirst(shardIntervalCell);
List *constraintList = NIL;
bool shardPruned = false;
/* set the min/max values in the base constraint */
UpdateConstraint(baseConstraint, shardInterval);
constraintList = list_make1(baseConstraint);
shardPruned = predicate_refuted_by(constraintList, restrictInfoList);
if (shardPruned)
{
ereport(DEBUG2, (errmsg("predicate pruning for shardId "
UINT64_FORMAT, shardInterval->id)));
}
else
{
remainingShardList = lappend(remainingShardList, &(shardInterval->id));
}
}
return remainingShardList;
}
/*
* BuildRestrictInfoList builds restrict info list using the selection criteria,
* and then return this list. Note that this function assumes there is only one
* relation for now.
*/
static List *
BuildRestrictInfoList(List *qualList)
{
List *restrictInfoList = NIL;
ListCell *qualCell = NULL;
foreach(qualCell, qualList)
{
RestrictInfo *restrictInfo = NULL;
Node *qualNode = (Node *) lfirst(qualCell);
restrictInfo = make_simple_restrictinfo((Expr *) qualNode);
restrictInfoList = lappend(restrictInfoList, restrictInfo);
}
return restrictInfoList;
}
/*
* BuildBaseConstraint builds and returns a base constraint. This constraint
* implements an expression in the form of (column <= max && column >= min),
* where column is the partition key, and min and max values represent a shard's
* min and max values. These shard values are filled in after the constraint is
* built.
*/
static Node *
BuildBaseConstraint(Var *column)
{
Node *baseConstraint = NULL;
OpExpr *lessThanExpr = NULL;
OpExpr *greaterThanExpr = NULL;
/* Build these expressions with only one argument for now */
lessThanExpr = MakeOpExpression(column, BTLessEqualStrategyNumber);
greaterThanExpr = MakeOpExpression(column, BTGreaterEqualStrategyNumber);
/* Build base constaint as an and of two qual conditions */
baseConstraint = make_and_qual((Node *) lessThanExpr, (Node *) greaterThanExpr);
return baseConstraint;
}
/* Updates the base constraint with the given min/max values. */
static void
UpdateConstraint(Node *baseConstraint, ShardInterval *shardInterval)
{
BoolExpr *andExpr = (BoolExpr *) baseConstraint;
Node *lessThanExpr = (Node *) linitial(andExpr->args);
Node *greaterThanExpr = (Node *) lsecond(andExpr->args);
Node *minNode = get_rightop((Expr *) greaterThanExpr); /* right op */
Node *maxNode = get_rightop((Expr *) lessThanExpr); /* right op */
Const *minConstant = NULL;
Const *maxConstant = NULL;
Assert(shardInterval != NULL);
Assert(IsA(minNode, Const));
Assert(IsA(maxNode, Const));
minConstant = (Const *) minNode;
maxConstant = (Const *) maxNode;
minConstant->constvalue = shardInterval->minValue;
maxConstant->constvalue = shardInterval->maxValue;
minConstant->constisnull = false;
maxConstant->constisnull = false;
minConstant->constbyval = true;
maxConstant->constbyval = true;
}
/*
* MakeOpExpression builds an operator expression node. This operator expression
* implements the operator clause as defined by the variable and the strategy
* number.
*/
OpExpr *
MakeOpExpression(Var *variable, int16 strategyNumber)
{
Oid typeId = variable->vartype;
Oid typeModId = variable->vartypmod;
Oid collationId = variable->varcollid;
Oid accessMethodId = BTREE_AM_OID;
Oid operatorId = InvalidOid;
Const *constantValue = NULL;
OpExpr *expression = NULL;
/* Load the operator from system catalogs */
operatorId = LookupOperatorByType(typeId, accessMethodId, strategyNumber);
constantValue = makeNullConst(typeId, typeModId, collationId);
/* Now make the expression with the given variable and a null constant */
expression = (OpExpr *) make_opclause(operatorId,
InvalidOid, /* no result type yet */
false, /* no return set */
(Expr *) variable,
(Expr *) constantValue,
InvalidOid, collationId);
/* Set implementing function id and result type */
expression->opfuncid = get_opcode(operatorId);
expression->opresulttype = get_func_rettype(expression->opfuncid);
return expression;
}
/*
* LookupOperatorByType is a wrapper around GetOperatorByType that uses a cache
* to avoid multiple lookups of operators within a single session by their types.
*/
static Oid
LookupOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber)
{
OperatorIdCacheEntry *matchingCacheEntry = NULL;
ListCell *cacheEntryCell = NULL;
/* search the cache */
foreach(cacheEntryCell, OperatorIdCache)
{
OperatorIdCacheEntry *cacheEntry = lfirst(cacheEntryCell);
if ((cacheEntry->typeId == typeId) &&
(cacheEntry->accessMethodId == accessMethodId) &&
(cacheEntry->strategyNumber == strategyNumber))
{
matchingCacheEntry = cacheEntry;
break;
}
}
/* if not found in the cache, call GetOperatorByType and put the result in cache */
if (matchingCacheEntry == NULL)
{
MemoryContext oldContext = NULL;
Oid operatorId = GetOperatorByType(typeId, accessMethodId, strategyNumber);
if (operatorId == InvalidOid)
{
/* if operatorId is invalid, return and do not cache its value */
return operatorId;
}
oldContext = MemoryContextSwitchTo(CacheMemoryContext);
matchingCacheEntry = palloc0(sizeof(OperatorIdCacheEntry));
matchingCacheEntry->typeId = typeId;
matchingCacheEntry->accessMethodId = accessMethodId;
matchingCacheEntry->strategyNumber = strategyNumber;
matchingCacheEntry->operatorId = operatorId;
OperatorIdCache = lappend(OperatorIdCache, matchingCacheEntry);
MemoryContextSwitchTo(oldContext);
}
return matchingCacheEntry->operatorId;
}
/*
* GetOperatorByType returns the operator oid for the given type, access
* method, and strategy number.
*/
Oid
GetOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber)
{
/* Get default operator class from pg_opclass */
Oid operatorClassId = GetDefaultOpClass(typeId, accessMethodId);
Oid operatorFamily = get_opclass_family(operatorClassId);
Oid operatorId = get_opfamily_member(operatorFamily, typeId, typeId, strategyNumber);
return operatorId;
}
/*
* SimpleOpExpression checks that given expression is a simple operator
* expression. A simple operator expression is a binary operator expression with
* operands of a var and a non-null constant.
*/
static bool
SimpleOpExpression(Expr *clause)
{
Node *leftOperand = NULL;
Node *rightOperand = NULL;
Const *constantClause = NULL;
if (is_opclause(clause) && list_length(((OpExpr *) clause)->args) == 2)
{
leftOperand = get_leftop(clause);
rightOperand = get_rightop(clause);
}
else
{
return false; /* not a binary opclause */
}
if (IsA(rightOperand, Const) && IsA(leftOperand, Var))
{
constantClause = (Const *) rightOperand;
}
else if (IsA(leftOperand, Const) && IsA(rightOperand, Var))
{
constantClause = (Const *) leftOperand;
}
else
{
return false;
}
if (constantClause->constisnull)
{
return false;
}
return true;
}
/*
* HashableClauseMutator walks over the original where clause list, replaces
* hashable nodes with hashed versions and keeps other nodes as they are.
*/
static Node *
HashableClauseMutator(Node *originalNode, Var *partitionColumn)
{
Node *newNode = NULL;
if (originalNode == NULL)
{
return NULL;
}
if (IsA(originalNode, OpExpr))
{
OpExpr *operatorExpression = (OpExpr *) originalNode;
bool hasPartitionColumn = false;
Oid leftHashFunction = InvalidOid;
Oid rightHashFunction = InvalidOid;
bool hasHashFunction = get_op_hash_functions(operatorExpression->opno,
&leftHashFunction,
&rightHashFunction);
bool simpleOpExpression = SimpleOpExpression((Expr *) operatorExpression);
if (simpleOpExpression)
{
hasPartitionColumn = OpExpressionContainsColumn(operatorExpression,
partitionColumn);
}
if (hasHashFunction && hasPartitionColumn)
{
OpExpr *hashedOperatorExpression =
MakeHashedOperatorExpression((OpExpr *) originalNode);
newNode = (Node *) hashedOperatorExpression;
}
}
else if (IsA(originalNode, NullTest))
{
NullTest *nullTest = (NullTest *) originalNode;
Var *column = NULL;
Expr *nullTestOperand = nullTest->arg;
if (IsA(nullTestOperand, Var))
{
column = (Var *) nullTestOperand;
}
if ((column != NULL) && equal(column, partitionColumn) &&
(nullTest->nulltesttype == IS_NULL))
{
OpExpr *opExpressionWithZeroConst = MakeOpExpressionWithZeroConst();
newNode = (Node *) opExpressionWithZeroConst;
}
}
else if (IsA(originalNode, ScalarArrayOpExpr))
{
ereport(NOTICE, (errmsg("cannot use shard pruning with ANY (array expression)"),
errhint("Consider rewriting the expression with OR clauses.")));
}
/*
* If this node is not hashable, continue walking down the expression tree
* to find and hash clauses which are eligible.
*/
if(newNode == NULL)
{
newNode = expression_tree_mutator(originalNode, HashableClauseMutator,
(void *) partitionColumn);
}
return newNode;
}
/*
* OpExpressionContainsColumn checks if the operator expression contains the
* given partition column. We assume that given operator expression is a simple
* operator expression which means it is a binary operator expression with
* operands of a var and a non-null constant.
*/
static bool
OpExpressionContainsColumn(OpExpr *operatorExpression, Var *partitionColumn)
{
Node *leftOperand = get_leftop((Expr *) operatorExpression);
Node *rightOperand = get_rightop((Expr *) operatorExpression);
Var *column = NULL;
if (IsA(leftOperand, Var))
{
column = (Var *) leftOperand;
}
else
{
column = (Var *) rightOperand;
}
return equal(column, partitionColumn);
}
/*
* MakeHashedOperatorExpression creates a new operator expression with a column
* of int4 type and hashed constant value.
*/
static OpExpr *
MakeHashedOperatorExpression(OpExpr *operatorExpression)
{
const Oid hashResultTypeId = INT4OID;
TypeCacheEntry *hashResultTypeEntry = NULL;
Oid operatorId = InvalidOid;
OpExpr *hashedExpression = NULL;
Var *hashedColumn = NULL;
Datum hashedValue = 0;
Const *hashedConstant = NULL;
FmgrInfo *hashFunction = NULL;
TypeCacheEntry *typeEntry = NULL;
Node *leftOperand = get_leftop((Expr *) operatorExpression);
Node *rightOperand = get_rightop((Expr *) operatorExpression);
Const *constant = NULL;
if (IsA(rightOperand, Const))
{
constant = (Const *) rightOperand;
}
else
{
constant = (Const *) leftOperand;
}
/* Load the operator from type cache */
hashResultTypeEntry = lookup_type_cache(hashResultTypeId, TYPECACHE_EQ_OPR);
operatorId = hashResultTypeEntry->eq_opr;
/* Get a column with int4 type */
hashedColumn = MakeInt4Column();
/* Load the hash function from type cache */
typeEntry = lookup_type_cache(constant->consttype, TYPECACHE_HASH_PROC_FINFO);
hashFunction = &(typeEntry->hash_proc_finfo);
if (!OidIsValid(hashFunction->fn_oid))
{
ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION),
errmsg("could not identify a hash function for type %s",
format_type_be(constant->consttype))));
}
/*
* Note that any changes to PostgreSQL's hashing functions will change the
* new value created by this function.
*/
hashedValue = FunctionCall1(hashFunction, constant->constvalue);
hashedConstant = MakeInt4Constant(hashedValue);
/* Now create the expression with modified partition column and hashed constant */
hashedExpression = (OpExpr *) make_opclause(operatorId,
InvalidOid, /* no result type yet */
false, /* no return set */
(Expr *) hashedColumn,
(Expr *) hashedConstant,
InvalidOid, InvalidOid);
/* Set implementing function id and result type */
hashedExpression->opfuncid = get_opcode(operatorId);
hashedExpression->opresulttype = get_func_rettype(hashedExpression->opfuncid);
return hashedExpression;
}
/*
* MakeInt4Column creates a column of int4 type with invalid table id and max
* attribute number.
*/
static Var *
MakeInt4Column()
{
Index tableId = 0;
AttrNumber columnAttributeNumber = RESERVED_HASHED_COLUMN_ID;
Oid columnType = INT4OID;
int32 columnTypeMod = -1;
Oid columnCollationOid = InvalidOid;
Index columnLevelSup = 0;
Var *int4Column = makeVar(tableId, columnAttributeNumber, columnType,
columnTypeMod, columnCollationOid, columnLevelSup);
return int4Column;
}
/*
* MakeInt4Constant creates a new constant of int4 type and assigns the given
* value as a constant value.
*/
static Const *
MakeInt4Constant(Datum constantValue)
{
Oid constantType = INT4OID;
int32 constantTypeMode = -1;
Oid constantCollationId = InvalidOid;
int constantLength = sizeof(int32);
bool constantIsNull = false;
bool constantByValue = true;
Const *int4Constant = makeConst(constantType, constantTypeMode, constantCollationId,
constantLength, constantValue, constantIsNull,
constantByValue);
return int4Constant;
}
/*
* MakeOpExpressionWithZeroConst creates a new operator expression with equality
* check to zero and returns it.
*/
static OpExpr *
MakeOpExpressionWithZeroConst()
{
Var *int4Column = MakeInt4Column();
OpExpr *operatorExpression = MakeOpExpression(int4Column, BTEqualStrategyNumber);
Const *constant = (Const *) get_rightop((Expr *) operatorExpression);
constant->constvalue = Int32GetDatum(0);
constant->constisnull = false;
return operatorExpression;
}