69. Iceberg Hidden Partition Layout
Difficulty: Medium · Topics: Lakehouse, Aggregations, Dates
An Iceberg table of events (event_id, user_id, ts as 'YYYY-MM-DD HH:MM:SS') is partitioned by day(ts) and bucket(4, user_id). Users never write partition values; Iceberg derives them.
Work out the layout the data would have. Use event_day = the date of ts as 'YYYY-MM-DD' and, as a simplified bucket transform, bucket = user_id modulo 4. Return event_day, bucket and events (rows in that partition). Rows with a null ts or user_id go to a partition whose value is null, as in Iceberg.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
events
| event_id | user_id | ts |
|---|---|---|
| 1 | 10 | 2025-03-01 09:15:00 |
| 2 | 11 | 2025-03-01 10:00:00 |
| 3 | 14 | 2025-03-01 23:59:59 |
| 4 | 10 | 2025-03-02 00:00:01 |
| 5 | 7 | 2025-03-02 12:30:00 |
Expected output
| event_day | bucket | events |
|---|---|---|
| 2025-03-01 | 2 | 2 |
| 2025-03-01 | 3 | 1 |
| 2025-03-02 | 2 | 1 |
| 2025-03-02 | 3 | 1 |
Hints
Hint 1
F.date_format("ts", "yyyy-MM-dd") gives the day; it returns null for a null timestamp.Hint 2
F.col("user_id") % 4 is the simplified bucket; null stays null.Hint 3
Group by both derived columns and count.Learn the concepts
- Hidden partitioning and partition evolution · 6 min read. Iceberg partition transforms, and changing a table layout without rewriting its data.
- Partitioning done right · 6 min read. When to partition a table, how to choose the column, and how over-partitioning backfires.
- Parsing dates and timestamps · 4 min read. to_date, to_timestamp and date_format patterns, and why yyyy and YYYY are not the same.
PySpark functions you'll practise
- groupBy
- agg
- count
- date_format
- withColumn
Related problems
- Rebuild a Table from Its Transaction Log · Medium · Lakehouse, Joins, Aggregations
- Files VACUUM Can Delete · Medium · Lakehouse, Joins, Aggregations
- Net Changes from a Change Feed · Hard · Lakehouse, Aggregations, Conditional Logic
- Apply a CDC Batch (MERGE Semantics) · Hard · Lakehouse, Window Functions, Joins
- What Changed Between Two Versions · Hard · Lakehouse, Joins, Strings
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice