← Back to Pull requests

[SPARK-60582][SQL] Use resolved keys for DataFrame ASOF sort-merge joins #59834

Closed
unknown closed this pull request without merging on Oct 29, 2026 16:32 UTC
View on GitHub
unknown unknown commented on Oct 27, 2026 14:56 UTC
Author

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.

  • Resolve keys in both the fixed-point and single-pass analyzers, including Connect expressions. Preserve explicit casts and decimal values used for nearest matching.

  • Use text ordering for backward/forward STRING keys and support TIME keys. Reuse SQL ASOF timestamp ordering for mixed LTZ/NTZ keys, including composite keys.

  • Select the DataFrame execution strategy during analysis and store the decision on the logical node. Build a distance expression only for the correlated-subquery implementation. Remove optimizer reanalysis, configuration replay, and obsolete key-extraction code.

  • Share tolerance validation so both analyzers report the same error when the tolerance and keys are invalid.
  • 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:

  • Backward/forward STRING matching uses text ordering and the keys' collation; TIME keys are supported.

  • Mixed LTZ/NTZ keys are accepted, including supported composite keys.

  • Constant keys and keys with incompatible collations are rejected during analysis.

  • A tolerance on two STRING keys is rejected with 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:

  • RewriteAsOfJoinSuite

  • SortMergeAsOfJoinSuite

  • DataFrameAsOfJoinSuite

  • AsOfJoinSQLSuite
  • New 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

    Diff: 573 lines (+429 / -144) across 7 files Reviewer: cloud-fan Review duration: 2975m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.380
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #59834"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 19.7% · Safe to merge
    19.7%
    Repository