Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ object FakeV2SessionCatalog extends TableCatalog {
* current catalog database.
* @param nestedViewDepth The nested depth in the view resolution, this enables us to limit the
* depth of nested views.
* @param relationToLogicalPlanMaps The UnresolvedRelation to LogicalPlan mapping, this can ensure
* that the table is resolved only once if a table is used
* multiple times in a query.
*/
case class AnalysisContext(
defaultDatabase: Option[String] = None,
nestedViewDepth: Int = 0)
nestedViewDepth: Int = 0,
relationToLogicalPlanMaps: mutable.Map[UnresolvedRelation, LogicalPlan] = mutable.Map.empty)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: how about just relationCache?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it's simpler to use Seq[String] as key.


object AnalysisContext {
Comment thread
cloud-fan marked this conversation as resolved.
private val value = new ThreadLocal[AnalysisContext]() {
Expand Down Expand Up @@ -861,7 +865,13 @@ class Analyzer(
case other => i.copy(table = other)
}

case u: UnresolvedRelation => resolveRelation(u)
case u: UnresolvedRelation =>
val relationToLogicalPlanMaps = AnalysisContext.get.relationToLogicalPlanMaps
relationToLogicalPlanMaps.getOrElse(u, {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safer to use the qualified table name as the key, i.e. after calling withNewNamespace

val relation = resolveRelation(u)
relationToLogicalPlanMaps.update(u, relation)
relation
})
}

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