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

    https://github.com/apache/spark/pull/12313#discussion_r63912028
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -498,6 +499,117 @@ class Analyzer(
         }
       }
     
    +  val ResolveOutputColumns = new ResolveOutputColumns
    +  class ResolveOutputColumns extends Rule[LogicalPlan] {
    +    def apply(plan: LogicalPlan): LogicalPlan = plan.transform {
    +      case ins @ InsertIntoTable(relation: LogicalPlan, partition, _, _, 
_, _)
    +          if ins.childrenResolved && !ins.resolved =>
    +        resolveOutputColumns(ins, expectedColumns(relation, partition), 
relation.toString)
    +    }
    +
    +    protected 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] = {
    --- End diff --
    
    Same as above.


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