-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-12957][SQL] Initial support for constraint propagation in SparkSQL #10844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8d050df
initial framework
sameeragarwal fed48b8
Initial set of constraints
sameeragarwal 76d2727
Constraint propagation in Set and Binary operators
sameeragarwal 67e138d
modify test
sameeragarwal 04ff99a
fix tests
sameeragarwal 7bde51d
outstanding changes
sameeragarwal 494b28f
upstream changes
sameeragarwal 0c4c78b
support union with multiple children
sameeragarwal 7fb2f9c
join propagation
sameeragarwal f15ef96
support all joins
sameeragarwal 53be837
Michael's comments
sameeragarwal 8c6bb70
upstream changes
sameeragarwal 302444f
Michael's comments
sameeragarwal b52742a
move constructIsNotNullConstraints in QueryPlan
sameeragarwal 2bd2735
Yin's comments
sameeragarwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.plans | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, Expression, VirtualColumn} | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.trees.TreeNode | ||
| import org.apache.spark.sql.types.{DataType, StructType} | ||
|
|
||
|
|
@@ -26,6 +26,56 @@ abstract class QueryPlan[PlanType <: TreeNode[PlanType]] extends TreeNode[PlanTy | |
|
|
||
| def output: Seq[Attribute] | ||
|
|
||
| /** | ||
| * Extracts the relevant constraints from a given set of constraints based on the attributes that | ||
| * appear in the [[outputSet]]. | ||
| */ | ||
| protected def getRelevantConstraints(constraints: Set[Expression]): Set[Expression] = { | ||
| constraints | ||
| .union(constructIsNotNullConstraints(constraints)) | ||
| .filter(constraint => | ||
| constraint.references.nonEmpty && constraint.references.subsetOf(outputSet)) | ||
| } | ||
|
|
||
| /** | ||
| * Infers a set of `isNotNull` constraints from a given set of equality/comparison expressions. | ||
| * For e.g., if an expression is of the form (`a > 5`), this returns a constraint of the form | ||
| * `isNotNull(a)` | ||
| */ | ||
| private def constructIsNotNullConstraints(constraints: Set[Expression]): Set[Expression] = { | ||
| // Currently we only propagate constraints if the condition consists of equality | ||
| // and ranges. For all other cases, we return an empty set of constraints | ||
| constraints.map { | ||
| case EqualTo(l, r) => | ||
| Set(IsNotNull(l), IsNotNull(r)) | ||
| case GreaterThan(l, r) => | ||
| Set(IsNotNull(l), IsNotNull(r)) | ||
| case GreaterThanOrEqual(l, r) => | ||
| Set(IsNotNull(l), IsNotNull(r)) | ||
| case LessThan(l, r) => | ||
| Set(IsNotNull(l), IsNotNull(r)) | ||
| case LessThanOrEqual(l, r) => | ||
| Set(IsNotNull(l), IsNotNull(r)) | ||
| case _ => | ||
| Set.empty[Expression] | ||
| }.foldLeft(Set.empty[Expression])(_ union _.toSet) | ||
| } | ||
|
|
||
| /** | ||
| * A sequence of expressions that describes the data property of the output rows of this | ||
| * operator. For example, if the output of this operator is column `a`, an example `constraints` | ||
| * can be `Set(a > 10, a < 20)`. | ||
| */ | ||
| lazy val constraints: Set[Expression] = getRelevantConstraints(validConstraints) | ||
|
|
||
| /** | ||
| * This method can be overridden by any child class of QueryPlan to specify a set of constraints | ||
| * based on the given operator's constraint propagation logic. These constraints are then | ||
| * canonicalized and filtered automatically to contain only those attributes that appear in the | ||
| * [[outputSet]] | ||
| */ | ||
| protected def validConstraints: Set[Expression] = Set.empty | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add doc here suggesting the child classes override this method along with the semantics (i.e. if we go with the suggestion above you are supposed to output any constraint that might be valid and it will canonicalized and filtered automatically). |
||
|
|
||
| /** | ||
| * Returns the set of attributes that are output by this node. | ||
| */ | ||
|
|
@@ -59,6 +109,7 @@ abstract class QueryPlan[PlanType <: TreeNode[PlanType]] extends TreeNode[PlanTy | |
| * Runs [[transform]] with `rule` on all expressions present in this query operator. | ||
| * Users should not expect a specific directionality. If a specific directionality is needed, | ||
| * transformExpressionsDown or transformExpressionsUp should be used. | ||
| * | ||
| * @param rule the rule to be applied to every expression in this operator. | ||
| */ | ||
| def transformExpressions(rule: PartialFunction[Expression, Expression]): this.type = { | ||
|
|
@@ -67,6 +118,7 @@ abstract class QueryPlan[PlanType <: TreeNode[PlanType]] extends TreeNode[PlanTy | |
|
|
||
| /** | ||
| * Runs [[transformDown]] with `rule` on all expressions present in this query operator. | ||
| * | ||
| * @param rule the rule to be applied to every expression in this operator. | ||
| */ | ||
| def transformExpressionsDown(rule: PartialFunction[Expression, Expression]): this.type = { | ||
|
|
@@ -99,6 +151,7 @@ abstract class QueryPlan[PlanType <: TreeNode[PlanType]] extends TreeNode[PlanTy | |
|
|
||
| /** | ||
| * Runs [[transformUp]] with `rule` on all expressions present in this query operator. | ||
| * | ||
| * @param rule the rule to be applied to every expression in this operator. | ||
| * @return | ||
| */ | ||
|
|
||
This file contains hidden or 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 hidden or 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
173 changes: 173 additions & 0 deletions
173
...alyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala
This file contains hidden or 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,173 @@ | ||
| /* | ||
| * 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.plans | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.analysis._ | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
|
|
||
| class ConstraintPropagationSuite extends SparkFunSuite { | ||
|
|
||
| private def resolveColumn(tr: LocalRelation, columnName: String): Expression = | ||
| tr.analyze.resolveQuoted(columnName, caseInsensitiveResolution).get | ||
|
|
||
| private def verifyConstraints(found: Set[Expression], expected: Set[Expression]): Unit = { | ||
| val missing = expected.filterNot(i => found.map(_.semanticEquals(i)).reduce(_ || _)) | ||
| val extra = found.filterNot(i => expected.map(_.semanticEquals(i)).reduce(_ || _)) | ||
| if (missing.nonEmpty || extra.nonEmpty) { | ||
| fail( | ||
| s""" | ||
| |== FAIL: Constraints do not match === | ||
| |Found: ${found.mkString(",")} | ||
| |Expected: ${expected.mkString(",")} | ||
| |== Result == | ||
| |Missing: ${if (missing.isEmpty) "N/A" else missing.mkString(",")} | ||
| |Found but not expected: ${if (extra.isEmpty) "N/A" else extra.mkString(",")} | ||
| """.stripMargin) | ||
| } | ||
| } | ||
|
|
||
| test("propagating constraints in filters") { | ||
| val tr = LocalRelation('a.int, 'b.string, 'c.int) | ||
|
|
||
| assert(tr.analyze.constraints.isEmpty) | ||
|
|
||
| assert(tr.where('a.attr > 10).select('c.attr, 'b.attr).analyze.constraints.isEmpty) | ||
|
|
||
| verifyConstraints(tr | ||
| .where('a.attr > 10) | ||
| .analyze.constraints, | ||
| Set(resolveColumn(tr, "a") > 10, | ||
| IsNotNull(resolveColumn(tr, "a")))) | ||
|
|
||
| verifyConstraints(tr | ||
| .where('a.attr > 10) | ||
| .select('c.attr, 'a.attr) | ||
| .where('c.attr < 100) | ||
| .analyze.constraints, | ||
| Set(resolveColumn(tr, "a") > 10, | ||
| resolveColumn(tr, "c") < 100, | ||
| IsNotNull(resolveColumn(tr, "a")), | ||
| IsNotNull(resolveColumn(tr, "c")))) | ||
| } | ||
|
|
||
| test("propagating constraints in union") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int) | ||
| val tr2 = LocalRelation('d.int, 'e.int, 'f.int) | ||
| val tr3 = LocalRelation('g.int, 'h.int, 'i.int) | ||
|
|
||
| assert(tr1 | ||
| .where('a.attr > 10) | ||
| .unionAll(tr2.where('e.attr > 10) | ||
| .unionAll(tr3.where('i.attr > 10))) | ||
| .analyze.constraints.isEmpty) | ||
|
|
||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .unionAll(tr2.where('d.attr > 10) | ||
| .unionAll(tr3.where('g.attr > 10))) | ||
| .analyze.constraints, | ||
| Set(resolveColumn(tr1, "a") > 10, | ||
| IsNotNull(resolveColumn(tr1, "a")))) | ||
| } | ||
|
|
||
| test("propagating constraints in intersect") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int) | ||
| val tr2 = LocalRelation('a.int, 'b.int, 'c.int) | ||
|
|
||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .intersect(tr2.where('b.attr < 100)) | ||
| .analyze.constraints, | ||
| Set(resolveColumn(tr1, "a") > 10, | ||
| resolveColumn(tr1, "b") < 100, | ||
| IsNotNull(resolveColumn(tr1, "a")), | ||
| IsNotNull(resolveColumn(tr1, "b")))) | ||
| } | ||
|
|
||
| test("propagating constraints in except") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int) | ||
| val tr2 = LocalRelation('a.int, 'b.int, 'c.int) | ||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .except(tr2.where('b.attr < 100)) | ||
| .analyze.constraints, | ||
| Set(resolveColumn(tr1, "a") > 10, | ||
| IsNotNull(resolveColumn(tr1, "a")))) | ||
| } | ||
|
|
||
| test("propagating constraints in inner join") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int).subquery('tr1) | ||
| val tr2 = LocalRelation('a.int, 'd.int, 'e.int).subquery('tr2) | ||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .join(tr2.where('d.attr < 100), Inner, Some("tr1.a".attr === "tr2.a".attr)) | ||
| .analyze.constraints, | ||
| Set(tr1.resolveQuoted("a", caseInsensitiveResolution).get > 10, | ||
| tr2.resolveQuoted("d", caseInsensitiveResolution).get < 100, | ||
| tr1.resolveQuoted("a", caseInsensitiveResolution).get === | ||
| tr2.resolveQuoted("a", caseInsensitiveResolution).get, | ||
| IsNotNull(tr2.resolveQuoted("a", caseInsensitiveResolution).get), | ||
| IsNotNull(tr1.resolveQuoted("a", caseInsensitiveResolution).get), | ||
| IsNotNull(tr2.resolveQuoted("d", caseInsensitiveResolution).get))) | ||
| } | ||
|
|
||
| test("propagating constraints in left-semi join") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int).subquery('tr1) | ||
| val tr2 = LocalRelation('a.int, 'd.int, 'e.int).subquery('tr2) | ||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .join(tr2.where('d.attr < 100), LeftSemi, Some("tr1.a".attr === "tr2.a".attr)) | ||
| .analyze.constraints, | ||
| Set(tr1.resolveQuoted("a", caseInsensitiveResolution).get > 10, | ||
| IsNotNull(tr1.resolveQuoted("a", caseInsensitiveResolution).get))) | ||
| } | ||
|
|
||
| test("propagating constraints in left-outer join") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int).subquery('tr1) | ||
| val tr2 = LocalRelation('a.int, 'd.int, 'e.int).subquery('tr2) | ||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .join(tr2.where('d.attr < 100), LeftOuter, Some("tr1.a".attr === "tr2.a".attr)) | ||
| .analyze.constraints, | ||
| Set(tr1.resolveQuoted("a", caseInsensitiveResolution).get > 10, | ||
| IsNotNull(tr1.resolveQuoted("a", caseInsensitiveResolution).get))) | ||
| } | ||
|
|
||
| test("propagating constraints in right-outer join") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int).subquery('tr1) | ||
| val tr2 = LocalRelation('a.int, 'd.int, 'e.int).subquery('tr2) | ||
| verifyConstraints(tr1 | ||
| .where('a.attr > 10) | ||
| .join(tr2.where('d.attr < 100), RightOuter, Some("tr1.a".attr === "tr2.a".attr)) | ||
| .analyze.constraints, | ||
| Set(tr2.resolveQuoted("d", caseInsensitiveResolution).get < 100, | ||
| IsNotNull(tr2.resolveQuoted("d", caseInsensitiveResolution).get))) | ||
| } | ||
|
|
||
| test("propagating constraints in full-outer join") { | ||
| val tr1 = LocalRelation('a.int, 'b.int, 'c.int).subquery('tr1) | ||
| val tr2 = LocalRelation('a.int, 'd.int, 'e.int).subquery('tr2) | ||
| assert(tr1.where('a.attr > 10) | ||
| .join(tr2.where('d.attr < 100), FullOuter, Some("tr1.a".attr === "tr2.a".attr)) | ||
| .analyze.constraints.isEmpty) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add scala doc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added