cloud-fan commented on a change in pull request #33522: URL: https://github.com/apache/spark/pull/33522#discussion_r743622126
########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner Review comment: can we just inline it? the join type doesn't matter anyway. ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner + Seq(Upper("y.d".attr), Substring("y.d".attr, 1, 5)).foreach { udf => + val originalQuery = x.join(y, joinType, Option("x.a".attr === udf)) + .select("x.a".attr, "y.e".attr) Review comment: we don't need to use qualified column names. There is no name conflict. ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner + Seq(Upper("y.d".attr), Substring("y.d".attr, 1, 5)).foreach { udf => + val originalQuery = x.join(y, joinType, Option("x.a".attr === udf)) + .select("x.a".attr, "y.e".attr) Review comment: ditto for other places in this test suite ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner Review comment: ditto for other places in this test suite ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner + Seq(Upper("y.d".attr), Substring("y.d".attr, 1, 5)).foreach { udf => + val originalQuery = x.join(y, joinType, Option("x.a".attr === udf)) + .select("x.a".attr, "y.e".attr) + val correctAnswer = x.select("x.a".attr, "x.b".attr, "x.c".attr) + .join(y.select("y.d".attr, "y.e".attr, Alias(udf, udf.sql)()), + joinType, Option("x.a".attr === s"`${udf.sql}`".attr)).select("x.a".attr, "y.e".attr) + + comparePlans(Optimize.execute(originalQuery.analyze), correctAnswer.analyze) + } + } + + test("Pull out join keys evaluation(null expressions)") { + val joinType = Inner + val udf = Coalesce(Seq("x.b".attr, "x.c".attr)) Review comment: Can't we merge this test case with the one above? ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner + Seq(Upper("y.d".attr), Substring("y.d".attr, 1, 5)).foreach { udf => + val originalQuery = x.join(y, joinType, Option("x.a".attr === udf)) + .select("x.a".attr, "y.e".attr) + val correctAnswer = x.select("x.a".attr, "x.b".attr, "x.c".attr) + .join(y.select("y.d".attr, "y.e".attr, Alias(udf, udf.sql)()), + joinType, Option("x.a".attr === s"`${udf.sql}`".attr)).select("x.a".attr, "y.e".attr) + + comparePlans(Optimize.execute(originalQuery.analyze), correctAnswer.analyze) + } + } + + test("Pull out join keys evaluation(null expressions)") { + val joinType = Inner + val udf = Coalesce(Seq("x.b".attr, "x.c".attr)) Review comment: `Seq(Upper("d".attr), Substring("d".attr, 1, 5), Coalesce(Seq("b".attr, "c".attr)))...` ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PullOutJoinConditionSuite.scala ########## @@ -0,0 +1,133 @@ +/* + * 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.dsl.expressions._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.expressions.{Alias, Coalesce, IsNull, Literal, Substring, Upper} +import org.apache.spark.sql.catalyst.plans._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules._ +import org.apache.spark.sql.internal.SQLConf + +class PullOutJoinConditionSuite extends PlanTest { + + private object Optimize extends RuleExecutor[LogicalPlan] { + val batches = + Batch("Pull out join condition", Once, + PullOutJoinCondition, + CollapseProject) :: Nil + } + + private val testRelation = LocalRelation('a.string, 'b.int, 'c.int) + private val testRelation1 = LocalRelation('d.string, 'e.int) + private val x = testRelation.subquery('x) + private val y = testRelation1.subquery('y) + + test("Pull out join keys evaluation(String expressions)") { + val joinType = Inner + Seq(Upper("y.d".attr), Substring("y.d".attr, 1, 5)).foreach { udf => + val originalQuery = x.join(y, joinType, Option("x.a".attr === udf)) + .select("x.a".attr, "y.e".attr) + val correctAnswer = x.select("x.a".attr, "x.b".attr, "x.c".attr) + .join(y.select("y.d".attr, "y.e".attr, Alias(udf, udf.sql)()), + joinType, Option("x.a".attr === s"`${udf.sql}`".attr)).select("x.a".attr, "y.e".attr) + + comparePlans(Optimize.execute(originalQuery.analyze), correctAnswer.analyze) + } + } + + test("Pull out join keys evaluation(null expressions)") { + val joinType = Inner + val udf = Coalesce(Seq("x.b".attr, "x.c".attr)) + val originalQuery = x.join(y, joinType, Option(udf === "y.e".attr)) + .select("x.a".attr, "y.e".attr) + val correctAnswer = + x.select("x.a".attr, "x.b".attr, "x.c".attr, Alias(udf, udf.sql)()).join( + y.select("y.d".attr, "y.e".attr), + joinType, Option(s"`${udf.sql}`".attr === "y.e".attr)) + .select("x.a".attr, "y.e".attr) + + comparePlans(Optimize.execute(originalQuery.analyze), correctAnswer.analyze) + } + + test("Pull out join condition contains other predicates") { + val udf = Substring("y.d".attr, 1, 5) + val joinType = Inner + val originalQuery = x.join(y, joinType, Option("x.a".attr === udf && "x.b".attr > "y.e".attr)) + .select("x.a".attr, "y.e".attr) + val correctAnswer = + x.select("x.a".attr, "x.b".attr, "x.c".attr).join( + y.select("y.d".attr, "y.e".attr, Alias(udf, udf.sql)()), + joinType, Option("x.a".attr === s"`${udf.sql}`".attr && "x.b".attr > "y.e".attr)) + .select("x.a".attr, "y.e".attr) + + comparePlans(Optimize.execute(originalQuery.analyze), correctAnswer.analyze) + } + + test("Pull out EqualNullSafe join condition") { + withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { Review comment: do we need this? ########## File path: sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala ########## @@ -1057,7 +1057,7 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan val pythonEvals = collect(joinNode.get) { case p: BatchEvalPythonExec => p } - assert(pythonEvals.size == 2) + assert(pythonEvals.size == 4) Review comment: This looks like a big problem. Can you investigate why we run python UDF 2 more times? ########## File path: sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala ########## @@ -1057,7 +1057,7 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan val pythonEvals = collect(joinNode.get) { case p: BatchEvalPythonExec => p } - assert(pythonEvals.size == 2) + assert(pythonEvals.size == 4) Review comment: OK, so it's because filter push down can lead to extra expression evaluation? -- 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]
