spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From concretevitamin <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-2783][SQL] Basic support for analyze in...
Date Sun, 03 Aug 2014 07:07:02 GMT
Github user concretevitamin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1741#discussion_r15733274
  
    --- 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)
    --- End diff --
    
    Do we need to be concerned about concurrent accesses to `tableParameters`? More generally
do we need to somehow synchronize on `MetastoreRelation#hiveQlTable` in various places?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message