forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-47296][SQL][COLLATION] Fail unsupported functions for non-bina…
…ry collations ### What changes were proposed in this pull request? ### Why are the changes needed? Currently, all `StringType` arguments passed to built-in string functions in Spark SQL get treated as binary strings. This behaviour is incorrect for almost all collationIds except the default (0), and we should instead warn the user if they try to use an unsupported collation for the given function. Over time, we should implement the appropriate support for these (function, collation) pairs, but until then - we should have a way to fail unsupported statements in query analysis. ### Does this PR introduce _any_ user-facing change? Yes, users will now get appropriate errors when they try to use an unsupported collation with a given string function. ### How was this patch tested? Tests in CollationSuite to check if these functions work for binary collations and throw exceptions for others. ### Was this patch authored or co-authored using generative AI tooling? Yes. Closes apache#45422 from uros-db/regexp-functions. Lead-authored-by: Uros Bojanic <[email protected]> Co-authored-by: Mihailo Milosevic <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information
Showing
9 changed files
with
637 additions
and
29 deletions.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
...t/src/main/scala/org/apache/spark/sql/catalyst/expressions/CollationTypeConstraints.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,77 @@ | ||
/* | ||
* 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.catalyst.expressions | ||
|
||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch | ||
import org.apache.spark.sql.catalyst.util.CollationFactory | ||
import org.apache.spark.sql.types.{AbstractDataType, DataType, StringType} | ||
|
||
object CollationTypeConstraints { | ||
|
||
def checkCollationCompatibility(collationId: Int, dataTypes: Seq[DataType]): TypeCheckResult = { | ||
val collationName = CollationFactory.fetchCollation(collationId).collationName | ||
// Additional check needed for collation compatibility | ||
dataTypes.collectFirst { | ||
case stringType: StringType if stringType.collationId != collationId => | ||
val collation = CollationFactory.fetchCollation(stringType.collationId) | ||
DataTypeMismatch( | ||
errorSubClass = "COLLATION_MISMATCH", | ||
messageParameters = Map( | ||
"collationNameLeft" -> collationName, | ||
"collationNameRight" -> collation.collationName | ||
) | ||
) | ||
} getOrElse TypeCheckResult.TypeCheckSuccess | ||
} | ||
|
||
} | ||
|
||
/** | ||
* StringTypeCollated is an abstract class for StringType with collation support. | ||
*/ | ||
abstract class StringTypeCollated extends AbstractDataType { | ||
override private[sql] def defaultConcreteType: DataType = StringType | ||
} | ||
|
||
/** | ||
* Use StringTypeBinary for expressions supporting only binary collation. | ||
*/ | ||
case object StringTypeBinary extends StringTypeCollated { | ||
override private[sql] def simpleString: String = "string_binary" | ||
override private[sql] def acceptsType(other: DataType): Boolean = | ||
other.isInstanceOf[StringType] && other.asInstanceOf[StringType].isBinaryCollation | ||
} | ||
|
||
/** | ||
* Use StringTypeBinaryLcase for expressions supporting only binary and lowercase collation. | ||
*/ | ||
case object StringTypeBinaryLcase extends StringTypeCollated { | ||
override private[sql] def simpleString: String = "string_binary_lcase" | ||
override private[sql] def acceptsType(other: DataType): Boolean = | ||
other.isInstanceOf[StringType] && (other.asInstanceOf[StringType].isBinaryCollation || | ||
other.asInstanceOf[StringType].isLowercaseCollation) | ||
} | ||
|
||
/** | ||
* Use StringTypeAnyCollation for expressions supporting all possible collation types. | ||
*/ | ||
case object StringTypeAnyCollation extends StringTypeCollated { | ||
override private[sql] def simpleString: String = "string_any_collation" | ||
override private[sql] def acceptsType(other: DataType): Boolean = other.isInstanceOf[StringType] | ||
} |
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.