You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "ulysses-you (via GitHub)" <gi...@apache.org> on 2026/07/23 10:30:23 UTC

[PR] [SPARK-58290][SQL] Fix O(n^2) partition index access in ShufflePartitionsUtil.coalescePartitionsWithSkew [spark]

ulysses-you opened a new pull request, #57459:
URL: https://github.com/apache/spark/pull/57459

   ### What changes were proposed in this pull request?
   
   In `ShufflePartitionsUtil.coalescePartitionsWithSkew`, the local `partitionIndices` is materialized from `inputPartitionSpecs`, whose inner collection is a `List` in practice. The coalescing loop then repeatedly accesses this sequence by index (`partitionIndices(i - 1)`, `partitionIndices(i)`, and the inner skew-section scan `partitionIndices(repeatIndex)`). Because `List.apply(i)` is O(i), the loop degrades to O(n^2) in the number of shuffle partitions.
   
   This PR calls `.toArray` on `partitionIndices` so all indexed accesses become O(1), restoring the loop to O(n). The change is local; all downstream usages (`.head`, `.last`, indexed access, `.length`) are valid on `Array`, and the sequence is only read.
   
   ### Why are the changes needed?
   
   A production job hung, and the driver thread dump showed it stuck in `ShufflePartitionsUtil.coalescePartitionsWithSkew`, spinning inside `List.drop` / `List.apply` (indexed list access):
   
   ```
   scala.collection.immutable.List.drop(List.scala:79)
   scala.collection.LinearSeqOps.apply(LinearSeq.scala:130)
   scala.collection.immutable.List.apply(List.scala:79)
   org.apache.spark.sql.execution.adaptive.ShufflePartitionsUtil$.coalescePartitionsWithSkew(ShufflePartitionsUtil.scala:166)
   org.apache.spark.sql.execution.adaptive.ShufflePartitionsUtil$.coalescePartitions(ShufflePartitionsUtil.scala:75)
   ```
   
   With a large number of shuffle partitions (skew join scenarios often produce many), the O(n^2) behavior can hang the driver during AQE planning.
   
   Local micro-benchmark driving `coalescePartitions` on the skew path, `List` vs `Array` (same machine, same inputs):
   
   | partitions | List (ms) | Array (ms) |
   |-----------:|----------:|-----------:|
   |       5000 |        72 |       <1   |
   |      10000 |       291 |       <1   |
   |      20000 |      1166 |        1   |
   
   The `List` version's per-element cost grows linearly with partition count (confirming O(n^2)), while the `Array` version stays flat (O(n)).
   
   ### Does this PR introduce _any_ user-facing change?
   
   No.
   
   ### How was this patch tested?
   
   Existing tests, all passing locally (39 tests):
   
   ```
   build/sbt "sql/testOnly *ShufflePartitionsUtilSuite *CoalesceShufflePartitionsSuite"
   ```
   
   This is a pure performance fix with no behavior change.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 4.8)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58290][SQL] Fix O(n^2) partition index access in ShufflePartitionsUtil.coalescePartitionsWithSkew [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #57459:
URL: https://github.com/apache/spark/pull/57459#issuecomment-5058725715

   **Merge Summary:**
   - merged into master https://github.com/apache/spark/commit/3082cdfda92f4507542ccbf99cf777a51f29414c
   - merged into branch-4.x https://github.com/apache/spark/commit/f52852becd4cdcc4e13d2ef85a9b9fc2f8e62ee2
   
   *Posted by `merge_spark_pr.py`*


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58290][SQL] Fix O(n^2) partition index access in ShufflePartitionsUtil.coalescePartitionsWithSkew [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #57459: [SPARK-58290][SQL] Fix O(n^2) partition index access in ShufflePartitionsUtil.coalescePartitionsWithSkew
URL: https://github.com/apache/spark/pull/57459


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58290][SQL] Fix O(n^2) partition index access in ShufflePartitionsUtil.coalescePartitionsWithSkew [spark]

Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on PR #57459:
URL: https://github.com/apache/spark/pull/57459#issuecomment-5058211203

   alco cc @dongjoon-hyun  @cloud-fan @viirya  thank you


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org