cypher2sql — Vision
The promise
A developer with an ordinary PostgreSQL schema (tables, primary keys, foreign keys, join tables) describes it once, statically, in Python as node types and edge types. They then write a Cypher graph pattern and name the node they want. cypher2sql returns one parameterized SQL query that selects the primary keys of every matching row of that node's table.
schema (static Python classes) + Cypher pattern + target node ──► SQL text + params
SELECT <pk cols> FROM ...
That output composes with everything: an ORM filter(id__in=RawSQL(...)), a CTE, a view,
a permission system ("which documents can this user reach?"), a recommendation query.
Non-negotiables
- DX is the product. The tutorial (
docs/tutorial.md) and the public API reference are written first, reviewed as if by a newcomer, and are executable: every code block in the tutorial runs in the test suite against a real PostgreSQL. If the tutorial is awkward, the API is wrong — fix the API, not the prose. - Schema is static and explicit. The input is "a couple of classes implementing a protocol that lets the library introspect the schema as nodes and edges" (node types ↔ tables with a primary key and property→column mapping; edge types ↔ foreign keys or join tables). No live database introspection for now. The protocol must be small enough that hand-writing it is pleasant, and a reference implementation (e.g. dataclasses) ships with the library.
- Output = primary keys of one node type. Not rows, not projections. Composite primary keys are supported. The SQL is deterministic, readable, and stable across runs (snapshot-testable).
- PostgreSQL only. One dialect, done well. Tests run on a real PostgreSQL (
tests/conftest.pyboots a throwaway socket-only cluster). No SQLite stand-ins, no mocked cursors. - Zero runtime dependencies. The library emits SQL text + a parameter mapping. It never opens connections. Drivers (psycopg) are dev-only.
- Secure by construction.
- Every value from the Cypher query (literals and
$params) becomes a bound parameter; no value is ever interpolated into SQL text. - Every identifier in the SQL comes from the declared schema and is quoted; nothing from the query text is ever used as an identifier. Unknown labels/types/properties are compile errors.
- Untrusted query text cannot exhaust resources: bounded parser recursion, bounded pattern size, bounded variable-length expansion (explicit, documented limits).
- Errors teach. A compile error says what is wrong, points at the exact span of the Cypher
text, and suggests the fix (e.g. "unknown label
:Custmer— did you mean:Customer?").
Correctness bar: the openCypher TCK
vendor/opencypher-tck/ holds the official openCypher Technology Compatibility Kit and is our
regression suite: 220 features, 3,897 scenario instances (1,615 declarations; outlines expand to 3,897).
Because the TCK assumes a schemaless graph and full result projections, while we compile against a
static relational schema and return primary keys, a TCK adapter is required (design owned by the
architect, issue-tracked):
- For each scenario, the adapter derives a static schema + PostgreSQL fixture data from the
scenario's setup (
having executed: CREATE ...), compiles the scenario query, runs it, and compares the returned node identities with the expected result. tests/tck/scope.tomlaccounts for every scenario:in(must pass),planned(runs and must not pass until promoted), orout-of-scopewith a named reason category (e.g.write-query,projection,aggregation,schemaless-only). Silent skips andxfailon in-scope scenarios are forbidden. The live split is 56in, 31plannedand 3,810 out of scope, andmake tckprints it.- The in-scope pass count is a ratchet: it may only go up. It has moved down once, as a reviewed correction (172 → 12, see compile-error expectations), never silently; moving a scenario out of scope needs a reason a reviewer accepts.
Releases
There is one release target at a time, and it must be genuinely usable: no demo or "foundation"
releases. 0.1.0 is the working library: schema protocol, MATCH/WHERE/RETURN,
query-level ORDER BY/SKIP/LIMIT, OPTIONAL MATCH, WITH, bounded and unbounded
variable-length paths, pattern predicates / EXISTS, lists/IN, string predicates and POSIX
=~, TCK adapter with every scenario accounted for, hardening and limits, and PyPI packaging.
Anything after that is driven by real usage and human-filed issues.
Out of scope (for now)
Writes (CREATE/MERGE/SET/DELETE as user queries), returning anything other than one node's
primary keys, other SQL dialects, runtime schema introspection, a query-execution layer.
Inside 0.1.0 the compiler also refuses valid Cypher it does not compile, always with
UnsupportedCypherError and a teaching hint. The canonical list — every construct, its reason and
its alternative — is the API reference's Not supported in 0.1.0 table,
whose every row also carries the hint the refusal attaches; the vision does not keep a second copy.