-
Notifications
You must be signed in to change notification settings - Fork 29.4k
[SPARK-31760][SQL] Simplify contained integral range predicates #58940
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
xiaoh1024
wants to merge
2
commits into
apache:master
from
xiaoh1024:fix/spark-31760-integral-ranges
+304
−0
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
175 changes: 175 additions & 0 deletions
175
...test/scala/org/apache/spark/sql/catalyst/optimizer/IntegralRangeSimplificationSuite.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,175 @@ | ||
| /* | ||
| * 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.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| class IntegralRangeSimplificationSuite extends PlanTest with ExpressionEvalHelper { | ||
| private val a = AttributeReference("a", IntegerType)() | ||
|
|
||
| private def optimize(expression: Expression): Expression = { | ||
| val relation = LocalRelation(expression.references.toSeq) | ||
| BooleanSimplification(Project(Seq(Alias(expression, "result")()), relation)) | ||
| .asInstanceOf[Project].projectList.head.asInstanceOf[Alias].child | ||
| } | ||
|
|
||
| private def checkSimplification(input: Expression, expected: Expression): Unit = { | ||
| val relation = LocalRelation(input.references.toSeq) | ||
| comparePlans( | ||
| BooleanSimplification(Project(Seq(Alias(input, "result")()), relation)), | ||
| Project(Seq(Alias(expected, "result")()), relation)) | ||
| comparePlans(BooleanSimplification(Filter(input, relation)), Filter(expected, relation)) | ||
| } | ||
|
|
||
| test("SPARK-31760: simplify contained integral ranges in either operand order") { | ||
| val cases = Seq( | ||
| (GreaterThan(a, Literal(5)), GreaterThan(a, Literal(0))), | ||
| (GreaterThanOrEqual(a, Literal(5)), GreaterThan(a, Literal(0))), | ||
| (GreaterThan(a, Literal(5)), GreaterThanOrEqual(a, Literal(5))), | ||
| (LessThan(a, Literal(0)), LessThan(a, Literal(5))), | ||
| (LessThanOrEqual(a, Literal(0)), LessThan(a, Literal(5))), | ||
| (LessThan(a, Literal(5)), LessThanOrEqual(a, Literal(5))), | ||
| (LessThan(Literal(5), a), GreaterThan(a, Literal(0))), | ||
| (GreaterThan(Literal(0), a), LessThan(a, Literal(5))), | ||
| (LessThanOrEqual(Literal(5), a), GreaterThan(a, Literal(0))), | ||
| (GreaterThanOrEqual(Literal(0), a), LessThan(a, Literal(5)))) | ||
|
|
||
| for ((narrow, wide) <- cases) { | ||
| checkSimplification(And(narrow, wide), narrow) | ||
| checkSimplification(And(wide, narrow), narrow) | ||
| checkSimplification(Or(narrow, wide), wide) | ||
| checkSimplification(Or(wide, narrow), wide) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-31760: absorb a contained range in a conjunction or disjunction") { | ||
| val wide = GreaterThan(a, Literal(1)) | ||
| val narrow = GreaterThan(a, Literal(2)) | ||
| val other = LessThan(a, Literal(4)) | ||
| for (conjunction <- Seq(And(narrow, other), And(other, narrow))) { | ||
| checkSimplification(Or(wide, conjunction), wide) | ||
| checkSimplification(Or(conjunction, wide), wide) | ||
| } | ||
| for (disjunction <- Seq(Or(wide, other), Or(other, wide))) { | ||
| checkSimplification(And(narrow, disjunction), narrow) | ||
| checkSimplification(And(disjunction, narrow), narrow) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-31760: leave unrelated or unsupported comparisons unchanged") { | ||
| val b = AttributeReference("b", IntegerType)() | ||
| val x = AttributeReference("x", DoubleType)() | ||
| val d = AttributeReference("d", DecimalType(10, 2))() | ||
| val expressions = Seq( | ||
| And(GreaterThan(a, Literal(5)), GreaterThan(b, Literal(0))), | ||
| And(GreaterThan(a, Literal(5)), LessThan(a, Literal(0))), | ||
| And(GreaterThan(a, Literal(5)), EqualTo(a, Literal(0))), | ||
| And(GreaterThan(a, Literal(5)), GreaterThan(a, Literal(null, IntegerType))), | ||
| And(GreaterThan(Add(a, Literal(1)), Literal(5)), | ||
| GreaterThan(Add(a, Literal(1)), Literal(0))), | ||
| And(GreaterThan(x, Literal(5.0)), GreaterThan(x, Literal(0.0))), | ||
| And(GreaterThan(x, Literal(Double.NaN)), GreaterThan(x, Literal(0.0))), | ||
| And(GreaterThan(d, Literal.create(Decimal(5), d.dataType)), | ||
| GreaterThan(d, Literal.create(Decimal(0), d.dataType))), | ||
| And(GreaterThan(Cast(x, IntegerType), Literal(5)), | ||
| GreaterThan(Cast(x, IntegerType), Literal(0))), | ||
| And(GreaterThan(Cast(Rand(0), IntegerType), Literal(5)), | ||
| GreaterThan(Cast(Rand(0), IntegerType), Literal(0)))) | ||
| expressions.foreach(expression => checkSimplification(expression, expression)) | ||
|
|
||
| val wide = GreaterThan(a, Literal(1)) | ||
| val narrow = GreaterThan(a, Literal(2)) | ||
| val other = GreaterThan(Cast(x, IntegerType), Literal(0)) | ||
| checkSimplification(Or(wide, And(narrow, other)), Or(wide, And(narrow, other))) | ||
| checkSimplification(And(narrow, Or(wide, other)), And(narrow, Or(wide, other))) | ||
| } | ||
|
|
||
| test("SPARK-31760: absorption preserves NULLs in an independent comparison") { | ||
| val b = AttributeReference("b", IntegerType)() | ||
| val wide = GreaterThan(a, Literal(1)) | ||
| val narrow = GreaterThan(a, Literal(2)) | ||
| val other = LessThan(b, Literal(4)) | ||
| val cases = Seq( | ||
| Or(wide, And(narrow, other)) -> wide, | ||
| Or(And(other, narrow), wide) -> wide, | ||
| And(narrow, Or(wide, other)) -> narrow, | ||
| And(Or(other, wide), narrow) -> narrow) | ||
| for ((input, expected) <- cases) { | ||
| checkSimplification(input, expected) | ||
| val original = BindReferences.bindReference(input, Seq(a, b)) | ||
| val simplified = BindReferences.bindReference(optimize(input), Seq(a, b)) | ||
| for (av <- Seq(null, 0, 1, 2, 3); bv <- Seq(null, 3, 4, 5)) { | ||
| val row = InternalRow(av, bv) | ||
| assert(original.eval(row) == simplified.eval(row)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-31760: preserve three-valued logic for all integral types and extreme bounds") { | ||
| val typesAndBounds = Seq( | ||
| (ByteType, Seq(Byte.MinValue, -1, 0, 1, Byte.MaxValue).map(_.toByte)), | ||
| (ShortType, Seq(Short.MinValue, -1, 0, 1, Short.MaxValue).map(_.toShort)), | ||
| (IntegerType, Seq(Int.MinValue, -1, 0, 1, Int.MaxValue)), | ||
| (LongType, Seq(Long.MinValue, -1L, 0L, 1L, Long.MaxValue))) | ||
|
|
||
| for ((dataType, bounds) <- typesAndBounds; nullable <- Seq(false, true)) { | ||
| val column = AttributeReference("a", dataType, nullable)() | ||
| val comparisons = bounds.flatMap { value => | ||
| val literal = Literal.create(value, dataType) | ||
| Seq(GreaterThan(column, literal), GreaterThanOrEqual(column, literal), | ||
| LessThan(column, literal), LessThanOrEqual(column, literal)) | ||
| } | ||
| val values = if (nullable) bounds :+ null else bounds | ||
| var changed = 0 | ||
| for { | ||
| left <- comparisons | ||
| right <- comparisons | ||
| input <- Seq(And(left, right), Or(left, right)) | ||
| } { | ||
| val simplified = optimize(input) | ||
| if (!input.semanticEquals(simplified)) changed += 1 | ||
| val original = BindReferences.bindReference(input, Seq(column)) | ||
| val result = BindReferences.bindReference(simplified, Seq(column)) | ||
| for (value <- values) { | ||
| val row = InternalRow(value) | ||
| assert(original.eval(row) == result.eval(row), | ||
| s"$dataType, nullable=$nullable, value=$value: $input -> $simplified") | ||
| } | ||
| } | ||
| assert(changed > 0) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-31760: generated evaluation preserves NULL and range boundaries") { | ||
| val inputs = Seq( | ||
| And(GreaterThan(a, Literal(5)), GreaterThan(a, Literal(0))), | ||
| Or(GreaterThan(a, Literal(1)), GreaterThan(a, Literal(2))), | ||
| Or(GreaterThan(a, Literal(1)), | ||
| And(GreaterThan(a, Literal(2)), LessThan(a, Literal(4))))) | ||
| for (input <- inputs; value <- Seq(null, Int.MinValue, 0, 1, 2, 5, 6, Int.MaxValue)) { | ||
| val original = BindReferences.bindReference(input, Seq(a)) | ||
| val simplified = BindReferences.bindReference(optimize(input), Seq(a)) | ||
| val row = InternalRow(value) | ||
| checkEvaluation(simplified, original.eval(row), row) | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
sql/core/src/test/scala/org/apache/spark/sql/IntegralRangeQuerySuite.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,57 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.BinaryComparison | ||
| import org.apache.spark.sql.catalyst.optimizer.BooleanSimplification | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| class IntegralRangeQuerySuite extends QueryTest with SharedSparkSession { | ||
| test("SPARK-31760: range simplification preserves filter and projection results") { | ||
| withTempView("ranges") { | ||
| val rows = Seq(null, Int.MinValue, -1, 0, 1, 2, 3, 4, 5, 6, 6, Int.MaxValue).map(Row(_)) | ||
| spark.createDataFrame(spark.sparkContext.parallelize(rows, 2), | ||
| StructType(Seq(StructField("a", IntegerType, nullable = true)))) | ||
| .createOrReplaceTempView("ranges") | ||
|
|
||
| for { | ||
| aqe <- Seq(false, true) | ||
| condition <- Seq("a > 5 AND a > 0", "a > 1 OR a > 2", | ||
| "a > 1 OR (a > 2 AND a < 4)", "5 >= a AND a < 5", | ||
| "a > (2 + 2) AND a > (1 - 1)", "a > (1 + 1) OR a > (2 * 2)") | ||
| sqlText <- Seq(s"SELECT a FROM ranges WHERE $condition", | ||
| s"SELECT $condition AS result FROM ranges") | ||
| } { | ||
| withSQLConf(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> aqe.toString) { | ||
| val expected = withSQLConf(SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> | ||
| BooleanSimplification.ruleName) { | ||
| spark.sql(sqlText).collect().toSeq | ||
| } | ||
| val query = spark.sql(sqlText) | ||
| val comparisons = query.queryExecution.optimizedPlan.flatMap { plan => | ||
| plan.expressions.flatMap(_.collect { case comparison: BinaryComparison => comparison }) | ||
| } | ||
| assert(comparisons.size == 1, query.queryExecution.optimizedPlan.toString) | ||
| checkAnswer(query, expected) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.