Skip to content

perf: reduce repeated coalesced integer keys before semi-join sorting - #7361

Open
sunchao wants to merge 3 commits into
apache:mainfrom
sunchao:sunchao/oss-computed-semi-key
Open

sunchao wants to merge 3 commits into
apache:mainfrom
sunchao:sunchao/oss-computed-semi-key

Conversation

@sunchao

@sunchao sunchao commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Part of #7189. This extends #7190.

Rationale for this change

A left-semi join such as l.id = coalesce(r.primary_id, r.backup_id) only needs to know whether each right-side ID exists. The native sort currently receives every right row, even when many rows produce the same ID.

What changes are included in this PR?

With spark.comet.exec.sortMergeJoin.rightKeyReduction.enabled=true, the native right sort can now reduce one integer coalesce key over same-type columns and literals. A projection computes the key once before reduction. The sort and join read that projected column. The existing reducer still decides when to bypass a batch.

The option defaults to false. As in #7190, it applies to an unfiltered left-semi join with a right sort directly over a Comet shuffle. Casts and arithmetic inside the coalesce fall outside the supported expression shape. If the planner cannot resolve the key’s type, it skips the reduction and continues planning the native join. The rewrite also checks SortExec::fetch() on the physical sort. For sorted keys [1, 1, 2] and a fetch of two rows, deduplication before the fetch would incorrectly admit key 2.

How are these changes tested?

  • CometJoinSuite covers all four signed integer widths. The two-column INT case matches keys 5 and 7 and exercises an all-NULL row. The TINYINT and SMALLINT cases add a typed literal as the third operand, so the all-NULL row matches -1. Each case checks the planned key type, all 24,576 rows entering reduction, and fewer rows leaving it. The BIGINT case covers a literal fallback and duplicate left rows. The admission test also covers a cast inside the coalesce.
  • The native planner tests check that the reducer reads from a projection and that both the right sort and the join use the projected column. They also cover a computed Boolean key, a fetch, and a skip.
  • On 13fd925bb5, the five focused join tests passed on Spark 3.5.9 and 4.1.3, along with both native planner tests and all four reducer tests. With the pre-PR JNI the INT test returned the correct SQL result and failed the reducer-input assertion. Earlier validation
  • On e990b40a9c, the benchmark checked the expected count and sum [114688, 7516078080], native plan and row counts on Spark 3.5.9 and 4.1.3. Rust formatting, Maven Spotless and git diff --check passed. The Rust and Comet test suites run in the fork. Fork CI · Apache CI

Benchmark

CometSortMergeJoinBenchmark now includes the four workloads below. All three modes shuffle the same two nullable INT columns, partitioned by coalesce(a, b). Baseline carries both columns into the sort. Projection computes the key once after the shuffle and sorts every row of that one column. Reduction also removes repeated keys before sorting. Comparing reduction with projection shows the effect of enabling the reducer after narrowing the sort input. Benchmark source

Each action time covers spark.sql(query) through collect(). Within each JVM, two warmups per mode precede six measured rounds per workload. Each round runs all three modes, with each of the six possible orders used once. Three fresh JVMs run the workloads in table order and three reverse the workload and round orders. Each pair of times is forward / reverse on Spark 3.5.9. Each value is the median of three per-JVM medians. The last column is right_key_reduction_time with reduction enabled, summed across tasks per action. It times attempts to deduplicate batches, including attempts that end in a bypass, and excludes projection and packing small batches. Timer scope

