odcs-check 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.
- odcs_check-0.1.0/LICENSE +21 -0
- odcs_check-0.1.0/PKG-INFO +245 -0
- odcs_check-0.1.0/README.md +233 -0
- odcs_check-0.1.0/odcs_check/__init__.py +1 -0
- odcs_check-0.1.0/odcs_check/__main__.py +3 -0
- odcs_check-0.1.0/odcs_check/cli.py +57 -0
- odcs_check-0.1.0/odcs_check/contract.py +644 -0
- odcs_check-0.1.0/odcs_check/run.py +355 -0
- odcs_check-0.1.0/odcs_check.egg-info/PKG-INFO +245 -0
- odcs_check-0.1.0/odcs_check.egg-info/SOURCES.txt +17 -0
- odcs_check-0.1.0/odcs_check.egg-info/dependency_links.txt +1 -0
- odcs_check-0.1.0/odcs_check.egg-info/entry_points.txt +2 -0
- odcs_check-0.1.0/odcs_check.egg-info/requires.txt +2 -0
- odcs_check-0.1.0/odcs_check.egg-info/top_level.txt +1 -0
- odcs_check-0.1.0/pyproject.toml +22 -0
- odcs_check-0.1.0/setup.cfg +4 -0
- odcs_check-0.1.0/tests/test_cli.py +131 -0
- odcs_check-0.1.0/tests/test_contract.py +332 -0
- odcs_check-0.1.0/tests/test_run.py +502 -0
odcs_check-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 v0rein
|
|
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,245 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: odcs-check
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Check a Parquet or CSV file against an ODCS v3 data contract subset.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: duckdb>=1.1
|
|
10
|
+
Requires-Dist: PyYAML>=6
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# odcs-check
|
|
14
|
+
|
|
15
|
+
[](https://github.com/v0rein/odcs-check/actions/workflows/ci.yml)
|
|
16
|
+
[](https://github.com/v0rein/odcs-check/actions/workflows/dogfood.yml)
|
|
17
|
+
|
|
18
|
+
Check a Parquet or CSV file against a data contract, and fail CI when the data breaks it. Two dependencies (`duckdb`, `PyYAML`), one command, local files only.
|
|
19
|
+
|
|
20
|
+
The contract is a **subset of ODCS v3** (Open Data Contract Standard), the format `datacontract-cli` uses. Every contract odcs-check accepts also runs unchanged in `datacontract-cli`.
|
|
21
|
+
|
|
22
|
+
## Use something else if
|
|
23
|
+
|
|
24
|
+
Mature tools already do this, and do more:
|
|
25
|
+
|
|
26
|
+
- **[datacontract-cli](https://github.com/datacontract/datacontract-cli)**: the reference tool for ODCS. 30+ data sources, full spec, many exports. Use it when your data is in a warehouse, S3 or Kafka, or you need the whole standard. It pulls in a lot more to install.
|
|
27
|
+
- **[Soda Core](https://github.com/sodadata/soda-core)**: its own check language, many sources. Licensed ELv2 since 2026; read the licence before you depend on it.
|
|
28
|
+
- **[Great Expectations Core](https://github.com/great-expectations/great_expectations)**: expectation suites and data docs, for teams that want a framework.
|
|
29
|
+
- **[Pandera](https://github.com/unionai-oss/pandera)**: schemas as Python code, for checking DataFrames inside a pipeline.
|
|
30
|
+
|
|
31
|
+
odcs-check is for one narrow job: "check this Parquet or CSV file in CI", with a contract you can hand to `datacontract-cli` later. If that is not your job, use one of the above.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install odcs-check
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Python 3.10+. DuckDB resolves to 1.5.x on Python 3.10 and 2.x on 3.11+; both are tested.
|
|
40
|
+
|
|
41
|
+
## Try it
|
|
42
|
+
|
|
43
|
+
`orders.csv`:
|
|
44
|
+
|
|
45
|
+
```csv
|
|
46
|
+
order_id,status,amount
|
|
47
|
+
1,paid,120000
|
|
48
|
+
2,paid,45000
|
|
49
|
+
3,refunded,45000
|
|
50
|
+
2,shipped,-5
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`orders.yaml`:
|
|
54
|
+
|
|
55
|
+
```yaml
|
|
56
|
+
apiVersion: v3.1.0
|
|
57
|
+
kind: DataContract
|
|
58
|
+
id: orders
|
|
59
|
+
version: 1.0.0
|
|
60
|
+
servers:
|
|
61
|
+
- {server: local, type: local, format: csv, path: orders.csv}
|
|
62
|
+
schema:
|
|
63
|
+
- name: orders
|
|
64
|
+
properties:
|
|
65
|
+
- name: order_id
|
|
66
|
+
logicalType: integer
|
|
67
|
+
required: true
|
|
68
|
+
unique: true
|
|
69
|
+
- name: status
|
|
70
|
+
logicalType: string
|
|
71
|
+
enum: [{value: paid}, {value: refunded}]
|
|
72
|
+
- name: amount
|
|
73
|
+
logicalType: integer
|
|
74
|
+
logicalTypeOptions: {minimum: 0}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```console
|
|
78
|
+
$ odcs-check test orders.yaml
|
|
79
|
+
passed column present order_id
|
|
80
|
+
passed logicalType order_id integer
|
|
81
|
+
passed required order_id
|
|
82
|
+
failed unique order_id 1 duplicated value(s)
|
|
83
|
+
passed column present status
|
|
84
|
+
passed logicalType status string
|
|
85
|
+
failed enum status 1 value(s) outside the enum
|
|
86
|
+
passed column present amount
|
|
87
|
+
passed logicalType amount integer
|
|
88
|
+
failed minimum amount 1 value(s) violate minimum 0
|
|
89
|
+
odcs-check: 10 checks, 7 passed, 3 failed, 0 warnings — contract invalid
|
|
90
|
+
$ echo $?
|
|
91
|
+
1
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Options: `--server NAME` (required when the contract lists more than one server), `--path FILE` (check this file instead of the server's `path`, for a file CI downloaded elsewhere). Paths are relative to the current directory, as in `datacontract-cli`.
|
|
95
|
+
|
|
96
|
+
### Exit codes
|
|
97
|
+
|
|
98
|
+
| Code | Meaning |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `0` | every check passed (warnings allowed) |
|
|
101
|
+
| `1` | the data broke the contract |
|
|
102
|
+
| `2` | the contract is invalid or uses something odcs-check does not support, or the file cannot be read |
|
|
103
|
+
|
|
104
|
+
**odcs-check never skips a check silently.** A key it does not know, including a typo, exits `2` and names the key:
|
|
105
|
+
|
|
106
|
+
```console
|
|
107
|
+
$ odcs-check test typo.yaml
|
|
108
|
+
contract error schema[0].properties[0] 'order_id': unknown key 'requird' (typo? odcs-check never ignores a key it does not know)
|
|
109
|
+
odcs-check: 1 contract problem(s) — contract invalid
|
|
110
|
+
$ echo $?
|
|
111
|
+
2
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`datacontract-cli` only warns on a typo nested in a property and carries on, so the check is quietly gone.
|
|
115
|
+
|
|
116
|
+
## What it checks
|
|
117
|
+
|
|
118
|
+
Everything else exits `2`.
|
|
119
|
+
|
|
120
|
+
| Check | ODCS key | Notes |
|
|
121
|
+
|---|---|---|
|
|
122
|
+
| column present | `schema[].properties[].name` | case-sensitive; extra columns in the file are fine |
|
|
123
|
+
| type | `logicalType` | `string`, `integer`, `number`, `boolean`, `date`, `timestamp`; required on every property |
|
|
124
|
+
| not null | `required: true` | |
|
|
125
|
+
| unique | `unique: true` | NULLs never count as duplicates |
|
|
126
|
+
| primary key | `primaryKey: true` | not null + unique; several members are unique as a tuple |
|
|
127
|
+
| allowed values | `enum: [{value: a}]` | object form only; plain strings are invalid ODCS |
|
|
128
|
+
| range | `logicalTypeOptions.minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` | numbers, dates, timestamps; at most one lower and one upper bound |
|
|
129
|
+
| length | `logicalTypeOptions.minLength`, `maxLength` | strings, in characters |
|
|
130
|
+
| regex | `logicalTypeOptions.pattern` | a **search**, not a full match (anchor with `^...$`); DuckDB RE2 syntax |
|
|
131
|
+
| quality metrics | `quality[]`: `nullValues`, `missingValues`, `invalidValues`, `duplicateValues`, `rowCount` | `unit: rows` or `percent` (percent only on the first three); `duplicateValues` counts duplicated keys, not extra rows |
|
|
132
|
+
| operators | `mustBe`, `mustNotBe`, `mustBeGreaterThan`, `mustBeGreaterOrEqualTo`, `mustBeLessThan`, `mustBeLessOrEqualTo`, `mustBeBetween`, `mustNotBeBetween` | exactly one per rule; between is inclusive |
|
|
133
|
+
| custom SQL | `quality[]` with `type: sql`, `query` | one read-only `SELECT`/`WITH`; `{object}`, `{property}` placeholders; must return one row, one number |
|
|
134
|
+
| freshness | `slaProperties` with `property: freshness`, `value`, `unit`, `element` | newest value must be younger than the limit, in UTC; `unit` is required |
|
|
135
|
+
| severity | `severity: warning` on a rule | reported, exit stays `0`; `error` is the default |
|
|
136
|
+
| text rules | `type: text` | listed as "not checked", does not affect the exit code |
|
|
137
|
+
|
|
138
|
+
Descriptive fields (`description`, `tags`, `team`, `support`, `customProperties` and the rest of ODCS's documentation keys) are read and ignored.
|
|
139
|
+
|
|
140
|
+
### How a file is read
|
|
141
|
+
|
|
142
|
+
Like `datacontract-cli`, odcs-check does not compare the file to the contract; it casts each declared column to the contract's type and checks the result. A cast that fails is a `fail`. Only declared columns are visible to `sql` rules. DuckDB runs in UTC.
|
|
143
|
+
|
|
144
|
+
| `logicalType` | Parquet cast | CSV cast |
|
|
145
|
+
|---|---|---|
|
|
146
|
+
| `string` | VARCHAR | VARCHAR (raw text kept) |
|
|
147
|
+
| `integer` | INTEGER (32-bit) | BIGINT |
|
|
148
|
+
| `number` | DECIMAL(18,3) | DOUBLE |
|
|
149
|
+
| `boolean` | BOOLEAN | BOOLEAN |
|
|
150
|
+
| `date` | DATE | DATE |
|
|
151
|
+
| `timestamp` | TIMESTAMPTZ (naive values read as UTC) | TIMESTAMP |
|
|
152
|
+
|
|
153
|
+
So a Parquet `BIGINT` above 2,147,483,647 fails an `integer` column, and `0.0004` in a `number` column becomes `0.000`. That is `datacontract-cli`'s behaviour too; odcs-check matches it so it is never looser.
|
|
154
|
+
|
|
155
|
+
## Stricter than datacontract-cli
|
|
156
|
+
|
|
157
|
+
The claim is "equal or stricter, never looser". Three checks are stricter, because `datacontract-cli` skips them or runs them loosely:
|
|
158
|
+
|
|
159
|
+
1. **Type family on the file's own column type.** `datacontract-cli` only casts, so a text column of `"1","2"` declared `integer` passes there. odcs-check fails it: `integer` accepts any integer width, `number` any numeric, `timestamp` TIMESTAMP or TIMESTAMPTZ.
|
|
160
|
+
2. **Case-sensitive column names.** Contract `ID` against file `id`: `datacontract-cli` loads zero rows, so `required` and `unique` pass on nothing. odcs-check reports the missing column.
|
|
161
|
+
3. **`sql` rules must return exactly one row and one number.** `datacontract-cli` takes the first cell of the first row and compares anything, so `SELECT 'abc'` with `mustNotBe: 0` passes there.
|
|
162
|
+
|
|
163
|
+
## What is rejected, and why
|
|
164
|
+
|
|
165
|
+
Anything in this list exits `2` and names the key. Three reasons, from [the spike](docs/spike-datacontract-cli.md):
|
|
166
|
+
|
|
167
|
+
- **`datacontract-cli` rejects it as invalid.** For example a `logicalTypeOptions` key on the wrong type (`pattern` on a number), `enum: [a, b]` as plain strings, an unknown top-level key, `apiVersion` outside `v3.0.0` to `v3.2.0`. Contracts must stay portable.
|
|
168
|
+
- **`datacontract-cli` accepts it and does nothing.** `logicalTypeOptions.format` and `multipleOf`, `type: custom`, a metric at the wrong level, `unit: pct` (silently read as rows), any `slaProperties` entry other than freshness. odcs-check refuses rather than pretend it ran.
|
|
169
|
+
- **`datacontract-cli` runs it differently from the ODCS text, or unreliably.**
|
|
170
|
+
- `physicalType` is never checked on files and changes the cast with its own vocabulary.
|
|
171
|
+
- Freshness `unit` omitted (it assumes days) or `m` (minutes there, ambiguous in ODCS).
|
|
172
|
+
- Two freshness rules on one column overwrite each other.
|
|
173
|
+
- `retention`, `relationships` and `physicalName`.
|
|
174
|
+
- A `severity` other than `error`/`warning`; `datacontract-cli` turns `info` and `low` into warnings and every other word into a failure.
|
|
175
|
+
- Several `schema` objects, a server `path` with a glob, and several servers with no `--server`; `datacontract-cli` silently takes the first.
|
|
176
|
+
- `logicalType: array`, `object`, `map`, `vector`, `time`.
|
|
177
|
+
|
|
178
|
+
The full list, with the fixture for each, is in [`docs/spike-datacontract-cli.md`](docs/spike-datacontract-cli.md) section 2.
|
|
179
|
+
|
|
180
|
+
## Parity with datacontract-cli
|
|
181
|
+
|
|
182
|
+
CI runs every fixture contract (clean and deliberately broken data) and every dogfood contract through odcs-check and through `datacontract-cli[duckdb]==1.2.12`. It fails if odcs-check passes where `datacontract-cli` fails, or fails where it passes on a check not on the stricter list above. The last run, measured:
|
|
183
|
+
|
|
184
|
+
```console
|
|
185
|
+
$ python tests/parity.py --dogfood
|
|
186
|
+
...
|
|
187
|
+
parity: 201 cases, 0 disagreements
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
It takes about 3 minutes (`datacontract-cli` needs ~3 s per contract). Two fixtures with a quote in an object name or path pass in odcs-check but error in `datacontract-cli`, which cannot run them at all; parity treats that as no verdict.
|
|
191
|
+
|
|
192
|
+
This covers the subset above and nothing more. It is a parity test on these fixtures, not a proof for every contract.
|
|
193
|
+
|
|
194
|
+
## Dogfood
|
|
195
|
+
|
|
196
|
+
A scheduled workflow downloads the published files of two real datasets and checks them against the contracts in [`contracts/`](contracts/), every day at 06:00 UTC. A red badge means a dataset changed under its contract or odcs-check broke.
|
|
197
|
+
|
|
198
|
+
[](https://github.com/v0rein/odcs-check/actions/workflows/dogfood.yml)
|
|
199
|
+
|
|
200
|
+
| Dataset | Contracts |
|
|
201
|
+
|---|---|
|
|
202
|
+
| [harga-pangan](https://github.com/v0rein/harga-pangan): PIHPS food prices, daily | [`contracts/harga-pangan/`](contracts/harga-pangan/): 6 files |
|
|
203
|
+
| [bps-wilayah](https://github.com/v0rein/bps-wilayah): BPS to Kemendagri region codes | [`contracts/bps-wilayah/`](contracts/bps-wilayah/): 2 files; `bridge.yaml` runs twice, `--server parquet` and `--server csv` |
|
|
204
|
+
|
|
205
|
+
**It found something.** 120 rows in harga-pangan's `price.parquet` have `price_idr` above Rp 1,000,000/kg. The largest: com_9 (producer tier, Bengkulu) at 12,000,000 from 2021-06-28 to 2021-10-19, then 120,000 the next day. That looks like a x100 typo at the source. harga-pangan keeps source values untouched, so the rule is `severity: warning`, not an error:
|
|
206
|
+
|
|
207
|
+
```console
|
|
208
|
+
$ odcs-check test contracts/harga-pangan/price.yaml
|
|
209
|
+
...
|
|
210
|
+
warning sql - got 120, expected mustBe 0
|
|
211
|
+
passed freshness date
|
|
212
|
+
odcs-check: 43 checks, 42 passed, 0 failed, 1 warnings — contract valid
|
|
213
|
+
$ echo $?
|
|
214
|
+
0
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
The rule becomes an error once the dataset flags or corrects those rows.
|
|
218
|
+
|
|
219
|
+
## Development
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
conda env create -f environment.yml
|
|
223
|
+
conda activate odcs-check
|
|
224
|
+
pip install -e .
|
|
225
|
+
python tests/test_contract.py
|
|
226
|
+
python tests/test_run.py
|
|
227
|
+
python tests/test_cli.py
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Tests are plain assert scripts, no pytest. Parity needs `datacontract-cli` in a second environment, plus the dataset files (`scripts/fetch-releases.sh`, needs an authenticated `gh`):
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pip install "datacontract-cli[duckdb,parquet]==1.2.12" # in its own env
|
|
234
|
+
DATACONTRACT=/path/to/env/bin/datacontract python tests/parity.py --dogfood
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Release: bump `odcs_check.__version__`, push a tag `v<version>`. [`publish.yml`](.github/workflows/publish.yml) checks the tag matches, builds, and publishes to PyPI by Trusted Publishing; no token is stored.
|
|
238
|
+
|
|
239
|
+
`sql` rules run with the same trust as any script you run. Do not point odcs-check at a contract you did not write.
|
|
240
|
+
|
|
241
|
+
Design: [`docs/specs/2026-12-03-odcs-check-design.md`](docs/specs/2026-12-03-odcs-check-design.md). Plan: [`docs/plans/2026-12-03-odcs-check.md`](docs/plans/2026-12-03-odcs-check.md). What `datacontract-cli` actually does: [`docs/spike-datacontract-cli.md`](docs/spike-datacontract-cli.md).
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
MIT.
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# odcs-check
|
|
2
|
+
|
|
3
|
+
[](https://github.com/v0rein/odcs-check/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/v0rein/odcs-check/actions/workflows/dogfood.yml)
|
|
5
|
+
|
|
6
|
+
Check a Parquet or CSV file against a data contract, and fail CI when the data breaks it. Two dependencies (`duckdb`, `PyYAML`), one command, local files only.
|
|
7
|
+
|
|
8
|
+
The contract is a **subset of ODCS v3** (Open Data Contract Standard), the format `datacontract-cli` uses. Every contract odcs-check accepts also runs unchanged in `datacontract-cli`.
|
|
9
|
+
|
|
10
|
+
## Use something else if
|
|
11
|
+
|
|
12
|
+
Mature tools already do this, and do more:
|
|
13
|
+
|
|
14
|
+
- **[datacontract-cli](https://github.com/datacontract/datacontract-cli)**: the reference tool for ODCS. 30+ data sources, full spec, many exports. Use it when your data is in a warehouse, S3 or Kafka, or you need the whole standard. It pulls in a lot more to install.
|
|
15
|
+
- **[Soda Core](https://github.com/sodadata/soda-core)**: its own check language, many sources. Licensed ELv2 since 2026; read the licence before you depend on it.
|
|
16
|
+
- **[Great Expectations Core](https://github.com/great-expectations/great_expectations)**: expectation suites and data docs, for teams that want a framework.
|
|
17
|
+
- **[Pandera](https://github.com/unionai-oss/pandera)**: schemas as Python code, for checking DataFrames inside a pipeline.
|
|
18
|
+
|
|
19
|
+
odcs-check is for one narrow job: "check this Parquet or CSV file in CI", with a contract you can hand to `datacontract-cli` later. If that is not your job, use one of the above.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install odcs-check
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Python 3.10+. DuckDB resolves to 1.5.x on Python 3.10 and 2.x on 3.11+; both are tested.
|
|
28
|
+
|
|
29
|
+
## Try it
|
|
30
|
+
|
|
31
|
+
`orders.csv`:
|
|
32
|
+
|
|
33
|
+
```csv
|
|
34
|
+
order_id,status,amount
|
|
35
|
+
1,paid,120000
|
|
36
|
+
2,paid,45000
|
|
37
|
+
3,refunded,45000
|
|
38
|
+
2,shipped,-5
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`orders.yaml`:
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
apiVersion: v3.1.0
|
|
45
|
+
kind: DataContract
|
|
46
|
+
id: orders
|
|
47
|
+
version: 1.0.0
|
|
48
|
+
servers:
|
|
49
|
+
- {server: local, type: local, format: csv, path: orders.csv}
|
|
50
|
+
schema:
|
|
51
|
+
- name: orders
|
|
52
|
+
properties:
|
|
53
|
+
- name: order_id
|
|
54
|
+
logicalType: integer
|
|
55
|
+
required: true
|
|
56
|
+
unique: true
|
|
57
|
+
- name: status
|
|
58
|
+
logicalType: string
|
|
59
|
+
enum: [{value: paid}, {value: refunded}]
|
|
60
|
+
- name: amount
|
|
61
|
+
logicalType: integer
|
|
62
|
+
logicalTypeOptions: {minimum: 0}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```console
|
|
66
|
+
$ odcs-check test orders.yaml
|
|
67
|
+
passed column present order_id
|
|
68
|
+
passed logicalType order_id integer
|
|
69
|
+
passed required order_id
|
|
70
|
+
failed unique order_id 1 duplicated value(s)
|
|
71
|
+
passed column present status
|
|
72
|
+
passed logicalType status string
|
|
73
|
+
failed enum status 1 value(s) outside the enum
|
|
74
|
+
passed column present amount
|
|
75
|
+
passed logicalType amount integer
|
|
76
|
+
failed minimum amount 1 value(s) violate minimum 0
|
|
77
|
+
odcs-check: 10 checks, 7 passed, 3 failed, 0 warnings — contract invalid
|
|
78
|
+
$ echo $?
|
|
79
|
+
1
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Options: `--server NAME` (required when the contract lists more than one server), `--path FILE` (check this file instead of the server's `path`, for a file CI downloaded elsewhere). Paths are relative to the current directory, as in `datacontract-cli`.
|
|
83
|
+
|
|
84
|
+
### Exit codes
|
|
85
|
+
|
|
86
|
+
| Code | Meaning |
|
|
87
|
+
|---|---|
|
|
88
|
+
| `0` | every check passed (warnings allowed) |
|
|
89
|
+
| `1` | the data broke the contract |
|
|
90
|
+
| `2` | the contract is invalid or uses something odcs-check does not support, or the file cannot be read |
|
|
91
|
+
|
|
92
|
+
**odcs-check never skips a check silently.** A key it does not know, including a typo, exits `2` and names the key:
|
|
93
|
+
|
|
94
|
+
```console
|
|
95
|
+
$ odcs-check test typo.yaml
|
|
96
|
+
contract error schema[0].properties[0] 'order_id': unknown key 'requird' (typo? odcs-check never ignores a key it does not know)
|
|
97
|
+
odcs-check: 1 contract problem(s) — contract invalid
|
|
98
|
+
$ echo $?
|
|
99
|
+
2
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`datacontract-cli` only warns on a typo nested in a property and carries on, so the check is quietly gone.
|
|
103
|
+
|
|
104
|
+
## What it checks
|
|
105
|
+
|
|
106
|
+
Everything else exits `2`.
|
|
107
|
+
|
|
108
|
+
| Check | ODCS key | Notes |
|
|
109
|
+
|---|---|---|
|
|
110
|
+
| column present | `schema[].properties[].name` | case-sensitive; extra columns in the file are fine |
|
|
111
|
+
| type | `logicalType` | `string`, `integer`, `number`, `boolean`, `date`, `timestamp`; required on every property |
|
|
112
|
+
| not null | `required: true` | |
|
|
113
|
+
| unique | `unique: true` | NULLs never count as duplicates |
|
|
114
|
+
| primary key | `primaryKey: true` | not null + unique; several members are unique as a tuple |
|
|
115
|
+
| allowed values | `enum: [{value: a}]` | object form only; plain strings are invalid ODCS |
|
|
116
|
+
| range | `logicalTypeOptions.minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` | numbers, dates, timestamps; at most one lower and one upper bound |
|
|
117
|
+
| length | `logicalTypeOptions.minLength`, `maxLength` | strings, in characters |
|
|
118
|
+
| regex | `logicalTypeOptions.pattern` | a **search**, not a full match (anchor with `^...$`); DuckDB RE2 syntax |
|
|
119
|
+
| quality metrics | `quality[]`: `nullValues`, `missingValues`, `invalidValues`, `duplicateValues`, `rowCount` | `unit: rows` or `percent` (percent only on the first three); `duplicateValues` counts duplicated keys, not extra rows |
|
|
120
|
+
| operators | `mustBe`, `mustNotBe`, `mustBeGreaterThan`, `mustBeGreaterOrEqualTo`, `mustBeLessThan`, `mustBeLessOrEqualTo`, `mustBeBetween`, `mustNotBeBetween` | exactly one per rule; between is inclusive |
|
|
121
|
+
| custom SQL | `quality[]` with `type: sql`, `query` | one read-only `SELECT`/`WITH`; `{object}`, `{property}` placeholders; must return one row, one number |
|
|
122
|
+
| freshness | `slaProperties` with `property: freshness`, `value`, `unit`, `element` | newest value must be younger than the limit, in UTC; `unit` is required |
|
|
123
|
+
| severity | `severity: warning` on a rule | reported, exit stays `0`; `error` is the default |
|
|
124
|
+
| text rules | `type: text` | listed as "not checked", does not affect the exit code |
|
|
125
|
+
|
|
126
|
+
Descriptive fields (`description`, `tags`, `team`, `support`, `customProperties` and the rest of ODCS's documentation keys) are read and ignored.
|
|
127
|
+
|
|
128
|
+
### How a file is read
|
|
129
|
+
|
|
130
|
+
Like `datacontract-cli`, odcs-check does not compare the file to the contract; it casts each declared column to the contract's type and checks the result. A cast that fails is a `fail`. Only declared columns are visible to `sql` rules. DuckDB runs in UTC.
|
|
131
|
+
|
|
132
|
+
| `logicalType` | Parquet cast | CSV cast |
|
|
133
|
+
|---|---|---|
|
|
134
|
+
| `string` | VARCHAR | VARCHAR (raw text kept) |
|
|
135
|
+
| `integer` | INTEGER (32-bit) | BIGINT |
|
|
136
|
+
| `number` | DECIMAL(18,3) | DOUBLE |
|
|
137
|
+
| `boolean` | BOOLEAN | BOOLEAN |
|
|
138
|
+
| `date` | DATE | DATE |
|
|
139
|
+
| `timestamp` | TIMESTAMPTZ (naive values read as UTC) | TIMESTAMP |
|
|
140
|
+
|
|
141
|
+
So a Parquet `BIGINT` above 2,147,483,647 fails an `integer` column, and `0.0004` in a `number` column becomes `0.000`. That is `datacontract-cli`'s behaviour too; odcs-check matches it so it is never looser.
|
|
142
|
+
|
|
143
|
+
## Stricter than datacontract-cli
|
|
144
|
+
|
|
145
|
+
The claim is "equal or stricter, never looser". Three checks are stricter, because `datacontract-cli` skips them or runs them loosely:
|
|
146
|
+
|
|
147
|
+
1. **Type family on the file's own column type.** `datacontract-cli` only casts, so a text column of `"1","2"` declared `integer` passes there. odcs-check fails it: `integer` accepts any integer width, `number` any numeric, `timestamp` TIMESTAMP or TIMESTAMPTZ.
|
|
148
|
+
2. **Case-sensitive column names.** Contract `ID` against file `id`: `datacontract-cli` loads zero rows, so `required` and `unique` pass on nothing. odcs-check reports the missing column.
|
|
149
|
+
3. **`sql` rules must return exactly one row and one number.** `datacontract-cli` takes the first cell of the first row and compares anything, so `SELECT 'abc'` with `mustNotBe: 0` passes there.
|
|
150
|
+
|
|
151
|
+
## What is rejected, and why
|
|
152
|
+
|
|
153
|
+
Anything in this list exits `2` and names the key. Three reasons, from [the spike](docs/spike-datacontract-cli.md):
|
|
154
|
+
|
|
155
|
+
- **`datacontract-cli` rejects it as invalid.** For example a `logicalTypeOptions` key on the wrong type (`pattern` on a number), `enum: [a, b]` as plain strings, an unknown top-level key, `apiVersion` outside `v3.0.0` to `v3.2.0`. Contracts must stay portable.
|
|
156
|
+
- **`datacontract-cli` accepts it and does nothing.** `logicalTypeOptions.format` and `multipleOf`, `type: custom`, a metric at the wrong level, `unit: pct` (silently read as rows), any `slaProperties` entry other than freshness. odcs-check refuses rather than pretend it ran.
|
|
157
|
+
- **`datacontract-cli` runs it differently from the ODCS text, or unreliably.**
|
|
158
|
+
- `physicalType` is never checked on files and changes the cast with its own vocabulary.
|
|
159
|
+
- Freshness `unit` omitted (it assumes days) or `m` (minutes there, ambiguous in ODCS).
|
|
160
|
+
- Two freshness rules on one column overwrite each other.
|
|
161
|
+
- `retention`, `relationships` and `physicalName`.
|
|
162
|
+
- A `severity` other than `error`/`warning`; `datacontract-cli` turns `info` and `low` into warnings and every other word into a failure.
|
|
163
|
+
- Several `schema` objects, a server `path` with a glob, and several servers with no `--server`; `datacontract-cli` silently takes the first.
|
|
164
|
+
- `logicalType: array`, `object`, `map`, `vector`, `time`.
|
|
165
|
+
|
|
166
|
+
The full list, with the fixture for each, is in [`docs/spike-datacontract-cli.md`](docs/spike-datacontract-cli.md) section 2.
|
|
167
|
+
|
|
168
|
+
## Parity with datacontract-cli
|
|
169
|
+
|
|
170
|
+
CI runs every fixture contract (clean and deliberately broken data) and every dogfood contract through odcs-check and through `datacontract-cli[duckdb]==1.2.12`. It fails if odcs-check passes where `datacontract-cli` fails, or fails where it passes on a check not on the stricter list above. The last run, measured:
|
|
171
|
+
|
|
172
|
+
```console
|
|
173
|
+
$ python tests/parity.py --dogfood
|
|
174
|
+
...
|
|
175
|
+
parity: 201 cases, 0 disagreements
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
It takes about 3 minutes (`datacontract-cli` needs ~3 s per contract). Two fixtures with a quote in an object name or path pass in odcs-check but error in `datacontract-cli`, which cannot run them at all; parity treats that as no verdict.
|
|
179
|
+
|
|
180
|
+
This covers the subset above and nothing more. It is a parity test on these fixtures, not a proof for every contract.
|
|
181
|
+
|
|
182
|
+
## Dogfood
|
|
183
|
+
|
|
184
|
+
A scheduled workflow downloads the published files of two real datasets and checks them against the contracts in [`contracts/`](contracts/), every day at 06:00 UTC. A red badge means a dataset changed under its contract or odcs-check broke.
|
|
185
|
+
|
|
186
|
+
[](https://github.com/v0rein/odcs-check/actions/workflows/dogfood.yml)
|
|
187
|
+
|
|
188
|
+
| Dataset | Contracts |
|
|
189
|
+
|---|---|
|
|
190
|
+
| [harga-pangan](https://github.com/v0rein/harga-pangan): PIHPS food prices, daily | [`contracts/harga-pangan/`](contracts/harga-pangan/): 6 files |
|
|
191
|
+
| [bps-wilayah](https://github.com/v0rein/bps-wilayah): BPS to Kemendagri region codes | [`contracts/bps-wilayah/`](contracts/bps-wilayah/): 2 files; `bridge.yaml` runs twice, `--server parquet` and `--server csv` |
|
|
192
|
+
|
|
193
|
+
**It found something.** 120 rows in harga-pangan's `price.parquet` have `price_idr` above Rp 1,000,000/kg. The largest: com_9 (producer tier, Bengkulu) at 12,000,000 from 2021-06-28 to 2021-10-19, then 120,000 the next day. That looks like a x100 typo at the source. harga-pangan keeps source values untouched, so the rule is `severity: warning`, not an error:
|
|
194
|
+
|
|
195
|
+
```console
|
|
196
|
+
$ odcs-check test contracts/harga-pangan/price.yaml
|
|
197
|
+
...
|
|
198
|
+
warning sql - got 120, expected mustBe 0
|
|
199
|
+
passed freshness date
|
|
200
|
+
odcs-check: 43 checks, 42 passed, 0 failed, 1 warnings — contract valid
|
|
201
|
+
$ echo $?
|
|
202
|
+
0
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
The rule becomes an error once the dataset flags or corrects those rows.
|
|
206
|
+
|
|
207
|
+
## Development
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
conda env create -f environment.yml
|
|
211
|
+
conda activate odcs-check
|
|
212
|
+
pip install -e .
|
|
213
|
+
python tests/test_contract.py
|
|
214
|
+
python tests/test_run.py
|
|
215
|
+
python tests/test_cli.py
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Tests are plain assert scripts, no pytest. Parity needs `datacontract-cli` in a second environment, plus the dataset files (`scripts/fetch-releases.sh`, needs an authenticated `gh`):
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
pip install "datacontract-cli[duckdb,parquet]==1.2.12" # in its own env
|
|
222
|
+
DATACONTRACT=/path/to/env/bin/datacontract python tests/parity.py --dogfood
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Release: bump `odcs_check.__version__`, push a tag `v<version>`. [`publish.yml`](.github/workflows/publish.yml) checks the tag matches, builds, and publishes to PyPI by Trusted Publishing; no token is stored.
|
|
226
|
+
|
|
227
|
+
`sql` rules run with the same trust as any script you run. Do not point odcs-check at a contract you did not write.
|
|
228
|
+
|
|
229
|
+
Design: [`docs/specs/2026-12-03-odcs-check-design.md`](docs/specs/2026-12-03-odcs-check-design.md). Plan: [`docs/plans/2026-12-03-odcs-check.md`](docs/plans/2026-12-03-odcs-check.md). What `datacontract-cli` actually does: [`docs/spike-datacontract-cli.md`](docs/spike-datacontract-cli.md).
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
MIT.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""odcs-check test CONTRACT: exit 0 pass, 1 data broke the contract, 2 could not run the check."""
|
|
2
|
+
import argparse
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from . import __version__
|
|
6
|
+
from .contract import ContractError, load
|
|
7
|
+
from .run import DataError, run
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _parser() -> argparse.ArgumentParser:
|
|
11
|
+
ap = argparse.ArgumentParser(prog="odcs-check", description="Check a Parquet or CSV file against an ODCS v3 contract.")
|
|
12
|
+
ap.add_argument("--version", action="version", version=__version__)
|
|
13
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
14
|
+
t = sub.add_parser("test", help="run the contract's checks against its data file")
|
|
15
|
+
t.add_argument("contract")
|
|
16
|
+
t.add_argument("--server", help="server name, required when the contract has more than one")
|
|
17
|
+
t.add_argument("--path", help="data file to check instead of the server's path")
|
|
18
|
+
return ap
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _line(*parts: str) -> str:
|
|
22
|
+
return " ".join(" ".join(p.split()) for p in parts).rstrip()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _test(args) -> int:
|
|
26
|
+
try:
|
|
27
|
+
c = load(args.contract, args.server)
|
|
28
|
+
except ContractError as e:
|
|
29
|
+
for p in e.problems:
|
|
30
|
+
print(_line("contract error", p))
|
|
31
|
+
print(f"odcs-check: {len(e.problems)} contract problem(s) — contract invalid")
|
|
32
|
+
return 2
|
|
33
|
+
try:
|
|
34
|
+
results = run(c, args.path)
|
|
35
|
+
except DataError as e:
|
|
36
|
+
print(_line("data error", str(e)))
|
|
37
|
+
print("odcs-check: data not readable — contract not checked")
|
|
38
|
+
return 2
|
|
39
|
+
for r in results:
|
|
40
|
+
print(_line(r.status, r.check, r.column or "-", r.detail))
|
|
41
|
+
n = lambda s: sum(r.status == s for r in results) # noqa: E731
|
|
42
|
+
skipped = f", {n('not checked')} not checked" if n("not checked") else ""
|
|
43
|
+
ok = n("failed") == 0
|
|
44
|
+
print(f"odcs-check: {len(results)} checks, {n('passed')} passed, {n('failed')} failed, "
|
|
45
|
+
f"{n('warning')} warnings{skipped} — contract {'valid' if ok else 'invalid'}")
|
|
46
|
+
return 0 if ok else 1
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def main(argv: list[str] | None = None) -> int:
|
|
50
|
+
if hasattr(sys.stdout, "reconfigure"):
|
|
51
|
+
sys.stdout.reconfigure(encoding="utf-8")
|
|
52
|
+
args = _parser().parse_args(argv) # bad usage: argparse exits 2 itself
|
|
53
|
+
try:
|
|
54
|
+
return _test(args)
|
|
55
|
+
except Exception as e: # noqa: BLE001 - exit 1 means "data broke the contract", never a bug
|
|
56
|
+
print(f"odcs-check: internal error: {type(e).__name__}: {e}", file=sys.stderr)
|
|
57
|
+
return 2
|