cypher2sql 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. cypher2sql-0.1.0/.gitignore +18 -0
  2. cypher2sql-0.1.0/CHANGELOG.md +108 -0
  3. cypher2sql-0.1.0/LICENSE +202 -0
  4. cypher2sql-0.1.0/PKG-INFO +95 -0
  5. cypher2sql-0.1.0/README.md +74 -0
  6. cypher2sql-0.1.0/docs/VISION.md +82 -0
  7. cypher2sql-0.1.0/docs/api-semantics.md +285 -0
  8. cypher2sql-0.1.0/docs/api.md +260 -0
  9. cypher2sql-0.1.0/docs/assets/charts/c2s-charts.css +2 -0
  10. cypher2sql-0.1.0/docs/assets/charts/c2s-charts.data.json +375 -0
  11. cypher2sql-0.1.0/docs/assets/charts/c2s-charts.js +32116 -0
  12. cypher2sql-0.1.0/docs/assets/stylesheets/extra.css +55 -0
  13. cypher2sql-0.1.0/docs/design.md +622 -0
  14. cypher2sql-0.1.0/docs/index.md +24 -0
  15. cypher2sql-0.1.0/docs/notes/51-walk-plan.md +62 -0
  16. cypher2sql-0.1.0/docs/notes/LESSONS.md +7 -0
  17. cypher2sql-0.1.0/docs/notes/tstrings-vs-psycopg.md +124 -0
  18. cypher2sql-0.1.0/docs/notes/zensical-plugins-and-charts.md +50 -0
  19. cypher2sql-0.1.0/docs/performance.md +224 -0
  20. cypher2sql-0.1.0/docs/release.md +132 -0
  21. cypher2sql-0.1.0/docs/tutorial.md +662 -0
  22. cypher2sql-0.1.0/pyproject.toml +78 -0
  23. cypher2sql-0.1.0/src/cypher2sql/__init__.py +36 -0
  24. cypher2sql-0.1.0/src/cypher2sql/_ast.py +426 -0
  25. cypher2sql-0.1.0/src/cypher2sql/_bind/__init__.py +46 -0
  26. cypher2sql-0.1.0/src/cypher2sql/_bind/_base.py +437 -0
  27. cypher2sql-0.1.0/src/cypher2sql/_bind/_binder.py +387 -0
  28. cypher2sql-0.1.0/src/cypher2sql/_bind/_build.py +701 -0
  29. cypher2sql-0.1.0/src/cypher2sql/_bind/_edges.py +354 -0
  30. cypher2sql-0.1.0/src/cypher2sql/_bind/_names.py +72 -0
  31. cypher2sql-0.1.0/src/cypher2sql/_bind/_patterns.py +633 -0
  32. cypher2sql-0.1.0/src/cypher2sql/_bind/_predicates.py +560 -0
  33. cypher2sql-0.1.0/src/cypher2sql/_bind/_values.py +664 -0
  34. cypher2sql-0.1.0/src/cypher2sql/_compare.py +265 -0
  35. cypher2sql-0.1.0/src/cypher2sql/_emit/__init__.py +42 -0
  36. cypher2sql-0.1.0/src/cypher2sql/_emit/_core.py +362 -0
  37. cypher2sql-0.1.0/src/cypher2sql/_emit/_walks.py +622 -0
  38. cypher2sql-0.1.0/src/cypher2sql/_errors.py +396 -0
  39. cypher2sql-0.1.0/src/cypher2sql/_infer.py +287 -0
  40. cypher2sql-0.1.0/src/cypher2sql/_ir.py +663 -0
  41. cypher2sql-0.1.0/src/cypher2sql/_lexer.py +557 -0
  42. cypher2sql-0.1.0/src/cypher2sql/_limits.py +79 -0
  43. cypher2sql-0.1.0/src/cypher2sql/_parser/__init__.py +90 -0
  44. cypher2sql-0.1.0/src/cypher2sql/_parser/_base.py +234 -0
  45. cypher2sql-0.1.0/src/cypher2sql/_parser/_clauses.py +404 -0
  46. cypher2sql-0.1.0/src/cypher2sql/_parser/_grammar.py +680 -0
  47. cypher2sql-0.1.0/src/cypher2sql/_plan/__init__.py +143 -0
  48. cypher2sql-0.1.0/src/cypher2sql/_plan/_groups.py +282 -0
  49. cypher2sql-0.1.0/src/cypher2sql/_plan/_nodes.py +562 -0
  50. cypher2sql-0.1.0/src/cypher2sql/_plan/_planner.py +794 -0
  51. cypher2sql-0.1.0/src/cypher2sql/_plan/_rows.py +803 -0
  52. cypher2sql-0.1.0/src/cypher2sql/_plan/_scopes.py +104 -0
  53. cypher2sql-0.1.0/src/cypher2sql/_plan/_uniqueness.py +84 -0
  54. cypher2sql-0.1.0/src/cypher2sql/_plan/_working.py +99 -0
  55. cypher2sql-0.1.0/src/cypher2sql/_schema.py +519 -0
  56. cypher2sql-0.1.0/src/cypher2sql/_template.py +61 -0
  57. cypher2sql-0.1.0/src/cypher2sql/_unsupported.py +170 -0
  58. cypher2sql-0.1.0/src/cypher2sql/py.typed +0 -0
