LukaZdravic opened a new pull request, #60795:
URL: https://github.com/apache/spark/pull/60795

   ### 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.
   ```sql
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to