Skip to content

[SPARK-62247][SQL] Use the cursor scan for ASOF joins with a tolerance - #60795

Open
LukaZdravic wants to merge 1 commit into
apache:masterfrom
LukaZdravic:asof-tolerance-cursor-scan
Open

LukaZdravic wants to merge 1 commit into
apache:masterfrom
LukaZdravic:asof-tolerance-cursor-scan

Conversation

@LukaZdravic

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

SortMergeAsOfJoinExec took its one-pass cursor scan only when the as-of condition was one comparison and there was no residual condition. A tolerance comes in one of two forms, and both took the buffered scan:

  • A DataFrame joinAsOf with tolerance builds the as-of condition l >= r AND r >= l - tolerance (forward: l <= r AND r <= l + tolerance).
  • A SQL ASOF JOIN writes the bound in ON, MATCH_CONDITION (t.ts >= q.ts) ON t.k = q.k AND q.ts >= t.ts - 1000. The bound becomes the residual condition.

Now backward and forward joins take the bound from either place, in both the interpreted and the generated scan. The cursor moves on the comparison of the keys alone, the same as a join without a tolerance. The bound is checked only on the closest right row: every other row that passes the comparison is at least as far from the left key, so it fails the bound whenever the closest row does.

supportsCursorScan takes the bound only when its right key is a column and comes first, and its limit is the left key, or the left key plus or minus a constant (+, -, or a DATE or TIMESTAMP add). In the as-of condition the bound must follow the comparison. In ON it must be the whole residual condition. Strict and inclusive forms are accepted. These keep the buffered scan: nearest joins, a residual with another condition, a bound in both places, a bound that keeps rows far enough away (q.ts <= t.ts - 1000, where the closest row can fail while a farther row passes), a bound written limit first or as a distance, and a bound that casts or computes the right key. There is no new config: the internal spark.sql.join.sortMergeAsOfJoin.cursorScan.enabled (default true) still turns the cursor scan off.

Why are the changes needed?

The buffered scan reads the right key group again from its start for every left row, so its cost grows with the square of the group size.

SELECT count(*) FROM l LEFT ASOF JOIN r
  MATCH_CONDITION (l.lt >= r.rt) ON l.lk = r.rk AND r.rt >= l.lt - 1000

One key group, 80,000 rows per side, local[4], median of 3 runs, before (cursor scan off) and after: SQL backward 15.0 s and 0.08 s, forward 10.5 s and 0.07 s. DataFrame tolerance = lit(1000L): backward 11.6 s and 0.09 s, forward 10.6 s and 0.09 s. With 20,000 rows, SQL backward takes 0.96 s before and 0.06 s after.

Does this PR introduce any user-facing change?

No. The rows, NULL handling and errors stay the same. Only the speed and two metrics change: number of cursor scan tasks now counts these joins, and their spill size is 0, since the cursor scan keeps no right rows.

How was this patch tested?

  • SortMergeAsOfJoinScannerSuite: which bounds supportsCursorScan takes and rejects, in the as-of condition and as the residual condition, and that the cursor evaluates the limit once per left row in both forms.
  • AsOfJoinSortMergeSQLSuite: SQL joins with an ON bound against exact rows, with the cursor scan off and on and the codegen off and on: backward and forward, INNER and LEFT, strict and inclusive, inside, at and past the bound, NULL keys on each side, a key with no right rows, ANSI on and off, and BIGINT, DATE and TIMESTAMP keys with no ON keys. Each ON shape that keeps the buffered scan still returns the right row, for example the farther row for a far-enough bound or a bound on a column the join does not sort on. DOUBLE bounds with an infinite or NaN limit return the same rows in both scans. A limit that overflows fails both scans with ARITHMETIC_OVERFLOW under ANSI, and matches nothing without ANSI.
  • SortMergeAsOfJoinSuite: the DataFrame tolerance joins against exact rows and random rows, in both scans, a bound given only in joinExprs takes the cursor, a tolerance of +Infinity on DOUBLE keys returns the same rows in both scans, and a bound in both places keeps the buffered scan. Its codegen grid test and the ASOF test in WholeStageCodegenSuite assumed that a tolerance keeps the buffered scan; they now check both scans.
  • Ran SortMergeAsOfJoinScannerSuite, SortMergeAsOfJoinSuite, AsOfJoinSortMergeSQLSuite, AsOfJoinSQLSuite, DataFrameAsOfJoinSuite, StreamingAsOfJoinSuite, TimestampNanosJoinAnsiOnSuite, TimestampNanosJoinAnsiOffSuite, WholeStageCodegenSuite, SQLMetricsSuite and UnionCodegenSuite, once as is and once with SPARK_ANSI_SQL_MODE=false. With ANSI off, one WholeStageCodegenSuite test fails (SPARK-60403: MergeRows compiles with its function spilled to a nested class); it checks the generated code size of MergeRows, runs no ASOF join, and fails the same way without this change.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code

This pull request and its description were written by Isaac.

SortMergeAsOfJoinExec took its one-pass cursor scan only when the as-of
condition was one comparison and there was no residual condition. So
a tolerance always took the buffered scan, which reads the key group
again from its start for every left row. A DataFrame joinAsOf with a
tolerance builds the as-of condition `l >= r AND r >= l - tolerance`
(forward: `l <= r AND r <= l + tolerance`). A SQL ASOF JOIN writes
the bound in ON, as in `ON t.k = q.k AND q.ts >= t.ts - 1000`, so
the bound becomes the residual condition.

Backward and forward joins now take the bound from either place. The
cursor moves on the comparison of the keys alone, and the bound is
checked only on the closest right row. Every other row that passes
the comparison is at least as far from the left key, so it fails
the bound whenever the closest row does. The interpreted and the
generated scan both do this.

supportsCursorScan takes the bound only when its right key is a
column and comes first, and its limit is the left key alone, for
a folded tolerance of 0, or the left key plus or minus a constant
(`+`, `-`, or a DATE or TIMESTAMP add). In the as-of condition the
bound must follow the comparison. In ON it must be the whole residual
condition. Both scans then evaluate the limit for the same left rows,
so they return the same rows and the same errors. Strict and inclusive
forms are accepted. Nearest joins, a residual with another condition,
a bound in both places, and a bound that keeps rows far enough away
keep the buffered scan.

One key group, 80,000 rows per side, local[4], before and after:
- SQL ON bound: backward 15.0 s and 0.08 s, forward 10.5 s and 0.07 s.
- DataFrame tolerance: backward 11.6 s and 0.09 s, forward 10.6 s and
  0.09 s.

Co-authored-by: Isaac <no-reply@databricks.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 00:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added the SQL label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants