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

    https://github.com/apache/spark/pull/16168#discussion_r91235152
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveMetastoreCatalog.scala ---
    @@ -126,6 +146,55 @@ private[hive] class HiveMetastoreCatalog(sparkSession: 
SparkSession) extends Log
         }
       }
     
    +  /**
    +   * Apply Projection on unresolved logical plan to:
    +   * 1. Omit the columns which are not referenced by the view;
    +   * 2. Reorder the columns to keep the same order with the view;
    +   */
    +  private def withProjection(plan: LogicalPlan, schema: StructType): 
LogicalPlan = {
    +    // All fields in schema should exist in plan.schema, or we should 
throw an AnalysisException
    +    // to notify the underlying schema has been changed.
    +    if (schema.fields.forall { field =>
    +      plan.schema.fields.exists(other => compareStructField(field, 
other))}) {
    +      val output = schema.fields.map { field =>
    +        plan.output.find { expr =>
    +          expr.name == field.name && expr.dataType == 
field.dataType}.getOrElse(
    +            throw new AnalysisException("The underlying schema doesn't 
match the original " +
    +              s"schema, expected ${schema.sql} but got ${plan.schema.sql}")
    +          )}
    +      Project(output, plan)
    +    } else {
    +      throw new AnalysisException("The underlying schema doesn't match the 
original schema, " +
    +        s"expected ${schema.sql} but got ${plan.schema.sql}")
    +    }
    +  }
    +
    +  /**
    +   * Compare the both [[StructField]] to verify whether they have the same 
name and dataType.
    +   */
    +  private def compareStructField(field: StructField, other: StructField): 
Boolean = {
    +    field.name == other.name && field.dataType == other.dataType
    +  }
    +
    +  /**
    +   * Aliases the schema of the LogicalPlan to the view attribute names
    +   */
    +  private def aliasColumns(plan: LogicalPlan, fields: Seq[StructField]): 
LogicalPlan = {
    +    val output = fields.map(field => (field.name, field.getComment))
    +    if (plan.output.size != output.size) {
    --- End diff --
    
    This should not happen. I just want to ensure we are safe here in case the 
`withProjection` has been modified.


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