-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-34246][SQL] New type coercion syntax rules in ANSI mode #31349
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
Closed
gengliangwang
wants to merge
23
commits into
apache:master
from
gengliangwang:ansiImplicitConversion
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cef9e31
new ansi implicit cast rules
gengliangwang 3566196
remove non-ascii char
gengliangwang 37722ee
special handling string literal
gengliangwang d135e83
WindowFrameCoercion
gengliangwang f3a5f39
update test outputs
gengliangwang 7d33ffd
revise comments
gengliangwang ab694bd
address maropu's comments
gengliangwang f48973e
update sql test output
gengliangwang 33dd04b
promote string literal in In predicates
gengliangwang 4fabb65
address nit comment
gengliangwang f831cf1
add comments for APIs
gengliangwang ae5caa2
address comments
gengliangwang 6f6bd87
fix compiling
gengliangwang a5058a6
revise rule `PromoteStringLiterals` and add test case
gengliangwang 2590ec5
add test cases
gengliangwang caa9449
implicit cast string literals recursively
gengliangwang bd58d33
fix unit tests
gengliangwang 39048ab
add test cases
gengliangwang b64b926
fix test failure
gengliangwang 025f915
address comments
gengliangwang 1c8a911
update with.sql
gengliangwang 7a57144
fix query outputs
gengliangwang 1f5ecdc
Merge remote-tracking branch 'upstream/master' into ansiImplicitConve…
gengliangwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
267 changes: 267 additions & 0 deletions
267
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala
This file contains hidden or 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,267 @@ | ||
/* | ||
* 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.analysis | ||
|
||
import org.apache.spark.sql.catalyst.analysis.TypeCoercion.numericPrecedence | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.types._ | ||
|
||
/** | ||
* In Spark ANSI mode, the type coercion rules are based on the type precedence lists of the input | ||
* data types. | ||
* As per the section "Type precedence list determination" of "ISO/IEC 9075-2:2011 | ||
* Information technology - Database languages - SQL - Part 2: Foundation (SQL/Foundation)", | ||
* the type precedence lists of primitive data types are as following: | ||
* * Byte: Byte, Short, Int, Long, Decimal, Float, Double | ||
* * Short: Short, Int, Long, Decimal, Float, Double | ||
* * Int: Int, Long, Decimal, Float, Double | ||
* * Long: Long, Decimal, Float, Double | ||
* * Decimal: Float, Double, or any wider Numeric type | ||
* * Float: Float, Double | ||
* * Double: Double | ||
* * String: String | ||
* * Date: Date, Timestamp | ||
* * Timestamp: Timestamp | ||
* * Binary: Binary | ||
* * Boolean: Boolean | ||
* * Interval: Interval | ||
* As for complex data types, Spark will determine the precedent list recursively based on their | ||
* sub-types and nullability. | ||
* | ||
* With the definition of type precedent list, the general type coercion rules are as following: | ||
* * Data type S is allowed to be implicitly cast as type T iff T is in the precedence list of S | ||
* * Comparison is allowed iff the data type precedence list of both sides has at least one common | ||
* element. When evaluating the comparison, Spark casts both sides as the tightest common data | ||
* type of their precedent lists. | ||
* * There should be at least one common data type among all the children's precedence lists for | ||
* the following operators. The data type of the operator is the tightest common precedent | ||
* data type. | ||
* * In | ||
* * Except | ||
* * Intersect | ||
* * Greatest | ||
* * Least | ||
* * Union | ||
* * If | ||
* * CaseWhen | ||
* * CreateArray | ||
* * Array Concat | ||
* * Sequence | ||
* * MapConcat | ||
* * CreateMap | ||
* * For complex types (struct, array, map), Spark recursively looks into the element type and | ||
* applies the rules above. | ||
* Note: this new type coercion system will allow implicit converting String type literals as other | ||
* primitive types, in case of breaking too many existing Spark SQL queries. This is a special | ||
* rule and it is not from the ANSI SQL standard. | ||
*/ | ||
object AnsiTypeCoercion extends TypeCoercionBase { | ||
override def typeCoercionRules: List[Rule[LogicalPlan]] = | ||
InConversion :: | ||
maropu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
WidenSetOperationTypes :: | ||
maropu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
PromoteStringLiterals :: | ||
DecimalPrecision :: | ||
FunctionArgumentConversion :: | ||
ConcatCoercion :: | ||
MapZipWithCoercion :: | ||
EltCoercion :: | ||
CaseWhenCoercion :: | ||
IfCoercion :: | ||
StackCoercion :: | ||
Division :: | ||
IntegralDivision :: | ||
ImplicitTypeCasts :: | ||
DateTimeOperations :: | ||
WindowFrameCoercion :: | ||
StringLiteralCoercion :: | ||
Nil | ||
|
||
override def findTightestCommonType(t1: DataType, t2: DataType): Option[DataType] = { | ||
(t1, t2) match { | ||
case (t1, t2) if t1 == t2 => Some(t1) | ||
case (NullType, t1) => Some(t1) | ||
case (t1, NullType) => Some(t1) | ||
|
||
case (t1: IntegralType, t2: DecimalType) if t2.isWiderThan(t1) => | ||
Some(t2) | ||
case (t1: DecimalType, t2: IntegralType) if t1.isWiderThan(t2) => | ||
Some(t1) | ||
|
||
case (t1: NumericType, t2: NumericType) | ||
if !t1.isInstanceOf[DecimalType] && !t2.isInstanceOf[DecimalType] => | ||
val index = numericPrecedence.lastIndexWhere(t => t == t1 || t == t2) | ||
val widerType = numericPrecedence(index) | ||
if (widerType == FloatType) { | ||
// If the input type is an Integral type and a Float type, simply return Double type as | ||
// the tightest common type to avoid potential precision loss on converting the Integral | ||
// type as Float type. | ||
Some(DoubleType) | ||
maropu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} else { | ||
Some(widerType) | ||
} | ||
maropu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
case (_: TimestampType, _: DateType) | (_: DateType, _: TimestampType) => | ||
Some(TimestampType) | ||
|
||
case (t1, t2) => findTypeForComplex(t1, t2, findTightestCommonType) | ||
} | ||
|
||
} | ||
|
||
override def findWiderTypeForTwo(t1: DataType, t2: DataType): Option[DataType] = { | ||
findTightestCommonType(t1, t2) | ||
.orElse(findWiderTypeForDecimal(t1, t2)) | ||
maropu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
.orElse(findTypeForComplex(t1, t2, findWiderTypeForTwo)) | ||
} | ||
|
||
override def findWiderCommonType(types: Seq[DataType]): Option[DataType] = { | ||
types.foldLeft[Option[DataType]](Some(NullType))((r, c) => | ||
r match { | ||
case Some(d) => findWiderTypeForTwo(d, c) | ||
case _ => None | ||
}) | ||
} | ||
|
||
override def implicitCast(e: Expression, expectedType: AbstractDataType): Option[Expression] = { | ||
implicitCast(e.dataType, expectedType, e.foldable).map { dt => | ||
if (dt == e.dataType) e else Cast(e, dt) | ||
} | ||
} | ||
|
||
/** | ||
* In Ansi mode, the implicit cast is only allow when `expectedType` is in the type precedent | ||
* list of `inType`. | ||
*/ | ||
private def implicitCast( | ||
inType: DataType, | ||
expectedType: AbstractDataType, | ||
isInputFoldable: Boolean): Option[DataType] = { | ||
(inType, expectedType) match { | ||
// If the expected type equals the input type, no need to cast. | ||
case _ if expectedType.acceptsType(inType) => Some(inType) | ||
|
||
// Cast null type (usually from null literals) into target types | ||
case (NullType, target) => Some(target.defaultConcreteType) | ||
|
||
// This type coercion system will allow implicit converting String type literals as other | ||
// primitive types, in case of breaking too many existing Spark SQL queries. | ||
case (StringType, a: AtomicType) if isInputFoldable => | ||
Some(a) | ||
|
||
// If the target type is any Numeric type, convert the String type literal as Double type. | ||
case (StringType, NumericType) if isInputFoldable => | ||
Some(DoubleType) | ||
|
||
// If the target type is any Decimal type, convert the String type literal as Double type. | ||
case (StringType, DecimalType) if isInputFoldable => | ||
Some(DecimalType.SYSTEM_DEFAULT) | ||
|
||
// If input is a numeric type but not decimal, and we expect a decimal type, | ||
// cast the input to decimal. | ||
case (d: NumericType, DecimalType) => Some(DecimalType.forType(d)) | ||
|
||
case (n1: NumericType, n2: NumericType) => | ||
val widerType = findWiderTypeForTwo(n1, n2) | ||
widerType match { | ||
// if the expected type is Float type, we should still return Float type. | ||
case Some(DoubleType) if n1 != DoubleType && n2 == FloatType => Some(FloatType) | ||
|
||
case Some(dt) if dt == n2 => Some(dt) | ||
|
||
case _ => None | ||
} | ||
|
||
case (DateType, TimestampType) => Some(TimestampType) | ||
|
||
// 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)) => | ||
types.flatMap(implicitCast(inType, _, isInputFoldable)).headOption | ||
|
||
// Implicit cast between array types. | ||
// | ||
// Compare the nullabilities of the from type and the to type, check whether the cast of | ||
// the nullability is resolvable by the following rules: | ||
// 1. If the nullability of the to type is true, the cast is always allowed; | ||
// 2. If the nullabilities of both the from type and the to type are false, the cast is | ||
// allowed. | ||
// 3. Otherwise, the cast is not allowed | ||
case (ArrayType(fromType, containsNullFrom), ArrayType(toType: DataType, containsNullTo)) | ||
if Cast.resolvableNullability(containsNullFrom, containsNullTo) => | ||
implicitCast(fromType, toType, isInputFoldable).map(ArrayType(_, containsNullTo)) | ||
|
||
// Implicit cast between Map types. | ||
// Follows the same semantics of implicit casting between two array types. | ||
// Refer to documentation above. | ||
case (MapType(fromKeyType, fromValueType, fn), MapType(toKeyType, toValueType, tn)) | ||
if Cast.resolvableNullability(fn, tn) => | ||
val newKeyType = implicitCast(fromKeyType, toKeyType, isInputFoldable) | ||
val newValueType = implicitCast(fromValueType, toValueType, isInputFoldable) | ||
if (newKeyType.isDefined && newValueType.isDefined) { | ||
Some(MapType(newKeyType.get, newValueType.get, tn)) | ||
} else { | ||
None | ||
} | ||
|
||
case _ => None | ||
} | ||
} | ||
|
||
override def canCast(from: DataType, to: DataType): Boolean = AnsiCast.canCast(from, to) | ||
|
||
/** | ||
* Promotes string literals that appear in arithmetic and comparison expressions. | ||
*/ | ||
object PromoteStringLiterals extends TypeCoercionRule { | ||
maropu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
private def castExpr(expr: Expression, targetType: DataType): Expression = { | ||
(expr.dataType, targetType) match { | ||
case (NullType, dt) => Literal.create(null, targetType) | ||
case (l, dt) if (l != dt) => Cast(expr, targetType) | ||
case _ => expr | ||
} | ||
} | ||
|
||
override protected def coerceTypes( | ||
plan: LogicalPlan): LogicalPlan = plan resolveExpressions { | ||
// Skip nodes who's children have not been resolved yet. | ||
case e if !e.childrenResolved => e | ||
|
||
case b @ BinaryOperator(left @ StringType(), right @ AtomicType()) if left.foldable => | ||
b.makeCopy(Array(castExpr(left, right.dataType), right)) | ||
|
||
case b @ BinaryOperator(left @ AtomicType(), right @ StringType()) if right.foldable => | ||
b.makeCopy(Array(left, castExpr(right, left.dataType))) | ||
|
||
case Abs(e @ StringType()) if e.foldable => Abs(Cast(e, DoubleType)) | ||
case m @ UnaryMinus(e @ StringType(), _) if e.foldable => | ||
m.withNewChildren(Seq(Cast(e, DoubleType))) | ||
case UnaryPositive(e @ StringType()) if e.foldable => UnaryPositive(Cast(e, DoubleType)) | ||
|
||
// Promotes string literals in `In predicate`. | ||
case p @ In(a, b) | ||
if a.dataType != StringType && b.exists( e => e.foldable && e.dataType == StringType) => | ||
val newList = b.map { | ||
case e @ StringType() if e.foldable => Cast(e, a.dataType) | ||
case other => other | ||
} | ||
p.makeCopy(Array(a, newList)) | ||
} | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.