Skip to content

Commit aa57083

Browse files
sameeragarwalrxin
authored andcommitted
[SPARK-17228][SQL] Not infer/propagate non-deterministic constraints
## What changes were proposed in this pull request? Given that filters based on non-deterministic constraints shouldn't be pushed down in the query plan, unnecessarily inferring them is confusing and a source of potential bugs. This patch simplifies the inferring logic by simply ignoring them. ## How was this patch tested? Added a new test in `ConstraintPropagationSuite`. Author: Sameer Agarwal <[email protected]> Closes #14795 from sameeragarwal/deterministic-constraints. (cherry picked from commit ac27557) Signed-off-by: Reynold Xin <[email protected]>
1 parent 3258f27 commit aa57083

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT
3535
.union(inferAdditionalConstraints(constraints))
3636
.union(constructIsNotNullConstraints(constraints))
3737
.filter(constraint =>
38-
constraint.references.nonEmpty && constraint.references.subsetOf(outputSet))
38+
constraint.references.nonEmpty && constraint.references.subsetOf(outputSet) &&
39+
constraint.deterministic)
3940
}
4041

4142
/**

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,21 @@ class ConstraintPropagationSuite extends SparkFunSuite {
352352
verifyConstraints(tr.analyze.constraints,
353353
ExpressionSet(Seq(IsNotNull(resolveColumn(tr, "b")), IsNotNull(resolveColumn(tr, "c")))))
354354
}
355+
356+
test("not infer non-deterministic constraints") {
357+
val tr = LocalRelation('a.int, 'b.string, 'c.int)
358+
359+
verifyConstraints(tr
360+
.where('a.attr === Rand(0))
361+
.analyze.constraints,
362+
ExpressionSet(Seq(IsNotNull(resolveColumn(tr, "a")))))
363+
364+
verifyConstraints(tr
365+
.where('a.attr === InputFileName())
366+
.where('a.attr =!= 'c.attr)
367+
.analyze.constraints,
368+
ExpressionSet(Seq(resolveColumn(tr, "a") =!= resolveColumn(tr, "c"),
369+
IsNotNull(resolveColumn(tr, "a")),
370+
IsNotNull(resolveColumn(tr, "c")))))
371+
}
355372
}

0 commit comments

Comments
 (0)