diffdb-core 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. {diffdb_core-0.1.0/src/diffdb_core.egg-info → diffdb_core-0.1.2}/PKG-INFO +172 -21
  2. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/README.md +171 -20
  3. {diffdb_core-0.1.0 → diffdb_core-0.1.2/diffdb_core.egg-info}/PKG-INFO +172 -21
  4. diffdb_core-0.1.2/diffdb_core.egg-info/SOURCES.txt +64 -0
  5. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/pyproject.toml +11 -2
  6. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/__init__.py +1 -1
  7. diffdb_core-0.1.0/src/diffdb_core.egg-info/SOURCES.txt +0 -64
  8. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/LICENSE.md +0 -0
  9. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/MANIFEST.in +0 -0
  10. {diffdb_core-0.1.0/src → diffdb_core-0.1.2}/diffdb_core.egg-info/dependency_links.txt +0 -0
  11. {diffdb_core-0.1.0/src → diffdb_core-0.1.2}/diffdb_core.egg-info/requires.txt +0 -0
  12. {diffdb_core-0.1.0/src → diffdb_core-0.1.2}/diffdb_core.egg-info/top_level.txt +0 -0
  13. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/setup.cfg +0 -0
  14. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/concurrency/__init__.py +0 -0
  15. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/concurrency/pool.py +0 -0
  16. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/concurrency/stream.py +0 -0
  17. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/core/__init__.py +0 -0
  18. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/core/differ.py +0 -0
  19. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/core/hash_differ.py +0 -0
  20. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/core/segment.py +0 -0
  21. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/core/sql.py +0 -0
  22. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/core/summary.py +0 -0
  23. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/__init__.py +0 -0
  24. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/_connect.py +0 -0
  25. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/base.py +0 -0
  26. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/bigquery.py +0 -0
  27. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/clickhouse.py +0 -0
  28. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/databricks.py +0 -0
  29. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/_base.yaml +0 -0
  30. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/bigquery.yaml +0 -0
  31. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/clickhouse.yaml +0 -0
  32. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/databricks.yaml +0 -0
  33. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/duckdb.yaml +0 -0
  34. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/mysql.yaml +0 -0
  35. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/oracle.yaml +0 -0
  36. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/postgresql.yaml +0 -0
  37. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/presto.yaml +0 -0
  38. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/redshift.yaml +0 -0
  39. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/snowflake.yaml +0 -0
  40. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/trino.yaml +0 -0
  41. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/dialects/vertica.yaml +0 -0
  42. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/duckdb.py +0 -0
  43. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/mysql.py +0 -0
  44. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/oracle.py +0 -0
  45. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/postgresql.py +0 -0
  46. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/presto.py +0 -0
  47. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/redshift.py +0 -0
  48. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/snowflake.py +0 -0
  49. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/trino.py +0 -0
  50. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/vertica.py +0 -0
  51. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/databases/yaml_dialect.py +0 -0
  52. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/geometry/__init__.py +0 -0
  53. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/geometry/arithmetic.py +0 -0
  54. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/geometry/bisection.py +0 -0
  55. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/geometry/vector.py +0 -0
  56. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/telemetry/__init__.py +0 -0
  57. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/telemetry/info_tree.py +0 -0
  58. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/telemetry/query_tracker.py +0 -0
  59. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/types.py +0 -0
  60. {diffdb_core-0.1.0/src/diffdb → diffdb_core-0.1.2/src}/utils.py +0 -0
  61. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/golden/dialect_sql.json +0 -0
  62. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/golden/query_sql.json +0 -0
  63. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/test_connect.py +0 -0
  64. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/test_dialects.py +0 -0
  65. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/test_diffdb_core.py +0 -0
  66. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/test_e2e_duckdb.py +0 -0
  67. {diffdb_core-0.1.0 → diffdb_core-0.1.2}/tests/test_query_snapshot.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: diffdb-core
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Cross-database table diffing by recursive checksum bisection, with declarative YAML SQL dialects
5
5
  Author: Sarath Mohan
6
6
  License: MIT
@@ -61,11 +61,16 @@ engines and a `FULL OUTER JOIN` isn't possible.
61
61
  Databricks, DuckDB, Trino, Presto, ClickHouse, Vertica.
62
62
  - **Any pair**: the source and target can be different engines; each side renders values to the same text before hashing.
63
63
  - **No metadata reads**: you pass the columns, their native types and optional SQL transform rules, so the
64
- engine sends only four read-only query shapes and never touches the catalog.
64
+ engine sends only four read-only query shapes and never touches the catalog. (The one exception: an Oracle
65
+ connection reads `NLS_CHARACTERSET` once when it opens, unless `hash_charset` is given.)
65
66
  - **Non-unique keys** work: bisection columns need only be selective, and duplicate rows are counted.
66
67
  - **Per-key summary**: changed rows with their differing columns, source-only and target-only rows, duplicate keys.
67
68
  - **Telemetry**: every query, row count, checksum and duration can be captured for auditing.
68
69
 
