[SPARK-60582][SQL] Use resolved keys for DataFrame ASOF sort-merge joins #59834
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 573 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.380
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #59834"
general
weight: 0.35
cloud-fan
What changes were proposed in this pull request?
JIRA: SPARK-60582
Resolve DataFrame ASOF keys directly during analysis and pass those keys to the sort-merge operator. This replaces extracting keys from subtraction expressions and shares key validation and comparison-order helpers with SQL ASOF.
The two DataFrame implementations have different references. The sort-merge path follows SQL ASOF: it uses the same key validation, comparison types, and timestamp ordering as
MATCH_CONDITION. The correlated-subquery implementation is not a reference for it. That implementation derives its distance from subtraction. Subtraction compares STRING keys as numbers (which doesn't work for most strings), and it can overflow on large key gaps. This PR does not align the two implementations in either direction, so some differences are intentional. For example, the sort-merge path rejects constant keys, as SQL does, while the correlated-subquery implementation still accepts them. Apart from the rejection of nearest STRING keys, the correlated-subquery implementation keeps its existing matching behavior.Why are the changes needed?
The existing DataFrame sort-merge path derives its sort keys from subtraction. For STRING keys, subtraction introduces numeric casts, while the matching condition compares the strings as text. For example, text ordering puts
'10'before'9', but numeric ordering reverses them. Sorting and matching can therefore disagree and select the wrong row. With ANSI off, non-numeric strings become NULL in those numeric sort keys.Deriving keys from arithmetic also ties planning to the shape of the subtraction expression and requires a restriction on mixed LTZ/NTZ timestamps. Resolving keys explicitly lets DataFrame ASOF reuse SQL's timestamp handling and preserve the original values needed for exact nearest gaps.
Selecting execution during analysis prevents a later configuration change from sending a plan to an operator for which its keys were not prepared.
Does this PR introduce _any_ user-facing change?
Yes. With
spark.sql.join.sortMergeAsOfJoin.enabled=true:AS_OF_JOIN.UNSUPPORTED_TOLERANCE_KEY_TYPE. Cast the keys to numeric types to use a tolerance.Nearest STRING keys are rejected during analysis with either flag value. The legacy implementation previously attempted implicit numeric conversion (which doesn't work for most strings); callers that want numeric distance must cast their keys explicitly.
The DataFrame flag remains off by default. The legacy implementation retains subtraction-based matching. Changing the flag after a DataFrame is analyzed no longer changes its execution strategy.
How was this patch tested?
The latest run passed 201 tests across:
RewriteAsOfJoinSuiteSortMergeAsOfJoinSuiteDataFrameAsOfJoinSuiteAsOfJoinSQLSuiteNew regressions cover both analyzers, numeric and decimal keys, STRING ordering and collations, TIME keys, explicit casts, mixed timestamps across DST, composite keys, strategy selection, reversed-key tolerance windows, and tolerance error precedence. Flag-off regressions cover nearest STRING rejection and keeping the correlated-subquery strategy after the flag is enabled.
Additional runs during implementation covered Connect
joinAsOf, scanner and key-type suites, and SQL ASOF execution/analyzer goldens. Catalyst and SQL scalastyle checks passed.Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex CLI 0.164.0