← Back to Pull requests

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

Open
unknown opened this pull request with 546 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 31, 2026 04:30 UTC
Author

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. If the planner cannot resolve the key’s type, it skips the reduction and continues planning the native join.

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.

How are these changes tested?

All five focused CometJoinSuite tests passed on both Spark 3.5.9 (JDK 17) and Spark 4.1.3 (JDK 21) using the release JNI built with this PR’s Cargo.lock. One test exercises a BIGINT key with a literal fallback and duplicate left rows. Another joins on two nullable INT columns and checks the matches for a non-NULL first key, a fallback key and two NULLs. All 24,576 right rows, including the 8,192 rows with both keys NULL, reached the reducer. With the pre-PR native library, this test returned the same SQL result and failed the reducer-input assertion. The admission test also covers a cast inside the coalesce.

Both native planner tests and all four membership reducer tests passed. The computed-key test checks the projection under the reducer and verifies that the right sort and join use the projected key. It also checks that a computed Boolean key keeps the original sort input. Rust formatting, Maven Spotless, and git diff --check passed.

Benchmark

Full local Spark SQL action time on 13fd925bb5, from query construction through collection. Each pair below is reduction OFF → ON, in milliseconds. A and B are separate JVMs with opposite run orders. Each value is the median of six actions after two warmups per setting and input shape.

| Right rows | Distinct computed keys | Right rows sorted, OFF → ON | JVM A, ms | JVM B, ms |
|---:|---:|---:|---:|---:|
| 1,048,576 | 128 | 1,048,576 → 37,260 | 264.29 → 270.64 | 408.38 → 439.78 |
| 1,048,576 | 1,048,576 | 1,048,576 → 1,048,576 | 300.78 → 286.38 | 497.05 → 480.88 |
| 8,388,608 | 128 | 8,388,608 → 66,528 | 551.37 → 345.76 | 471.64 → 321.12 |
| 8,388,608 | 8,388,608 | 8,388,608 → 8,388,608 | 636.66 → 653.20 | 677.02 → 639.21 |

With 8,388,608 right rows and 128 keys, enabling reduction cut the right sort to 66,528 rows. Elapsed time fell 37.3% and 31.9% in the two JVMs, and all 12 measured pairs were faster. With 1,048,576 rows and the same 128 keys, the sort also shrank, but median action time rose 2.4% and 7.7%. With unique keys, every row bypassed reduction. The smaller unique case was faster in both JVMs, and the larger one had mixed timings.

All 128 actions returned the same count and sum. Each executed a fresh native shuffle and left-semi sort-merge join, with zero right-sort spills.

<details>
<summary>Benchmark workload and settings</summary>

Spark 3.5.9, JDK 17.0.21, four pinned AMD EPYC Milan CPUs, local[4], 4 GiB heap, 1 GiB off-heap, fair unified pool, four shuffle partitions and 8,192-row batches. The local release JNI used LTO off and 16 codegen units. Native direct shuffle reads were enabled. AQE, automatic broadcast and exchange reuse were disabled. The four-file Parquet inputs were written before timing.

SELECT /*+ MERGE(r) */ count(*), sum(p.event_id)
FROM probe p LEFT SEMI JOIN build r ON p.k = coalesce(r.a, r.b)

The left input has 131,072 rows (event_id from zero). Every eighth row has a key above the right-side range, and other keys are (event_id 48271) % 128. For right-side row i, the integer key is (i 48271) % distinct_keys. Every third row puts that key in b and a NULL in a. All other rows put it in a and a NULL in b. Each result was checked against [114688, 7516078080], along with the shuffle, sort, and reducer counters.

</details>

Diff: 546 lines (+409 / -137) across 6 files Reviewer: andygrove Review duration: 5928m
All Vouch review confidence checks passed
Residual risk 0.27 = Change Risk (0.48) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 546 lines diff across 6 files
0.48
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
42%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.377
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #7361"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 43.0% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 48% (MEDIUM) · Higher = larger blast radius
48%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 43.0% · Lower = rushed or shallow review
43.0%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 27.4% · Safe to merge
27.4%