joinforecast 0.2.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.
- joinforecast-0.2.2/LICENSE +21 -0
- joinforecast-0.2.2/PKG-INFO +287 -0
- joinforecast-0.2.2/README.md +272 -0
- joinforecast-0.2.2/pyproject.toml +40 -0
- joinforecast-0.2.2/setup.cfg +4 -0
- joinforecast-0.2.2/src/joinforecast/__init__.py +231 -0
- joinforecast-0.2.2/src/joinforecast/__main__.py +147 -0
- joinforecast-0.2.2/src/joinforecast/disk.py +142 -0
- joinforecast-0.2.2/src/joinforecast.egg-info/PKG-INFO +287 -0
- joinforecast-0.2.2/src/joinforecast.egg-info/SOURCES.txt +17 -0
- joinforecast-0.2.2/src/joinforecast.egg-info/dependency_links.txt +1 -0
- joinforecast-0.2.2/src/joinforecast.egg-info/entry_points.txt +2 -0
- joinforecast-0.2.2/src/joinforecast.egg-info/top_level.txt +1 -0
- joinforecast-0.2.2/tests/test_console_output.py +50 -0
- joinforecast-0.2.2/tests/test_disk.py +282 -0
- joinforecast-0.2.2/tests/test_fused.py +268 -0
- joinforecast-0.2.2/tests/test_joinforecast.py +315 -0
- joinforecast-0.2.2/tests/test_output.py +162 -0
- joinforecast-0.2.2/tests/test_validation_sweep.py +116 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anish Pentyala
|
|
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.
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: joinforecast
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Offline, exact CSV equality-join row-count preflight
|
|
5
|
+
Author: Anish Pentyala
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/anishpentyala/joinforecast
|
|
8
|
+
Project-URL: Repository, https://github.com/anishpentyala/joinforecast
|
|
9
|
+
Project-URL: Issues, https://github.com/anishpentyala/joinforecast/issues
|
|
10
|
+
Keywords: csv,join,data-quality,etl
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# joinforecast
|
|
17
|
+
|
|
18
|
+
Count CSV join rows before a many-to-many merge eats your memory.
|
|
19
|
+
|
|
20
|
+
[](https://github.com/anishpentyala/joinforecast/actions)
|
|
21
|
+
|
|
22
|
+
## Quickstart
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
python -m pip install "git+https://github.com/anishpentyala/joinforecast.git"
|
|
26
|
+
printf 'customer_id\n001\n001\n002\n' > orders.csv
|
|
27
|
+
printf 'customer_id\n001\n001\n003\n' > customers.csv
|
|
28
|
+
joinforecast orders.csv customers.csv --left-key customer_id
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This prints a full JSON report. Selected fields for these exact files:
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{"predicted_rows": {"inner": 4, "left": 5, "right": 5, "full": 6, "left_anti": 1, "right_anti": 1}}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The four matching rows come from two orders times two customer records for `001`.
|
|
38
|
+
No join is built, and neither input is changed. Add `--expect many-to-one` to make
|
|
39
|
+
repeated lookup keys fail the contract (exit 1, with a complete report).
|
|
40
|
+
|
|
41
|
+
Installation is currently from source. A PyPI release is being prepared; do not
|
|
42
|
+
assume `pip install joinforecast` is available until its package page is linked here.
|
|
43
|
+
|
|
44
|
+
## Details
|
|
45
|
+
|
|
46
|
+
Find a many-to-many row explosion before a CSV equality join, without building
|
|
47
|
+
that join or uploading either file.
|
|
48
|
+
|
|
49
|
+
`joinforecast` counts key frequencies and reports exact inner, left, right, full,
|
|
50
|
+
and anti-join row counts. It shows the largest matched-key contributions,
|
|
51
|
+
unmatched input rows, duplicate joinable keys, and optional contract violations.
|
|
52
|
+
It uses only Python's standard library. Python 3.9 or later is required.
|
|
53
|
+
|
|
54
|
+
## Install from source
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
git clone https://github.com/anishpentyala/joinforecast.git
|
|
58
|
+
cd joinforecast
|
|
59
|
+
python -m pip install .
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This is a source-installable tool, not a claimed PyPI release.
|
|
63
|
+
|
|
64
|
+
## Catch a broken lookup
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
joinforecast examples/orders.csv examples/customers.csv \
|
|
68
|
+
--left-key customer_id --expect many-to-one --require-match left
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The example has two orders for `001` and two customer records for `001`.
|
|
72
|
+
Their key alone contributes **four** output rows. With the default null policy,
|
|
73
|
+
counts are inner **5**, left **7**, right **7**, and full **9**. Two orders have
|
|
74
|
+
no match: customer `003` and the empty customer ID. The command exits **1** because
|
|
75
|
+
the right keys are not unique and some left rows have no match. The report is
|
|
76
|
+
still complete, machine-readable JSON.
|
|
77
|
+
|
|
78
|
+
Without contracts, a valid forecast exits 0, even when it finds duplicates.
|
|
79
|
+
This is a preflight, not a declaration that a join is safe for your analysis.
|
|
80
|
+
|
|
81
|
+
## Specify the key and null semantics
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
# Composite key; map differently named columns in order.
|
|
85
|
+
joinforecast left.csv right.csv \
|
|
86
|
+
--left-key account --left-key period \
|
|
87
|
+
--right-key customer --right-key month --nulls never
|
|
88
|
+
|
|
89
|
+
# Matching null components; explicitly recognize empty fields and NA as null.
|
|
90
|
+
joinforecast left.csv right.csv --left-key id \
|
|
91
|
+
--nulls match --null-value '' --null-value NA
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- Keys are **exact CSV strings**. `001`, `1`, ` 1`, and `1.0` stay different.
|
|
95
|
+
No numeric conversion, trimming, case folding, locale rules, or date parsing.
|
|
96
|
+
- Only empty fields are null by default. `--null-value` can be repeated and
|
|
97
|
+
**replaces**, rather than extends, that default. `--null-value NA` makes
|
|
98
|
+
an empty string an ordinary key. The library can use `null_values=()` for
|
|
99
|
+
no null tokens.
|
|
100
|
+
- `--nulls never` is SQL equality behavior: any null component prevents a match.
|
|
101
|
+
- `--nulls match` matches nulls at corresponding key positions. This models
|
|
102
|
+
null equality, **not a complete pandas CSV-loader simulation**. Set tokens and
|
|
103
|
+
types to match the loader you actually use. pandas warns that its null-key
|
|
104
|
+
matches differ from SQL: [pandas.merge documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge.html).
|
|
105
|
+
- UTF-8 and UTF-8 BOM input only. Comma delimiter by default; `--delimiter ';'`
|
|
106
|
+
is available. CSV quoting and multiline fields are supported.
|
|
107
|
+
- Blank/duplicate header names, missing key columns, ragged rows (including
|
|
108
|
+
blank data records), malformed quoting, and invalid UTF-8 are errors.
|
|
109
|
+
A header-only file is valid and has zero data rows.
|
|
110
|
+
|
|
111
|
+
No missing rows are silently dropped, sampled, or counted as success.
|
|
112
|
+
|
|
113
|
+
## Contracts and resource limits
|
|
114
|
+
|
|
115
|
+
| Option | Meaning |
|
|
116
|
+
|---|---|
|
|
117
|
+
| `--expect one-to-one` | Both sides' joinable keys must be unique |
|
|
118
|
+
| `--expect one-to-many` | Left joinable keys must be unique |
|
|
119
|
+
| `--expect many-to-one` | Right joinable keys must be unique |
|
|
120
|
+
| `--expect many-to-many` | No uniqueness constraint |
|
|
121
|
+
| `--require-match left/right/both` | All rows on those sides must have a match |
|
|
122
|
+
| `--max-output-rows N` | Each of the four materializing join forecasts must be at most N |
|
|
123
|
+
| `--max-input-rows N` | Per-input row cap, default 10,000,000 |
|
|
124
|
+
| `--max-keys N` | Per-input distinct-key cap, default 1,000,000 |
|
|
125
|
+
| `--top N` | Return at most N matched-key contributors, default 10 |
|
|
126
|
+
|
|
127
|
+
Contracts check all joinable keys, even unmatched ones. Under `never`, null keys
|
|
128
|
+
are excluded from uniqueness checks, but still count as unmatched rows. Under
|
|
129
|
+
`match`, null keys participate normally. This is an explicit contract definition,
|
|
130
|
+
not a promise to duplicate a particular database's uniqueness constraint rules.
|
|
131
|
+
|
|
132
|
+
Input limits stop with exit 2 and **no JSON forecast**, rather than returning a
|
|
133
|
+
plausible partial count. They are safety bounds, not hard RAM guarantees: memory
|
|
134
|
+
is proportional to distinct keys and their lengths. One huge field is also
|
|
135
|
+
subject to Python's CSV parser field-size limit. Output row arithmetic uses
|
|
136
|
+
Python integers, with no floating-point rounding or output-row materialization.
|
|
137
|
+
|
|
138
|
+
Exit codes: **0** complete forecast satisfying requested contracts; **1** complete
|
|
139
|
+
forecast with contract violations; **2** invalid/incomplete input, bad options,
|
|
140
|
+
or output-delivery failure. A successful report write must accept the complete
|
|
141
|
+
text and flush before a normal verdict is returned. Help, version and argument
|
|
142
|
+
messages use the same checked output path. If output fails, partial bytes may
|
|
143
|
+
already have escaped; discard them, since they are not a delivered forecast.
|
|
144
|
+
Diagnostics are best effort if stderr fails too. Embedded `main(argv)` leaves
|
|
145
|
+
caller streams open and unchanged. The executable wrappers own shutdown and
|
|
146
|
+
avoid retrying failed flushes that would otherwise turn exit 2 into exit 120.
|
|
147
|
+
JSON `schema_version` is 1. `ok` means only that the requested contracts passed.
|
|
148
|
+
|
|
149
|
+
## Opt-in disk-backed key frequencies
|
|
150
|
+
|
|
151
|
+
The default backend keeps key frequencies in memory. If the distinct-key index
|
|
152
|
+
is too large for that, use a private temporary SQLite database instead:
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
joinforecast large-left.csv large-right.csv --left-key id \
|
|
156
|
+
--storage sqlite --max-keys 5000000 --temp-dir /path/to/private/scratch
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
This uses Python's `sqlite3` module, not a database server. It writes exact key
|
|
160
|
+
values and their frequencies to temporary disk. Use a suitable scratch location
|
|
161
|
+
for sensitive data. The directory is private to the current user; normal success
|
|
162
|
+
and errors close and remove both databases. Removal is **not secure erasure**.
|
|
163
|
+
Forced termination or power loss can leave files behind. Other users with elevated
|
|
164
|
+
access, disk backups, and filesystem snapshots may still expose the data.
|
|
165
|
+
|
|
166
|
+
`--temp-dir` selects an existing parent directory, not a database filename. Omit
|
|
167
|
+
it to use Python's normal temporary location. It requires `--storage sqlite`.
|
|
168
|
+
An unwritable, missing, or full temporary location fails with exit 2 and no JSON
|
|
169
|
+
forecast. Neither CSV input is modified. The SQLite backend needs a Python build
|
|
170
|
+
with `sqlite3` support; the default memory backend does not.
|
|
171
|
+
|
|
172
|
+
Counts, null rules, top-contributor tie order, contracts, and input limits stay the
|
|
173
|
+
same. The default one-million distinct-key cap still applies: increase `--max-keys`
|
|
174
|
+
explicitly if needed. The disk backend reduces Python memory spent on the key
|
|
175
|
+
index, but adds database and disk I/O cost and may be much slower. SQLite's cache
|
|
176
|
+
is configured to about 8 MiB per profile, **not a hard process-memory limit**.
|
|
177
|
+
Long fields, database overhead, and `--top` also consume memory. Database storage
|
|
178
|
+
includes an index and journal; allow more space than the CSV key bytes alone.
|
|
179
|
+
|
|
180
|
+
Library callers use a context manager and keep every forecast inside it:
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from joinforecast import forecast
|
|
184
|
+
from joinforecast.disk import sqlite_csv
|
|
185
|
+
|
|
186
|
+
with sqlite_csv('orders.csv', ['customer_id']) as left:
|
|
187
|
+
with sqlite_csv('customers.csv', ['id']) as right:
|
|
188
|
+
report = forecast(left, right, expectation='many-to-one')
|
|
189
|
+
# The JSON-compatible report is independent of the closed databases.
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Disk profile frequencies are a mapping, not an in-memory `Counter`. Access the
|
|
193
|
+
profile only inside its context; it cannot be used after cleanup. Normal
|
|
194
|
+
`read_csv` still returns an in-memory `Counter` profile as before.
|
|
195
|
+
|
|
196
|
+
## Library
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
from joinforecast import read_csv, forecast
|
|
200
|
+
|
|
201
|
+
left = read_csv('orders.csv', ['customer_id'])
|
|
202
|
+
right = read_csv('customers.csv', ['id'])
|
|
203
|
+
report = forecast(left, right, nulls='never', expectation='many-to-one')
|
|
204
|
+
print(report['predicted_rows']['left'])
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Input files are read only. There is no network code or telemetry.
|
|
208
|
+
The opt-in SQLite backend writes key frequencies to temporary disk as described above. Reports include
|
|
209
|
+
actual key values in `top_contributors`; treat redirected JSON as sensitive when
|
|
210
|
+
your CSV contains private identifiers. `--top 0` omits those examples.
|
|
211
|
+
|
|
212
|
+
## How counts work
|
|
213
|
+
|
|
214
|
+
For every joinable shared key with L left rows and R right rows, the inner join
|
|
215
|
+
contribution is L × R. Each left row with no right match adds one row to the left
|
|
216
|
+
and full joins; each unmatched right row adds one row to the right and full joins.
|
|
217
|
+
Composite keys are tuples, not delimiter-joined strings, so embedded separators
|
|
218
|
+
cannot create false matches. Top contributors are bounded with a heap.
|
|
219
|
+
|
|
220
|
+
This does not perform the join, validate non-key columns, estimate output bytes,
|
|
221
|
+
resolve suffix conflicts, check aggregates, or predict range/as-of/fuzzy/collation
|
|
222
|
+
joins. It cannot infer the semantics of a later loader or changed input files.
|
|
223
|
+
Run the preflight on the exact inputs and settings used by the eventual join.
|
|
224
|
+
|
|
225
|
+
## Develop
|
|
226
|
+
|
|
227
|
+
```sh
|
|
228
|
+
python -m pip install -e . coverage ruff build
|
|
229
|
+
python -m unittest discover -s tests -v
|
|
230
|
+
coverage run -m unittest discover -s tests
|
|
231
|
+
coverage report
|
|
232
|
+
ruff check src tests
|
|
233
|
+
ruff format --check src tests
|
|
234
|
+
python -m build
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
The test suite includes a seeded independent SQLite relational oracle for
|
|
238
|
+
composite-key joins under SQL equality and null-equal semantics, hand-computed
|
|
239
|
+
fanout, huge exact counts, malformed CSV rejection, contract failures, CLI exit
|
|
240
|
+
codes, and input byte preservation. Disk tests compare complete reports with the
|
|
241
|
+
memory backend, use an independent relational oracle, and exercise private-file
|
|
242
|
+
cleanup and real storage-full/read-interruption failures. CI tests the installed package, not a
|
|
243
|
+
materialized large join. No database service is required.
|
|
244
|
+
|
|
245
|
+
## Frequency-scan implementation (0.2.1)
|
|
246
|
+
|
|
247
|
+
After full profile validation, forecasting visits each left key once to count
|
|
248
|
+
matches, check left uniqueness and maintain the bounded top-contributor heap.
|
|
249
|
+
Each joinable left key needs one right-frequency lookup, regardless of `top`.
|
|
250
|
+
Equal-contribution ties still prefer the left input's first-seen key. Validation
|
|
251
|
+
and the right uniqueness scan remain complete, even with `top=0`.
|
|
252
|
+
|
|
253
|
+
The SQLite CSV parser encodes each key once, tries an insert, then increments
|
|
254
|
+
existing counts in SQL. It no longer reads a frequency back into Python for
|
|
255
|
+
each CSV row. The private parser callback is supplied explicitly by the SQLite
|
|
256
|
+
backend; ordinary mapping assignment still replaces a value rather than adding
|
|
257
|
+
to it. No SQLite `RETURNING` support is needed.
|
|
258
|
+
|
|
259
|
+
These are operation-count changes, not a promise of a particular speedup.
|
|
260
|
+
SQLite is still an opt-in lower-memory tradeoff and can be slower than the
|
|
261
|
+
in-memory backend. High `top` values retain up to that many contributor entries;
|
|
262
|
+
there is no hard process-memory guarantee. Parsing, null handling, report fields,
|
|
263
|
+
validation and contract failures are unchanged. Synthetic tests compare complete
|
|
264
|
+
reports and stable ties, and independently enumerate row pairs for small cases.
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
## Combined validation and aggregation (0.2.2)
|
|
268
|
+
|
|
269
|
+
Forecasting now validates frequencies while using them, rather than scanning
|
|
270
|
+
both complete mappings once for validation and again for aggregation. Each
|
|
271
|
+
mapping's `items()` is traversed once. The right profile is fully checked while
|
|
272
|
+
counting duplicate keys, then the left profile is checked while counting matches
|
|
273
|
+
and selecting contributors. Totals are checked after each traversal. An invalid
|
|
274
|
+
last key or inconsistent row total still raises an error, including with
|
|
275
|
+
`top=0`; no partial forecast is returned.
|
|
276
|
+
|
|
277
|
+
Accepted inputs produce the same complete reports, null semantics, stable ties,
|
|
278
|
+
and contract results. If several inputs or options are invalid, which error is
|
|
279
|
+
reported first can change. Profiles and mappings are not atomic snapshots, so do
|
|
280
|
+
not mutate them during a forecast.
|
|
281
|
+
|
|
282
|
+
This removes two full SQLite key-decoding passes. In a local Linux CPython 3.14
|
|
283
|
+
measurement of the forecast alone over two 100,000-distinct-key profiles, the
|
|
284
|
+
median of three fresh-process runs changed from 0.718 to 0.564 seconds for disk
|
|
285
|
+
profiles. The in-memory case was slower, from 0.079 to 0.085 seconds. Input parsing
|
|
286
|
+
was outside that timing; peak process RSS was similar. These are synthetic local
|
|
287
|
+
measurements, not a universal speed or memory guarantee.
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# joinforecast
|
|
2
|
+
|
|
3
|
+
Count CSV join rows before a many-to-many merge eats your memory.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/anishpentyala/joinforecast/actions)
|
|
6
|
+
|
|
7
|
+
## Quickstart
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
python -m pip install "git+https://github.com/anishpentyala/joinforecast.git"
|
|
11
|
+
printf 'customer_id\n001\n001\n002\n' > orders.csv
|
|
12
|
+
printf 'customer_id\n001\n001\n003\n' > customers.csv
|
|
13
|
+
joinforecast orders.csv customers.csv --left-key customer_id
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This prints a full JSON report. Selected fields for these exact files:
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
{"predicted_rows": {"inner": 4, "left": 5, "right": 5, "full": 6, "left_anti": 1, "right_anti": 1}}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The four matching rows come from two orders times two customer records for `001`.
|
|
23
|
+
No join is built, and neither input is changed. Add `--expect many-to-one` to make
|
|
24
|
+
repeated lookup keys fail the contract (exit 1, with a complete report).
|
|
25
|
+
|
|
26
|
+
Installation is currently from source. A PyPI release is being prepared; do not
|
|
27
|
+
assume `pip install joinforecast` is available until its package page is linked here.
|
|
28
|
+
|
|
29
|
+
## Details
|
|
30
|
+
|
|
31
|
+
Find a many-to-many row explosion before a CSV equality join, without building
|
|
32
|
+
that join or uploading either file.
|
|
33
|
+
|
|
34
|
+
`joinforecast` counts key frequencies and reports exact inner, left, right, full,
|
|
35
|
+
and anti-join row counts. It shows the largest matched-key contributions,
|
|
36
|
+
unmatched input rows, duplicate joinable keys, and optional contract violations.
|
|
37
|
+
It uses only Python's standard library. Python 3.9 or later is required.
|
|
38
|
+
|
|
39
|
+
## Install from source
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
git clone https://github.com/anishpentyala/joinforecast.git
|
|
43
|
+
cd joinforecast
|
|
44
|
+
python -m pip install .
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This is a source-installable tool, not a claimed PyPI release.
|
|
48
|
+
|
|
49
|
+
## Catch a broken lookup
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
joinforecast examples/orders.csv examples/customers.csv \
|
|
53
|
+
--left-key customer_id --expect many-to-one --require-match left
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The example has two orders for `001` and two customer records for `001`.
|
|
57
|
+
Their key alone contributes **four** output rows. With the default null policy,
|
|
58
|
+
counts are inner **5**, left **7**, right **7**, and full **9**. Two orders have
|
|
59
|
+
no match: customer `003` and the empty customer ID. The command exits **1** because
|
|
60
|
+
the right keys are not unique and some left rows have no match. The report is
|
|
61
|
+
still complete, machine-readable JSON.
|
|
62
|
+
|
|
63
|
+
Without contracts, a valid forecast exits 0, even when it finds duplicates.
|
|
64
|
+
This is a preflight, not a declaration that a join is safe for your analysis.
|
|
65
|
+
|
|
66
|
+
## Specify the key and null semantics
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
# Composite key; map differently named columns in order.
|
|
70
|
+
joinforecast left.csv right.csv \
|
|
71
|
+
--left-key account --left-key period \
|
|
72
|
+
--right-key customer --right-key month --nulls never
|
|
73
|
+
|
|
74
|
+
# Matching null components; explicitly recognize empty fields and NA as null.
|
|
75
|
+
joinforecast left.csv right.csv --left-key id \
|
|
76
|
+
--nulls match --null-value '' --null-value NA
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- Keys are **exact CSV strings**. `001`, `1`, ` 1`, and `1.0` stay different.
|
|
80
|
+
No numeric conversion, trimming, case folding, locale rules, or date parsing.
|
|
81
|
+
- Only empty fields are null by default. `--null-value` can be repeated and
|
|
82
|
+
**replaces**, rather than extends, that default. `--null-value NA` makes
|
|
83
|
+
an empty string an ordinary key. The library can use `null_values=()` for
|
|
84
|
+
no null tokens.
|
|
85
|
+
- `--nulls never` is SQL equality behavior: any null component prevents a match.
|
|
86
|
+
- `--nulls match` matches nulls at corresponding key positions. This models
|
|
87
|
+
null equality, **not a complete pandas CSV-loader simulation**. Set tokens and
|
|
88
|
+
types to match the loader you actually use. pandas warns that its null-key
|
|
89
|
+
matches differ from SQL: [pandas.merge documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge.html).
|
|
90
|
+
- UTF-8 and UTF-8 BOM input only. Comma delimiter by default; `--delimiter ';'`
|
|
91
|
+
is available. CSV quoting and multiline fields are supported.
|
|
92
|
+
- Blank/duplicate header names, missing key columns, ragged rows (including
|
|
93
|
+
blank data records), malformed quoting, and invalid UTF-8 are errors.
|
|
94
|
+
A header-only file is valid and has zero data rows.
|
|
95
|
+
|
|
96
|
+
No missing rows are silently dropped, sampled, or counted as success.
|
|
97
|
+
|
|
98
|
+
## Contracts and resource limits
|
|
99
|
+
|
|
100
|
+
| Option | Meaning |
|
|
101
|
+
|---|---|
|
|
102
|
+
| `--expect one-to-one` | Both sides' joinable keys must be unique |
|
|
103
|
+
| `--expect one-to-many` | Left joinable keys must be unique |
|
|
104
|
+
| `--expect many-to-one` | Right joinable keys must be unique |
|
|
105
|
+
| `--expect many-to-many` | No uniqueness constraint |
|
|
106
|
+
| `--require-match left/right/both` | All rows on those sides must have a match |
|
|
107
|
+
| `--max-output-rows N` | Each of the four materializing join forecasts must be at most N |
|
|
108
|
+
| `--max-input-rows N` | Per-input row cap, default 10,000,000 |
|
|
109
|
+
| `--max-keys N` | Per-input distinct-key cap, default 1,000,000 |
|
|
110
|
+
| `--top N` | Return at most N matched-key contributors, default 10 |
|
|
111
|
+
|
|
112
|
+
Contracts check all joinable keys, even unmatched ones. Under `never`, null keys
|
|
113
|
+
are excluded from uniqueness checks, but still count as unmatched rows. Under
|
|
114
|
+
`match`, null keys participate normally. This is an explicit contract definition,
|
|
115
|
+
not a promise to duplicate a particular database's uniqueness constraint rules.
|
|
116
|
+
|
|
117
|
+
Input limits stop with exit 2 and **no JSON forecast**, rather than returning a
|
|
118
|
+
plausible partial count. They are safety bounds, not hard RAM guarantees: memory
|
|
119
|
+
is proportional to distinct keys and their lengths. One huge field is also
|
|
120
|
+
subject to Python's CSV parser field-size limit. Output row arithmetic uses
|
|
121
|
+
Python integers, with no floating-point rounding or output-row materialization.
|
|
122
|
+
|
|
123
|
+
Exit codes: **0** complete forecast satisfying requested contracts; **1** complete
|
|
124
|
+
forecast with contract violations; **2** invalid/incomplete input, bad options,
|
|
125
|
+
or output-delivery failure. A successful report write must accept the complete
|
|
126
|
+
text and flush before a normal verdict is returned. Help, version and argument
|
|
127
|
+
messages use the same checked output path. If output fails, partial bytes may
|
|
128
|
+
already have escaped; discard them, since they are not a delivered forecast.
|
|
129
|
+
Diagnostics are best effort if stderr fails too. Embedded `main(argv)` leaves
|
|
130
|
+
caller streams open and unchanged. The executable wrappers own shutdown and
|
|
131
|
+
avoid retrying failed flushes that would otherwise turn exit 2 into exit 120.
|
|
132
|
+
JSON `schema_version` is 1. `ok` means only that the requested contracts passed.
|
|
133
|
+
|
|
134
|
+
## Opt-in disk-backed key frequencies
|
|
135
|
+
|
|
136
|
+
The default backend keeps key frequencies in memory. If the distinct-key index
|
|
137
|
+
is too large for that, use a private temporary SQLite database instead:
|
|
138
|
+
|
|
139
|
+
```sh
|
|
140
|
+
joinforecast large-left.csv large-right.csv --left-key id \
|
|
141
|
+
--storage sqlite --max-keys 5000000 --temp-dir /path/to/private/scratch
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
This uses Python's `sqlite3` module, not a database server. It writes exact key
|
|
145
|
+
values and their frequencies to temporary disk. Use a suitable scratch location
|
|
146
|
+
for sensitive data. The directory is private to the current user; normal success
|
|
147
|
+
and errors close and remove both databases. Removal is **not secure erasure**.
|
|
148
|
+
Forced termination or power loss can leave files behind. Other users with elevated
|
|
149
|
+
access, disk backups, and filesystem snapshots may still expose the data.
|
|
150
|
+
|
|
151
|
+
`--temp-dir` selects an existing parent directory, not a database filename. Omit
|
|
152
|
+
it to use Python's normal temporary location. It requires `--storage sqlite`.
|
|
153
|
+
An unwritable, missing, or full temporary location fails with exit 2 and no JSON
|
|
154
|
+
forecast. Neither CSV input is modified. The SQLite backend needs a Python build
|
|
155
|
+
with `sqlite3` support; the default memory backend does not.
|
|
156
|
+
|
|
157
|
+
Counts, null rules, top-contributor tie order, contracts, and input limits stay the
|
|
158
|
+
same. The default one-million distinct-key cap still applies: increase `--max-keys`
|
|
159
|
+
explicitly if needed. The disk backend reduces Python memory spent on the key
|
|
160
|
+
index, but adds database and disk I/O cost and may be much slower. SQLite's cache
|
|
161
|
+
is configured to about 8 MiB per profile, **not a hard process-memory limit**.
|
|
162
|
+
Long fields, database overhead, and `--top` also consume memory. Database storage
|
|
163
|
+
includes an index and journal; allow more space than the CSV key bytes alone.
|
|
164
|
+
|
|
165
|
+
Library callers use a context manager and keep every forecast inside it:
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from joinforecast import forecast
|
|
169
|
+
from joinforecast.disk import sqlite_csv
|
|
170
|
+
|
|
171
|
+
with sqlite_csv('orders.csv', ['customer_id']) as left:
|
|
172
|
+
with sqlite_csv('customers.csv', ['id']) as right:
|
|
173
|
+
report = forecast(left, right, expectation='many-to-one')
|
|
174
|
+
# The JSON-compatible report is independent of the closed databases.
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Disk profile frequencies are a mapping, not an in-memory `Counter`. Access the
|
|
178
|
+
profile only inside its context; it cannot be used after cleanup. Normal
|
|
179
|
+
`read_csv` still returns an in-memory `Counter` profile as before.
|
|
180
|
+
|
|
181
|
+
## Library
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from joinforecast import read_csv, forecast
|
|
185
|
+
|
|
186
|
+
left = read_csv('orders.csv', ['customer_id'])
|
|
187
|
+
right = read_csv('customers.csv', ['id'])
|
|
188
|
+
report = forecast(left, right, nulls='never', expectation='many-to-one')
|
|
189
|
+
print(report['predicted_rows']['left'])
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Input files are read only. There is no network code or telemetry.
|
|
193
|
+
The opt-in SQLite backend writes key frequencies to temporary disk as described above. Reports include
|
|
194
|
+
actual key values in `top_contributors`; treat redirected JSON as sensitive when
|
|
195
|
+
your CSV contains private identifiers. `--top 0` omits those examples.
|
|
196
|
+
|
|
197
|
+
## How counts work
|
|
198
|
+
|
|
199
|
+
For every joinable shared key with L left rows and R right rows, the inner join
|
|
200
|
+
contribution is L × R. Each left row with no right match adds one row to the left
|
|
201
|
+
and full joins; each unmatched right row adds one row to the right and full joins.
|
|
202
|
+
Composite keys are tuples, not delimiter-joined strings, so embedded separators
|
|
203
|
+
cannot create false matches. Top contributors are bounded with a heap.
|
|
204
|
+
|
|
205
|
+
This does not perform the join, validate non-key columns, estimate output bytes,
|
|
206
|
+
resolve suffix conflicts, check aggregates, or predict range/as-of/fuzzy/collation
|
|
207
|
+
joins. It cannot infer the semantics of a later loader or changed input files.
|
|
208
|
+
Run the preflight on the exact inputs and settings used by the eventual join.
|
|
209
|
+
|
|
210
|
+
## Develop
|
|
211
|
+
|
|
212
|
+
```sh
|
|
213
|
+
python -m pip install -e . coverage ruff build
|
|
214
|
+
python -m unittest discover -s tests -v
|
|
215
|
+
coverage run -m unittest discover -s tests
|
|
216
|
+
coverage report
|
|
217
|
+
ruff check src tests
|
|
218
|
+
ruff format --check src tests
|
|
219
|
+
python -m build
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
The test suite includes a seeded independent SQLite relational oracle for
|
|
223
|
+
composite-key joins under SQL equality and null-equal semantics, hand-computed
|
|
224
|
+
fanout, huge exact counts, malformed CSV rejection, contract failures, CLI exit
|
|
225
|
+
codes, and input byte preservation. Disk tests compare complete reports with the
|
|
226
|
+
memory backend, use an independent relational oracle, and exercise private-file
|
|
227
|
+
cleanup and real storage-full/read-interruption failures. CI tests the installed package, not a
|
|
228
|
+
materialized large join. No database service is required.
|
|
229
|
+
|
|
230
|
+
## Frequency-scan implementation (0.2.1)
|
|
231
|
+
|
|
232
|
+
After full profile validation, forecasting visits each left key once to count
|
|
233
|
+
matches, check left uniqueness and maintain the bounded top-contributor heap.
|
|
234
|
+
Each joinable left key needs one right-frequency lookup, regardless of `top`.
|
|
235
|
+
Equal-contribution ties still prefer the left input's first-seen key. Validation
|
|
236
|
+
and the right uniqueness scan remain complete, even with `top=0`.
|
|
237
|
+
|
|
238
|
+
The SQLite CSV parser encodes each key once, tries an insert, then increments
|
|
239
|
+
existing counts in SQL. It no longer reads a frequency back into Python for
|
|
240
|
+
each CSV row. The private parser callback is supplied explicitly by the SQLite
|
|
241
|
+
backend; ordinary mapping assignment still replaces a value rather than adding
|
|
242
|
+
to it. No SQLite `RETURNING` support is needed.
|
|
243
|
+
|
|
244
|
+
These are operation-count changes, not a promise of a particular speedup.
|
|
245
|
+
SQLite is still an opt-in lower-memory tradeoff and can be slower than the
|
|
246
|
+
in-memory backend. High `top` values retain up to that many contributor entries;
|
|
247
|
+
there is no hard process-memory guarantee. Parsing, null handling, report fields,
|
|
248
|
+
validation and contract failures are unchanged. Synthetic tests compare complete
|
|
249
|
+
reports and stable ties, and independently enumerate row pairs for small cases.
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
## Combined validation and aggregation (0.2.2)
|
|
253
|
+
|
|
254
|
+
Forecasting now validates frequencies while using them, rather than scanning
|
|
255
|
+
both complete mappings once for validation and again for aggregation. Each
|
|
256
|
+
mapping's `items()` is traversed once. The right profile is fully checked while
|
|
257
|
+
counting duplicate keys, then the left profile is checked while counting matches
|
|
258
|
+
and selecting contributors. Totals are checked after each traversal. An invalid
|
|
259
|
+
last key or inconsistent row total still raises an error, including with
|
|
260
|
+
`top=0`; no partial forecast is returned.
|
|
261
|
+
|
|
262
|
+
Accepted inputs produce the same complete reports, null semantics, stable ties,
|
|
263
|
+
and contract results. If several inputs or options are invalid, which error is
|
|
264
|
+
reported first can change. Profiles and mappings are not atomic snapshots, so do
|
|
265
|
+
not mutate them during a forecast.
|
|
266
|
+
|
|
267
|
+
This removes two full SQLite key-decoding passes. In a local Linux CPython 3.14
|
|
268
|
+
measurement of the forecast alone over two 100,000-distinct-key profiles, the
|
|
269
|
+
median of three fresh-process runs changed from 0.718 to 0.564 seconds for disk
|
|
270
|
+
profiles. The in-memory case was slower, from 0.079 to 0.085 seconds. Input parsing
|
|
271
|
+
was outside that timing; peak process RSS was similar. These are synthetic local
|
|
272
|
+
measurements, not a universal speed or memory guarantee.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
name = "joinforecast"
|
|
8
|
+
version = "0.2.2"
|
|
9
|
+
description = "Offline, exact CSV equality-join row-count preflight"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{name = "Anish Pentyala"}]
|
|
13
|
+
keywords = ["csv", "join", "data-quality", "etl"]
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
dependencies = []
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/anishpentyala/joinforecast"
|
|
19
|
+
Repository = "https://github.com/anishpentyala/joinforecast"
|
|
20
|
+
Issues = "https://github.com/anishpentyala/joinforecast/issues"
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
joinforecast = "joinforecast.__main__:cli"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
where = ["src"]
|
|
27
|
+
|
|
28
|
+
[tool.ruff]
|
|
29
|
+
target-version = "py39"
|
|
30
|
+
line-length = 100
|
|
31
|
+
|
|
32
|
+
[tool.ruff.lint]
|
|
33
|
+
select = ["E4", "E7", "E9", "F", "I"]
|
|
34
|
+
|
|
35
|
+
[tool.coverage.run]
|
|
36
|
+
source = ["joinforecast"]
|
|
37
|
+
branch = true
|
|
38
|
+
|
|
39
|
+
[tool.coverage.report]
|
|
40
|
+
show_missing = true
|