diffscope dbt pull request review

PR #1 · jaffle_demo/orders · order 103 · amount_cents

One order. One wrong unit.

diffscope caught it before merge.

Before 760 cents After

After, in cents 76,000 ×100
amount_cents8 orders
  1. 101 complete → completed · 850 cents
  2. 102 = · placed · 625 cents
  3. 103 760 → 76,000 · completed
  4. 104 = · completed · 490 cents
  5. 105 = · completed · 1150 cents
  6. 106 = · returned · 400 cents
  7. 107 = · completed · 550 cents
  8. 108 = · placed · 700 cents
Exact values from the synthetic Jaffle Shop demo. Order 103

Reading the PR comment

jaffle_demo #1

The row counts hold

Both models kept the same keys. The comment still found changes within matching rows.

Rows by model
ModelBeforeAfterChanged
orders882
customers441

The intended rename

On order 101, the change to status adds a single letter.

Order 101 · status
Before
complete
After
completed

The currency bug

Order 103 was already in cents. Its amount grew by a factor of 100.

Order 103 · amount_cents
Before
760
After
76,000

The customer total follows

Order 103 belongs to customer 2. The higher order amount carries into the customers model.

Customer 2 · total_amount_cents
Order 103 76,000 Customer 2
Before
1,250
After
76,490

Across all orders

The sum of amount_cents rose 1,361.8%. A single order moved both the mean and the upper end of the distribution.

All orders · amount_cents
StatisticBeforeAfter
Sum5,525.0 ×14.680,765.0
Mean690.62 ×14.610,095.62
P951,045.0 ×47.749,802.5

The PR comment

Both models kept the same row count. The comment shows which values changed.

Read the full comment on GitHub
The orders section of the diffscope comment on GitHub PR #1, including row counts, numeric shifts, and the order 103 amount change.
diffscope Jaffle Shop · comment excerpt

orders

Rows
ComparisonCount
Before8
After8
Added0
Removed0
Changed2
Unchanged6
Most changed columns
ColumnChanged rows
status1
amount_cents1
Numeric shifts amount_cents
StatisticBeforeAfter
Mean690.62 ×14.610,095.62
P951,045.0 ×47.749,802.5
Sum5,525.0 ×14.680,765.0
Example rows
order_id / columnBeforeAfter
101 statuscompletecompleted
103 amount_cents760 ×10076,000

customers

Rows
ComparisonCount
Before4
After4
Added0
Removed0
Changed1
Unchanged3
Most changed columns
ColumnChanged rows
total_amount_cents1
Numeric shifts total_amount_cents
StatisticBeforeAfter
Mean1,381.25 ×14.620,191.25
Median1,637.5 ×1.32,137.5
P952,216.25 ×29.565,354.0
Sum5,525.0 ×14.680,765.0
Example rows
customer_id / columnBeforeAfter
2 total_amount_cents1,250 ×61.276,490

Order 101 shows the intended status rename. The currency error on order 103 raised customer 2’s total. Across orders, the sum of amount_cents rose 1,361.8%.

How it works

Baseline → join on key → compare → comment baselinejoin on keycomparecomment

DuckDB checks that the key identifies each row uniquely in both versions. A full outer join on the key finds rows that appear on just one side and marks them as added or removed.

For matching keys, two NULLs count as equal. A NULL paired with a value counts as a change. For numeric columns of the same type, diffscope tables --tolerance treats absolute differences up to the threshold as equal. The default is 0.

The diff lists added, removed and retyped columns. It compares retyped columns as text. Numeric summaries cover each full table, including rows that were added or removed.

DuckDB SQL handles the checks, join, counts and summaries. Python runs the queries and formats the result for the pull request.

Benchmark

Benchmark results
Time to compare two tables
Rows per sideChanged rowsWall time (s)
100,0001,000 0.023≈4,347,826 rows/s
1,000,00010,000 0.071≈14,084,507 rows/s
10,000,000100,000 0.541≈18,484,288 rows/s
Each dot represents 10,000 rows. Each dot represents 100,000 rows. DuckDB 2.0.1 on Darwin arm64 with a 4 GB memory limit. Each time is a single wall-clock observation and excludes table creation. A confidence interval requires repeated runs.

Tests with known answers

Each scenario starts with 1,000 synthetic rows. The expected counts and aggregates are exact properties of those tables.

Fixtures and verification plan
ScenarioExpected resultExpectation
identical1,000 unchanged rowsEqual values, including two NULLs, stay unchanged.known
value_changes10 changed rows, all in amountChanges are counted at the row and column level.known
additions_removals20 added, 10 removed, 0 changedRows are matched by key, and the sum reflects rows added or removed.known
null_flips20 changed, with 10 flips in each directionNULL to value and value to NULL are counted separately.known
retyped_columnamount changes from INTEGER to VARCHAR, with 0 changed rowsA type change is reported even when the text values match.known
renamed_columnmemo added, note removed, 0 changed rowsA rename appears as an added column and a removed column.known
fanout_join10 duplicate keys, with 3 examples in the errorA join fanout fails the uniqueness check before comparison.known
currency_unit10 changed rows, sum rises from 120,000 to 219,000, mean rises from 120 to 219A unit error shifts the sum and mean even when the median and P95 stay fixed.known

Quick start

Run these commands from the repository root with Python 3.10 or newer.

Repository instructions
diffscope / shell

Install diffscope

python -m pip install .

Compare tables or files

diffscope tables baseline.orders candidate.orders --db shop.duckdb --key order_id
diffscope tables before.csv after.parquet --key order_id

Repeat --key for a composite key. Use --format json for machine-readable output.

Compare built dbt models

python -m pip install 'dbt-duckdb>=1.11,<1.12' 'dbt-core>=1.11,<1.12'
bash examples/jaffle_demo/build.sh
diffscope dbt --project-dir examples/jaffle_demo --models orders customers \
  --base-schema baseline --target-schema candidate

The command reads meta.diffscope_keys from the manifest. Override it with --key, or choose the DuckDB file with --db.

Post the comparison on a pull request

Grant comment permission in the pull_request workflow and group runs by pull request.

permissions:
  contents: read
  pull-requests: write
concurrency:
  group: diffscope-${{ github.event.pull_request.number }}
  cancel-in-progress: true

After checkout, Python setup and the build of both schemas, add the comparison step.

- uses: Ghostboy789/diffscope@main
  with:
    project-dir: examples/jaffle_demo
    models: orders customers
    base-schema: baseline
    target-schema: candidate

The action uses gh to create or update one comment. The demo skips this step on fork pull requests, where the token is read-only. Demo workflow

Limits

  • The dbt integration supports the DuckDB adapter only.
  • The key or combination of keys must be unique in each version.
  • Both versions of each dbt model must be built before comparison.
  • Cross-database diffs are outside the current scope.

Datafold archived its open-source table comparison tool in May 2024. datafold/data-diff archive