Jump to content

Bucketing

From Open Lakehouse Wiki
Bucketing
Also known as Bucketed tables, CLUSTERED BY ... INTO n BUCKETS, bucket join
Field Data layout; query optimization
Key parts Bucket columns, number of buckets, hash function, optional sort columns, catalog metadata
Open source implementations Apache Spark, Apache Hive, Apache Iceberg (bucket partition transform)
Related Shuffle, Partition, Storage partitioned join, Liquid clustering, Sort-merge join
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (SQLConf.scala, DataFrameWriter.scala, V1Writes.scala, partitioning.scala, DataSourceScanExec.scala, docs/sql-data-sources-load-save-functions.md, docs/sql-performance-tuning.md), Spark JIRA, the Apache Iceberg 1.12.0 spec and Spark configuration docs, and AbstractDeltaCatalog.scala at Delta Lake v4.4.1.

Bucketing is a table layout in which rows are assigned to a fixed number of logical buckets by hashing the values of one or more bucket columns, so that all rows with the same key land in the same bucket; each bucket is stored as one or more files, whose names carry the bucket ID.[1] In Apache Spark, a bucketed table lets a join or aggregation on the bucket columns skip the shuffle, because the data is already grouped the way the operator needs it. Spark's file layout for buckets follows the bucketing scheme of Apache Hive;[2] in Apache Iceberg the same idea is the bucket[N] partition transform, and Delta Lake does not support bucketed tables.

How does bucketing work in Apache Spark?

Spark writes a bucketed table with DataFrameWriter.bucketBy (optionally with sortBy), available since Spark 2.0, or with CLUSTERED BY ... INTO n BUCKETS in SQL. Bucketing and sorting apply only to persistent tables written with saveAsTable, because the bucket specification is stored in the catalog.[3][2]

(orders.write
    .bucketBy(64, "customer_id")
    .sortBy("customer_id")
    .format("parquet")
    .saveAsTable("sales.orders_bucketed"))
CREATE TABLE sales.customers_bucketed
USING parquet
CLUSTERED BY (customer_id) INTO 64 BUCKETS
AS SELECT * FROM sales.customers;

A bucket is not a single file. Every write task that holds rows for a bucket writes its own file for that bucket, and a task starts a new file when it reaches maxRecordsPerFile; Spark appends the bucket ID and a file counter to each file name.[1]

For Spark's own bucketed tables, the bucket ID is the same expression Spark uses for hash partitioning during a shuffle: a Murmur3 hash of the bucket columns, modulo the number of buckets. The source code comment explains why: matching the shuffle's distribution lets Spark shuffle only one side when it joins a bucketed table with a table that is not bucketed.[4][5]

When does bucketing remove a shuffle?

Spark can skip the shuffle when the plan needs data clustered on the bucket columns and the scan already provides it:

  • Joins. A sort-merge join or shuffled hash join of two tables bucketed on the join keys, with the same number of buckets, needs no Exchange. If the bucket counts differ and the larger is a multiple of the smaller, spark.sql.bucketing.coalesceBucketsInJoin.enabled (default false, since 3.1.0) can merge buckets on the larger side, as long as the ratio is at most spark.sql.bucketing.coalesceBucketsInJoin.maxBucketRatio (default 4). The setting's documentation warns that coalescing reduces parallelism.[6]
  • Aggregations and windows grouped or partitioned by the bucket columns.
  • One-sided joins. When only one table is bucketed, Spark shuffles only the other side, because the bucket hash matches the shuffle hash.[4]

By default spark.sql.requireAllClusterKeysForCoPartition (true, since 3.3.0) requires the join keys to cover all the clustering keys before Spark removes the shuffle. This avoids skew that can come from eliminating shuffles.[6]

Bucketing also helps filters. A filter on a bucket column with an equality, IN or IS NULL condition (and AND or OR combinations of them) lets Spark read only the matching bucket files, which is called bucket pruning.[7]

Settings that control bucketing

Setting Default Since Effect
spark.sql.sources.bucketing.enabled true 2.0.0 When false, Spark treats bucketed tables as ordinary tables.
spark.sql.sources.bucketing.autoBucketedScan.enabled true 3.1.0 Spark skips the bucketed scan when the query has no operator that uses bucketing, or when an exchange sits between the scan and that operator.
spark.sql.sources.bucketing.maxBuckets 100000 2.4.0 Largest bucket count allowed.
spark.sql.bucketing.coalesceBucketsInJoin.enabled false 3.1.0 Merges buckets on the larger side of a join when one count divides the other.

