-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SC-56391] Add char/varchar length check to Delta CONSTRAINT
This PR adds the char/varchar type input string length check to Delta CONSTRAINT, so that it applies to all the write paths. This also introduces a reserved constraint name that end-users can't use: `__CHAR_VARCHAR_STRING_LENGTH_CHECK__` This PR removes the char type padding from Delta INSERT, to be consistent with other write paths. We can add the padding back when we have the infra to do it in the future. new tests Before this PR, when writing data to delta tables via UPDATE/MERGE, no char/varchar check is done. The written data may exceed the char/varchar length limitation, which violates the char/varchar semantic. This PR fixes this issue by adding the length check in Delta CONSTRAINTS so that it applies to all the write paths. This PR also removes the char padding logic from Delta INSERT, to be consistent with other write paths that only do length check. GitOrigin-RevId: 91e96ee9643bf1eba4d6ed159858d433e8357c95
- Loading branch information
Showing
9 changed files
with
105 additions
and
220 deletions.
There are no files selected for viewing
201 changes: 0 additions & 201 deletions
201
core/src/main/scala/org/apache/spark/sql/delta/CharVarcharUtils.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/scala/org/apache/spark/sql/delta/constraints/CharVarcharConstraint.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.delta.constraints | ||
|
||
import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.util.CharVarcharUtils | ||
import org.apache.spark.sql.types._ | ||
|
||
// Delta implements char/varchar length check with CONSTRAINTS, and needs to generate predicate | ||
// expression which is different from the OSS version. | ||
object CharVarcharConstraint { | ||
final val INVARIANT_NAME = "__CHAR_VARCHAR_STRING_LENGTH_CHECK__" | ||
|
||
def stringConstraints(schema: StructType): Seq[Constraint] = { | ||
schema.flatMap { f => | ||
val targetType = CharVarcharUtils.getRawType(f.metadata).getOrElse(f.dataType) | ||
val col = UnresolvedAttribute(Seq(f.name)) | ||
checkStringLength(col, targetType).map { lengthCheckExpr => | ||
Constraints.Check(INVARIANT_NAME, lengthCheckExpr) | ||
} | ||
} | ||
} | ||
|
||
private def checkStringLength(expr: Expression, dt: DataType): Option[Expression] = dt match { | ||
case VarcharType(length) => | ||
Some(Or(IsNull(expr), LessThanOrEqual(Length(expr), Literal(length)))) | ||
|
||
case CharType(length) => | ||
checkStringLength(expr, VarcharType(length)) | ||
|
||
case StructType(fields) => | ||
fields.zipWithIndex.flatMap { case (f, i) => | ||
checkStringLength(GetStructField(expr, i, Some(f.name)), f.dataType) | ||
}.reduceOption(And(_, _)) | ||
|
||
case ArrayType(et, containsNull) => | ||
checkStringLengthInArray(expr, et, containsNull) | ||
|
||
case MapType(kt, vt, valueContainsNull) => | ||
(checkStringLengthInArray(MapKeys(expr), kt, false) ++ | ||
checkStringLengthInArray(MapValues(expr), vt, valueContainsNull)) | ||
.reduceOption(And(_, _)) | ||
|
||
case _ => None | ||
} | ||
|
||
private def checkStringLengthInArray( | ||
arr: Expression, et: DataType, containsNull: Boolean): Option[Expression] = { | ||
val cleanedType = CharVarcharUtils.replaceCharVarcharWithString(et) | ||
val param = NamedLambdaVariable("x", cleanedType, containsNull) | ||
checkStringLength(param, et).map { checkExpr => | ||
Or(IsNull(arr), ArrayForAll(arr, LambdaFunction(checkExpr, Seq(param)))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.