@@ -0,0 +1,18 @@
1
+ .venv/
2
+ .cache/
3
+ __pycache__/
4
+ *.pyc
5
+ .pytest_cache/
6
+ .mypy_cache/
7
+ .ruff_cache/
8
+ dist/
9
+ # The built documentation site (`make docs`); the Markdown under docs/ is the source.
10
+ site/
11
+ .worktrees/
12
+ # Local harness runtime state (autopilot flag, journal, budget report)
13
+ .harness/state/
14
+ .opencode/plugins/*/node_modules/
15
+ # Dev-only chart build (`make charts`): dependencies and the module build.mjs regenerates from
16
+ # scripts/bench/bench.json before every build. The committed output is docs/assets/charts/.
17
+ charts-src/node_modules/
18
+ charts-src/src/lib/benchData.js
@@ -0,0 +1,108 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to
5
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## 0.1.0 — 2026-11-29
8
+
9
+ First usable release: describe your PostgreSQL tables and foreign keys once, write a Cypher
10
+ pattern, and get back one parameterized SQL query that selects the primary keys you asked for.
11
+ Zero runtime dependencies; Python ≥ 3.12; PostgreSQL ≥ 15.
12
+
13
+ ### Added
14
+
15
+ - **Schema protocol and reference classes.** `Node`, `Edge`, `Column`, `Schema` and `Limits`
16
+ describe tables, primary keys, foreign keys, join tables and property types in plain Python
17
+ dataclasses; `Schema.to_sql(cypher, params=None)` compiles one query. A node's key may be a tuple
18
+ (composite keys), a property may rename its column, and an edge names the columns on each side
19
+ of a foreign key or join table — one declaration covers join inference, direction and key shape.
20
+ - **`MATCH` / `WHERE` / `RETURN`.** Label and relationship-type selectors, unnamed nodes such as
21
+ `(:Order)`, property maps, `AND` / `OR` / `XOR` / `NOT`, `=`, `<>`, `<`, `<=`, `>`, `>=`,
22
+ `IS NULL`, three-valued (Cypher) null semantics, and an untyped `--` that infers the shortest
23
+ declared path between two node types.
24
+ - **Bound parameters.** Every literal and every `$param` travels as a bound parameter, never SQL
25
+ text; a value the declared property type cannot represent — a huge integer, a NUL in a string, a
26
+ wrong-typed or non-`int` page value, a scalar where a list is expected — raises `ParameterError`
27
+ naming the parameter and the declared type, never the value.
28
+ - **`OPTIONAL MATCH` and `WITH`.** Missing variables behave as in Cypher (`o IS NULL` keeps the
29
+ rows the optional side missed); `WITH` starts a new scope, optionally renames, accepts
30
+ `DISTINCT`, and takes a `WHERE` that filters the rows produced so far. Only whole variables are
31
+ accepted here; aggregation, projections, `WITH *` and paging inside `WITH` raise
32
+ `UnsupportedCypherError` with a hint.
33
+ - **Variable-length paths.** Bounded `[:TYPE*a..b]` — typed or untyped, `*..b`, `*n`, `*0..0` as
34
+ the identity — compile to joins while their hops fit the join budget; open-ended `*` / `*a..`
35
+ and over-budget bounds compile to `WITH RECURSIVE` walks that carry the rows they read and never
36
+ reuse one on the same path — the trail compares row identities by code point, so a
37
+ case-insensitive or ICU-nondeterministic collation cannot hide a legitimate row. An untyped
38
+ bound walks every declared edge the arrow allows.
39
+ - **`EXISTS { … }` and pattern predicates.** `WHERE (c)-[:PLACED]->()` and
40
+ `WHERE EXISTS { MATCH … WHERE … }` ask a question without fetching rows: one correlated
41
+ `EXISTS`, never a `DISTINCT` on the outer query, with `NOT` and NULL-over-optional semantics
42
+ matching Cypher.
43
+ - **Lists and text.** `IN` with a query list or a `$param` array (one array parameter,
44
+ `= ANY(%s::type[])`; an empty list matches nothing, never everything), `STARTS WITH`,
45
+ `ENDS WITH`, `CONTAINS` as PostgreSQL functions rather than `LIKE` (so `%`, `_` and `\` stay
46
+ ordinary characters), and `=~` against PostgreSQL's POSIX regular expressions, anchored and
47
+ bound as a parameter. Every string comparison and text ordering is code-point order
48
+ (`COLLATE "C"`), so a case-insensitive or ICU-nondeterministic column collation cannot change
49
+ the returned keys; a non-`C` column keeps its index through an expression index instead.
50
+ - **Query-level ordering and paging.** `ORDER BY <variable>` (composite keys in key order,
51
+ `DESC` flipping every key column, text keys compared under `COLLATE "C"`), `SKIP` and `LIMIT`
52
+ as bound `bigint` parameters, and `DISTINCT` key sets.
53
+ - **t-string queries.** On Python ≥ 3.14, `to_sql(t"MATCH (c:Customer {country: {country}}) …")`
54
+ binds each interpolation as a value with no params dict, and `SQLQuery.to_template()` splices a
55
+ compiled query into another t-string or a psycopg query.
56
+ - **Limits.** `Limits` bounds query length, parser nesting, pattern size, edge branches,
57
+ inferred-path length and count, path length and the depth of an open-ended walk; exceeding one
58
+ raises `LimitExceededError` naming the limit and the fix instead of emitting partial SQL.
59
+ - **Error contract.** Every failure derives from `Cypher2SQLError`; syntax errors point at the
60
+ token with a caret and a line/column, unknown names suggest a declared name ("did you mean
61
+ :Customer?"), names are never truncated into an identifier, and messages never repeat values.
62
+ - **Typing.** Ships `py.typed` and is `mypy --strict` clean.
63
+ - **openCypher TCK adapter.** The official TCK is vendored and every scenario is accounted for in
64
+ `tests/tck/scope.toml`: 56 `in` (must pass), 31 `planned` (run and must not pass until
65
+ promoted) and 3,810 out of scope by category, of 3,897 scenario instances. `make tck` prints the
66
+ live split, and the in-scope count is a ratchet.
67
+
68
+ ### Packaging and CI
69
+
70
+ - Wheel and source distribution built by `uv build` (`cypher2sql-0.1.0-py3-none-any.whl`,
71
+ `cypher2sql-0.1.0.tar.gz`) with no `Requires-Dist`; CI runs `make check`, `make tck`,
72
+ `make check-py314` (Python 3.14 mandatory, no block skipped) and a clean-venv wheel smoke test
73
+ of the tutorial's first query.
74
+ - A `workflow_dispatch`-only PyPI publish workflow (`.github/workflows/publish.yml`) builds the
75
+ sdist and wheel and uploads them with trusted publishing (OIDC, environment `release`). A human
76
+ runs it after the release PR merges; agents never publish. `docs/release.md` carries the exact
77
+ fallback and the post-publish install gate.
78
+
79
+ ### Documentation site and benchmark
80
+
81
+ - **Documentation site.** Markdown docs are built with Zensical and published to GitHub Pages by
82
+ a SHA-pinned workflow on a merge to `develop`; a hand-run is allowed from `develop` only.
83
+ `make docs` builds with `--strict`, and `make docs-check` builds `--strict` again and proves
84
+ every source heading and fenced block reaches the built HTML, every internal link and anchor
85
+ resolves, every page is in the nav, and two builds are byte-identical. `make check` executes the
86
+ same pages as 50 code blocks (47 Python, 3 SQL); the four Python-3.14-only blocks run under
87
+ `make check-py314`.
88
+ - **Benchmark.** `make bench` times six shop query shapes against a real `neo4j:5-community`
89
+ pinned by image digest, reporting compile-only, PostgreSQL end-to-end, Neo4j end-to-end, plus
90
+ the one-time ingest and index build on both engines; `make bench-check` loads the small dataset
91
+ into both and asserts identical keys for every shape. It needs Docker and a network pull, so it
92
+ never runs in `make check`. At the recorded sizes PostgreSQL answers all six shapes faster,
93
+ every Neo4j number includes its HTTP round trip, and cypher2sql adds no ingest cost to a
94
+ database you already own.
95
+
96
+ ### Known limits (0.1.0)
97
+
98
+ - One target node type per query: `RETURN` takes one node variable, optionally ordered, paged and
99
+ distinct; properties, relationships, paths and several columns are refused.
100
+ - Every construct this release refuses — all of them valid Cypher — is listed once, with the
101
+ reason and an alternative for each, in the API reference's
102
+ [Not supported in 0.1.0](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/api.md#not-supported-in-010)
103
+ table. The changelog does not keep a second copy of that list.
104
+ - PostgreSQL only, and no runtime schema introspection: the schema is declared once in Python.
105
+ - Limits do not bound execution; set `statement_timeout` (and a row cap) where untrusted queries
106
+ run.
107
+
108
+ [0.1.0]: https://github.com/OffworldNexus/cypher2sql/releases/tag/v0.1.0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,95 @@
1
+ Metadata-Version: 2.5
2
+ Name: cypher2sql
3
+ Version: 0.1.0
4
+ Summary: Write Cypher graph patterns against your existing PostgreSQL schema; get back plain SQL that selects the matching primary keys.
5
+ Project-URL: Repository, https://github.com/OffworldNexus/cypher2sql
6
+ Project-URL: Issues, https://github.com/OffworldNexus/cypher2sql/issues
7
+ Author-email: Rémy Sanchez <remy.sanchez@hyperthese.net>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Topic :: Database
18
+ Classifier: Typing :: Typed
19
+ Requires-Python: >=3.12
20
+ Description-Content-Type: text/markdown
21
+
22
+ # cypher2sql
23
+
24
+ [![CI](https://github.com/OffworldNexus/cypher2sql/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/OffworldNexus/cypher2sql/actions/workflows/ci.yml)
25
+ [![PyPI](https://img.shields.io/pypi/v/cypher2sql.svg)](https://pypi.org/project/cypher2sql/)
26
+ [![Python](https://img.shields.io/badge/python-3.12%20%7C%203.13%20%7C%203.14-blue.svg)](https://github.com/OffworldNexus/cypher2sql/blob/develop/pyproject.toml)
27
+ [![Docs](https://img.shields.io/badge/docs-offworldnexus.github.io%2Fcypher2sql-blue.svg)](https://offworldnexus.github.io/cypher2sql/)
28
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/OffworldNexus/cypher2sql/blob/develop/LICENSE)
29
+
30
+ **Answer graph questions on the PostgreSQL schema you already have — up to 17.1× faster than Neo4j (median 6.1×), no migration.**
31
+
32
+ Install with `pip install cypher2sql`, describe your tables and foreign keys once, write Cypher, get one parameterized SQL query that returns the primary keys you asked for.
33
+
34
+ [Documentation](https://offworldnexus.github.io/cypher2sql/) · [Tutorial](https://offworldnexus.github.io/cypher2sql/tutorial/) · [API reference](https://offworldnexus.github.io/cypher2sql/api/) · [Performance](https://offworldnexus.github.io/cypher2sql/performance/) · [openCypher parity](https://offworldnexus.github.io/cypher2sql/api/#feature-parity-with-neo4j)
35
+
36
+ ```python
37
+ from cypher2sql import Edge, Node, Schema
38
+
39
+ customer = Node("Customer", table="customer", key="id", properties={"country": str})
40
+ order = Node("Order", table="order", key="id", properties={"id": int})
41
+ shop = Schema(
42
+ nodes=[customer, order],
43
+ edges=[Edge("PLACED", customer, order, source_columns="customer_id")],
44
+ )
45
+
46
+ q = shop.to_sql(
47
+ "MATCH (c:Customer {country: $country})-[:PLACED]->(:Order) RETURN c",
48
+ {"country": "FR"},
49
+ )
50
+ print(q.sql)
51
+ print(q.params)
52
+ ```
53
+ ```text title="output"
54
+ SELECT n1."id"
55
+ FROM "customer" AS n1
56
+ WHERE n1."country" COLLATE "C" = %s::text
57
+ AND EXISTS (
58
+ SELECT 1
59
+ FROM "order" AS n2
60
+ WHERE n2."customer_id" = n1."id"
61
+ )
62
+ ('FR',)
63
+ ```
64
+
65
+ Run it with any driver: `conn.execute(q.sql, q.params)` (psycopg), or feed it to your ORM. A single-column
66
+ key fits `WHERE id IN (...)`, a `psycopg.sql` wrapper still needs your own literal `%` as `%%`, and composite keys
67
+ join a CTE. Compiling never touches the database; on Python ≥ 3.14 a
68
+ [t-string](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/api.md#t-string-queries) carries its
69
+ values as `{expr}` and takes no `params` dict.
70
+
71
+ - Your database stays relational: no graph extension, no data migration, no runtime dependencies.
72
+ - Property values are always bound parameters; a label/type selector only chooses among identifiers from your
73
+ declared schema.
74
+
75
+ Start with the
76
+ [tutorial](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/tutorial.md); every name
77
+ is in the
78
+ [API reference](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/api.md).
79
+
80
+ ## Correctness
81
+
82
+ Correctness is checked on a real PostgreSQL against the official
83
+ [openCypher TCK](https://github.com/opencypher/openCypher/tree/main/tck), vendored in
84
+ `vendor/opencypher-tck/`: 220 features, 3,897 scenario instances (1,615 declarations; outlines expand to 3,897).
85
+ `tests/tck/scope.toml` classifies every instance and
86
+ [the design](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/design.md#tck) records why.
87
+ `make tck` prints the live split: its first line is
88
+ `in-scope passing: N / N, out-of-scope: M (by category)`, then `planned: P (must not pass)` and one
89
+ count line per category, including `in`; N, M and P are the live counts.
90
+ [The vision](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/VISION.md#correctness-bar-the-opencypher-tck)
91
+ states that split.
92
+
93
+ ## License
94
+
95
+ Apache-2.0. The vendored openCypher TCK and grammar are Apache-2.0 (see `vendor/*/NOTICE`).
@@ -0,0 +1,74 @@
1
+ # cypher2sql
2
+
3
+ [![CI](https://github.com/OffworldNexus/cypher2sql/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/OffworldNexus/cypher2sql/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/cypher2sql.svg)](https://pypi.org/project/cypher2sql/)
5
+ [![Python](https://img.shields.io/badge/python-3.12%20%7C%203.13%20%7C%203.14-blue.svg)](https://github.com/OffworldNexus/cypher2sql/blob/develop/pyproject.toml)
6
+ [![Docs](https://img.shields.io/badge/docs-offworldnexus.github.io%2Fcypher2sql-blue.svg)](https://offworldnexus.github.io/cypher2sql/)
7
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/OffworldNexus/cypher2sql/blob/develop/LICENSE)
8
+
9
+ **Answer graph questions on the PostgreSQL schema you already have — up to 17.1× faster than Neo4j (median 6.1×), no migration.**
10
+
11
+ Install with `pip install cypher2sql`, describe your tables and foreign keys once, write Cypher, get one parameterized SQL query that returns the primary keys you asked for.
12
+
13
+ [Documentation](https://offworldnexus.github.io/cypher2sql/) · [Tutorial](https://offworldnexus.github.io/cypher2sql/tutorial/) · [API reference](https://offworldnexus.github.io/cypher2sql/api/) · [Performance](https://offworldnexus.github.io/cypher2sql/performance/) · [openCypher parity](https://offworldnexus.github.io/cypher2sql/api/#feature-parity-with-neo4j)
14
+
15
+ ```python
16
+ from cypher2sql import Edge, Node, Schema
17
+
18
+ customer = Node("Customer", table="customer", key="id", properties={"country": str})
19
+ order = Node("Order", table="order", key="id", properties={"id": int})
20
+ shop = Schema(
21
+ nodes=[customer, order],
22
+ edges=[Edge("PLACED", customer, order, source_columns="customer_id")],
23
+ )
24
+
25
+ q = shop.to_sql(
26
+ "MATCH (c:Customer {country: $country})-[:PLACED]->(:Order) RETURN c",
27
+ {"country": "FR"},
28
+ )
29
+ print(q.sql)
30
+ print(q.params)
31
+ ```
32
+ ```text title="output"
33
+ SELECT n1."id"
34
+ FROM "customer" AS n1
35
+ WHERE n1."country" COLLATE "C" = %s::text
36
+ AND EXISTS (
37
+ SELECT 1
38
+ FROM "order" AS n2
39
+ WHERE n2."customer_id" = n1."id"
40
+ )
41
+ ('FR',)
42
+ ```
43
+
44
+ Run it with any driver: `conn.execute(q.sql, q.params)` (psycopg), or feed it to your ORM. A single-column
45
+ key fits `WHERE id IN (...)`, a `psycopg.sql` wrapper still needs your own literal `%` as `%%`, and composite keys
46
+ join a CTE. Compiling never touches the database; on Python ≥ 3.14 a
47
+ [t-string](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/api.md#t-string-queries) carries its
48
+ values as `{expr}` and takes no `params` dict.
49
+
50
+ - Your database stays relational: no graph extension, no data migration, no runtime dependencies.
51
+ - Property values are always bound parameters; a label/type selector only chooses among identifiers from your
52
+ declared schema.
53
+
54
+ Start with the
55
+ [tutorial](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/tutorial.md); every name
56
+ is in the
57
+ [API reference](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/api.md).
58
+
59
+ ## Correctness
60
+
61
+ Correctness is checked on a real PostgreSQL against the official
62
+ [openCypher TCK](https://github.com/opencypher/openCypher/tree/main/tck), vendored in
63
+ `vendor/opencypher-tck/`: 220 features, 3,897 scenario instances (1,615 declarations; outlines expand to 3,897).
64
+ `tests/tck/scope.toml` classifies every instance and
65
+ [the design](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/design.md#tck) records why.
66
+ `make tck` prints the live split: its first line is
67
+ `in-scope passing: N / N, out-of-scope: M (by category)`, then `planned: P (must not pass)` and one
68
+ count line per category, including `in`; N, M and P are the live counts.
69
+ [The vision](https://github.com/OffworldNexus/cypher2sql/blob/develop/docs/VISION.md#correctness-bar-the-opencypher-tck)
70
+ states that split.
71
+
72
+ ## License
73
+
74
+ Apache-2.0. The vendored openCypher TCK and grammar are Apache-2.0 (see `vendor/*/NOTICE`).
@@ -0,0 +1,82 @@
1
+ # cypher2sql — Vision
2
+
3
+ ## The promise
4
+
5
+ A developer with an ordinary PostgreSQL schema (tables, primary keys, foreign keys, join tables)
6
+ describes it **once, statically, in Python** as node types and edge types. They then write a
7
+ Cypher graph pattern and name the node they want. cypher2sql returns **one parameterized SQL
8
+ query** that selects the primary keys of every matching row of that node's table.
9
+
10
+ ```text
11
+ schema (static Python classes) + Cypher pattern + target node ──► SQL text + params
12
+ SELECT <pk cols> FROM ...
13
+ ```
14
+
15
+ That output composes with everything: an ORM `filter(id__in=RawSQL(...))`, a CTE, a view,
16
+ a permission system ("which documents can this user reach?"), a recommendation query.
17
+
18
+ ## Non-negotiables
19
+
20
+ 1. **DX is the product.** The tutorial (`docs/tutorial.md`) and the public API reference are
21
+ written *first*, reviewed as if by a newcomer, and are **executable**: every code block in
22
+ the tutorial runs in the test suite against a real PostgreSQL. If the tutorial is awkward,
23
+ the API is wrong — fix the API, not the prose.
24
+ 2. **Schema is static and explicit.** The input is "a couple of classes implementing a protocol
25
+ that lets the library introspect the schema as nodes and edges" (node types ↔ tables with a
26
+ primary key and property→column mapping; edge types ↔ foreign keys or join tables). No live
27
+ database introspection for now. The protocol must be small enough that hand-writing it is
28
+ pleasant, and a reference implementation (e.g. dataclasses) ships with the library.
29
+ 3. **Output = primary keys of one node type.** Not rows, not projections. Composite primary keys
30
+ are supported. The SQL is deterministic, readable, and stable across runs (snapshot-testable).
31
+ 4. **PostgreSQL only.** One dialect, done well. Tests run on a real PostgreSQL (`tests/conftest.py`
32
+ boots a throwaway socket-only cluster). No SQLite stand-ins, no mocked cursors.
33
+ 5. **Zero runtime dependencies.** The library emits SQL text + a parameter mapping. It never opens
34
+ connections. Drivers (psycopg) are dev-only.
35
+ 6. **Secure by construction.**
36
+ - Every value from the Cypher query (literals *and* `$params`) becomes a bound parameter;
37
+ no value is ever interpolated into SQL text.
38
+ - Every identifier in the SQL comes from the declared schema and is quoted; nothing from the
39
+ query text is ever used as an identifier. Unknown labels/types/properties are compile errors.
40
+ - Untrusted query text cannot exhaust resources: bounded parser recursion, bounded pattern
41
+ size, bounded variable-length expansion (explicit, documented limits).
42
+ 7. **Errors teach.** A compile error says what is wrong, points at the exact span of the Cypher
43
+ text, and suggests the fix (e.g. "unknown label `:Custmer` — did you mean `:Customer`?").
44
+
45
+ ## Correctness bar: the openCypher TCK
46
+
47
+ `vendor/opencypher-tck/` holds the official openCypher Technology Compatibility Kit and is our
48
+ regression suite: 220 features, 3,897 scenario instances (1,615 declarations; outlines expand to 3,897).
49
+ Because the TCK assumes a schemaless graph and full result projections, while we compile against a
50
+ static relational schema and return primary keys, a **TCK adapter** is required (design owned by the
51
+ architect, issue-tracked):
52
+
53
+ - For each scenario, the adapter derives a static schema + PostgreSQL fixture data from the
54
+ scenario's setup (`having executed: CREATE ...`), compiles the scenario query, runs it, and
55
+ compares the returned node identities with the expected result.
56
+ - `tests/tck/scope.toml` accounts for **every** scenario: `in` (must pass), `planned` (runs and
57
+ must not pass until promoted), or `out-of-scope` with a named reason category (e.g. `write-query`,
58
+ `projection`, `aggregation`, `schemaless-only`). Silent skips and `xfail` on in-scope scenarios
59
+ are forbidden. The live split is 56 `in`, 31 `planned` and 3,810 out of scope, and `make tck`
60
+ prints it.
61
+ - The in-scope pass count is a ratchet: it may only go up. It has moved down once, as a reviewed
62
+ correction (172 → 12, see
63
+ [compile-error expectations](design.md#compile-error-expectations)), never silently; moving a
64
+ scenario out of scope needs a reason a reviewer accepts.
65
+
66
+ ## Releases
67
+
68
+ There is one release target at a time, and it must be genuinely usable: no demo or "foundation"
69
+ releases. **0.1.0** is the working library: schema protocol, `MATCH`/`WHERE`/`RETURN`,
70
+ query-level `ORDER BY`/`SKIP`/`LIMIT`, `OPTIONAL MATCH`, `WITH`, bounded and unbounded
71
+ variable-length paths, pattern predicates / `EXISTS`, lists/`IN`, string predicates and POSIX
72
+ `=~`, TCK adapter with every scenario accounted for, hardening and limits, and PyPI packaging.
73
+ Anything after that is driven by real usage and human-filed issues.
74
+
75
+ ## Out of scope (for now)
76
+
77
+ Writes (CREATE/MERGE/SET/DELETE as user queries), returning anything other than one node's
78
+ primary keys, other SQL dialects, runtime schema introspection, a query-execution layer.
79
+ Inside 0.1.0 the compiler also refuses valid Cypher it does not compile, always with
80
+ `UnsupportedCypherError` and a teaching hint. The canonical list — every construct, its reason and
81
+ its alternative — is the [API reference's Not supported in 0.1.0 table](api.md#not-supported-in-010),
82
+ whose every row also carries the hint the refusal attaches; the vision does not keep a second copy.