Handling late-arriving data in dbt - Time & Space Complexity
When working with data pipelines, late-arriving data can affect how long processing takes.
We want to know how the time to handle this data grows as more late data arrives.
Analyze the time complexity of the following dbt code snippet.
-- Model to merge late-arriving data
with new_data as (
select * from source_table where event_date >= dateadd(day, -7, current_date)
),
merged as (
select * from target_table
union all
select * from new_data
)
select * from merged
where event_date <= current_date
This code merges recent late-arriving data with existing data for processing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning and combining rows from both existing and late-arriving data tables.
- How many times: Each row in both tables is read once during the union operation.
As the amount of late-arriving data grows, the total rows to process increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 (existing) + 10 (late) = 20 rows processed |
| 100 | About 100 + 100 = 200 rows processed |
| 1000 | About 1000 + 1000 = 2000 rows processed |
Pattern observation: The work grows roughly in direct proportion to the total rows combined.
Time Complexity: O(n)
This means the time to handle late-arriving data grows linearly with the total number of rows processed.
[X] Wrong: "Handling late-arriving data only adds a fixed small cost regardless of data size."
[OK] Correct: Because the system must read and combine all late data rows, the cost grows with how much late data arrives.
Understanding how data volume affects processing time helps you design efficient pipelines and explain your choices clearly.
"What if we indexed the event_date column to speed up filtering? How would the time complexity change?"
Practice
late-arriving data mean in data processing?Solution
Step 1: Understand the timing of data arrival
Late-arriving data refers to data that comes in after the time it was expected to be available for processing.Step 2: Differentiate from other data issues
It is not about corruption, duplication, or source system differences, but purely about timing.Final Answer:
Data that arrives after the expected time window -> Option BQuick Check:
Late-arriving data = late arrival [OK]
- Confusing late data with corrupted data
- Thinking late data means duplicate records
- Assuming late data is from a different system
Solution
Step 1: Understand filtering late data by date
Late-arriving data is usually older than the current date minus a buffer (like 1 day), so we filter for dates less than that.Step 2: Check each option's logic
WHERE event_date < current_date - interval '1 day' filters for event_date before yesterday, which captures late data. WHERE event_date > current_date + interval '1 day' filters future dates, which is incorrect. WHERE event_date = current_date filters only today's data, ignoring late data. WHERE event_date IS NULL filters nulls, unrelated to late data.Final Answer:
WHERE event_date < current_date - interval '1 day' -> Option AQuick Check:
Filter dates before yesterday = WHERE event_date < current_date - interval '1 day' [OK]
- Filtering future dates instead of past dates
- Filtering only current date data
- Filtering null dates instead of late data
SELECT
id,
event_date,
data_value
FROM source_table
WHERE event_date >= (SELECT MAX(event_date) FROM {{ this }})What is the main purpose of this WHERE clause?
Solution
Step 1: Analyze the WHERE clause logic
The clause filters rows where event_date is greater or equal to the max event_date already in the target table, meaning it picks new or updated data since last run.Step 2: Understand incremental model behavior
This is a common pattern in dbt incremental models to avoid reprocessing old data and only add new or changed rows.Final Answer:
To select only new or updated data since last run -> Option AQuick Check:
Incremental filter = new or updated data [OK]
- Thinking it excludes late-arriving data
- Assuming it selects duplicates
- Confusing null filtering with this clause
WHERE event_date > (SELECT MAX(event_date) FROM {{ this }})But your model misses some late-arriving data. What is the likely problem?
Solution
Step 1: Understand the difference between > and >=
Using > excludes rows where event_date equals the max date already processed, which can miss late-arriving data with that date.Step 2: Correct the filter to include equal dates
Changing to >= includes those rows, capturing late-arriving data that arrived late but has the same event_date.Final Answer:
Using > instead of >= excludes data with event_date equal to max date -> Option CQuick Check:
Use >= to include late data on max date [OK]
- Using > excludes late data with max date
- Thinking subquery is invalid in dbt
- Filtering with < instead of >
- Assuming late data has null dates
Solution
Step 1: Understand the goal of including late-arriving data
Late data can have event_dates already processed, so we must update or insert without duplicates.Step 2: Use merge strategy with unique keys and proper filtering
Using a merge (upsert) with unique keys and filtering event_date >= max date ensures late data is added or updated correctly.Step 3: Evaluate other options
Deleting all data every run is costly. Ignoring late data loses accuracy. Filtering only current_date misses late data.Final Answer:
Use a merge strategy with unique keys and filter event_date >= max date -> Option DQuick Check:
Merge + unique keys + >= max date = best late data handling [OK]
- Reloading full data every run is inefficient
- Ignoring late data causes incomplete analysis
- Filtering only current date misses late data
Ready to prove it?
Pass the test and this pattern moves unknown
