@@ -46,7 +46,7 @@ class Analyzer(
4646 val resolver = if (caseSensitive) caseSensitiveResolution else caseInsensitiveResolution
4747
4848 val fixedPoint = FixedPoint (maxIterations)
49-
49+
5050 /**
5151 * Override to provide additional rules for the "Resolution" batch.
5252 */
@@ -72,16 +72,12 @@ class Analyzer(
7272 )
7373
7474 /**
75- * Substitute CTE definitions
75+ * Substitute child plan with cte definitions
7676 */
7777 object CTESubstitution extends Rule [LogicalPlan ] {
78- def apply (plan : LogicalPlan ): LogicalPlan = {
79- val (realPlan, cteRelations) = plan match {
80- case With (child, relations) =>
81- (child, relations)
82- case other => (other, Map .empty[String , LogicalPlan ])
83- }
84- substituteCTE(realPlan, cteRelations)
78+ def apply (plan : LogicalPlan ): LogicalPlan = plan match {
79+ case With (child, relations) => substituteCTE(child, relations)
80+ case other => other
8581 }
8682
8783 def substituteCTE (plan : LogicalPlan , cteRelations : Map [String , LogicalPlan ]): LogicalPlan = {
@@ -91,9 +87,11 @@ class Analyzer(
9187 // hive will use the table in database, not the CTE one.
9288 // Taking into account the reasonableness and the implementation complexity,
9389 // here use the CTE definition first, check table name only and ignore database name
94- val relation = cteRelations.get(u.tableIdentifier.last)
95- .map(relation => u.alias.map(Subquery (_, relation)).getOrElse(relation))
96- .getOrElse(u)
90+ val substituted = cteRelations.get(u.tableIdentifier.last).map { relation =>
91+ val withAlias = u.alias.map(Subquery (_, relation))
92+ withAlias.getOrElse(relation)
93+ }
94+ substituted.getOrElse(u)
9795 i.copy(table = relation)
9896 case u : UnresolvedRelation =>
9997 cteRelations.get(u.tableIdentifier.last)
0 commit comments