spark-test commented on code in PR #60760:
URL: https://github.com/apache/spark/pull/60760#discussion_r4650584770
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3587,8 +3600,9 @@ object PushPredicateThroughJoin extends Rule[LogicalPlan]
with PredicateHelper {
// Left-side WHERE predicate can run before the ASOF join, but a
right-side one cannot
case f @ Filter(filterCondition, j: AsOfJoin) =>
- val (leftFilterConditions, rightFilterConditions, commonFilterCondition)
=
- split(splitConjunctivePredicates(filterCondition), j.left, j.right)
+ val (leftFilterConditions, rightFilterConditions, commonFilterCondition)
= split(
+ splitConjunctivePredicates(filterCondition), j.left, j.right,
+ hasBarrier(j.joinType, j.condition))
Review Comment:
**Non-blocking (P2):** This call gives `hasBarrier` only the ON clause. An
as-of join keeps its match comparison in `j.asOfCondition`, and its operands
are also the sort keys `j.leftSortExprs` and `j.rightSortExprs`. Analysis
accepts a throwable operand there: `findInvalidMatchConditionExpression`
(AsOfJoinValidation.scala:166-172) rejects only subqueries, aggregates,
windows, generators and non-deterministic expressions.
Tracing the code, the PR's own test query with its guard moved from ON into
the MATCH_CONDITION still fails:
```sql
-- ANSI mode on; ta(k, t, v) holds (1, 10, 9) and tb(k, t) holds (1, 15)
SELECT a.k FROM ta a ASOF JOIN tb b
MATCH_CONDITION (a.t >= CASE WHEN b.t < 0 THEN raise_error('neg') ELSE b.t
END)
ON a.k = b.k
WHERE 10 / (a.v - 9) < 100
```
The match condition is false on the only pair (10 >= 15), so the inner join
drops the only row of `ta` and the query should return no row, as it does with
the guard in ON. But `hasBarrier(Inner, Some(a.k = b.k))` is false, so the
division is pushed into a filter on `ta` below the join and raises
DIVIDE_BY_ZERO, as on the base branch. A guard in the left operand fails the
same way, and so does a DataFrame as-of join on the sort-merge operator whose
as-of key holds the guard. For a DataFrame nearest join that allows exact
matches and has no tolerance, the as-of condition is just `true`
(basicLogicalOperators.scala:3140), so its keys appear only in the sort keys,
and checking `j.asOfCondition` alone would still miss them.
This affects only inner sort-merge as-of joins, which need the internal
settings `spark.sql.join.asofJoin.enabled` (SQL) or
`spark.sql.join.sortMergeAsOfJoin.enabled` (DataFrame), both off by default.
Left outer as-of joins keep every left row and are not affected, and neither
are as-of joins that RewriteAsOfJoin turns into a subquery.
**Recommended change:** In the Filter-over-AsOfJoin case of
PushPredicateThroughJoin (Optimizer.scala:3602-3605), let the barrier check see
every expression the as-of join evaluates to choose a match: its match
condition j.asOfCondition, its as-of keys j.leftSortExprs and j.rightSortExprs,
and its ON condition. For example, hasBarrier could take a list of expressions,
which the regular Join and lookup join cases fill with their condition alone.
Then a throwable MATCH_CONDITION operand or a throwable DataFrame as-of key
keeps WHERE conditions that can fail above an inner as-of join, as a throwable
ON conjunct now does; the keys matter for a DataFrame nearest join that allows
exact matches and has no tolerance, whose match condition is just true. Say in
the hasBarrier doc (Optimizer.scala:3437-3446) that the condition of an as-of
join includes its match condition and as-of keys, and add optimizer pushdown
tests, as-of SQL tests and a DataFrame sort-merge nearest join test with a thr
owable match condition or key.
**Why this works:** hasBarrier already returns true for an inner join whose
condition is non-deterministic or throwable. Given every expression the as-of
join evaluates to choose a match, it makes split call splitMovable with
afterBarrier set, so only the WHERE conditions that
ExprUtils.canEvaluateUnconditionally accepts, and that read no column that can
fail, move into the left input; the rest stay in the filter above the join,
which the sort-merge operator feeds only with left rows that found a match
under the match condition, the keys and the ON condition.
**Compatibility:** A left outer as-of join, an inner as-of join whose match
condition, as-of keys and ON condition are neither throwable nor
non-deterministic, and WHERE conditions that the totality check accepts keep
the plans they get with this PR.
**Risks:** For an inner sort-merge as-of join whose match condition or as-of
keys are throwable, WHERE conditions that cannot fail but that the conservative
check does not accept, such as upper(a.sym) = 'X' or a deterministic UDF, also
stay above the join and stop filtering its left input, as they already do when
the ON condition is throwable; only such joins change, they need internal
settings that are off by default, and reverting the one call restores the
current plans. When a streaming query starts with
spark.sql.streaming.v2FilterPushdown.enabled, which is off by default, Spark
chooses the stream's scan filters from a plan in which current-time expressions
and date or timestamp conversions are not deterministic, so a stream-static
inner as-of join with such an expression in its match condition or keys gets
the barrier in that plan only. A WHERE condition over the stream that the
conservative check rejects then reaches no scan filter, but each micro-batch
still applies it, so
results do not change; a regular join or an external lookup join with such an
expression in its condition already behaves this way.
**Constraints:** Keep the left outer as-of join as it is: hasBarrier is
false for it, so its WHERE conditions over the left side keep moving below the
join. Keep the regular Join and external lookup join filter cases as they are,
since they already pass their whole condition to hasBarrier.
**Exclusions:**
- The case that pushes an as-of join's own ON condition into its inputs
keeps the default flag, as the PR keeps it for every join-condition case.
- As-of joins that RewriteAsOfJoin turns into a subquery before
optimization, the default for DataFrame joinAsOf, never reach the changed case
and are not changed.
**Verification:**
- **Regression:** Run an inner SQL as-of join under ANSI mode whose
MATCH_CONDITION holds a raise_error guard that does not fire, such as a.t >=
CASE WHEN b.t < 0 THEN raise_error('neg') ELSE b.t END, over a left row that
the match condition rejects and whose WHERE division is by zero, and check that
it returns no row with the as-of join codegen off and on, where it fails with
DIVIDE_BY_ZERO before the repair.
- **Regression:** Run a DataFrame inner nearest as-of join on the sort-merge
operator under ANSI mode, with exact matches allowed and no tolerance, whose
left as-of key is when(t < 0, raise_error('neg')).otherwise(t), over a left row
whose key is null and whose WHERE division is by zero, and check that it
returns no row, where it fails with DIVIDE_BY_ZERO before the repair.
- **Behavior:** Check with an optimizer plan comparison that, over an inner
as-of join whose as-of condition holds raise_error and whose ON condition is a
plain equality, an ANSI division over the left side stays above the join while
a comparison of a left column with a literal moves into the left input, and
that over a left outer as-of join with the same conditions both move into the
left input.
--
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]