Skip to content

Commit 3c8c714

Browse files
committed
[SPARK-53574][SQL][FOLLOWUP] still reset the default AnalysisContext
### What changes were proposed in this pull request? This is a followup of #52330 . To be safe, we should keep the previous behavior that resets the `AnalysisContext`, in case this is the default `AnalysisContext` (not a fresh one created to analyze a new query) which is kind of a global state. ### Why are the changes needed? to avoid potention regressions ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? N/A ### Was this patch authored or co-authored using generative AI tooling? No Closes #52482 from cloud-fan/follow. Authored-by: Wenchen Fan <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 746db3d commit 3c8c714

File tree

1 file changed

+26
-12
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis

1 file changed

+26
-12
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ object FakeV2SessionCatalog extends TableCatalog with FunctionCatalog with Suppo
139139
* expressions in a subquery.
140140
*/
141141
case class AnalysisContext(
142+
isDefault: Boolean = false,
142143
catalogAndNamespace: Seq[String] = Nil,
143144
nestedViewDepth: Int = 0,
144145
maxNestedViewDepth: Int = -1,
@@ -175,7 +176,7 @@ case class AnalysisContext(
175176

176177
object AnalysisContext {
177178
private val value = new ThreadLocal[AnalysisContext]() {
178-
override def initialValue: AnalysisContext = AnalysisContext()
179+
override def initialValue: AnalysisContext = AnalysisContext(isDefault = true)
179180
}
180181

181182
def get: AnalysisContext = value.get()
@@ -201,13 +202,14 @@ object AnalysisContext {
201202
originContext.maxNestedViewDepth
202203
}
203204
val context = AnalysisContext(
204-
viewDesc.viewCatalogAndNamespace,
205-
originContext.nestedViewDepth + 1,
206-
maxNestedViewDepth,
207-
originContext.relationCache,
208-
viewDesc.viewReferredTempViewNames,
209-
mutable.Set(viewDesc.viewReferredTempFunctionNames: _*),
210-
viewDesc.viewReferredTempVariableNames,
205+
isDefault = false,
206+
catalogAndNamespace = viewDesc.viewCatalogAndNamespace,
207+
nestedViewDepth = originContext.nestedViewDepth + 1,
208+
maxNestedViewDepth = maxNestedViewDepth,
209+
relationCache = originContext.relationCache,
210+
referredTempViewNames = viewDesc.viewReferredTempViewNames,
211+
referredTempFunctionNames = mutable.Set(viewDesc.viewReferredTempFunctionNames: _*),
212+
referredTempVariableNames = viewDesc.viewReferredTempVariableNames,
211213
collation = viewDesc.collation)
212214
set(context)
213215
try f finally { set(originContext) }
@@ -217,12 +219,13 @@ object AnalysisContext {
217219
def withNewAnalysisContext[A](f: => A): A = {
218220
val originContext = value.get()
219221
reset()
222+
set(get.copy(isDefault = false))
220223
try f finally { set(originContext) }
221224
}
222225

223226
def withOuterPlan[A](outerPlan: LogicalPlan)(f: => A): A = {
224227
val originContext = value.get()
225-
val context = originContext.copy(outerPlan = Some(outerPlan))
228+
val context = originContext.copy(isDefault = false, outerPlan = Some(outerPlan))
226229
set(context)
227230
try f finally { set(originContext) }
228231
}
@@ -308,9 +311,20 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
308311
if (plan.analyzed) {
309312
plan
310313
} else {
311-
AnalysisContext.withNewAnalysisContext {
312-
AnalysisHelper.markInAnalyzer {
313-
HybridAnalyzer.fromLegacyAnalyzer(legacyAnalyzer = this).apply(plan, tracker)
314+
if (AnalysisContext.get.isDefault) {
315+
AnalysisContext.reset()
316+
try {
317+
AnalysisHelper.markInAnalyzer {
318+
HybridAnalyzer.fromLegacyAnalyzer(legacyAnalyzer = this).apply(plan, tracker)
319+
}
320+
} finally {
321+
AnalysisContext.reset()
322+
}
323+
} else {
324+
AnalysisContext.withNewAnalysisContext {
325+
AnalysisHelper.markInAnalyzer {
326+
HybridAnalyzer.fromLegacyAnalyzer(legacyAnalyzer = this).apply(plan, tracker)
327+
}
314328
}
315329
}
316330
}

0 commit comments

Comments
 (0)