From 28a377179d060067e48d4b57768a339c6cb10185 Mon Sep 17 00:00:00 2001 From: amit Date: Tue, 22 Aug 2017 13:48:13 +0900 Subject: [PATCH 6/8] Implement get_partitions_from_clauses This now actually processes partclauses and classifies them into a set of keys that can be used to look up partitions in the partition descriptor, although there is still no support for the latter. --- src/backend/catalog/partition.c | 1205 ++++++++++++++++++++++++++++++- src/backend/optimizer/util/clauses.c | 4 +- src/include/optimizer/clauses.h | 2 + src/test/regress/expected/partition.out | 15 +- 4 files changed, 1209 insertions(+), 17 deletions(-) diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index a8ddd4fab2..8a7d305357 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -28,6 +28,8 @@ #include "catalog/pg_inherits.h" #include "catalog/pg_inherits_fn.h" #include "catalog/pg_opclass.h" +#include "catalog/pg_operator.h" +#include "catalog/pg_opfamily.h" #include "catalog/pg_partitioned_table.h" #include "catalog/pg_type.h" #include "commands/tablecmds.h" @@ -38,6 +40,8 @@ #include "nodes/parsenodes.h" #include "optimizer/clauses.h" #include "optimizer/planmain.h" +#include "optimizer/planner.h" +#include "optimizer/predtest.h" #include "optimizer/prep.h" #include "optimizer/var.h" #include "parser/parse_coerce.h" @@ -131,6 +135,69 @@ typedef struct PartitionRangeBound bool lower; /* this is the lower (vs upper) bound */ } PartitionRangeBound; +/* + * Information about a clause matched with a partition key column kept to + * avoid recomputing the same in remove_redundant_clauses(). + */ +typedef struct +{ + OpExpr *op; + Expr *constarg; + + /* cached info. */ + bool valid_cache; /* Is the following information initialized? */ + int op_strategy; + Oid op_subtype; + FmgrInfo op_func; +} PartClause; + +/* + * PartScanKeyInfo + * Bounding scan keys to look up a table's partitions obtained from + * mutually-ANDed clauses containing partitioning-compatible operators + */ +typedef struct PartScanKeyInfo +{ + /* + * Constants constituting the *whole* partition key compared using + * partitioning-compatible equality operator(s). When n_eqkeys > 0, other + * keys (minkeys and maxkeys) are irrelevant. + * + * Equal keys are not required to be in any particular order, unlike the + * keys below which must appear in the same order as partition keys. + */ + Datum eqkeys[PARTITION_MAX_KEYS]; + int n_eqkeys; + + /* + * Constants that constitute the lower bound on the partition key or a + * prefix thereof. The last of those constants is compared using > or >= + * operator compatible with partitioning, making this the lower bound in + * a range query. + */ + Datum minkeys[PARTITION_MAX_KEYS]; + int n_minkeys; + bool min_incl; + + /* + * Constants that constitute the upper bound on the partition key or a + * prefix thereof. The last of those constants is compared using < or <= + * operator compatible with partitioning, making this the upper bound in + * a range query. + */ + Datum maxkeys[PARTITION_MAX_KEYS]; + int n_maxkeys; + bool max_incl; + + /* + * Does the query specify a key to be null or not null? Partitioning + * handles null partition keys specially depending on the partitioning + * method in use, we store this information. + */ + bool keyisnull[PARTITION_MAX_KEYS]; + bool keyisnotnull[PARTITION_MAX_KEYS]; +} PartScanKeyInfo; + static int32 qsort_partition_hbound_cmp(const void *a, const void *b); static int32 qsort_partition_list_value_cmp(const void *a, const void *b, void *arg); @@ -178,6 +245,25 @@ static uint64 compute_hash_value(PartitionKey key, Datum *values, bool *isnull); /* SQL-callable function for use in hash partition CHECK constraints */ PG_FUNCTION_INFO_V1(satisfies_hash_partition); +static Bitmapset *get_partitions_from_clauses_recurse(Relation relation, + int rt_index, List *clauses); +static int classify_partition_bounding_keys(Relation relation, List *clauses, + int rt_index, + PartScanKeyInfo *keys, bool *constfalse, + List **or_clauses); +static void remove_redundant_clauses(PartitionKey partkey, + int partattoff, List *all_clauses, + List **result, bool *constfalse); +static bool partition_cmp_args(PartitionKey key, int partattoff, + PartClause *op, PartClause *leftarg, PartClause *rightarg, + bool *result); +static int32 partition_op_strategy(PartitionKey key, PartClause *op, + bool *incl); +static bool partkey_datum_from_expr(PartitionKey key, int partattoff, + Expr *expr, Datum *value); +static Bitmapset *get_partitions_for_keys(Relation rel, + PartScanKeyInfo *keys); + /* * RelationBuildPartitionDesc * Form rel's partition descriptor @@ -1530,7 +1616,7 @@ get_partition_qual_relid(Oid relid) } /* - * get_partitions_using_clauses + * get_partitions_from_clauses * Determine the set of partitions of relation that will satisfy all * the clauses contained in partclauses * @@ -1541,17 +1627,1128 @@ Bitmapset * get_partitions_from_clauses(Relation relation, int rt_index, List *partclauses) { - PartitionDesc partdesc = RelationGetPartitionDesc(relation); - Bitmapset *result = NULL; + Bitmapset *result; + List *partconstr = RelationGetPartitionQual(relation); Assert(partclauses != NIL); - result = bms_add_range(result, 0, partdesc->nparts - 1); + + /* + * If relation is a partition itself, add its partition constraint + * clauses to the list of clauses to use for partition pruning. This + * is done to facilitate correct decision regarding the default + * partition. Adding the partition constraint clauses to the list helps + * restrict the possible key space to only that allowed by the partition + * and thus avoids the default partition being inadvertently added to the + * set of selected partitions for a query whose clauses select a key space + * bigger than the partition's. + */ + if (partconstr) + { + PartitionBoundInfo boundinfo = + RelationGetPartitionDesc(relation)->boundinfo; + + /* + * We need to worry about such a case only if the relation has a + * default partition to begin with. + */ + if (partition_bound_has_default(boundinfo)) + { + partconstr = (List *) expression_planner((Expr *) partconstr); + partclauses = list_concat(partclauses, partconstr); + } + } + + result = get_partitions_from_clauses_recurse(relation, rt_index, + partclauses); + return result; } /* Module-local functions */ /* + * get_partitions_from_clauses_guts + * Determine relation's partitions that satisfy *all* of the clauses + * in the list + * + * Return value is a Bitmapset containing the indexes of selected partitions. + */ +static Bitmapset * +get_partitions_from_clauses_recurse(Relation relation, int rt_index, + List *clauses) +{ + PartitionDesc partdesc = RelationGetPartitionDesc(relation); + Bitmapset *result = NULL; + PartScanKeyInfo keys; + int nkeys; + bool constfalse; + List *or_clauses; + ListCell *lc; + + /* + * Reduce the set of clauses into a form that get_partitions_for_keys() + * can work with. + */ + nkeys = classify_partition_bounding_keys(relation, clauses, rt_index, + &keys, &constfalse, + &or_clauses); + + /* + * The analysis of the matched clauses done by + * classify_partition_bounding_keys may have found mutually contradictory + * clauses. + */ + if (!constfalse) + { + /* + * If all clauses in the list were OR clauses, + * classify_partition_bounding_keys() wouldn't have formed keys + * yet. They will be handled below by recursively calling this + * function for each of OR clauses' arguments and combining the + * resulting partition sets appropriately. + */ + if (nkeys > 0) + result = get_partitions_for_keys(relation, &keys); + else + result = bms_add_range(result, 0, partdesc->nparts - 1); + } + else + return NULL; + + /* No point in trying to look at other conjunctive clauses. */ + if (bms_is_empty(result)) + return NULL; + + foreach(lc, or_clauses) + { + BoolExpr *or = (BoolExpr *) lfirst(lc); + ListCell *lc1; + Bitmapset *or_partset = NULL; + + foreach(lc1, or->args) + { + List *arg_clauses = list_make1(lfirst(lc1)); + List *partconstr = RelationGetPartitionQual(relation); + Bitmapset *arg_partset; + + /* + * It's possible that this clause is never true for this relation + * due to the latter's partition constraint, which means we must + * not add its partitions to or_partset. But the clause may not + * contain this relation's partition key expressions (instead the + * parent's), so we could not depend on just calling + * get_partitions_from_clauses_recurse(relation, ...) to determine + * that the clause indeed prunes all of the relation's partition. + * + * Use predicate refutation proof instead. + */ + if (partconstr) + { + partconstr = (List *) expression_planner((Expr *) partconstr); + if (rt_index != 1) + ChangeVarNodes((Node *) partconstr, 1, rt_index, 0); + if (predicate_refuted_by(partconstr, arg_clauses, false)) + continue; + } + + arg_partset = get_partitions_from_clauses_recurse(relation, + rt_index, + arg_clauses); + + /* + * Partition sets obtained from mutually-disjunctive clauses are + * combined using set union. + */ + or_partset = bms_union(or_partset, arg_partset); + } + + /* + * Partition sets obtained from mutually-conjunctive clauses are + * combined using set intersection. + */ + result = bms_intersect(result, or_partset); + } + + return result; +} + +#define EXPR_MATCHES_PARTKEY(expr, partattno, partexpr) \ + ((IsA((expr), Var) &&\ + ((Var *) (expr))->varattno == (partattno)) ||\ + equal((expr), (partexpr))) + +/* + * classify_partition_bounding_keys + * Classify partition clauses into equal, min, and max keys, along with + * any Nullness constraints and return that information in the output + * argument keys (number of keys is the return value) + * + * Clauses in the provided list are implicitly ANDed, each of which is known + * to match some partition key column. Map them to individual key columns + * and for each column, determine the equal bound or "best" min and max + * bounds. For example, of a > 1, a > 2, and a >= 5, "5" is the best min + * bound for the column a, which also happens to be an inclusive bound. + * When analyzing multiple clauses referencing the same key, it is checked + * if there are mutually contradictory clauses and if so, we set *constfalse + * to true to indicate to the caller that the set of clauses cannot be true + * for any partition. It is also set if the list already contains a + * pseudo-constant clause. + * + * For multi-column keys, an equal bound is returned only if all the columns + * are constrained by clauses containing equality operator, unless hash + * partitioning is in use, in which case, it's possible that some keys have + * IS NULL clauses while remaining have clauses with equality operator. + * Min and max bounds could contain bound values for only a prefix of keys. + * + * All the OR clauses encountered in the list and those generated from certain + * ScalarArrayOpExprs are added to *or_clauses. It's the responsibility of the + * caller to process the argument clauses of each of the OR clauses, which + * would involve recursively calling this function. + */ +static int +classify_partition_bounding_keys(Relation relation, List *clauses, + int rt_index, + PartScanKeyInfo *keys, bool *constfalse, + List **or_clauses) +{ + PartitionKey partkey = RelationGetPartitionKey(relation); + int i; + ListCell *lc; + List *keyclauses_all[PARTITION_MAX_KEYS], + *keyclauses[PARTITION_MAX_KEYS]; + bool only_or_clauses = true; + bool keyisnull[PARTITION_MAX_KEYS]; + bool keyisnotnull[PARTITION_MAX_KEYS]; + bool need_next_eq, + need_next_min, + need_next_max; + int n_keynullness = 0; + + *or_clauses = NIL; + *constfalse = false; + memset(keyclauses_all, 0, sizeof(keyclauses_all)); + /* false means we don't know if a given key is null */ + memset(keyisnull, false, sizeof(keyisnull)); + /* false means we don't know if a given key is not null */ + memset(keyisnotnull, false, sizeof(keyisnull)); + + foreach(lc, clauses) + { + Expr *clause; + ListCell *partexprs_item; + + if (IsA(lfirst(lc), RestrictInfo)) + { + RestrictInfo *rinfo = lfirst(lc); + + clause = rinfo->clause; + if (rinfo->pseudoconstant && + !DatumGetBool(((Const *) clause)->constvalue)) + { + *constfalse = true; + continue; + } + } + else + clause = (Expr *) lfirst(lc); + + /* Get the BoolExpr's out of the way.*/ + if (IsA(clause, BoolExpr)) + { + if (or_clause((Node *) clause)) + { + *or_clauses = lappend(*or_clauses, clause); + continue; + } + else if (and_clause((Node *) clause)) + { + clauses = list_concat(clauses, + list_copy(((BoolExpr *) clause)->args)); + continue; + } + /* Fall-through for a NOT clause, which is handled below. */ + } + + partexprs_item = list_head(partkey->partexprs); + for (i = 0; i < partkey->partnatts; i++) + { + Oid partopfamily = partkey->partopfamily[i], + partcoll = partkey->partcollation[i]; + AttrNumber partattno = partkey->partattrs[i]; + Expr *partexpr = NULL; + PartClause *pc; + + /* Set partexpr if needed. */ + if (partattno == 0) + { + if (partexprs_item == NULL) + elog(ERROR, "wrong number of partition key expressions"); + partexpr = copyObject(lfirst(partexprs_item)); + if (rt_index != 1) + ChangeVarNodes((Node *) partexpr, 1, rt_index, 0); + partexprs_item = lnext(partexprs_item); + } + + if (IsA(clause, OpExpr)) + { + OpExpr *opclause = (OpExpr *) clause; + Expr *leftop, + *rightop, + *constexpr; + + leftop = (Expr *) get_leftop(clause); + if (IsA(leftop, RelabelType)) + leftop = ((RelabelType *) leftop)->arg; + rightop = (Expr *) get_rightop(clause); + if (EXPR_MATCHES_PARTKEY(leftop, partattno, partexpr)) + constexpr = rightop; + else if (EXPR_MATCHES_PARTKEY(rightop, partattno, partexpr)) + constexpr = leftop; + else + /* Clause not meant for this column. */ + continue; + + /* + * Handle some cases wherein the clause's operator may not + * belong to the partitioning operator family. For example, + * operators named '<>' are not listed in any operator + * family whatsoever. Also, ordering opertors like '<' are + * not listed in the hash operator family. + */ + if (!op_in_opfamily(opclause->opno, partopfamily)) + { + Expr *ltexpr, + *gtexpr; + Oid negator, + ltop, + gtop; + int strategy; + Oid lefttype, + righttype; + + /* + * To confirm if the operator is '<>', check if its + * negator is an equality operator. If so and it's a btree + * equality operator, we can use a special trick to prune + * partitions that won't satisfy the original '<>' + * operator -- we generate an OR expression + * 'leftop < rightop OR leftop > rightop' and add it to + * *or_clauses. + */ + negator = get_negator(opclause->opno); + if (OidIsValid(negator) && + op_in_opfamily(negator, partopfamily)) + { + get_op_opfamily_properties(negator, partopfamily, + false, + &strategy, + &lefttype, &righttype); + if (strategy == BTEqualStrategyNumber) + { + Expr *or; + + ltop = get_opfamily_member(partopfamily, + lefttype, righttype, + BTLessStrategyNumber); + gtop = get_opfamily_member(partopfamily, + lefttype, righttype, + BTGreaterStrategyNumber); + ltexpr = make_opclause(ltop, BOOLOID, false, + (Expr *) leftop, + (Expr *) rightop, + InvalidOid, partcoll); + gtexpr = make_opclause(gtop, BOOLOID, false, + (Expr *) leftop, + (Expr *) rightop, + InvalidOid, partcoll); + or = makeBoolExpr(OR_EXPR, + list_make2(ltexpr, gtexpr), -1); + *or_clauses = lappend(*or_clauses, or); + continue; + } + } + + /* + * Getting here means opclause uses an ordering op and + * hash partitioning is in use. We shouldn't try to + * reason about such an operator for the purposes of + * partition pruning, because hash partitioning doesn't + * make partitioning decisions based on relative ordering + * of keys. + */ + continue; + } + + pc = palloc0(sizeof(PartClause)); + pc->constarg = constexpr; + + /* + * Flip the left and right args if we have to, because the + * code which extract the constant value to use for + * partition-pruning expects to find it as the rightop of the + * clause. (See below in this function.) + */ + if (constexpr == rightop) + pc->op = opclause; + else + { + OpExpr *commuted; + + commuted = (OpExpr *) copyObject(opclause); + commuted->opno = get_commutator(opclause->opno); + commuted->opfuncid = get_opcode(commuted->opno); + commuted->args = list_make2(rightop, leftop); + pc->op = commuted; + } + + keyclauses_all[i] = lappend(keyclauses_all[i], pc); + only_or_clauses = false; + + /* + * Since we only allow strict operators, require keys to be + * not null. + */ + keyisnotnull[i] = true; + } + else if (IsA(clause, ScalarArrayOpExpr)) + { + ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause; + Oid saop_op = saop->opno; + Oid saop_opfuncid = saop->opfuncid; + Oid saop_coll = saop->inputcollid; + Node *leftop = (Node *) linitial(saop->args), + *rightop = (Node *) lsecond(saop->args); + List *elem_exprs, + *elem_clauses; + ListCell *lc1; + bool negated = false; + + /* + * In case of NOT IN (..), we get a '<>', which while not + * listed as part of any operator family, we are able to + * handle the same if its negator is indeed a part of the + * partitioning operator family. + */ + if (!op_in_opfamily(saop_op, partopfamily)) + { + Oid negator = get_negator(saop_op); + int strategy; + Oid lefttype, + righttype; + + if (!OidIsValid(negator)) + continue; + get_op_opfamily_properties(negator, partopfamily, false, + &strategy, + &lefttype, &righttype); + if (strategy == BTEqualStrategyNumber) + negated = true; + } + + /* + * First generate a list of Const nodes, one for each array + * element. + */ + elem_exprs = NIL; + if (IsA(rightop, Const)) + { + Const *arr = (Const *) lsecond(saop->args); + ArrayType *arrval = DatumGetArrayTypeP(arr->constvalue); + int16 elemlen; + bool elembyval; + char elemalign; + Datum *elem_values; + bool *elem_nulls; + int num_elems; + + get_typlenbyvalalign(ARR_ELEMTYPE(arrval), + &elemlen, &elembyval, &elemalign); + deconstruct_array(arrval, + ARR_ELEMTYPE(arrval), + elemlen, elembyval, elemalign, + &elem_values, &elem_nulls, + &num_elems); + for (i = 0; i < num_elems; i++) + { + if (!elem_nulls[i]) + elem_exprs = lappend(elem_exprs, + makeConst(ARR_ELEMTYPE(arrval), + -1, arr->constcollid, + elemlen, elem_values[i], + false, elembyval)); + else + elem_exprs = lappend(elem_exprs, + makeNullConst(ARR_ELEMTYPE(arrval), + -1, + arr->constcollid)); + } + } + else + { + ArrayExpr *arrexpr = castNode(ArrayExpr, rightop); + + elem_exprs = list_copy(arrexpr->elements); + } + + /* + * Now generate a list of clauses, one for each array element, + * of the form: saop_leftop saop_op elem_expr + */ + elem_clauses = NIL; + foreach(lc1, elem_exprs) + { + Const *rightop = castNode(Const, lfirst(lc1)); + Expr *elem_clause; + + if (rightop->constisnull) + { + NullTest *nulltest = makeNode(NullTest); + + nulltest->arg = (Expr *) leftop; + nulltest->nulltesttype = !negated ? IS_NULL + : IS_NOT_NULL; + nulltest->argisrow = false; + nulltest->location = -1; + elem_clause = (Expr *) nulltest; + } + else + { + OpExpr *opexpr = makeNode(OpExpr); + + opexpr->opno = saop_op; + opexpr->opfuncid = saop_opfuncid; + opexpr->opresulttype = BOOLOID; + opexpr->opretset = false; + opexpr->opcollid = InvalidOid; + opexpr->inputcollid = saop_coll; + opexpr->args = list_make2(leftop, rightop); + opexpr->location = -1; + elem_clause = (Expr *) opexpr; + } + + elem_clauses = lappend(elem_clauses, elem_clause); + } + + /* + * Build the OR clause if needed or add the clauses to the end + * of the list that's being processed currently. + */ + if (saop->useOr) + *or_clauses = lappend(*or_clauses, + makeBoolExpr(OR_EXPR, elem_clauses, + -1)); + else + clauses = list_concat(clauses, elem_clauses); + } + else if (IsA(clause, NullTest)) + { + NullTest *nulltest = (NullTest *) clause; + Expr *arg = nulltest->arg; + + if (IsA(arg, RelabelType)) + arg = ((RelabelType *) arg)->arg; + + /* Does leftop match with this partition key column? */ + if ((IsA(arg, Var) && + ((Var *) arg)->varattno == partattno) || + equal(arg, partexpr)) + { + if (nulltest->nulltesttype == IS_NULL) + keyisnull[i] = true; + else + keyisnotnull[i] = true; + n_keynullness++; + only_or_clauses = false; + } + } + /* + * Boolean conditions have a special shape, which would've been + * accepted if the partitioning opfamily accepts Boolean + * conditions. + */ + else if (IsBooleanOpfamily(partopfamily) && + (IsA(clause, BooleanTest) || + IsA(clause, Var) || + not_clause((Node *) clause))) + { + Expr *leftop, + *rightop; + + pc = palloc0(sizeof(PartClause)); + + if (IsA(clause, BooleanTest)) + { + BooleanTest *btest = (BooleanTest *) clause; + + leftop = btest->arg; + rightop = (btest->booltesttype == IS_TRUE || + btest->booltesttype == IS_NOT_FALSE) + ? (Expr *) makeBoolConst(true, false) + : (Expr *) makeBoolConst(false, false); + } + else + { + leftop = IsA(clause, Var) + ? (Expr *) clause + : (Expr *) get_notclausearg((Expr *) clause); + rightop = IsA(clause, Var) + ? (Expr *) makeBoolConst(true, false) + : (Expr *) makeBoolConst(false, false); + } + pc->op = (OpExpr *) make_opclause(BooleanEqualOperator, + BOOLOID, false, + leftop, rightop, + InvalidOid, InvalidOid); + pc->constarg = rightop; + keyclauses_all[i] = lappend(keyclauses_all[i], pc); + only_or_clauses = false; + } + } + } + + /* Return if no work to do below. */ + if (only_or_clauses || *constfalse) + return 0; + + /* + * Try to eliminate redundant keys. In the process, we might find out + * that clauses are mutually contradictory and hence can never be true + * for any rows. + */ + memset(keyclauses, 0, PARTITION_MAX_KEYS * sizeof(List *)); + for (i = 0; i < partkey->partnatts; i++) + { + remove_redundant_clauses(partkey, i, + keyclauses_all[i], &keyclauses[i], + constfalse); + if (*constfalse) + return 0; + } + + /* + * Now, generate the bounding tuples that can serve as equal, min, and + * max keys. + */ + need_next_eq = true; + need_next_min = true; + need_next_max = true; + memset(keys, 0, sizeof(PartScanKeyInfo)); + for (i = 0; i < partkey->partnatts; i++) + { + /* + * Min and max keys must constitute a prefix of the partition key and + * must appear in the same order as partition keys. Equal keys have + * to satisfy that requirement only for non-hash partitioning. + */ + if (i > keys->n_eqkeys && + partkey->strategy != PARTITION_STRATEGY_HASH) + need_next_eq = false; + + if (i > keys->n_minkeys) + need_next_min = false; + + if (i > keys->n_maxkeys) + need_next_max = false; + + foreach(lc, keyclauses[i]) + { + PartClause *clause = lfirst(lc); + Expr *constarg = clause->constarg; + bool incl; + int32 op_strategy; + + op_strategy = partition_op_strategy(partkey, clause, &incl); + if (op_strategy < 0 && + need_next_max && + partkey_datum_from_expr(partkey, i, constarg, + &keys->maxkeys[i])) + { + keys->n_maxkeys++; + keys->max_incl = incl; + if (!incl) + need_next_eq = need_next_max = false; + } + else if (op_strategy == 0) + { + Assert(incl); + if (need_next_eq && + partkey_datum_from_expr(partkey, i, constarg, + &keys->eqkeys[i])) + keys->n_eqkeys++; + + if (need_next_max && + partkey_datum_from_expr(partkey, i, constarg, + &keys->maxkeys[i])) + { + keys->n_maxkeys++; + keys->max_incl = true; + } + + if (need_next_min && + partkey_datum_from_expr(partkey, i, constarg, + &keys->minkeys[i])) + { + keys->n_minkeys++; + keys->min_incl = true; + } + } + else if (need_next_min && + partkey_datum_from_expr(partkey, i, constarg, + &keys->minkeys[i])) + { + keys->n_minkeys++; + keys->min_incl = incl; + if (!incl) + need_next_eq = need_next_min = false; + } + } + } + + /* + * To set eqkeys, we must have found the same for partition key columns. + * If present, we don't need minkeys and maxkeys anymore. In the case + * of hash partitioning, we don't require all equal keys to be operator + * clauses. For hash partitioning, any IS NULL clauses are considered + * as equal keys by the code performing actual pruning, at which time it + * is checked whether, along with any operator clauses, all partition key + * columns are covered. + */ + if (keys->n_eqkeys == partkey->partnatts || + partkey->strategy == PARTITION_STRATEGY_HASH) + keys->n_minkeys = keys->n_maxkeys = 0; + else + keys->n_eqkeys = 0; + + /* Finally, also set the keyisnull and keyisnotnull values. */ + for (i = 0; i < partkey->partnatts; i++) + { + keys->keyisnull[i] = keyisnull[i]; + keys->keyisnotnull[i] = keyisnotnull[i]; + } + + return keys->n_eqkeys + keys->n_minkeys + keys->n_maxkeys + n_keynullness; +} + +/* + * Returns -1, 0, or 1 to signify that the partitioning clause has a />= operator, respectively. Sets *incl to true if equality is + * implied. + */ +static int32 +partition_op_strategy(PartitionKey key, PartClause *op, bool *incl) +{ + int32 result; + + switch (key->strategy) + { + /* Hash partitioning allows only hash equality. */ + case PARTITION_STRATEGY_HASH: + if (op->op_strategy == HTEqualStrategyNumber) + { + *incl = true; + result = 0; + } + break; + + /* List and range partitioning support all btree operators. */ + case PARTITION_STRATEGY_LIST: + case PARTITION_STRATEGY_RANGE: + switch (op->op_strategy) + { + case BTLessStrategyNumber: + case BTLessEqualStrategyNumber: + result = -1; + *incl = (op->op_strategy == BTLessEqualStrategyNumber); + break; + case BTEqualStrategyNumber: + result = 0; + *incl = true; + break; + case BTGreaterStrategyNumber: + case BTGreaterEqualStrategyNumber: + result = 1; + *incl = (op->op_strategy == BTGreaterEqualStrategyNumber); + break; + } + break; + } + + return result; +} + +/* + * partkey_datum_from_expr + * Extract constant value from expr and set *datum to that value + */ +static bool +partkey_datum_from_expr(PartitionKey key, int partattoff, + Expr *expr, Datum *value) +{ + Oid exprtype = exprType((Node *) expr); + + if (exprtype != key->parttypid[partattoff]) + { + ParseState *pstate = make_parsestate(NULL); + + expr = (Expr *) coerce_to_target_type(pstate, (Node *) expr, + exprtype, + key->parttypid[partattoff], -1, + COERCION_EXPLICIT, + COERCE_IMPLICIT_CAST, -1); + /* + * If couldn't coerce to the partition key type, that is, the type of + * datums stored in PartitionBoundInfo, no hope of using this + * expression for anything partitioning-related. + */ + if (expr == NULL) + return false; + + /* + * Transform into a form that the following code can do something + * useful with. + */ + expr = evaluate_expr(expr, + exprType((Node *) expr), + exprTypmod((Node *) expr), + exprCollation((Node *) expr)); + } + + /* + * Add more expression types here as needed to support higher-level + * code. + */ + switch (nodeTag(expr)) + { + case T_Const: + *value = ((Const *) expr)->constvalue; + return true; + + default: + return false; + } + + Assert(false); /* don't ever get here */ + return false; +} + +/* + * For a given partition key column, find the most restrictive of the clauses + * contained in all_clauses that are known to match the column. If in the + * process, it is found that two clauses are mutually contradictory, we simply + * stop, set *constfalse to true, and return. + */ +static void +remove_redundant_clauses(PartitionKey partkey, int partattoff, + List *all_clauses, List **result, + bool *constfalse) +{ + PartClause *hash_clause, + *btree_clauses[BTMaxStrategyNumber]; + ListCell *lc; + int s; + bool test_result; + + *result = NIL; + + hash_clause = NULL; + memset(btree_clauses, 0, sizeof(btree_clauses)); + foreach(lc, all_clauses) + { + PartClause *cur = lfirst(lc); + + if (!cur->valid_cache) + { + Oid lefttype; + + get_op_opfamily_properties(cur->op->opno, + partkey->partopfamily[partattoff], + false, + &cur->op_strategy, + &lefttype, + &cur->op_subtype); + fmgr_info(get_opcode(cur->op->opno), &cur->op_func); + cur->valid_cache = true; + } + + /* + * Hash-partitioning knows only about equality. So, if we've matched + * a clause and found another whose constant operand doesn't match + * the constant operand of the former, we have a case of mutually + * contradictory clauses. + */ + if (partkey->strategy == PARTITION_STRATEGY_HASH) + { + if (hash_clause == NULL) + hash_clause = cur; + /* check if another clause would contradict the one we have */ + else if (partition_cmp_args(partkey, partattoff, + cur, cur, hash_clause, + &test_result)) + { + if (!test_result) + { + *constfalse = true; + return; + } + } + /* + * Couldn't compare; keep hash_clause set to the previous value and + * so add this one directly to the result. Caller would + * arbitrarily choose one of the many and perform + * partition-pruning with the same. It's possible that mutual + * contradiction is proved at some higher level, but it's just + * that we couldn't do so here. + */ + else + *result = lappend(*result, cur); + + /* The code below is for btree operators, which cur is not. */ + continue; + } + + /* + * Stuff that follows closely mimics similar processing done by + * nbtutils.c: _bt_preprocess_keys(). + * + * btree_clauses[s] points to the currently best scan key of strategy + * type s+1; it is NULL if we haven't yet found such a key for this + * attr. + */ + s = cur->op_strategy - 1; + if (btree_clauses[s] == NULL) + { + btree_clauses[s] = cur; + } + else + { + /* + * Is this one more restrictive than what we already have? + * + * Consider some examples: 1. If btree_clauses[BTLT] now contains + * a < 5, and cur is a < 3, then because 3 < 5 is true, a < 5 + * currently at btree_clauses[BTLT] will be replaced by a < 3. + * + * 2. If btree_clauses[BTEQ] now contains a = 5 and cur is a = 7, + * then because 5 = 7 is false, we found a mutual contradiction, + * so we set *constfalse to true and return. + * + * 3. If btree_clauses[BTLT] now contains a < 5 and cur is a < 7, + * then because 7 < 5 is false, we leave a < 5 where it is and + * effectively discard a < 7 as being redundant. + */ + if (partition_cmp_args(partkey, partattoff, + cur, cur, btree_clauses[s], + &test_result)) + { + /* cur is more restrictive, replace old key. */ + if (test_result) + btree_clauses[s] = cur; + else if (s == BTEqualStrategyNumber - 1) + { + *constfalse = true; + return; + } + + /* The old key is more restrictive, keep around. */ + } + else + { + /* + * we couldn't determine which one is more restrictive. Keep + * the previous one in btree_clauses[s] and push this one directly + * to the output list. + */ + *result = lappend(*result, cur); + } + } + } + + if (partkey->strategy == PARTITION_STRATEGY_HASH) + { + /* Note we didn't add this one to the result yet. */ + if (hash_clause) + *result = lappend(*result, hash_clause); + return; + } + + /* Compare btree operator clauses across strategies. */ + + /* Compare the equal key with keys of other strategies. */ + if (btree_clauses[BTEqualStrategyNumber - 1]) + { + PartClause *eq = btree_clauses[BTEqualStrategyNumber - 1]; + + for (s = 0; s < BTMaxStrategyNumber; s++) + { + PartClause *chk = btree_clauses[s]; + + if (!chk || s == (BTEqualStrategyNumber - 1)) + continue; + + /* + * Suppose btree_clauses[BTLT] contained a < 5 and the eq key is + * a = 5, then because 5 < 5 is false, we found contradiction. + * That is, a < 5 and a = 5 are mutually contradictory. OTOH, if + * eq key is a = 3, then because 3 < 5, we no longer need a < 5, + * because a = 3 is more restrictive. + */ + if (partition_cmp_args(partkey, partattoff, + chk, eq, chk, + &test_result)) + { + if (!test_result) + { + *constfalse = true; + return; + } + /* discard the redundant key. */ + btree_clauses[s] = NULL; + } + } + } + + /* + * Try to keep only one of <, <=. + * + * Suppose btree_clauses[BTLT] contains a < 3 and btree_clauses[BTLE] + * contains a <= 3 (or a <= 4), then because 3 <= 3 (or 3 <= 4) is true, + * we discard the a <= 3 (or a <= 4) as redundant. If the latter contains + * contains a <= 2, then because 3 <= 2 is false, we dicard a < 3 as + * redundant. + */ + if (btree_clauses[BTLessStrategyNumber - 1] && + btree_clauses[BTLessEqualStrategyNumber - 1]) + { + PartClause *lt = btree_clauses[BTLessStrategyNumber - 1], + *le = btree_clauses[BTLessEqualStrategyNumber - 1]; + + if (partition_cmp_args(partkey, partattoff, + le, lt, le, + &test_result)) + { + if (test_result) + btree_clauses[BTLessEqualStrategyNumber - 1] = NULL; + else + btree_clauses[BTLessStrategyNumber - 1] = NULL; + } + } + + /* Try to keep only one of >, >=. See the example above. */ + if (btree_clauses[BTGreaterStrategyNumber - 1] && + btree_clauses[BTGreaterEqualStrategyNumber - 1]) + { + PartClause *gt = btree_clauses[BTGreaterStrategyNumber - 1], + *ge = btree_clauses[BTGreaterEqualStrategyNumber - 1]; + + if (partition_cmp_args(partkey, partattoff, ge, gt, ge, + &test_result)) + { + if (test_result) + btree_clauses[BTGreaterEqualStrategyNumber - 1] = NULL; + else + btree_clauses[BTGreaterStrategyNumber - 1] = NULL; + } + } + + /* + * btree_clauses now contains the "best" clause or NULL for each btree + * strategy number. Add to the result. + */ + for (s = 0; s < BTMaxStrategyNumber; s++) + if (btree_clauses[s]) + *result = lappend(*result, btree_clauses[s]); +} + +/* + * Evaluate 'leftarg op rightarg' and set *result to its value. + * + * leftarg and rightarg referred to above actually refer to the constant + * operand (Datum) of the clause contained in the parameters leftarg and + * rightarg below, respectively. And op refers to the operator of the + * clause contained in the parameter op below. + * + * Returns true if we could actually perform the evaluation. False is + * returned otherwise, that is, in cases where we couldn't perform the + * evaluation for reasons such as operands values being unavailable or + * types of operands being incompatible with the operator. + */ +static bool +partition_cmp_args(PartitionKey key, int partattoff, + PartClause *op, PartClause *leftarg, PartClause *rightarg, + bool *result) +{ + Oid partopfamily = key->partopfamily[partattoff]; + Datum leftarg_const, + rightarg_const; + + Assert(op->valid_cache && leftarg->valid_cache && rightarg->valid_cache); + /* Get the constant values from the operands */ + if (!partkey_datum_from_expr(key, partattoff, + leftarg->constarg, &leftarg_const)) + return false; + if (!partkey_datum_from_expr(key, partattoff, + rightarg->constarg, &rightarg_const)) + return false; + + /* + * If the leftarg_const and rightarg_consr are both of the type expected + * by op's operator, then compare them using the latter. + */ + if (leftarg->op_subtype == op->op_subtype && + rightarg->op_subtype == op->op_subtype) + { + *result = DatumGetBool(FunctionCall2Coll(&op->op_func, + op->op->inputcollid, + leftarg_const, + rightarg_const)); + return true; + } + else + { + /* Otherwise, look one up in the partitioning operator family. */ + Oid cmp_op = get_opfamily_member(partopfamily, + leftarg->op_subtype, + rightarg->op_subtype, + op->op_strategy); + if (OidIsValid(cmp_op)) + { + *result = DatumGetBool(OidFunctionCall2Coll(get_opcode(cmp_op), + op->op->inputcollid, + leftarg_const, + rightarg_const)); + return true; + } + } + + /* Couldn't do the comparison. */ + *result = false; + return false; +} + +/* + * get_partitions_for_keys + * Returns the partitions that will need to be scanned for the given + * bounding keys + * + * Input: + * See the comments above the definition of PartScanKeyInfo to see what + * kind of information is received here. + * + * Outputs: + * Partition set satisfying the keys. + */ +static Bitmapset * +get_partitions_for_keys(Relation rel, PartScanKeyInfo *keys) +{ + PartitionDesc partdesc = RelationGetPartitionDesc(rel); + Bitmapset *result = NULL; + + result = bms_add_range(result, 0, partdesc->nparts - 1); + + return result; +} + +/* * get_partition_operator * * Return oid of the operator of given strategy for a given partition key diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index d14ef31eae..72f1fa30a6 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -149,8 +149,6 @@ static Node *substitute_actual_parameters(Node *expr, int nargs, List *args, static Node *substitute_actual_parameters_mutator(Node *node, substitute_actual_parameters_context *context); static void sql_inline_error_callback(void *arg); -static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, - Oid result_collation); static Query *substitute_actual_srf_parameters(Query *expr, int nargs, List *args); static Node *substitute_actual_srf_parameters_mutator(Node *node, @@ -4744,7 +4742,7 @@ sql_inline_error_callback(void *arg) * We use the executor's routine ExecEvalExpr() to avoid duplication of * code and ensure we get the same result as the executor would get. */ -static Expr * +Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation) { diff --git a/src/include/optimizer/clauses.h b/src/include/optimizer/clauses.h index e3672218f3..1ef13a49de 100644 --- a/src/include/optimizer/clauses.h +++ b/src/include/optimizer/clauses.h @@ -84,5 +84,7 @@ extern Node *estimate_expression_value(PlannerInfo *root, Node *node); extern Query *inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte); +extern Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, + Oid result_collation); #endif /* CLAUSES_H */ diff --git a/src/test/regress/expected/partition.out b/src/test/regress/expected/partition.out index 963561dbfe..d44ff4f608 100644 --- a/src/test/regress/expected/partition.out +++ b/src/test/regress/expected/partition.out @@ -1049,16 +1049,11 @@ explain (costs off) select * from boolpart where a is not true; (7 rows) explain (costs off) select * from boolpart where a is not true and a is not false; - QUERY PLAN --------------------------------------------------------- - Append - -> Seq Scan on boolpart_f - Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE)) - -> Seq Scan on boolpart_t - Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE)) - -> Seq Scan on boolpart_default - Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE)) -(7 rows) + QUERY PLAN +-------------------------- + Result + One-Time Filter: false +(2 rows) explain (costs off) select * from boolpart where a is unknown; QUERY PLAN -- 2.11.0