Skip to content

[SPARK-52439][SQL] Support creating check constraint with NULL #51146

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.connector.expressions.filter;

import org.apache.spark.annotation.Evolving;
import org.apache.spark.sql.connector.expressions.Literal;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DataTypes;

/**
* A predicate that always evaluates to {@code null}.
*
* @since 4.1.0
*/
@Evolving
public final class AlwaysNull extends Predicate implements Literal<Boolean> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to add it? can v2 Literal do the work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be a V2 Predicate:
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/V2ExpressionBuilder.scala#L58

Also, having AlwaysNull besides AlwaysTrue and AlwaysFalse is reasonable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what about the return type, however? Why boolean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that it is a predicate, hence boolean. Let me think a bit more about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally don't mind adding AlwaysNull but I am not sure there is value in allowing CHECK (null), to be honest. I know some databases accept it but it seems useless. Are there databases that reject or ignore these? Or most databases support them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Databases supports CHECK(null):

  • PostgreSQL
  • Oracle

Databases doesn't support CHECK(null):

  • MySQL: ERROR 3813 (HY000) at line 2: Column check constraint 'product_chk_1' references other column.
  • SQL server: The bare token NULL has no boolean meaning: An expression of non-boolean type specified in a context where a condition is expected


public AlwaysNull() {
super("ALWAYS_NULL", new Predicate[]{});
}

@Override
public Boolean value() {
return null;
}

@Override
public DataType dataType() {
return DataTypes.BooleanType;
}

