Skip to content

Commit

Permalink
address yhuai's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
scwf committed May 8, 2015
1 parent 553005a commit ecaeafb
Showing 1 changed file with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,19 @@ 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
}

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))
Expand Down

0 comments on commit ecaeafb

Please sign in to comment.