Run incremental workloads
Most production workloads are incremental. A pipeline does not rebuild its tables from scratch on every run, but adds the rows of the new batch of data, such as the last hour or the last day, to tables that already exist. Bauplan supports incremental workloads through two materialization strategies, APPEND and OVERWRITE_PARTITIONS.
A batch is rarely complete when it is first processed. Some rows arrive late, after their batch has already been processed, delayed by network latency, client retries or devices that were offline.
On every run, process the current batch and a few batches before it, overwrite that window with OVERWRITE_PARTITIONS, and keep every row the model returns inside it.
Choose a lookback window
Whenever data comes in batches, for instance every hour or every day, write it incrementally rather than replacing the whole table on every run, and process again a few batches before the current one on each run. That lookback window is what catches late arrivals.
Most systems record two times for each event, created_at, when the event happened (a click, a transaction), and received_at, when the server that writes it to object storage received it. When using cloud services such as AWS Firehose, the Hive-partitions on object storage of the raw files usually follow received_at, as in s3://my-bucket/raw/events/y=2026/m=10/d=12/h=09/. An event created at 8:59:58 and received at 9:00:02 lands in the 9 AM partition, although it happened the hour before.
A lookback window of a few partitions catches most late arrivals. How far to look back depends on the system and on how much compute power you are willing to dedicate, since a longer lookback catches more late rows and reprocesses more data on every run. Whatever the length, rows that arrive later than the lookback are not processed, and choosing a window means accepting that loss.
Overwrite the window instead of appending it
Prefer OVERWRITE_PARTITIONS to APPEND, and do not use APPEND when the windows of consecutive runs overlap. A reliable system must guarantee that processing a partition again does not duplicate its rows. APPEND cannot guarantee it, since it adds the rows of every run whether or not they are already in the table. OVERWRITE_PARTITIONS adds the new rows like APPEND does, but it first deletes the rows of the window. Each run then writes them again with the late arrivals included and a rerun over the same window duplicates nothing. See overwrite a window of partitions for how the strategy works.
Read by arrival time and keep by creation time
In Bauplan, partition the table by the time the data was created, not by the time it was received, because its consumers care about when an event happened. Conversely, read the input by arrival time, and keep only the rows created inside the window.
The following model runs every hour on raw events organized by the hour they were received, and writes them to a table partitioned by the hour they were created. Each run reads a window that covers the current hour and the two before it.
from typing import Annotated
import bauplan
import pyarrow as pa
class Event(bauplan.TableSchema):
"""An event, with the time it was created on the client and the time it was received by the server."""
event_id: bauplan.String
user_id: bauplan.String
created_at: bauplan.TimestampMicro
received_at: bauplan.TimestampMicro
@bauplan.model(
materialization_strategy="OVERWRITE_PARTITIONS",
# Consumers query events by the time they happened, not by the time they reached the server
partitioned_by=["hour(created_at)"],
overwrite_filter="created_at >= $window_start AND created_at < $window_end",
)
@bauplan.python("3.13", pip={"polars": "2.2.0"})
def events(
raw_events: Annotated[
pa.Table,
bauplan.Model(
"raw.events",
projection_schema=Event,
# received_at selects the hours of the lookback window, created_at keeps the output inside overwrite_filter
filter=(
"received_at >= $window_start AND received_at < $window_end "
"AND created_at >= $window_start AND created_at < $window_end"
),
),
],
) -> Annotated[pa.Table, Event]:
"""Deduplicate the events of the window, which clients can send more than once."""
import polars as pl
...
The filtering condition on created_at is necessary because partitions read by a run may contain late arrivals (or even future events in some cases). Besides removing spurious events, the condition keeps the output inside overwrite_filter.
Suppose you use a 2-hour lookback window with hourly partitions. The 9 AM run then overwrites the partitions for 7, 8, and 9 AM. If that run also kept an event from 6 AM, it would write the event into a partition it does not overwrite, so the row would be added next to the copy already there, and each later run that reads the same event would add one more.
Pass the window from the orchestrator
The orchestrator computes the window from the scheduled time of the run, as a half-open interval aligned to the hour, and passes it as run parameters, declared with a default in the project file as described in how to parametrize a pipeline. Schedule the run for an hour once that hour is over, for instance the run for 9 AM data at 10:10 AM, to guarantee that the run reads the events received up to the end of the hour.
from datetime import timedelta
# How many hours before the current one each run processes again
LOOKBACK = timedelta(hours=2)
# scheduled_time is the hour the run processes, in UTC
current_hour = scheduled_time.replace(minute=0, second=0, microsecond=0)
state = client.run(
project_dir="pipelines/events",
ref=branch,
parameters={
"window_start": (current_hour - LOOKBACK).isoformat(),
"window_end": (current_hour + timedelta(hours=1)).isoformat(),
},
detach=True,
)
Computing the window from the scheduled time, and not from the time the run starts, keeps it the same when a run is delayed or retried.
Check what each run writes
Take the hourly pipeline above, with a lookback of two hours, and call each run by the hour it processes. Each run reads three hourly partitions of the raw files, and overwrites the same three hours of creation in the table.
| Run | Raw partitions read | Hours of creation overwritten |
|---|---|---|
| 9 AM | h=07, h=08, h=09 | 7 AM, 8 AM, 9 AM |
| 10 AM | h=08, h=09, h=10 | 8 AM, 9 AM, 10 AM |
| 11 AM | h=09, h=10, h=11 | 9 AM, 10 AM, 11 AM |
Every hour of creation is therefore overwritten by three consecutive runs, each time with the events that have arrived so far.
The 9 AM run is the last one that overwrites the 7 AM hour of the table. From then on, that hour holds every event created at 7 AM and received by 10 AM, which amounts to up to two hours of late arrivals. An event created at 7 AM and received at 10 AM falls outside the window of the 10 AM run, which starts at 8 AM, and is dropped. This is the loss accepted with a lookback of two hours.