The situation
A nightly job copies new rows from an event feed into the warehouse. To pick up only
new rows, it filters on event_time > time of the previous run.
Some events reach the feed hours or days after they happened. By then a later run has
already moved past their event_time, so they are never loaded. The warehouse is
missing events and nobody gets an error.
You are rebuilding the load logic. For every run in the history, work out which events that run should have loaded.
Input tables
events
One row per delivery of an event to the feed. An event is normally delivered once, but the feed sometimes delivers the same event again later.
| Column | Type | Description |
|---|---|---|
event_id |
BIGINT | Identifies the event. Repeated if the event was delivered again. |
user_id |
BIGINT | The user who triggered the event. |
event_type |
STRING | page_view, search, add_to_cart or purchase. |
event_time |
TIMESTAMP_NTZ | When the event happened. |
ingested_at |
TIMESTAMP_NTZ | When this delivery arrived in the feed. Always later than event_time. |
load_runs
One row per run of the load job.
| Column | Type | Description |
|---|---|---|
run_id |
BIGINT | Unique per run. |
run_time |
TIMESTAMP_NTZ | The cutoff of the run. The run can see everything that had arrived by this time. |
Requirement
- A run loads the events that arrived after the previous run's
run_timeand up to and including its ownrun_time. The first run loads everything that had arrived by itsrun_time. - "Arrived" means
ingested_at. When the event happened does not matter. - Each event is loaded exactly once, by the run that covers its first delivery. A later delivery of the same event is ignored.
- Events that arrived after the last run are not loaded yet and are not in the result.
- A run with nothing to load has no rows in the result.
Expected output
One row per loaded event.
| Column | Type | Description |
|---|---|---|
event_id |
BIGINT | |
run_id |
BIGINT | The run that loads the event. |
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
An incremental load has to answer one question per run: "what is new since last
time?" New means "arrived since last time", so the filter belongs on the arrival
time, ingested_at. event_time says when something happened in the real world,
and the feed gives no promise that events arrive in that order.
Three steps:
- Give every run its window. A run owns the arrivals after the previous run's
run_timeand up to its own.LAG(run_time)over the runs in time order gives the lower bound, which is NULL for the first run. - Reduce every event to its first arrival with
MIN(ingested_at)perevent_id. That makes a redelivered event indistinguishable from one delivered once. - Join each event to the run whose window contains its first arrival.
run_windows = runs.withColumn("prev_run_time", F.lag("run_time").over(Window.orderBy("run_time")))
first_arrival = events.groupBy("event_id").agg(F.min("ingested_at").alias("first_ingested_at"))
in_window = (F.col("first_ingested_at") <= F.col("run_time")) & (
F.col("prev_run_time").isNull() | (F.col("first_ingested_at") > F.col("prev_run_time"))
)
result = first_arrival.join(run_windows, in_window, "inner").select("event_id", "run_id")
The windows are half-open: > on the lower bound, <= on the upper. Every moment
in time then belongs to exactly one run, so no event is loaded twice and none falls
between two runs.
The feed has 193 rows for 183 events. 21 events arrived after the last run, which leaves 162 to load.
Common wrong answers
Windows on event_time, restricted to what had arrived. This is the original job:
at each run, take rows with event_time after the previous cutoff that are already in
the feed. It returns 141 rows. The 21 events that happened before one cutoff but
arrived after it are never picked up by any run.
Windows on event_time alone. Returns 170 rows. It assigns events to runs that
could not have seen them yet, and it "loads" events that only arrived after the last
run.
No handling of redelivery. Joining every row of events to the windows returns
166 rows: an event delivered again in a later window is loaded by both runs. Adding
DISTINCT only removes the repeats that fall into the same window as the first
delivery, and still returns 164.
Using the last arrival instead of the first. Returns 159 rows. Events whose second delivery came after the last run disappear, although their first delivery was loaded long before.
Getting the boundary the wrong way round. With < on the upper bound and >= on
the lower, the row count is still 162, but the three events that arrived exactly at a
run_time move to the following run. With >= and <= together (or BETWEEN),
those three events are loaded by two runs each, giving 165 rows.
Edge cases in the data
| Case | Where | What it catches |
|---|---|---|
| Events that arrive 6 to 60 hours late | 21 of them cross a run boundary | Filtering on event_time. |
Arrival exactly at a run_time |
events 9501, 9502, 9503 | < vs <=, and BETWEEN. |
| Event delivered twice | 10 events | Loading the same event in two runs. |
| Events that arrived after the last run | 21 events | Assigning by event_time; inventing a run for them. |
| A run with nothing to load | run 5, a rerun 30 minutes after run 4 | Assuming every run has rows. |
| First run has no previous run | run 1 | A lower bound of NULL that silently matches nothing. |