|
| 1 | +# Partition distribution metadata |
| 2 | + |
| 3 | +Protocol 3.1 describes scan distribution with two independent facts: |
| 4 | + |
| 5 | +- `PlanResponse.key_grouping` says that every row produced by one split has one |
| 6 | + exact transformed key tuple. This is the contract needed by Spark's |
| 7 | + key-grouped input partitions. |
| 8 | +- `PlanResponse.range_partitioning` says that splits belong to exact, ordered |
| 9 | + ranges separated by shared split points. This is the contract needed by |
| 10 | + DataFusion's range partitioning. |
| 11 | + |
| 12 | +Neither field implies the other. `sort_order` continues to describe order |
| 13 | +*within* a split, while `ScanSplit.column_bounds` contains pruning statistics |
| 14 | +only. A client must not promote bounds, Hive-style directory values, or the |
| 15 | +deprecated protocol-2 `partitioning` field into an exact distribution claim. |
| 16 | + |
| 17 | +## Transform specification |
| 18 | + |
| 19 | +A `TransformSpec` contains: |
| 20 | + |
| 21 | +- `id`: a versioned registry name such as `vgi.identity/1` or |
| 22 | + `iceberg.bucket/1`; |
| 23 | +- `inputs`: one or more serialized `FieldPath` records; and |
| 24 | +- optional `parameters`: an Arrow IPC record batch containing exactly one row. |
| 25 | + |
| 26 | +`FieldPath.components` preserves identifier boundaries. For example, |
| 27 | +`["customer", "region.code"]` means nested field `region.code` under |
| 28 | +`customer`; it is not the three-part dotted path `customer.region.code`. |
| 29 | + |
| 30 | +The one-row parameter batch may have any number of named, typed columns. This |
| 31 | +allows a transform to carry, for example, an `int32` bucket count, a string |
| 32 | +calendar name, and a Boolean mode flag without adding transform-specific wire |
| 33 | +fields. Parameter names, Arrow types, null values, and transform-registry |
| 34 | +semantics are all part of the transform identity. Arbitrary recursive |
| 35 | +expressions are deliberately outside this contract. |
| 36 | + |
| 37 | +The initial portable profile contains only `vgi.identity/1`, with exactly one |
| 38 | +input and no parameter batch. An adapter that does not implement a transform's |
| 39 | +registered semantics exactly must report unknown partitioning. It must not |
| 40 | +approximate another engine's bucket, truncate, hash, collation, or time-zone |
| 41 | +rules. |
| 42 | + |
| 43 | +## Exact key grouping |
| 44 | + |
| 45 | +`KeyGrouping.keys` is the ordered tuple of transforms. When it is present: |
| 46 | + |
| 47 | +1. every ordinary split has a `key_value` batch; |
| 48 | +2. that batch has exactly one row and one column per transform, in key order; |
| 49 | +3. each column has the registered output type of its transform; and |
| 50 | +4. every row emitted by the split evaluates to that tuple under null-safe |
| 51 | + equality. |
| 52 | + |
| 53 | +Several splits may have the same key. A split without a key, or a key whose |
| 54 | +shape or type is invalid, makes the grouping claim unusable. The no-split |
| 55 | +sentinel does not carry a grouping claim. |
| 56 | + |
| 57 | +Spark maps a supported grouping to `KeyGroupedPartitioning`; every physical |
| 58 | +input partition exposes its tuple through `HasPartitionKey`. The current Spark |
| 59 | +adapter accepts only the portable identity profile. Mixed scan branches must |
| 60 | +advertise the same grouping, otherwise the complete scan reports unknown |
| 61 | +partitioning. |
| 62 | + |
| 63 | +The DuckDB 2 adapter uses the same exact tuple for |
| 64 | +`SINGLE_VALUE_PARTITIONS`, but only when the function has already declared |
| 65 | +that optimization at bind time and its annotated partition fields exactly |
| 66 | +match the identity-key sequence. The plan metadata cannot independently opt a |
| 67 | +function into the optimization because it arrives after physical planning. |
| 68 | +Workers without a supported grouping continue to use the existing per-batch |
| 69 | +`vgi_partition_values` metadata path. |
| 70 | + |
| 71 | +## Exact range partitioning |
| 72 | + |
| 73 | +`RangePartitioning.keys` is an ordered tuple of `RangeKey` records. Each range |
| 74 | +key combines a transform with an ascending/descending direction and a null |
| 75 | +ordering. `split_points` has one column per range key, using each transform's |
| 76 | +output type. |
| 77 | + |
| 78 | +For `N` split-point rows there are `N + 1` ranges. The rows must be strictly |
| 79 | +ordered by the declared lexicographic comparator. A value equal to split point |
| 80 | +`i` belongs to range `i + 1`; ranges are therefore half-open at their lower |
| 81 | +boundary except for the first range. Each ordinary split supplies a zero-based |
| 82 | +`range_index`, and all of its output belongs to that exact range. More than one |
| 83 | +split may share a range, and a range may contain no splits. |
| 84 | + |
| 85 | +DataFusion maps this metadata to physical `RangePartitioning` only when it can |
| 86 | +preserve one output partition per range within the requested parallelism and |
| 87 | +worker cap. It currently accepts only top-level `vgi.identity/1` keys whose |
| 88 | +split-point Arrow types match the output schema. Otherwise it retains the |
| 89 | +splits but reports `UnknownPartitioning`. |
| 90 | + |
| 91 | +## Pagination and compatibility |
| 92 | + |
| 93 | +Distribution metadata is a property of the complete plan. Every page reached |
| 94 | +through `next_cursors` must repeat byte-identical `key_grouping` and |
| 95 | +`range_partitioning` values, and the split-local values on that page must obey |
| 96 | +them. A client must discard the distribution claim or reject a malformed plan |
| 97 | +if pages disagree; it must never combine inconsistent claims. |
| 98 | + |
| 99 | +Protocol 3.1 producers should use `key_grouping`, `range_partitioning`, |
| 100 | +`key_value`, `range_index`, and `column_bounds`. The deprecated |
| 101 | +`PlanResponse.partitioning` and `ScanSplit.partition_bounds` fields remain on |
| 102 | +the wire for protocol-2 compatibility, but do not carry the exact semantics |
| 103 | +required by the new engine mappings. |
0 commit comments