This is an automated email from the ASF dual-hosted git repository.
peter-toth pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new c0566fedecd2 [SPARK-60460][4.1][SQL][FOLLOWUP] Shuffle nested SPJ
children whose transform functions differ
c0566fedecd2 is described below
commit c0566fedecd29da68d796e9cd34f3cf586f1c313
Author: Ammar Chalifah <[email protected]>
AuthorDate: Wed Nov 11 19:54:42 2026 +0100
[SPARK-60460][4.1][SQL][FOLLOWUP] Shuffle nested SPJ children whose
transform functions differ
### What changes were proposed in this pull request?
When a nested storage-partitioned join refuses to push its outer join's
values, `matchedIndexes` in `EnsureRequirements` still treats two children as
compatible if their raw partition values match. Compatible transforms can
report the same raw values for different keys, for example `bucket(12, id)` and
`bucket(8, id)`, so a child can be left unshuffled while its rows land in
different partitions.
This PR keeps a child in `matchedIndexes` only when its matching member
uses the same transform function as the best spec. A collection matches when
one of its members does. The check uses `TransformExpression.isSameFunction`.
### Why are the changes needed?
On `branch-4.1`, `pa12 JOIN pd JOIN pb8` on `id` returns 8 of 9 rows with
AQE on and off. `pa12` is `bucket(12, id)`, `pb8` is `bucket(8, id)`, and `pd`
is unpartitioned. The ids are 0 to 7 and 12. Id 12 is dropped, with no error.
The same drop happens with the nested join on the right, `pa12 JOIN (pb8 JOIN
pd)`. On `branch-4.0` the left-nested query fails at planning or execution, so
the 4.1 result is silently wrong.
This was reported in review of
[#60295](https://github.com/apache/spark/pull/60295).
[SPARK-60460](https://issues.apache.org/jira/browse/SPARK-60460) is already
fixed for 4.1.5 by [#60058](https://github.com/apache/spark/pull/60058), which
is not released yet. The `branch-4.0` backport of that fix needs this follow-up.
### Does this PR introduce _any_ user-facing change?
Yes, for storage-partitioned joins on `branch-4.1` (4.1.x). A nested join
whose outer push is refused now shuffles a child whose transform function
differs from the best spec. The queries above return all 9 rows. Plans whose
children use the same function are unchanged.
### How was this patch tested?
`SPARK-60460: nested SPJ shuffles when compatible transforms share
partition values` in `KeyGroupedPartitioningSuite` runs three queries, with AQE
on and off. Each asserts all 9 rows and two shuffles:
- `pa12 JOIN pd JOIN pb8`, and the same join with `pb8` first. The nested
join is the best spec, so the `ShuffleSpecCollection` arm runs.
- `pa12 JOIN (pb8 JOIN pd)`. The plain scan is the best spec, so the other
collection arm runs.
Ran `build/sbt 'sql/testOnly
org.apache.spark.sql.connector.KeyGroupedPartitioningSuite -- -z "share
partition values"'` on this branch: 1 test, passed.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Grok 4.7
Closes #60334 from ammarchalifah/SPARK-60460-follow-up.
Authored-by: Ammar Chalifah <[email protected]>
Signed-off-by: Peter Toth <[email protected]>
---
.../execution/exchange/EnsureRequirements.scala | 16 +++++++-
.../connector/KeyGroupedPartitioningSuite.scala | 47 ++++++++++++++++++++++
2 files changed, 62 insertions(+), 1 deletion(-)
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/EnsureRequirements.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/EnsureRequirements.scala
index b10c467e47da..de8d0bd308af 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/EnsureRequirements.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/EnsureRequirements.scala
@@ -211,8 +211,22 @@ case class EnsureRequirements(
// qualify. Only the branch that shuffles a child reads these, hence
`lazy`.
// A child with a member built from a reduced scan only looks compatible
on the transform
// the scan still reports, so it is shuffled onto the best spec instead.
+ // Compatible transforms can report the same raw partition values for
different keys.
+ // `isCompatibleWith` accepts that, so a child whose member uses another
function is
+ // shuffled onto the best spec instead.
+ def sameFunctions(a: ShuffleSpec, b: ShuffleSpec): Boolean = (a, b)
match {
+ case (x: KeyGroupedShuffleSpec, y: KeyGroupedShuffleSpec) =>
+ x.isCompatibleWith(y) &&
+ x.partitioning.expressions.zip(y.partitioning.expressions).forall {
+ case (l: TransformExpression, r: TransformExpression) =>
l.isSameFunction(r)
+ case _ => true
+ }
+ case (ShuffleSpecCollection(xs), _) => xs.exists(sameFunctions(_, b))
+ case (_, ShuffleSpecCollection(ys)) => ys.exists(sameFunctions(a, _))
+ case _ => a.isCompatibleWith(b)
+ }
lazy val matchedIndexes = bestSpecOpt.toSeq.flatMap { best =>
- childrenIndexes.filter(i => best.isCompatibleWith(specs(i)) &&
+ childrenIndexes.filter(i => sameFunctions(best, specs(i)) &&
!hasMemberBuiltFromReducedScan(children(i), specs(i)))
}
lazy val bestMemberOpt = bestSpecOpt.flatMap { best =>
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala
b/sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala
index 4d0288cb3375..ca1a547195a1 100644
---
a/sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala
+++
b/sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala
@@ -4581,6 +4581,53 @@ class KeyGroupedPartitioningSuite extends
DistributionAndOrderingSuiteBase {
}
}
+ test("SPARK-60460: nested SPJ shuffles when compatible transforms share
partition values") {
+ val idCol = Array(Column.create("id", LongType))
+ createTable("pa12", idCol, Array(bucket(12, "id")))
+ createTable("pd", idCol, Array.empty)
+ createTable("pb8", idCol, Array(bucket(8, "id")))
+ // 12 is bucket 0 on pa12 and bucket 4 on pb8. The other ids make both
scans report 0-7.
+ val ids = (0L to 7L) :+ 12L
+ Seq("pa12", "pd", "pb8").foreach { t =>
+ sql(s"INSERT INTO testcat.ns.$t VALUES ${ids.map(i =>
s"($i)").mkString(", ")}")
+ }
+
+ // The two partitioned scans report the same raw values and tie on
partition count, so the
+ // first child becomes the best spec. A nested join on the left makes that
spec a collection.
+ // A nested join on the right makes the plain scan the best spec. Both
arms have to shuffle.
+ val queries = Seq(
+ """
+ |SELECT /*+ MERGE(pa12, pd, pb8) */ pa12.id, pd.id, pb8.id
+ |FROM testcat.ns.pa12 JOIN testcat.ns.pd ON pa12.id = pd.id
+ |JOIN testcat.ns.pb8 ON pa12.id = pb8.id
+ |""".stripMargin,
+ """
+ |SELECT /*+ MERGE(pb8, pd, pa12) */ pb8.id, pd.id, pa12.id
+ |FROM testcat.ns.pb8 JOIN testcat.ns.pd ON pb8.id = pd.id
+ |JOIN testcat.ns.pa12 ON pb8.id = pa12.id
+ |""".stripMargin,
+ """
+ |SELECT /*+ MERGE(pa12, pb8, pd) */ pa12.id, pb8.id, pd.id
+ |FROM testcat.ns.pa12 JOIN (testcat.ns.pb8 JOIN testcat.ns.pd ON
pb8.id = pd.id)
+ |ON pa12.id = pb8.id
+ |""".stripMargin)
+
+ Seq(true, false).foreach { aqe =>
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> aqe.toString,
+ SQLConf.V2_BUCKETING_SHUFFLE_ENABLED.key -> "true",
+ SQLConf.V2_BUCKETING_ALLOW_COMPATIBLE_TRANSFORMS.key -> "true") {
+ queries.foreach { query =>
+ val df = sql(query)
+ checkAnswer(df, ids.map(i => Row(i, i, i)))
+ assert(collectAllShuffles(df.queryExecution.executedPlan).size == 2,
+ s"expected two shuffles, got:\n${df.queryExecution.executedPlan}")
+ }
+ }
+ }
+ }
+
test("SPARK-48012: one-side shuffle with partition transforms") {
val items_partitions = Array(bucket(2, "id"), identity("arrive_time"))
val items_partitions2 = Array(identity("arrive_time"), bucket(2, "id"))
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]