Skip to content

45. Iceberg Hidden Partition Layout

Difficulty: Medium · Topics: 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_iduser_idts
1102025-03-01 09:15:00
2112025-03-01 10:00:00
3142025-03-01 23:59:59
4102025-03-02 00:00:01
572025-03-02 12:30:00

Expected output

event_daybucketevents
2025-03-0122
2025-03-0131
2025-03-0221
2025-03-0231

Hints

Hint 1F.date_format("ts", "yyyy-MM-dd") gives the day; it returns null for a null timestamp.
Hint 2F.col("user_id") % 4 is the simplified bucket; null stays null.
Hint 3Group by both derived columns and count.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · Learn · All problems