-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
[SPARK-13495][SQL] Add Null Filters in the query plan for Filters/Joins based on their data constraints #11372
Changes from all commits
cc4323f
28050b3
0b1520c
2a469e8
80dab7e
013f97a
31b1700
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
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. I had a few more test cases when i tried to this. Can you see if any of them should be added? We should also have outer join tests to make sure they don't add the is not null filter. 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. Added a modified version of multi-join test based on your patch and a new test for outer join. As discussed offline, there is an additional test for non-equal keys ( |
||
} |
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.
you haven't done anything with a === 1 right? There's still no logic that a === 1 has a not nullable
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.
We can do that in a follow up pr