Metadata-Version: 2.5
Name: rowdelta
Version: 0.2.0
Summary: Diff two tables (CSV, Parquet, JSON, SQLite, DuckDB or git revisions) by key: added, removed and changed rows, column by column. Fast, because it runs on DuckDB.
Project-URL: Homepage, https://github.com/Atharv1414/rowdelta
Project-URL: Issues, https://github.com/Atharv1414/rowdelta/issues
Project-URL: Changelog, https://github.com/Atharv1414/rowdelta/blob/main/CHANGELOG.md
Author: Atharv Patil
License-Expression: MIT
License-File: LICENSE
Keywords: cli,csv,data-diff,data-engineering,data-quality,diff,duckdb,github-action,parquet,testing
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Utilities
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: duckdb>=1.1
Requires-Dist: rich>=13.7
Provides-Extra: dev
Requires-Dist: hypothesis>=6.100; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pandas>=2.0; extra == 'dev'
Requires-Dist: polars>=1.0; extra == 'dev'
Requires-Dist: pyarrow>=15; extra == 'dev'
Requires-Dist: pytest-cov>=5; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: zstandard>=0.22; extra == 'dev'
Description-Content-Type: text/markdown

# rowdelta

**See exactly what changed between two versions of a table.**

rowdelta matches rows by key and tells you which rows were added, removed and changed, and which columns
changed in each one. It reads CSV, Parquet, JSON, SQLite, DuckDB, pandas and Arrow, or a file at a git
revision, and it runs on [DuckDB](https://duckdb.org), so ten million rows take seconds.

[![PyPI](https://img.shields.io/pypi/v/rowdelta?color=2337f0)](https://pypi.org/project/rowdelta/)
[![ci](https://github.com/Atharv1414/rowdelta/actions/workflows/ci.yml/badge.svg)](https://github.com/Atharv1414/rowdelta/actions/workflows/ci.yml)
![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-3776AB?logo=python&logoColor=white)
![DuckDB](https://img.shields.io/badge/engine-DuckDB-FFF000?logo=duckdb&logoColor=black)
[![License: MIT](https://img.shields.io/badge/license-MIT-green)](https://github.com/Atharv1414/rowdelta/blob/main/LICENSE)

![rowdelta comparing two customer exports: 2 added, 1 removed and 3 changed rows, a new column, and the before and after values](https://raw.githubusercontent.com/Atharv1414/rowdelta/main/docs/terminal.svg)

```bash
rowdelta customers_2026-10.csv customers_2026-11.csv --key customer_id
```

## Why

Tables change all the time: a nightly export, a dbt model after a refactor, a pipeline after an upgrade,
a spreadsheet someone "fixed". `git diff` on a CSV compares lines, not records, so it falls apart as soon
as rows are reordered, a column moves, or `29` becomes `29.0`. Most teams end up writing the same
one-off pandas script each time.

rowdelta is that script done properly, once:

- **Matches rows by key**, including composite keys, so order never matters.
- **Shows the change itself**: for each changed row, which columns changed and their old and new values.
- **Is safe with real data**: NULLs compare correctly, duplicate and empty keys are reported instead of
  silently mismatched, and columns match even when their order or capitalisation differs.
- **Fits into CI**: exit code `1` when something differs, Markdown output for pull request comments,
  JSON for scripts, and a ready-made GitHub Action.
- **Is fast**: one DuckDB join does the work. See [Performance](#performance).

## Install

```bash
pipx install rowdelta
```

or `pip install rowdelta`. It needs Python 3.10 or newer and installs two dependencies: `duckdb` and
`rich`.

## Quick start

```bash
# Two files, matched on a key
rowdelta before.csv after.csv --key id

# Any mix of formats, and composite keys
rowdelta orders_old.parquet orders.csv --key order_id,line_no

# What changed in a file since the last commit, or against main
rowdelta HEAD:data/prices.csv data/prices.csv --key sku
rowdelta origin/main:data/prices.csv data/prices.csv --key sku

# A table in SQLite or DuckDB
rowdelta prod.sqlite::users staging.sqlite::users --key id

# Ignore noise: timestamps, tiny float differences, case and spaces
rowdelta a.parquet b.parquet --key id --ignore updated_at --atol 0.005 --trim --ignore-case

# Only some rows, only some columns
rowdelta a.csv b.csv --key id --where "country = 'GB'" --columns price,stock

# Reports and the full list of changes
rowdelta a.csv b.csv --key id -o report.html -o report.json
rowdelta a.csv b.csv --key id --export changes.parquet
```

`--export` writes every added, removed and changed row with a `status` column, a `changed_columns` column,
and `<column>_before` / `<column>_after` pairs, so you can keep investigating in SQL, pandas or a
spreadsheet.

No key? rowdelta suggests columns that look like one. Or use `--keyless` to compare whole rows as a
multiset (it then reports added and removed copies, but cannot say which rows "changed").

## Sources

| Source | Example |
|---|---|
| CSV, TSV (also `.gz`, `.zst`) | `data.csv`, `data.tsv.gz` |
| Parquet, one file or a glob | `data.parquet`, `"lake/2026/*.parquet"` |
| JSON array or JSON Lines | `data.json`, `data.jsonl` |
| SQLite table | `app.sqlite::users` (`.sqlite`, `.sqlite3`, `.db`) |
| DuckDB table | `warehouse.duckdb::orders` |
| A file at a git revision | `main:data/users.csv`, `HEAD~3:data/users.csv` |
| pandas, polars, Arrow (Python API) | `rowdelta.compare(df_old, df_new, key="id")` |

The two sides can be different formats. A column that is text on one side and numeric on the other is
compared as text (so `01234` vs `1234` is a change); integers and floats are compared as numbers.
Differences only in width, such as `INTEGER` vs `BIGINT`, are not reported as schema changes.

## In CI: the GitHub Action

Post a diff on every pull request that changes a data file:

```yaml
name: data diff
on:
  pull_request:
    paths: ["data/**"]

jobs:
  diff:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0 # so the base branch is available to compare against

      - uses: Atharv1414/rowdelta@v0
        with:
          before: origin/${{ github.base_ref }}:data/customers.csv
          after: data/customers.csv
          key: customer_id
          args: --ignore updated_at
          comment: true # post the diff on the pull request; later pushes update the same comment
          # fail-on: removed   # make the check fail if any rows were removed
```

With `comment: true` the Action posts one comment per table and edits it on every push, so the pull
request never fills up with old reports. Diffing several tables in one workflow? Give each step its own
`comment-id`. Pull requests from forks get a read-only token, so there the Action warns and carries on
instead of failing.

The report also goes to the job summary. Outputs: `has-differences`, `added`, `removed`, `changed`,
`report` (Markdown file), `json` (JSON file) and `exit-code`. Set `fail-on` to `any`, `rows`, `schema`, `added`,
`removed` or `changed` to turn the step into a gate; it defaults to `none`, which only reports.

Without the Action, any CI system can use the exit code:

```bash
rowdelta expected.parquet actual.parquet --key id --quiet   # 0 = same, 1 = different, 2 = error
```

## Python API

```python
import rowdelta

with rowdelta.compare("old.parquet", "new.parquet", key=["order_id", "line"], atol=0.01) as diff:
    print(diff.added, diff.removed, diff.changed, diff.unchanged)
    print(diff.column_changes)  # {'price': 812, 'status': 40}
    print(diff.schema.only_in_b)  # new columns
    for row in diff.rows("changed", limit=5):
        print(row)  # {'status': 'changed', 'order_id': 7, 'price_before': 9.5, ...}
    diff.export("changes.parquet")

    report = diff.result.to_dict()  # plain data, safe to keep after the connection closes
```

`compare` also accepts pandas or polars DataFrames and Arrow tables.

## In tests: `assert_same`

`assert_same` fails a test with the same readable diff you see in the terminal, instead of
"DataFrames are not equal":

```python
from rowdelta import assert_same


def test_refactor_keeps_the_output(old_orders, new_orders):
    assert_same(old_orders, new_orders, key="order_id", atol=1e-9, ignore="loaded_at")
```

```text
AssertionError: tables differ

rowdelta expected → actual
key order_id   rows 3 → 3   columns compared 1   0.01s

rows          count   share
───────────────────────────
+ added           0    0.0%
− removed         0    0.0%
~ changed         1   33.3%
= unchanged       2   66.7%

CHANGED COLUMNS
  price  1  ████████████████████████  100.0%

CHANGED ROWS (1 OF 1)
  key          column   before   after
  ────────────────────────────────────
  order_id=2   price    20.0     21.0

✗ Differences in 1 row.
```

It takes every `compare` option, plus `fail_on` to choose what counts (for example
`fail_on="removed,changed"` allows new rows). It works with pytest, unittest or plain `assert` code, and
the raised `TablesDiffer` error carries the full result as `.result`.

## Options

| Option | What it does |
|---|---|
| `-k, --key COL` | Column(s) that identify a row. Repeat or comma-separate for a composite key. |
| `--keyless` | Compare whole rows as a multiset instead of by key. |
| `-c, --columns COL` | Compare only these columns. |
| `-i, --ignore COL` | Leave these columns out. |
| `--atol N`, `--rtol N` | Numbers within `atol + rtol × abs(after)` count as equal. |
| `--trim`, `--ignore-case` | Ignore spaces at the ends of text, or differences in case. |
| `--where SQL` | Filter both sides first, e.g. `"region = 'EU'"`. |
| `-o, --output FILE` | Write a report: `.md`, `.json`, `.html`, anything else is text. Repeatable. |
| `-f, --format` | `terminal`, `markdown`, `json` or `html` for the screen (or a single `-o`). |
| `--export FILE` | Every changed row with before and after values, as `.parquet`, `.csv` or `.json`. |
| `-n, --sample N` | Example rows per kind in reports (default 10). |
| `--fail-on WHAT` | What sets exit code 1: `any` (default), `rows`, `schema`, `added`, `removed`, `changed`, `none`. |
| `--labels A B` | Names to show instead of the paths. |
| `--title TEXT` | Heading for Markdown and HTML reports. |
| `--memory-limit 2GB` | Cap DuckDB's memory; larger work spills to disk. |
| `-q, --quiet` | Print nothing; files are still written. |

## Performance

Measured with [`benchmarks/bench.py`](https://github.com/Atharv1414/rowdelta/blob/main/benchmarks/bench.py) on a MacBook Air (Apple M2, 8 GB RAM), DuckDB
2.0.1, Python 3.13. Table B is table A with 1% of rows changed, 0.5% removed and 0.5% added; 8 columns of
mixed types. Times include reading both files and computing counts, per-column totals and samples.

| Rows | Format | Added | Removed | Changed | Time |
|---:|---|---:|---:|---:|---:|
| 1,000,000 | Parquet | 5,000 | 5,000 | 10,000 | 0.3 s |
| 1,000,000 | CSV | 5,000 | 5,000 | 10,000 | 0.6 s |
| 10,000,000 | Parquet | 50,000 | 50,000 | 100,000 | 7.3 s |

Run `python benchmarks/bench.py` to measure on your machine.

## How it works

1. Each side is loaded into DuckDB (filtered by `--where`), and the two schemas are compared.
2. Rows with an empty key, and keys that appear more than once on either side, are counted and set aside,
   because they cannot be matched one to one. They are reported, never guessed.
3. A single `FULL OUTER JOIN` on the key gives each key a status (added, removed, changed or same), with
   one NULL-safe "differs" flag per compared column that applies the tolerance and text options.
4. Totals and per-column counts come from that one table; samples and `--export` join back for values.

The engine is about 600 lines in [`src/rowdelta/engine.py`](https://github.com/Atharv1414/rowdelta/blob/main/src/rowdelta/engine.py). Tests compare it
against a plain-Python reference diff on hundreds of random tables on every run, using
[Hypothesis](https://hypothesis.readthedocs.io).

## Limits

- Both tables are loaded into DuckDB. Large inputs spill to disk, but very large ones are better compared
  where they live; set `--memory-limit` to keep a laptop responsive.
- Comparing tables directly inside Postgres, MySQL or a cloud warehouse is not built in yet. Export to
  Parquet first (or use DuckDB's database extensions with the Python API).
- Excel files are not read directly; save as CSV.
- Floats compare exactly unless you give `--atol` or `--rtol`.

## Related tools

[datacompy](https://github.com/capitalone/datacompy) is a Python library for comparing DataFrames.
Datafold's [data-diff](https://github.com/datafold/data-diff), which compared tables across databases, was
archived in May 2024. rowdelta aims at the everyday case: two files or tables, one command, a clear answer,
and a report you can paste into a pull request.

## Contributing

Issues and pull requests are welcome; see [CONTRIBUTING.md](https://github.com/Atharv1414/rowdelta/blob/main/CONTRIBUTING.md). Good first areas: more
sources (Excel, Postgres through DuckDB extensions), report polish, and Windows testing.

## License

MIT
