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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala
##########
@@ -0,0 +1,316 @@
+/*
+ * 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 scala.annotation.tailrec
+
+import org.apache.spark.sql.catalyst.analysis.TypeCoercion.numericPrecedence
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.aggregate._
+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: 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.
+ *
+ * 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(odd)
+ *       * 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. If the element nullability is converted from 
true to false, add
+ *     runtime null check to the elements.
+ *  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 ::
+      WidenSetOperationTypes ::
+      PromoteStringLiterals ::
+      DecimalPrecision ::
+      FunctionArgumentConversion ::
+      ConcatCoercion ::
+      MapZipWithCoercion ::
+      EltCoercion ::
+      CaseWhenCoercion ::
+      IfCoercion ::
+      StackCoercion ::
+      Division ::
+      IntegralDivision ::
+      ImplicitTypeCasts ::
+      DateTimeOperations ::
+      WindowFrameCoercion ::
+      StringLiteralCoercion ::
+      Nil
+
+  /**
+   * Find the tightest common type of two types that might be used in a binary 
expression.
+   */
+  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: NumericType, t2: NumericType) =>
+        findTightestCommonNumericType(t1, t2)
+
+      case (_: TimestampType, _: DateType) | (_: DateType, _: TimestampType) =>
+        Some(TimestampType)
+
+      case (t1, t2) => findTypeForComplex(t1, t2, findTightestCommonType)
+    }
+  }
+
+  @tailrec
+  private def findTightestCommonNumericType(t1: NumericType, t2: NumericType): 
Option[DataType] = {
+    (t1, t2) match {
+      case (i: IntegralType, d: DecimalType) =>
+        if (d.isWiderThan(i)) {
+          Some(t2)
+        } else {
+          findTightestCommonNumericType(DecimalType.forType(i), d)
+        }
+
+      case (t1: DecimalType, t2: IntegralType) =>
+        findTightestCommonNumericType(t2, t1)
+
+      case (t1: DecimalType, t2: DecimalType) =>
+        Some(DecimalPrecision.widerDecimalType(t1, t2))

Review comment:
       Ah, ok. The latest change looks good.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.expressions.aggregate._
+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: 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.
+ *
+ * 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(odd)
+ *       * 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. If the element nullability is converted from 
true to false, add
+ *     runtime null check to the elements.
+ *  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 ::
+      WidenSetOperationTypes ::
+      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)

Review comment:
       Just out of curiosity; I know the other DBMSs, e.g., postgresql, has the 
same behaviour. The standard says something about it, too? 

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AnsiTypeCoercion.scala
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.expressions.aggregate._
+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: 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.
+ *
+ * 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(odd)

Review comment:
       what does `odd` means?




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to