Aggregate pushdown
| Also known as | Aggregation pushdown, metadata-only aggregation |
|---|---|
| Field | Query optimization |
| Key parts | Connector interface for aggregates, file footer or table metadata statistics (row counts, min, max, null counts), fallback to a normal scan |
| Open source implementations | Apache Spark (Data Source V2), Apache Iceberg, Apache Parquet and ORC readers in Spark, JDBC sources |
| Related | Predicate pushdown, Column pruning, Apache Parquet, Data Source V2 |
Aggregate pushdown is a query optimization in which an engine hands an aggregate such as COUNT(*), MIN(col) or MAX(col) to the data source, which answers it from metadata or computes it itself, instead of returning every row for the engine to aggregate.[1] For file formats like Apache Parquet, the answer can come from statistics in the file footers without reading data pages; for Apache Iceberg, it can come from the per-file statistics in the table metadata. In Apache Spark, aggregate pushdown is part of the Data Source V2 API.
How does aggregate pushdown work in Apache Spark?
A Data Source V2 connector opts in by implementing SupportsPushDownAggregates on its ScanBuilder (since Spark 3.2.0). Spark calls pushAggregation with the aggregate functions and grouping expressions; the source returns true if it can handle them. A source may complete the aggregation fully or only partially, in which case Spark adds a final aggregate on top of the source's output.[1] The API was added in SPARK-34952, fixed in 3.2.0.[2]
Spark pushes filters to the source first and aggregates second. If a pushed filter must still be evaluated after the scan, the aggregate cannot be pushed, because the source's answer would include rows that the filter later removes.[1] For Parquet and ORC footers the rule is stricter: any filter on a data column blocks pushdown, because the statistics describe all rows and not only those that pass the filter.[3]
A Parquet file footer records the number of rows in the file and in each row group, and optional column chunk statistics, including min_value, max_value and null_count.[4] From these values a reader can compute COUNT(*) as the sum of row counts, COUNT(col) as row count minus null count, and MIN or MAX as the smallest or largest bound, without decoding any data page.
Spark's Parquet and ORC aggregate pushdown, added in Spark 3.3.0 (SPARK-36645), works this way.[5] It is off by default and is enabled with spark.sql.parquet.aggregatePushdown or spark.sql.orc.aggregatePushdown (both default false, since 3.3.0). The setting's documentation warns that if statistics are missing from any file footer, Spark throws an exception.[6]
spark.conf.set("spark.sql.parquet.aggregatePushdown", "true")
spark.read.parquet("/data/events").selectExpr("count(*)", "max(event_id)").explain()
# On the V2 Parquet scan path, the scan metadata lists the pushed aggregates under PushedAggregation
What are the limits of Parquet and ORC aggregate pushdown in Spark?
At Spark 4.3.0, the rules in AggregatePushDownUtils are:[3]
- Functions.
MIN,MAX,COUNT(col)andCOUNT(*).COUNT(DISTINCT ...)and other aggregates are not pushed. - Types for MIN and MAX. Boolean, byte, short, integer, long, float, double, date and time. Timestamps are excluded because the INT96 sort order is undefined; strings, binary and decimals are excluded because they can be stored as Parquet binary values, whose minimum and maximum may be truncated.
- No data filters. Any filter on a data column blocks pushdown. A filter on a partition column is allowed.
- Grouping.
GROUP BYis pushed only when the grouping columns are exactly the table's partition columns. - No partition columns in MIN or MAX, because the footer has no statistics for them.
The pushdown is implemented in the Data Source V2 Parquet and ORC scans.[7] In Spark 4.3.0 the built-in parquet and orc sources still use the V1 code path by default, because they are in the internal setting spark.sql.sources.useV1SourceList (default avro,csv,json,kafka,orc,parquet,text), so the pushdown applies only when Spark reads these formats through the V2 path.[6]
Aggregate pushdown in Apache Iceberg
Apache Iceberg's Spark connector answers aggregates from the column statistics stored in its manifest files. The feature is controlled by spark.sql.iceberg.aggregate-push-down.enabled (default true), and covers MAX, MIN and COUNT.[8]
In Iceberg 1.12.0, the Spark scan builder skips aggregate pushdown when:[9]
- the query has a
GROUP BY; - any data file in the scan has row-level delete files, since deleted rows would be counted;
- the column's metrics mode is
none, or iscountsand the query asks forMINorMAX; - the query asks for
MINorMAXof a string or binary column, because stored bounds may be truncated; - the table is a metadata table rather than the main table.
When pushdown succeeds, Iceberg plans the files, computes the result from their statistics, and returns it to Spark as a local scan of one row, so no data file is opened.[9]
Aggregate pushdown to JDBC sources
For the JDBC data source, the pushDownAggregate option (default true) lets Spark send aggregates to the database. The Spark documentation notes that aggregates are pushed only if all the aggregate functions and their related filters can be pushed, and that Spark still applies a final aggregate unless numPartitions is 1 or the group-by key equals partitionColumn.[10]
How can you tell if an aggregate was pushed?
In EXPLAIN output, a V2 Parquet or ORC scan with a pushed aggregate lists PushedAggregation and PushedGroupBy in its metadata.[11] A JDBC scan lists PushedAggregates and PushedGroupByExpressions.[12] For Iceberg, a pushed aggregate replaces the batch scan with a local scan, and the Iceberg logs record the reason whenever pushdown is skipped.[9]
See also
References
- ↑ 1.0 1.1 1.2 SupportsPushDownAggregates.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SPARK-34952: DS V2 Aggregate push down. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 3.0 3.1 AggregatePushDownUtils.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ parquet.thrift (apache-parquet-format-2.12.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SPARK-36645: Aggregate (Min/Max/Count) push down for Parquet. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 6.0 6.1 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ ParquetScanBuilder.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Configuration (Iceberg 1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 9.0 9.1 9.2 SparkScanBuilder.java (apache-iceberg-1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ JDBC To Other Databases (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ ParquetScan.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ DataSourceScanExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.