diffdb-core 0.1.2__tar.gz → 0.2.0__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.
- {diffdb_core-0.1.2/diffdb_core.egg-info → diffdb_core-0.2.0}/PKG-INFO +286 -89
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/README.md +285 -88
- {diffdb_core-0.1.2 → diffdb_core-0.2.0/diffdb_core.egg-info}/PKG-INFO +286 -89
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/__init__.py +1 -1
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/core/hash_differ.py +1 -12
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/core/segment.py +105 -17
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/core/sql.py +13 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/base.py +45 -1
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/duckdb.py +7 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/oracle.py +43 -3
- diffdb_core-0.2.0/src/databases/postgresql.py +70 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/redshift.py +3 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/yaml_dialect.py +1 -1
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/types.py +4 -4
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/golden/query_sql.json +208 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/test_dialects.py +15 -17
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/test_diffdb_core.py +207 -3
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/test_e2e_duckdb.py +18 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/test_query_snapshot.py +6 -2
- diffdb_core-0.1.2/src/databases/postgresql.py +0 -34
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/LICENSE.md +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/MANIFEST.in +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/diffdb_core.egg-info/SOURCES.txt +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/diffdb_core.egg-info/dependency_links.txt +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/diffdb_core.egg-info/requires.txt +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/diffdb_core.egg-info/top_level.txt +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/pyproject.toml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/setup.cfg +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/concurrency/__init__.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/concurrency/pool.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/concurrency/stream.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/core/__init__.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/core/differ.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/core/summary.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/__init__.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/_connect.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/bigquery.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/clickhouse.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/databricks.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/_base.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/bigquery.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/clickhouse.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/databricks.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/duckdb.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/mysql.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/oracle.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/postgresql.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/presto.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/redshift.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/snowflake.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/trino.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/dialects/vertica.yaml +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/mysql.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/presto.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/snowflake.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/trino.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/databases/vertica.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/geometry/__init__.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/geometry/arithmetic.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/geometry/bisection.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/geometry/vector.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/telemetry/__init__.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/telemetry/info_tree.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/telemetry/query_tracker.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/src/utils.py +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/golden/dialect_sql.json +0 -0
- {diffdb_core-0.1.2 → diffdb_core-0.2.0}/tests/test_connect.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: diffdb-core
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
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
|
|
@@ -60,16 +60,26 @@ engines and a `FULL OUTER JOIN` isn't possible.
|
|
|
60
60
|
- **12 databases** through declarative YAML dialects: PostgreSQL, Oracle, MySQL, Snowflake, BigQuery, Redshift,
|
|
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
|
-
- **
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
- **Just name the columns**: on PostgreSQL, Oracle and DuckDB the column types come from the database. Declare a
|
|
64
|
+
type or compare an SQL expression only when you need to.
|
|
65
|
+
- **No catalog reads**: no `information_schema`, primary-key or table-list queries. The engine sends a handful of
|
|
66
|
+
read-only query shapes (see [How it works](#how-it-works)).
|
|
66
67
|
- **Non-unique keys** work: bisection columns need only be selective, and duplicate rows are counted.
|
|
67
68
|
- **Per-key summary**: changed rows with their differing columns, source-only and target-only rows, duplicate keys.
|
|
68
69
|
- **Telemetry**: every query, row count, checksum and duration can be captured for auditing.
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
**Contents**
|
|
72
|
+
|
|
73
|
+
- Getting started: [Install](#install) · [Quick start](#quick-start-no-database-server-needed) ·
|
|
74
|
+
[Compare two real databases](#compare-two-real-databases) · [Reading the results](#reading-the-results)
|
|
75
|
+
- Going further: [When to declare a datatype](#when-to-declare-a-datatype) ·
|
|
76
|
+
[Comparing expressions](#comparing-expressions-transform_rule) · [Filtering](#filtering-and-incremental-runs) ·
|
|
77
|
+
[Performance](#performance) · [Auditing](#auditing-queries-raw-rows-and-stats) · [Common errors](#common-errors)
|
|
78
|
+
- Reference: [Arguments](#reference) · [How it works](#how-it-works) ·
|
|
79
|
+
[Keys and the per-key summary](#keys-and-the-per-key-summary) · [Known limitations](#known-limitations)
|
|
80
|
+
- Background: [From fingerprints to bisection](#background-from-aggregate-fingerprints-to-checksum-bisection) ·
|
|
81
|
+
[How diffdb-core differs from reladiff](#how-diffdb-core-differs-from-reladiff) ·
|
|
82
|
+
[Adding a database](#adding-a-database) · [Development](#development)
|
|
73
83
|
|
|
74
84
|
## Install
|
|
75
85
|
|
|
@@ -80,62 +90,98 @@ pip install "diffdb-core[postgresql,oracle]" # pick the extras for your databa
|
|
|
80
90
|
Extras: `oracle`, `postgresql`, `redshift`, `mysql`, `snowflake`, `bigquery`, `databricks`, `duckdb`, `trino`,
|
|
81
91
|
`presto`, `clickhouse`, `vertica`. The package is imported as `diffdb`. Python ≥ 3.12.
|
|
82
92
|
|
|
83
|
-
## Quick start
|
|
93
|
+
## Quick start (no database server needed)
|
|
94
|
+
|
|
95
|
+
This runs as is after `pip install "diffdb-core[duckdb]"`. It builds two small tables in a local DuckDB file, with
|
|
96
|
+
three differences between them, and finds them:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import diffdb
|
|
100
|
+
|
|
101
|
+
db = diffdb.connect("duckdb://demo@demo.duckdb") # a local file; created if missing
|
|
102
|
+
|
|
103
|
+
# Two copies of a table, with a few differences in the new one
|
|
104
|
+
db.query("""CREATE OR REPLACE TABLE customers_old AS
|
|
105
|
+
SELECT i AS id, 'user' || i || '@example.com' AS email, CAST(i * 1.5 AS DECIMAL(10,2)) AS balance
|
|
106
|
+
FROM range(1, 10001) t(i)""")
|
|
107
|
+
db.query("CREATE OR REPLACE TABLE customers_new AS SELECT * FROM customers_old")
|
|
108
|
+
db.query("UPDATE customers_new SET balance = balance + 1 WHERE id = 42")
|
|
109
|
+
db.query("DELETE FROM customers_new WHERE id = 7")
|
|
110
|
+
db.query("INSERT INTO customers_new VALUES (10001, 'new@example.com', 0)")
|
|
111
|
+
|
|
112
|
+
columns = {"id": {}, "email": {}, "balance": {}} # just the names; types come from the database
|
|
113
|
+
|
|
114
|
+
src = diffdb.connect_to_table(db, "customers_old", "id", columns) # "id" splits the table into key ranges
|
|
115
|
+
tgt = diffdb.connect_to_table(db, "customers_new", "id", columns)
|
|
116
|
+
|
|
117
|
+
with diffdb.diff_tables(src, tgt) as diff:
|
|
118
|
+
summary = diff.summarize()
|
|
119
|
+
|
|
120
|
+
print(f"changed rows: {summary.changed}")
|
|
121
|
+
print(f"only in source: {summary.source_only}")
|
|
122
|
+
print(f"only in target: {summary.target_only}")
|
|
123
|
+
|
|
124
|
+
for kd in summary.key_diffs:
|
|
125
|
+
if kd.kind == "changed":
|
|
126
|
+
for cd in kd.column_diffs:
|
|
127
|
+
print(f"id {kd.key[0]}: {cd.source_column} changed from {cd.source_value} to {cd.target_value}")
|
|
128
|
+
else:
|
|
129
|
+
print(f"id {kd.key[0]}: {kd.kind}")
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```text
|
|
133
|
+
changed rows: 1
|
|
134
|
+
only in source: 1
|
|
135
|
+
only in target: 1
|
|
136
|
+
id 10001: target_only
|
|
137
|
+
id 42: balance changed from 63.00 to 64.00
|
|
138
|
+
id 7: source_only
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The three things you always give are the table, the **bisection column** (`"id"`: the column used to split the
|
|
142
|
+
table into ranges, ideally the indexed primary key) and the **columns to compare**.
|
|
143
|
+
|
|
144
|
+
## Compare two real databases
|
|
145
|
+
|
|
146
|
+
The same call, one connection per side. Here an Oracle table is checked against its PostgreSQL copy:
|
|
84
147
|
|
|
85
148
|
```python
|
|
86
149
|
from diffdb import connect_to_table, diff_tables
|
|
87
150
|
|
|
88
|
-
# Every compared column with its native datatype. Source and target are paired by position,
|
|
89
|
-
# so both dicts list the columns in the same order (bisection columns first).
|
|
90
151
|
src = connect_to_table(
|
|
91
|
-
"oracle://app_user:pwd@oracle-host:1521/
|
|
152
|
+
"oracle://app_user:pwd@oracle-host:1521/ORCLPDB1",
|
|
92
153
|
"SALES.CUSTOMER",
|
|
93
|
-
bisection_columns=
|
|
94
|
-
columns={
|
|
95
|
-
"CUSTOMER_ID": {"datatype": "NUMBER(10,0)"},
|
|
96
|
-
"EMAIL": {"datatype": "VARCHAR2(200)"},
|
|
97
|
-
"BALANCE": {"datatype": "NUMBER(12,2)"},
|
|
98
|
-
"NOTES": {"datatype": "NUMBER(10,0)", "transform_rule": 'DBMS_LOB.GETLENGTH("NOTES")'}, # CLOB -> length
|
|
99
|
-
},
|
|
154
|
+
bisection_columns="CUSTOMER_ID",
|
|
155
|
+
columns={"CUSTOMER_ID": {}, "EMAIL": {}, "BALANCE": {}, "CREATED_AT": {}},
|
|
100
156
|
)
|
|
101
157
|
tgt = connect_to_table(
|
|
102
158
|
"postgresql://app_user:pwd@pg-host:5432/sales",
|
|
103
159
|
"sales.customer",
|
|
104
|
-
bisection_columns=
|
|
105
|
-
columns={
|
|
106
|
-
"customer_id": {"datatype": "bigint"},
|
|
107
|
-
"email": {"datatype": "varchar(200)"},
|
|
108
|
-
"balance": {"datatype": "numeric(12,2)"},
|
|
109
|
-
"notes": {"datatype": "integer", "transform_rule": 'LENGTH("notes")'},
|
|
110
|
-
},
|
|
160
|
+
bisection_columns="customer_id",
|
|
161
|
+
columns={"customer_id": {}, "email": {}, "balance": {}, "created_at": {}},
|
|
111
162
|
)
|
|
112
163
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
summary = diff.summarize(limit=1000) # stop after 1000 differing keys
|
|
116
|
-
finally:
|
|
117
|
-
diff.close()
|
|
164
|
+
with diff_tables(src, tgt) as diff:
|
|
165
|
+
summary = diff.summarize(limit=1000) # stop after 1000 differing keys
|
|
118
166
|
|
|
119
|
-
print(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
else: # source_only | target_only | duplicate_key: the unpaired rows
|
|
125
|
-
print(kd.key, kd.kind, kd.source_rows, kd.target_rows)
|
|
167
|
+
print(f"changed rows: {summary.changed}")
|
|
168
|
+
print(f"only in Oracle: {summary.source_only}")
|
|
169
|
+
print(f"only in PG: {summary.target_only}")
|
|
170
|
+
if summary.limit_reached:
|
|
171
|
+
print("stopped at 1000 differing keys: the counts above are partial")
|
|
126
172
|
```
|
|
127
173
|
|
|
128
|
-
|
|
129
|
-
[Keys and the per-key summary](#keys-and-the-per-key-summary).
|
|
174
|
+
Two rules to know from the start:
|
|
130
175
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
176
|
+
- **Columns are paired by position, not by name.** The first source column is compared with the first target
|
|
177
|
+
column, and so on, so list both sides in the same order. Names may differ (`CUSTOMER_ID` ↔ `customer_id`).
|
|
178
|
+
- **The bisection column is always compared first**, whatever its place in the dict.
|
|
134
179
|
|
|
135
|
-
|
|
180
|
+
That's all a typical migration check needs. If a column can't be compared as is (a plain Oracle `NUMBER`, a
|
|
181
|
+
`CLOB`, a database without type reporting), `connect_to_table` raises one `ValueError` that lists every such
|
|
182
|
+
column and how to fix it; the next sections explain the fixes.
|
|
136
183
|
|
|
137
|
-
|
|
138
|
-
one of the schemes above and whose other keys go to that database's Python driver:
|
|
184
|
+
Connections take a URI or a dict whose other keys go to that database's Python driver:
|
|
139
185
|
|
|
140
186
|
```python
|
|
141
187
|
import diffdb
|
|
@@ -146,7 +192,180 @@ ora = diffdb.connect({"driver": "oracle", "user": "app_user", "password": "...",
|
|
|
146
192
|
"dsn": "oracle-host:1521/ORCLPDB1"}, thread_count=4)
|
|
147
193
|
```
|
|
148
194
|
|
|
149
|
-
|
|
195
|
+
Pass a connected `Database` to `connect_to_table()` instead of a URI so one connection pool serves many tables.
|
|
196
|
+
|
|
197
|
+
## Reading the results
|
|
198
|
+
|
|
199
|
+
`summarize()` returns a `DiffSummary`:
|
|
200
|
+
|
|
201
|
+
| Field | Meaning |
|
|
202
|
+
| :--- | :--- |
|
|
203
|
+
| `changed` | keys present on both sides with different values |
|
|
204
|
+
| `source_only` / `target_only` | rows found on one side only |
|
|
205
|
+
| `source_scanned` / `target_scanned` | rows covered on each side |
|
|
206
|
+
| `limit_reached` | `True` if `limit` stopped the run early (the counts are then partial) |
|
|
207
|
+
| `duplicate_keys` | `True` if some key had several differing rows on one side |
|
|
208
|
+
| `key_diffs` | one `KeyDiff` per differing key: `key`, `kind` (`changed`, `source_only`, `target_only`, `duplicate_key`), `column_diffs` for changed rows, `source_rows` / `target_rows` otherwise |
|
|
209
|
+
|
|
210
|
+
Each `ColumnDiff` holds `source_column`, `target_column`, `source_value` and `target_value`. Values are the
|
|
211
|
+
normalized text that was compared (`'63.00'`, `'2024-01-01 12:57:00.777000'`, `'<null>'` for NULL).
|
|
212
|
+
`summary.as_dict()` gives the whole result as JSON-ready data.
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
for kd in summary.key_diffs:
|
|
216
|
+
if kd.kind == "changed":
|
|
217
|
+
for cd in kd.column_diffs:
|
|
218
|
+
print(kd.key, cd.target_column, cd.source_value, "->", cd.target_value)
|
|
219
|
+
else: # source_only | target_only | duplicate_key: the unpaired rows
|
|
220
|
+
print(kd.key, kd.kind, kd.source_rows, kd.target_rows)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Going further
|
|
224
|
+
|
|
225
|
+
### When to declare a datatype
|
|
226
|
+
|
|
227
|
+
Each entry in `columns` takes two optional keys: `{"datatype": "<native type>", "transform_rule": "<sql>"}`. With
|
|
228
|
+
neither, diffdb asks the database for the type: when the table is opened it sends one query that returns no
|
|
229
|
+
rows (`SELECT "CUSTOMER_ID", "EMAIL", … FROM "SALES"."CUSTOMER" WHERE 1=0`) and reads each column's type from the
|
|
230
|
+
driver's result metadata. `src.datatypes` shows the type used for every column.
|
|
231
|
+
|
|
232
|
+
Declare `datatype` yourself when:
|
|
233
|
+
|
|
234
|
+
| Situation | Example | Why |
|
|
235
|
+
| :--- | :--- | :--- |
|
|
236
|
+
| A plain decimal with no scale | Oracle `NUMBER`, PostgreSQL `numeric` | The number of decimals to compare is unknown, so it's rejected. Declare it: `{"datatype": "NUMBER(12,2)"}` / `{"datatype": "numeric(12,2)"}`, or `NUMBER(*,0)` / `numeric(38,0)` for whole numbers. |
|
|
237
|
+
| The database can't report types | MySQL, Snowflake, BigQuery, Redshift, Databricks, Trino, Presto, ClickHouse, Vertica | Type reporting is available on PostgreSQL, Oracle and DuckDB only. |
|
|
238
|
+
| An Oracle `TIMESTAMP(0)` | | Oracle reports no precision for it. |
|
|
239
|
+
| The bisection column is a plain `NUMBER` | `"ID": {"datatype": "NUMBER(12,0)"}` | Better than a `CAST` in a `transform_rule`, which would stop the database from using the column's index. |
|
|
240
|
+
|
|
241
|
+
A declared `datatype` always wins and isn't probed, so you can mix both styles in one dict:
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
columns={
|
|
245
|
+
"ID": {"datatype": "NUMBER(12,0)"}, # plain NUMBER key: declared
|
|
246
|
+
"EMAIL": {}, # VARCHAR2, reported
|
|
247
|
+
"AMOUNT": {}, # NUMBER(12,2), reported
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
`datatype` is the native type name as the database spells it (`VARCHAR2(100)`, `TIMESTAMP(3)`, `numeric(12,2)`,
|
|
252
|
+
`bigint`). Only basic types compare: numbers, strings, timestamps/dates and booleans; see [Types](#types).
|
|
253
|
+
|
|
254
|
+
### Comparing expressions: `transform_rule`
|
|
255
|
+
|
|
256
|
+
A `transform_rule` is an SQL expression used instead of the column, on that side only. Use it to reduce a type
|
|
257
|
+
that can't be compared (LOBs, binary), to clean values, or to make both engines agree:
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
oracle_columns = {
|
|
261
|
+
"ID": {},
|
|
262
|
+
"NOTES": {"transform_rule": 'CAST(DBMS_LOB.GETLENGTH("NOTES") AS NUMBER(10,0))'}, # CLOB -> its length
|
|
263
|
+
"NAME": {"transform_rule": 'TRIM("NAME")'},
|
|
264
|
+
"CREATED": {"transform_rule": 'TRUNC("CREATED")'}, # drop the time of day
|
|
265
|
+
}
|
|
266
|
+
postgres_columns = {
|
|
267
|
+
"id": {},
|
|
268
|
+
"notes": {"transform_rule": 'length("notes")'},
|
|
269
|
+
"name": {"transform_rule": 'trim("name")'},
|
|
270
|
+
"created": {"transform_rule": "date_trunc('day', \"created\")"},
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
- The rule is wrapped in parentheses and used **everywhere** the column appears: the checksum, the key-range
|
|
275
|
+
`MIN`/`MAX`, the downloaded rows and, for a bisection column, the key-range `WHERE` clauses.
|
|
276
|
+
- The type of the rule's result is reported like a column's, but engines often can't name it exactly. Oracle
|
|
277
|
+
reports `LENGTH`, `ROUND` and arithmetic as a plain `NUMBER`, and PostgreSQL reports `round()` and arithmetic as a
|
|
278
|
+
plain `numeric`. Both are rejected, so `CAST` the result (as above) or declare `datatype` for the result:
|
|
279
|
+
`{"datatype": "NUMBER(10,0)", "transform_rule": 'DBMS_LOB.GETLENGTH("NOTES")'}`.
|
|
280
|
+
- PostgreSQL's `length()` returns `integer`, so it needs no cast.
|
|
281
|
+
|
|
282
|
+
### Filtering and incremental runs
|
|
283
|
+
|
|
284
|
+
`where` adds an SQL filter to every query on that side, e.g. to check only recent changes (CDC windows):
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
src = connect_to_table(ora, "SALES.ORDERS", "ORDER_ID", cols,
|
|
288
|
+
where="\"UPDATED_AT\" >= TIMESTAMP '2026-10-01 00:00:00'")
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
`diff_tables(..., where=...)` sets the same filter on both sides, and `min_key` / `max_key` restrict the
|
|
292
|
+
bisection key range.
|
|
293
|
+
|
|
294
|
+
### Performance
|
|
295
|
+
|
|
296
|
+
- **Bisect on an indexed, selective column**, usually the primary key. Every checksum query filters on it.
|
|
297
|
+
- **Threads**: `connect(..., thread_count=N)` opens N connections per database, and
|
|
298
|
+
`diff_tables(..., max_threadpool_size=M)` sets how many queries run at once; make M at least 2 × N.
|
|
299
|
+
- **`bisection_factor`** (default 32): how many ranges each mismatching range is split into.
|
|
300
|
+
**`bisection_threshold`** (default 16,384): ranges with fewer rows are downloaded and compared in memory.
|
|
301
|
+
- **Mostly identical tables are fast**: matching ranges cost one checksum query each and nothing is downloaded.
|
|
302
|
+
- `summarize(limit=N)` stops after N differing keys and cancels the remaining queries.
|
|
303
|
+
|
|
304
|
+
### Auditing: queries, raw rows and stats
|
|
305
|
+
|
|
306
|
+
- `diff.get_queries_and_checksums()` lists every query sent, with row counts, checksums and durations
|
|
307
|
+
(`diff_tables(..., return_queries=True)` also returns the `QueryTracker`).
|
|
308
|
+
- `for sign, row in diff_tables(src, tgt)` streams the raw differences: `'-'` = source only, `'+'` = target only.
|
|
309
|
+
- `diff.get_stats_dict()` returns counts only.
|
|
310
|
+
- Logging goes through the `diffdb` logger (`logging.getLogger("diffdb").setLevel(logging.INFO)` shows each
|
|
311
|
+
reported column type).
|
|
312
|
+
|
|
313
|
+
See [ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#6-python-api-reference--practical-recipes)
|
|
314
|
+
for more recipes.
|
|
315
|
+
|
|
316
|
+
### Common errors
|
|
317
|
+
|
|
318
|
+
All problems with `columns` are reported together in one `ValueError` when the table is opened.
|
|
319
|
+
|
|
320
|
+
| Error says | Fix |
|
|
321
|
+
| :--- | :--- |
|
|
322
|
+
| `datatype 'NUMBER' has no scale` / `reports 'NUMBER' (no scale)` | Declare the scale (`NUMBER(12,2)`, `NUMBER(*,0)`) or `CAST` the rule. |
|
|
323
|
+
| `unsupported type 'CLOB'` (or `BLOB`, `bytea`, …) | Reduce it with a `transform_rule`, e.g. its length. |
|
|
324
|
+
| `can't report column types yet` | That database has no type reporting: declare `datatype` for each column. |
|
|
325
|
+
| `doesn't report enough to name the exact type` | Declare `datatype` (e.g. an Oracle `TIMESTAMP(0)`). |
|
|
326
|
+
| `bisection column(s) [...] are not in columns` | Add the bisection column to `columns`. |
|
|
327
|
+
| `Incompatible types for column` | The paired columns have different kinds (e.g. a string and a number). Check the column order, or cast one side. |
|
|
328
|
+
| `key_columns must sit at the same positions on both sides` | List the columns in the same order on both sides. |
|
|
329
|
+
| `EmptyTable` | A side has no rows in range: pass `allow_empty_tables=True` if that's expected. |
|
|
330
|
+
| `ORA-01489` | The Oracle row string is longer than 4000 bytes: compare wide text columns through `LENGTH(...)`. |
|
|
331
|
+
|
|
332
|
+
## Reference
|
|
333
|
+
|
|
334
|
+
`TableSegment` (or `connect_to_table`) takes:
|
|
335
|
+
|
|
336
|
+
| Argument | Meaning |
|
|
337
|
+
| :--- | :--- |
|
|
338
|
+
| `bisection_columns` | Columns that split the table into key ranges. Need not be unique, but should be selective and indexed: one value holding more rows than `bisection_threshold` can't be split and is downloaded whole (with a warning). Integers, decimals, timestamps, UUIDs and alphanumeric strings are supported. |
|
|
339
|
+
| `columns` | `{name: {"datatype": "<native type>", "transform_rule": "<sql>"}}` for every compared column, bisection columns included. Both keys are optional (see [When to declare a datatype](#when-to-declare-a-datatype) and [Comparing expressions](#comparing-expressions-transform_rule)). Paired with the other side by position. |
|
|
340
|
+
| `key_columns` | Columns identifying a logical row in the summary (default: `bisection_columns`). Used only to report differences; see [Keys and the per-key summary](#keys-and-the-per-key-summary). |
|
|
341
|
+
| `where` | Optional SQL filter added to every query, e.g. a time window. |
|
|
342
|
+
|
|
343
|
+
`diff_tables(src, tgt, ...)` options:
|
|
344
|
+
|
|
345
|
+
| Option | Default | Meaning |
|
|
346
|
+
| :--- | :--- | :--- |
|
|
347
|
+
| `bisection_columns`, `key_columns`, `where`, `min_key`, `max_key` | from the tables | override these on both sides |
|
|
348
|
+
| `bisection_factor` | 32 | ranges per split |
|
|
349
|
+
| `bisection_threshold` | 16384 | rows below which a range is downloaded |
|
|
350
|
+
| `threaded` / `max_threadpool_size` | `True` / 1 | run queries concurrently |
|
|
351
|
+
| `duplicate_rows_support` | `True` | count identical duplicate rows (needed when keys repeat) |
|
|
352
|
+
| `allow_empty_tables` | `False` | don't raise `EmptyTable` for empty sides |
|
|
353
|
+
| `skip_sort_results` | `False` | skip sorting the raw stream |
|
|
354
|
+
| `track_queries` / `return_queries` | `True` / `False` | record queries; also return the `QueryTracker` |
|
|
355
|
+
|
|
356
|
+
### Types
|
|
357
|
+
|
|
358
|
+
The engine compares numbers, strings, timestamps/dates and booleans (hashed as `1`/`0`); `NULL` is `'<null>'` on
|
|
359
|
+
every side. LOB and binary types (`CLOB`, `BLOB`, `RAW`, `bytea`, …) are rejected on purpose: reduce them with a
|
|
360
|
+
`transform_rule`. When the two sides have different precisions (`TIMESTAMP(3)` vs `timestamp(6)`, `NUMBER(12,4)`
|
|
361
|
+
vs `numeric(12,2)`), both are compared at the lower one.
|
|
362
|
+
|
|
363
|
+
A decimal must have a scale, declared or reported: a plain Oracle `NUMBER` / `NUMBER(*)` or PostgreSQL `numeric`
|
|
364
|
+
(or any dialect's decimal type without arguments) is rejected, because the number of fractional digits to compare
|
|
365
|
+
is unknown. An engine that can't name the exact type of a column or expression makes it fail the same way,
|
|
366
|
+
instead of comparing it at a guessed precision. One exception is safe: psycopg2 doesn't report a PostgreSQL
|
|
367
|
+
timestamp's precision, so it takes 6, PostgreSQL's maximum. That never understates it, and the other side's lower
|
|
368
|
+
precision still wins.
|
|
150
369
|
|
|
151
370
|
## How it works
|
|
152
371
|
|
|
@@ -158,6 +377,9 @@ An existing `Database` can be passed to `connect_to_table()` instead of a URI, s
|
|
|
158
377
|
4. **Download & compare**: only those small ranges are downloaded and compared in memory.
|
|
159
378
|
5. **Summarize**: `summarize()` pairs the differing rows by `key_columns` (default: the bisection columns).
|
|
160
379
|
|
|
380
|
+
Before that, once per table and only for columns without a declared `datatype`, one query that returns no rows
|
|
381
|
+
(`SELECT <columns> FROM t WHERE 1=0`) reads their types from the driver's result metadata.
|
|
382
|
+
|
|
161
383
|
For a table bisected on `(ORDER_ID, LINE_CODE)`, with columns `ORDER_ID NUMBER(12,0)` / `bigint`,
|
|
162
384
|
`LINE_CODE VARCHAR2(20)` / `varchar(20)`, `UPDATED_AT TIMESTAMP(6)` / `timestamp(6)` and
|
|
163
385
|
`UPDATED_BY VARCHAR2(30)` / `varchar(30)`, the checksum queries look like this. Oracle (database character set
|
|
@@ -194,25 +416,7 @@ WHERE ("order_id" >= 1405) AND (("line_code" COLLATE "C") >= 'A-001')
|
|
|
194
416
|
AND ("order_id" < 2662) AND (("line_code" COLLATE "C") < 'K-250')
|
|
195
417
|
```
|
|
196
418
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
`TableSegment` (or `connect_to_table`) takes:
|
|
200
|
-
|
|
201
|
-
| Argument | Meaning |
|
|
202
|
-
| :--- | :--- |
|
|
203
|
-
| `bisection_columns` | Columns that split the table into key ranges. Need not be unique, but should be selective and indexed: one value holding more rows than `bisection_threshold` can't be split and is downloaded whole (with a warning). Integers, decimals, timestamps, UUIDs and alphanumeric strings are supported. |
|
|
204
|
-
| `columns` | `{name: {"datatype": "<native type>", "transform_rule": "<sql>"}}` for every compared column, bisection columns included. `transform_rule` replaces the column (wrapped in parentheses); `datatype` is then the type of the expression's result. |
|
|
205
|
-
| `key_columns` | Columns identifying a logical row in the summary (default: `bisection_columns`). Used only to report differences; see [Keys and the per-key summary](#keys-and-the-per-key-summary). |
|
|
206
|
-
| `where` | Optional SQL filter added to every query, e.g. a time window. |
|
|
207
|
-
|
|
208
|
-
All problems with `columns` (missing datatype, unknown or unsupported type, bisection column not listed) are
|
|
209
|
-
reported together in one `ValueError`.
|
|
210
|
-
|
|
211
|
-
**Types.** The engine compares numbers, strings, timestamps/dates and booleans (hashed as `1`/`0`); `NULL` is
|
|
212
|
-
`'<null>'` on every side. LOB and binary types (`CLOB`, `BLOB`, `RAW`, `bytea`, …) are rejected on purpose: reduce
|
|
213
|
-
them with a `transform_rule` (e.g. a length). When the two sides declare different precisions, both are compared
|
|
214
|
-
at the lower one. An unscaled decimal (Oracle `NUMBER`, PostgreSQL `numeric`) is compared as a whole number, with a
|
|
215
|
-
warning; declare a scale on both sides (`NUMBER(12,2)` / `numeric(12,2)`) to compare decimals.
|
|
419
|
+
The only other query is Oracle's `NLS_CHARACTERSET` lookup, once per connection, unless `hash_charset` is given.
|
|
216
420
|
|
|
217
421
|
## Keys and the per-key summary
|
|
218
422
|
|
|
@@ -292,8 +496,8 @@ entries and `duplicate_keys` as the signal to look at the rows yourself.
|
|
|
292
496
|
|
|
293
497
|
## Background: from aggregate fingerprints to checksum bisection
|
|
294
498
|
|
|
295
|
-
diffdb-core is the third iteration of my migration data-validation tooling
|
|
296
|
-
for reference.
|
|
499
|
+
diffdb-core is the third iteration of my migration data-validation tooling, after `simple-dv-tool` and
|
|
500
|
+
`ibis-dv-tool`, and is built on reladiff's algorithm. The first two are archived and kept for reference.
|
|
297
501
|
|
|
298
502
|
### 1. simple-dv-tool: hand-written aggregate SQL
|
|
299
503
|
|
|
@@ -347,10 +551,11 @@ diffdb-core adapts reladiff's bisection engine (`HashDiffer`, segments, key-spac
|
|
|
347
551
|
|
|
348
552
|
| | reladiff | diffdb-core |
|
|
349
553
|
| :--- | :--- | :--- |
|
|
350
|
-
| Column definitions | column **names** (`key_columns`, `extra_columns`); types read from the database |
|
|
351
|
-
| Metadata queries | reads `information_schema` and samples rows to refine types |
|
|
554
|
+
| Column definitions | column **names** (`key_columns`, `extra_columns`); types read from the database | column names per side, paired by position; types from the result metadata of a zero-row query, or declared; optional `transform_rule` per column |
|
|
555
|
+
| Metadata queries | reads `information_schema` and samples rows to refine types | no catalog reads: one `WHERE 1=0` probe per table for columns without a declared type, plus Oracle `NLS_CHARACTERSET` once per connection |
|
|
352
556
|
| Compare an SQL expression | not supported (create a view) | `transform_rule` on any column, bisection columns included |
|
|
353
557
|
| LOB / binary / unknown types | compared anyway, with a "may result in false positives" warning | rejected up front; reduce them with a `transform_rule` |
|
|
558
|
+
| Types an engine can't name exactly | sampled / guessed | rejected with the fix (declare the type, or `CAST`), never compared at a guessed precision |
|
|
354
559
|
| 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
560
|
| Result | `+`/`-` row stream; `get_stats_dict()` counts | the same stream, plus `summarize()`: per-key, per-column results |
|
|
356
561
|
| 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` |
|
|
@@ -362,28 +567,19 @@ diffdb-core adapts reladiff's bisection engine (`HashDiffer`, segments, key-spac
|
|
|
362
567
|
### `transform_rule`: comparing expressions, not just columns
|
|
363
568
|
|
|
364
569
|
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
|
-
```
|
|
570
|
+
length, or a string key under binary collation, you have to create a view on each side first. In diffdb-core each
|
|
571
|
+
column entry can carry a `transform_rule` (see [Comparing expressions](#comparing-expressions-transform_rule)):
|
|
375
572
|
|
|
376
|
-
- The rule is
|
|
377
|
-
|
|
378
|
-
PostgreSQL query in [How it works](#how-it-works)).
|
|
573
|
+
- The rule is used **everywhere** the column appears, including the key-range `WHERE` clauses when it's a
|
|
574
|
+
bisection column (see the PostgreSQL query in [How it works](#how-it-works)).
|
|
379
575
|
- Each side has its own rule, so the two sides can use different SQL to reach the same value.
|
|
380
|
-
- The declared
|
|
381
|
-
|
|
576
|
+
- The result's type (declared, or reported by the database for the rule) drives normalization and precision
|
|
577
|
+
reconciliation, so the result is hashed as the expression's type, not the column's original type.
|
|
382
578
|
- This is how unsupported types are handled: LOB and binary columns are rejected unless a rule reduces them to a
|
|
383
579
|
number, string or timestamp. reladiff instead hashes them with a warning, which can report false differences.
|
|
384
580
|
|
|
385
581
|
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
|
|
582
|
+
applied inside the hash, on both sides, with a known type.
|
|
387
583
|
|
|
388
584
|
### YAML dialect declarations
|
|
389
585
|
|
|
@@ -401,12 +597,12 @@ diffdb-core declares each dialect in one YAML file (`src/databases/dialects/<db>
|
|
|
401
597
|
- **Validated when it loads**: unknown keys, unknown type classes and unknown template placeholders raise
|
|
402
598
|
`DialectConfigError`, rather than producing wrong SQL at run time.
|
|
403
599
|
- Engine-specific behaviour lives in YAML, not in `if name == ...` branches. The only Python per database is a small
|
|
404
|
-
driver class (import and
|
|
600
|
+
driver class (import, connection and, optionally, naming the types in the driver's result metadata).
|
|
405
601
|
- Every dialect's generated SQL is pinned by golden snapshots (`tests/golden/`), so a YAML change that alters a
|
|
406
602
|
query shows up as a test diff.
|
|
407
603
|
|
|
408
|
-
|
|
409
|
-
`type_classes
|
|
604
|
+
A column's native type (reported by the database, or declared) is turned into a diffdb type by the dialect's
|
|
605
|
+
`type_classes`. That replaces reladiff's `information_schema` query and row sampling.
|
|
410
606
|
|
|
411
607
|
(This YAML is a dialect definition. It is different from reladiff's TOML file, which describes runs, and from
|
|
412
608
|
ibis-dv-tool's YAML, which described the table mapping. diffdb-core has no run-config file.)
|
|
@@ -440,7 +636,8 @@ tables into memory: only the rows of mismatching segments are ever downloaded.
|
|
|
440
636
|
fragments, type mapping and normalization templates. `_base.yaml` documents every key; unknown keys, type
|
|
441
637
|
classes and template placeholders are rejected when the dialect loads.
|
|
442
638
|
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`).
|
|
639
|
+
`DATABASE_BY_SCHEME` (`src/databases/_connect.py`). Optionally a `_native_type()` hook that names the type of
|
|
640
|
+
a `cursor.description` entry, so `datatype` can be left out on that database.
|
|
444
641
|
|
|
445
642
|
The new dialect must render every value byte-identically to its counterpart. See
|
|
446
643
|
[ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#4-declarative-yaml-dialect-framework).
|