gengliangwang commented on a change in pull request #33516: URL: https://github.com/apache/spark/pull/33516#discussion_r676771658
########## File path: docs/sql-ref-ansi-compliance.md ########## @@ -160,6 +160,81 @@ SELECT * FROM t; +---+ ``` +### Type coercion +#### Type Promotion and Precedence +When `spark.sql.ansi.enabled` is set to `true`, Spark SQL uses several rules that govern how conflicts between data types are resolved. +At the heart of this conflict resolution is the Type Precedence List which defines whether values of a given data type can be promoted to another data type implicitly. + +| Data type | precedence list(from narrowest to widest) | +|-----------|------------------------------------------------------------------| +| 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 | Decimal -> Float* -> Double | +| Float | Float -> Double | +| Double | Double | +| Date | Date-> Timestamp | +| Timestamp | Timestamp | +| String | String | +| Binary | Binary | +| Boolean | Boolean | +| Interval | Interval | +| Map | Map** | +| Array | Array** | +| Struct | Struct** | + +\* For least common type resolution float is skipped to avoid loss of precision. + +\*\* For a complex type, the precedence rule applies recursively to its component elements. + +Special rules apply for string literals and untyped NULL. +A NULL can be promoted to any other type, while a string literal can be promoted to any simple data type. + +This is a graphical depiction of the precedence list as a directed tree: +<img src="img/type-precedence-list.png" width="80%" title="Type Precedence List" alt="Type Precedence List"> + +#### Least Common Type Resolution +The least common type from a set of types is the narrowest type reachable from the precedence list by all elements of the set of types. + +The least common type resolution is used to: +- Decide whether a function expecting a parameter of a type can be invoked using an argument of a narrower type. +- Derive the argument type for functions which expect a shared argument type for multiple parameters, such as coalesce, least, or greatest. Review comment: Done(BTW the extra space will be omitted in web page anyway) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org