All values are from SQLConf.scala at Spark 4.3.0.[6] The automatic choice of bucketed scan was added in SPARK-32859, and bucket coalescing in SPARK-31350 and SPARK-32286, all fixed in 3.1.0.[8][9]

How can you tell if bucketing was used?

In EXPLAIN output, a file scan that reads buckets lists Bucketed: true and SelectedBucketsCount (for example "8 out of 64") in its metadata, and there is no Exchange between the scan and the join or aggregate.[10] If the scan does not read buckets, the value gives the reason: false (disabled by configuration), false (disabled by query planner) or false (bucket column(s) not read).[10]

Spark bucketing vs Hive bucketing

Spark's bucket layout looks like Hive's but uses a different hash function, so Spark-native bucketed tables are not compatible with Hive's bucketing.[2] When Spark writes into a Hive-format bucketed table, it uses Hive's hash and Hive's bucket file naming instead, so that Hive and other engines can read the result; SPARK-32709 and SPARK-32712 added this in Spark 3.3.0.[4][11][12]

How does bucketing interact with table formats?

Delta Lake rejects bucketed tables: at Delta Lake 4.4.1, creating a Delta table with a bucket specification raises an "operation not supported" error, and combining CLUSTER BY with CLUSTERED BY ... INTO BUCKETS is rejected with a message to remove the bucketing.[13][14] Delta tables use partitioning or liquid clustering for data layout instead.

Apache Iceberg has bucketing as a partition transform. bucket[N] hashes a value with the 32-bit Murmur3 hash (x86 variant, seed 0), drops the sign bit and takes the result modulo N.[15] Because it is a partition, a bucket partition gets partition pruning from the table metadata, not from file names. An Iceberg bucket hash does not match Spark's shuffle hash, so Spark removes the shuffle through a storage partitioned join instead, using the partitioning that a Data Source V2 table reports. This needs spark.sql.sources.v2.bucketing.enabled (default true, since 3.3.0) on the Spark side and spark.sql.iceberg.planning.preserve-data-grouping (default false) on the Iceberg side.[6][16][17]

-- Iceberg: bucketing as a partition transform
CREATE TABLE prod.db.orders (id BIGINT, customer_id BIGINT, amount DECIMAL(10,2))
USING iceberg
PARTITIONED BY (bucket(16, customer_id));

Limitations

  • Bucketing is fixed at write time. Changing the number of buckets or the bucket columns means rewriting the table.
  • Spark-native buckets are readable as buckets only by Spark, because of the hash function.[2]

See also

References

  1. ↑ 1.0 1.1 FileFormatDataWriter.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 2.3 DataFrameWriter.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ Generic Load/Save Functions (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 4.2 V1Writes.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ partitioning.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ 6.0 6.1 6.2 6.3 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ FileSourceStrategy.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ SPARK-32859: Introduce SQL physical plan rule to decide enable/disable bucketing. Apache Software Foundation.Retrieved 2026-11-23.
  9. ↑ SPARK-31350: Coalesce bucketed tables for join if applicable. Apache Software Foundation.Retrieved 2026-11-23.
  10. ↑ 10.0 10.1 DataSourceScanExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  11. ↑ SPARK-32709: Write Hive ORC/Parquet bucketed table with hivehash (for Hive 1,2). Apache Software Foundation.Retrieved 2026-11-23.
  12. ↑ SPARK-32712: Support writing Hive non-ORC/Parquet bucketed table. Apache Software Foundation.Retrieved 2026-11-23.
  13. ↑ AbstractDeltaCatalog.scala (v4.4.1). Delta Lake.Retrieved 2026-11-23.
  14. ↑ ClusterBySpec.scala (v4.4.1). Delta Lake.Retrieved 2026-11-23.
  15. ↑ Iceberg Table Spec (apache-iceberg-1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
  16. ↑ Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  17. ↑ Spark Configuration (Iceberg 1.12.0). Apache Software Foundation.Retrieved 2026-11-23.