From dffb01f28a1f5fb6445e59e1c8eefdd683fe4a29 Mon Sep 17 00:00:00 2001 From: Wenchen Fan Date: Thu, 25 Feb 2021 15:25:41 -0800 Subject: [PATCH] [SPARK-34152][SQL][FOLLOWUP] Do not uncache the temp view if it doesn't exist ### What changes were proposed in this pull request? This PR fixes a mistake in https://github.com/apache/spark/pull/31273. When CREATE OR REPLACE a temp view, we need to uncache the to-be-replaced existing temp view. However, we shouldn't uncache if there is no existing temp view. This doesn't cause real issues because the uncache action is failure-safe. But it produces a lot of warning messages. ### Why are the changes needed? Avoid unnecessary warning logs. ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? manually run tests and check the warning messages. Closes #31650 from cloud-fan/warnning. Authored-by: Wenchen Fan Signed-off-by: Dongjoon Hyun --- .../spark/sql/execution/command/views.scala | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala index bae7c54f0b99d..5f5f0099e2bfb 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala @@ -115,7 +115,7 @@ case class CreateViewCommand( if (viewType == LocalTempView) { val aliasedPlan = aliasPlan(sparkSession, analyzedPlan) - if (replace && !isSamePlan(catalog.getRawTempView(name.table), aliasedPlan)) { + if (replace && needsToUncache(catalog.getRawTempView(name.table), aliasedPlan)) { logInfo(s"Try to uncache ${name.quotedString} before replacing.") checkCyclicViewReference(analyzedPlan, Seq(name), name) CommandUtils.uncacheTableOrView(sparkSession, name.quotedString) @@ -139,7 +139,7 @@ case class CreateViewCommand( val db = sparkSession.sessionState.conf.getConf(StaticSQLConf.GLOBAL_TEMP_DATABASE) val viewIdent = TableIdentifier(name.table, Option(db)) val aliasedPlan = aliasPlan(sparkSession, analyzedPlan) - if (replace && !isSamePlan(catalog.getRawGlobalTempView(name.table), aliasedPlan)) { + if (replace && needsToUncache(catalog.getRawGlobalTempView(name.table), aliasedPlan)) { logInfo(s"Try to uncache ${viewIdent.quotedString} before replacing.") checkCyclicViewReference(analyzedPlan, Seq(viewIdent), viewIdent) CommandUtils.uncacheTableOrView(sparkSession, viewIdent.quotedString) @@ -193,15 +193,17 @@ case class CreateViewCommand( } /** - * Checks if the temp view (the result of getTempViewRawPlan or getRawGlobalTempView) is storing - * the same plan as the given aliased plan. + * Checks if need to uncache the temp view being replaced. */ - private def isSamePlan( + private def needsToUncache( rawTempView: Option[LogicalPlan], aliasedPlan: LogicalPlan): Boolean = rawTempView match { - case Some(TemporaryViewRelation(_, Some(p))) => p.sameResult(aliasedPlan) - case Some(p) => p.sameResult(aliasedPlan) - case _ => false + // The temp view doesn't exist, no need to uncache. + case None => false + // Do not need to uncache if the to-be-replaced temp view plan and the new plan are the + // same-result plans. + case Some(TemporaryViewRelation(_, Some(p))) => !p.sameResult(aliasedPlan) + case Some(p) => !p.sameResult(aliasedPlan) } /**