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

    https://github.com/apache/spark/pull/10577#discussion_r49186779
  
    --- 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)
    +        case other => None
           }
         }
     
         def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
           case p if p.analyzed => p
     
    -      case s @ SetOperation(left, right) if s.childrenResolved
    -          && left.output.length == right.output.length && !s.resolved =>
    -        val (newLeft, newRight) = widenOutputTypes(s.nodeName, left, right)
    -        s.makeCopy(Array(newLeft, newRight))
    +      case s @ SetOperation(left, right) if s.childrenResolved &&
    +          left.output.length == right.output.length && !s.resolved =>
    +        val newChildren: Seq[LogicalPlan] = widenOutputTypes(s.nodeName, 
left :: right :: Nil)
    +        assert(newChildren.length == 2)
    --- End diff --
    
    Yeah, it is impossible now, but this relies on the implementation of 
`widenOutputTypes`. When the others do a code change in the future, it might 
break the assumption of the next line.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to