Right rows Distinct keys Rows sorted with reduction Baseline (ms) Projection (ms) Reduction (ms) Reducer (task ms)
1,048,576 128 37,260 242.73 / 132.64 249.22 / 131.97 217.73 / 121.79 7.36 / 4.32
1,048,576 1,048,576 1,048,576 192.38 / 158.39 195.26 / 157.38 192.21 / 155.22 0.14 / 0.14
8,388,608 128 66,528 412.47 / 410.59 369.01 / 369.78 254.26 / 274.44 34.76 / 34.41
8,388,608 8,388,608 8,388,608 548.15 / 624.50 496.28 / 584.31 489.49 / 591.91 0.15 / 0.17
  • 8,388,608 rows, 128 keys. After projection alone, the sort still receives 8,388,608 rows. Reduction cuts that to 66,528 and beats projection in all 36 rounds. Median action time falls another 31.1% forward and 25.8% in reverse relative to projection, for a total improvement of 38.4% and 33.2% over baseline.
  • 1,048,576 rows, 128 keys. The earlier 2.4% and 7.7% slowdowns varied across fresh JVMs. In the one JVM that reproduced a slowdown, the medians were 231.69 ms at baseline, 260.47 ms with projection, and 240.09 ms with reduction (+3.6% over baseline). The slowdown was already present with projection alone. Across the six JVMs, reduction beat baseline in 30 of 36 rounds and projection in 28 of 36. This workload ran first in the forward order and last in reverse, with a large difference in absolute times for all three modes.
  • Unique keys. Every row bypasses reduction. For 8,388,608 rows, projection alone improves on baseline by 9.5% forward and 6.4% in reverse. The reducer beats projection in 17 of 36 rounds and spends 0.15–0.17 task ms on deduplication attempts. The projection control accounts for the consistent gain on this input. For 1,048,576 rows, reduction beats baseline in 16 of 36 rounds.

Across the six Spark 3.5.9 JVMs, all 576 actions passed the result and plan checks and all 432 measured right sorts had zero spills.

Reproduce the workload

The probe has 131,072 rows. Every eighth key is above the build range and the rest use (event_id * 48271) % 128. Build row i has key (i * 48271) % distinct_keys, in b on every third row and a otherwise, with the other column NULL. The benchmark writes four Parquet files per input before timing. It uses four native shuffle partitions, direct reads, 8,192-row batches and the fair unified memory pool. AQE, broadcast and exchange reuse are disabled.

The control uses DISTRIBUTE BY coalesce(a, b) to place its projection after the shuffle. With constraint propagation enabled, Catalyst would infer an extra right-side null filter for the control's key alias. All three modes disable constraint propagation and specify the same probe filter. In the Spark 3.5.9 plans for the original and revised SQL at 1,048,576 rows and 128 keys, the scans, filters, hash partitioning and join matched for both flag settings. The right shuffle origin changed from ENSURE_REQUIREMENTS to REPARTITION_BY_COL. The control expresses the projection as a Spark-visible Comet operator. The enabled planner inserts a native projection. Both use DataFusion ProjectionExec.

The six JVMs invoked the benchmark main class directly with the Maven test classpath. They used JDK 17.0.21, four pinned AMD EPYC Milan CPUs, a 4 GiB heap, 1 GiB off-heap and the release JNI built with the checked-in lockfile. From the repository root with JDK 17 active, run the following commands on Linux, selecting four available CPUs for taskset.

export PROFILES=-Pspark-3.5
export CARGO_PROFILE_RELEASE_LTO=false CARGO_PROFILE_RELEASE_CODEGEN_UNITS=16 CARGO_PROFILE_RELEASE_DEBUG=0
make release

export SPARK_LOCAL_IP=127.0.0.1
export COMET_BENCHMARK_MASTER='local[4]' BENCH_HEAP=4g
export JAVA_TOOL_OPTIONS='-Dspark.memory.offHeap.enabled=true -Dspark.memory.offHeap.size=1g'
suite=benchmark-org.apache.spark.sql.benchmark.CometSortMergeJoinBenchmark
taskset -c 0-3 make -o release "$suite" -- --coalesced-keys-only
taskset -c 0-3 make -o release "$suite" -- --coalesced-keys-only --reverse

Repeat the last two commands for each pair of JVMs. The output includes the six raw action times per mode, medians, sort row counts and spills.

@sunchao sunchao added the run-spark-4.1-tests Run the Spark 4.1 SQL tests on this pull request instead of waiting for the merge queue label Oct 31, 2026
@github-actions github-actions Bot added enhancement New feature or request performance labels Oct 31, 2026
@sunchao
sunchao marked this pull request as ready for review October 31, 2026 04:37

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for extending the reducer to computed keys. I compared against branch-1.2 and the rightKeyReduction config does not exist there, so nothing changes for users upgrading from the release. The default is off, so the new path is opt-in. I read the planner rewrite closely and did not find a correctness problem. The projection is built before the join key is rewritten, so an early return cannot leave join_on half-changed. The new SortExec keeps preserve_partitioning. The left-semi join with no filter needs no other right columns, so narrowing the right schema to the projected key is safe. CI is green, including the Spark 4.1 label run.

