Github user gatorsmile commented on a diff in the pull request:
https://github.com/apache/spark/pull/14712#discussion_r77373066
--- Diff:
sql/hive/src/test/scala/org/apache/spark/sql/hive/StatisticsSuite.scala ---
@@ -168,6 +170,137 @@ class StatisticsSuite extends QueryTest with
TestHiveSingleton with SQLTestUtils
TableIdentifier("tempTable"), ignoreIfNotExists = true, purge =
false)
}
+ private def checkMetastoreRelationStats(
+ tableName: String,
+ expectedStats: Option[Statistics]): Unit = {
+ val df = sql(s"SELECT * FROM $tableName")
+ val relations = df.queryExecution.analyzed.collect { case rel:
MetastoreRelation =>
+ expectedStats match {
+ case Some(es) =>
+ assert(rel.catalogTable.stats.isDefined)
+ val stats = rel.catalogTable.stats.get
+ assert(stats.sizeInBytes === es.sizeInBytes)
+ assert(stats.rowCount === es.rowCount)
+ case None =>
+ assert(rel.catalogTable.stats.isEmpty)
+ }
+ rel
+ }
+ assert(relations.size === 1)
+ }
+
+ test("test table-level statistics for hive tables created in
HiveExternalCatalog") {
+ val textTable = "textTable"
+ withTable(textTable) {
+ // Currently Spark's statistics are self-contained, we don't have
statistics until we use
+ // the `ANALYZE TABLE` command.
+ sql(s"CREATE TABLE $textTable (key STRING, value STRING) STORED AS
TEXTFILE")
+ checkMetastoreRelationStats(textTable, expectedStats = None)
+ sql(s"INSERT INTO TABLE $textTable SELECT * FROM src")
+ checkMetastoreRelationStats(textTable, expectedStats = None)
+
+ // noscan won't count the number of rows
+ sql(s"ANALYZE TABLE $textTable COMPUTE STATISTICS noscan")
+ checkMetastoreRelationStats(textTable, expectedStats =
+ Some(Statistics(sizeInBytes = 5812, rowCount = None)))
+
+ // without noscan, we count the number of rows
+ sql(s"ANALYZE TABLE $textTable COMPUTE STATISTICS")
+ checkMetastoreRelationStats(textTable, expectedStats =
+ Some(Statistics(sizeInBytes = 5812, rowCount = Some(500))))
+ }
+ }
+
+ test("test elimination of the influences of the old stats") {
+ 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")
+ checkMetastoreRelationStats(textTable, expectedStats =
+ Some(Statistics(sizeInBytes = 5812, rowCount = Some(500))))
+
--- End diff --
Just here, could you add a few lines?
```
sql(s"ANALYZE TABLE $textTable COMPUTE STATISTICS noscan")
// when the total size is not changed, the old row count is kept
checkMetastoreRelationStats(textTable, expectedStats =
Some(Statistics(sizeInBytes = 5812, rowCount = Some(500))))
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]