@@ -91,10 +91,14 @@ object FakeV2SessionCatalog extends TableCatalog {
91
91
* current catalog database.
92
92
* @param nestedViewDepth The nested depth in the view resolution, this enables us to limit the
93
93
* 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.
94
97
*/
95
98
case class AnalysisContext (
96
99
defaultDatabase : Option [String ] = None ,
97
- nestedViewDepth : Int = 0 )
100
+ nestedViewDepth : Int = 0 ,
101
+ relationToLogicalPlanMaps : mutable.Map [UnresolvedRelation , LogicalPlan ] = mutable.Map .empty)
98
102
99
103
object AnalysisContext {
100
104
private val value = new ThreadLocal [AnalysisContext ]() {
@@ -795,6 +799,7 @@ class Analyzer(
795
799
* Replaces [[UnresolvedRelation ]]s with concrete relations from the catalog.
796
800
*/
797
801
object ResolveRelations extends Rule [LogicalPlan ] {
802
+ private val relationToLogicalPlanMaps = AnalysisContext .get.relationToLogicalPlanMaps
798
803
799
804
// If an unresolved relation is given, it is looked up from the session catalog and either v1
800
805
// or v2 relation is returned. Otherwise, we look up the table from catalog
@@ -847,32 +852,26 @@ class Analyzer(
847
852
case _ => plan
848
853
}
849
854
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
+ }
859
862
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
+ }
866
868
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
+ })
876
875
}
877
876
878
877
// Look up a relation from the given session catalog with the following logic:
0 commit comments