wangyum commented on a change in pull request #33522: URL: https://github.com/apache/spark/pull/33522#discussion_r741667104
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinCondition.scala ########## @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.optimizer + +import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, PredicateHelper} +import org.apache.spark.sql.catalyst.plans.logical.{Join, LogicalPlan, Project} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern.JOIN + +/** + * This rule ensures that [[Join]] condition doesn't contain complex expressions in the + * optimization phase. + * + * Complex condition expressions are pulled out to a [[Project]] node under [[Join]] and are + * referenced in join condition. + * + * {{{ + * SELECT * FROM t1 JOIN t2 ON t1.a + 10 = t2.x ==> + * Project [a#0, b#1, x#2, y#3] + * +- Join Inner, ((spark_catalog.default.t1.a + 10)#8 = x#2) + * :- Project [a#0, b#1, (a#0 + 10) AS (spark_catalog.default.t1.a + 10)#8] + * : +- Filter isnotnull((a#0 + 10)) + * : +- Relation default.t1[a#0,b#1] parquet + * +- Filter isnotnull(x#2) + * +- Relation default.t2[x#2,y#3] parquet + * }}} + */ +object PullOutJoinCondition extends Rule[LogicalPlan] + with JoinSelectionHelper with PredicateHelper { + + def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(_.containsPattern(JOIN)) { + case j @ Join(left, right, _, Some(condition), _) Review comment: ```scala object PullOutJoinCondition extends Rule[LogicalPlan] with JoinSelectionHelper with PredicateHelper { def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(_.containsPattern(JOIN)) { case j @ ExtractEquiJoinKeys(_, leftKeys, rightKeys, _, _, left, right, _) if j.resolved && !canPlanAsBroadcastHashJoin(j, conf) => val leftComplexExpressions = leftKeys.filter(e => !e.foldable && e.children.nonEmpty) val rightComplexExpressions = rightKeys.filter(e => !e.foldable && e.children.nonEmpty) val leftComplexExpressionMap = leftComplexExpressions.map(e => e.canonicalized -> Alias(e, e.sql)()).toMap val rightComplexExpressionMap = rightComplexExpressions.map(e => e.canonicalized -> Alias(e, e.sql)()).toMap val allComplexExpressionMap = leftComplexExpressionMap ++ rightComplexExpressionMap if (allComplexExpressionMap.nonEmpty) { val newCondition = j.condition.map { _.transformDown { case e: Expression if e.children.nonEmpty && allComplexExpressionMap.contains(e.canonicalized) => allComplexExpressionMap.get(e.canonicalized).map(_.toAttribute).getOrElse(e) } } val newLeft = Project(left.output ++ leftComplexExpressionMap.values, left) val newRight = Project(right.output ++ rightComplexExpressionMap.values, right) Project(j.output, j.copy(left = newLeft, right = newRight, condition = newCondition)) } else { j } } } ``` It will throw `StackOverflowError`: ```scala spark.sql("CREATE TABLE t1(a int, b int) using parquet") spark.sql("CREATE TABLE t2(x int, y int) using parquet") spark.sql("SELECT * FROM t1 JOIN t2 ON t1.a + 10 <=> t2.x").explain("extended") ``` ``` An exception or error caused a run to abort. java.lang.StackOverflowError at scala.collection.mutable.AbstractSeq.<init>(Seq.scala:50) at scala.collection.mutable.AbstractBuffer.<init>(Buffer.scala:50) at scala.collection.mutable.ListBuffer.<init>(ListBuffer.scala:48) at scala.collection.immutable.List$.newBuilder(List.scala:501) at scala.collection.generic.GenericTraversableTemplate.genericBuilder(GenericTraversableTemplate.scala:72) at scala.collection.generic.GenericTraversableTemplate.genericBuilder$(GenericTraversableTemplate.scala:72) at scala.collection.AbstractTraversable.genericBuilder(Traversable.scala:108) at scala.collection.generic.GenericTraversableTemplate.flatten(GenericTraversableTemplate.scala:172) at scala.collection.generic.GenericTraversableTemplate.flatten$(GenericTraversableTemplate.scala:171) at scala.collection.AbstractTraversable.flatten(Traversable.scala:108) at org.apache.spark.internal.config.ConfigEntry.readString(ConfigEntry.scala:95) at org.apache.spark.internal.config.ConfigEntryWithDefault.readFrom(ConfigEntry.scala:141) at org.apache.spark.sql.internal.SQLConf.getConf(SQLConf.scala:4220) at org.apache.spark.sql.internal.SQLConf.caseSensitiveAnalysis(SQLConf.scala:3770) at org.apache.spark.sql.types.DataType.sameType(DataType.scala:99) at org.apache.spark.sql.catalyst.analysis.TypeCoercionBase.$anonfun$haveSameType$1(TypeCoercion.scala:157) at org.apache.spark.sql.catalyst.analysis.TypeCoercionBase.$anonfun$haveSameType$1$adapted(TypeCoercion.scala:157) at scala.collection.LinearSeqOptimized.forall(LinearSeqOptimized.scala:85) at scala.collection.LinearSeqOptimized.forall$(LinearSeqOptimized.scala:82) at scala.collection.immutable.List.forall(List.scala:91) at org.apache.spark.sql.catalyst.analysis.TypeCoercionBase.haveSameType(TypeCoercion.scala:157) at org.apache.spark.sql.catalyst.util.TypeUtils$.checkForSameTypeInputExpr(TypeUtils.scala:47) at org.apache.spark.sql.catalyst.expressions.Coalesce.checkInputDataTypes(nullExpressions.scala:65) at org.apache.spark.sql.catalyst.expressions.Expression.resolved$lzycompute(Expression.scala:211) at org.apache.spark.sql.catalyst.expressions.Expression.resolved(Expression.scala:211) at org.apache.spark.sql.catalyst.expressions.Expression.$anonfun$childrenResolved$1(Expression.scala:223) at org.apache.spark.sql.catalyst.expressions.Expression.$anonfun$childrenResolved$1$adapted(Expression.scala:223) at scala.collection.Iterator.forall(Iterator.scala:955) at scala.collection.Iterator.forall$(Iterator.scala:953) at scala.collection.AbstractIterator.forall(Iterator.scala:1431) at scala.collection.IterableLike.forall(IterableLike.scala:77) at scala.collection.IterableLike.forall$(IterableLike.scala:76) at scala.collection.AbstractIterable.forall(Iterable.scala:56) at org.apache.spark.sql.catalyst.expressions.Expression.childrenResolved(Expression.scala:223) at org.apache.spark.sql.catalyst.expressions.Alias.resolved$lzycompute(namedExpressions.scala:166) at org.apache.spark.sql.catalyst.expressions.Alias.resolved(namedExpressions.scala:166) at org.apache.spark.sql.catalyst.expressions.Alias.toAttribute(namedExpressions.scala:206) at org.apache.spark.sql.catalyst.plans.logical.Project.$anonfun$output$1(basicLogicalOperators.scala:70) at scala.collection.immutable.List.map(List.scala:297) at org.apache.spark.sql.catalyst.plans.logical.Project.output(basicLogicalOperators.scala:70) at org.apache.spark.sql.catalyst.plans.QueryPlan.outputSet$lzycompute(QueryPlan.scala:54) at org.apache.spark.sql.catalyst.plans.QueryPlan.outputSet(QueryPlan.scala:54) at org.apache.spark.sql.catalyst.expressions.PredicateHelper.canEvaluate(predicates.scala:199) at org.apache.spark.sql.catalyst.expressions.PredicateHelper.canEvaluate$(predicates.scala:198) at org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys$.canEvaluate(patterns.scala:138) at org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys$.$anonfun$unapply$5(patterns.scala:162) at scala.collection.immutable.List.flatMap(List.scala:366) at org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys$.unapply(patterns.scala:156) at org.apache.spark.sql.catalyst.optimizer.PullOutJoinCondition$$anonfun$apply$2.applyOrElse(PullOutJoinCondition.scala:48) at org.apache.spark.sql.catalyst.optimizer.PullOutJoinCondition$$anonfun$apply$2.applyOrElse(PullOutJoinCondition.scala:47) ``` ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinCondition.scala ########## @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.optimizer + +import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, PredicateHelper} +import org.apache.spark.sql.catalyst.plans.logical.{Join, LogicalPlan, Project} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern.JOIN + +/** + * This rule ensures that [[Join]] condition doesn't contain complex expressions in the + * optimization phase. + * + * Complex condition expressions are pulled out to a [[Project]] node under [[Join]] and are + * referenced in join condition. + * + * {{{ + * SELECT * FROM t1 JOIN t2 ON t1.a + 10 = t2.x ==> + * Project [a#0, b#1, x#2, y#3] + * +- Join Inner, ((spark_catalog.default.t1.a + 10)#8 = x#2) + * :- Project [a#0, b#1, (a#0 + 10) AS (spark_catalog.default.t1.a + 10)#8] + * : +- Filter isnotnull((a#0 + 10)) + * : +- Relation default.t1[a#0,b#1] parquet + * +- Filter isnotnull(x#2) + * +- Relation default.t2[x#2,y#3] parquet + * }}} + */ +object PullOutJoinCondition extends Rule[LogicalPlan] + with JoinSelectionHelper with PredicateHelper { + + def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(_.containsPattern(JOIN)) { + case j @ Join(left, right, _, Some(condition), _) + if j.resolved && !canPlanAsBroadcastHashJoin(j, conf) => + val complexExpressions = splitConjunctivePredicates(condition).flatMap(_.children).flatMap { + case e: Expression if !e.foldable && e.children.nonEmpty => Seq(e) + case _ => Nil + } Review comment: How about change it to: ```scala val complexExpressions = splitConjunctivePredicates(condition).flatMap { case p: Predicate => p.children.filter(e => !e.foldable && e.children.nonEmpty) case _ => Nil } ``` or ```scala val complexExpressions = splitConjunctivePredicates(condition).flatMap { case e: EqualTo => e.children.filter(e => !e.foldable && e.children.nonEmpty) case _ => Nil } ``` to reduce risk. -- 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]
