Github user concretevitamin commented on a diff in the pull request:
https://github.com/apache/spark/pull/1741#discussion_r15733277
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveContext.scala ---
@@ -92,6 +95,64 @@ class HiveContext(sc: SparkContext) extends
SQLContext(sc) {
catalog.createTable("default", tableName,
ScalaReflection.attributesFor[A], allowExisting)
}
+ /**
+ * Analyzes the given table in the current database to generate
statistics, which will be
+ * used in query optimizations.
+ *
+ * Right now, it only supports Hive tables and it only updates the size
of a Hive table
+ * in the Hive metastore.
+ */
+ def analyze(tableName: String) {
+ val relation = catalog.lookupRelation(None, tableName) match {
+ case LowerCaseSchema(r) => r
+ case o => o
+ }
+
+ relation match {
+ case relation: MetastoreRelation => {
+ // This method is borrowed from
+ //
org.apache.hadoop.hive.ql.stats.StatsUtils.getFileSizeForTable(HiveConf, Table)
+ // in Hive 0.13.
+ // TODO: Generalize statistics collection.
+ def getFileSizeForTable(conf: HiveConf, table: Table): Long = {
+ val path = table.getPath()
+ var size: Long = 0L
+ try {
+ val fs = path.getFileSystem(conf)
+ size = fs.getContentSummary(path).getLength()
+ } catch {
+ case e: Exception =>
+ logWarning(
+ s"Failed to get the size of table ${table.getTableName} in
the " +
+ s"database ${table.getDbName} because of ${e.toString}", e)
+ size = 0L
+ }
+
+ size
+ }
+
+ val tableParameters = relation.hiveQlTable.getParameters
+ val oldTotalSize =
+
Option(tableParameters.get(StatsSetupConst.TOTAL_SIZE)).map(_.toLong).getOrElse(0L)
+ val newTotalSize = getFileSizeForTable(hiveconf,
relation.hiveQlTable)
+ // Update the Hive metastore if the total size of the table is
different than the size
+ // recorded in the Hive metastore.
+ // This logic is based on
org.apache.hadoop.hive.ql.exec.StatsTask.aggregateStats().
+ if (newTotalSize > 0 && newTotalSize != oldTotalSize) {
+ tableParameters.put(StatsSetupConst.TOTAL_SIZE,
newTotalSize.toString)
+ val hiveTTable = relation.hiveQlTable.getTTable
+ hiveTTable.setParameters(tableParameters)
+ val tableFullName =
+ relation.hiveQlTable.getDbName() + "." +
relation.hiveQlTable.getTableName()
+
+ catalog.client.alterTable(tableFullName, new Table(hiveTTable))
+ }
+ }
+ case otherRelation =>
+ throw new NotImplementedError(s"Analyzing a ${otherRelation} has
not been implemented")
--- End diff --
We probably don't want the result of a general `.toString`. Perhaps just
say "Analyzing relations other than MetastoreRelation's has not been
implemented" instead.
---
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]