dongjoon-hyun commented on code in PR #60711:
URL: https://github.com/apache/spark/pull/60711#discussion_r4644597942
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -95,10 +95,29 @@ object ReorderJoin extends Rule[LogicalPlan] with
PredicateHelper {
}
}
+ /**
+ * Returns the conditions written in the joins and filters below `plan`, the
top join or filter:
+ * all of `conditions` but the ones of `plan` itself, which are the last
ones.
+ */
+ private def conditionsBelow(plan: LogicalPlan, conditions: Seq[Expression]):
Seq[Expression] = {
+ val topCondition = plan match {
+ case Filter(condition, _) => Some(condition)
+ case j: Join => j.condition
+ }
+
conditions.dropRight(topCondition.map(splitConjunctivePredicates(_).size).getOrElse(0))
+ }
+
def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
_.containsPattern(INNER_LIKE_JOIN), ruleId) {
+ // A join or filter removes a row on which its condition is NULL, so a
condition above it is
+ // not evaluated on the row. In a filter, however, `NULL AND x` evaluates
`x`, and such a filter
+ // can take a condition from above, e.g. by `CombineFilters`. So the joins
are reordered only
+ // when the conditions written below the top join or filter may all move,
so that the filter
+ // that keeps the conditions that may not move above all the joins takes
the place of the top
+ // join or filter, whose conditions they are.
case p @ ExtractFiltersAndInnerJoins(input, conditions)
- if input.size > 2 && conditions.nonEmpty =>
+ if input.size > 2 && conditions.nonEmpty &&
+ splitMovable(conditionsBelow(p, conditions),
input.map(_._1))._2.isEmpty =>
Review Comment:
Thank you. I confirmed it: with your query, the previous commit plans a
`BroadcastNestedLoopJoin` with `td` and fails with
`spark.sql.crossJoin.enabled=false`, while master and v4.2.1 plan three hash
joins.
I took your suggestion: `ExtractFiltersAndInnerJoins` no longer flattens a
join or filter below the top one that has a condition that may not move, so it
is joined as one plan, with its condition, and the joins above it are reordered
as on master. Your query plans three hash joins again. Only the top join or
filter of the flattened joins can have such a condition now, so the
`conditionsBelow` guard is gone. The new `JoinOptimizationSuite` test reorders
comma joins with a lower join that has a throwable condition, and with a lower
filter that has `rand(0)`, as one of the joined plans.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -95,10 +95,29 @@ object ReorderJoin extends Rule[LogicalPlan] with
PredicateHelper {
}
}
+ /**
+ * Returns the conditions written in the joins and filters below `plan`, the
top join or filter:
+ * all of `conditions` but the ones of `plan` itself, which are the last
ones.
+ */
+ private def conditionsBelow(plan: LogicalPlan, conditions: Seq[Expression]):
Seq[Expression] = {
+ val topCondition = plan match {
+ case Filter(condition, _) => Some(condition)
+ case j: Join => j.condition
Review Comment:
Confirmed: your query fails with `DIVIDE_BY_ZERO` with the previous commit
and on master, and returns 2 on v4.2.1.
Putting the kept conditions into the condition of the top join fixes it, but
an equality that can fail, written after the `CASE WHEN`, then becomes a join
key, which is evaluated on every row before the `CASE WHEN`. With `xc1(k, s,
f)` = `(1, 'abc', 5)`, `xc2(g)` = `(10)` and `xc3(k)` = `(1)`, the following
query fails with `CAST_INVALID_INPUT` with that change, as on v4.2.1 and
v4.3.0, while master and the previous commit return no rows, since the `CASE
WHEN` is false.
```sql
SELECT t1.k FROM xc1 t1 JOIN xc2 t2 ON t2.g > 0
JOIN xc3 t3 ON t1.k = t3.k AND <the CASE WHEN> AND CAST(t1.s AS INT) = t2.g
```
So `ReorderJoin` now leaves the joins as written when the top join has a
condition that may not move, while the joins below it can still be reordered.
Your query returns 2, and so does the same query without the subquery, since
SPARK-62091 keeps that `WHERE` clause above the join. I added your query to the
`JoinSuite` test, and a `JoinOptimizationSuite` test that leaves such a plan as
written.
--
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]