Skip to content

Commit 07e6b7f

Browse files
committed
Add map to AnalysisContext
1 parent 100ea1a commit 07e6b7f

File tree

1 file changed

+24
-25
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis

1 file changed

+24
-25
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

+24-25
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ object FakeV2SessionCatalog extends TableCatalog {
9191
* current catalog database.
9292
* @param nestedViewDepth The nested depth in the view resolution, this enables us to limit the
9393
* depth of nested views.
94+
* @param relationToLogicalPlanMaps The UnresolvedRelation to LogicalPlan mapping, this can ensure
95+
* that the table is resolved only once if a table is used
96+
* multiple times in a query.
9497
*/
9598
case class AnalysisContext(
9699
defaultDatabase: Option[String] = None,
97-
nestedViewDepth: Int = 0)
100+
nestedViewDepth: Int = 0,
101+
relationToLogicalPlanMaps: mutable.Map[UnresolvedRelation, LogicalPlan] = mutable.Map.empty)
98102

99103
object AnalysisContext {
100104
private val value = new ThreadLocal[AnalysisContext]() {
@@ -795,6 +799,7 @@ class Analyzer(
795799
* Replaces [[UnresolvedRelation]]s with concrete relations from the catalog.
796800
*/
797801
object ResolveRelations extends Rule[LogicalPlan] {
802+
private val relationToLogicalPlanMaps = AnalysisContext.get.relationToLogicalPlanMaps
798803

799804
// If an unresolved relation is given, it is looked up from the session catalog and either v1
800805
// or v2 relation is returned. Otherwise, we look up the table from catalog
@@ -847,32 +852,26 @@ class Analyzer(
847852
case _ => plan
848853
}
849854

850-
def apply(plan: LogicalPlan): LogicalPlan = {
851-
var logicalPlans = Map.empty[UnresolvedRelation, LogicalPlan]
852-
ResolveTempViews(plan).resolveOperatorsUp {
853-
case i@InsertIntoStatement(table, _, _, _, _) if i.query.resolved =>
854-
val relation = table match {
855-
case u@UnresolvedRelation(SessionCatalogAndIdentifier(catalog, ident)) =>
856-
lookupRelation(catalog, ident, recurse = false).getOrElse(u)
857-
case other => other
858-
}
855+
def apply(plan: LogicalPlan): LogicalPlan = ResolveTempViews(plan).resolveOperatorsUp {
856+
case i @ InsertIntoStatement(table, _, _, _, _) if i.query.resolved =>
857+
val relation = table match {
858+
case u @ UnresolvedRelation(SessionCatalogAndIdentifier(catalog, ident)) =>
859+
lookupRelation(catalog, ident, recurse = false).getOrElse(u)
860+
case other => other
861+
}
859862

860-
EliminateSubqueryAliases(relation) match {
861-
case v: View =>
862-
table.failAnalysis(
863-
s"Inserting into a view is not allowed. View: ${v.desc.identifier}.")
864-
case other => i.copy(table = other)
865-
}
863+
EliminateSubqueryAliases(relation) match {
864+
case v: View =>
865+
table.failAnalysis(s"Inserting into a view is not allowed. View: ${v.desc.identifier}.")
866+
case other => i.copy(table = other)
867+
}
866868

867-
case u: UnresolvedRelation =>
868-
if (logicalPlans.contains(u)) {
869-
logicalPlans(u)
870-
} else {
871-
val relation = resolveRelation(u)
872-
logicalPlans += (u -> relation)
873-
relation
874-
}
875-
}
869+
case u: UnresolvedRelation =>
870+
relationToLogicalPlanMaps.getOrElse(u, {
871+
val relation = resolveRelation(u)
872+
relationToLogicalPlanMaps.update(u, relation)
873+
relation
874+
})
876875
}
877876

878877
// Look up a relation from the given session catalog with the following logic:

0 commit comments

Comments
 (0)