@@ -671,33 +671,41 @@ class Analyzer(
671
671
* [[ResolveRelations ]] still resolves v1 tables.
672
672
*/
673
673
object ResolveTables extends Rule [LogicalPlan ] {
674
- def apply (plan : LogicalPlan ): LogicalPlan = plan.resolveOperatorsUp {
675
- case u : UnresolvedRelation =>
676
- lookupV2Relation(u.multipartIdentifier)
677
- .getOrElse(u)
674
+ def apply (plan : LogicalPlan ): LogicalPlan = {
675
+ var dataSourceV2Relations = Map .empty[UnresolvedRelation , Option [DataSourceV2Relation ]]
676
+ plan.resolveOperatorsUp {
677
+ case u : UnresolvedRelation =>
678
+ if (dataSourceV2Relations.contains(u)) {
679
+ dataSourceV2Relations(u).getOrElse(u)
680
+ } else {
681
+ val dataSourceV2Relation = lookupV2Relation(u.multipartIdentifier)
682
+ dataSourceV2Relations += (u -> dataSourceV2Relation)
683
+ dataSourceV2Relation.getOrElse(u)
684
+ }
678
685
679
- case i @ InsertIntoStatement (u : UnresolvedRelation , _, _, _, _) if i.query.resolved =>
680
- lookupV2Relation(u.multipartIdentifier)
681
- .map(v2Relation => i.copy(table = v2Relation))
682
- .getOrElse(i)
686
+ case i @ InsertIntoStatement (u : UnresolvedRelation , _, _, _, _) if i.query.resolved =>
687
+ lookupV2Relation(u.multipartIdentifier)
688
+ .map(v2Relation => i.copy(table = v2Relation))
689
+ .getOrElse(i)
683
690
684
- case desc @ DescribeTable (u : UnresolvedV2Relation , _) =>
685
- CatalogV2Util .loadRelation(u.catalog, u.tableName)
691
+ case desc @ DescribeTable (u : UnresolvedV2Relation , _) =>
692
+ CatalogV2Util .loadRelation(u.catalog, u.tableName)
686
693
.map(rel => desc.copy(table = rel))
687
694
.getOrElse(desc)
688
695
689
- case alter @ AlterTable (_, _, u : UnresolvedV2Relation , _) =>
690
- CatalogV2Util .loadRelation(u.catalog, u.tableName)
696
+ case alter @ AlterTable (_, _, u : UnresolvedV2Relation , _) =>
697
+ CatalogV2Util .loadRelation(u.catalog, u.tableName)
691
698
.map(rel => alter.copy(table = rel))
692
699
.getOrElse(alter)
693
700
694
- case show @ ShowTableProperties (u : UnresolvedV2Relation , _) =>
695
- CatalogV2Util .loadRelation(u.catalog, u.tableName)
696
- .map(rel => show.copy(table = rel))
697
- .getOrElse(show)
701
+ case show @ ShowTableProperties (u : UnresolvedV2Relation , _) =>
702
+ CatalogV2Util .loadRelation(u.catalog, u.tableName)
703
+ .map(rel => show.copy(table = rel))
704
+ .getOrElse(show)
698
705
699
- case u : UnresolvedV2Relation =>
700
- CatalogV2Util .loadRelation(u.catalog, u.tableName).getOrElse(u)
706
+ case u : UnresolvedV2Relation =>
707
+ CatalogV2Util .loadRelation(u.catalog, u.tableName).getOrElse(u)
708
+ }
701
709
}
702
710
}
703
711
@@ -767,15 +775,25 @@ class Analyzer(
767
775
case _ => plan
768
776
}
769
777
770
- def apply (plan : LogicalPlan ): LogicalPlan = plan.resolveOperatorsUp {
771
- case i @ InsertIntoStatement (u @ UnresolvedRelation (AsTableIdentifier (ident)), _, child, _, _)
772
- if child.resolved =>
773
- EliminateSubqueryAliases (lookupTableFromCatalog(ident, u)) match {
774
- case v : View =>
775
- u.failAnalysis(s " Inserting into a view is not allowed. View: ${v.desc.identifier}. " )
776
- case other => i.copy(table = other)
777
- }
778
- case u : UnresolvedRelation => resolveRelation(u)
778
+ def apply (plan : LogicalPlan ): LogicalPlan = {
779
+ var logicalPlans = Map .empty[UnresolvedRelation , LogicalPlan ]
780
+ plan.resolveOperatorsUp {
781
+ case i @ InsertIntoStatement (
782
+ u @ UnresolvedRelation (AsTableIdentifier (ident)), _, child, _, _) if child.resolved =>
783
+ EliminateSubqueryAliases (lookupTableFromCatalog(ident, u)) match {
784
+ case v : View =>
785
+ u.failAnalysis(s " Inserting into a view is not allowed. View: ${v.desc.identifier}. " )
786
+ case other => i.copy(table = other)
787
+ }
788
+ case u : UnresolvedRelation =>
789
+ if (logicalPlans.contains(u)) {
790
+ logicalPlans(u)
791
+ } else {
792
+ val relation = resolveRelation(u)
793
+ logicalPlans += (u -> relation)
794
+ relation
795
+ }
796
+ }
779
797
}
780
798
781
799
// Look up the table with the given name from catalog. The database we used is decided by the
0 commit comments