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

    https://github.com/apache/spark/pull/13269#discussion_r65638864
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -1884,10 +1884,63 @@ class Analyzer(
               } else {
                 inputAttributes
               }
    -          val unbound = deserializer transform {
    -            case b: BoundReference => inputs(b.ordinal)
    +
    +          validateTupleColumns(deserializer, inputs)
    +          val ordinalResolved = deserializer transform {
    +            case GetColumnByOrdinal(ordinal, _) => inputs(ordinal)
    +          }
    +          val attrResolved = resolveExpression(
    +            ordinalResolved, LocalRelation(inputs), throws = true)
    +          validateInnerTupleFields(attrResolved)
    +          attrResolved
    +      }
    +    }
    +
    +    private def fail(schema: StructType, maxOrdinal: Int): Unit = {
    +      throw new AnalysisException(s"Try to map ${schema.simpleString} to 
Tuple${maxOrdinal + 1}, " +
    +        "but failed as the number of fields does not line up.")
    +    }
    +
    +    /**
    +     * For each Tuple field, we use [[GetColumnByOrdinal]] to get its 
corresponding column by
    +     * position.  However, the actual number of columns may be different 
from the number of Tuple
    +     * fields.  This method is used to check the number of columns and 
fields, and throw an
    +     * exception if they do not match.
    +     */
    +    private def validateTupleColumns(deserializer: Expression, inputs: 
Seq[Attribute]): Unit = {
    +      val ordinals = deserializer.collect {
    +        case GetColumnByOrdinal(ordinal, _) => ordinal
    +      }.distinct.sorted
    +
    +      if (ordinals.nonEmpty && ordinals != inputs.indices) {
    +        fail(inputs.toStructType, ordinals.last)
    +      }
    +    }
    +
    +    /**
    +     * For each inner Tuple field, we use [[GetStructField]] to get its 
corresponding struct field
    +     * by position.  However, the actual number of struct fields may be 
different from the number
    +     * of inner Tuple fields.  This method is used to check the number of 
struct fields and inner
    +     * Tuple fields, and throw an exception if they do not match.
    +     */
    +    private def validateInnerTupleFields(deserializer: Expression): Unit = 
{
    +      val exprToOrdinals = 
scala.collection.mutable.HashMap.empty[Expression, ArrayBuffer[Int]]
    +      deserializer foreach {
    +        case g: GetStructField =>
    +          if (exprToOrdinals.contains(g.child)) {
    +            exprToOrdinals(g.child) += g.ordinal
    +          } else {
    +            exprToOrdinals += g.child -> ArrayBuffer(g.ordinal)
    +          }
    +        case _ =>
    +      }
    +      exprToOrdinals.foreach {
    +        case (expr, ordinals) =>
    +          val schema = expr.dataType.asInstanceOf[StructType]
    +          val sortedOrdinals: Seq[Int] = ordinals.distinct.sorted
    +          if (sortedOrdinals.nonEmpty && sortedOrdinals != schema.indices) 
{
    +            fail(schema, sortedOrdinals.last)
    --- End diff --
    
    This can be simplified to:
    
    ```scala
    val structChildToOrdinals =
      deserializer
        .collect { case g: GetStructField => g }
        .groupBy(_.child)
        .mapValues(_.map(_.ordinal).distinct.sorted)
    
    structChildToOrdinals.foreach { case (expr, ordinals) =>
      val schema = expr.dataType.asInstanceOf[StructType]
      if (ordinals != schema.indices) {
        fail(schema, ordinals.last)
      }
    }
    ```


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