-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-47210][SQL] Addition of implicit casting without indeterminate support #45383
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
Changes from 12 commits
b34544a
fdbfa44
ce9b027
e537190
b5a79c1
8321d0c
d178233
a4b9be7
a6e7662
e1d7ad5
b3b1356
7773d13
198a728
255b1ab
9ce417f
50f3aa2
ca0c84d
880a1b1
56d6c7c
94e5259
ccb52ba
9b1387b
c9974e1
fca9a65
660d664
c8edd93
a91490b
49a8d61
4c4cd84
66122a6
50f46e4
cc86a87
c01e80c
cc797a2
5d001ee
c68fc7d
1c926ab
dec39bf
e808446
ca1a23a
116931c
af487a2
4ba7055
e490e42
00e88e7
85b4d16
30f7225
e89a354
788dc06
75c0140
2918413
f6ed55a
98960c0
de623c8
a92b4e1
f7f3011
f67808e
815ce42
7fca38a
b19b0eb
a111f03
55bdd9b
a7228be
27a72c6
18ada04
38670af
01d891e
c5daf86
9ac5678
0f1757d
506c8c0
f743cf8
4f8fe1d
52bf4dc
7cbeafe
3e92e92
b23e106
880ebed
f96ecd9
e1e0cf4
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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| --- | ||
| layout: global | ||
| title: COLLATION_MISMATCH error class | ||
| displayTitle: COLLATION_MISMATCH error class | ||
| license: | | ||
| 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. | ||
| --- | ||
|
|
||
| <!-- | ||
| DO NOT EDIT THIS FILE. | ||
| It was generated automatically by `org.apache.spark.SparkThrowableSuite`. | ||
| --> | ||
|
|
||
| [SQLSTATE: 42P21](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation) | ||
|
|
||
| Could not determine which collation to use for string comparison. | ||
|
|
||
| This error class has the following derived error classes: | ||
|
|
||
| ## EXPLICIT | ||
|
|
||
| Error occurred due to the mismatch between explicit collations: `<explicitTypes>`. Decide on a single explicit collation and remove others. | ||
|
|
||
| ## IMPLICIT | ||
|
|
||
| Error occurred due to the mismatch between multiple implicit non-default collations. Use COLLATE function to set the collation explicitly. | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.TypeCoercion.{castStringType, hasStringType} | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
@@ -90,6 +91,7 @@ object AnsiTypeCoercion extends TypeCoercionBase { | |
| Division :: | ||
| IntegralDivision :: | ||
| ImplicitTypeCasts :: | ||
| CollationTypeCasts :: | ||
| DateTimeOperations :: | ||
| WindowFrameCoercion :: | ||
| GetDateFieldOperations:: Nil) :: Nil | ||
|
|
@@ -138,21 +140,31 @@ object AnsiTypeCoercion extends TypeCoercionBase { | |
| @scala.annotation.tailrec | ||
| private def findWiderTypeForString(dt1: DataType, dt2: DataType): Option[DataType] = { | ||
| (dt1, dt2) match { | ||
| case (StringType, _: IntegralType) => Some(LongType) | ||
| case (StringType, _: FractionalType) => Some(DoubleType) | ||
| case (StringType, NullType) => Some(StringType) | ||
| case (_: StringType, _: IntegralType) => Some(LongType) | ||
| case (_: StringType, _: FractionalType) => Some(DoubleType) | ||
| case (st: StringType, NullType) => Some(st) | ||
| // If a binary operation contains interval type and string, we can't decide which | ||
| // interval type the string should be promoted as. There are many possible interval | ||
| // types, such as year interval, month interval, day interval, hour interval, etc. | ||
| case (StringType, _: AnsiIntervalType) => None | ||
| case (StringType, a: AtomicType) => Some(a) | ||
| case (other, StringType) if other != StringType => findWiderTypeForString(StringType, other) | ||
| case (_: StringType, _: AnsiIntervalType) => None | ||
| case (_: StringType, a: AtomicType) => Some(a) | ||
| case (other, st: StringType) if !other.isInstanceOf[StringType] => | ||
| findWiderTypeForString(st, other) | ||
| case _ => None | ||
| } | ||
| } | ||
|
|
||
| override def findWiderCommonType(types: Seq[DataType]): Option[DataType] = { | ||
| types.foldLeft[Option[DataType]](Some(NullType))((r, c) => | ||
| override def findWiderCommonType(exprs: Seq[Expression], | ||
| failOnIndeterminate: Boolean = false): Option[DataType] = { | ||
| (if (exprs.map(_.dataType).partition(hasStringType)._1.distinct.size > 1) { | ||
mihailomilosevic2001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val collationId = CollationTypeCasts.getOutputCollation(exprs, failOnIndeterminate) | ||
| exprs.map(e => | ||
| if (hasStringType(e.dataType)) { | ||
| castStringType(e.dataType, collationId) | ||
| e | ||
| } | ||
| else e) | ||
| } else exprs).map(_.dataType).foldLeft[Option[DataType]](Some(NullType))((r, c) => | ||
| r match { | ||
| case Some(d) => findWiderTypeForTwo(d, c) | ||
|
Contributor
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 find this pretty weird.
Contributor
Author
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.
|
||
| case _ => None | ||
|
|
@@ -173,6 +185,8 @@ object AnsiTypeCoercion extends TypeCoercionBase { | |
| inType: DataType, | ||
| expectedType: AbstractDataType): Option[DataType] = { | ||
| (inType, expectedType) match { | ||
| case (_: StringType, st: StringType) => | ||
| Some(st) | ||
mihailomilosevic2001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // If the expected type equals the input type, no need to cast. | ||
| case _ if expectedType.acceptsType(inType) => Some(inType) | ||
|
|
||
|
|
@@ -188,21 +202,21 @@ object AnsiTypeCoercion extends TypeCoercionBase { | |
|
|
||
| // This type coercion system will allow implicit converting String type as other | ||
| // primitive types, in case of breaking too many existing Spark SQL queries. | ||
| case (StringType, a: AtomicType) => | ||
| case (_: StringType, a: AtomicType) => | ||
| Some(a) | ||
|
|
||
| // If the target type is any Numeric type, convert the String type as Double type. | ||
| case (StringType, NumericType) => | ||
| case (_: StringType, NumericType) => | ||
| Some(DoubleType) | ||
|
|
||
| // If the target type is any Decimal type, convert the String type as the default | ||
| // Decimal type. | ||
| case (StringType, DecimalType) => | ||
| case (_: StringType, DecimalType) => | ||
| Some(DecimalType.SYSTEM_DEFAULT) | ||
|
|
||
| // If the target type is any timestamp type, convert the String type as the default | ||
| // Timestamp type. | ||
| case (StringType, AnyTimestampType) => | ||
| case (_: StringType, AnyTimestampType) => | ||
| Some(AnyTimestampType.defaultConcreteType) | ||
|
|
||
| case (DateType, AnyTimestampType) => | ||
mihailomilosevic2001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.