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

   ### What changes were proposed in this pull request?
   
   `SortMergeAsOfJoinExec` chose what a backward or a forward scan does in 
three places: the interpreted scans of `SortMergeAsOfJoinScanner`, and the two 
generated scans (cursor and buffered). This PR writes those choices once.
   
   - Adds `AsOfScanStrategy` (`private[joins]`), with `LastMatch` for a 
backward join and `FirstMatch` for a forward join. It holds the two choices 
that depend on the direction:
     - `firstMatchWins`: whether the first or the last right row that passes is 
the closest to the left key.
     - `matchOutlivesLeftRow` (cursor scan only): whether a right row that 
passes for one left row also passes for every later one. It decides whether the 
scan stops at a failing row or drops it, and whether it checks the kept row 
again.
   - `AsOfScanStrategy.of(direction)` is an exhaustive match that returns 
`None` for a nearest join. `AsOfScanStrategy(direction)` throws an internal 
error for a direction without a strategy.
   - The generated cursor scan, the generated buffered scan and the interpreted 
cursor scan read the strategy, instead of `direction match { ... case _ => }` 
and `direction == Forward` checks.
   - `supportCodegen` asks whether the direction has a strategy, instead of 
`direction != Nearest`.
   - The interpreted `findLastBackward` and `findFirstForward`, which differed 
in one line, become one `findInOrder`.
   
   The scan loops are still written twice, once in Scala and once as generated 
Java, but both read the same named choices.
   
   ### Why are the changes needed?
   
   The two generated scans matched on the direction with `case _`, so a nearest 
join would have silently got the backward scan and returned the last passing 
row instead of the nearest one. It cannot get there today, because 
`supportCodegen` is false for a nearest join and `supportsCursorScan` rejects 
it, but nothing in the generated scans said so. The interpreted and the 
generated scans could also drift apart, since each spelled out the choices of a 
direction on its own.
   
   Now a direction that a scan does not support fails with an internal error, 
and a new direction does not compile until `AsOfScanStrategy.of` handles it.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Two details change without changing any result:
   
   - The generated buffered scan of a forward join returns its `best` variable, 
which is always null there, where it returned a null literal.
   - A nearest join whose scanner or codegen stage is built by hand, which the 
planner never does, fails with an internal error instead of running the scan of 
another direction.
   
   ### How was this patch tested?
   
   New tests:
   
   - `SortMergeAsOfJoinSuite`: for each direction and for inner and left outer 
joins, a differential test runs 32 cases (equi-key, residual condition, 
tolerance, `allowExactMatches`, cursor scan on and off) with the ASOF join 
codegen off and on, and checks that the generated scan returns the rows of the 
interpreted one. The data has ties on the as-of key, NULL as-of keys, a NULL 
equi-key and groups that only one side has.
   - `SortMergeAsOfJoinSuite`: a nearest join copied with `codegenEnabled = 
true` has `supportCodegen == false`, and a codegen stage built by hand around 
it fails with `INTERNAL_ERROR`.
   - `SortMergeAsOfJoinScannerSuite`: the strategy of each direction, and a 
nearest scanner built with the cursor scan fails with an internal error.
   
   Existing suites: `SortMergeAsOfJoinSuite`, `SortMergeAsOfJoinScannerSuite`, 
`AsOfJoinSortMergeSQLSuite`, `DataFrameAsOfJoinSuite` and `AsOfJoinSQLSuite`.
   
   ### 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