Cypher language frontend
The Cypher language frontend turns one Cypher statement into a validated Helix query request. It separates text parsing, name and graph binding, request decoding, and execution so the planner receives a clear relational query while every client sees the same API and error rules. It is a child of Query planning and execution; Query language AST, Planner and optimizer, and Interpreter execution describe the shared query model and the later stages.
Frontend role
Compilation path
The compilation path changes source text into planner input without reading the graph. The lexer recognizes tokens and source spans, the parser builds source-shaped syntax, and the binder resolves names and validates the supported language profile. The result is the relational query consumed by planning. A request can retain this compiled result while routing determines whether execution needs a reader or writer.
This separation lets a gateway classify a statement as a read or write before it selects a database node. Execution later adds the current graph scope, catalog, parameters, limits, and transaction state.
The main path is:
- Decode one request and its parameter values.
- Parse one Cypher statement into clauses, patterns, and expressions.
- Bind variables, graph entities, aliases, functions, and aggregates.
- Produce a shared relational query and its read or write effect.
- Pass the validated query to planning and execution.
The contract tests extend this boundary into planning. They check that bound scopes, optional nullability, correlations, effects, and resource limits remain valid as the planner consumes the query.
The following diagram shows the boundary between language work and graph execution.
flowchart LR
A[Cypher text and parameters] --> B[Tokens and source spans]
B --> C[Source syntax]
C --> D[Bound relational query]
D --> E[Planner and optimizer]
E --> F[Interpreter and graph view]
F --> G[Rows or write result]
References: crates/…/lib.rs• L1-13 crates/…/lib.rs• L17-44 crates/…/request.rs• L44-80 crates/…/contracts.rs• L831-873
Cypher syntax profile
Clauses and expressions
The profile supports graph matching and creation, row shaping, expressions, and graph updates. A statement is a sequence of supported clauses and must end in a result projection or a mutation. MATCH and OPTIONAL MATCH create graph rows. WITH, RETURN, and UNWIND shape those rows. CREATE, SET, REMOVE, DELETE, and DETACH DELETE change graph state.
Expressions provide literals, parameters, property and list access, arithmetic, comparisons, Boolean logic, null checks, string predicates, CASE, lists, maps, scalar functions, and aggregates. The profile also supports fixed-length directed or undirected match patterns, named paths, aliases, ordering, distinct results, skips, and limits.
The frontend is a supported Cypher profile, not a promise of full openCypher compatibility. Unsupported areas include variable-length and shortest paths, MERGE, subqueries, UNION, comprehensions, procedures, schema DDL, and temporal or spatial functions. An unsupported feature has a distinct capability error; invalid syntax or invalid use of a supported form has a syntax or semantic error instead.
References: crates/…/parser.rs• L137-239 crates/…/parser.rs• L250-327 crates/…/parser.rs• L383-489 crates/…/bind.rs• L1036-1151
Lexical boundaries
Lexing defines the text rules that the parser can rely on. It accepts comments, quoted strings, escaped identifiers, quoted parameter names, decimal and alternate-base numbers, and pattern punctuation. Tokens retain source spans so compile errors can point to the relevant text.
The frontend limits query text to 16 MiB and token count to 200,000. It also limits expression nesting to 48 levels. These limits protect compilation before planning or graph access begins.
References: crates/…/lexer.rs• L15-35 crates/…/lexer.rs• L44-170 crates/…/lexer.rs• L198-259 crates/…/lib.rs• L28-36
Binding model
Scope and graph bindings
Binding gives each visible Cypher name a planner slot and a value kind. Node variables, relationship variables, named paths, scalar aliases, and anonymous pattern elements become row bindings that later clauses can read or replace. A WITH or RETURN projection creates the next visible scope, so names not projected out are no longer available.
Pattern binding checks the relationships between graph entities. It enforces variable reuse rules, relationship uniqueness within a match, label and relationship-type limits, directed creation, property-key rules, and the requirement for labels and one type when creating graph data. The binder also infers whether projected expressions remain nodes, relationships, paths, or scalar values so later patterns and mutations can use them correctly.
This is the point where Cypher graph meaning becomes the common query contract. The frontend does not look up nodes or labels; the planner and interpreter use the bound slots against the graph view chosen for execution.
References: crates/…/bind.rs• L34-79 crates/…/bind.rs• L123-242 crates/…/bind.rs• L660-710 crates/…/bind.rs• L747-881
Aggregate bindings
Aggregate binding separates grouping values from aggregate state when a projection mixes both. It preserves public column order while allowing later ordering expressions to reuse the exact projected aggregate or a matching aggregate inside a larger expression. Grouping dependencies are limited to recognized variables and direct property or map access, so complex ungrouped expressions fail at compile time instead of producing an ambiguous result.
The aggregate stage also keeps incoming names separate from output aliases. This matters when an alias shadows an input name, because aggregate arguments must still read the incoming scope.
References: crates/…/bind.rs• L246-455 crates/…/aggregation.rs• L9-16 crates/…/aggregation.rs• L42-151 crates/…/grouping.rs• L5-48 crates/…/ordering.rs• L12-49
Parameters and values
Parameters are runtime values, not query syntax. Variable names, labels, relationship types, and property names remain part of the statement and cannot be supplied as parameter values. This keeps the compiled query shape stable while allowing data values to change between executions.
The request accepts ordinary JSON values and lossless envelopes for signed 64-bit integers, non-finite floats, and maps whose keys include $type. The frontend carries these values into the compiled request without validating them against a graph catalog. Execution validates parameter use and applies the current request limits at the database boundary.
Pattern maps have a separate rule: property maps in MATCH use literal map syntax, while parameter maps in patterns are rejected or marked unsupported according to the owning clause. Use a parameter as a property value when the query needs data supplied by the caller.
References: crates/…/request.rs• L10-33 crates/…/request.rs• L75-112 crates/…/request.rs• L116-163 crates/…/parser.rs• L125-135 crates/…/parser.rs• L447-461
Name and capability errors
Binding reports errors before planning when a query has an undefined variable, duplicate output name, invalid clause order, invalid aggregate use, or an incompatible mutation target. It preserves standard syntax and semantic errors ahead of deferred capability errors, so an invalid query does not hide its more useful basic error behind an unsupported feature.
The error carries a category, detail, phase, message, and optional source span. This gives local callers and transports one stable distinction between an invalid request, an unsupported profile feature, a resource limit, an access-mode failure, and an internal planner failure.
References: crates/…/bind.rs• L69-79 crates/…/bind.rs• L397-480 crates/…/bind.rs• L594-657 crates/…/api.rs• L15-57
API contracts
Request contract
The request contract contains one required query string, optional parameters, and an optional query_name. Unknown fields are rejected. A request can be compiled once into a CompiledRequest; routing can then inspect its read or write effect without parsing again, while execution consumes the query and parameter map with the caller's current scope and limits.
The compiled request keeps the original text and name for telemetry until execution takes ownership of the query parts. It owns no database, catalog snapshot, or transaction, so it can safely cross a transport routing boundary.
References: crates/…/request.rs• L25-73 crates/…/request.rs• L75-113 crates/…/request.rs• L145-188
HTTP and gRPC surfaces
Cypher execution is exposed at POST /v2/cypher and planning-only explain is exposed at POST /v2/cypher/explain. gRPC provides matching ExecuteCypher and ExplainCypher methods with the same JSON request and response meaning. The explain path plans as a read and does not execute the statement.
Successful responses contain columns and rows. Query errors use an envelope with error, msg, and details containing the detail, phase, and source span. HTTP maps invalid queries, resource limits, writer requirements, and internal failures to 400, 429, 503, and 500. Other transport or storage failures retain their native transport error contract.
References: crates/…/api.rs• L1-13 crates/…/api.rs• L60-98 crates/…/api.rs• L5-64 docs/cypher.md• L9-53
OpenCypher TCK conformance
Corpus and scenario execution
The OpenCypher TCK tool measures conformance against a pinned upstream Gherkin corpus. It verifies the corpus manifest and file checksums, expands scenario examples, and runs each scenario against a fresh in-memory database. The runner compares columns, values, row order where required, graph side effects, rollback state, and expected error phase and detail.
The report records whether each scenario parsed, resolved, and planned. It distinguishes passed, failed, unsupported, setup-blocked, timed-out, harness-error, and not-executed outcomes. Workers isolate scenarios and apply per-scenario timeouts so a bad query or runtime path cannot silently stop the full conformance run. Named graph fixtures are loaded through Cypher before scenarios that need them.
References: tools/…/corpus.rs• L10-74 tools/…/corpus.rs• L77-149 tools/…/report.rs• L4-32 tools/…/report.rs• L35-81 tools/…/runner.rs• L55-128 tools/…/runner.rs• L130-240
Conformance gate
The gate turns the TCK report into a reviewed capability check. It requires every scenario to appear exactly once, rejects harness errors and unexecuted scenarios, and requires the capability manifest to classify every scenario as required or excluded. Excluded scenarios must stop at the declared unsupported capability rather than pass, fail later, or reach an unrelated error.
The gate also compares the committed manifest with the current parser and binder profile draft. This means a newly supported or newly rejected form cannot silently change the required set. Required scenarios and the previously passing regression set must pass before the gate succeeds.
Use the tool with cargo run --locked -p helix-opencypher-tck -- --gate --output target/cypher-tck.json. Use --profile-draft to review the current parser and binder classification before changing the manifest. The coverage script runs the same gate and checks explicit frontend and TCK coverage thresholds.
References: tools/…/main.rs• L20-101 tools/…/gate.rs• L24-140 tools/…/gate.rs• L143-212 tools/…/gate.rs• L1-220 scripts/cypher-coverage.sh• L10-16 scripts/cypher-coverage.sh• L35-59
Change checklist
When these files or sections are changed, remember to consider these:
- Syntax or supported-feature changes: update parser, binder, profile documentation, and the TCK capability classification together.
- Binding or scope changes: trace projected column names, value kinds, aggregate rules, and the planner relational-query contract.
- Parameter envelope changes: trace JSON decoding, SDK request builders, lossless result values, and gateway routing before execution.
- Error category or detail changes: preserve source spans, HTTP and gRPC status mapping, and TCK expected-error matching.
- Request compilation changes: keep pre-routing effect detection free of database access and keep compiled requests reusable by transports.
- Plan contract changes: trace optional boundaries, correlation, mutation effects, row multiplicity, and blocking stages into planner and execution consumers.
- Plan matrix changes: keep the fixed case denominator, query-family coverage, reviewed captures, and replay inputs aligned.
- TCK corpus or manifest changes: verify checksums, scenario counts, required regressions, exclusions, and coverage denominators together.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A write statement is routed as a read because its effect is not compiled first | The request reaches the wrong database authority or is rejected late | Compile once and inspect CompiledRequest::request_type() before routing |
A projected name is used after a WITH has removed it | Compilation returns UndefinedVariable | Bind each projection into a new visible scope |
| A parameter is used as a label, relationship type, or pattern map | The query is rejected as invalid or unsupported rather than changing query shape | Keep graph syntax in the statement and use parameters for values |
| A mixed aggregate depends on an unprojected or complex grouping expression | Compilation returns AmbiguousAggregationExpression | Keep grouping dependencies to projected variables or direct property or map access |
| A bound operator loses an optional, correlated, mutation, or blocking boundary | The planner or executor can choose an invalid scope or row shape | Preserve the operator contract fields through the planner handoff |
| A plan matrix case changes without its reviewed capture or denominator | Coverage or plan behavior can drift without a clear review point | Keep the fixed matrix and reviewed manifest in sync |
| A query uses an openCypher feature outside the Helix profile | The TCK scenario is marked unsupported or the API returns UnsupportedFeature | Keep the capability manifest aligned with parser and binder classification |
| A conformance worker times out during a write | The scenario result is ambiguous if the database closes too early | Retain the database handle through close after dropping the timed-out statement future |
| A TCK exclusion passes or fails at a later capability than declared | The gate rejects the report and prevents silent coverage drift | Require the observed unsupported detail to equal the manifest reason |
Key files
| File | Why to read it |
|---|---|
| crates/…/lib.rs | Public parse, resolve, compile, and telemetry-shape entry points |
| crates/…/lexer.rs | Cypher token and source-span boundaries |
| crates/…/parser.rs | Supported clauses, patterns, expressions, and syntax errors |
| crates/…/syntax.rs | Source-shaped statement, pattern, clause, and expression model |
| crates/…/bind.rs | Scope, graph bindings, semantic validation, functions, and relational lowering |
| crates/…/aggregation.rs | Aggregate grouping, expression identity, and mixed projection normalization |
| crates/…/request.rs | JSON request, parameter, compiled-request, and read/write effect contract |
| crates/…/api.rs | Shared route and query-error transport contract |
| crates/…/frontend.rs | Frontend behavior, profile boundaries, precedence, and error guarantees |
| crates/…/contracts.rs | Planner handoff, scope, effects, limits, correlations, and contract guarantees |
| crates/…/grouping.rs | Aggregate grouping and projection identity guarantees |
| crates/…/input_windows.rs | Parameter demand and row-window guarantees |
| crates/…/queries.rs | Cypher operator families used by the plan review matrix |
| crates/…/matrix.rs | Population, index, storage, skew, and optimizer-budget matrix inputs |
| crates/…/tests.rs | Plan capture, replay, normalization, and review invariants |
| crates/…/reviewed.json | Reviewed plan outcomes and case manifest |
| tools/…/corpus.rs | Pinned corpus loading and scenario expansion |
| tools/…/runner.rs | Fresh-database scenario execution and result or side-effect comparison |
| tools/…/report.rs | Parse, resolve, plan, and scenario outcome reporting |
| tools/…/gate.rs | Capability manifest and regression gate rules |
| scripts/cypher-coverage.sh | Local conformance and frontend coverage gate |