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

    https://github.com/apache/spark/pull/14355#discussion_r72452237
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala
 ---
    @@ -280,6 +267,37 @@ trait CheckAnalysis extends PredicateHelper {
               case p if 
p.expressions.exists(PredicateSubquery.hasPredicateSubquery) =>
                 failAnalysis(s"Predicate sub-queries can only be used in a 
Filter: $p")
     
    +          case _: Union | _: SetOperation =>
    +            if (operator.children.length > 1) {
    +              val dataTypes = operator.children.head.output.map(_.dataType)
    +              val firstError = 
operator.children.find(_.output.map(_.dataType) != dataTypes)
    +              if (firstError.isDefined) {
    +                val tableIndex = operator.children.indexOf(firstError.get) 
+ 1
    +                if (dataTypes.length != firstError.get.output.length) {
    +                  failAnalysis(
    +                    s"""
    +                      |${operator.nodeName} can only be performed on 
tables with the same number
    +                      |of columns, but the first table has 
${dataTypes.length} columns and
    +                      |the ${if (tableIndex == 2) "second" else tableIndex 
+ "th"} table has
    +                      |${firstError.get.output.length} columns
    +                    """.stripMargin.replace("\n", " ").trim())
    +                } else {
    +                  val columnIndex = 
firstError.get.output.map(_.dataType).zipWithIndex.find {
    +                    case (dataType, i) => dataType != dataTypes(i)
    +                  }.get._2 + 1
    +                  failAnalysis(
    +                    s"""
    +                      |${operator.nodeName} can only be performed on 
tables with the compatible
    +                      |column types. The first table has 
'[${dataTypes.mkString(", ")}]'
    +                      |and ${if (tableIndex == 2) "second" else tableIndex 
+ "th"} table has
    +                      
|'[${firstError.get.output.map(_.dataType).mkString(", ")}]'. The
    +                      |${if (columnIndex == 1) "first" else columnIndex + 
"th"} column is
    +                      |incompatible
    +                    """.stripMargin.replace("\n", " ").trim())
    +                }
    +              }
    +            }
    +
    --- End diff --
    
    I was more thinking of something like this (not checked for syntax):
    ```scala
    def dataTypes(plan: LogicalPlan): Seq[DataType] = 
plan.output.map(_.dataType)
    val ref = dataTypes(operator.children.head)
    operator.children.tail.foreach { child =>
      // Check the number of columns
      if (children.output.size() != ref.size()) {
        failAnalysis("size")
      }
      // Check if the data types match.
      dataTypes(child).zip(ref).zipWithIndex.foreach { case ((dt1, dt2), index) 
=>
        if (dt1 != dt2) {
          failAnalysis(s"$dt1 <> $dt2 @ $i")
        }
      }
    }
    ```


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