<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Bucketing</id>
	<title>Bucketing - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Bucketing"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Bucketing&amp;action=history"/>
	<updated>2026-12-06T00:08:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Bucketing&amp;diff=128&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Bucketing&amp;diff=128&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:35Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Bucketing&lt;br /&gt;
| also_known_as   = Bucketed tables, CLUSTERED BY ... INTO n BUCKETS, bucket join&lt;br /&gt;
| field           = Data layout; query optimization&lt;br /&gt;
| key_parts       = Bucket columns, number of buckets, hash function, optional sort columns, catalog metadata&lt;br /&gt;
| implementations = [[Apache Spark]], [[Apache Hive]], [[Apache Iceberg]] (bucket partition transform)&lt;br /&gt;
| related         = [[Shuffle]], [[Partition]], [[Storage partitioned join]], [[Liquid clustering]], [[Sort-merge join]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|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}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Bucketing&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;fdw&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatDataWriter.scala |title=FileFormatDataWriter.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; 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&amp;#039;s file layout for buckets follows the bucketing scheme of [[Apache Hive]];&amp;lt;ref name=&amp;quot;writer&amp;quot; /&amp;gt; in [[Apache Iceberg]] the same idea is the &amp;lt;code&amp;gt;bucket[N]&amp;lt;/code&amp;gt; partition transform, and [[Delta Lake]] does not support bucketed tables.&lt;br /&gt;
&lt;br /&gt;
== How does bucketing work in Apache Spark? ==&lt;br /&gt;
Spark writes a bucketed table with &amp;lt;code&amp;gt;DataFrameWriter.bucketBy&amp;lt;/code&amp;gt; (optionally with &amp;lt;code&amp;gt;sortBy&amp;lt;/code&amp;gt;), available since Spark 2.0, or with &amp;lt;code&amp;gt;CLUSTERED BY ... INTO n BUCKETS&amp;lt;/code&amp;gt; in SQL. Bucketing and sorting apply only to persistent tables written with &amp;lt;code&amp;gt;saveAsTable&amp;lt;/code&amp;gt;, because the bucket specification is stored in the catalog.&amp;lt;ref name=&amp;quot;loadsave&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-data-sources-load-save-functions.html |title=Generic Load/Save Functions (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;writer&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/DataFrameWriter.scala |title=DataFrameWriter.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
(orders.write&lt;br /&gt;
    .bucketBy(64, &amp;quot;customer_id&amp;quot;)&lt;br /&gt;
    .sortBy(&amp;quot;customer_id&amp;quot;)&lt;br /&gt;
    .format(&amp;quot;parquet&amp;quot;)&lt;br /&gt;
    .saveAsTable(&amp;quot;sales.orders_bucketed&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE sales.customers_bucketed&lt;br /&gt;
USING parquet&lt;br /&gt;
CLUSTERED BY (customer_id) INTO 64 BUCKETS&lt;br /&gt;
AS SELECT * FROM sales.customers;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;code&amp;gt;maxRecordsPerFile&amp;lt;/code&amp;gt;; Spark appends the bucket ID and a file counter to each file name.&amp;lt;ref name=&amp;quot;fdw&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Spark&amp;#039;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&amp;#039;s distribution lets Spark shuffle only one side when it joins a bucketed table with a table that is not bucketed.&amp;lt;ref name=&amp;quot;v1writes&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.scala |title=V1Writes.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;partitioning&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/physical/partitioning.scala |title=partitioning.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== When does bucketing remove a shuffle? ==&lt;br /&gt;
Spark can skip the shuffle when the plan needs data clustered on the bucket columns and the scan already provides it:&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Joins.&amp;#039;&amp;#039;&amp;#039; A [[sort-merge join]] or shuffled hash join of two tables bucketed on the join keys, with the same number of buckets, needs no &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt;. If the bucket counts differ and the larger is a multiple of the smaller, &amp;lt;code&amp;gt;spark.sql.bucketing.coalesceBucketsInJoin.enabled&amp;lt;/code&amp;gt; (default false, since 3.1.0) can merge buckets on the larger side, as long as the ratio is at most &amp;lt;code&amp;gt;spark.sql.bucketing.coalesceBucketsInJoin.maxBucketRatio&amp;lt;/code&amp;gt; (default 4). The setting&amp;#039;s documentation warns that coalescing reduces parallelism.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Aggregations and windows&amp;#039;&amp;#039;&amp;#039; grouped or partitioned by the bucket columns.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;One-sided joins.&amp;#039;&amp;#039;&amp;#039; When only one table is bucketed, Spark shuffles only the other side, because the bucket hash matches the shuffle hash.&amp;lt;ref name=&amp;quot;v1writes&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default &amp;lt;code&amp;gt;spark.sql.requireAllClusterKeysForCoPartition&amp;lt;/code&amp;gt; (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.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bucketing also helps filters. A filter on a bucket column with an equality, &amp;lt;code&amp;gt;IN&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;IS NULL&amp;lt;/code&amp;gt; condition (and &amp;lt;code&amp;gt;AND&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;OR&amp;lt;/code&amp;gt; combinations of them) lets Spark read only the matching bucket files, which is called &amp;#039;&amp;#039;&amp;#039;bucket pruning&amp;#039;&amp;#039;&amp;#039;.&amp;lt;ref name=&amp;quot;fss&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileSourceStrategy.scala |title=FileSourceStrategy.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Settings that control bucketing ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Setting !! Default !! Since !! Effect&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.sources.bucketing.enabled&amp;lt;/code&amp;gt; || true || 2.0.0 || When false, Spark treats bucketed tables as ordinary tables.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.sources.bucketing.autoBucketedScan.enabled&amp;lt;/code&amp;gt; || 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.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.sources.bucketing.maxBuckets&amp;lt;/code&amp;gt; || 100000 || 2.4.0 || Largest bucket count allowed.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.bucketing.coalesceBucketsInJoin.enabled&amp;lt;/code&amp;gt; || false || 3.1.0 || Merges buckets on the larger side of a join when one count divides the other.&lt;br /&gt;
|}&lt;br /&gt;
All values are from &amp;lt;code&amp;gt;SQLConf.scala&amp;lt;/code&amp;gt; at Spark 4.3.0.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt; 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.&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-32859 |title=SPARK-32859: Introduce SQL physical plan rule to decide enable/disable bucketing |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-31350 |title=SPARK-31350: Coalesce bucketed tables for join if applicable |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How can you tell if bucketing was used? ==&lt;br /&gt;
In &amp;lt;code&amp;gt;EXPLAIN&amp;lt;/code&amp;gt; output, a file scan that reads buckets lists &amp;lt;code&amp;gt;Bucketed: true&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SelectedBucketsCount&amp;lt;/code&amp;gt; (for example &amp;quot;8 out of 64&amp;quot;) in its metadata, and there is no &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; between the scan and the join or aggregate.&amp;lt;ref name=&amp;quot;scanexec&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/DataSourceScanExec.scala |title=DataSourceScanExec.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; If the scan does not read buckets, the value gives the reason: &amp;lt;code&amp;gt;false (disabled by configuration)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;false (disabled by query planner)&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;false (bucket column(s) not read)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;scanexec&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Spark bucketing vs Hive bucketing ==&lt;br /&gt;
Spark&amp;#039;s bucket layout looks like Hive&amp;#039;s but uses a different hash function, so Spark-native bucketed tables are not compatible with Hive&amp;#039;s bucketing.&amp;lt;ref name=&amp;quot;writer&amp;quot; /&amp;gt; When Spark writes into a Hive-format bucketed table, it uses Hive&amp;#039;s hash and Hive&amp;#039;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.&amp;lt;ref name=&amp;quot;v1writes&amp;quot; /&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-32709 |title=SPARK-32709: Write Hive ORC/Parquet bucketed table with hivehash (for Hive 1,2) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-32712 |title=SPARK-32712: Support writing Hive non-ORC/Parquet bucketed table |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does bucketing interact with table formats? ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[Delta Lake]]&amp;#039;&amp;#039;&amp;#039; rejects bucketed tables: at Delta Lake 4.4.1, creating a Delta table with a bucket specification raises an &amp;quot;operation not supported&amp;quot; error, and combining &amp;lt;code&amp;gt;CLUSTER BY&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;CLUSTERED BY ... INTO BUCKETS&amp;lt;/code&amp;gt; is rejected with a message to remove the bucketing.&amp;lt;ref name=&amp;quot;delta&amp;quot;&amp;gt;{{Cite web |url=https://github.com/delta-io/delta/blob/v4.4.1/spark/src/main/scala/org/apache/spark/sql/delta/catalog/AbstractDeltaCatalog.scala |title=AbstractDeltaCatalog.scala (v4.4.1) |publisher=Delta Lake |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/delta-io/delta/blob/v4.4.1/spark/src/main/scala/org/apache/spark/sql/delta/skipping/clustering/temp/ClusterBySpec.scala |title=ClusterBySpec.scala (v4.4.1) |publisher=Delta Lake |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Delta tables use partitioning or [[liquid clustering]] for data layout instead.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[Apache Iceberg]]&amp;#039;&amp;#039;&amp;#039; has bucketing as a partition transform. &amp;lt;code&amp;gt;bucket[N]&amp;lt;/code&amp;gt; hashes a value with the 32-bit Murmur3 hash (x86 variant, seed 0), drops the sign bit and takes the result modulo N.&amp;lt;ref name=&amp;quot;icebergspec&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/format/spec.md |title=Iceberg Table Spec (apache-iceberg-1.12.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; 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&amp;#039;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 &amp;lt;code&amp;gt;spark.sql.sources.v2.bucketing.enabled&amp;lt;/code&amp;gt; (default true, since 3.3.0) on the Spark side and &amp;lt;code&amp;gt;spark.sql.iceberg.planning.preserve-data-grouping&amp;lt;/code&amp;gt; (default false) on the Iceberg side.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/docs/docs/spark-configuration.md |title=Spark Configuration (Iceberg 1.12.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
-- Iceberg: bucketing as a partition transform&lt;br /&gt;
CREATE TABLE prod.db.orders (id BIGINT, customer_id BIGINT, amount DECIMAL(10,2))&lt;br /&gt;
USING iceberg&lt;br /&gt;
PARTITIONED BY (bucket(16, customer_id));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Limitations ==&lt;br /&gt;
* Bucketing is fixed at write time. Changing the number of buckets or the bucket columns means rewriting the table.&lt;br /&gt;
* Spark-native buckets are readable as buckets only by Spark, because of the hash function.&amp;lt;ref name=&amp;quot;writer&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
* [[Partition]]&lt;br /&gt;
* [[Partition pruning]]&lt;br /&gt;
* [[Storage partitioned join]]&lt;br /&gt;
* [[Sort-merge join]]&lt;br /&gt;
* [[Apache Iceberg]]&lt;br /&gt;
* [[Delta Lake]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>