diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala index 02a5f08ed7ee9..11123f1c312d4 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala @@ -75,6 +75,7 @@ class Analyzer( * Substitute child plan with cte definitions */ object CTESubstitution extends Rule[LogicalPlan] { + // TODO allow subquery to define CTE def apply(plan: LogicalPlan): LogicalPlan = plan match { case With(child, relations) => substituteCTE(child, relations) case other => other @@ -82,16 +83,11 @@ class Analyzer( 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) - } - i.copy(table = substituted.getOrElse(u)) + // 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 + // see https://github.com/apache/spark/pull/4929#discussion_r27186638 for more info case u : UnresolvedRelation => val substituted = cteRelations.get(u.tableIdentifier.last).map { relation => val withAlias = u.alias.map(Subquery(_, relation))