Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12313#discussion_r65822892
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -505,6 +506,117 @@ class Analyzer(
         }
       }
     
    +  object ResolveOutputColumns extends Rule[LogicalPlan] {
    +    def apply(plan: LogicalPlan): LogicalPlan = plan.transform {
    +      case ins @ InsertIntoTable(relation: LogicalPlan, partition, _, _, 
_, _)
    +          if relation.resolved && !ins.resolved =>
    +        resolveOutputColumns(ins, expectedColumns(relation, partition), 
relation.toString)
    +    }
    +
    +    private def resolveOutputColumns(
    +        insertInto: InsertIntoTable,
    +        columns: Seq[Attribute],
    +        relation: String) = {
    +      val resolved = if (insertInto.isMatchByName) {
    +        projectAndCastOutputColumns(columns, insertInto.child, relation)
    +      } else {
    +        castAndRenameOutputColumns(columns, insertInto.child, relation)
    +      }
    +
    +      if (resolved == insertInto.child.output) {
    +        insertInto
    +      } else {
    +        insertInto.copy(child = Project(resolved, insertInto.child))
    +      }
    +    }
    +
    +    /**
    +     * Resolves output columns by input column name, adding casts if 
necessary.
    +     */
    +    private def projectAndCastOutputColumns(
    +        output: Seq[Attribute],
    +        data: LogicalPlan,
    +        relation: String): Seq[NamedExpression] = {
    +      output.map { col =>
    +        data.resolveQuoted(col.name, resolver) match {
    +          case Some(inCol) if col.dataType != inCol.dataType =>
    +            Alias(UpCast(inCol, col.dataType, Seq()), col.name)()
    +          case Some(inCol) => inCol
    +          case None =>
    +            throw new AnalysisException(
    +              s"Cannot resolve ${col.name} in 
${data.output.mkString(",")}")
    +        }
    +      }
    +    }
    +
    +    private def castAndRenameOutputColumns(
    +        output: Seq[Attribute],
    +        data: LogicalPlan,
    +        relation: String): Seq[NamedExpression] = {
    +      val outputNames = output.map(_.name)
    +      // incoming expressions may not have names
    +      val inputNames = data.output.flatMap(col => Option(col.name))
    +      if (output.size > data.output.size) {
    +        // always a problem
    +        throw new AnalysisException(
    +          s"""Not enough data columns to write into $relation:
    +             |Data columns: ${data.output.mkString(",")}
    +             |Table columns: ${outputNames.mkString(",")}""".stripMargin)
    +      } else if (output.size < data.output.size) {
    +        if (outputNames.toSet.subsetOf(inputNames.toSet)) {
    --- End diff --
    
    do we really need to distinguish these 2 cases? How about we just say that 
the number of columns mismatch?


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