diffdb-core 0.1.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.
Files changed (66) hide show
  1. diffdb_core-0.1.0/LICENSE.md +87 -0
  2. diffdb_core-0.1.0/MANIFEST.in +1 -0
  3. diffdb_core-0.1.0/PKG-INFO +315 -0
  4. diffdb_core-0.1.0/README.md +266 -0
  5. diffdb_core-0.1.0/pyproject.toml +71 -0
  6. diffdb_core-0.1.0/setup.cfg +4 -0
  7. diffdb_core-0.1.0/src/diffdb/__init__.py +179 -0
  8. diffdb_core-0.1.0/src/diffdb/concurrency/__init__.py +10 -0
  9. diffdb_core-0.1.0/src/diffdb/concurrency/pool.py +88 -0
  10. diffdb_core-0.1.0/src/diffdb/concurrency/stream.py +111 -0
  11. diffdb_core-0.1.0/src/diffdb/core/__init__.py +38 -0
  12. diffdb_core-0.1.0/src/diffdb/core/differ.py +136 -0
  13. diffdb_core-0.1.0/src/diffdb/core/hash_differ.py +390 -0
  14. diffdb_core-0.1.0/src/diffdb/core/segment.py +323 -0
  15. diffdb_core-0.1.0/src/diffdb/core/sql.py +81 -0
  16. diffdb_core-0.1.0/src/diffdb/core/summary.py +179 -0
  17. diffdb_core-0.1.0/src/diffdb/databases/__init__.py +59 -0
  18. diffdb_core-0.1.0/src/diffdb/databases/_connect.py +210 -0
  19. diffdb_core-0.1.0/src/diffdb/databases/base.py +266 -0
  20. diffdb_core-0.1.0/src/diffdb/databases/bigquery.py +54 -0
  21. diffdb_core-0.1.0/src/diffdb/databases/clickhouse.py +35 -0
  22. diffdb_core-0.1.0/src/diffdb/databases/databricks.py +40 -0
  23. diffdb_core-0.1.0/src/diffdb/databases/dialects/_base.yaml +182 -0
  24. diffdb_core-0.1.0/src/diffdb/databases/dialects/bigquery.yaml +39 -0
  25. diffdb_core-0.1.0/src/diffdb/databases/dialects/clickhouse.yaml +42 -0
  26. diffdb_core-0.1.0/src/diffdb/databases/dialects/databricks.yaml +36 -0
  27. diffdb_core-0.1.0/src/diffdb/databases/dialects/duckdb.yaml +48 -0
  28. diffdb_core-0.1.0/src/diffdb/databases/dialects/mysql.yaml +45 -0
  29. diffdb_core-0.1.0/src/diffdb/databases/dialects/oracle.yaml +44 -0
  30. diffdb_core-0.1.0/src/diffdb/databases/dialects/postgresql.yaml +44 -0
  31. diffdb_core-0.1.0/src/diffdb/databases/dialects/presto.yaml +37 -0
  32. diffdb_core-0.1.0/src/diffdb/databases/dialects/redshift.yaml +11 -0
  33. diffdb_core-0.1.0/src/diffdb/databases/dialects/snowflake.yaml +55 -0
  34. diffdb_core-0.1.0/src/diffdb/databases/dialects/trino.yaml +5 -0
  35. diffdb_core-0.1.0/src/diffdb/databases/dialects/vertica.yaml +39 -0
  36. diffdb_core-0.1.0/src/diffdb/databases/duckdb.py +40 -0
  37. diffdb_core-0.1.0/src/diffdb/databases/mysql.py +38 -0
  38. diffdb_core-0.1.0/src/diffdb/databases/oracle.py +99 -0
  39. diffdb_core-0.1.0/src/diffdb/databases/postgresql.py +34 -0
  40. diffdb_core-0.1.0/src/diffdb/databases/presto.py +41 -0
  41. diffdb_core-0.1.0/src/diffdb/databases/redshift.py +12 -0
  42. diffdb_core-0.1.0/src/diffdb/databases/snowflake.py +55 -0
  43. diffdb_core-0.1.0/src/diffdb/databases/trino.py +41 -0
  44. diffdb_core-0.1.0/src/diffdb/databases/vertica.py +43 -0
  45. diffdb_core-0.1.0/src/diffdb/databases/yaml_dialect.py +428 -0
  46. diffdb_core-0.1.0/src/diffdb/geometry/__init__.py +42 -0
  47. diffdb_core-0.1.0/src/diffdb/geometry/arithmetic.py +141 -0
  48. diffdb_core-0.1.0/src/diffdb/geometry/bisection.py +100 -0
  49. diffdb_core-0.1.0/src/diffdb/geometry/vector.py +42 -0
  50. diffdb_core-0.1.0/src/diffdb/telemetry/__init__.py +11 -0
  51. diffdb_core-0.1.0/src/diffdb/telemetry/info_tree.py +52 -0
  52. diffdb_core-0.1.0/src/diffdb/telemetry/query_tracker.py +53 -0
  53. diffdb_core-0.1.0/src/diffdb/types.py +302 -0
  54. diffdb_core-0.1.0/src/diffdb/utils.py +121 -0
  55. diffdb_core-0.1.0/src/diffdb_core.egg-info/PKG-INFO +315 -0
  56. diffdb_core-0.1.0/src/diffdb_core.egg-info/SOURCES.txt +64 -0
  57. diffdb_core-0.1.0/src/diffdb_core.egg-info/dependency_links.txt +1 -0
  58. diffdb_core-0.1.0/src/diffdb_core.egg-info/requires.txt +39 -0
  59. diffdb_core-0.1.0/src/diffdb_core.egg-info/top_level.txt +1 -0
  60. diffdb_core-0.1.0/tests/golden/dialect_sql.json +302 -0
  61. diffdb_core-0.1.0/tests/golden/query_sql.json +860 -0
  62. diffdb_core-0.1.0/tests/test_connect.py +69 -0
  63. diffdb_core-0.1.0/tests/test_dialects.py +439 -0
  64. diffdb_core-0.1.0/tests/test_diffdb_core.py +596 -0
  65. diffdb_core-0.1.0/tests/test_e2e_duckdb.py +181 -0
  66. diffdb_core-0.1.0/tests/test_query_snapshot.py +156 -0