I have three asks on coverage and robustness.

  1. The native test in native/core/src/execution/planner/tests/semi_join_reduction.rs only builds bare Column keys. The new ProjectionExec branch in reduce_before_sort is covered only through CometJoinSuite. Could you add a native case with a non-Column integer key, such as a coalesce over a bound reference and a literal? It could assert that the reducer sits under the sort, that a projection sits under the reducer, and that the join key becomes membership_key. A negative case with a non-integer key would also help.

  2. The new CometJoinSuite query uses a BIGINT column with a literal fallback. Could you add a variant like your motivating example, coalesce(r._1, r._2) over two nullable columns? It could include rows where both are null, so the projected key is null and must not match any left row. It would also be good to run it on an INT column, since the Scala gate admits every IntegralType and the native check accepts Int8 through Int64.

  3. In reduce_before_sort, key.data_type(&schema)? would fail the whole query if the key's type can't be resolved. The rest of that guard skips reduction when something doesn't fit. Should this return Ok(None) instead, so the function keeps its "check, then skip" contract?

@sunchao

sunchao commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Thanks @andygrove. I addressed all three points in 13fd925bb5.

  1. The native planner test now builds coalesce(k, 0) and checks that the right sort reads from the reducer, the reducer reads from the projection, and the sort and join both use the projected membership_key. A computed Boolean key keeps the original sort over the scan.
  2. The new Spark test joins against coalesce(r._1, r._2) on two nullable INT columns. Only left keys 5 and 7 match. All 24,576 right rows reach the reducer, including 8,192 with both columns NULL. The left-side zero also catches an accidental NULL-to-zero match.
  3. If the planner cannot resolve the computed key’s type, it now skips the reduction. The ordinary native join planner still validates the join.

Both native planner tests and all four membership reducer tests passed. All five focused join tests passed on Spark 3.5.9 and 4.1.3. I also refreshed the benchmark table in the PR description.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sunchao for extending the reducer to computed keys. I did not find a correctness problem for the gated shape: LeftSemi with no filter and one key, NullEqualsNothing keeps a NULL key from matching, and the reducer keeps one NULL row per batch.

The numbers in the description come from a workload that is not in the repo. Could CometSortMergeJoinBenchmark's right key reduction section get a coalesce case so the table can be reproduced? The unique-key rows also get faster with reduction on although every row bypasses the reducer, so I expect the gain there comes from projecting the sort input to one column, not from reduction. A projection-only variant, or the right_key_reduction_time metric, would show where the 2.4% and 7.7% slowdowns at 1,048,576 rows with 128 keys come from.

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR backlog is huge and the maintainers don't have the bandwidth to review them all, so here is an automated review. Feel free to ignore it.

Thanks for extending the reducer to computed keys. I found no blocking concerns in the head commit 13fd925bb5. I read reduce_before_sort in native/core/src/execution/planner/join.rs against the Scala gate in CometSortMergeJoinExec.canReduceRightKey. The projection and the rewrite of join_on happen only after every skip check has passed, so an early return cannot leave the join half-changed. The Scala gate admits only Attribute and Literal operands, so casts and arithmetic never reach the native projection. A left-semi join with no filter reads no other right columns, so narrowing the right side to membership_key is safe. The new key.data_type(&schema) check now skips reduction instead of failing the plan, which addresses the earlier robustness question.

I compared against the latest release branch. The rightKeyReduction config is not there, and it defaults to false here, so a user upgrading sees no change in results, fallbacks or performance unless they opt in.

The remaining asks in the existing threads on SortExec fetch, the TINYINT and SMALLINT coverage, and the benchmark reproduction are the ones I would want addressed before merge. I have nothing to add beyond them.

Check fetch on the physical sort before rewriting it. Deduplicating
[1, 1, 2] below a fetch of two would bring key 2 into the join.

Exercise both nullable columns and a typed literal for TINYINT and
SMALLINT, checking the key type, result, and reducer row counts.

Compare the original sort, a post-shuffle projection, and projection
with reduction over repeated and unique keys. Interleave the cases,
check the native plan and result, and report time spent attempting
reduction alongside action time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request performance run-spark-4.1-tests Run the Spark 4.1 SQL tests on this pull request instead of waiting for the merge queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants