Skip to content

Commit 3379cf3

Browse files
committed
Improve ResolveTables and ResolveRelations performance
1 parent 882f54b commit 3379cf3

File tree

1 file changed

+45
-27
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis

1 file changed

+45
-27
lines changed

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

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -671,33 +671,41 @@ class Analyzer(
671671
* [[ResolveRelations]] still resolves v1 tables.
672672
*/
673673
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+
}
678685

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)
683690

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)
686693
.map(rel => desc.copy(table = rel))
687694
.getOrElse(desc)
688695

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)
691698
.map(rel => alter.copy(table = rel))
692699
.getOrElse(alter)
693700

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)
698705

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+
}
701709
}
702710
}
703711

@@ -767,15 +775,25 @@ class Analyzer(
767775
case _ => plan
768776
}
769777

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+
}
779797
}
780798

781799
// Look up the table with the given name from catalog. The database we used is decided by the

0 commit comments

Comments
 (0)