graphmill 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.
- graphmill-0.1.0/LICENSE +21 -0
- graphmill-0.1.0/PKG-INFO +300 -0
- graphmill-0.1.0/README.md +264 -0
- graphmill-0.1.0/graphmill/__init__.py +8 -0
- graphmill-0.1.0/graphmill/adapters/__init__.py +0 -0
- graphmill-0.1.0/graphmill/adapters/clickhouse.py +96 -0
- graphmill-0.1.0/graphmill/adapters/neo4j.py +199 -0
- graphmill-0.1.0/graphmill/checks.py +209 -0
- graphmill-0.1.0/graphmill/cli.py +222 -0
- graphmill-0.1.0/graphmill/context.py +79 -0
- graphmill-0.1.0/graphmill/cypher.py +111 -0
- graphmill-0.1.0/graphmill/errors.py +2 -0
- graphmill-0.1.0/graphmill/models.py +272 -0
- graphmill-0.1.0/graphmill/planner.py +75 -0
- graphmill-0.1.0/graphmill/project.py +96 -0
- graphmill-0.1.0/graphmill/render.py +106 -0
- graphmill-0.1.0/graphmill/runner.py +288 -0
- graphmill-0.1.0/graphmill/values.py +111 -0
- graphmill-0.1.0/graphmill.egg-info/PKG-INFO +300 -0
- graphmill-0.1.0/graphmill.egg-info/SOURCES.txt +28 -0
- graphmill-0.1.0/graphmill.egg-info/dependency_links.txt +1 -0
- graphmill-0.1.0/graphmill.egg-info/entry_points.txt +2 -0
- graphmill-0.1.0/graphmill.egg-info/requires.txt +8 -0
- graphmill-0.1.0/graphmill.egg-info/top_level.txt +1 -0
- graphmill-0.1.0/pyproject.toml +54 -0
- graphmill-0.1.0/setup.cfg +4 -0
- graphmill-0.1.0/tests/test_cypher.py +43 -0
- graphmill-0.1.0/tests/test_e2e.py +197 -0
- graphmill-0.1.0/tests/test_models.py +116 -0
- graphmill-0.1.0/tests/test_values.py +51 -0
graphmill-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muhammad Mirza Fahmi
|
|
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.
|
graphmill-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: graphmill
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: dbt-style modeling for graphs: load warehouse data into Neo4j, derive relationships in Cypher, export results back.
|
|
5
|
+
Author: Muhammad Mirza Fahmi
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mmirzafahmi/graphmill
|
|
8
|
+
Project-URL: Repository, https://github.com/mmirzafahmi/graphmill
|
|
9
|
+
Project-URL: Issues, https://github.com/mmirzafahmi/graphmill/issues
|
|
10
|
+
Keywords: neo4j,clickhouse,graph,cypher,dbt,etl,data-modeling,analytics-engineering
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Database
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: click>=8.1
|
|
29
|
+
Requires-Dist: jinja2>=3.1
|
|
30
|
+
Requires-Dist: pyyaml>=6.0
|
|
31
|
+
Requires-Dist: clickhouse-connect>=0.8
|
|
32
|
+
Requires-Dist: neo4j>=5.28
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# graphmill
|
|
38
|
+
|
|
39
|
+
[](https://pypi.org/project/graphmill/)
|
|
40
|
+
[](https://github.com/mmirzafahmi/graphmill/actions/workflows/ci.yml)
|
|
41
|
+
|
|
42
|
+
dbt-style modeling for property graphs. Write SQL models that turn warehouse rows into
|
|
43
|
+
nodes and relationships, Cypher models that derive new relationships inside the graph,
|
|
44
|
+
and export models that put graph results back into the warehouse.
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
ClickHouse --[node / edge models: SQL]--> Neo4j --[relationship / cypher models]--> Neo4j
|
|
48
|
+
\--[export models: Cypher]--> ClickHouse
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Dependencies are inferred: an edge model depends on the node models of its endpoint
|
|
52
|
+
labels, and a Cypher model depends on whatever models produce the labels and
|
|
53
|
+
relationship types in its patterns. No `ref()` needed.
|
|
54
|
+
|
|
55
|
+
## Install
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install graphmill
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Requires Python 3.10+ and Neo4j 5.26+. For local services, the repo's
|
|
62
|
+
`docker-compose.yml` starts Neo4j (Browser at http://127.0.0.1:7474, `neo4j` /
|
|
63
|
+
`graphmill-dev`) and an empty ClickHouse:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
git clone https://github.com/mmirzafahmi/graphmill.git && cd graphmill
|
|
67
|
+
docker compose --profile local-warehouse up -d
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Quickstart
|
|
71
|
+
|
|
72
|
+
A minimal project: a bill of materials, exploded into raw-material quantities. The
|
|
73
|
+
source rows are inline `VALUES`, so it runs against the empty local ClickHouse.
|
|
74
|
+
|
|
75
|
+
`my_graph/project.yml`
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
name: my_graph
|
|
79
|
+
connections:
|
|
80
|
+
clickhouse:
|
|
81
|
+
host: "{{ env_var('CLICKHOUSE_HOST', '127.0.0.1') }}"
|
|
82
|
+
port: "{{ env_var('CLICKHOUSE_PORT', '8123') }}"
|
|
83
|
+
username: "{{ env_var('CLICKHOUSE_USER', 'default') }}"
|
|
84
|
+
password: "{{ env_var('CLICKHOUSE_PASSWORD', 'graphmill-dev') }}"
|
|
85
|
+
secure: "{{ env_var('CLICKHOUSE_SECURE', 'false') }}"
|
|
86
|
+
neo4j:
|
|
87
|
+
uri: "{{ env_var('NEO4J_URI', 'bolt://127.0.0.1:7687') }}"
|
|
88
|
+
user: "{{ env_var('NEO4J_USER', 'neo4j') }}"
|
|
89
|
+
password: "{{ env_var('NEO4J_PASSWORD', 'graphmill-dev') }}"
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`my_graph/models/nodes/product.sql`
|
|
93
|
+
|
|
94
|
+
```sql
|
|
95
|
+
{{ config(label='Product', key='sku') }}
|
|
96
|
+
SELECT * FROM VALUES('sku String, name String',
|
|
97
|
+
('bread', 'Bread'), ('dough', 'Dough'), ('flour', 'Flour'), ('water', 'Water'))
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`my_graph/models/edges/uses.sql`
|
|
101
|
+
|
|
102
|
+
```sql
|
|
103
|
+
{{ config(type='USES', from_label='Product', from_key='parent',
|
|
104
|
+
to_label='Product', to_key='component', tests=['acyclic']) }}
|
|
105
|
+
SELECT * FROM VALUES('parent String, component String, qty_per_unit Float64',
|
|
106
|
+
('bread', 'dough', 0.5), ('dough', 'flour', 0.6), ('dough', 'water', 0.4))
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`my_graph/models/derived/contains_raw.cypher`
|
|
110
|
+
|
|
111
|
+
```cypher
|
|
112
|
+
{{ config(materialized='relationship', type='CONTAINS_RAW') }}
|
|
113
|
+
MATCH path = (p:Product)-[:USES*1..10]->(raw:Product)
|
|
114
|
+
WHERE NOT EXISTS { (raw)-[:USES]->() }
|
|
115
|
+
WITH p, raw, sum(reduce(q = 1.0, r IN relationships(path) | q * r.qty_per_unit)) AS qty
|
|
116
|
+
MERGE (p)-[c:CONTAINS_RAW]->(raw)
|
|
117
|
+
SET c.qty_per_unit = qty, {{ stamp('c') }}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`my_graph/models/exports/raw_materials.cypher`
|
|
121
|
+
|
|
122
|
+
```cypher
|
|
123
|
+
{{ config(table='default.raw_materials',
|
|
124
|
+
columns={'product': 'String', 'raw': 'String', 'qty_per_unit': 'Float64'}) }}
|
|
125
|
+
MATCH (p:Product)-[c:CONTAINS_RAW]->(raw:Product)
|
|
126
|
+
RETURN p.sku AS product, raw.sku AS raw, c.qty_per_unit AS qty_per_unit
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
cd my_graph
|
|
131
|
+
graphmill debug # check both connections
|
|
132
|
+
graphmill ls # run order: product -> uses -> contains_raw -> raw_materials
|
|
133
|
+
graphmill plan # what would change
|
|
134
|
+
graphmill run # load, derive, export: bread contains 0.3 flour and 0.2 water
|
|
135
|
+
graphmill test # data tests
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
To use your own warehouse, set the `CLICKHOUSE_*` variables (or put them in a `.env`
|
|
139
|
+
file next to `project.yml`) and point the models at your tables with
|
|
140
|
+
`{{ source('database', 'table') }}`.
|
|
141
|
+
|
|
142
|
+
Use `127.0.0.1` rather than `localhost` for local services: on Windows, `localhost`
|
|
143
|
+
tries IPv6 first and each new connection stalls for 10-20 seconds.
|
|
144
|
+
|
|
145
|
+
## Project layout
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
project.yml # name, vars, connections (Jinja: env_var())
|
|
149
|
+
models/
|
|
150
|
+
nodes/*.sql # materialized='node' (folder sets the default)
|
|
151
|
+
edges/*.sql # materialized='edge'
|
|
152
|
+
derived/*.cypher # materialized='cypher' or 'relationship'
|
|
153
|
+
exports/*.cypher # materialized='export'
|
|
154
|
+
tests/*.cypher # singular tests: a query returning failing rows
|
|
155
|
+
target/ # compiled/ and run_results.json
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Model types
|
|
159
|
+
|
|
160
|
+
Every model starts with `{{ config(...) }}`. Common keys: `materialized`, `depends_on`
|
|
161
|
+
(extra dependencies), `enabled`, `batch_size`, `tests`.
|
|
162
|
+
|
|
163
|
+
| materialized | language | required config | optional config |
|
|
164
|
+
|---|---|---|---|
|
|
165
|
+
| `node` | SQL | `label`, `key` (column, becomes the unique property) | `labels` (extra static labels), `incremental`, `allow_empty`, `reconcile` |
|
|
166
|
+
| `edge` | SQL | `type`, `from_label`, `from_key`, `to_label`, `to_key` (columns holding endpoint keys) | `key` (relationship identity column), `on_missing_endpoint` (`fail`/`warn`), `incremental`, `allow_empty`, `reconcile` |
|
|
167
|
+
| `relationship` | Cypher | `type` (one or a list of derived relationship types) | |
|
|
168
|
+
| `cypher` | Cypher | | |
|
|
169
|
+
| `export` | Cypher | `table` (`db.table`), `columns` (name -> ClickHouse type) | `order_by`, `engine` |
|
|
170
|
+
|
|
171
|
+
Every column of a node/edge query other than the key columns becomes a property.
|
|
172
|
+
|
|
173
|
+
```sql
|
|
174
|
+
-- models/edges/uses.sql
|
|
175
|
+
{{ config(materialized='edge', type='USES',
|
|
176
|
+
from_label='Product', from_key='parent_key',
|
|
177
|
+
to_label='Product', to_key='component_key',
|
|
178
|
+
key='line_key', tests=['acyclic']) }}
|
|
179
|
+
SELECT line_key, parent_key, component_key, qty_per_unit FROM ...
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
```cypher
|
|
183
|
+
// models/derived/contains_raw.cypher
|
|
184
|
+
{{ config(materialized='relationship', type='CONTAINS_RAW') }}
|
|
185
|
+
MATCH path = (p:Product)-[:USES*1..10]->(raw:Product)
|
|
186
|
+
WHERE NOT EXISTS { (raw)-[:USES]->() }
|
|
187
|
+
WITH p, raw, sum(reduce(q = 1.0, r IN relationships(path) | q * r.qty_per_unit)) AS qty
|
|
188
|
+
MERGE (p)-[c:CONTAINS_RAW]->(raw)
|
|
189
|
+
SET c.qty_per_unit = qty, {{ stamp('c') }}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Cypher models may contain several statements separated by `;` (e.g. GDS project, run,
|
|
193
|
+
drop). Each runs in an auto-commit transaction, so `CALL { ... } IN TRANSACTIONS` works.
|
|
194
|
+
Statements receive `$run_id` and `$model` parameters.
|
|
195
|
+
|
|
196
|
+
### Jinja functions
|
|
197
|
+
|
|
198
|
+
| function | |
|
|
199
|
+
|---|---|
|
|
200
|
+
| `config(**kw)` | model configuration |
|
|
201
|
+
| `var(name, default)` | project vars, overridable with `--vars "{k: v}"` |
|
|
202
|
+
| `env_var(name, default)` | environment variables (`.env` is loaded automatically) |
|
|
203
|
+
| `source(db, table)` | quoted ClickHouse table name |
|
|
204
|
+
| `ref(model)` | the ClickHouse table of an export model (adds a dependency) |
|
|
205
|
+
| `is_incremental()`, `watermark()` | incremental loading, see below |
|
|
206
|
+
| `stamp(alias)` | `SET` items that mark what a relationship model wrote |
|
|
207
|
+
|
|
208
|
+
## Commands
|
|
209
|
+
|
|
210
|
+
| command | |
|
|
211
|
+
|---|---|
|
|
212
|
+
| `graphmill debug` | validate the project, test both connections |
|
|
213
|
+
| `graphmill ls` | models in run order with dependencies, and the graph schema |
|
|
214
|
+
| `graphmill compile [--show]` | render models to `target/compiled/` |
|
|
215
|
+
| `graphmill plan` | per node/edge model: how many elements would be created, updated, deleted |
|
|
216
|
+
| `graphmill run [--full-refresh]` | execute models in dependency order |
|
|
217
|
+
| `graphmill test` | run data tests |
|
|
218
|
+
|
|
219
|
+
All accept `-s/--select` (`name`, glob, `+name` with upstream, `name+` with downstream),
|
|
220
|
+
`-p/--project-dir` and `--vars`.
|
|
221
|
+
|
|
222
|
+
## How loading works
|
|
223
|
+
|
|
224
|
+
- **Keys and constraints.** Each node model gets a uniqueness constraint on its key before
|
|
225
|
+
loading. Without one, every `MERGE` scans the whole label.
|
|
226
|
+
- **Upserts.** Rows are streamed from ClickHouse and written in `UNWIND ... MERGE` batches.
|
|
227
|
+
`SET n += row` means a source value that became NULL removes the property.
|
|
228
|
+
- **Types.** Integers beyond int64 become strings, `Decimal` becomes float, maps and
|
|
229
|
+
nested/mixed arrays become JSON strings. ClickHouse comparisons and `AND`/`OR` return
|
|
230
|
+
`UInt8`, so `CAST(... AS Bool)` anything you want to use as a boolean in Cypher.
|
|
231
|
+
- **Mark and sweep.** Everything written is stamped with `_model`, `_run_id` and
|
|
232
|
+
`_loaded_at`. After a full run, elements the model wrote in earlier runs but not this
|
|
233
|
+
one are deleted. This handles source deletes without a separate delete feed.
|
|
234
|
+
- **Empty-source guard.** If a full run's query returns zero rows while the graph has data
|
|
235
|
+
from that model, the run fails instead of sweeping everything (`allow_empty=True` to
|
|
236
|
+
override).
|
|
237
|
+
- **Missing endpoints.** An edge row whose endpoint node does not exist would otherwise be
|
|
238
|
+
dropped silently by `MATCH`. graphmill counts written vs read, reports examples, and fails
|
|
239
|
+
the model (or warns with `on_missing_endpoint='warn'`).
|
|
240
|
+
- **Keyed relationships.** With `key=`, a relationship whose endpoints changed is moved,
|
|
241
|
+
not duplicated.
|
|
242
|
+
- **Incremental.** With `incremental='write_date'`, the max value of that column is stored
|
|
243
|
+
in the graph (`:_GraphmillState`). On the next run `is_incremental()` is true:
|
|
244
|
+
|
|
245
|
+
```sql
|
|
246
|
+
SELECT ... FROM ...
|
|
247
|
+
{% if is_incremental() %} WHERE write_date >= {{ watermark() }} {% endif %}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Use `>=`: re-reading boundary rows is harmless because writes are idempotent.
|
|
251
|
+
Incremental runs do not sweep, so hard deletes in the source are only removed by a
|
|
252
|
+
periodic `--full-refresh`.
|
|
253
|
+
- **Exports** are written to `<table>__graphmill_tmp` and swapped in with
|
|
254
|
+
`EXCHANGE TABLES`, so readers never see a half-written table.
|
|
255
|
+
- Failed models skip their downstream models; results go to `target/run_results.json`.
|
|
256
|
+
|
|
257
|
+
## Tests
|
|
258
|
+
|
|
259
|
+
`graphmill test` runs:
|
|
260
|
+
|
|
261
|
+
- `unique_key:<model>`: duplicate keys in a node/edge model's source query (ClickHouse).
|
|
262
|
+
- `reconcile:<model>`: distinct source keys vs elements in the graph (disable with
|
|
263
|
+
`reconcile=False`).
|
|
264
|
+
- Generic tests from `config(tests=[...])`:
|
|
265
|
+
- `acyclic` (`max_depth`, default 10): no cycles along the model's relationship type.
|
|
266
|
+
- `{'degree': {'direction': 'out'|'in', 'min': 1, 'max': 1, 'where': "n.active"}}`.
|
|
267
|
+
- Singular tests: `tests/*.cypher` queries that return failing rows. Add
|
|
268
|
+
`{{ config(severity='warn') }}` to warn instead of fail. With `--select`, only singular
|
|
269
|
+
tests touching the selected models' labels/types run.
|
|
270
|
+
|
|
271
|
+
## Development
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
python -m venv .venv
|
|
275
|
+
.venv/Scripts/pip install -e ".[dev]" # macOS/Linux: .venv/bin/pip
|
|
276
|
+
pytest # unit tests
|
|
277
|
+
docker compose --profile local-warehouse up -d
|
|
278
|
+
GRAPHMILL_E2E=1 pytest # + end-to-end tests (they wipe the local Neo4j)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Releasing
|
|
282
|
+
|
|
283
|
+
1. Bump `version` in `pyproject.toml` and commit.
|
|
284
|
+
2. Tag and push: `git tag v0.2.0 && git push origin v0.2.0`.
|
|
285
|
+
|
|
286
|
+
The `Publish to PyPI` workflow checks that the tag matches the version, runs the tests,
|
|
287
|
+
builds, and publishes through PyPI trusted publishing.
|
|
288
|
+
|
|
289
|
+
## Limitations (v0.1)
|
|
290
|
+
|
|
291
|
+
- One node model per label and one model per relationship type.
|
|
292
|
+
- Neo4j 5.26+ (uses `CALL (x) { ... } IN TRANSACTIONS`). Memgraph would need its own adapter.
|
|
293
|
+
- Dependency inference reads labels/types from node and relationship patterns. Labels
|
|
294
|
+
only mentioned in strings (e.g. `gds.graph.project('g', 'Product', ...)`) need
|
|
295
|
+
`depends_on=[...]`.
|
|
296
|
+
- `plan` pulls all keys from both sides to diff them. Fine for millions, not billions.
|
|
297
|
+
|
|
298
|
+
## License
|
|
299
|
+
|
|
300
|
+
MIT, see [LICENSE](https://github.com/mmirzafahmi/graphmill/blob/main/LICENSE).
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# graphmill
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/graphmill/)
|
|
4
|
+
[](https://github.com/mmirzafahmi/graphmill/actions/workflows/ci.yml)
|
|
5
|
+
|
|
6
|
+
dbt-style modeling for property graphs. Write SQL models that turn warehouse rows into
|
|
7
|
+
nodes and relationships, Cypher models that derive new relationships inside the graph,
|
|
8
|
+
and export models that put graph results back into the warehouse.
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
ClickHouse --[node / edge models: SQL]--> Neo4j --[relationship / cypher models]--> Neo4j
|
|
12
|
+
\--[export models: Cypher]--> ClickHouse
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Dependencies are inferred: an edge model depends on the node models of its endpoint
|
|
16
|
+
labels, and a Cypher model depends on whatever models produce the labels and
|
|
17
|
+
relationship types in its patterns. No `ref()` needed.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install graphmill
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Requires Python 3.10+ and Neo4j 5.26+. For local services, the repo's
|
|
26
|
+
`docker-compose.yml` starts Neo4j (Browser at http://127.0.0.1:7474, `neo4j` /
|
|
27
|
+
`graphmill-dev`) and an empty ClickHouse:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
git clone https://github.com/mmirzafahmi/graphmill.git && cd graphmill
|
|
31
|
+
docker compose --profile local-warehouse up -d
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quickstart
|
|
35
|
+
|
|
36
|
+
A minimal project: a bill of materials, exploded into raw-material quantities. The
|
|
37
|
+
source rows are inline `VALUES`, so it runs against the empty local ClickHouse.
|
|
38
|
+
|
|
39
|
+
`my_graph/project.yml`
|
|
40
|
+
|
|
41
|
+
```yaml
|
|
42
|
+
name: my_graph
|
|
43
|
+
connections:
|
|
44
|
+
clickhouse:
|
|
45
|
+
host: "{{ env_var('CLICKHOUSE_HOST', '127.0.0.1') }}"
|
|
46
|
+
port: "{{ env_var('CLICKHOUSE_PORT', '8123') }}"
|
|
47
|
+
username: "{{ env_var('CLICKHOUSE_USER', 'default') }}"
|
|
48
|
+
password: "{{ env_var('CLICKHOUSE_PASSWORD', 'graphmill-dev') }}"
|
|
49
|
+
secure: "{{ env_var('CLICKHOUSE_SECURE', 'false') }}"
|
|
50
|
+
neo4j:
|
|
51
|
+
uri: "{{ env_var('NEO4J_URI', 'bolt://127.0.0.1:7687') }}"
|
|
52
|
+
user: "{{ env_var('NEO4J_USER', 'neo4j') }}"
|
|
53
|
+
password: "{{ env_var('NEO4J_PASSWORD', 'graphmill-dev') }}"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`my_graph/models/nodes/product.sql`
|
|
57
|
+
|
|
58
|
+
```sql
|
|
59
|
+
{{ config(label='Product', key='sku') }}
|
|
60
|
+
SELECT * FROM VALUES('sku String, name String',
|
|
61
|
+
('bread', 'Bread'), ('dough', 'Dough'), ('flour', 'Flour'), ('water', 'Water'))
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`my_graph/models/edges/uses.sql`
|
|
65
|
+
|
|
66
|
+
```sql
|
|
67
|
+
{{ config(type='USES', from_label='Product', from_key='parent',
|
|
68
|
+
to_label='Product', to_key='component', tests=['acyclic']) }}
|
|
69
|
+
SELECT * FROM VALUES('parent String, component String, qty_per_unit Float64',
|
|
70
|
+
('bread', 'dough', 0.5), ('dough', 'flour', 0.6), ('dough', 'water', 0.4))
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`my_graph/models/derived/contains_raw.cypher`
|
|
74
|
+
|
|
75
|
+
```cypher
|
|
76
|
+
{{ config(materialized='relationship', type='CONTAINS_RAW') }}
|
|
77
|
+
MATCH path = (p:Product)-[:USES*1..10]->(raw:Product)
|
|
78
|
+
WHERE NOT EXISTS { (raw)-[:USES]->() }
|
|
79
|
+
WITH p, raw, sum(reduce(q = 1.0, r IN relationships(path) | q * r.qty_per_unit)) AS qty
|
|
80
|
+
MERGE (p)-[c:CONTAINS_RAW]->(raw)
|
|
81
|
+
SET c.qty_per_unit = qty, {{ stamp('c') }}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`my_graph/models/exports/raw_materials.cypher`
|
|
85
|
+
|
|
86
|
+
```cypher
|
|
87
|
+
{{ config(table='default.raw_materials',
|
|
88
|
+
columns={'product': 'String', 'raw': 'String', 'qty_per_unit': 'Float64'}) }}
|
|
89
|
+
MATCH (p:Product)-[c:CONTAINS_RAW]->(raw:Product)
|
|
90
|
+
RETURN p.sku AS product, raw.sku AS raw, c.qty_per_unit AS qty_per_unit
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
cd my_graph
|
|
95
|
+
graphmill debug # check both connections
|
|
96
|
+
graphmill ls # run order: product -> uses -> contains_raw -> raw_materials
|
|
97
|
+
graphmill plan # what would change
|
|
98
|
+
graphmill run # load, derive, export: bread contains 0.3 flour and 0.2 water
|
|
99
|
+
graphmill test # data tests
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
To use your own warehouse, set the `CLICKHOUSE_*` variables (or put them in a `.env`
|
|
103
|
+
file next to `project.yml`) and point the models at your tables with
|
|
104
|
+
`{{ source('database', 'table') }}`.
|
|
105
|
+
|
|
106
|
+
Use `127.0.0.1` rather than `localhost` for local services: on Windows, `localhost`
|
|
107
|
+
tries IPv6 first and each new connection stalls for 10-20 seconds.
|
|
108
|
+
|
|
109
|
+
## Project layout
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
project.yml # name, vars, connections (Jinja: env_var())
|
|
113
|
+
models/
|
|
114
|
+
nodes/*.sql # materialized='node' (folder sets the default)
|
|
115
|
+
edges/*.sql # materialized='edge'
|
|
116
|
+
derived/*.cypher # materialized='cypher' or 'relationship'
|
|
117
|
+
exports/*.cypher # materialized='export'
|
|
118
|
+
tests/*.cypher # singular tests: a query returning failing rows
|
|
119
|
+
target/ # compiled/ and run_results.json
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Model types
|
|
123
|
+
|
|
124
|
+
Every model starts with `{{ config(...) }}`. Common keys: `materialized`, `depends_on`
|
|
125
|
+
(extra dependencies), `enabled`, `batch_size`, `tests`.
|
|
126
|
+
|
|
127
|
+
| materialized | language | required config | optional config |
|
|
128
|
+
|---|---|---|---|
|
|
129
|
+
| `node` | SQL | `label`, `key` (column, becomes the unique property) | `labels` (extra static labels), `incremental`, `allow_empty`, `reconcile` |
|
|
130
|
+
| `edge` | SQL | `type`, `from_label`, `from_key`, `to_label`, `to_key` (columns holding endpoint keys) | `key` (relationship identity column), `on_missing_endpoint` (`fail`/`warn`), `incremental`, `allow_empty`, `reconcile` |
|
|
131
|
+
| `relationship` | Cypher | `type` (one or a list of derived relationship types) | |
|
|
132
|
+
| `cypher` | Cypher | | |
|
|
133
|
+
| `export` | Cypher | `table` (`db.table`), `columns` (name -> ClickHouse type) | `order_by`, `engine` |
|
|
134
|
+
|
|
135
|
+
Every column of a node/edge query other than the key columns becomes a property.
|
|
136
|
+
|
|
137
|
+
```sql
|
|
138
|
+
-- models/edges/uses.sql
|
|
139
|
+
{{ config(materialized='edge', type='USES',
|
|
140
|
+
from_label='Product', from_key='parent_key',
|
|
141
|
+
to_label='Product', to_key='component_key',
|
|
142
|
+
key='line_key', tests=['acyclic']) }}
|
|
143
|
+
SELECT line_key, parent_key, component_key, qty_per_unit FROM ...
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```cypher
|
|
147
|
+
// models/derived/contains_raw.cypher
|
|
148
|
+
{{ config(materialized='relationship', type='CONTAINS_RAW') }}
|
|
149
|
+
MATCH path = (p:Product)-[:USES*1..10]->(raw:Product)
|
|
150
|
+
WHERE NOT EXISTS { (raw)-[:USES]->() }
|
|
151
|
+
WITH p, raw, sum(reduce(q = 1.0, r IN relationships(path) | q * r.qty_per_unit)) AS qty
|
|
152
|
+
MERGE (p)-[c:CONTAINS_RAW]->(raw)
|
|
153
|
+
SET c.qty_per_unit = qty, {{ stamp('c') }}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Cypher models may contain several statements separated by `;` (e.g. GDS project, run,
|
|
157
|
+
drop). Each runs in an auto-commit transaction, so `CALL { ... } IN TRANSACTIONS` works.
|
|
158
|
+
Statements receive `$run_id` and `$model` parameters.
|
|
159
|
+
|
|
160
|
+
### Jinja functions
|
|
161
|
+
|
|
162
|
+
| function | |
|
|
163
|
+
|---|---|
|
|
164
|
+
| `config(**kw)` | model configuration |
|
|
165
|
+
| `var(name, default)` | project vars, overridable with `--vars "{k: v}"` |
|
|
166
|
+
| `env_var(name, default)` | environment variables (`.env` is loaded automatically) |
|
|
167
|
+
| `source(db, table)` | quoted ClickHouse table name |
|
|
168
|
+
| `ref(model)` | the ClickHouse table of an export model (adds a dependency) |
|
|
169
|
+
| `is_incremental()`, `watermark()` | incremental loading, see below |
|
|
170
|
+
| `stamp(alias)` | `SET` items that mark what a relationship model wrote |
|
|
171
|
+
|
|
172
|
+
## Commands
|
|
173
|
+
|
|
174
|
+
| command | |
|
|
175
|
+
|---|---|
|
|
176
|
+
| `graphmill debug` | validate the project, test both connections |
|
|
177
|
+
| `graphmill ls` | models in run order with dependencies, and the graph schema |
|
|
178
|
+
| `graphmill compile [--show]` | render models to `target/compiled/` |
|
|
179
|
+
| `graphmill plan` | per node/edge model: how many elements would be created, updated, deleted |
|
|
180
|
+
| `graphmill run [--full-refresh]` | execute models in dependency order |
|
|
181
|
+
| `graphmill test` | run data tests |
|
|
182
|
+
|
|
183
|
+
All accept `-s/--select` (`name`, glob, `+name` with upstream, `name+` with downstream),
|
|
184
|
+
`-p/--project-dir` and `--vars`.
|
|
185
|
+
|
|
186
|
+
## How loading works
|
|
187
|
+
|
|
188
|
+
- **Keys and constraints.** Each node model gets a uniqueness constraint on its key before
|
|
189
|
+
loading. Without one, every `MERGE` scans the whole label.
|
|
190
|
+
- **Upserts.** Rows are streamed from ClickHouse and written in `UNWIND ... MERGE` batches.
|
|
191
|
+
`SET n += row` means a source value that became NULL removes the property.
|
|
192
|
+
- **Types.** Integers beyond int64 become strings, `Decimal` becomes float, maps and
|
|
193
|
+
nested/mixed arrays become JSON strings. ClickHouse comparisons and `AND`/`OR` return
|
|
194
|
+
`UInt8`, so `CAST(... AS Bool)` anything you want to use as a boolean in Cypher.
|
|
195
|
+
- **Mark and sweep.** Everything written is stamped with `_model`, `_run_id` and
|
|
196
|
+
`_loaded_at`. After a full run, elements the model wrote in earlier runs but not this
|
|
197
|
+
one are deleted. This handles source deletes without a separate delete feed.
|
|
198
|
+
- **Empty-source guard.** If a full run's query returns zero rows while the graph has data
|
|
199
|
+
from that model, the run fails instead of sweeping everything (`allow_empty=True` to
|
|
200
|
+
override).
|
|
201
|
+
- **Missing endpoints.** An edge row whose endpoint node does not exist would otherwise be
|
|
202
|
+
dropped silently by `MATCH`. graphmill counts written vs read, reports examples, and fails
|
|
203
|
+
the model (or warns with `on_missing_endpoint='warn'`).
|
|
204
|
+
- **Keyed relationships.** With `key=`, a relationship whose endpoints changed is moved,
|
|
205
|
+
not duplicated.
|
|
206
|
+
- **Incremental.** With `incremental='write_date'`, the max value of that column is stored
|
|
207
|
+
in the graph (`:_GraphmillState`). On the next run `is_incremental()` is true:
|
|
208
|
+
|
|
209
|
+
```sql
|
|
210
|
+
SELECT ... FROM ...
|
|
211
|
+
{% if is_incremental() %} WHERE write_date >= {{ watermark() }} {% endif %}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Use `>=`: re-reading boundary rows is harmless because writes are idempotent.
|
|
215
|
+
Incremental runs do not sweep, so hard deletes in the source are only removed by a
|
|
216
|
+
periodic `--full-refresh`.
|
|
217
|
+
- **Exports** are written to `<table>__graphmill_tmp` and swapped in with
|
|
218
|
+
`EXCHANGE TABLES`, so readers never see a half-written table.
|
|
219
|
+
- Failed models skip their downstream models; results go to `target/run_results.json`.
|
|
220
|
+
|
|
221
|
+
## Tests
|
|
222
|
+
|
|
223
|
+
`graphmill test` runs:
|
|
224
|
+
|
|
225
|
+
- `unique_key:<model>`: duplicate keys in a node/edge model's source query (ClickHouse).
|
|
226
|
+
- `reconcile:<model>`: distinct source keys vs elements in the graph (disable with
|
|
227
|
+
`reconcile=False`).
|
|
228
|
+
- Generic tests from `config(tests=[...])`:
|
|
229
|
+
- `acyclic` (`max_depth`, default 10): no cycles along the model's relationship type.
|
|
230
|
+
- `{'degree': {'direction': 'out'|'in', 'min': 1, 'max': 1, 'where': "n.active"}}`.
|
|
231
|
+
- Singular tests: `tests/*.cypher` queries that return failing rows. Add
|
|
232
|
+
`{{ config(severity='warn') }}` to warn instead of fail. With `--select`, only singular
|
|
233
|
+
tests touching the selected models' labels/types run.
|
|
234
|
+
|
|
235
|
+
## Development
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
python -m venv .venv
|
|
239
|
+
.venv/Scripts/pip install -e ".[dev]" # macOS/Linux: .venv/bin/pip
|
|
240
|
+
pytest # unit tests
|
|
241
|
+
docker compose --profile local-warehouse up -d
|
|
242
|
+
GRAPHMILL_E2E=1 pytest # + end-to-end tests (they wipe the local Neo4j)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Releasing
|
|
246
|
+
|
|
247
|
+
1. Bump `version` in `pyproject.toml` and commit.
|
|
248
|
+
2. Tag and push: `git tag v0.2.0 && git push origin v0.2.0`.
|
|
249
|
+
|
|
250
|
+
The `Publish to PyPI` workflow checks that the tag matches the version, runs the tests,
|
|
251
|
+
builds, and publishes through PyPI trusted publishing.
|
|
252
|
+
|
|
253
|
+
## Limitations (v0.1)
|
|
254
|
+
|
|
255
|
+
- One node model per label and one model per relationship type.
|
|
256
|
+
- Neo4j 5.26+ (uses `CALL (x) { ... } IN TRANSACTIONS`). Memgraph would need its own adapter.
|
|
257
|
+
- Dependency inference reads labels/types from node and relationship patterns. Labels
|
|
258
|
+
only mentioned in strings (e.g. `gds.graph.project('g', 'Product', ...)`) need
|
|
259
|
+
`depends_on=[...]`.
|
|
260
|
+
- `plan` pulls all keys from both sides to diff them. Fine for millions, not billions.
|
|
261
|
+
|
|
262
|
+
## License
|
|
263
|
+
|
|
264
|
+
MIT, see [LICENSE](https://github.com/mmirzafahmi/graphmill/blob/main/LICENSE).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""graphmill: dbt-style modeling for property graphs."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
__version__ = version("graphmill") # single source of truth: pyproject.toml
|
|
7
|
+
except PackageNotFoundError: # running from a source tree that isn't installed
|
|
8
|
+
__version__ = "0.0.0+unknown"
|
|
File without changes
|