Github user viirya commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10577#discussion_r49168529
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
 ---
    @@ -200,41 +200,62 @@ object HiveTypeCoercion {
        */
       object WidenSetOperationTypes extends Rule[LogicalPlan] {
     
    -    private[this] def widenOutputTypes(
    +    private def widenOutputTypes(
             planName: String,
    -        left: LogicalPlan,
    -        right: LogicalPlan): (LogicalPlan, LogicalPlan) = {
    -      require(left.output.length == right.output.length)
    -
    -      val castedTypes = left.output.zip(right.output).map {
    -        case (lhs, rhs) if lhs.dataType != rhs.dataType =>
    -          findWiderTypeForTwo(lhs.dataType, rhs.dataType)
    -        case other => None
    +        children: Seq[LogicalPlan]): Seq[LogicalPlan] = {
    +      require(children.forall(_.output.length == 
children.head.output.length))
    +
    +      // Get a sequence of data types, each of which is the widest type of 
this specific attribute
    +      // in all the children
    +      val castedTypes: Seq[Option[DataType]] = {
    +        val initialTypeSeq = children.head.output.map(a => 
Option(a.dataType))
    +        children.tail.foldLeft(initialTypeSeq) { (currentOutputDataTypes, 
child) =>
    +          // Find the wider type if the data type of this child do not 
match with
    +          // the casted data types of the already processed children
    +          getCastedTypes(currentOutputDataTypes, child.output)
    +        }
           }
     
    -      def castOutput(plan: LogicalPlan): LogicalPlan = {
    -        val casted = plan.output.zip(castedTypes).map {
    -          case (e, Some(dt)) if e.dataType != dt =>
    -            Alias(Cast(e, dt), e.name)()
    -          case (e, _) => e
    -        }
    -        Project(casted, plan)
    +      // Add extra Project for type promotion if necessary
    +      children.map(castOutput(_, castedTypes))
    +    }
    +
    +    // Add Project if the data types do not match
    +    private def castOutput(
    +        plan: LogicalPlan,
    +        castedTypes: Seq[Option[DataType]]): LogicalPlan = {
    +      val casted = plan.output.zip(castedTypes).map {
    +        case (e, Some(dt)) if e.dataType != dt =>
    +          Alias(Cast(e, dt), e.name)()
    +        case (e, _) => e
           }
    +      if (casted.exists(_.isInstanceOf[Alias])) Project(casted, plan) else 
plan
    +    }
     
    -      if (castedTypes.exists(_.isDefined)) {
    -        (castOutput(left), castOutput(right))
    -      } else {
    -        (left, right)
    +    private def getCastedTypes(
    +        typeSeq: Seq[Option[DataType]],
    +        attrSeq: Seq[Attribute]): Seq[Option[DataType]] = {
    +      typeSeq.zip(attrSeq).map {
    +        case (Some(dt), ar) if dt != ar.dataType =>
    +          findWiderTypeForTwo(dt, ar.dataType)
    +        case (Some(dt), ar) if dt == ar.dataType => Option(dt)
    --- End diff --
    
    Do we need this case?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to