diff options
Diffstat (limited to 'sql/catalyst')
-rw-r--r-- | sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala | 38 | ||||
-rw-r--r-- | sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala | 9 |
2 files changed, 32 insertions, 15 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala index e9bfa09b7d..d31164fe94 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala @@ -39,29 +39,37 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT } /** - * Infers a set of `isNotNull` constraints from a given set of equality/comparison expressions. - * For e.g., if an expression is of the form (`a > 5`), this returns a constraint of the form - * `isNotNull(a)` + * 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 + * returns a constraint of the form `isNotNull(a)` */ private def constructIsNotNullConstraints(constraints: Set[Expression]): Set[Expression] = { - // Currently we only propagate constraints if the condition consists of equality - // and ranges. For all other cases, we return an empty set of constraints - constraints.map { + var isNotNullConstraints = Set.empty[Expression] + + // First, we propagate constraints if the condition consists of equality and ranges. For all + // other cases, we return an empty set of constraints + constraints.foreach { case EqualTo(l, r) => - Set(IsNotNull(l), IsNotNull(r)) + isNotNullConstraints ++= Set(IsNotNull(l), IsNotNull(r)) case GreaterThan(l, r) => - Set(IsNotNull(l), IsNotNull(r)) + isNotNullConstraints ++= Set(IsNotNull(l), IsNotNull(r)) case GreaterThanOrEqual(l, r) => - Set(IsNotNull(l), IsNotNull(r)) + isNotNullConstraints ++= Set(IsNotNull(l), IsNotNull(r)) case LessThan(l, r) => - Set(IsNotNull(l), IsNotNull(r)) + isNotNullConstraints ++= Set(IsNotNull(l), IsNotNull(r)) case LessThanOrEqual(l, r) => - Set(IsNotNull(l), IsNotNull(r)) + isNotNullConstraints ++= Set(IsNotNull(l), IsNotNull(r)) case Not(EqualTo(l, r)) => - Set(IsNotNull(l), IsNotNull(r)) - case _ => - Set.empty[Expression] - }.foldLeft(Set.empty[Expression])(_ union _.toSet) + isNotNullConstraints ++= Set(IsNotNull(l), IsNotNull(r)) + case _ => // No inference + } + + // Second, we infer additional constraints from non-nullable attributes that are part of the + // operator's output + val nonNullableAttributes = output.filterNot(_.nullable) + isNotNullConstraints ++= nonNullableAttributes.map(IsNotNull).toSet + + isNotNullConstraints -- constraints } /** diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala index f3ab026192..e5063599a3 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala @@ -23,6 +23,7 @@ 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.logical._ +import org.apache.spark.sql.types.{IntegerType, StringType} class ConstraintPropagationSuite extends SparkFunSuite { @@ -217,4 +218,12 @@ class ConstraintPropagationSuite extends SparkFunSuite { IsNotNull(resolveColumn(tr, "a")), IsNotNull(resolveColumn(tr, "b"))))) } + + test("infer IsNotNull constraints from non-nullable attributes") { + val tr = LocalRelation('a.int, AttributeReference("b", IntegerType, nullable = false)(), + AttributeReference("c", StringType, nullable = false)()) + + verifyConstraints(tr.analyze.constraints, + ExpressionSet(Seq(IsNotNull(resolveColumn(tr, "b")), IsNotNull(resolveColumn(tr, "c"))))) + } } |