Github user yhuai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1741#discussion_r15735734
  
    --- 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 --
    
    Seems every time we do lookupRelation, we get a new instance of hiveQlTable 
(with a new instance of underlying Hive TTable). So, I think `tableParameters` 
will not be shared. The place we do metastore update is 
`catalog.client.alterTable(tableFullName, new Table(hiveTTable))`. I guess the 
underlying metastore can take care the concurrent update.
    
    @liancheng can you also take a look at this analyze method?


---
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]

Reply via email to