-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21213][SQL] Support collecting partition-level statistics: rowCount and sizeInBytes #18421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5384f7a
3ee5ebf
d17aa4b
e0e351e
8dad9bc
4fdefd5
1d696c3
89c0767
7210568
9aa2a1e
fa21860
f76f49f
8f31f53
fae6d49
8880fbd
1053991
41ab30d
dc488e5
c839855
72e2cd5
87594d6
3353afa
8ffb140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,30 +90,40 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder(conf) { | |
| } | ||
|
|
||
| /** | ||
| * Create an [[AnalyzeTableCommand]] command or an [[AnalyzeColumnCommand]] command. | ||
| * Example SQL for analyzing table : | ||
| * Create an [[AnalyzeTableCommand]] command, or an [[AnalyzePartitionCommand]] | ||
| * or an [[AnalyzeColumnCommand]] command. | ||
| * Example SQL for analyzing a table or a set of partitions : | ||
| * {{{ | ||
| * ANALYZE TABLE table COMPUTE STATISTICS [NOSCAN]; | ||
| * ANALYZE TABLE [db_name.]tablename [PARTITION (partcol1[=val1], partcol2[=val2], ...)] | ||
| * COMPUTE STATISTICS [NOSCAN]; | ||
| * }}} | ||
| * | ||
| * Example SQL for analyzing columns : | ||
| * {{{ | ||
| * ANALYZE TABLE table COMPUTE STATISTICS FOR COLUMNS column1, column2; | ||
| * ANALYZE TABLE [db_name.]tablename COMPUTE STATISTICS FOR COLUMNS column1, column2; | ||
| * }}} | ||
| */ | ||
| override def visitAnalyze(ctx: AnalyzeContext): LogicalPlan = withOrigin(ctx) { | ||
| if (ctx.partitionSpec != null) { | ||
| logWarning(s"Partition specification is ignored: ${ctx.partitionSpec.getText}") | ||
| if (ctx.identifier != null && | ||
| ctx.identifier.getText.toLowerCase(Locale.ROOT) != "noscan") { | ||
| throw new ParseException(s"Expected `NOSCAN` instead of `${ctx.identifier.getText}`", ctx) | ||
| } | ||
| if (ctx.identifier != null) { | ||
| if (ctx.identifier.getText.toLowerCase(Locale.ROOT) != "noscan") { | ||
| throw new ParseException(s"Expected `NOSCAN` instead of `${ctx.identifier.getText}`", ctx) | ||
|
|
||
| val table = visitTableIdentifier(ctx.tableIdentifier) | ||
| if (ctx.identifierSeq() == null) { | ||
| if (ctx.partitionSpec != null) { | ||
| AnalyzePartitionCommand(table, visitPartitionSpec(ctx.partitionSpec), | ||
| noscan = ctx.identifier != null) | ||
| } else { | ||
| AnalyzeTableCommand(table, noscan = ctx.identifier != null) | ||
| } | ||
| AnalyzeTableCommand(visitTableIdentifier(ctx.tableIdentifier)) | ||
| } else if (ctx.identifierSeq() == null) { | ||
| AnalyzeTableCommand(visitTableIdentifier(ctx.tableIdentifier), noscan = false) | ||
| } else { | ||
| if (ctx.partitionSpec != null) { | ||
| logWarning("Partition specification is ignored when collecting column statistics: " + | ||
| ctx.partitionSpec.getText) | ||
|
||
| } | ||
| AnalyzeColumnCommand( | ||
| visitTableIdentifier(ctx.tableIdentifier), | ||
| table, | ||
| visitIdentifierSeq(ctx.identifierSeq())) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.command | ||
|
|
||
| import org.apache.spark.sql.{AnalysisException, Column, Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.TableIdentifier | ||
| import org.apache.spark.sql.catalyst.analysis.{NoSuchPartitionException, UnresolvedAttribute} | ||
| import org.apache.spark.sql.catalyst.catalog.{CatalogTable, CatalogTableType} | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec | ||
| import org.apache.spark.sql.catalyst.expressions.{And, EqualTo, Literal} | ||
| import org.apache.spark.sql.execution.datasources.PartitioningUtils | ||
|
|
||
| /** | ||
| * Analyzes a given set of partitions to generate per-partition statistics, which will be used in | ||
| * query optimizations. | ||
| * | ||
| * When `partitionSpec` is empty, statistics for all partitions are collected and stored in | ||
| * Metastore. | ||
| * | ||
| * When `partitionSpec` mentions only some of the partition columns, all partitions with | ||
| * matching values for specified columns are processed. | ||
| * | ||
| * If `partitionSpec` mentions unknown partition column, an `AnalysisException` is raised. | ||
| * | ||
| * By default, total number of rows and total size in bytes are calculated. When `noscan` | ||
| * is `true`, only total size in bytes is computed. | ||
| */ | ||
| case class AnalyzePartitionCommand( | ||
| tableIdent: TableIdentifier, | ||
| partitionSpec: Map[String, Option[String]], | ||
| noscan: Boolean = true) extends RunnableCommand { | ||
|
|
||
| private def getPartitionSpec(table: CatalogTable): Option[TablePartitionSpec] = { | ||
| val normalizedPartitionSpec = | ||
| PartitioningUtils.normalizePartitionSpec(partitionSpec, table.partitionColumnNames, | ||
| table.identifier.quotedString, conf.resolver) | ||
|
|
||
| // Report an error if partition columns in partition specification do not form | ||
| // a prefix of the list of partition columns defined in the table schema | ||
| val isNotSpecified = | ||
| table.partitionColumnNames.map(normalizedPartitionSpec.getOrElse(_, None).isEmpty) | ||
| if (isNotSpecified.init.zip(isNotSpecified.tail).contains((true, false))) { | ||
| val tableId = table.identifier | ||
| val schemaColumns = table.partitionColumnNames.mkString(",") | ||
| val specColumns = normalizedPartitionSpec.keys.mkString(",") | ||
| throw new AnalysisException("The list of partition columns with values " + | ||
| s"in partition specification for table '${tableId.table}' " + | ||
| s"in database '${tableId.database.get}' is not a prefix of the list of " + | ||
| "partition columns defined in the table schema. " + | ||
| s"Expected a prefix of [${schemaColumns}], but got [${specColumns}].") | ||
| } | ||
|
|
||
| val filteredSpec = normalizedPartitionSpec.filter(_._2.isDefined).mapValues(_.get) | ||
| if (filteredSpec.isEmpty) { | ||
| None | ||
| } else { | ||
| Some(filteredSpec) | ||
| } | ||
| } | ||
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
| val sessionState = sparkSession.sessionState | ||
| val db = tableIdent.database.getOrElse(sessionState.catalog.getCurrentDatabase) | ||
| val tableIdentWithDB = TableIdentifier(tableIdent.table, Some(db)) | ||
| val tableMeta = sessionState.catalog.getTableMetadata(tableIdentWithDB) | ||
| if (tableMeta.tableType == CatalogTableType.VIEW) { | ||
| throw new AnalysisException("ANALYZE TABLE is not supported on views.") | ||
| } | ||
|
|
||
| val partitionValueSpec = getPartitionSpec(tableMeta) | ||
|
|
||
| val partitions = sessionState.catalog.listPartitions(tableMeta.identifier, partitionValueSpec) | ||
|
|
||
| if (partitions.isEmpty) { | ||
| if (partitionValueSpec.isDefined) { | ||
| throw new NoSuchPartitionException(db, tableIdent.table, partitionValueSpec.get) | ||
| } else { | ||
| // the user requested to analyze all partitions for a table which has no partitions | ||
| // return normally, since there is nothing to do | ||
| return Seq.empty[Row] | ||
| } | ||
| } | ||
|
|
||
| // Compute statistics for individual partitions | ||
| val rowCounts: Map[TablePartitionSpec, BigInt] = | ||
| if (noscan) { | ||
| Map.empty | ||
| } else { | ||
| calculateRowCountsPerPartition(sparkSession, tableMeta, partitionValueSpec) | ||
| } | ||
|
|
||
| // Update the metastore if newly computed statistics are different from those | ||
| // recorded in the metastore. | ||
| val newPartitions = partitions.flatMap { p => | ||
| val newTotalSize = CommandUtils.calculateLocationSize( | ||
| sessionState, tableMeta.identifier, p.storage.locationUri) | ||
| val newRowCount = rowCounts.get(p.spec) | ||
| val newStats = CommandUtils.compareAndGetNewStats(tableMeta.stats, newTotalSize, newRowCount) | ||
| newStats.map(_ => p.copy(stats = newStats)) | ||
| } | ||
|
|
||
| if (newPartitions.nonEmpty) { | ||
| sessionState.catalog.alterPartitions(tableMeta.identifier, newPartitions) | ||
| } | ||
|
|
||
| Seq.empty[Row] | ||
| } | ||
|
|
||
| private def calculateRowCountsPerPartition( | ||
| sparkSession: SparkSession, | ||
| tableMeta: CatalogTable, | ||
| partitionValueSpec: Option[TablePartitionSpec]): Map[TablePartitionSpec, BigInt] = { | ||
| val filter = if (partitionValueSpec.isDefined) { | ||
| val filters = partitionValueSpec.get.map { | ||
| case (columnName, value) => EqualTo(UnresolvedAttribute(columnName), Literal(value)) | ||
| } | ||
| filters.reduce(And) | ||
| } else { | ||
| Literal.TrueLiteral | ||
| } | ||
|
|
||
| val tableDf = sparkSession.table(tableMeta.identifier) | ||
| val partitionColumns = tableMeta.partitionColumnNames.map(Column(_)) | ||
|
|
||
| val df = tableDf.filter(Column(filter)).groupBy(partitionColumns: _*).count() | ||
|
|
||
| df.collect().map { r => | ||
| val partitionColumnValues = partitionColumns.indices.map(r.get(_).toString) | ||
| val spec = tableMeta.partitionColumnNames.zip(partitionColumnValues).toMap | ||
| val count = BigInt(r.getLong(partitionColumns.size)) | ||
| (spec, count) | ||
| }.toMap | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| CREATE TABLE t (key STRING, value STRING, ds STRING, hr INT) USING parquet | ||
| PARTITIONED BY (ds, hr); | ||
|
|
||
| INSERT INTO TABLE t PARTITION (ds='2017-08-01', hr=10) | ||
| VALUES ('k1', 100), ('k2', 200), ('k3', 300); | ||
|
|
||
| INSERT INTO TABLE t PARTITION (ds='2017-08-01', hr=11) | ||
| VALUES ('k1', 101), ('k2', 201), ('k3', 301), ('k4', 401); | ||
|
|
||
| INSERT INTO TABLE t PARTITION (ds='2017-09-01', hr=5) | ||
| VALUES ('k1', 102), ('k2', 202); | ||
|
|
||
| DESC EXTENDED t PARTITION (ds='2017-08-01', hr=10); | ||
|
|
||
| -- Collect stats for a single partition | ||
| ANALYZE TABLE t PARTITION (ds='2017-08-01', hr=10) COMPUTE STATISTICS; | ||
|
|
||
| DESC EXTENDED t PARTITION (ds='2017-08-01', hr=10); | ||
|
|
||
| -- Collect stats for 2 partitions | ||
| ANALYZE TABLE t PARTITION (ds='2017-08-01') COMPUTE STATISTICS; | ||
|
|
||
| DESC EXTENDED t PARTITION (ds='2017-08-01', hr=10); | ||
| DESC EXTENDED t PARTITION (ds='2017-08-01', hr=11); | ||
|
|
||
| -- Collect stats for all partitions | ||
| ANALYZE TABLE t PARTITION (ds, hr) COMPUTE STATISTICS; | ||
|
|
||
| DESC EXTENDED t PARTITION (ds='2017-08-01', hr=10); | ||
| DESC EXTENDED t PARTITION (ds='2017-08-01', hr=11); | ||
| DESC EXTENDED t PARTITION (ds='2017-09-01', hr=5); | ||
|
|
||
| -- DROP TEST TABLES/VIEWS | ||
| DROP TABLE t; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a test case. Could you add a new test file to this test suite for analyzing and describing table partitions? You can generate the result file by running the command:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gatorsmile , this code doesn't make partition stats appear in the output of DESC command. It only adds stats into to CatalogTablePartition.toString output (similar to CatalogTable.toString). Do you still want me to add some tests for this functionality?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please add it. Try it and we should expose it to the external users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. It works like you said it would. :-) Adding a test.