@@ -75,23 +75,19 @@ class Analyzer(
7575 * Substitute child plan with cte definitions
7676 */
7777 object CTESubstitution extends Rule [LogicalPlan ] {
78+ // TODO allow subquery to define CTE
7879 def apply (plan : LogicalPlan ): LogicalPlan = plan match {
7980 case With (child, relations) => substituteCTE(child, relations)
8081 case other => other
8182 }
8283
8384 def substituteCTE (plan : LogicalPlan , cteRelations : Map [String , LogicalPlan ]): LogicalPlan = {
8485 plan transform {
85- case i @ InsertIntoTable (u : UnresolvedRelation , _, _, _, _) =>
86- // In hive, if there is same table name in database and CTE definition,
87- // hive will use the table in database, not the CTE one.
88- // Taking into account the reasonableness and the implementation complexity,
89- // here use the CTE definition first, check table name only and ignore database name
90- val substituted = cteRelations.get(u.tableIdentifier.last).map { relation =>
91- val withAlias = u.alias.map(Subquery (_, relation))
92- withAlias.getOrElse(relation)
93- }
94- i.copy(table = substituted.getOrElse(u))
86+ // In hive, if there is same table name in database and CTE definition,
87+ // hive will use the table in database, not the CTE one.
88+ // Taking into account the reasonableness and the implementation complexity,
89+ // here use the CTE definition first, check table name only and ignore database name
90+ // see https://github.com/apache/spark/pull/4929#discussion_r27186638 for more info
9591 case u : UnresolvedRelation =>
9692 val substituted = cteRelations.get(u.tableIdentifier.last).map { relation =>
9793 val withAlias = u.alias.map(Subquery (_, relation))
0 commit comments