cloud-fan commented on code in PR #60403:
URL: https://github.com/apache/spark/pull/60403#discussion_r4559803534
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2315,6 +2315,19 @@ object InferFiltersFromConstraints extends
Rule[LogicalPlan]
case _ => join
}
+
+ // A right row that fails the ON condition is never the match of any left
row, so a filter
+ // that follows from the left constraints and the ON condition can drop it
before the join.
+ // An inner join also drops the left rows without a match, so its left
side gets one too.
+ case j: AsOfJoin =>
+ val allConstraints = getAllConstraints(j.left, j.right, j.condition)
+ val newLeft = j.joinType match {
+ case Inner => inferNewFilter(j.left, allConstraints)
+ case LeftOuter => j.left
+ case other =>
+ throw SparkException.internalError(s"Unexpected as-of join type:
$other")
+ }
+ j.copy(left = newLeft, right = inferNewFilter(j.right, allConstraints))
Review Comment:
**Non-blocking (P2):** This line also passes uncorrelated `IN` / `NOT IN`
subquery predicates to the right input. Take `WHERE t.cust IN (SELECT cust FROM
accounts WHERE ...)` with `ON t.cust = q.cust`. Predicate pushdown places the
`IN` on the left input, its constraint carries across the ON equality as
`q.cust IN (SELECT ...)`, and `canBeFilter` accepts it, because
`stripCannotFail` reduces an `InSubquery` to `IsNotNull` of its values.
`RewritePredicateSubquery` runs in a later batch and turns that filter into a
left semi join under the as-of join, or a null-aware anti join for `NOT IN`.
When the subquery result is too large to broadcast, the planner code gives
the right input this shape: `Exchange(q.cust)` over every quote, as before,
then `Sort(q.cust)`, then a `SortMergeJoin` LeftSemi, then `Sort(q.cust,
q.ts)`, then `SortMergeAsOfJoin`. A sort-merge semi join reports only its key
ordering (`SortMergeJoinExec.getKeyOrdering`), while `SortMergeAsOfJoinExec`
requires the keys followed by the as-of sort expressions, so
`EnsureRequirements` adds the second sort. The semi join runs after the
shuffle, so the shuffle does not shrink either. The query returns the same rows
but does more sort and merge work than on the base branch, and when the `IN`
keeps most rows the right side's sort work roughly doubles. For `NOT IN`, the
null-aware anti join becomes a nested-loop join over the right input. An inner
as-of join has the mirror case on its left input (line 2325).
Regular joins keep these inferred semi joins on purpose (SPARK-60985 kept
them for TPC-H q18 and q20), but a regular sort-merge join needs only key
order, so only the as-of join pays the second sort. When the subquery result is
small enough to broadcast, the inferred filter does help, but the PR neither
mentions nor tests the subquery case, and its description does not accept this
cost. The plan shape above comes from reading the planner code, not from an
EXPLAIN run.
**Recommended change:** In the AsOfJoin case of InferFiltersFromConstraints,
leave out every inferred predicate that contains an IN subquery or a correlated
EXISTS subquery before building the filters for either input, and add a code
comment explaining why: on an as-of input such a filter becomes a semi or anti
join that keeps only key order, so the as-of join would have to sort that input
again by key and as-of key. Plain predicates, IsNotNull on ON keys, and
scalar-subquery comparisons are still inferred, and the regular join cases do
not change. Add optimizer plan tests for both as-of join types with an IN or
NOT IN subquery on an ON key, and a query test that checks the results and the
plan below the as-of join.
**Why this works:** The existing
SubqueryExpression.hasInOrCorrelatedExistsSubquery test identifies exactly the
predicates that the subquery rewrite turns into joins. Applying it to the
inferred predicates of the as-of case, after transitivity and literal-binding
inference and before the per-side filters are built, keeps those joins off the
as-of inputs, so each input keeps the base branch's single shuffle and sort.
Every other inferred predicate is still derived from the full constraint set,
so its inference does not change.
**Compatibility:** Every filter the PR now infers across an as-of join that
contains no IN or correlated EXISTS subquery, and every filter the rule infers
for regular joins, stays the same.
**Risks:** An IN subquery on an as-of ON key whose result is small enough to
broadcast no longer pre-filters the other input before its shuffle, so such a
query shuffles and sorts that input as on the base branch instead of less.
Removing the exclusion restores the current behavior; no config gates it.
**Constraints:** Do not change canBeFilter, inferNewFilter, or
getAllConstraints for the regular join cases, which keep inferring subquery
filters. Apply the exclusion to the inferred predicates after transitivity and
literal-binding inference, so that no other inferred predicate changes. Apply
it to both inputs: the right input of both join types and the left input of an
inner join.
**Exclusions:**
- User-written IN subqueries in WHERE or ON, which predicate pushdown
already places below the as-of join on the base branch, are left as they are.
- Making subquery inference depend on the subquery result's size or on the
physical ordering of the input is left out, because the optimizer has no
reliable size at that point.
**Verification:**
- **Regression:** For inner and left outer as-of joins whose left input is
filtered by an uncorrelated IN subquery on the ON key, the optimized plan's
right input gets IsNotNull on its ON key but no IN subquery filter; the same
holds for NOT IN, and, for an inner join, for the left input when the right
input carries the subquery.
- **Behavior:** An as-of join query with an IN subquery on the ON key
returns the same rows as on the base branch for both join types, and its
physical plan has no semi or anti join below the as-of join on the input the
subquery was not written on.
- **Behavior:** An as-of join whose left input compares its ON key with an
uncorrelated scalar subquery still gets that comparison inferred onto the right
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]