diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index 3232844..194258d 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -127,9 +127,8 @@ static void get_range_key_properties(PartitionKey key, int keynum, Expr **keyCol, Const **lower_val, Const **upper_val); static List *get_qual_for_list(Relation parent, PartitionBoundSpec *spec); -static List *get_qual_for_range(Relation parent, PartitionBoundSpec *spec, - bool for_default); -static List *get_range_nulltest(PartitionKey key); +static List *get_qual_for_range(Relation parent, PartitionBoundSpec *spec); +static List *get_range_nulltest(PartitionKey key, bool for_default); static List *generate_partition_qual(Relation rel); static PartitionRangeBound *make_one_range_bound(PartitionKey key, int index, @@ -902,7 +901,7 @@ check_default_allows_bound(Relation parent, Relation default_rel, new_part_constraints = (new_spec->strategy == PARTITION_STRATEGY_LIST) ? get_qual_for_list(parent, new_spec) - : get_qual_for_range(parent, new_spec, false); + : get_qual_for_range(parent, new_spec); def_part_constraints = get_default_part_validation_constraint(new_part_constraints); @@ -1105,7 +1104,7 @@ get_qual_from_partbound(Relation rel, Relation parent, case PARTITION_STRATEGY_RANGE: Assert(spec->strategy == PARTITION_STRATEGY_RANGE); - my_qual = get_qual_for_range(parent, spec, false); + my_qual = get_qual_for_range(parent, spec); break; default: @@ -1740,7 +1739,7 @@ get_range_key_properties(PartitionKey key, int keynum, * keys to be null, so emit an IS NOT NULL expression for each key column. */ static List * -get_range_nulltest(PartitionKey key) +get_range_nulltest(PartitionKey key, bool for_default) { List *result = NIL; NullTest *nulltest; @@ -1771,7 +1770,7 @@ get_range_nulltest(PartitionKey key) nulltest = makeNode(NullTest); nulltest->arg = keyCol; - nulltest->nulltesttype = IS_NOT_NULL; + nulltest->nulltesttype = for_default? IS_NULL: IS_NOT_NULL; nulltest->argisrow = false; nulltest->location = -1; result = lappend(result, nulltest); @@ -1824,8 +1823,7 @@ get_range_nulltest(PartitionKey key) * If we end up with an empty result list, we return NULL. */ static List * -get_qual_for_range(Relation parent, PartitionBoundSpec *spec, - bool for_default) +get_qual_for_range(Relation parent, PartitionBoundSpec *spec) { List *result = NIL; ListCell *cell1, @@ -1878,15 +1876,34 @@ get_qual_for_range(Relation parent, PartitionBoundSpec *spec, if (!bspec->is_default) { - List *part_qual = get_qual_for_range(parent, bspec, true); + List *part_qual = get_qual_for_range(parent, bspec); + List *list_tmp = list_copy(part_qual); + ListCell *lc; + int count = 0; + + foreach (lc, list_tmp) + { + Node *n = (Node *) lfirst(lc); + + if (IsA(n, NullTest)) + { + list_delete(part_qual, n); + count++; + } + } + + if (count == list_length(list_tmp)) + part_qual = NIL; /* * AND the constraints of the partition and add to * or_expr_args */ - or_expr_args = lappend(or_expr_args, list_length(part_qual) > 1 - ? makeBoolExpr(AND_EXPR, part_qual, -1) - : linitial(part_qual)); + + if (part_qual != NIL) + or_expr_args = lappend(or_expr_args, list_length(part_qual) > 1 + ? makeBoolExpr(AND_EXPR, part_qual, -1) + : linitial(part_qual)); } ReleaseSysCache(tuple); } @@ -1900,6 +1917,8 @@ get_qual_for_range(Relation parent, PartitionBoundSpec *spec, : linitial(or_expr_args)); result = list_make1(makeBoolExpr(NOT_EXPR, result, -1)); } + else if (nparts != 1) + result = lappend(result,makeBoolExpr(OR_EXPR, get_range_nulltest(key, true), -1)); return result; } @@ -1908,8 +1928,7 @@ get_qual_for_range(Relation parent, PartitionBoundSpec *spec, upper_or_start_datum = list_head(spec->upperdatums); num_or_arms = key->partnatts; - if (!for_default) - result = get_range_nulltest(key); + result = get_range_nulltest(key, false); /* * Iterate over the key columns and check if the corresponding lower and @@ -2132,12 +2151,6 @@ get_qual_for_range(Relation parent, PartitionBoundSpec *spec, ? makeBoolExpr(OR_EXPR, upper_or_arms, -1) : linitial(upper_or_arms)); - /* As noted above, caller expects the list to be non-empty. */ - if (result == NIL) - result = for_default - ? get_range_nulltest(key) - : list_make1(makeBoolConst(true, false)); - return result; }