From 94666e9cd63a9471f50ab2fce6c8439c8fd74154 Mon Sep 17 00:00:00 2001 From: Rui Wang Date: Fri, 6 Oct 2023 10:48:24 +0800 Subject: [PATCH] [SPARK-45421][SQL] Catch AnalysisException over InlineCTE ### What changes were proposed in this pull request? We are catching exceptions for `CheckAnalysis` and attach inlined plan. However, if `InlineCTE` itself throws we can also catch but attach original plan. ### Why are the changes needed? Attach original plan if `InlineCTE` throws. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Existing UT ### Was this patch authored or co-authored using generative AI tooling? NO Closes #43227 from amaliujia/inline_cte_plan. Authored-by: Rui Wang Signed-off-by: Wenchen Fan --- .../spark/sql/catalyst/analysis/CheckAnalysis.scala | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala index 64f1cafd03fea..81ca59c0976eb 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala @@ -164,12 +164,18 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB } // Inline all CTEs in the plan to help check query plan structures in subqueries. - val inlinedPlan = inlineCTE(plan) + var inlinedPlan: Option[LogicalPlan] = None try { - checkAnalysis0(inlinedPlan) + inlinedPlan = Some(inlineCTE(plan)) } catch { case e: AnalysisException => - throw new ExtendedAnalysisException(e, inlinedPlan) + throw new ExtendedAnalysisException(e, plan) + } + try { + checkAnalysis0(inlinedPlan.get) + } catch { + case e: AnalysisException => + throw new ExtendedAnalysisException(e, inlinedPlan.get) } plan.setAnalyzed() }