Github user wzhfy commented on a diff in the pull request: https://github.com/apache/spark/pull/18421#discussion_r125152564 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/command/AnalyzeTableCommand.scala --- @@ -38,10 +44,92 @@ case class AnalyzeTableCommand( if (tableMeta.tableType == CatalogTableType.VIEW) { throw new AnalysisException("ANALYZE TABLE is not supported on views.") } - val newTotalSize = CommandUtils.calculateTotalSize(sessionState, tableMeta) - val oldTotalSize = tableMeta.stats.map(_.sizeInBytes.toLong).getOrElse(0L) - val oldRowCount = tableMeta.stats.flatMap(_.rowCount.map(_.toLong)).getOrElse(-1L) + if (!partitionSpec.isDefined) { + // Compute stats for the whole table + val newTotalSize = CommandUtils.calculateTotalSize(sessionState, tableMeta) + val newRowCount = + if (noscan) { + None + } else { + Some(BigInt(sparkSession.table(tableIdentWithDB).count())) + } + + def updateStats(newStats: CatalogStatistics): Unit = { + sessionState.catalog.alterTableStats(tableIdentWithDB, Some(newStats)) + // Refresh the cached data source table in the catalog. + sessionState.catalog.refreshTable(tableIdentWithDB) + } + + calculateAndUpdateStats(tableMeta.stats, newTotalSize, newRowCount, updateStats) + } else { + val partitions = sessionState.catalog.listPartitions(tableMeta.identifier, partitionSpec) + + if (partitionSpec.isDefined && partitions.isEmpty) { + throw new NoSuchPartitionException(db, tableIdent.table, partitionSpec.get) + } + + // Compute stats for individual partitions + val rowCounts: Map[TablePartitionSpec, BigInt] = + if (noscan) { + Map.empty + } else { + calculateRowCountsPerPartition(sparkSession, tableMeta) + } + + partitions.foreach(p => { + val newTotalSize = CommandUtils.calculateTotalSize(sessionState, tableMeta, p) + val newRowCount = rowCounts.get(p.spec) + + def updateStats(newStats: CatalogStatistics): Unit = { + sessionState.catalog.alterPartitions(tableMeta.identifier, + List(p.copy(stats = Some(newStats)))) + } + + calculateAndUpdateStats(p.stats, newTotalSize, newRowCount, updateStats) + }) + } + + Seq.empty[Row] + } + + private def calculateRowCountsPerPartition( + sparkSession: SparkSession, + tableMeta: CatalogTable): Map[TablePartitionSpec, BigInt] = { + val filters = partitionSpec.get.map { + case (columnName, value) => EqualTo(UnresolvedAttribute(columnName), Literal(value)) + } + val filter = filters match { + case head :: tail => + if (tail.isEmpty) head + else tail.foldLeft(head: Expression)((a, b) => And(a, b)) + } --- End diff -- `val filter = filters.reduce(And)`
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org