-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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-17897] [SQL] Fixed IsNotNull Constraint Inference Rule #16067
Changes from 5 commits
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 |
---|---|---|
|
@@ -40,14 +40,13 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT | |
} | ||
|
||
/** | ||
* Infers a set of `isNotNull` constraints from a given set of equality/comparison expressions as | ||
* well as non-nullable attributes. For e.g., if an expression is of the form (`a > 5`), this | ||
* Infers a set of `isNotNull` constraints from null intolerant expressions as well as | ||
* non-nullable attributes. 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] = { | ||
// First, we propagate constraints from the null intolerant expressions. | ||
var isNotNullConstraints: Set[Expression] = | ||
constraints.flatMap(scanNullIntolerantExpr).map(IsNotNull(_)) | ||
var isNotNullConstraints: Set[Expression] = constraints.flatMap(inferIsNotNullConstraints) | ||
|
||
// Second, we infer additional constraints from non-nullable attributes that are part of the | ||
// operator's output | ||
|
@@ -57,14 +56,29 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT | |
isNotNullConstraints -- constraints | ||
} | ||
|
||
/** | ||
* Infer the Attribute-specific IsNotNull constraints from the null intolerant child expressions | ||
* of constraints. | ||
*/ | ||
private def inferIsNotNullConstraints(constraint: Expression): Seq[Expression] = | ||
constraint match { | ||
case IsNotNull(_: Attribute) => constraint :: Nil | ||
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. we don't need this case, I think it can be covered by the next case? 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. Yeah, my original idea is to do a fast stop. After rethinking it, it might be fine. |
||
// When the root is IsNotNull, we can push IsNotNull through the child null intolerant | ||
// expressions | ||
case IsNotNull(expr) => scanNullIntolerantExpr(expr).map(IsNotNull(_)) | ||
// Constraints always return true for all the inputs. That means, null will never be returned. | ||
// Thus, we can infer `IsNotNull(constraint)`, and also push IsNotNull through the child | ||
// null intolerant expressions. | ||
case _ => scanNullIntolerantExpr(constraint).map(IsNotNull(_)) | ||
} | ||
|
||
/** | ||
* Recursively explores the expressions which are null intolerant and returns all attributes | ||
* in these expressions. | ||
*/ | ||
private def scanNullIntolerantExpr(expr: Expression): Seq[Attribute] = expr match { | ||
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. shall we rename it to 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. Sure |
||
case a: Attribute => Seq(a) | ||
case _: NullIntolerant | IsNotNull(_: NullIntolerant) => | ||
expr.children.flatMap(scanNullIntolerantExpr) | ||
case _: NullIntolerant => expr.children.flatMap(scanNullIntolerantExpr) | ||
case _ => Seq.empty[Attribute] | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,6 +351,15 @@ class ConstraintPropagationSuite extends SparkFunSuite { | |
IsNotNull(IsNotNull(resolveColumn(tr, "b"))), | ||
IsNotNull(resolveColumn(tr, "a")), | ||
IsNotNull(resolveColumn(tr, "c"))))) | ||
|
||
verifyConstraints( | ||
tr.where('a.attr === 1 && IsNotNull(resolveColumn(tr, "b")) && | ||
IsNotNull(resolveColumn(tr, "c"))).analyze.constraints, | ||
ExpressionSet(Seq( | ||
resolveColumn(tr, "a") === 1, | ||
IsNotNull(resolveColumn(tr, "c")), | ||
IsNotNull(resolveColumn(tr, "a")), | ||
IsNotNull(resolveColumn(tr, "b"))))) | ||
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. The test case is added. |
||
} | ||
|
||
test("infer IsNotNull constraints from non-nullable attributes") { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1697,6 +1697,12 @@ class DataFrameSuite extends QueryTest with SharedSQLContext { | |
expr = "cast((_1 + _2) as boolean)", expectedNonNullableColumns = Seq("_1", "_2")) | ||
} | ||
|
||
test("SPARK-17897: Fixed IsNotNull Constraint Inference Rule") { | ||
val data = Seq[java.lang.Integer](1, null).toDF("key") | ||
checkAnswer(data.filter("not key is not null"), Row(null)) | ||
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. shall we use DataFrame API? i.e. 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. sure. |
||
checkAnswer(data.filter("not ((- key) is not null)"), Row(null)) | ||
} | ||
|
||
test("SPARK-17957: outer join + na.fill") { | ||
val df1 = Seq((1, 2), (2, 3)).toDF("a", "b") | ||
val df2 = Seq((2, 5), (3, 4)).toDF("a", "c") | ||
|
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.
Can this infer
IsNotNull(a)
,IsNotNull(b)
fromIsNotNull(a) && IsNotNull(b)
?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.
It should be split before entering this function. Let me add a test case.
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.
This change simply ignores all
IsNotNull
s which are not the top expression. The above case works becauseFilter
splits it. But if the constraint looks likeCast(IsNotNull(a), Integer) == 1
, we won't inferIsNotNull(a)
from it, right?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.
Yes. After this PR, we do not support it. This is a pretty rare case, right?
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.
Yeah.