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 tripsExchange addedWhy
Join on `driver_id`, `drivers` in 2048 buckets on itNoneBoth scans already cluster rows on the join key
`groupBy("driver_id", "city")`NoneRows sharing both keys already share a `driver_id` bucket
`groupBy("city")`1Each city's trips sit in all 2048 buckets
Join on `driver_id` and `city`, both sides bucketed on `driver_id`Both sidesThe join needs rows clustered on both keys together
Join to `drivers` whose `driver_id` is a stringThe string sideSpark 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()
trips
trip_iddriver_idcityfare
1017New York18
1023Austin12.5
1037New York31
1045Denver9
1053Austin22
1069New York15.5
1075Denver27
1089New York11
1097New York19.5
1103Austin14

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.

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")

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.