70
+ diffdb-core is the third iteration of this tooling, after `simple-dv-tool` and `ibis-dv-tool`, and is built on
71
+ reladiff's algorithm. See [Background](#background-from-aggregate-fingerprints-to-checksum-bisection) for how it got
72
+ here and [How diffdb-core differs from reladiff](#how-diffdb-core-differs-from-reladiff) for what changed.
73
+
69
74
  ## Install
70
75
 
71
76
  ```bash
@@ -153,22 +158,24 @@ An existing `Database` can be passed to `connect_to_table()` instead of a URI, s
153
158
  4. **Download & compare**: only those small ranges are downloaded and compared in memory.
154
159
  5. **Summarize**: `summarize()` pairs the differing rows by `key_columns` (default: the bisection columns).
155
160
 
156
- For a table bisected on `(ADDRESS_SK, ORACLE_COLUMN_NAME)` the checksum queries look like this. Oracle (database
157
- character set isn't AL32UTF8, so the row is converted first, to hash the same bytes as PostgreSQL; detected from
161
+ For a table bisected on `(ORDER_ID, LINE_CODE)`, with columns `ORDER_ID NUMBER(12,0)` / `bigint`,
162
+ `LINE_CODE VARCHAR2(20)` / `varchar(20)`, `UPDATED_AT TIMESTAMP(6)` / `timestamp(6)` and
163
+ `UPDATED_BY VARCHAR2(30)` / `varchar(30)`, the checksum queries look like this. Oracle (database character set
164
+ isn't AL32UTF8, so the row is converted first, to hash the same bytes as PostgreSQL; detected from
158
165
  `NLS_CHARACTERSET` or forced with a `hash_charset` connection setting):
159
166
 
160
167
  ```sql
161
168
  SELECT
162
169
  count(*),
163
170
  sum(to_number(substr(standard_hash(CONVERT((
164
- NVL(to_char("ADDRESS_SK", 'FM99999999999999999999999999999999999999'), '<null>')
165
- || '|' || NVL("ORACLE_COLUMN_NAME", '<null>')
166
- || '|' || NVL(to_char(cast(("UPDATE_TMSTMP" + INTERVAL '+5' HOUR) as timestamp(6)), 'YYYY-MM-DD HH24:MI:SS.FF6'), '<null>')
167
- || '|' || NVL("UPDATE_USER_ID", '<null>')
171
+ NVL(to_char("ORDER_ID", 'FM99999999999999999999999999999999999999'), '<null>')
172
+ || '|' || NVL("LINE_CODE", '<null>')
173
+ || '|' || NVL(to_char(cast("UPDATED_AT" as timestamp(6)), 'YYYY-MM-DD HH24:MI:SS.FF6'), '<null>')
174
+ || '|' || NVL("UPDATED_BY", '<null>')
168
175
  ), 'AL32UTF8'), 'MD5'), 18), 'xxxxxxxxxxxxxxx'))
169
- FROM "SALES"."ADDRESS_AUDIT"
170
- WHERE ("ADDRESS_SK" >= 1405) AND ("ORACLE_COLUMN_NAME" >= 'active_ind')
171
- AND ("ADDRESS_SK" < 2662) AND ("ORACLE_COLUMN_NAME" < 'dffkhzQ2yoa4QcQl')
176
+ FROM "SALES"."ORDER_LINE"
177
+ WHERE ("ORDER_ID" >= 1405) AND ("LINE_CODE" >= 'A-001')
178
+ AND ("ORDER_ID" < 2662) AND ("LINE_CODE" < 'K-250')
172
179
  ```
173
180
 
174
181
  PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column, see Limitations):
@@ -177,14 +184,14 @@ PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column,
177
184
  SELECT
178
185
  count(*),
179
186
  sum(('x' || substring(md5((
180
- coalesce("address_sk"::varchar, '<null>')
181
- || '|' || coalesce(("oracle_column_name" COLLATE "C"), '<null>')
182
- || '|' || coalesce(to_char("update_tmstmp"::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), '<null>')
183
- || '|' || coalesce("update_user_id", '<null>')
187
+ coalesce("order_id"::varchar, '<null>')
188
+ || '|' || coalesce(("line_code" COLLATE "C"), '<null>')
189
+ || '|' || coalesce(to_char("updated_at"::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), '<null>')
190
+ || '|' || coalesce("updated_by", '<null>')
184
191
  )), 18))::bit(60)::bigint)
185
- FROM "sales"."address_audit"
186
- WHERE ("address_sk" >= 1405) AND (("oracle_column_name" COLLATE "C") >= 'active_ind')
187
- AND ("address_sk" < 2662) AND (("oracle_column_name" COLLATE "C") < 'dffkhzQ2yoa4QcQl')
192
+ FROM "sales"."order_line"
193
+ WHERE ("order_id" >= 1405) AND (("line_code" COLLATE "C") >= 'A-001')
194
+ AND ("order_id" < 2662) AND (("line_code" COLLATE "C") < 'K-250')
188
195
  ```
189
196
 
190
197
  ## Defining columns
@@ -283,13 +290,157 @@ entries and `duplicate_keys` as the signal to look at the rows yourself.
283
290
  - **Character sets**: values both drivers decode to the same Unicode text compare equal at row level, but a
284
291
  checksum mismatch still costs a download of that segment.
285
292
 
293
+ ## Background: from aggregate fingerprints to checksum bisection
294
+
295
+ diffdb-core is the third iteration of my migration data-validation tooling. The first two are archived and kept
296
+ for reference.
297
+
298
+ ### 1. simple-dv-tool: hand-written aggregate SQL
299
+
300
+ [simple-dv-tool](https://github.com/sarath-mec/simple-dv-tool) validated Hive / MySQL → BigQuery migrations from a
301
+ notebook. It split the source into N batches by ranking on a split key, sent each batch's source key range to
302
+ BigQuery as a temp table, and compared **aggregate fingerprints** per batch: `COUNT(1)`, and per column
303
+ `COUNT(DISTINCT c)`, null count, `MIN(c)` and `MAX(c)`. The results were compared in DuckDB, and a BigQuery UDF
304
+ decided what counted as a match (rounding, timezone shifts, NULL-like strings).
305
+
306
+ - Every query was an f-string, with engine-specific code (Hive `DESCRIBE` parsing, MySQL collation, BigQuery scripting).
307
+ - The target could only be BigQuery, and each table was configured by hand.
308
+
309
+ ### 2. ibis-dv-tool: the same idea on Ibis, database-wide
310
+
311
+ [ibis-dv-tool](https://github.com/sarath-mec/ibis-dv-tool) kept the fingerprint idea but wrote every query as an
312
+ [Ibis](https://ibis-project.org/) expression, compiled to each backend's SQL, so any Ibis-supported pair worked. It
313
+ added a database-wide mapping phase (a metadata view in each database, fuzzy table/column matching, a reviewable
314
+ YAML config per table) and a **detailed** mode that pulled small tables into pandas and diffed them cell by cell on
315
+ the primary key.
316
+
317
+ Both tools hit the same limits:
318
+
319
+ - **Fingerprints miss changes.** `COUNT(DISTINCT)`, `MIN` and `MAX` stay the same when a value changes in the
320
+ middle of a range, or when two rows swap values. They show *which batch and column* look wrong, never *which rows*.
321
+ - **Row-level comparison doesn't scale.** Detailed mode had to pull the whole table (or batch) into pandas.
322
+ - **Batches are fixed up front.** Narrowing a mismatch inside a large batch was manual drill-down SQL.
323
+ - **Matching rules ran after the fact.** Values were compared in whatever text each engine returned, then a UDF or
324
+ pandas dtype alignment tried to make them agree.
325
+
326
+ ### 3. reladiff: what checksum bisection fixed
327
+
328
+ [reladiff](https://github.com/erezsh/reladiff) (a maintained fork of data-diff) solves those problems with
329
+ **recursive checksum bisection**, which is the algorithm diffdb-core uses:
330
+
331
+ | | simple-dv-tool / ibis-dv-tool | reladiff (and diffdb-core) |
332
+ | :--- | :--- | :--- |
333
+ | What is compared | `COUNT`, `COUNT(DISTINCT)`, `MIN`, `MAX` per column | `count(*)` + sum of an MD5 of **every value of every row** |
334
+ | Detects | changes that move a count, min or max | any changed, missing or extra row |
335
+ | Result | batch + column that differ, plus drill-down SQL | the exact differing rows |
336
+ | Narrowing down | fixed batches; manual drill-down | ranges that differ are split again, automatically, until small |
337
+ | Rows moved | none (summary) or the whole table (detailed) | only the rows of small, mismatching ranges |
338
+ | Cross-engine values | compared as returned, then matched by tolerance rules | normalized to the same text **in SQL** before hashing, with precision lowered to the common minimum |
339
+
340
+ What reladiff doesn't do, and the earlier tools did: database-wide table and column mapping, fuzzy name matching
341
+ and tolerance rules. diffdb-core doesn't do those either; it expects the caller to supply the pairing (see below).
342
+
343
+ ## How diffdb-core differs from reladiff
344
+
345
+ diffdb-core adapts reladiff's bisection engine (`HashDiffer`, segments, key-space geometry, threaded yielder; see
346
+ [License](#license)) and changes how tables are described, how dialects are defined and how results are reported.
347
+
348
+ | | reladiff | diffdb-core |
349
+ | :--- | :--- | :--- |
350
+ | Column definitions | column **names** (`key_columns`, `extra_columns`); types read from the database | `columns={name: {"datatype", "transform_rule"}}` per side; nothing read from the database |
351
+ | Metadata queries | reads `information_schema` and samples rows to refine types | none (except Oracle `NLS_CHARACTERSET`, once per connection) |
352
+ | Compare an SQL expression | not supported (create a view) | `transform_rule` on any column, bisection columns included |
353
+ | LOB / binary / unknown types | compared anyway, with a "may result in false positives" warning | rejected up front; reduce them with a `transform_rule` |
354
+ | Dialects | Python classes and mixins from [sqeleton](https://github.com/erezsh/sqeleton), SQL built by its query builder | one validated YAML file per database; SQL built as plain strings in `core/sql.py` |
355
+ | Result | `+`/`-` row stream; `get_stats_dict()` counts | the same stream, plus `summarize()`: per-key, per-column results |
356
+ | Duplicate keys in stats | a key with two differing rows on the same side fails an `assert` | reported as `duplicate_key`, with `duplicate_keys=True` |
357
+ | Limit | `--limit` on rows (CLI; can't be combined with `--stats`) | `summarize(limit=N)` on distinct differing keys, cancels outstanding queries |
358
+ | Algorithms | `hashdiff` (cross-database) and `joindiff` (same database) | `hashdiff` only |
359
+ | Interface | CLI, TOML run config, Python API | Python API only |
360
+ | Incremental runs | `update_column` with `min_update` / `max_update` | a `where` filter |
361
+
362
+ ### `transform_rule`: comparing expressions, not just columns
363
+
364
+ In reladiff a compared column is a name that must exist in the table's schema. To compare `TRIM(name)`, a CLOB's
365
+ length, or a string key under binary collation, you have to create a view on each side first.
366
+
367
+ In diffdb-core each column entry can carry a `transform_rule`, an SQL expression used instead of the column, and a
368
+ `datatype` that describes the expression's result:
369
+
370
+ ```python
371
+ "NOTES": {"datatype": "NUMBER(10,0)", "transform_rule": 'DBMS_LOB.GETLENGTH("NOTES")'}, # Oracle CLOB
372
+ "notes": {"datatype": "integer", "transform_rule": 'LENGTH("notes")'}, # PostgreSQL text
373
+ "line_code": {"datatype": "varchar(20)", "transform_rule": '"line_code" COLLATE "C"'}, # byte-order bisection
374
+ ```
375
+
376
+ - The rule is wrapped in parentheses and used **everywhere** the column appears: in the checksum, the key-range
377
+ `MIN`/`MAX`, the downloaded rows, and the key-range `WHERE` clauses when it's a bisection column (see the
378
+ PostgreSQL query in [How it works](#how-it-works)).
379
+ - Each side has its own rule, so the two sides can use different SQL to reach the same value.
380
+ - The declared `datatype` drives normalization and precision reconciliation, so the result is hashed as the type
381
+ you declared, not the column's original type.
382
+ - This is how unsupported types are handled: LOB and binary columns are rejected unless a rule reduces them to a
383
+ number, string or timestamp. reladiff instead hashes them with a warning, which can report false differences.
384
+
385
+ This is the same idea as ibis-dv-tool's editable `src_matched_cols` (where a source "column" could be a cast), now
386
+ applied inside the hash, on both sides, with a declared type.
387
+
388
+ ### YAML dialect declarations
389
+
390
+ reladiff gets its dialects from sqeleton: each database is a Python class composed from mixins
391
+ (`oracle.Dialect, oracle.Mixin_MD5, oracle.Mixin_NormalizeValue, …`), with type parsing and value normalization
392
+ spread across Python methods. Checking that two engines render a timestamp identically means reading code in
393
+ several classes.
394
+
395
+ diffdb-core declares each dialect in one YAML file (`src/databases/dialects/<db>.yaml`):
396
+
397
+ - `extends:` another dialect (usually `_base.yaml`, which holds portable ANSI defaults and documents every key).
398
+ - `syntax:` fragments (quoting, concatenation, `md5_as_int`, …), `type_classes` / `type_regexps` (native type name
399
+ → diffdb type, e.g. `NUMBER` → `Decimal`), and `normalization:` templates (how each type becomes text before
400
+ hashing).
401
+ - **Validated when it loads**: unknown keys, unknown type classes and unknown template placeholders raise
402
+ `DialectConfigError`, rather than producing wrong SQL at run time.
403
+ - Engine-specific behaviour lives in YAML, not in `if name == ...` branches. The only Python per database is a small
404
+ driver class (import and connection).
405
+ - Every dialect's generated SQL is pinned by golden snapshots (`tests/golden/`), so a YAML change that alters a
406
+ query shows up as a test diff.
407
+
408
+ Columns are also declared, not discovered: you pass the native `datatype` of each column, and the dialect's
409
+ `type_classes` turn it into a diffdb type. That replaces reladiff's `information_schema` query and row sampling.
410
+
411
+ (This YAML is a dialect definition. It is different from reladiff's TOML file, which describes runs, and from
412
+ ibis-dv-tool's YAML, which described the table mapping. diffdb-core has no run-config file.)
413
+
414
+ ### Summarization
415
+
416
+ reladiff's result is a stream of `('-', row)` and `('+', row)`. `get_stats_dict()` groups that stream by key and
417
+ returns counts: `rows_A`, `rows_B`, `exclusive_A`, `exclusive_B`, `updated`, `unchanged`, `total`. It doesn't say
418
+ *which* columns of an updated row changed, and it assumes each key has at most one differing row per side.
419
+
420
+ diffdb-core keeps that stream and `get_stats_dict()`, and adds `summarize(limit=N)`, which returns a `DiffSummary`:
421
+
422
+ | | reladiff `get_stats_dict()` | diffdb-core `summarize()` |
423
+ | :--- | :--- | :--- |
424
+ | Granularity | counts only | one `KeyDiff` per differing key |
425
+ | Changed rows | counted as `updated` | `changed`, with a `ColumnDiff` per differing column: source and target column names and values |
426
+ | Missing / extra rows | `exclusive_A` / `exclusive_B` counts | `source_only` / `target_only`, with the rows |
427
+ | Pairing key | the table's key columns | `key_columns`, separate from the bisection columns, need not be unique |
428
+ | Duplicate keys | `AssertionError` when one side has two differing rows for a key | `duplicate_key` entries with all unpaired rows, and `duplicate_keys=True` |
429
+ | Early stop | none for stats | `limit` = number of distinct differing keys; queries are cancelled and `limit_reached=True` |
430
+ | Rows covered | `rows_A` / `rows_B` | `source_scanned` / `target_scanned`, plus `raw_rows` read from the stream |
431
+ | Export | dict | `as_dict()`, JSON-ready |
432
+
433
+ See [Keys and the per-key summary](#keys-and-the-per-key-summary) for how rows are paired. This brings back the
434
+ cell-level report of ibis-dv-tool's detailed mode (key, column, source value, target value) without pulling whole
435
+ tables into memory: only the rows of mismatching segments are ever downloaded.
436
+
286
437
  ## Adding a database
287
438
 
288
- 1. A YAML dialect in `src/diffdb/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
439
+ 1. A YAML dialect in `src/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
289
440
  fragments, type mapping and normalization templates. `_base.yaml` documents every key; unknown keys, type
290
441
  classes and template placeholders are rejected when the dialect loads.
291
- 2. A small driver class in `src/diffdb/databases/<db>.py` (driver import and connection creation), registered in
292
- `DATABASE_BY_SCHEME` (`src/diffdb/databases/_connect.py`).
442
+ 2. A small driver class in `src/databases/<db>.py` (driver import and connection creation), registered in
443
+ `DATABASE_BY_SCHEME` (`src/databases/_connect.py`).
293
444
 
294
445
  The new dialect must render every value byte-identically to its counterpart. See
295
446
  [ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#4-declarative-yaml-dialect-framework).
@@ -12,11 +12,16 @@ engines and a `FULL OUTER JOIN` isn't possible.
12
12
  Databricks, DuckDB, Trino, Presto, ClickHouse, Vertica.
13
13
  - **Any pair**: the source and target can be different engines; each side renders values to the same text before hashing.
14
14
  - **No metadata reads**: you pass the columns, their native types and optional SQL transform rules, so the
15
- engine sends only four read-only query shapes and never touches the catalog.
15
+ engine sends only four read-only query shapes and never touches the catalog. (The one exception: an Oracle
16
+ connection reads `NLS_CHARACTERSET` once when it opens, unless `hash_charset` is given.)
16
17
  - **Non-unique keys** work: bisection columns need only be selective, and duplicate rows are counted.
17
18
  - **Per-key summary**: changed rows with their differing columns, source-only and target-only rows, duplicate keys.
18
19
  - **Telemetry**: every query, row count, checksum and duration can be captured for auditing.
19
20
 
21
+ diffdb-core is the third iteration of this tooling, after `simple-dv-tool` and `ibis-dv-tool`, and is built on
22
+ reladiff's algorithm. See [Background](#background-from-aggregate-fingerprints-to-checksum-bisection) for how it got
23
+ here and [How diffdb-core differs from reladiff](#how-diffdb-core-differs-from-reladiff) for what changed.
24
+
20
25
  ## Install
21
26
 
22
27
  ```bash
@@ -104,22 +109,24 @@ An existing `Database` can be passed to `connect_to_table()` instead of a URI, s
104
109
  4. **Download & compare**: only those small ranges are downloaded and compared in memory.
105
110
  5. **Summarize**: `summarize()` pairs the differing rows by `key_columns` (default: the bisection columns).
106
111
 
107
- For a table bisected on `(ADDRESS_SK, ORACLE_COLUMN_NAME)` the checksum queries look like this. Oracle (database
108
- character set isn't AL32UTF8, so the row is converted first, to hash the same bytes as PostgreSQL; detected from
112
+ For a table bisected on `(ORDER_ID, LINE_CODE)`, with columns `ORDER_ID NUMBER(12,0)` / `bigint`,
113
+ `LINE_CODE VARCHAR2(20)` / `varchar(20)`, `UPDATED_AT TIMESTAMP(6)` / `timestamp(6)` and
114
+ `UPDATED_BY VARCHAR2(30)` / `varchar(30)`, the checksum queries look like this. Oracle (database character set
115
+ isn't AL32UTF8, so the row is converted first, to hash the same bytes as PostgreSQL; detected from
109
116
  `NLS_CHARACTERSET` or forced with a `hash_charset` connection setting):
110
117
 
111
118
  ```sql
112
119
  SELECT
113
120
  count(*),
114
121
  sum(to_number(substr(standard_hash(CONVERT((
115
- NVL(to_char("ADDRESS_SK", 'FM99999999999999999999999999999999999999'), '<null>')
116
- || '|' || NVL("ORACLE_COLUMN_NAME", '<null>')
117
- || '|' || NVL(to_char(cast(("UPDATE_TMSTMP" + INTERVAL '+5' HOUR) as timestamp(6)), 'YYYY-MM-DD HH24:MI:SS.FF6'), '<null>')
118
- || '|' || NVL("UPDATE_USER_ID", '<null>')
122
+ NVL(to_char("ORDER_ID", 'FM99999999999999999999999999999999999999'), '<null>')
123
+ || '|' || NVL("LINE_CODE", '<null>')
124
+ || '|' || NVL(to_char(cast("UPDATED_AT" as timestamp(6)), 'YYYY-MM-DD HH24:MI:SS.FF6'), '<null>')
125
+ || '|' || NVL("UPDATED_BY", '<null>')
119
126
  ), 'AL32UTF8'), 'MD5'), 18), 'xxxxxxxxxxxxxxx'))
120
- FROM "SALES"."ADDRESS_AUDIT"
121
- WHERE ("ADDRESS_SK" >= 1405) AND ("ORACLE_COLUMN_NAME" >= 'active_ind')
122
- AND ("ADDRESS_SK" < 2662) AND ("ORACLE_COLUMN_NAME" < 'dffkhzQ2yoa4QcQl')
127
+ FROM "SALES"."ORDER_LINE"
128
+ WHERE ("ORDER_ID" >= 1405) AND ("LINE_CODE" >= 'A-001')
129
+ AND ("ORDER_ID" < 2662) AND ("LINE_CODE" < 'K-250')
123
130
  ```
124
131
 
125
132
  PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column, see Limitations):
@@ -128,14 +135,14 @@ PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column,
128
135
  SELECT
129
136
  count(*),
130
137
  sum(('x' || substring(md5((
131
- coalesce("address_sk"::varchar, '<null>')
132
- || '|' || coalesce(("oracle_column_name" COLLATE "C"), '<null>')
133
- || '|' || coalesce(to_char("update_tmstmp"::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), '<null>')
134
- || '|' || coalesce("update_user_id", '<null>')
138
+ coalesce("order_id"::varchar, '<null>')
139
+ || '|' || coalesce(("line_code" COLLATE "C"), '<null>')
140
+ || '|' || coalesce(to_char("updated_at"::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), '<null>')
141
+ || '|' || coalesce("updated_by", '<null>')
135
142
  )), 18))::bit(60)::bigint)
136
- FROM "sales"."address_audit"
137
- WHERE ("address_sk" >= 1405) AND (("oracle_column_name" COLLATE "C") >= 'active_ind')
138
- AND ("address_sk" < 2662) AND (("oracle_column_name" COLLATE "C") < 'dffkhzQ2yoa4QcQl')
143
+ FROM "sales"."order_line"
144
+ WHERE ("order_id" >= 1405) AND (("line_code" COLLATE "C") >= 'A-001')
145
+ AND ("order_id" < 2662) AND (("line_code" COLLATE "C") < 'K-250')
139
146
  ```
140
147
 
141
148
  ## Defining columns
@@ -234,13 +241,157 @@ entries and `duplicate_keys` as the signal to look at the rows yourself.
234
241
  - **Character sets**: values both drivers decode to the same Unicode text compare equal at row level, but a
235
242
  checksum mismatch still costs a download of that segment.
236
243
 
244
+ ## Background: from aggregate fingerprints to checksum bisection
245
+
246
+ diffdb-core is the third iteration of my migration data-validation tooling. The first two are archived and kept
247
+ for reference.
248
+
249
+ ### 1. simple-dv-tool: hand-written aggregate SQL
250
+
251
+ [simple-dv-tool](https://github.com/sarath-mec/simple-dv-tool) validated Hive / MySQL → BigQuery migrations from a
252
+ notebook. It split the source into N batches by ranking on a split key, sent each batch's source key range to
253
+ BigQuery as a temp table, and compared **aggregate fingerprints** per batch: `COUNT(1)`, and per column
254
+ `COUNT(DISTINCT c)`, null count, `MIN(c)` and `MAX(c)`. The results were compared in DuckDB, and a BigQuery UDF
255
+ decided what counted as a match (rounding, timezone shifts, NULL-like strings).
256
+
257
+ - Every query was an f-string, with engine-specific code (Hive `DESCRIBE` parsing, MySQL collation, BigQuery scripting).
258
+ - The target could only be BigQuery, and each table was configured by hand.
259
+
260
+ ### 2. ibis-dv-tool: the same idea on Ibis, database-wide
261
+
262
+ [ibis-dv-tool](https://github.com/sarath-mec/ibis-dv-tool) kept the fingerprint idea but wrote every query as an
263
+ [Ibis](https://ibis-project.org/) expression, compiled to each backend's SQL, so any Ibis-supported pair worked. It
264
+ added a database-wide mapping phase (a metadata view in each database, fuzzy table/column matching, a reviewable
265
+ YAML config per table) and a **detailed** mode that pulled small tables into pandas and diffed them cell by cell on
266
+ the primary key.
267
+
268
+ Both tools hit the same limits:
269
+
270
+ - **Fingerprints miss changes.** `COUNT(DISTINCT)`, `MIN` and `MAX` stay the same when a value changes in the
271
+ middle of a range, or when two rows swap values. They show *which batch and column* look wrong, never *which rows*.
272
+ - **Row-level comparison doesn't scale.** Detailed mode had to pull the whole table (or batch) into pandas.
273
+ - **Batches are fixed up front.** Narrowing a mismatch inside a large batch was manual drill-down SQL.
274
+ - **Matching rules ran after the fact.** Values were compared in whatever text each engine returned, then a UDF or
275
+ pandas dtype alignment tried to make them agree.
276
+
277
+ ### 3. reladiff: what checksum bisection fixed
278
+
279
+ [reladiff](https://github.com/erezsh/reladiff) (a maintained fork of data-diff) solves those problems with
280
+ **recursive checksum bisection**, which is the algorithm diffdb-core uses:
281
+
282
+ | | simple-dv-tool / ibis-dv-tool | reladiff (and diffdb-core) |
283
+ | :--- | :--- | :--- |
284
+ | What is compared | `COUNT`, `COUNT(DISTINCT)`, `MIN`, `MAX` per column | `count(*)` + sum of an MD5 of **every value of every row** |
285
+ | Detects | changes that move a count, min or max | any changed, missing or extra row |
286
+ | Result | batch + column that differ, plus drill-down SQL | the exact differing rows |
287
+ | Narrowing down | fixed batches; manual drill-down | ranges that differ are split again, automatically, until small |
288
+ | Rows moved | none (summary) or the whole table (detailed) | only the rows of small, mismatching ranges |
289
+ | Cross-engine values | compared as returned, then matched by tolerance rules | normalized to the same text **in SQL** before hashing, with precision lowered to the common minimum |
290
+
291
+ What reladiff doesn't do, and the earlier tools did: database-wide table and column mapping, fuzzy name matching
292
+ and tolerance rules. diffdb-core doesn't do those either; it expects the caller to supply the pairing (see below).
293
+
294
+ ## How diffdb-core differs from reladiff
295
+
296
+ diffdb-core adapts reladiff's bisection engine (`HashDiffer`, segments, key-space geometry, threaded yielder; see
297
+ [License](#license)) and changes how tables are described, how dialects are defined and how results are reported.
298
+
299
+ | | reladiff | diffdb-core |
300
+ | :--- | :--- | :--- |
301
+ | Column definitions | column **names** (`key_columns`, `extra_columns`); types read from the database | `columns={name: {"datatype", "transform_rule"}}` per side; nothing read from the database |
302
+ | Metadata queries | reads `information_schema` and samples rows to refine types | none (except Oracle `NLS_CHARACTERSET`, once per connection) |
303
+ | Compare an SQL expression | not supported (create a view) | `transform_rule` on any column, bisection columns included |
304
+ | LOB / binary / unknown types | compared anyway, with a "may result in false positives" warning | rejected up front; reduce them with a `transform_rule` |
305
+ | Dialects | Python classes and mixins from [sqeleton](https://github.com/erezsh/sqeleton), SQL built by its query builder | one validated YAML file per database; SQL built as plain strings in `core/sql.py` |
306
+ | Result | `+`/`-` row stream; `get_stats_dict()` counts | the same stream, plus `summarize()`: per-key, per-column results |
307
+ | Duplicate keys in stats | a key with two differing rows on the same side fails an `assert` | reported as `duplicate_key`, with `duplicate_keys=True` |
308
+ | Limit | `--limit` on rows (CLI; can't be combined with `--stats`) | `summarize(limit=N)` on distinct differing keys, cancels outstanding queries |
309
+ | Algorithms | `hashdiff` (cross-database) and `joindiff` (same database) | `hashdiff` only |
310
+ | Interface | CLI, TOML run config, Python API | Python API only |
311
+ | Incremental runs | `update_column` with `min_update` / `max_update` | a `where` filter |
312
+
313
+ ### `transform_rule`: comparing expressions, not just columns
314
+
315
+ In reladiff a compared column is a name that must exist in the table's schema. To compare `TRIM(name)`, a CLOB's
316
+ length, or a string key under binary collation, you have to create a view on each side first.
317
+
318
+ In diffdb-core each column entry can carry a `transform_rule`, an SQL expression used instead of the column, and a
319
+ `datatype` that describes the expression's result:
320
+
321
+ ```python
322
+ "NOTES": {"datatype": "NUMBER(10,0)", "transform_rule": 'DBMS_LOB.GETLENGTH("NOTES")'}, # Oracle CLOB
323
+ "notes": {"datatype": "integer", "transform_rule": 'LENGTH("notes")'}, # PostgreSQL text
324
+ "line_code": {"datatype": "varchar(20)", "transform_rule": '"line_code" COLLATE "C"'}, # byte-order bisection
325
+ ```
326
+
327
+ - The rule is wrapped in parentheses and used **everywhere** the column appears: in the checksum, the key-range
328
+ `MIN`/`MAX`, the downloaded rows, and the key-range `WHERE` clauses when it's a bisection column (see the
329
+ PostgreSQL query in [How it works](#how-it-works)).
330
+ - Each side has its own rule, so the two sides can use different SQL to reach the same value.
331
+ - The declared `datatype` drives normalization and precision reconciliation, so the result is hashed as the type
332
+ you declared, not the column's original type.
333
+ - This is how unsupported types are handled: LOB and binary columns are rejected unless a rule reduces them to a
334
+ number, string or timestamp. reladiff instead hashes them with a warning, which can report false differences.
335
+
336
+ This is the same idea as ibis-dv-tool's editable `src_matched_cols` (where a source "column" could be a cast), now
337
+ applied inside the hash, on both sides, with a declared type.
338
+
339
+ ### YAML dialect declarations
340
+
341
+ reladiff gets its dialects from sqeleton: each database is a Python class composed from mixins
342
+ (`oracle.Dialect, oracle.Mixin_MD5, oracle.Mixin_NormalizeValue, …`), with type parsing and value normalization
343
+ spread across Python methods. Checking that two engines render a timestamp identically means reading code in
344
+ several classes.
345
+
346
+ diffdb-core declares each dialect in one YAML file (`src/databases/dialects/<db>.yaml`):
347
+
348
+ - `extends:` another dialect (usually `_base.yaml`, which holds portable ANSI defaults and documents every key).
349
+ - `syntax:` fragments (quoting, concatenation, `md5_as_int`, …), `type_classes` / `type_regexps` (native type name
350
+ → diffdb type, e.g. `NUMBER` → `Decimal`), and `normalization:` templates (how each type becomes text before
351
+ hashing).
352
+ - **Validated when it loads**: unknown keys, unknown type classes and unknown template placeholders raise
353
+ `DialectConfigError`, rather than producing wrong SQL at run time.
354
+ - Engine-specific behaviour lives in YAML, not in `if name == ...` branches. The only Python per database is a small
355
+ driver class (import and connection).
356
+ - Every dialect's generated SQL is pinned by golden snapshots (`tests/golden/`), so a YAML change that alters a
357
+ query shows up as a test diff.
358
+
359
+ Columns are also declared, not discovered: you pass the native `datatype` of each column, and the dialect's
360
+ `type_classes` turn it into a diffdb type. That replaces reladiff's `information_schema` query and row sampling.
361
+
362
+ (This YAML is a dialect definition. It is different from reladiff's TOML file, which describes runs, and from
363
+ ibis-dv-tool's YAML, which described the table mapping. diffdb-core has no run-config file.)
364
+
365
+ ### Summarization
366
+
367
+ reladiff's result is a stream of `('-', row)` and `('+', row)`. `get_stats_dict()` groups that stream by key and
368
+ returns counts: `rows_A`, `rows_B`, `exclusive_A`, `exclusive_B`, `updated`, `unchanged`, `total`. It doesn't say
369
+ *which* columns of an updated row changed, and it assumes each key has at most one differing row per side.
370
+
371
+ diffdb-core keeps that stream and `get_stats_dict()`, and adds `summarize(limit=N)`, which returns a `DiffSummary`:
372
+
373
+ | | reladiff `get_stats_dict()` | diffdb-core `summarize()` |
374
+ | :--- | :--- | :--- |
375
+ | Granularity | counts only | one `KeyDiff` per differing key |
376
+ | Changed rows | counted as `updated` | `changed`, with a `ColumnDiff` per differing column: source and target column names and values |
377
+ | Missing / extra rows | `exclusive_A` / `exclusive_B` counts | `source_only` / `target_only`, with the rows |
378
+ | Pairing key | the table's key columns | `key_columns`, separate from the bisection columns, need not be unique |
379
+ | Duplicate keys | `AssertionError` when one side has two differing rows for a key | `duplicate_key` entries with all unpaired rows, and `duplicate_keys=True` |
380
+ | Early stop | none for stats | `limit` = number of distinct differing keys; queries are cancelled and `limit_reached=True` |
381
+ | Rows covered | `rows_A` / `rows_B` | `source_scanned` / `target_scanned`, plus `raw_rows` read from the stream |
382
+ | Export | dict | `as_dict()`, JSON-ready |
383
+
384
+ See [Keys and the per-key summary](#keys-and-the-per-key-summary) for how rows are paired. This brings back the
385
+ cell-level report of ibis-dv-tool's detailed mode (key, column, source value, target value) without pulling whole
386
+ tables into memory: only the rows of mismatching segments are ever downloaded.
387
+
237
388
  ## Adding a database
238
389
 
239
- 1. A YAML dialect in `src/diffdb/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
390
+ 1. A YAML dialect in `src/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
240
391
  fragments, type mapping and normalization templates. `_base.yaml` documents every key; unknown keys, type
241
392
  classes and template placeholders are rejected when the dialect loads.
242
- 2. A small driver class in `src/diffdb/databases/<db>.py` (driver import and connection creation), registered in
243
- `DATABASE_BY_SCHEME` (`src/diffdb/databases/_connect.py`).
393
+ 2. A small driver class in `src/databases/<db>.py` (driver import and connection creation), registered in
394
+ `DATABASE_BY_SCHEME` (`src/databases/_connect.py`).
244
395
 
245
396
  The new dialect must render every value byte-identically to its counterpart. See
246
397
  [ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#4-declarative-yaml-dialect-framework).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: diffdb-core
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Cross-database table diffing by recursive checksum bisection, with declarative YAML SQL dialects
5
5
  Author: Sarath Mohan
6
6
  License: MIT
@@ -61,11 +61,16 @@ engines and a `FULL OUTER JOIN` isn't possible.
61
61
  Databricks, DuckDB, Trino, Presto, ClickHouse, Vertica.
62
62
  - **Any pair**: the source and target can be different engines; each side renders values to the same text before hashing.
63
63
  - **No metadata reads**: you pass the columns, their native types and optional SQL transform rules, so the
64
- engine sends only four read-only query shapes and never touches the catalog.
64
+ engine sends only four read-only query shapes and never touches the catalog. (The one exception: an Oracle
65
+ connection reads `NLS_CHARACTERSET` once when it opens, unless `hash_charset` is given.)
65
66
  - **Non-unique keys** work: bisection columns need only be selective, and duplicate rows are counted.
66
67
  - **Per-key summary**: changed rows with their differing columns, source-only and target-only rows, duplicate keys.
67
68
  - **Telemetry**: every query, row count, checksum and duration can be captured for auditing.
68
69
 
70
+ diffdb-core is the third iteration of this tooling, after `simple-dv-tool` and `ibis-dv-tool`, and is built on
71
+ reladiff's algorithm. See [Background](#background-from-aggregate-fingerprints-to-checksum-bisection) for how it got
72
+ here and [How diffdb-core differs from reladiff](#how-diffdb-core-differs-from-reladiff) for what changed.
73
+
69
74
  ## Install
70
75
 
71
76
  ```bash
@@ -153,22 +158,24 @@ An existing `Database` can be passed to `connect_to_table()` instead of a URI, s
153
158
  4. **Download & compare**: only those small ranges are downloaded and compared in memory.
154
159
  5. **Summarize**: `summarize()` pairs the differing rows by `key_columns` (default: the bisection columns).
155
160
 
156
- For a table bisected on `(ADDRESS_SK, ORACLE_COLUMN_NAME)` the checksum queries look like this. Oracle (database
157
- character set isn't AL32UTF8, so the row is converted first, to hash the same bytes as PostgreSQL; detected from
161
+ For a table bisected on `(ORDER_ID, LINE_CODE)`, with columns `ORDER_ID NUMBER(12,0)` / `bigint`,
162
+ `LINE_CODE VARCHAR2(20)` / `varchar(20)`, `UPDATED_AT TIMESTAMP(6)` / `timestamp(6)` and
163
+ `UPDATED_BY VARCHAR2(30)` / `varchar(30)`, the checksum queries look like this. Oracle (database character set
164
+ isn't AL32UTF8, so the row is converted first, to hash the same bytes as PostgreSQL; detected from
158
165
  `NLS_CHARACTERSET` or forced with a `hash_charset` connection setting):
159
166
 
160
167
  ```sql
161
168
  SELECT
162
169
  count(*),
163
170
  sum(to_number(substr(standard_hash(CONVERT((
164
- NVL(to_char("ADDRESS_SK", 'FM99999999999999999999999999999999999999'), '<null>')
165
- || '|' || NVL("ORACLE_COLUMN_NAME", '<null>')
166
- || '|' || NVL(to_char(cast(("UPDATE_TMSTMP" + INTERVAL '+5' HOUR) as timestamp(6)), 'YYYY-MM-DD HH24:MI:SS.FF6'), '<null>')
167
- || '|' || NVL("UPDATE_USER_ID", '<null>')
171
+ NVL(to_char("ORDER_ID", 'FM99999999999999999999999999999999999999'), '<null>')
172
+ || '|' || NVL("LINE_CODE", '<null>')
173
+ || '|' || NVL(to_char(cast("UPDATED_AT" as timestamp(6)), 'YYYY-MM-DD HH24:MI:SS.FF6'), '<null>')
174
+ || '|' || NVL("UPDATED_BY", '<null>')
168
175
  ), 'AL32UTF8'), 'MD5'), 18), 'xxxxxxxxxxxxxxx'))
169
- FROM "SALES"."ADDRESS_AUDIT"
170
- WHERE ("ADDRESS_SK" >= 1405) AND ("ORACLE_COLUMN_NAME" >= 'active_ind')
171
- AND ("ADDRESS_SK" < 2662) AND ("ORACLE_COLUMN_NAME" < 'dffkhzQ2yoa4QcQl')
176
+ FROM "SALES"."ORDER_LINE"
177
+ WHERE ("ORDER_ID" >= 1405) AND ("LINE_CODE" >= 'A-001')
178
+ AND ("ORDER_ID" < 2662) AND ("LINE_CODE" < 'K-250')
172
179
  ```
173
180
 
174
181
  PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column, see Limitations):
@@ -177,14 +184,14 @@ PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column,
177
184
  SELECT
178
185
  count(*),
179
186
  sum(('x' || substring(md5((
180
- coalesce("address_sk"::varchar, '<null>')
181
- || '|' || coalesce(("oracle_column_name" COLLATE "C"), '<null>')
182
- || '|' || coalesce(to_char("update_tmstmp"::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), '<null>')
183
- || '|' || coalesce("update_user_id", '<null>')
187
+ coalesce("order_id"::varchar, '<null>')
188
+ || '|' || coalesce(("line_code" COLLATE "C"), '<null>')
189
+ || '|' || coalesce(to_char("updated_at"::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), '<null>')
190
+ || '|' || coalesce("updated_by", '<null>')
184
191
  )), 18))::bit(60)::bigint)
185
- FROM "sales"."address_audit"
186
- WHERE ("address_sk" >= 1405) AND (("oracle_column_name" COLLATE "C") >= 'active_ind')
187
- AND ("address_sk" < 2662) AND (("oracle_column_name" COLLATE "C") < 'dffkhzQ2yoa4QcQl')
192
+ FROM "sales"."order_line"
193
+ WHERE ("order_id" >= 1405) AND (("line_code" COLLATE "C") >= 'A-001')
194
+ AND ("order_id" < 2662) AND (("line_code" COLLATE "C") < 'K-250')
188
195
  ```
189
196
 
190
197
  ## Defining columns
@@ -283,13 +290,157 @@ entries and `duplicate_keys` as the signal to look at the rows yourself.
283
290
  - **Character sets**: values both drivers decode to the same Unicode text compare equal at row level, but a
284
291
  checksum mismatch still costs a download of that segment.
285
292
 
293
+ ## Background: from aggregate fingerprints to checksum bisection
294
+
295
+ diffdb-core is the third iteration of my migration data-validation tooling. The first two are archived and kept
296
+ for reference.
297
+
298
+ ### 1. simple-dv-tool: hand-written aggregate SQL
299
+
300
+ [simple-dv-tool](https://github.com/sarath-mec/simple-dv-tool) validated Hive / MySQL → BigQuery migrations from a
301
+ notebook. It split the source into N batches by ranking on a split key, sent each batch's source key range to
302
+ BigQuery as a temp table, and compared **aggregate fingerprints** per batch: `COUNT(1)`, and per column
303
+ `COUNT(DISTINCT c)`, null count, `MIN(c)` and `MAX(c)`. The results were compared in DuckDB, and a BigQuery UDF
304
+ decided what counted as a match (rounding, timezone shifts, NULL-like strings).
305
+
306
+ - Every query was an f-string, with engine-specific code (Hive `DESCRIBE` parsing, MySQL collation, BigQuery scripting).
307
+ - The target could only be BigQuery, and each table was configured by hand.
308
+
309
+ ### 2. ibis-dv-tool: the same idea on Ibis, database-wide
310
+
311
+ [ibis-dv-tool](https://github.com/sarath-mec/ibis-dv-tool) kept the fingerprint idea but wrote every query as an
312
+ [Ibis](https://ibis-project.org/) expression, compiled to each backend's SQL, so any Ibis-supported pair worked. It
313
+ added a database-wide mapping phase (a metadata view in each database, fuzzy table/column matching, a reviewable
314
+ YAML config per table) and a **detailed** mode that pulled small tables into pandas and diffed them cell by cell on
315
+ the primary key.
316
+
317
+ Both tools hit the same limits:
318
+
319
+ - **Fingerprints miss changes.** `COUNT(DISTINCT)`, `MIN` and `MAX` stay the same when a value changes in the
320
+ middle of a range, or when two rows swap values. They show *which batch and column* look wrong, never *which rows*.
321
+ - **Row-level comparison doesn't scale.** Detailed mode had to pull the whole table (or batch) into pandas.
322
+ - **Batches are fixed up front.** Narrowing a mismatch inside a large batch was manual drill-down SQL.
323
+ - **Matching rules ran after the fact.** Values were compared in whatever text each engine returned, then a UDF or
324
+ pandas dtype alignment tried to make them agree.
325
+
326
+ ### 3. reladiff: what checksum bisection fixed
327
+
328
+ [reladiff](https://github.com/erezsh/reladiff) (a maintained fork of data-diff) solves those problems with
329
+ **recursive checksum bisection**, which is the algorithm diffdb-core uses:
330
+
331
+ | | simple-dv-tool / ibis-dv-tool | reladiff (and diffdb-core) |
332
+ | :--- | :--- | :--- |
333
+ | What is compared | `COUNT`, `COUNT(DISTINCT)`, `MIN`, `MAX` per column | `count(*)` + sum of an MD5 of **every value of every row** |
334
+ | Detects | changes that move a count, min or max | any changed, missing or extra row |
335
+ | Result | batch + column that differ, plus drill-down SQL | the exact differing rows |
336
+ | Narrowing down | fixed batches; manual drill-down | ranges that differ are split again, automatically, until small |
337
+ | Rows moved | none (summary) or the whole table (detailed) | only the rows of small, mismatching ranges |
338
+ | Cross-engine values | compared as returned, then matched by tolerance rules | normalized to the same text **in SQL** before hashing, with precision lowered to the common minimum |
339
+
340
+ What reladiff doesn't do, and the earlier tools did: database-wide table and column mapping, fuzzy name matching
341
+ and tolerance rules. diffdb-core doesn't do those either; it expects the caller to supply the pairing (see below).
342
+
343
+ ## How diffdb-core differs from reladiff
344
+
345
+ diffdb-core adapts reladiff's bisection engine (`HashDiffer`, segments, key-space geometry, threaded yielder; see
346
+ [License](#license)) and changes how tables are described, how dialects are defined and how results are reported.
347
+
348
+ | | reladiff | diffdb-core |
349
+ | :--- | :--- | :--- |
350
+ | Column definitions | column **names** (`key_columns`, `extra_columns`); types read from the database | `columns={name: {"datatype", "transform_rule"}}` per side; nothing read from the database |
351
+ | Metadata queries | reads `information_schema` and samples rows to refine types | none (except Oracle `NLS_CHARACTERSET`, once per connection) |
352
+ | Compare an SQL expression | not supported (create a view) | `transform_rule` on any column, bisection columns included |
353
+ | LOB / binary / unknown types | compared anyway, with a "may result in false positives" warning | rejected up front; reduce them with a `transform_rule` |
354
+ | Dialects | Python classes and mixins from [sqeleton](https://github.com/erezsh/sqeleton), SQL built by its query builder | one validated YAML file per database; SQL built as plain strings in `core/sql.py` |
355
+ | Result | `+`/`-` row stream; `get_stats_dict()` counts | the same stream, plus `summarize()`: per-key, per-column results |
356
+ | Duplicate keys in stats | a key with two differing rows on the same side fails an `assert` | reported as `duplicate_key`, with `duplicate_keys=True` |
357
+ | Limit | `--limit` on rows (CLI; can't be combined with `--stats`) | `summarize(limit=N)` on distinct differing keys, cancels outstanding queries |
358
+ | Algorithms | `hashdiff` (cross-database) and `joindiff` (same database) | `hashdiff` only |
359
+ | Interface | CLI, TOML run config, Python API | Python API only |
360
+ | Incremental runs | `update_column` with `min_update` / `max_update` | a `where` filter |
361
+
362
+ ### `transform_rule`: comparing expressions, not just columns
363
+
364
+ In reladiff a compared column is a name that must exist in the table's schema. To compare `TRIM(name)`, a CLOB's
365
+ length, or a string key under binary collation, you have to create a view on each side first.
366
+
367
+ In diffdb-core each column entry can carry a `transform_rule`, an SQL expression used instead of the column, and a
368
+ `datatype` that describes the expression's result:
369
+
370
+ ```python
371
+ "NOTES": {"datatype": "NUMBER(10,0)", "transform_rule": 'DBMS_LOB.GETLENGTH("NOTES")'}, # Oracle CLOB
372
+ "notes": {"datatype": "integer", "transform_rule": 'LENGTH("notes")'}, # PostgreSQL text
373
+ "line_code": {"datatype": "varchar(20)", "transform_rule": '"line_code" COLLATE "C"'}, # byte-order bisection
374
+ ```
375
+
376
+ - The rule is wrapped in parentheses and used **everywhere** the column appears: in the checksum, the key-range
377
+ `MIN`/`MAX`, the downloaded rows, and the key-range `WHERE` clauses when it's a bisection column (see the
378
+ PostgreSQL query in [How it works](#how-it-works)).
379
+ - Each side has its own rule, so the two sides can use different SQL to reach the same value.
380
+ - The declared `datatype` drives normalization and precision reconciliation, so the result is hashed as the type
381
+ you declared, not the column's original type.
382
+ - This is how unsupported types are handled: LOB and binary columns are rejected unless a rule reduces them to a
383
+ number, string or timestamp. reladiff instead hashes them with a warning, which can report false differences.
384
+
385
+ This is the same idea as ibis-dv-tool's editable `src_matched_cols` (where a source "column" could be a cast), now
386
+ applied inside the hash, on both sides, with a declared type.
387
+
388
+ ### YAML dialect declarations
389
+
390
+ reladiff gets its dialects from sqeleton: each database is a Python class composed from mixins
391
+ (`oracle.Dialect, oracle.Mixin_MD5, oracle.Mixin_NormalizeValue, …`), with type parsing and value normalization
392
+ spread across Python methods. Checking that two engines render a timestamp identically means reading code in
393
+ several classes.
394
+
395
+ diffdb-core declares each dialect in one YAML file (`src/databases/dialects/<db>.yaml`):
396
+
397
+ - `extends:` another dialect (usually `_base.yaml`, which holds portable ANSI defaults and documents every key).
398
+ - `syntax:` fragments (quoting, concatenation, `md5_as_int`, …), `type_classes` / `type_regexps` (native type name
399
+ → diffdb type, e.g. `NUMBER` → `Decimal`), and `normalization:` templates (how each type becomes text before
400
+ hashing).
401
+ - **Validated when it loads**: unknown keys, unknown type classes and unknown template placeholders raise
402
+ `DialectConfigError`, rather than producing wrong SQL at run time.
403
+ - Engine-specific behaviour lives in YAML, not in `if name == ...` branches. The only Python per database is a small
404
+ driver class (import and connection).
405
+ - Every dialect's generated SQL is pinned by golden snapshots (`tests/golden/`), so a YAML change that alters a
406
+ query shows up as a test diff.
407
+
408
+ Columns are also declared, not discovered: you pass the native `datatype` of each column, and the dialect's
409
+ `type_classes` turn it into a diffdb type. That replaces reladiff's `information_schema` query and row sampling.
410
+
411
+ (This YAML is a dialect definition. It is different from reladiff's TOML file, which describes runs, and from
412
+ ibis-dv-tool's YAML, which described the table mapping. diffdb-core has no run-config file.)
413
+
414
+ ### Summarization
415
+
416
+ reladiff's result is a stream of `('-', row)` and `('+', row)`. `get_stats_dict()` groups that stream by key and
417
+ returns counts: `rows_A`, `rows_B`, `exclusive_A`, `exclusive_B`, `updated`, `unchanged`, `total`. It doesn't say
418
+ *which* columns of an updated row changed, and it assumes each key has at most one differing row per side.
419
+
420
+ diffdb-core keeps that stream and `get_stats_dict()`, and adds `summarize(limit=N)`, which returns a `DiffSummary`:
421
+
422
+ | | reladiff `get_stats_dict()` | diffdb-core `summarize()` |
423
+ | :--- | :--- | :--- |
424
+ | Granularity | counts only | one `KeyDiff` per differing key |
425
+ | Changed rows | counted as `updated` | `changed`, with a `ColumnDiff` per differing column: source and target column names and values |
426
+ | Missing / extra rows | `exclusive_A` / `exclusive_B` counts | `source_only` / `target_only`, with the rows |
427
+ | Pairing key | the table's key columns | `key_columns`, separate from the bisection columns, need not be unique |
428
+ | Duplicate keys | `AssertionError` when one side has two differing rows for a key | `duplicate_key` entries with all unpaired rows, and `duplicate_keys=True` |
429
+ | Early stop | none for stats | `limit` = number of distinct differing keys; queries are cancelled and `limit_reached=True` |
430
+ | Rows covered | `rows_A` / `rows_B` | `source_scanned` / `target_scanned`, plus `raw_rows` read from the stream |
431
+ | Export | dict | `as_dict()`, JSON-ready |
432
+
433
+ See [Keys and the per-key summary](#keys-and-the-per-key-summary) for how rows are paired. This brings back the
434
+ cell-level report of ibis-dv-tool's detailed mode (key, column, source value, target value) without pulling whole
435
+ tables into memory: only the rows of mismatching segments are ever downloaded.
436
+
286
437
  ## Adding a database
287
438
 
288
- 1. A YAML dialect in `src/diffdb/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
439
+ 1. A YAML dialect in `src/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
289
440
  fragments, type mapping and normalization templates. `_base.yaml` documents every key; unknown keys, type
290
441
  classes and template placeholders are rejected when the dialect loads.
291
- 2. A small driver class in `src/diffdb/databases/<db>.py` (driver import and connection creation), registered in
292
- `DATABASE_BY_SCHEME` (`src/diffdb/databases/_connect.py`).
442
+ 2. A small driver class in `src/databases/<db>.py` (driver import and connection creation), registered in
443
+ `DATABASE_BY_SCHEME` (`src/databases/_connect.py`).
293
444
 
294
445
  The new dialect must render every value byte-identically to its counterpart. See
295
446
  [ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#4-declarative-yaml-dialect-framework).
@@ -0,0 +1,64 @@
1
+ LICENSE.md
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ diffdb_core.egg-info/PKG-INFO
6
+ diffdb_core.egg-info/SOURCES.txt
7
+ diffdb_core.egg-info/dependency_links.txt
8
+ diffdb_core.egg-info/requires.txt
9
+ diffdb_core.egg-info/top_level.txt
10
+ src/__init__.py
11
+ src/types.py
12
+ src/utils.py
13
+ src/concurrency/__init__.py
14
+ src/concurrency/pool.py
15
+ src/concurrency/stream.py
16
+ src/core/__init__.py
17
+ src/core/differ.py
18
+ src/core/hash_differ.py
19
+ src/core/segment.py
20
+ src/core/sql.py
21
+ src/core/summary.py
22
+ src/databases/__init__.py
23
+ src/databases/_connect.py
24
+ src/databases/base.py
25
+ src/databases/bigquery.py
26
+ src/databases/clickhouse.py
27
+ src/databases/databricks.py
28
+ src/databases/duckdb.py
29
+ src/databases/mysql.py
30
+ src/databases/oracle.py
31
+ src/databases/postgresql.py
32
+ src/databases/presto.py
33
+ src/databases/redshift.py
34
+ src/databases/snowflake.py
35
+ src/databases/trino.py
36
+ src/databases/vertica.py
37
+ src/databases/yaml_dialect.py
38
+ src/databases/dialects/_base.yaml
39
+ src/databases/dialects/bigquery.yaml
40
+ src/databases/dialects/clickhouse.yaml
41
+ src/databases/dialects/databricks.yaml
42
+ src/databases/dialects/duckdb.yaml
43
+ src/databases/dialects/mysql.yaml
44
+ src/databases/dialects/oracle.yaml
45
+ src/databases/dialects/postgresql.yaml
46
+ src/databases/dialects/presto.yaml
47
+ src/databases/dialects/redshift.yaml
48
+ src/databases/dialects/snowflake.yaml
49
+ src/databases/dialects/trino.yaml
50
+ src/databases/dialects/vertica.yaml
51
+ src/geometry/__init__.py
52
+ src/geometry/arithmetic.py
53
+ src/geometry/bisection.py
54
+ src/geometry/vector.py
55
+ src/telemetry/__init__.py
56
+ src/telemetry/info_tree.py
57
+ src/telemetry/query_tracker.py
58
+ tests/test_connect.py
59
+ tests/test_dialects.py
60
+ tests/test_diffdb_core.py
61
+ tests/test_e2e_duckdb.py
62
+ tests/test_query_snapshot.py
63
+ tests/golden/dialect_sql.json
64
+ tests/golden/query_sql.json
@@ -54,8 +54,17 @@ dev = [
54
54
  [tool.setuptools.dynamic]
55
55
  version = { attr = "diffdb.__version__" }
56
56
 
57
- [tool.setuptools.packages.find]
58
- where = ["src"]
57
+ [tool.setuptools]
58
+ # The package's source lives directly in src/ and installs as `diffdb`.
59
+ package-dir = { "diffdb" = "src" }
60
+ packages = [
61
+ "diffdb",
62
+ "diffdb.concurrency",
63
+ "diffdb.core",
64
+ "diffdb.databases",
65
+ "diffdb.geometry",
66
+ "diffdb.telemetry",
67
+ ]
59
68
 
60
69
  [tool.setuptools.package-data]
61
70
  diffdb = ["databases/dialects/*.yaml"]
@@ -17,7 +17,7 @@ from .geometry import Vector
17
17
  from .telemetry import QueryTracker
18
18
  from .types import DbPath
19
19
 
20
- __version__ = "0.1.0"
20
+ __version__ = "0.1.2"
21
21
 
22
22
 
23
23
  def connect_to_table(
@@ -1,64 +0,0 @@
1
- LICENSE.md
2
- MANIFEST.in
3
- README.md
4
- pyproject.toml
5
- src/diffdb/__init__.py
6
- src/diffdb/types.py
7
- src/diffdb/utils.py
8
- src/diffdb/concurrency/__init__.py
9
- src/diffdb/concurrency/pool.py
10
- src/diffdb/concurrency/stream.py
11
- src/diffdb/core/__init__.py
12
- src/diffdb/core/differ.py
13
- src/diffdb/core/hash_differ.py
14
- src/diffdb/core/segment.py
15
- src/diffdb/core/sql.py
16
- src/diffdb/core/summary.py
17
- src/diffdb/databases/__init__.py
18
- src/diffdb/databases/_connect.py
19
- src/diffdb/databases/base.py
20
- src/diffdb/databases/bigquery.py
21
- src/diffdb/databases/clickhouse.py
22
- src/diffdb/databases/databricks.py
23
- src/diffdb/databases/duckdb.py
24
- src/diffdb/databases/mysql.py
25
- src/diffdb/databases/oracle.py
26
- src/diffdb/databases/postgresql.py
27
- src/diffdb/databases/presto.py
28
- src/diffdb/databases/redshift.py
29
- src/diffdb/databases/snowflake.py
30
- src/diffdb/databases/trino.py
31
- src/diffdb/databases/vertica.py
32
- src/diffdb/databases/yaml_dialect.py
33
- src/diffdb/databases/dialects/_base.yaml
34
- src/diffdb/databases/dialects/bigquery.yaml
35
- src/diffdb/databases/dialects/clickhouse.yaml
36
- src/diffdb/databases/dialects/databricks.yaml
37
- src/diffdb/databases/dialects/duckdb.yaml
38
- src/diffdb/databases/dialects/mysql.yaml
39
- src/diffdb/databases/dialects/oracle.yaml
40
- src/diffdb/databases/dialects/postgresql.yaml
41
- src/diffdb/databases/dialects/presto.yaml
42
- src/diffdb/databases/dialects/redshift.yaml
43
- src/diffdb/databases/dialects/snowflake.yaml
44
- src/diffdb/databases/dialects/trino.yaml
45
- src/diffdb/databases/dialects/vertica.yaml
46
- src/diffdb/geometry/__init__.py
47
- src/diffdb/geometry/arithmetic.py
48
- src/diffdb/geometry/bisection.py
49
- src/diffdb/geometry/vector.py
50
- src/diffdb/telemetry/__init__.py
51
- src/diffdb/telemetry/info_tree.py
52
- src/diffdb/telemetry/query_tracker.py
53
- src/diffdb_core.egg-info/PKG-INFO
54
- src/diffdb_core.egg-info/SOURCES.txt
55
- src/diffdb_core.egg-info/dependency_links.txt
56
- src/diffdb_core.egg-info/requires.txt
57
- src/diffdb_core.egg-info/top_level.txt
58
- tests/test_connect.py
59
- tests/test_dialects.py
60
- tests/test_diffdb_core.py
61
- tests/test_e2e_duckdb.py
62
- tests/test_query_snapshot.py
63
- tests/golden/dialect_sql.json
64
- tests/golden/query_sql.json
File without changes
File without changes
File without changes