-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-13495][SQL] Add Null Filters in the query plan for Filters/Joi…
…ns based on their data constraints ## What changes were proposed in this pull request? This PR adds an optimizer rule to eliminate reading (unnecessary) NULL values if they are not required for correctness by inserting `isNotNull` filters is the query plan. These filters are currently inserted beneath existing `Filter` and `Join` operators and are inferred based on their data constraints. Note: While this optimization is applicable to all types of join, it primarily benefits `Inner` and `LeftSemi` joins. ## How was this patch tested? 1. Added a new `NullFilteringSuite` that tests for `IsNotNull` filters in the query plan for joins and filters. Also, tests interaction with the `CombineFilters` optimizer rules. 2. Test generated ExpressionTrees via `OrcFilterSuite` 3. Test filter source pushdown logic via `SimpleTextHadoopFsRelationSuite` cc yhuai nongli Author: Sameer Agarwal <sameer@databricks.com> Closes #11372 from sameeragarwal/gen-isnotnull.
- Loading branch information
1 parent
4896411
commit ef77003
Showing
8 changed files
with
182 additions
and
20 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
95 changes: 95 additions & 0 deletions
95
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/NullFilteringSuite.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,95 @@ | ||
/* | ||
* 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._ | ||
import org.apache.spark.sql.catalyst.plans._ | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules._ | ||
|
||
class NullFilteringSuite extends PlanTest { | ||
|
||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val batches = Batch("NullFiltering", Once, NullFiltering) :: | ||
Batch("CombineFilters", FixedPoint(5), CombineFilters) :: Nil | ||
} | ||
|
||
val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
|
||
test("filter: filter out nulls in condition") { | ||
val originalQuery = testRelation.where('a === 1).analyze | ||
val correctAnswer = testRelation.where(IsNotNull('a) && 'a === 1).analyze | ||
val optimized = Optimize.execute(originalQuery) | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("single inner join: filter out nulls on either side on equi-join keys") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
val originalQuery = x.join(y, | ||
condition = Some("x.a".attr === "y.a".attr && "x.b".attr === 1 && "y.c".attr > 5)).analyze | ||
val left = x.where(IsNotNull('a) && IsNotNull('b)) | ||
val right = y.where(IsNotNull('a) && IsNotNull('c)) | ||
val correctAnswer = left.join(right, | ||
condition = Some("x.a".attr === "y.a".attr && "x.b".attr === 1 && "y.c".attr > 5)).analyze | ||
val optimized = Optimize.execute(originalQuery) | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("single inner join with pre-existing filters: filter out nulls on either side") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
val originalQuery = x.where('b > 5).join(y.where('c === 10), | ||
condition = Some("x.a".attr === "y.a".attr)).analyze | ||
val left = x.where(IsNotNull('a) && IsNotNull('b) && 'b > 5) | ||
val right = y.where(IsNotNull('a) && IsNotNull('c) && 'c === 10) | ||
val correctAnswer = left.join(right, | ||
condition = Some("x.a".attr === "y.a".attr)).analyze | ||
val optimized = Optimize.execute(originalQuery) | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("single outer join: no null filters are generated") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
val originalQuery = x.join(y, FullOuter, | ||
condition = Some("x.a".attr === "y.a".attr)).analyze | ||
val optimized = Optimize.execute(originalQuery) | ||
comparePlans(optimized, originalQuery) | ||
} | ||
|
||
test("multiple inner joins: filter out nulls on all sides on equi-join keys") { | ||
val t1 = testRelation.subquery('t1) | ||
val t2 = testRelation.subquery('t2) | ||
val t3 = testRelation.subquery('t3) | ||
val t4 = testRelation.subquery('t4) | ||
|
||
val originalQuery = t1 | ||
.join(t2, condition = Some("t1.b".attr === "t2.b".attr)) | ||
.join(t3, condition = Some("t2.b".attr === "t3.b".attr)) | ||
.join(t4, condition = Some("t3.b".attr === "t4.b".attr)).analyze | ||
val correctAnswer = t1.where(IsNotNull('b)) | ||
.join(t2.where(IsNotNull('b)), condition = Some("t1.b".attr === "t2.b".attr)) | ||
.join(t3.where(IsNotNull('b)), condition = Some("t2.b".attr === "t3.b".attr)) | ||
.join(t4.where(IsNotNull('b)), condition = Some("t3.b".attr === "t4.b".attr)).analyze | ||
val optimized = Optimize.execute(originalQuery) | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
} |
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
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
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