Moving to a new Hadoop cluster gave us an accidental test of our incremental jobs. On the old cluster, many derived tables had been built up over months, a few days at a time. On the new one, we often backfilled a whole table in a single run. If a transformation is correct, those two ways of building a table give the same result. For one table they didn’t.
Comparing the two clusters
Trino on the new cluster could read tables from both clusters, so comparing them was one query: row counts per day from each side, joined on the date.
WITH old_cluster AS (
SELECT dateymd, count(*) AS old_rows
FROM old.analytics.source_events
GROUP BY dateymd
),
new_cluster AS (
SELECT dateymd, count(*) AS new_rows
FROM new.analytics.source_events
GROUP BY dateymd
)
SELECT o.dateymd, old_rows, new_rows, old_rows - new_rows AS diff
FROM old_cluster o
LEFT JOIN new_cluster n ON o.dateymd = n.dateymd
ORDER BY o.dateymd;
For a table that had recently been converted to an incremental transformation, diff wasn’t zero: the incrementally built table and the backfilled one disagreed on many days.
Filtering at the end
An incremental run is given a date range, 14 days in this case, and overwrites only the partitions in that range. This transformation read the entire source table, computed its result, and applied the date range only at the very end, to decide which rows to write. The pattern looked something like this (simplified, not the actual query):
INSERT OVERWRITE TABLE item_status PARTITION (dateymd)
SELECT user_id, item_id, status, dateymd
FROM (
SELECT *,
row_number() OVER (PARTITION BY user_id, item_id ORDER BY ts DESC) AS rn
FROM source_events -- the whole source table
) latest
WHERE rn = 1
AND dateymd BETWEEN ${start} AND ${end}; -- the range, applied last
The result for a given day depends on when you run it. A run on January 10 picks the latest event per user and item as of January 10 and writes it into that day’s partition. A backfill run in December sees later events too, picks one of those instead, and writes nothing for January 10. Each run is correct about the data it saw, and the partitions written earlier are never revisited, so the incremental table ends up as a mix of answers computed at different times.
The fix depends on what the table is meant to contain. Either each day’s partition should only depend on that day’s data, in which case the date range belongs in the query that reads the source, or the table describes current state, in which case it shouldn’t be partitioned by day and incremental at all. Here the transformation’s logic was corrected and tests were added.
The rule
An incremental transformation has to produce the same table as a full rebuild from the same source data. If it doesn’t, the table depends on the history of how it was built, and nobody can reproduce it.
That makes for a cheap test, and you don’t need a cluster migration to run it. Build a small table twice, once in a single run and once in several smaller ranges, and compare the two. Anything that reads outside the run’s range, like window functions over the whole source, “latest value” lookups, or first-seen dates, is where to look first when they differ.
Leave a Reply