id: table-diff-checksum-bisection
namespace: company.team
inputs:
- id: bucket_count
type: INT
displayName: Key buckets
description: The key space is split into this many buckets. Only buckets whose
checksums differ are compared row by row.
defaults: 64
- id: inject_drift
type: BOOL
displayName: Inject drift
description: Make the target differ the way a broken migration does (a few
missing rows, an extra row, a rounded amount, a lost timezone). Set it to
false to see a clean cutover.
defaults: true
- id: max_diff_rows
type: INT
displayName: Allowed differing rows
description: Cutover is approved only when the number of differing rows is at most this.
defaults: 0
variables:
report_path: migration/orders-diff-report.md
tasks:
- id: diff
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
Build a source table and a migrated copy, checksum both per key bucket,
keep only the buckets whose checksums differ, then diff those buckets row
by row and name the changed columns. Writes the row-level differences as
CSV.
outputFiles:
- differences
fetchType: FETCH_ONE
sql: |
SELECT setseed(0.31);
-- Source of truth. In real use, ATTACH the old and the new database instead.
CREATE TABLE source AS
SELECT i AS order_id,
1 + CAST(floor(random() * 20000) AS INTEGER) AS customer_id,
round(5 + random() * 495, 2) AS amount,
(['NEW', 'PAID', 'SHIPPED', 'REFUNDED'])[1 + CAST(floor(random() * 4) AS INTEGER)] AS status,
TIMESTAMPTZ '2026-01-01 00:00:00+00' + to_minutes(CAST(floor(random() * 380000) AS BIGINT)) AS created_at
FROM range(1, 200001) t(i);
CREATE TABLE target AS SELECT * FROM source;
{% if inputs.inject_drift %}
DELETE FROM target WHERE order_id IN (40011, 40012, 151009);
INSERT INTO target VALUES (200007, 17, 12.50, 'NEW', TIMESTAMPTZ '2026-09-30 10:00:00+00');
UPDATE target SET amount = round(amount, 0) WHERE order_id = 87654;
UPDATE target SET created_at = created_at + INTERVAL 2 HOUR WHERE order_id = 123456;
{% endif %}
-- Stage 1: one checksum per bucket on each side. A row's hash covers every column.
CREATE TABLE bucket_sums AS
WITH s AS (
SELECT order_id % {{ inputs.bucket_count }} AS bucket, count(*) AS n, bit_xor(hash(order_id, customer_id, amount, status, created_at)) AS h FROM source GROUP BY 1
), t AS (
SELECT order_id % {{ inputs.bucket_count }} AS bucket, count(*) AS n, bit_xor(hash(order_id, customer_id, amount, status, created_at)) AS h FROM target GROUP BY 1
)
SELECT coalesce(s.bucket, t.bucket) AS bucket, s.n AS source_rows, t.n AS target_rows,
s.h IS DISTINCT FROM t.h OR s.n IS DISTINCT FROM t.n AS differs
FROM s FULL OUTER JOIN t USING (bucket);
-- Stage 2: row-level diff, restricted to the buckets that differ.
CREATE TABLE differences AS
WITH s AS (SELECT * FROM source WHERE order_id % {{ inputs.bucket_count }} IN (SELECT bucket FROM bucket_sums WHERE differs)),
t AS (SELECT * FROM target WHERE order_id % {{ inputs.bucket_count }} IN (SELECT bucket FROM bucket_sums WHERE differs))
SELECT coalesce(s.order_id, t.order_id) AS order_id,
CASE WHEN t.order_id IS NULL THEN 'MISSING_IN_TARGET'
WHEN s.order_id IS NULL THEN 'EXTRA_IN_TARGET'
ELSE 'CHANGED' END AS kind,
concat_ws(', ',
CASE WHEN s.customer_id IS DISTINCT FROM t.customer_id AND s.order_id = t.order_id THEN 'customer_id' END,
CASE WHEN s.amount IS DISTINCT FROM t.amount AND s.order_id = t.order_id THEN 'amount ' || s.amount || ' -> ' || t.amount END,
CASE WHEN s.status IS DISTINCT FROM t.status AND s.order_id = t.order_id THEN 'status ' || s.status || ' -> ' || t.status END,
CASE WHEN s.created_at IS DISTINCT FROM t.created_at AND s.order_id = t.order_id THEN 'created_at ' || s.created_at || ' -> ' || t.created_at END
) AS changed_columns
FROM s FULL OUTER JOIN t USING (order_id)
WHERE t.order_id IS NULL OR s.order_id IS NULL
OR hash(s.order_id, s.customer_id, s.amount, s.status, s.created_at) <> hash(t.order_id, t.customer_id, t.amount, t.status, t.created_at)
ORDER BY order_id;
COPY differences TO '{{ outputFiles.differences }}' (HEADER, DELIMITER ',');
SELECT
(SELECT count(*) FROM source) AS source_rows,
(SELECT count(*) FROM target) AS target_rows,
(SELECT count(*) FROM bucket_sums) AS buckets,
(SELECT count(*) FROM bucket_sums WHERE differs) AS differing_buckets,
(SELECT coalesce(sum(source_rows), 0) FROM bucket_sums WHERE differs) AS rows_compared,
(SELECT count(*) FROM differences) AS differing_rows,
(SELECT count(*) FROM differences WHERE kind = 'MISSING_IN_TARGET') AS missing,
(SELECT count(*) FROM differences WHERE kind = 'EXTRA_IN_TARGET') AS extra,
(SELECT count(*) FROM differences WHERE kind = 'CHANGED') AS changed,
(SELECT coalesce(string_agg(order_id || ' ' || kind || CASE WHEN changed_columns <> '' THEN ' (' || changed_columns || ')' ELSE '' END, '; '), '') FROM (SELECT * FROM differences LIMIT 10)) AS sample;
- id: result
type: io.kestra.plugin.core.output.OutputValues
values:
row: "{{ (outputs.diff.outputs | last).row | toJson }}"
- id: log_result
type: io.kestra.plugin.core.log.Log
message: >-
Source {{ outputs.result.values.row | jq('.source_rows') | first }} rows,
target {{ outputs.result.values.row | jq('.target_rows') | first }}. {{
outputs.result.values.row | jq('.differing_buckets') | first }} of {{
outputs.result.values.row | jq('.buckets') | first }} buckets differ, so
{{ outputs.result.values.row | jq('.rows_compared') | first }} rows were
compared row by row. Differences: {{ outputs.result.values.row |
jq('.missing') | first }} missing, {{ outputs.result.values.row |
jq('.extra') | first }} extra, {{ outputs.result.values.row |
jq('.changed') | first }} changed. {{ outputs.result.values.row |
jq('.sample') | first }}
- id: cutover_gate
type: io.kestra.plugin.core.flow.If
description: Approve the cutover only when the copy matches the source within
the allowed number of rows.
condition: "{{ (outputs.result.values.row | jq('.differing_rows') | first) <=
inputs.max_diff_rows }}"
then:
- id: record_approval
type: io.kestra.plugin.core.kv.Set
key: orders_migration_cutover
kvType: JSON
value: |
{"approved": true, "rows": {{ outputs.result.values.row | jq('.target_rows') | first }}, "differing_rows": {{ outputs.result.values.row | jq('.differing_rows') | first }}, "execution_id": "{{ execution.id }}"}
else:
- id: publish_differences
type: io.kestra.plugin.core.namespace.UploadFiles
description: Every differing row, with the kind of difference and the changed
columns, for the migration team to fix.
namespace: "{{ flow.namespace }}"
filesMap:
migration/orders-differences.csv: "{{ outputs.diff.outputFiles.differences }}"
- id: block_cutover
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Cutover blocked: {{ outputs.result.values.row |
jq('.differing_rows') | first }} differing rows (max {{
inputs.max_diff_rows }}). See migration/orders-differences.csv. First
differences: {{ outputs.result.values.row | jq('.sample') | first }}"
triggers:
- id: hourly_during_migration
type: io.kestra.plugin.core.trigger.Schedule
disabled: true
description: Re-check every hour while both systems run in parallel. Disable it
after the cutover.
cron: "15 * * * *"