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

   ### What changes were proposed in this pull request?
   `InferFiltersFromConstraints` now handles `AsOfJoin`:
   - The right side gets the filters that follow from the left side's 
constraints and the `ON` condition, for both inner and left outer as-of joins.
   - An inner as-of join also gets the filters that follow from the right side 
and the `ON` condition on its left side.
   - A left outer as-of join keeps its left side as is, because it keeps every 
left row.
   
   Only the `ON` condition is used, not `MATCH_CONDITION`. A filter that can 
fail is not added, by the same check that normal joins use.
   
   ### Why are the changes needed?
   A right row that fails the `ON` condition is never the match of any left 
row. But no filter was inferred across an `AsOfJoin`, so a filter on the left 
side's `ON` key did not reach the right side.
   
   ```sql
   SELECT * FROM trades t LEFT ASOF JOIN quotes q
     MATCH_CONDITION (t.ts >= q.ts) ON t.cust = q.cust
   WHERE t.cust % 100 = 7
   ```
   
   Before: `t.cust % 100 = 7` filters only `trades`, so every `quotes` row is 
shuffled and sorted. After: `quotes` also gets `q.cust % 100 = 7` and `q.cust 
IS NOT NULL` before the join. A quote with another `cust` can never match a 
trade, so the result stays the same.
   
   A local run with 20M rows on each side (local[16]) took this query from 2.2 
s to 0.39 s. The same query without a `WHERE` takes the same time as before.
   
   ### Does this PR introduce _any_ user-facing change?
   No. Query results stay the same. Only the plan changes.
   
   ### How was this patch tested?
   - New `InferFiltersFromConstraintsSuite` tests check which side gets which 
filter for inner and left outer as-of joins, the filters on NULL `ON` keys, and 
that a null-safe `<=>` key keeps its NULL rows.
   - New `AsOfJoinSortMergeSQLSuite` tests on parquet tables check the results 
and the scan filters. They also check that a filter that can fail is not added: 
`CAST(r.v AS INT) >= 0` would fail on `'abc'`.
   - Five existing cursor-scan tests send NULL `ON` keys to the join. They now 
exclude `InferFiltersFromConstraints`, so the NULL rows still reach the join. 
Without that, one of them reads on to a row whose `ON` key divides by zero.
   - Ran `InferFiltersFromConstraintsSuite`, `FilterPushdownSuite`, 
`RewriteAsOfJoinSuite`, `AsOfJoinSQLSuite`, `AsOfJoinSortMergeSQLSuite`, 
`DataFrameAsOfJoinSuite`, `SortMergeAsOfJoinSuite`, 
`SortMergeAsOfJoinScannerSuite`, `StreamingAsOfJoinSuite`, the 
`TimestampNanosJoin` suites, `SQLMetricsSuite`, `PlannerSuite` and 
`WholeStageCodegenSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Generated-by: Claude Code
   


-- 
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