Repository navigation
[SPARK-62247][SQL] Use the cursor scan for ASOF joins with a tolerance - #60795
Open
LukaZdravic wants to merge 1 commit into
Open
LukaZdravic wants to merge 1 commit into
LukaZdravic wants to merge 1 commit into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
SortMergeAsOfJoinExectook 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:joinAsOfwithtolerancebuilds the as-of conditionl >= r AND r >= l - tolerance(forward:l <= r AND r <= l + tolerance).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.
supportsCursorScantakes 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 internalspark.sql.join.sortMergeAsOfJoin.cursorScan.enabled(defaulttrue) 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.
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. DataFrametolerance = 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 tasksnow counts these joins, and theirspill sizeis 0, since the cursor scan keeps no right rows.How was this patch tested?
SortMergeAsOfJoinScannerSuite: which boundssupportsCursorScantakes 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 withARITHMETIC_OVERFLOWunder ANSI, and matches nothing without ANSI.SortMergeAsOfJoinSuite: the DataFrame tolerance joins against exact rows and random rows, in both scans, a bound given only injoinExprstakes 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 inWholeStageCodegenSuiteassumed that a tolerance keeps the buffered scan; they now check both scans.SortMergeAsOfJoinScannerSuite,SortMergeAsOfJoinSuite,AsOfJoinSortMergeSQLSuite,AsOfJoinSQLSuite,DataFrameAsOfJoinSuite,StreamingAsOfJoinSuite,TimestampNanosJoinAnsiOnSuite,TimestampNanosJoinAnsiOffSuite,WholeStageCodegenSuite,SQLMetricsSuiteandUnionCodegenSuite, once as is and once withSPARK_ANSI_SQL_MODE=false. With ANSI off, oneWholeStageCodegenSuitetest 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.