Commit 8c2dfe6
[SPARK-59054][CORE][SQL] Fix
This PR fixes a correctness bug in the `KeyedPartitioning` shuffle path (storage-partitioned join with `spark.sql.sources.v2.bucketing.shuffle.enabled=true`) by making the partition-key lookup in `KeyGroupedPartitioner` use the exact same equivalence that grouped the partition keys.
- In the `val part` match inside `ShuffleExchangeExec.prepareShuffleDependency`, the driver-side `valueMap` now uses the partitioning's own `InternalRowComparableWrapper`s as keys (`k.partitionKeys.zipWithIndex.toMap`), instead of `Seq[Any]` from `InternalRow.toSeq`.
- In the local `getPartitionKeyExtractor` of that same method, the executor-side lookup key is built by evaluating the bound partition expressions into a reused `GenericInternalRow` and wrapping it with `InternalRowComparableWrapper.getInternalRowComparableWrapperFactory`, instead of `Seq[Any]` from per-row `eval`.
- `InternalRowComparableWrapper` is now `Serializable`: `structType` and `ordering` cannot cross the wire (the ordering may be generated code), so they are `transient` and re-derived from the shared caches on first use after deserialization. This is what allows the wrappers to be shipped to executors inside the partitioner.
- `KeyGroupedPartitioner` (core) takes `Map[Any, Int]` and looks keys up with `getOrElse`, removing the `ArraySeq` normalization and the `getOrElseUpdate` map mutation. Since lookup keys are no longer retained by the map, the executor side can reuse a single row per task without per-record copies.
Because both the map keys and the lookup keys are wrappers over the same data types, lookups share the `RowOrdering` equivalence (binary keys by content, `-0.0 == 0.0`, NaNs equal, collation-aware strings) that grouped and de-duplicated `partitionKeys` on the driver -- by construction, not by approximation.
The `valueMap` keys and the executor-side lookup keys were `Seq[Any]` compared with Scala `==` element equality, while partition-key grouping/de-duplication uses `InternalRowComparableWrapper` (`RowOrdering`) semantics. The two disagree for `BinaryType`: `Array[Byte]` elements are compared by reference, so every lookup misses and falls back to `nonNegativeMod(hashCode, numPartitions)` -- effectively a random partition per row, since `Array` hash codes are identity-based.
As a result, when the non-keyed side of a storage-partitioned join is shuffled into a table partitioned by e.g. `identity(binary_col)`, rows land in partitions that do not match the keyed side, and the join silently drops matches (wrong results, no error).
An earlier revision of this PR used `UnsafeRow`s produced by identical `UnsafeProjection`s as the shared key representation. As pointed out in review, byte equality is strictly finer than the `RowOrdering` equality that grouped `partitionKeys`: a transform whose `resultType()` is floating point can produce `-0.0` and `0.0` partition keys, which the driver collapses into one partition but byte comparison splits, reintroducing the same lost-match failure (and regressing a case the `Seq[Any]` code handled). Using the wrappers themselves eliminates the mismatch by construction.
Note that this bug also exists in the released 4.0, 4.1 and 4.2 lines, which carry the same `KeyGroupedPartitioner` lookup.
Yes, it is a bug fix. Previously, a storage-partitioned join with a `BinaryType` partition key and `spark.sql.sources.v2.bucketing.shuffle.enabled=true` could silently return fewer rows than expected. Now it returns correct results.
Two new regression tests in `KeyGroupedPartitioningSuite`, each exercising both `V2_BUCKETING_SHUFFLE_ENABLED` states (1 shuffle when on, 2 shuffles when off, same results either way):
- `SPARK-59054: shuffle one side: partition keys with binary type`: joins a v2 table partitioned by `identity` on a binary column with an unpartitioned table. Confirmed to fail before the fix (missing join rows) and pass after it.
- `SPARK-59054: shuffle one side: partition transform collapsing -0.0 and 0.0`: uses a new test connector function `signed_zeros` whose `resultType()` (`DoubleType`) differs from its `inputTypes()` (`LongType`), mapping ids 1 and 2 to `-0.0` and `0.0`. Confirmed to fail against the earlier `UnsafeRow`-based revision (one match lost) and pass with the wrapper-based fix. `InMemoryBaseTable` gained the matching transform whitelist entry and `getKey` case.
The full `KeyGroupedPartitioningSuite` (106 tests) passes.
A NaN-key reproduction through ordinary float/double join keys is not reachable, because `NormalizeFloatingNumbers` wraps such join keys in `KnownFloatingPointNormalized(NormalizeNaNAndZero(...))`, which prevents SPJ from triggering; the wrapper-based comparison handles NaN keys correctly regardless.
Generated-by: Claude Fable 5
Closes apache#58345 from dongjoon-hyun/SPARK-59054.
Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
(cherry picked from commit 74b5db8)
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
(cherry picked from commit b46334b)
(cherry picked from commit 6686830)KeyGroupedPartitioner to compare partition keys by value in storage-partitioned join shuffles1 parent 3d70c2c commit 8c2dfe6
6 files changed
Lines changed: 215 additions & 20 deletions
File tree
- core/src/main/scala/org/apache/spark
- sql
- catalyst/src
- main/scala/org/apache/spark/sql/catalyst/util
- test/scala/org/apache/spark/sql/connector/catalog
- core/src
- main/scala/org/apache/spark/sql/execution/exchange
- test/scala/org/apache/spark/sql/connector
- catalog/functions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
144 | 143 | | |
145 | 144 | | |
146 | 145 | | |
147 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
148 | 150 | | |
149 | 151 | | |
150 | | - | |
| 152 | + | |
151 | 153 | | |
152 | 154 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
| 155 | + | |
157 | 156 | | |
158 | 157 | | |
159 | 158 | | |
| |||
Lines changed: 21 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
| 36 | + | |
| 37 | + | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
41 | 57 | | |
42 | 58 | | |
43 | 59 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| |||
221 | 222 | | |
222 | 223 | | |
223 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
224 | 234 | | |
225 | 235 | | |
226 | 236 | | |
| |||
Lines changed: 24 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
| |||
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
33 | | - | |
| 32 | + | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
37 | 36 | | |
38 | 37 | | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
374 | 378 | | |
375 | 379 | | |
376 | 380 | | |
| |||
398 | 402 | | |
399 | 403 | | |
400 | 404 | | |
401 | | - | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
402 | 419 | | |
403 | 420 | | |
404 | 421 | | |
| |||
Lines changed: 128 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
1405 | 1406 | | |
1406 | 1407 | | |
1407 | 1408 | | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
1408 | 1535 | | |
1409 | 1536 | | |
1410 | 1537 | | |
| |||
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
136 | 162 | | |
137 | 163 | | |
138 | 164 | | |
| |||
0 commit comments