@Override
public String toString() { return "NULL"; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.sql.connector.catalog.{FunctionCatalog, Identifier}
import org.apache.spark.sql.connector.catalog.functions._
import org.apache.spark.sql.connector.catalog.functions.ScalarFunction.MAGIC_METHOD_NAME
import org.apache.spark.sql.connector.expressions.{BucketTransform, Cast => V2Cast, Expression => V2Expression, FieldReference, GeneralScalarExpression, IdentityTransform, Literal => V2Literal, NamedReference, NamedTransform, NullOrdering => V2NullOrdering, SortDirection => V2SortDirection, SortOrder => V2SortOrder, SortValue, Transform}
import org.apache.spark.sql.connector.expressions.filter.{AlwaysFalse, AlwaysTrue}
import org.apache.spark.sql.connector.expressions.filter.{AlwaysFalse, AlwaysNull, AlwaysTrue}
import org.apache.spark.sql.errors.DataTypeErrors.toSQLId
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.types._
Expand Down Expand Up @@ -210,6 +210,7 @@ object V2ExpressionUtils extends SQLConfHelper with Logging {
def toCatalyst(expr: V2Expression): Option[Expression] = expr match {
case _: AlwaysTrue => Some(Literal.TrueLiteral)
case _: AlwaysFalse => Some(Literal.FalseLiteral)
case _: AlwaysNull => Some(Literal.NullPredicateLiteral)
case l: V2Literal[_] => Some(Literal(l.value, l.dataType))
case r: NamedReference => Some(UnresolvedAttribute(r.fieldNames.toImmutableArraySeq))
case c: V2Cast => toCatalyst(c.expression).map(Cast(_, c.dataType, ansiEnabled = true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ case class CaseWhen(
branches.exists(_._2.nullable) || elseValue.map(_.nullable).getOrElse(true)
}

override def contextIndependentFoldable: Boolean = children.forall(_.contextIndependentFoldable)

override def checkInputDataTypes(): TypeCheckResult = {
if (TypeCoercion.haveSameType(inputTypesForMerging)) {
// Make sure all branch conditions are boolean types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.apache.spark.sql.catalyst.util.V2ExpressionBuilder
import org.apache.spark.sql.connector.catalog.constraints.Constraint
import org.apache.spark.sql.connector.expressions.FieldReference
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.types.{BooleanType, DataType}
import org.apache.spark.sql.types.{AbstractDataType, BooleanType, DataType}

trait TableConstraint extends Expression with Unevaluable {
/** Convert to a data source v2 constraint */
Expand Down Expand Up @@ -122,9 +122,12 @@ case class CheckConstraint(
override val tableName: String = null,
override val userProvidedCharacteristic: ConstraintCharacteristic = ConstraintCharacteristic.empty)
extends UnaryExpression
with TableConstraint {
with TableConstraint
with ImplicitCastInputTypes {
// scalastyle:on line.size.limit

override def inputTypes: Seq[AbstractDataType] = Seq(BooleanType)

def toV2Constraint: Constraint = {
val predicate = new V2ExpressionBuilder(child, true).buildPredicate().orNull
val enforced = userProvidedCharacteristic.enforced.getOrElse(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ object Literal {

val FalseLiteral: Literal = Literal(false, BooleanType)

val NullPredicateLiteral: Literal = Literal(null, BooleanType)

def apply(v: Any): Literal = v match {
case i: Int => Literal(i, IntegerType)
case l: Long => Literal(l, LongType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object ConstantFolding extends Rule[LogicalPlan] {
case _ => false
}

private def constantFolding(
private[sql] def constantFolding(
e: Expression,
isConditionalBranch: Boolean = false): Expression = e match {
case c: ConditionalExpression if !c.foldable =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import org.apache.spark.internal.LogKeys.EXPR
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, AggregateFunction, Complete}
import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, StaticInvoke}
import org.apache.spark.sql.catalyst.optimizer.ConstantFolding
import org.apache.spark.sql.connector.catalog.functions.ScalarFunction
import org.apache.spark.sql.connector.expressions.{Cast => V2Cast, Expression => V2Expression, Extract => V2Extract, FieldReference, GeneralScalarExpression, LiteralValue, NullOrdering, SortDirection, SortValue, UserDefinedScalarFunc}
import org.apache.spark.sql.connector.expressions.aggregate.{AggregateFunc, Avg, Count, CountStar, GeneralAggregateFunc, Max, Min, Sum, UserDefinedAggregateFunc}
import org.apache.spark.sql.connector.expressions.filter.{AlwaysFalse, AlwaysTrue, And => V2And, Not => V2Not, Or => V2Or, Predicate => V2Predicate}
import org.apache.spark.sql.connector.expressions.filter.{AlwaysFalse, AlwaysNull, AlwaysTrue, And => V2And, Not => V2Not, Or => V2Or, Predicate => V2Predicate}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{BooleanType, DataType, IntegerType, StringType}

Expand Down Expand Up @@ -83,11 +84,21 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) extends L
case _ => false
}

private def generateExpression(
expr: Expression, isPredicate: Boolean = false): Option[V2Expression] = expr match {
private def translateLiteral(l: Literal): Option[V2Expression] = l match {
case Literal(true, BooleanType) => Some(new AlwaysTrue())
case Literal(false, BooleanType) => Some(new AlwaysFalse())
case Literal(value, dataType) => Some(LiteralValue(value, dataType))
case Literal(null, BooleanType) => Some(new AlwaysNull())
case other => Some(LiteralValue(other.value, other.dataType))
}

private def generateExpression(
expr: Expression, isPredicate: Boolean = false): Option[V2Expression] = expr match {
case literal: Literal => translateLiteral(literal)
case _ if expr.contextIndependentFoldable =>
// If the expression is context independent foldable, we can convert it to a literal.
// This is useful for increasing the coverage of V2 expressions.
val constantExpr = ConstantFolding.constantFolding(expr)
generateExpression(constantExpr, isPredicate)
case col @ ColumnOrField(nameParts) =>
val ref = FieldReference(nameParts)
if (isPredicate && col.dataType.isInstanceOf[BooleanType]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.sql.execution.command.v2

import org.apache.spark.SparkRuntimeException
import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
import org.apache.spark.sql.catalyst.plans.logical.Filter
import org.apache.spark.sql.connector.catalog.Table
import org.apache.spark.sql.connector.catalog.constraints.Check
import org.apache.spark.sql.execution.command.DDLCommandTestUtils
Expand Down Expand Up @@ -1150,4 +1151,47 @@ class CheckConstraintSuite extends QueryTest with CommandSuiteBase with DDLComma
}
}
}

test("Check constraint with constant valid expression should be optimized out") {
Seq(
"1 > 0",
"abs(-99) < 100",
"null",
"current_date() > DATE'2023-01-01'"
).foreach { constant =>
withNamespaceAndTable("ns", "tbl", nonPartitionCatalog) { t =>
sql(s"CREATE TABLE $t (id INT, value INT," +
s" CONSTRAINT positive_id CHECK ($constant)) $defaultUsing")
val optimizedPlan =
sql(s"INSERT INTO $t VALUES (1, 10), (2, 20)").queryExecution.optimizedPlan
val filter = optimizedPlan.collectFirst {
case f: Filter => f
}
assert(filter.isEmpty)
}
}
}

test("Check constraint with constant invalid expression should throw error") {
Seq(
"1 < 0",
"abs(-99) > 100",
"current_date() < DATE'2023-01-01'"
).foreach { constant =>
withNamespaceAndTable("ns", "tbl", nonPartitionCatalog) { t =>
sql(s"CREATE TABLE $t (id INT, value INT," +
s" CONSTRAINT positive_id CHECK ($constant)) $defaultUsing")
val error = intercept[SparkRuntimeException] {
sql(s"INSERT INTO $t VALUES (1, 10), (2, 20)")
}
checkError(
exception = error,
condition = "CHECK_CONSTRAINT_VIOLATION",
sqlState = "23001",
parameters = Map("constraintName" -> "positive_id", "expression" -> constant,
"values" -> "")
)
}
}
}
}
Loading