-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-47296][SQL][COLLATION] Fail unsupported functions for non-binary collations #45422
Changes from 24 commits
e0485fc
355eee9
cde9eff
616335b
e6bec45
77e606c
ee72ed3
8bc10f5
8ee0f58
d3a9e70
c11c458
4778951
1582f24
8d75846
63caef6
38c3906
1d60cef
2f5aba9
b9b185d
3fed6cb
638c6d4
af7611f
ceabb4a
0f4a6b1
9b24fba
831197a
99b36b0
9051923
6986b8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,11 @@ object AnsiTypeCoercion extends TypeCoercionBase { | |
case (StringType, AnyTimestampType) => | ||
Some(AnyTimestampType.defaultConcreteType) | ||
|
||
// If a function expects a StringType, no StringType instance should be implicitly cast to | ||
// StringType with a collation that's not accepted (aka. lockdown unsupported collations). | ||
case (StringType, StringType) => None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this case match covered by the first case match There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
case (StringType, _: StringTypeCollated) => None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case should be put before |
||
|
||
case (DateType, AnyTimestampType) => | ||
Some(AnyTimestampType.defaultConcreteType) | ||
|
||
|
@@ -215,6 +220,10 @@ object AnsiTypeCoercion extends TypeCoercionBase { | |
None | ||
} | ||
|
||
// "canANSIStoreAssign" doesn't account for targets extending StringTypeCollated, but | ||
// ANSIStoreAssign is generally expected to return "true" for (AtomicType, StringType) | ||
case (_: AtomicType, _: StringTypeCollated) => Some(StringType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is. canANSIStoreAssign has a rule for casting AtomicType to StringType, but since StringTypeCollated does not extend StringType, but only AbstractDataType, this cast rule will not be picked up. But I would say this rule has to be improved to check for all canANsiStoreAssign rules. |
||
|
||
// When we reach here, input type is not acceptable for any types in this type collection, | ||
// try to find the first one we can implicitly cast. | ||
case (_, TypeCollection(types)) => | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -994,8 +994,12 @@ object TypeCoercion extends TypeCoercionBase { | |
case (StringType, datetime: DatetimeType) => datetime | ||
case (StringType, AnyTimestampType) => AnyTimestampType.defaultConcreteType | ||
case (StringType, BinaryType) => BinaryType | ||
case (st: StringType, StringType) => st | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reading the code around here, I think |
||
case (st: StringType, _: StringTypeCollated) => st | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will be covered by the last default case match? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and no. The following two lines would have made a cast, but I changed them so they doesn't. |
||
// Cast any atomic type to string. | ||
case (any: AtomicType, StringType) if any != StringType => StringType | ||
uros-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case (any: AtomicType, st: StringTypeCollated) | ||
if any != st.defaultConcreteType => st.defaultConcreteType | ||
|
||
// When we reach here, input type is not acceptable for any types in this type collection, | ||
// try to find the first one we can implicitly cast. | ||
|
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] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove even this guy and push the check into
StringTypeBinaryLcase
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure this is possible. StringTypeBinaryLcase does not extend StringType, and the point of this function for now is to call it on StringType object in acceptsType to check if we should let the function proceed with that input.