[SPARK-33575][SQL] Fix misleading exception for "ANALYZE TABLE ... FOR COLUMNS" on temporary views

### What changes were proposed in this pull request?

This PR proposes to fix the exception message for `ANALYZE TABLE ... FOR COLUMNS` on temporary views.

The current behavior throws `NoSuchTableException` even if the temporary view exists:
```
sql("CREATE TEMP VIEW t AS SELECT 1 AS id")
sql("ANALYZE TABLE t COMPUTE STATISTICS FOR COLUMNS id")
org.apache.spark.sql.catalyst.analysis.NoSuchTableException: Table or view 't' not found in database 'db';
  at org.apache.spark.sql.execution.command.AnalyzeColumnCommand.analyzeColumnInTempView(AnalyzeColumnCommand.scala:76)
  at org.apache.spark.sql.execution.command.AnalyzeColumnCommand.run(AnalyzeColumnCommand.scala:54)
```

After this PR, more reasonable exception is thrown:
```
org.apache.spark.sql.AnalysisException: Temporary view `testView` is not cached for analyzing columns.;
[info]   at org.apache.spark.sql.execution.command.AnalyzeColumnCommand.analyzeColumnInTempView(AnalyzeColumnCommand.scala:74)
[info]   at org.apache.spark.sql.execution.command.AnalyzeColumnCommand.run(AnalyzeColumnCommand.scala:54)
```

### Why are the changes needed?

To fix a misleading exception.

### Does this PR introduce _any_ user-facing change?

Yes, the exception thrown is changed as shown above.

### How was this patch tested?

Updated existing test.

Closes #30519 from imback82/analyze_table_message.

Authored-by: Terry Kim <yuminkim@gmail.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
This commit is contained in:
Terry Kim 2020-11-27 07:08:24 +00:00 committed by Wenchen Fan
parent 433ae9064f
commit 8792280a73
3 changed files with 9 additions and 6 deletions

View file

@ -71,9 +71,8 @@ case class AnalyzeColumnCommand(
private def analyzeColumnInTempView(plan: LogicalPlan, sparkSession: SparkSession): Unit = {
if (!analyzeColumnInCachedData(plan, sparkSession)) {
val catalog = sparkSession.sessionState.catalog
val db = tableIdent.database.getOrElse(catalog.getCurrentDatabase)
throw new NoSuchTableException(db = db, table = tableIdent.identifier)
throw new AnalysisException(
s"Temporary view $tableIdent is not cached for analyzing columns.")
}
}

View file

@ -526,7 +526,7 @@ class StatisticsCollectionSuite extends StatisticsCollectionTestBase with Shared
val errMsg = intercept[AnalysisException] {
sql("ANALYZE TABLE tempView COMPUTE STATISTICS FOR COLUMNS id")
}.getMessage
assert(errMsg.contains(s"Table or view 'tempView' not found in database 'default'"))
assert(errMsg.contains("Temporary view `tempView` is not cached for analyzing columns"))
// Cache the view then analyze it
sql("CACHE TABLE tempView")
@ -548,7 +548,8 @@ class StatisticsCollectionSuite extends StatisticsCollectionTestBase with Shared
val errMsg2 = intercept[AnalysisException] {
sql(s"ANALYZE TABLE $globalTempDB.gTempView COMPUTE STATISTICS FOR COLUMNS id")
}.getMessage
assert(errMsg2.contains(s"Table or view 'gTempView' not found in database '$globalTempDB'"))
assert(errMsg2.contains(
s"Temporary view `$globalTempDB`.`gTempView` is not cached for analyzing columns"))
// Cache the view then analyze it
sql(s"CACHE TABLE $globalTempDB.gTempView")

View file

@ -188,7 +188,10 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
sql(s"ANALYZE TABLE $viewName COMPUTE STATISTICS")
}.getMessage
assert(e5.contains(s"$viewName is a temp view not table or permanent view"))
assertNoSuchTable(s"ANALYZE TABLE $viewName COMPUTE STATISTICS FOR COLUMNS id")
val e6 = intercept[AnalysisException] {
sql(s"ANALYZE TABLE $viewName COMPUTE STATISTICS FOR COLUMNS id")
}.getMessage
assert(e6.contains(s"Temporary view `$viewName` is not cached for analyzing columns."))
}
}