maropu commented on a change in pull request #31349:
URL: https://github.com/apache/spark/pull/31349#discussion_r578037055



##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercionSuite.scala
##########
@@ -0,0 +1,1402 @@
+/*
+ * 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 java.sql.Timestamp
+
+import org.apache.spark.sql.catalyst.analysis.AnsiTypeCoercion._
+import org.apache.spark.sql.catalyst.dsl.expressions._
+import org.apache.spark.sql.catalyst.dsl.plans._
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules.{Rule, RuleExecutor}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+class AnsiTypeCoercionSuite extends AnalysisTest {
+  import TypeCoercionSuite._
+
+  // scalastyle:off line.size.limit
+  // The following table shows all implicit data type conversions that are not 
visible to the user.
+  // 
+----------------------+----------+-----------+-------------+----------+------------+------------+------------+------------+-------------+------------+----------+---------------+------------+----------+-------------+----------+----------------------+---------------------+-------------+--------------+
+  // | Source Type\CAST TO  | ByteType | ShortType | IntegerType | LongType | 
FloatType  | DoubleType | Dec(10, 2) | BinaryType | BooleanType | StringType | 
DateType | TimestampType | ArrayType  | MapType  | StructType  | NullType | 
CalendarIntervalType |     DecimalType     | NumericType | IntegralType |
+  // 
+----------------------+----------+-----------+-------------+----------+------------+------------+------------+------------+-------------+------------+----------+---------------+------------+----------+-------------+----------+----------------------+---------------------+-------------+--------------+
+  // | ByteType             | ByteType | ShortType | IntegerType | LongType | 
DoubleType | DoubleType | Dec(10, 2) | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(3, 0)   | ByteType    | ByteType     |
+  // | ShortType            | X        | ShortType | IntegerType | LongType | 
DoubleType | DoubleType | Dec(10, 2) | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(5, 0)   | ShortType   | ShortType    |
+  // | IntegerType          | X        | X         | IntegerType | LongType | 
DoubleType | DoubleType | X          | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(10, 0)  | IntegerType | IntegerType  |
+  // | LongType             | X        | X         | X           | LongType | 
DoubleType | DoubleType | X          | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(20, 0)  | LongType    | LongType     |
+  // | FloatType            | X        | X         | X           | X        | 
FloatType  | DoubleType | X          | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(30, 15) | DoubleType  | X            |
+  // | DoubleType           | X        | X         | X           | X        | 
X          | DoubleType | X          | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(14, 7)  | FloatType   | X            |
+  // | Dec(10, 2)           | X        | X         | X           | X        | 
DoubleType | DoubleType | Dec(10, 2) | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | DecimalType(10, 2)  | Dec(10, 2)  | X            |
+  // | BinaryType           | X        | X         | X           | X        | 
X          | X          | X          | BinaryType | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | X                   | X           | X            |
+  // | BooleanType          | X        | X         | X           | X        | 
X          | X          | X          | X          | BooleanType | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | X                   | X           | X            |
+  // | StringType           | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | X   
                 | X                   | X           | X            |
+  // | DateType             | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
DateType | TimestampType | X          | X        | X           | X        | X   
                 | X                   | X           | X            |
+  // | TimestampType        | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
X        | TimestampType | X          | X        | X           | X        | X   
                 | X                   | X           | X            |
+  // | ArrayType            | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
X        | X             | ArrayType* | X        | X           | X        | X   
                 | X                   | X           | X            |
+  // | MapType              | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
X        | X             | X          | MapType* | X           | X        | X   
                 | X                   | X           | X            |
+  // | StructType           | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
X        | X             | X          | X        | StructType* | X        | X   
                 | X                   | X           | X            |
+  // | NullType             | ByteType | ShortType | IntegerType | LongType | 
DoubleType | FloatType  | Dec(10, 2) | BinaryType | BooleanType | StringType | 
DateType | TimestampType | ArrayType  | MapType  | StructType  | NullType | 
CalendarIntervalType | DecimalType(38, 18) | DoubleType  | IntegerType  |
+  // | CalendarIntervalType | X        | X         | X           | X        | 
X          | X          | X          | X          | X           | X          | 
X        | X             | X          | X        | X           | X        | 
CalendarIntervalType | X                   | X           | X            |
+  // 
+----------------------+----------+-----------+-------------+----------+------------+------------+------------+------------+-------------+------------+----------+---------------+------------+----------+-------------+----------+----------------------+---------------------+-------------+--------------+
+  // Note: StructType* is castable when all the internal child types are 
castable according to the table.
+  // Note: ArrayType* is castable when the element type is castable according 
to the table.
+  // Note: MapType* is castable when both the key type and the value type are 
castable according to the table.
+  // scalastyle:on line.size.limit
+
+  private def shouldCast(from: DataType, to: AbstractDataType, expected: 
DataType): Unit = {
+    // Check default value
+    val castDefault = AnsiTypeCoercion.implicitCast(default(from), to)
+    assert(DataType.equalsIgnoreCompatibleNullability(
+      castDefault.map(_.dataType).getOrElse(null), expected),
+      s"Failed to cast $from to $to")
+
+    // Check null value
+    val castNull = AnsiTypeCoercion.implicitCast(createNull(from), to)
+    assert(DataType.equalsIgnoreCaseAndNullability(
+      castNull.map(_.dataType).getOrElse(null), expected),
+      s"Failed to cast $from to $to")
+  }
+
+  private def shouldNotCast(from: DataType, to: AbstractDataType): Unit = {
+    // Check default value
+    val castDefault = AnsiTypeCoercion.implicitCast(default(from), to)
+    assert(castDefault.isEmpty, s"Should not be able to cast $from to $to, but 
got $castDefault")
+
+    // Check null value
+    val castNull = AnsiTypeCoercion.implicitCast(createNull(from), to)
+    assert(castNull.isEmpty, s"Should not be able to cast $from to $to, but 
got $castNull")
+  }
+
+  private def shouldCastStringLiteral(to: AbstractDataType, expected: 
DataType): Unit = {
+    val input = Literal("123")
+    val castResult = AnsiTypeCoercion.implicitCast(input, to)
+    assert(DataType.equalsIgnoreCaseAndNullability(
+      castResult.map(_.dataType).getOrElse(null), expected),
+      s"Failed to cast String literal to $to")
+  }
+
+  private def shouldNotCastStringInput(to: AbstractDataType): Unit = {
+    val input = AttributeReference("s", StringType)()
+    val castResult = AnsiTypeCoercion.implicitCast(input, to)
+    assert(castResult.isEmpty, s"Should not be able to cast non-foldable 
String input to $to")
+  }
+
+  private def default(dataType: DataType): Expression = dataType match {
+    case ArrayType(internalType: DataType, _) =>
+      CreateArray(Seq(Literal.default(internalType)))
+    case MapType(keyDataType: DataType, valueDataType: DataType, _) =>
+      CreateMap(Seq(Literal.default(keyDataType), 
Literal.default(valueDataType)))
+    case _ => Literal.default(dataType)
+  }
+
+  private def createNull(dataType: DataType): Expression = dataType match {
+    case ArrayType(internalType: DataType, _) =>
+      CreateArray(Seq(Literal.create(null, internalType)))
+    case MapType(keyDataType: DataType, valueDataType: DataType, _) =>
+      CreateMap(Seq(Literal.create(null, keyDataType), Literal.create(null, 
valueDataType)))
+    case _ => Literal.create(null, dataType)
+  }
+
+  // Check whether the type `checkedType` can be cast to all the types in 
`castableTypes`,
+  // but cannot be cast to the other types in `allTypes`.
+  private def checkTypeCasting(checkedType: DataType, castableTypes: 
Seq[DataType]): Unit = {
+    val nonCastableTypes = allTypes.filterNot(castableTypes.contains)
+
+    castableTypes.foreach { tpe =>
+      shouldCast(checkedType, tpe, tpe)
+    }
+    nonCastableTypes.foreach { tpe =>
+      shouldNotCast(checkedType, tpe)
+    }
+  }
+
+  private def checkWidenType(
+      widenFunc: (DataType, DataType) => Option[DataType],
+      t1: DataType,
+      t2: DataType,
+      expected: Option[DataType],
+      isSymmetric: Boolean = true): Unit = {
+    var found = widenFunc(t1, t2)
+    assert(found == expected,
+      s"Expected $expected as wider common type for $t1 and $t2, found $found")
+    // Test both directions to make sure the widening is symmetric.
+    if (isSymmetric) {
+      found = widenFunc(t2, t1)
+      assert(found == expected,
+        s"Expected $expected as wider common type for $t2 and $t1, found 
$found")
+    }
+  }
+
+  test("implicit type cast - ByteType") {
+    val checkedType = ByteType
+    checkTypeCasting(checkedType, castableTypes = numericTypes)
+    shouldCast(checkedType, DecimalType, DecimalType.ByteDecimal)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldCast(checkedType, IntegralType, checkedType)
+  }
+
+  test("implicit type cast - ShortType") {
+    val checkedType = ShortType
+    checkTypeCasting(checkedType, castableTypes = numericTypes.filterNot(_ == 
ByteType))
+    shouldCast(checkedType, DecimalType, DecimalType.ShortDecimal)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldCast(checkedType, IntegralType, checkedType)
+  }
+
+  test("implicit type cast - IntegerType") {
+    val checkedType = IntegerType
+    checkTypeCasting(checkedType, castableTypes =
+      Seq(IntegerType, LongType, FloatType, DoubleType, 
DecimalType.SYSTEM_DEFAULT))
+    shouldCast(IntegerType, DecimalType, DecimalType.IntDecimal)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldCast(checkedType, IntegralType, checkedType)
+  }
+
+  test("implicit type cast - LongType") {
+    val checkedType = LongType
+    checkTypeCasting(checkedType, castableTypes =
+      Seq(LongType, FloatType, DoubleType, DecimalType.SYSTEM_DEFAULT))
+    shouldCast(checkedType, DecimalType, DecimalType.LongDecimal)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldCast(checkedType, IntegralType, checkedType)
+  }
+
+  test("implicit type cast - FloatType") {
+    val checkedType = FloatType
+    checkTypeCasting(checkedType, castableTypes = Seq(FloatType, DoubleType))
+    shouldCast(checkedType, DecimalType, DecimalType.FloatDecimal)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldNotCast(checkedType, IntegralType)
+  }
+
+  test("implicit type cast - DoubleType") {
+    val checkedType = DoubleType
+    checkTypeCasting(checkedType, castableTypes = Seq(DoubleType))
+    shouldCast(checkedType, DecimalType, DecimalType.DoubleDecimal)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldNotCast(checkedType, IntegralType)
+  }
+
+  test("implicit type cast - DecimalType(10, 2)") {
+    val checkedType = DecimalType(10, 2)
+    checkTypeCasting(checkedType, castableTypes = fractionalTypes)
+    shouldCast(checkedType, DecimalType, checkedType)
+    shouldCast(checkedType, NumericType, checkedType)
+    shouldNotCast(checkedType, IntegralType)
+  }
+
+  test("implicit type cast - BinaryType") {
+    val checkedType = BinaryType
+    checkTypeCasting(checkedType, castableTypes = Seq(checkedType))
+    shouldNotCast(checkedType, DecimalType)
+    shouldNotCast(checkedType, NumericType)
+    shouldNotCast(checkedType, IntegralType)
+  }
+
+  test("implicit type cast - BooleanType") {
+    val checkedType = BooleanType
+    checkTypeCasting(checkedType, castableTypes = Seq(checkedType))
+    shouldNotCast(checkedType, DecimalType)
+    shouldNotCast(checkedType, NumericType)
+    shouldNotCast(checkedType, IntegralType)
+    shouldNotCast(checkedType, StringType)
+  }
+
+  test("implicit type cast - unfoldable StringType") {
+    val nonCastableTypes =
+      complexTypes ++ Seq(BooleanType, NullType, CalendarIntervalType)

Review comment:
       Is there any reason to choose these partial types for this test? IMO its 
better to add tests for all the cases where implicit casts have different 
behaviours between ansi/non-ansi modes (e.g., datetime types).




----------------------------------------------------------------
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.

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

Reply via email to