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

    https://github.com/apache/spark/pull/5776#discussion_r29402584
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -69,6 +72,36 @@ class Analyzer(
       )
     
       /**
    +   * Substitute child plan with cte definitions
    +   */
    +  object CTESubstitution extends Rule[LogicalPlan] {
    +    def apply(plan: LogicalPlan): LogicalPlan = plan match {
    +      case With(child, relations) => substituteCTE(child, relations)
    +      case other => other
    +    }
    +
    +    def substituteCTE(plan: LogicalPlan, cteRelations: Map[String, 
LogicalPlan]): LogicalPlan = {
    +      plan transform {
    +        case i @ InsertIntoTable(u: UnresolvedRelation, _, _, _, _) =>
    +          // In hive, if there is same table name in database and CTE 
definition,
    +          // hive will use the table in database, not the CTE one.
    +          // Taking into account the reasonableness and the implementation 
complexity,
    +          // here use the CTE definition first, check table name only and 
ignore database name
    +          val substituted = cteRelations.get(u.tableIdentifier.last).map { 
relation =>
    +            val withAlias = u.alias.map(Subquery(_, relation))
    +            withAlias.getOrElse(relation)
    +          }
    +          substituted.getOrElse(u)
    +          i.copy(table = relation)
    +        case u : UnresolvedRelation =>
    +          cteRelations.get(u.tableIdentifier.last)
    +            .map(relation => u.alias.map(Subquery(_, 
relation)).getOrElse(relation))
    +            .getOrElse(u)
    --- End diff --
    
    You may want to also update this part.


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