-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-31705][SQL] Push more possible predicates through Join via CNF…
… conversion ### What changes were proposed in this pull request? This PR add a new rule to support push predicate through join by rewriting join condition to CNF(conjunctive normal form). The following example is the steps of this rule: 1. Prepare Table: ```sql CREATE TABLE x(a INT); CREATE TABLE y(b INT); ... SELECT * FROM x JOIN y ON ((a < 0 and a > b) or a > 10); ``` 2. Convert the join condition to CNF: ``` (a < 0 or a > 10) and (a > b or a > 10) ``` 3. Split conjunctive predicates Predicates ---| (a < 0 or a > 10) (a > b or a > 10) 4. Push predicate Table | Predicate --- | --- x | (a < 0 or a > 10) ### Why are the changes needed? Improve query performance. PostgreSQL, [Impala](https://issues.apache.org/jira/browse/IMPALA-9183) and Hive support this feature. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Unit test and benchmark test. SQL | Before this PR | After this PR --- | --- | --- TPCDS 5T Q13 | 84s | 21s TPCDS 5T q85 | 66s | 34s TPCH 1T q19 | 37s | 32s Closes #28733 from gengliangwang/cnf. Lead-authored-by: Gengliang Wang <[email protected]> Co-authored-by: Yuming Wang <[email protected]> Signed-off-by: Gengliang Wang <[email protected]>
- Loading branch information
1 parent
91cd06b
commit 11d3a74
Showing
6 changed files
with
468 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushCNFPredicateThroughJoin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.{And, PredicateHelper} | ||
import org.apache.spark.sql.catalyst.plans._ | ||
import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LogicalPlan} | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
|
||
/** | ||
* Try converting join condition to conjunctive normal form expression so that more predicates may | ||
* be able to be pushed down. | ||
* To avoid expanding the join condition, the join condition will be kept in the original form even | ||
* when predicate pushdown happens. | ||
*/ | ||
object PushCNFPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { | ||
def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
case j @ Join(left, right, joinType, Some(joinCondition), hint) => | ||
val predicates = conjunctiveNormalForm(joinCondition) | ||
if (predicates.isEmpty) { | ||
j | ||
} else { | ||
val pushDownCandidates = predicates.filter(_.deterministic) | ||
lazy val leftFilterConditions = | ||
pushDownCandidates.filter(_.references.subsetOf(left.outputSet)) | ||
lazy val rightFilterConditions = | ||
pushDownCandidates.filter(_.references.subsetOf(right.outputSet)) | ||
|
||
lazy val newLeft = | ||
leftFilterConditions.reduceLeftOption(And).map(Filter(_, left)).getOrElse(left) | ||
lazy val newRight = | ||
rightFilterConditions.reduceLeftOption(And).map(Filter(_, right)).getOrElse(right) | ||
|
||
joinType match { | ||
case _: InnerLike | LeftSemi => | ||
Join(newLeft, newRight, joinType, Some(joinCondition), hint) | ||
case RightOuter => | ||
Join(newLeft, right, RightOuter, Some(joinCondition), hint) | ||
case LeftOuter | LeftAnti | ExistenceJoin(_) => | ||
Join(left, newRight, joinType, Some(joinCondition), hint) | ||
case FullOuter => j | ||
case NaturalJoin(_) => sys.error("Untransformed NaturalJoin node") | ||
case UsingJoin(_, _) => sys.error("Untransformed Using join node") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...scala/org/apache/spark/sql/catalyst/expressions/ConjunctiveNormalFormPredicateSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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.expressions | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.BooleanType | ||
|
||
class ConjunctiveNormalFormPredicateSuite extends SparkFunSuite with PredicateHelper with PlanTest { | ||
private val a = AttributeReference("A", BooleanType)(exprId = ExprId(1)).withQualifier(Seq("ta")) | ||
private val b = AttributeReference("B", BooleanType)(exprId = ExprId(2)).withQualifier(Seq("tb")) | ||
private val c = AttributeReference("C", BooleanType)(exprId = ExprId(3)).withQualifier(Seq("tc")) | ||
private val d = AttributeReference("D", BooleanType)(exprId = ExprId(4)).withQualifier(Seq("td")) | ||
private val e = AttributeReference("E", BooleanType)(exprId = ExprId(5)).withQualifier(Seq("te")) | ||
private val f = AttributeReference("F", BooleanType)(exprId = ExprId(6)).withQualifier(Seq("tf")) | ||
private val g = AttributeReference("G", BooleanType)(exprId = ExprId(7)).withQualifier(Seq("tg")) | ||
private val h = AttributeReference("H", BooleanType)(exprId = ExprId(8)).withQualifier(Seq("th")) | ||
private val i = AttributeReference("I", BooleanType)(exprId = ExprId(9)).withQualifier(Seq("ti")) | ||
private val j = AttributeReference("J", BooleanType)(exprId = ExprId(10)).withQualifier(Seq("tj")) | ||
private val a1 = | ||
AttributeReference("a1", BooleanType)(exprId = ExprId(11)).withQualifier(Seq("ta")) | ||
private val a2 = | ||
AttributeReference("a2", BooleanType)(exprId = ExprId(12)).withQualifier(Seq("ta")) | ||
private val b1 = | ||
AttributeReference("b1", BooleanType)(exprId = ExprId(12)).withQualifier(Seq("tb")) | ||
|
||
// Check CNF conversion with expected expression, assuming the input has non-empty result. | ||
private def checkCondition(input: Expression, expected: Expression): Unit = { | ||
val cnf = conjunctiveNormalForm(input) | ||
assert(cnf.nonEmpty) | ||
val result = cnf.reduceLeft(And) | ||
assert(result.semanticEquals(expected)) | ||
} | ||
|
||
test("Keep non-predicated expressions") { | ||
checkCondition(a, a) | ||
checkCondition(Literal(1), Literal(1)) | ||
} | ||
|
||
test("Conversion of Not") { | ||
checkCondition(!a, !a) | ||
checkCondition(!(!a), a) | ||
checkCondition(!(!(a && b)), a && b) | ||
checkCondition(!(!(a || b)), a || b) | ||
checkCondition(!(a || b), !a && !b) | ||
checkCondition(!(a && b), !a || !b) | ||
} | ||
|
||
test("Conversion of And") { | ||
checkCondition(a && b, a && b) | ||
checkCondition(a && b && c, a && b && c) | ||
checkCondition(a && (b || c), a && (b || c)) | ||
checkCondition((a || b) && c, (a || b) && c) | ||
checkCondition(a && b && c && d, a && b && c && d) | ||
} | ||
|
||
test("Conversion of Or") { | ||
checkCondition(a || b, a || b) | ||
checkCondition(a || b || c, a || b || c) | ||
checkCondition(a || b || c || d, a || b || c || d) | ||
checkCondition((a && b) || c, (a || c) && (b || c)) | ||
checkCondition((a && b) || (c && d), (a || c) && (a || d) && (b || c) && (b || d)) | ||
} | ||
|
||
test("More complex cases") { | ||
checkCondition(a && !(b || c), a && !b && !c) | ||
checkCondition((a && b) || !(c && d), (a || !c || !d) && (b || !c || !d)) | ||
checkCondition(a || b || c && d, (a || b || c) && (a || b || d)) | ||
checkCondition(a || (b && c || d), (a || b || d) && (a || c || d)) | ||
checkCondition(a && !(b && c || d && e), a && (!b || !c) && (!d || !e)) | ||
checkCondition(((a && b) || c) || (d || e), (a || c || d || e) && (b || c || d || e)) | ||
|
||
checkCondition( | ||
(a && b && c) || (d && e && f), | ||
(a || d) && (a || e) && (a || f) && (b || d) && (b || e) && (b || f) && | ||
(c || d) && (c || e) && (c || f) | ||
) | ||
} | ||
|
||
test("Aggregate predicate of same qualifiers to avoid expanding") { | ||
checkCondition(((a && b && a1) || c), ((a && a1) || c) && (b ||c)) | ||
checkCondition(((a && a1 && b) || c), ((a && a1) || c) && (b ||c)) | ||
checkCondition(((b && d && a && a1) || c), ((a && a1) || c) && (b ||c) && (d || c)) | ||
checkCondition(((b && a2 && d && a && a1) || c), ((a2 && a && a1) || c) && (b ||c) && (d || c)) | ||
checkCondition(((b && d && a && a1 && b1) || c), | ||
((a && a1) || c) && ((b && b1) ||c) && (d || c)) | ||
checkCondition((a && a1) || (b && b1), (a && a1) || (b && b1)) | ||
checkCondition((a && a1 && c) || (b && b1), ((a && a1) || (b && b1)) && (c || (b && b1))) | ||
} | ||
|
||
test("Return Seq.empty when exceeding MAX_CNF_NODE_COUNT") { | ||
// The following expression contains 36 conjunctive sub-expressions in CNF | ||
val input = (a && b && c) || (d && e && f) || (g && h && i && j) | ||
// The following expression contains 9 conjunctive sub-expressions in CNF | ||
val input2 = (a && b && c) || (d && e && f) | ||
Seq(8, 9, 10, 35, 36, 37).foreach { maxCount => | ||
withSQLConf(SQLConf.MAX_CNF_NODE_COUNT.key -> maxCount.toString) { | ||
if (maxCount < 36) { | ||
assert(conjunctiveNormalForm(input).isEmpty) | ||
} else { | ||
assert(conjunctiveNormalForm(input).nonEmpty) | ||
} | ||
if (maxCount < 9) { | ||
assert(conjunctiveNormalForm(input2).isEmpty) | ||
} else { | ||
assert(conjunctiveNormalForm(input2).nonEmpty) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.