@@ -0,0 +1,87 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sarath Mohan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ ## Third-Party Software Notices & Attributions
26
+
27
+ `diffdb` incorporates, adapts, or derives code from the following open-source projects under the permissive **MIT License**:
28
+
29
+ ### 1. `reladiff`
30
+ - **Source**: https://github.com/erezsh/reladiff
31
+ - **Copyright**: Copyright (c) 2024 Erez Shinan
32
+ - **License**: MIT License
33
+
34
+ ```text
35
+ MIT License
36
+
37
+ Copyright (c) 2024 Erez Shinan
38
+
39
+ Permission is hereby granted, free of charge, to any person obtaining a copy
40
+ of this software and associated documentation files (the "Software"), to deal
41
+ in the Software without restriction, including without limitation the rights
42
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43
+ copies of the Software, and to permit persons to whom the Software is
44
+ furnished to do so, subject to the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be included in all
47
+ copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55
+ SOFTWARE.
56
+ ```
57
+
58
+ ---
59
+
60
+ ### 2. `sqeleton`
61
+ - **Source**: https://github.com/erezsh/sqeleton
62
+ - **Copyright**: Copyright (c) 2023 Erez Shinan
63
+ - **License**: MIT License
64
+
65
+ ```text
66
+ MIT License
67
+
68
+ Copyright (c) 2023 Erez Shinan
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining a copy
71
+ of this software and associated documentation files (the "Software"), to deal
72
+ in the Software without restriction, including without limitation the rights
73
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
74
+ copies of the Software, and to permit persons to whom the Software is
75
+ furnished to do so, subject to the following conditions:
76
+
77
+ The above copyright notice and this permission notice shall be included in all
78
+ copies or substantial portions of the Software.
79
+
80
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
83
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
85
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
86
+ SOFTWARE.
87
+ ```
@@ -0,0 +1 @@
1
+ recursive-include tests/golden *.json
@@ -0,0 +1,315 @@
1
+ Metadata-Version: 2.4
2
+ Name: diffdb-core
3
+ Version: 0.1.0
4
+ Summary: Cross-database table diffing by recursive checksum bisection, with declarative YAML SQL dialects
5
+ Author: Sarath Mohan
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/sarath-mec/diffdb-core
8
+ Project-URL: Documentation, https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md
9
+ Project-URL: Issues, https://github.com/sarath-mec/diffdb-core/issues
10
+ Keywords: database,diff,data-validation,migration,checksum,oracle,postgresql
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Database
18
+ Requires-Python: >=3.12
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE.md
21
+ Requires-Dist: dsnparse>=0.2.0
22
+ Requires-Dist: pyyaml>=6.0
23
+ Provides-Extra: oracle
24
+ Requires-Dist: oracledb>=2.0.0; extra == "oracle"
25
+ Provides-Extra: postgresql
26
+ Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgresql"
27
+ Provides-Extra: redshift
28
+ Requires-Dist: psycopg2-binary>=2.9.0; extra == "redshift"
29
+ Provides-Extra: mysql
30
+ Requires-Dist: pymysql>=1.0.0; extra == "mysql"
31
+ Provides-Extra: snowflake
32
+ Requires-Dist: snowflake-connector-python>=3.0.0; extra == "snowflake"
33
+ Requires-Dist: cryptography>=41.0.0; extra == "snowflake"
34
+ Provides-Extra: bigquery
35
+ Requires-Dist: google-cloud-bigquery>=3.0.0; extra == "bigquery"
36
+ Provides-Extra: databricks
37
+ Requires-Dist: databricks-sql-connector>=2.0.0; extra == "databricks"
38
+ Provides-Extra: duckdb
39
+ Requires-Dist: duckdb>=0.9.0; extra == "duckdb"
40
+ Provides-Extra: trino
41
+ Requires-Dist: trino>=0.320.0; extra == "trino"
42
+ Provides-Extra: presto
43
+ Requires-Dist: presto-python-client>=0.8.0; extra == "presto"
44
+ Provides-Extra: clickhouse
45
+ Requires-Dist: clickhouse-driver>=0.2.0; extra == "clickhouse"
46
+ Provides-Extra: vertica
47
+ Requires-Dist: vertica-python>=1.0.0; extra == "vertica"
48
+ Dynamic: license-file
49
+
50
+ # diffdb-core
51
+
52
+ Compare tables across different databases without moving the data. `diffdb` pushes a
53
+ `count(*)` + checksum query for each key range down to both databases, and recursively splits only the ranges
54
+ whose checksums differ. Rows are downloaded only for the small segments that actually contain differences,
55
+ then paired by key into a per-row, per-column summary.
56
+
57
+ Built for validating database migrations (e.g. Oracle → PostgreSQL), where the two sides run on different
58
+ engines and a `FULL OUTER JOIN` isn't possible.
59
+
60
+ - **12 databases** through declarative YAML dialects: PostgreSQL, Oracle, MySQL, Snowflake, BigQuery, Redshift,
61
+ Databricks, DuckDB, Trino, Presto, ClickHouse, Vertica.
62
+ - **Any pair**: the source and target can be different engines; each side renders values to the same text before hashing.
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.
65
+ - **Non-unique keys** work: bisection columns need only be selective, and duplicate rows are counted.
66
+ - **Per-key summary**: changed rows with their differing columns, source-only and target-only rows, duplicate keys.
67
+ - **Telemetry**: every query, row count, checksum and duration can be captured for auditing.
68
+
69
+ ## Install
70
+
71
+ ```bash
72
+ pip install "diffdb-core[postgresql,oracle]" # pick the extras for your databases
73
+ ```
74
+
75
+ Extras: `oracle`, `postgresql`, `redshift`, `mysql`, `snowflake`, `bigquery`, `databricks`, `duckdb`, `trino`,
76
+ `presto`, `clickhouse`, `vertica`. The package is imported as `diffdb`. Python ≥ 3.12.
77
+
78
+ ## Quick start
79
+
80
+ ```python
81
+ from diffdb import connect_to_table, diff_tables
82
+
83
+ # Every compared column with its native datatype. Source and target are paired by position,
84
+ # so both dicts list the columns in the same order (bisection columns first).
85
+ src = connect_to_table(
86
+ "oracle://app_user:pwd@oracle-host:1521/ORCL",
87
+ "SALES.CUSTOMER",
88
+ bisection_columns=("CUSTOMER_ID",),
89
+ columns={
90
+ "CUSTOMER_ID": {"datatype": "NUMBER(10,0)"},
91
+ "EMAIL": {"datatype": "VARCHAR2(200)"},
92
+ "BALANCE": {"datatype": "NUMBER(12,2)"},
93
+ "NOTES": {"datatype": "NUMBER(10,0)", "transform_rule": 'DBMS_LOB.GETLENGTH("NOTES")'}, # CLOB -> length
94
+ },
95
+ )
96
+ tgt = connect_to_table(
97
+ "postgresql://app_user:pwd@pg-host:5432/sales",
98
+ "sales.customer",
99
+ bisection_columns=("customer_id",),
100
+ columns={
101
+ "customer_id": {"datatype": "bigint"},
102
+ "email": {"datatype": "varchar(200)"},
103
+ "balance": {"datatype": "numeric(12,2)"},
104
+ "notes": {"datatype": "integer", "transform_rule": 'LENGTH("notes")'},
105
+ },
106
+ )
107
+
108
+ diff = diff_tables(src, tgt)
109
+ try:
110
+ summary = diff.summarize(limit=1000) # stop after 1000 differing keys
111
+ finally:
112
+ diff.close()
113
+
114
+ print(summary.changed, summary.source_only, summary.target_only, summary.limit_reached)
115
+ for kd in summary.key_diffs: # kind: changed | source_only | target_only | duplicate_key
116
+ if kd.kind == "changed":
117
+ for cd in kd.column_diffs:
118
+ print(kd.key, cd.target_column, cd.source_value, "->", cd.target_value)
119
+ else: # source_only | target_only | duplicate_key: the unpaired rows
120
+ print(kd.key, kd.kind, kd.source_rows, kd.target_rows)
121
+ ```
122
+
123
+ Rows are paired into `changed` results by `key_columns`, which default to the bisection columns; see
124
+ [Keys and the per-key summary](#keys-and-the-per-key-summary).
125
+
126
+ You can also iterate the raw stream (`for sign, row in diff_tables(...)`, with `-` = source only and `+` = target
127
+ only), get counts with `get_stats_dict()`, and capture every query with `track_queries=True, return_queries=True`.
128
+ See [ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#6-python-api-reference--practical-recipes) for these recipes.
129
+
130
+ ### Connections
131
+
132
+ `connect()` / `connect_to_table()` take a URI (`postgresql://user:pwd@host:5432/db`) or a dict whose `driver` is
133
+ one of the schemes above and whose other keys go to that database's Python driver:
134
+
135
+ ```python
136
+ import diffdb
137
+
138
+ pg = diffdb.connect({"driver": "postgresql", "host": "pg-host", "port": 5432, "database": "sales",
139
+ "user": "app_user", "password": "..."})
140
+ ora = diffdb.connect({"driver": "oracle", "user": "app_user", "password": "...",
141
+ "dsn": "oracle-host:1521/ORCLPDB1"}, thread_count=4)
142
+ ```
143
+
144
+ An existing `Database` can be passed to `connect_to_table()` instead of a URI, so one connection pool serves many tables.
145
+
146
+ ## How it works
147
+
148
+ 1. **Divide**: the key space of `bisection_columns` (from `min`/`max`) is split into `bisection_factor` ranges (default 32).
149
+ 2. **Checksum**: for each range, both databases compute `count(*)` and the sum of a 60-bit integer taken from the MD5
150
+ of each row, where the row is its normalized columns joined with `'|'`.
151
+ 3. **Recurse**: ranges with equal counts and checksums are done. The others are split again until they hold fewer
152
+ rows than `bisection_threshold` (default 16,384).
153
+ 4. **Download & compare**: only those small ranges are downloaded and compared in memory.
154
+ 5. **Summarize**: `summarize()` pairs the differing rows by `key_columns` (default: the bisection columns).
155
+
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
158
+ `NLS_CHARACTERSET` or forced with a `hash_charset` connection setting):
159
+
160
+ ```sql
161
+ SELECT
162
+ count(*),
163
+ 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>')
168
+ ), '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')
172
+ ```
173
+
174
+ PostgreSQL (with a `COLLATE "C"` transform rule on the string bisection column, see Limitations):
175
+
176
+ ```sql
177
+ SELECT
178
+ count(*),
179
+ 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>')
184
+ )), 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')
188
+ ```
189
+
190
+ ## Defining columns
191
+
192
+ `TableSegment` (or `connect_to_table`) takes:
193
+
194
+ | Argument | Meaning |
195
+ | :--- | :--- |
196
+ | `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. |
197
+ | `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. |
198
+ | `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). |
199
+ | `where` | Optional SQL filter added to every query, e.g. a time window. |
200
+
201
+ All problems with `columns` (missing datatype, unknown or unsupported type, bisection column not listed) are
202
+ reported together in one `ValueError`.
203
+
204
+ **Types.** The engine compares numbers, strings, timestamps/dates and booleans (hashed as `1`/`0`); `NULL` is
205
+ `'<null>'` on every side. LOB and binary types (`CLOB`, `BLOB`, `RAW`, `bytea`, …) are rejected on purpose: reduce
206
+ them with a `transform_rule` (e.g. a length). When the two sides declare different precisions, both are compared
207
+ at the lower one. An unscaled decimal (Oracle `NUMBER`, PostgreSQL `numeric`) is compared as a whole number, with a
208
+ warning; declare a scale on both sides (`NUMBER(12,2)` / `numeric(12,2)`) to compare decimals.
209
+
210
+ ## Keys and the per-key summary
211
+
212
+ The diff itself works on whole rows: a range whose checksums differ is downloaded, and every row that isn't
213
+ present on the other side, value for value, comes out as `('-', row)` (source only) or `('+', row)` (target only).
214
+ `key_columns` decide how `summarize()` and `get_stats_dict()` turn those rows into results. They are never sent to
215
+ the database and don't change which rows are compared.
216
+
217
+ - **Bisection columns vs key columns**: `bisection_columns` split the table into ranges (they should be indexed);
218
+ `key_columns` say which rows are "the same row" (usually the primary key). The default is the bisection columns.
219
+ - **Positions, not names**: the two sides are paired by column position, so the key columns must sit at the same
220
+ positions in both `columns` dicts; `diff_tables` checks this before running any query.
221
+ - **Not unique**: keys may repeat. Nothing in the database is checked.
222
+ - **Include every bisection column** in the key when you can. Both versions of a key then land in the same range, so
223
+ pairing stays exact even when `summarize(limit=N)` stops the run early.
224
+
225
+ ### How rows become `key_diffs`
226
+
227
+ All differing rows are grouped by key. Rows that appear identically on both sides cancel out first, so only the
228
+ leftover rows of a key decide its `kind`:
229
+
230
+ | Leftover rows for a key | `kind` | What the `KeyDiff` holds | Counted in |
231
+ | :--- | :--- | :--- | :--- |
232
+ | 1 source, 1 target | `changed` | `column_diffs`: the differing columns, with both sides' names and values | `changed` (1) |
233
+ | source only (1 or more) | `source_only` | `source_rows` | `source_only` (one per row) |
234
+ | target only (1 or more) | `target_only` | `target_rows` | `target_only` (one per row) |
235
+ | both sides, not exactly 1 and 1 (e.g. 2 source, 1 target) | `duplicate_key` | `source_rows` and `target_rows`, unpaired, no `column_diffs` | `source_only` and `target_only` (one per row) |
236
+
237
+ `summary.duplicate_keys` is `True` when any key had more than one leftover row on a side. Rows hold the values as
238
+ downloaded (normalized strings), in `columns` order with the bisection columns first.
239
+
240
+ For example, with `key_columns=("ORDER_ID", "LINE_NO")` these differing rows:
241
+
242
+ ```text
243
+ - (1, 1, QTY 5) + (1, 1, QTY 6) one row each side
244
+ - (2, 1, QTY 3) source only
245
+ + (3, 1, QTY 9) target only
246
+ - (4, 1, QTY 2) + (4, 1, QTY 8) key 4 is duplicated in the source:
247
+ - (4, 1, QTY 7) 2 source rows, 1 target row
248
+ ```
249
+
250
+ summarize to (`summary.as_dict()["key_diffs"]`, abridged):
251
+
252
+ ```text
253
+ key ["1","1"] changed column_diffs [QTY/qty: "5" -> "6"]
254
+ key ["2","1"] source_only source_rows [["2","1","3"]]
255
+ key ["3","1"] target_only target_rows [["3","1","9"]]
256
+ key ["4","1"] duplicate_key source_rows [["4","1","2"], ["4","1","7"]] target_rows [["4","1","8"]]
257
+
258
+ counts: changed 1, source_only 3, target_only 2; duplicate_keys: true
259
+ ```
260
+
261
+ Duplicates often look like ordinary differences, because matching copies cancel:
262
+
263
+ | Source rows for a key | Target rows | Reported as |
264
+ | :--- | :--- | :--- |
265
+ | `A`, `A` | `A`, `A` | nothing (identical) |
266
+ | `A`, `A` | `A` | `source_only` with one row `A` (the extra copy); `duplicate_keys` stays `False` |
267
+ | `A`, `B` | `A`, `C` | `changed`, `B` → `C` (the `A` copies cancel) |
268
+ | `B`, `C` | `D` | `duplicate_key`: no way to tell which source row became `D` |
269
+
270
+ Use a key that is unique in your data when you need every difference to be a `changed` row. Use `duplicate_key`
271
+ entries and `duplicate_keys` as the signal to look at the rows yourself.
272
+
273
+ ## Known limitations
274
+
275
+ - **String collation**: Oracle sorts strings by bytes, PostgreSQL by its collation by default, so the same key
276
+ range selects different rows. Add `COLLATE "C"` as the PostgreSQL `transform_rule` of string bisection columns.
277
+ That bypasses the default index (create one with `COLLATE "C"`), so prefer numeric bisection columns.
278
+ - **Alphanumeric keys**: string bisection keys may only contain `space - . 0-9 A-Z _ a-z`
279
+ (`diffdb/geometry/arithmetic.py`). Bisect on another column when keys contain other characters.
280
+ - **Oracle 4000-byte limit**: the row string must fit in a `VARCHAR2` (`ORA-01489` otherwise). Compare very wide
281
+ text columns through a transform rule such as `LENGTH(col)`.
282
+ - **Live data**: rows changing during a run show up as differences. Restrict `where` to a stable window.
283
+ - **Character sets**: values both drivers decode to the same Unicode text compare equal at row level, but a
284
+ checksum mismatch still costs a download of that segment.
285
+
286
+ ## Adding a database
287
+
288
+ 1. A YAML dialect in `src/diffdb/databases/dialects/<db>.yaml` that `extends: _base.yaml` and declares its SQL
289
+ fragments, type mapping and normalization templates. `_base.yaml` documents every key; unknown keys, type
290
+ 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`).
293
+
294
+ The new dialect must render every value byte-identically to its counterpart. See
295
+ [ARCHITECTURE.md](https://github.com/sarath-mec/diffdb-core/blob/main/ARCHITECTURE.md#4-declarative-yaml-dialect-framework).
296
+
297
+ ## Development
298
+
299
+ ```bash
300
+ uv sync # installs the package with pytest, ruff and duckdb
301
+ uv run pytest # no external database needed
302
+ uv run ruff check .
303
+ ```
304
+
305
+ The tests include unit tests, a DuckDB end-to-end diff with planted differences, and golden snapshots of the SQL
306
+ every dialect generates (`tests/golden/`). After an intended dialect or SQL change, regenerate and review them:
307
+
308
+ ```bash
309
+ DIFFDB_UPDATE_GOLDEN=1 uv run python -m unittest tests.test_dialects tests.test_query_snapshot
310
+ ```
311
+
312
+ ## License
313
+
314
+ MIT. `diffdb` adapts code from [reladiff](https://github.com/erezsh/reladiff) and
315
+ [sqeleton](https://github.com/erezsh/sqeleton) (both MIT, by Erez Shinan); see [LICENSE.md](https://github.com/sarath-mec/diffdb-core/blob/main/LICENSE.md).