|
| 1 | +/* |
| 2 | + * partutils.c |
| 3 | + * |
| 4 | + */ |
| 5 | +#include "partutils.h" |
| 6 | + |
| 7 | +#include "optimizer/paths.h" |
| 8 | +#include "partitioning/partbounds.h" |
| 9 | + |
| 10 | + |
| 11 | +bool |
| 12 | +build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, |
| 13 | + RelOptInfo *inner_rel, List *restrictlist, |
| 14 | + JoinType jointype) |
| 15 | +{ |
| 16 | + int partnatts; |
| 17 | + int cnt; |
| 18 | + PartitionScheme part_scheme; |
| 19 | + |
| 20 | + /* |
| 21 | + * We can only consider this join as an input to further partitionwise |
| 22 | + * joins if (a) the input relations are partitioned and have |
| 23 | + * consider_partitionwise_join=true, (b) the partition schemes match, and |
| 24 | + * (c) we can identify an equi-join between the partition keys. Note that |
| 25 | + * if it were possible for have_partkey_equi_join to return different |
| 26 | + * answers for the same joinrel depending on which join ordering we try |
| 27 | + * first, this logic would break. That shouldn't happen, though, because |
| 28 | + * of the way the query planner deduces implied equalities and reorders |
| 29 | + * the joins. Please see optimizer/README for details. |
| 30 | + */ |
| 31 | + if (!IS_PARTITIONED_REL(outer_rel) || !IS_PARTITIONED_REL(inner_rel) || |
| 32 | + outer_rel->part_scheme != inner_rel->part_scheme || |
| 33 | + !have_partkey_equi_join(joinrel, outer_rel, inner_rel, |
| 34 | + jointype, restrictlist)) |
| 35 | + return false; |
| 36 | + |
| 37 | + part_scheme = outer_rel->part_scheme; |
| 38 | + |
| 39 | + Assert(REL_HAS_ALL_PART_PROPS(outer_rel) && |
| 40 | + REL_HAS_ALL_PART_PROPS(inner_rel)); |
| 41 | + |
| 42 | + /* |
| 43 | + * For now, our partition matching algorithm can match partitions only |
| 44 | + * when the partition bounds of the joining relations are exactly same. |
| 45 | + * So, bail out otherwise. |
| 46 | + */ |
| 47 | + if (outer_rel->nparts != inner_rel->nparts || |
| 48 | + !partition_bounds_equal(part_scheme->partnatts, |
| 49 | + part_scheme->parttyplen, |
| 50 | + part_scheme->parttypbyval, |
| 51 | + outer_rel->boundinfo, inner_rel->boundinfo)) |
| 52 | + { |
| 53 | + Assert(0); |
| 54 | + Assert(!IS_PARTITIONED_REL(joinrel)); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * This function will be called only once for each joinrel, hence it |
| 60 | + * should not have partition scheme, partition bounds, partition key |
| 61 | + * expressions and array for storing child relations set. |
| 62 | + */ |
| 63 | + Assert(!joinrel->part_scheme && !joinrel->partexprs && |
| 64 | + !joinrel->nullable_partexprs && !joinrel->part_rels && |
| 65 | + !joinrel->boundinfo); |
| 66 | + |
| 67 | + /* |
| 68 | + * Join relation is partitioned using the same partitioning scheme as the |
| 69 | + * joining relations and has same bounds. |
| 70 | + */ |
| 71 | + joinrel->part_scheme = part_scheme; |
| 72 | + joinrel->boundinfo = outer_rel->boundinfo; |
| 73 | + partnatts = joinrel->part_scheme->partnatts; |
| 74 | + joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts); |
| 75 | + joinrel->nullable_partexprs = |
| 76 | + (List **) palloc0(sizeof(List *) * partnatts); |
| 77 | + joinrel->nparts = outer_rel->nparts; |
| 78 | + joinrel->part_rels = |
| 79 | + (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * joinrel->nparts); |
| 80 | + |
| 81 | + /* |
| 82 | + * Construct partition keys for the join. |
| 83 | + * |
| 84 | + * An INNER join between two partitioned relations can be regarded as |
| 85 | + * partitioned by either key expression. For example, A INNER JOIN B ON |
| 86 | + * A.a = B.b can be regarded as partitioned on A.a or on B.b; they are |
| 87 | + * equivalent. |
| 88 | + * |
| 89 | + * For a SEMI or ANTI join, the result can only be regarded as being |
| 90 | + * partitioned in the same manner as the outer side, since the inner |
| 91 | + * columns are not retained. |
| 92 | + * |
| 93 | + * An OUTER join like (A LEFT JOIN B ON A.a = B.b) may produce rows with |
| 94 | + * B.b NULL. These rows may not fit the partitioning conditions imposed on |
| 95 | + * B.b. Hence, strictly speaking, the join is not partitioned by B.b and |
| 96 | + * thus partition keys of an OUTER join should include partition key |
| 97 | + * expressions from the OUTER side only. However, because all |
| 98 | + * commonly-used comparison operators are strict, the presence of nulls on |
| 99 | + * the outer side doesn't cause any problem; they can't match anything at |
| 100 | + * future join levels anyway. Therefore, we track two sets of |
| 101 | + * expressions: those that authentically partition the relation |
| 102 | + * (partexprs) and those that partition the relation with the exception |
| 103 | + * that extra nulls may be present (nullable_partexprs). When the |
| 104 | + * comparison operator is strict, the latter is just as good as the |
| 105 | + * former. |
| 106 | + */ |
| 107 | + for (cnt = 0; cnt < partnatts; cnt++) |
| 108 | + { |
| 109 | + List *outer_expr; |
| 110 | + List *outer_null_expr; |
| 111 | + List *inner_expr; |
| 112 | + List *inner_null_expr; |
| 113 | + List *partexpr = NIL; |
| 114 | + List *nullable_partexpr = NIL; |
| 115 | + |
| 116 | + outer_expr = list_copy(outer_rel->partexprs[cnt]); |
| 117 | + outer_null_expr = list_copy(outer_rel->nullable_partexprs[cnt]); |
| 118 | + inner_expr = list_copy(inner_rel->partexprs[cnt]); |
| 119 | + inner_null_expr = list_copy(inner_rel->nullable_partexprs[cnt]); |
| 120 | + |
| 121 | + switch (jointype) |
| 122 | + { |
| 123 | + case JOIN_INNER: |
| 124 | + partexpr = list_concat(outer_expr, inner_expr); |
| 125 | + nullable_partexpr = list_concat(outer_null_expr, |
| 126 | + inner_null_expr); |
| 127 | + break; |
| 128 | + |
| 129 | + case JOIN_SEMI: |
| 130 | + case JOIN_ANTI: |
| 131 | + partexpr = outer_expr; |
| 132 | + nullable_partexpr = outer_null_expr; |
| 133 | + break; |
| 134 | + |
| 135 | + case JOIN_LEFT: |
| 136 | + partexpr = outer_expr; |
| 137 | + nullable_partexpr = list_concat(inner_expr, |
| 138 | + outer_null_expr); |
| 139 | + nullable_partexpr = list_concat(nullable_partexpr, |
| 140 | + inner_null_expr); |
| 141 | + break; |
| 142 | + |
| 143 | + case JOIN_FULL: |
| 144 | + nullable_partexpr = list_concat(outer_expr, |
| 145 | + inner_expr); |
| 146 | + nullable_partexpr = list_concat(nullable_partexpr, |
| 147 | + outer_null_expr); |
| 148 | + nullable_partexpr = list_concat(nullable_partexpr, |
| 149 | + inner_null_expr); |
| 150 | + break; |
| 151 | + |
| 152 | + default: |
| 153 | + elog(ERROR, "unrecognized join type: %d", (int) jointype); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + joinrel->partexprs[cnt] = partexpr; |
| 158 | + joinrel->nullable_partexprs[cnt] = nullable_partexpr; |
| 159 | + } |
| 160 | + return true; |
| 161 | +} |
0 commit comments