LuciferYang commented on code in PR #60711:
URL: https://github.com/apache/spark/pull/60711#discussion_r4644068439
##########
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:
When a lower `ON` clause or a filter between the joins keeps a condition,
this refuses the chain, and also each lower chain that still contains that join
or filter, since the condition is below its top too. So a comma join keeps its
written order. With `INT` columns, `SELECT * FROM ta JOIN tbb ON ta.id =
tbb.aid AND <raise_error CASE WHEN>, td, tc WHERE tc.k2 = ta.k AND td.j2 =
tc.j` now plans a `BroadcastNestedLoopJoin` with `td` and fails with
`spark.sql.crossJoin.enabled=false`, while master and 4.2.0 plan three hash
joins. A `rand(0) < 0.5` filter over non-null view columns also plans a
`BroadcastNestedLoopJoin` here, in SQL and DataFrame form.
Locally, stopping `ExtractFiltersAndInnerJoins` at a lower join or filter
whose own condition has a non-deterministic or throwable conjunct, so it
becomes one input of the chain above, keeps the hash joins and still returns 2
for the earlier forms. What do you think?
##########
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:
When the top of the chain is a join, its kept conditions go into a `Filter`,
and `CombineFilters` can merge into that filter a `WHERE` conjunct that
`PushPredicateThroughJoin` would leave above the join, such as one with a
correlated scalar subquery. With your data, this fails with `DIVIDE_BY_ZERO`
here and on master, and returns 2 on 4.2.0 and with `ReorderJoin` excluded:
```sql
SELECT t1.k FROM tb1 t1 JOIN tb2 t2 ON t1.k = t2.k
JOIN tb3 t3 ON t1.v = t3.v AND <the CASE WHEN>
WHERE 10 / (t1.k - t3.w) > (SELECT max(x.k) FROM tb2 x WHERE x.k = t1.k) - 5
```
Locally, putting the kept conditions into the top join's condition when the
top is a join and `canEvaluateWithinJoin` accepts them returns 2 and passes
`JoinOptimizationSuite` and the SPARK-6* tests of `JoinSuite`. Could we do that
here?
--
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]