Skip to content

Commit 2b32a44

Browse files
gatorsmilehvanhovell
authored andcommitted
[SPARK-17167][2.0][SQL] Issue Exceptions when Analyze Table on In-Memory Cataloged Tables
### What changes were proposed in this pull request? Currently, `Analyze Table` is only used for Hive-serde tables. We should issue exceptions in all the other cases. When the tables are data source tables, we issued an exception. However, when tables are In-Memory Cataloged tables, we do not issue any exception. This PR is to issue an exception when the tables are in-memory cataloged. For example, ```SQL CREATE TABLE tbl(a INT, b INT) USING parquet ``` `tbl` is a `SimpleCatalogRelation` when the hive support is not enabled. ### How was this patch tested? Added two test cases. One of them is just to improve the test coverage when the analyzed table is data source tables. Author: gatorsmile <gatorsmile@gmail.com> Closes #14781 from gatorsmile/analyzeInMemoryTable2.
1 parent 48ecf3d commit 2b32a44

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/command/AnalyzeTableCommand.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import org.apache.hadoop.fs.{FileSystem, Path}
2323

2424
import org.apache.spark.sql.{AnalysisException, Row, SparkSession}
2525
import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases
26-
import org.apache.spark.sql.catalyst.catalog.{CatalogRelation, CatalogTable}
26+
import org.apache.spark.sql.catalyst.catalog.{CatalogRelation, CatalogTable, SimpleCatalogRelation}
2727

2828

2929
/**
@@ -41,7 +41,7 @@ case class AnalyzeTableCommand(tableName: String) extends RunnableCommand {
4141
val relation = EliminateSubqueryAliases(sessionState.catalog.lookupRelation(tableIdent))
4242

4343
relation match {
44-
case relation: CatalogRelation =>
44+
case relation: CatalogRelation if !relation.isInstanceOf[SimpleCatalogRelation] =>
4545
val catalogTable: CatalogTable = relation.catalogTable
4646
// This method is mainly based on
4747
// org.apache.hadoop.hive.ql.stats.StatsUtils.getFileSizeForTable(HiveConf, Table)

sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,17 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
395395
assert(catalog.getTableMetadata(tableIdent1) === expectedTable)
396396
}
397397

398+
test("Analyze in-memory cataloged tables(SimpleCatalogRelation)") {
399+
withTable("tbl") {
400+
sql("CREATE TABLE tbl(a INT, b INT) USING parquet")
401+
val e = intercept[AnalysisException] {
402+
sql("ANALYZE TABLE tbl COMPUTE STATISTICS")
403+
}.getMessage
404+
assert(e.contains("ANALYZE TABLE is only supported for Hive tables, " +
405+
"but 'tbl' is a SimpleCatalogRelation"))
406+
}
407+
}
408+
398409
test("create table using") {
399410
val catalog = spark.sessionState.catalog
400411
withTable("tbl") {

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,21 @@ class HiveDDLSuite
594594
}
595595
}
596596

597+
test("Analyze data source tables(LogicalRelation)") {
598+
withTable("t1") {
599+
withTempPath { dir =>
600+
val path = dir.getCanonicalPath
601+
spark.range(1).write.format("parquet").save(path)
602+
sql(s"CREATE TABLE t1 USING parquet OPTIONS (PATH '$path')")
603+
val e = intercept[AnalysisException] {
604+
sql("ANALYZE TABLE t1 COMPUTE STATISTICS")
605+
}.getMessage
606+
assert(e.contains("ANALYZE TABLE is only supported for Hive tables, " +
607+
"but 't1' is a LogicalRelation"))
608+
}
609+
}
610+
}
611+
597612
test("desc table for data source table") {
598613
withTable("tab1") {
599614
val tabName = "tab1"

0 commit comments

Comments
 (0)