metanil opened a new pull request, #60793:
URL: https://github.com/apache/spark/pull/60793
### What changes were proposed in this pull request?
A `KeyedPartitioning` satisfies a clustering on a column when each partition
expression is the column, or a function of it
(`KeyedPartitioning.isFunctionOfClusterKeys`, and, for the projection under
`spark.sql.sources.v2.bucketing.allowKeysSubsetOfPartitionKeys.enabled`,
`positionsCoveringClusterKeys` and `keysCanSatisfy`). A function of a column,
such as a connector's partition transform, computes from the value as stored.
So it keeps rows that share a value of the column together only if equal values
have the same bytes. That doesn't hold for a string of a non-binary collation
(`'A'` = `'a'` under `UTF8_LCASE`), or for a `FLOAT` or `DOUBLE` (`0.0` =
`-0.0`). This PR adds that condition to the inference:
- **`KeyedPartitioning.coversKey(required)(ref)`**: a partition expression's
reference covers a cluster key only if it is one, and values its type deems
equal are stored alike. That is, `UnsafeRowUtils.isBinaryStable` holds and no
`FLOAT`/`DOUBLE` appears at any depth.
- **`isFunctionOfClusterKeys` and `positionsCoveringClusterKeys`** ask
`coversKey` of an expression's references. **`keysCanSatisfy`** counts a
position whose reference is a cluster key, if the position is a cluster key
itself or covers it. That is master's condition, narrowed.
- **`createShuffleSpec`** projects only onto positions that
`KeyedShuffleSpec.keyPositions` maps and `positionsCoveringClusterKeys` counts.
`keyPositions` reads a reference alone and stays as it is.
- **A partition expression that is itself a cluster key** is not affected.
So a column as a partition key of its own (`identity`) still clusters itself,
whatever its type, since partition keys are compared under their type's
equality. So does a clustering that names the transform itself.
What a scan reports doesn't change. The fix is in the inference, so it
covers every producer of a keyed partitioning: a scan, a union, and the layout
a broadcast hash join reports over the build side's key.
### Why are the changes needed?
An aggregate, a window or a semi-join over such a column skips its shuffle
and splits equal values. Measured on master with the test catalog, one split
per row, against the same query with
`spark.sql.sources.v2.bucketing.enabled=false`:
| partitioning | query | result | expected | shuffles |
|---|---|---|---|---|
| `bucket(n, s)`, `s` `UTF8_LCASE`, rows `'A'`, `'a'` | `count(*) OVER
(PARTITION BY s)` | 1, 1 | 2, 2 | 0 |
| same | `GROUP BY s` | two groups of 1 | one group of 2 | 0 |
| `bucket(n, s)` on both sides | `s IN (SELECT s ...)`, or the `EXISTS` form
| no rows | `'A'` | 0 |
| fact `bucket(n, b)` or `identity(b)`, `b` binary; dim `c.s` `UTF8_LCASE`;
broadcast join on `f.b = c.s COLLATE UTF8_BINARY` | `GROUP BY c.s` | two groups
of 1 | one group of 2 | 0 |
| a transform keying a `DOUBLE` on its sign bit, rows `0.0`, `-0.0` | `GROUP
BY d`, `DISTINCT d` | two groups | one group | 0 |
The JIRA lists more: `UTF8_BINARY_RTRIM`, a union, a layout `(identity(id),
bucket(n, s))`, and a broadcast join over `CAST(c.d AS STRING)`.
There are three ways in:
- **A scan's transform of the column.**
- **The layout a broadcast hash join reports over the build side's key.**
Even `identity(f.b)` over a binary column becomes a function of the collated
`c.s` there.
- **A semi-join from an `IN` or `EXISTS` subquery.** It is planned after
`RewriteCollationJoin`, so it compares `s` itself rather than `CollationKey(s)`.
An aggregate normalizes a float key but keeps the column's exprId, so it is
affected. A window clusters on the normalized expression, so it isn't. Other
equi-joins are correct: two collated columns are compared through
`CollationKey`, and float keys are normalized.
Alternatives considered:
- **Refuse such a transform in `KeyedPartitioning.supportsExpressions`, or
drop its position from the scan's reported partitioning.** Both miss the
broadcast join and float cases. The first also costs every other position of
the layout its shuffle-free plans.
- **Rewrite aggregates and windows to cluster on `CollationKey`, as joins
are.** It touches aggregate planning, costs `identity` its shuffle-free plans,
and doesn't cover floats.
The inference is the step that is wrong, so this fixes it there.
### Does this PR introduce _any_ user-facing change?
Yes, a correctness fix. The results above are measured on master; the JIRA
lists the released versions it affects.
- **Wrong results fixed.** An aggregate, a window, or the join of an
`IN`/`EXISTS` subquery over a column of a non-binary collation now shuffles a
V2 scan partitioned by a transform of the column, such as `bucket(n, s)`. So
does an aggregate over a `FLOAT` or `DOUBLE` column. These give the same
results as with V2 bucketing disabled. The same applies to a struct, array or
map holding such a value, and to the layout a broadcast hash join reports over
such a column.
- **Kept.** A column as a partition key of its own (`identity(s)`) keeps its
shuffle-free plans. On `(identity(id), bucket(n, s))`, an aggregate, a window
or such a join over `id, s` skips its shuffle only with
`spark.sql.sources.v2.bucketing.allowKeysSubsetOfPartitionKeys.enabled`.
- **Cost.** A connector transform that does respect the collation, or
normalizes `-0.0`, loses its shuffle-free plans over the column, since Spark
can't tell that it does.
The migration guide describes this under "Upgrading from Spark SQL 4.4 to
4.5".
### How was this patch tested?
New tests:
- **`KeyGroupedPartitioningSuite`**, end to end with the in-memory catalog,
comparing answers and shuffle counts:
- a bucket of a `UTF8_LCASE` and of a `UTF8_BINARY_RTRIM` column, under a
window and a `GROUP BY`;
- a union of two such scans;
- the broadcast join layout over `c.s COLLATE UTF8_BINARY`, with the fact
side bucketed or keyed by a binary column;
- a sign-bit transform of a `DOUBLE` (new `SignBitFunction` fixture),
under `GROUP BY` and `DISTINCT`, plus the broadcast join over `CAST(c.d AS
STRING)`;
- `IN` and `EXISTS` semi-joins over `bucket(n, s)` and over
`(identity(id), bucket(n, s))` in both orders, with and without the subset conf;
- controls that keep 0 shuffles: `identity(s)`, `identity(d)`, a bucket of
a binary string, `(identity(id), identity(s))` under the subset conf, and the
mixed layout joined on `id`.
- **`DistributionSuite`**: `satisfies` per data type, a clustering that
names the transform, `identity`, a cast of a collated column, and the mixed
layout's covering positions.
With the fix reverted, all 7 tests fail. Each part of the change also has a
mutant that a test catches:
- the float clause, by the two float tests;
- the `keysCanSatisfy` condition, by the semi-join test;
- its `isClusterKey(e)` half, by the `(identity(id), identity(s))` control;
- the `createShuffleSpec` intersection, by the mixed-layout semi-join.
Also run:
- catalyst: `ShuffleSpecSuite`, `DistributionSuite`,
`V2ExpressionUtilsSuite`, `InjectRuntimeFilter*`,
`InternalRowComparableWrapperSuite`;
- sql: `KeyGroupedPartitioningSuite`, `EnsureRequirementsSuite`,
`ProjectedOrderingAndPartitioningSuite`, `GroupPartitionsExecSuite`,
`ValidateRequirementsSuite`, `WriteDistributionAndOrderingSuite`,
`PlannerSuite`, `DataSourceV2FunctionSuite`, `BloomFilterAggregateQuerySuite`,
`PerPartitionRuntimeFilterSuite`, `Collation*Suite`, `*DataSourceV2Suite`.
A matrix of 64 SPJ conf combinations over 37 layouts, each result compared
with bucketing disabled, gave 0 wrong results with the fix and 3,072 on master,
and no extra shuffle against master on plain or `identity` layouts.
cc @sunchao @peter-toth @dongjoon-hyun
### Was this patch authored or co-authored using generative AI tooling?
Yes.
Generated-by: Claude Code (Opus 5.5)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]