From 54df39243eca2eed0482ce37f842b3ccfbc77723 Mon Sep 17 00:00:00 2001 From: gatorsmile Date: Sat, 17 Sep 2016 23:21:38 -0700 Subject: [PATCH] fix. --- .../spark/sql/execution/command/ddl.scala | 3 ++- .../spark/sql/hive/StatisticsSuite.scala | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala index c0ccdca98e05b..76f65e21190df 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala @@ -679,7 +679,8 @@ case class AlterTableSetLocationCommand( } else { table.withNewStorage(locationUri = Some(location)) } - catalog.alterTable(newTable) + val newTableWithInvalidatedStats = newTable.copy(stats = None) + catalog.alterTable(newTableWithInvalidatedStats) } Seq.empty[Row] } diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/StatisticsSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/StatisticsSuite.scala index 9956706929cd1..6a9945c87d209 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/StatisticsSuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/StatisticsSuite.scala @@ -290,6 +290,27 @@ class StatisticsSuite extends QueryTest with TestHiveSingleton with SQLTestUtils } } + test("alter table set location after analyze table") { + val textTable = "textTable" + withTable(textTable) { + sql(s"CREATE TABLE $textTable (key STRING, value STRING) STORED AS TEXTFILE") + sql(s"INSERT INTO TABLE $textTable SELECT * FROM src") + sql(s"ANALYZE TABLE $textTable COMPUTE STATISTICS") + checkStats( + textTable, isDataSourceTable = false, hasSizeInBytes = true, expectedRowCounts = Some(500)) + + withTempDir { tmpDir => + val path = tmpDir.getCanonicalPath + sql(s"ALTER TABLE $textTable SET LOCATION '$path'") + checkStats( + textTable, + isDataSourceTable = false, + hasSizeInBytes = false, + expectedRowCounts = None) + } + } + } + test("test table-level statistics for data source table created in HiveExternalCatalog") { val parquetTable = "parquetTable" withTable(parquetTable) {