Bucketing Pitfalls
Concepts covered: Shuffle Optimization
A bucketed table keeps its exchange-free plan only while each query meets the conditions the planner checks. Miss any of them and the plan regains an exchange with no error and no warning: the same result, at the old shuffle cost. Traps 1 to 3 show up in explain() as an Exchange sitting above a scan you expected to read buckets. Traps 4 and 5 add no exchange; they show up as file counts on disk and as per-task input sizes in the Spark UI.
Trap 1: reading the files instead of the table
The bucket column and count live in the metastore; the files hold rows and nothing more. spark.read.parquet("/lake/curated/trips_bucketed/") reads the same bytes as spark.table("curated.trips_bucketed"), yet sees a plain directory, so its scan reports no partitioning at all. Read a bucketed table by its name, or through a view over that name.
Trap 2: bucket counts that differ
Buckets line up only when both sides split the key space the same number of ways. With trips in 2048 buckets and drivers in 512, a driver in bucket 1,031 of trips sits in bucket 7 of drivers, since 1,031 modulo 512 is 7. Spark keeps the side with more buckets as it is and shuffles the other into 2048 partitions, so drivers pays a 120GB exchange on every run.
Spark 3.1 added spark.sql.bucketing.coalesceBucketsInJoin.enabled, off by default. When one count divides the other, it reads the larger side at the smaller count, giving up parallelism to drop the exchange.
Trap 3: join columns that differ from the bucket columns
Join keys must equal the bucket columns, in name and in type. A groupBy is looser: its keys must include the bucket columns and may add others. This is what each query on trips, bucketed by driver_id, gets:
| Query on trips | Exchange added | Why |
|---|---|---|
| Join on `driver_id`, `drivers` in 2048 buckets on it | None | Both scans already cluster rows on the join key |
| `groupBy("driver_id", "city")` | None | Rows sharing both keys already share a `driver_id` bucket |
| `groupBy("city")` | 1 | Each city's trips sit in all 2048 buckets |
| Join on `driver_id` and `city`, both sides bucketed on `driver_id` | Both sides | The join needs rows clustered on both keys together |
| Join to `drivers` whose `driver_id` is a string | The string side | Spark casts the string side, and the cast column has no bucket layout |
The animation plays the groupBy("city") row at small scale: rows are laid out by driver_id first, then aggregated by city.
from pyspark.sql import functions as F
by_driver = trips.repartition(3, "driver_id")
per_city = by_driver.groupBy("city").agg(F.sum("fare").alias("revenue"))
per_city.collect()| trip_id | driver_id | city | fare |
|---|---|---|---|
| 101 | 7 | New York | 18 |
| 102 | 3 | Austin | 12.5 |
| 103 | 7 | New York | 31 |
| 104 | 5 | Denver | 9 |
| 105 | 3 | Austin | 22 |
| 106 | 9 | New York | 15.5 |
| 107 | 5 | Denver | 27 |
| 108 | 9 | New York | 11 |
| 109 | 7 | New York | 19.5 |
| 110 | 3 | Austin | 14 |
The second exchange moves every row again, because each city's trips are scattered across all 3 driver partitions. A layout on driver_id is worth nothing to a query that groups or joins on anything else.
Trap 4: the file explosion on write
Leave out the repartition before bucketBy and every write task opens a file for each bucket it holds rows for. A day of trips is read as 16,384 input tasks of 128MB, and hashing scatters rows from nearly every bucket into each of them.
- 33.5M
- 1GB
Trap 5: the parallelism ceiling
A bucketed scan runs 1 task per bucket and never splits a bucket across tasks, so the count fixed at write time is the parallelism of every bucketed join and aggregation that follows. Bucket 2TB into 64 and each of 64 tasks reads 32GB, however many cores the cluster has. Bucket a column with 30 distinct values into 2048 and 2,018 buckets sit empty while 30 tasks do the work.
A teammate reads the bucketed trips table by path and joins it to drivers_bucketed, read by name. Both were written with 2048 buckets on driver_id. What does the join's plan contain?
trips = spark.read.parquet("/lake/curated/trips_bucketed/")
drivers = spark.table("curated.drivers_bucketed")
joined = trips.join(drivers, "driver_id")
joined.write.mode("overwrite").saveAsTable("curated.trips_priced")- No exchange, because both sets of files are bucketed on disk
- Exchanges on both sides, because a path read disables bucketing for the query
- An analysis error, because a bucketed table cannot be read by path
- 1 exchange, on trips, into 2048 partitions to match the drivers buckets
Cardinality decides whether a column can fill its buckets: a column with fewer distinct values than buckets caps the useful task count at that number. Compare 2 candidate bucket columns on user_events.
About this lesson
This section is part of the Partitioning: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.