SQL language frontend
The SQL language frontend turns one SQL statement into the same Helix query form used by Cypher. It maps relational names to graph patterns, keeps SQL result and expression rules, and sends the result through shared binding, planning, and execution. It does not read storage while it translates a request. See query planning and execution for the shared downstream path, and query language AST, planner optimizer, interpreter execution, and Cypher language frontend for related contracts.
System boundary
The frontend owns the boundary between SQL input and the shared Helix query system. Its output is a syntax statement or a compiled request that later stages can handle without knowing that the source language was SQL.
Request lifecycle
SQL input moves through parsing, graph translation, query resolution, and request construction. The mapping data supplies graph names; it is not a schema lookup.
The main flow is:
flowchart LR
A[SQL text] --> B[Parse and validate]
M[Name mappings] --> C[Translate tables and expressions]
B --> C
C --> D[Helix syntax statement]
D --> E[Resolve to planner query]
P[Parameters] --> F[Compiled request]
E --> F
F --> G[Shared transport and execution]
translate returns a syntax statement. compile resolves that statement into the relational planner query without storage access. The request path then packages the query, parameters, translated text, and optional query name as the same CompiledRequest used by Cypher.
References: crates/…/sql.rs• L1-9 crates/…/sql.rs• L124-136 crates/…/request.rs• L1-5 crates/…/request.rs• L53-62
API contract
The SQL request API accepts one statement in the query field. parameters accepts either named values or a list of values for positional ? parameters, while mappings controls SQL-to-graph names and query_name carries an optional request name.
The frontend numbers ? by written order and emits $1, $2, and later names in the translated query. Named forms such as :name, $name, and @name keep their names. Positional parameters are decoded into the same lossless query values used by the general request API, so transport binding remains shared.
References: crates/…/request.rs• L13-18 crates/…/request.rs• L31-40 crates/…/request.rs• L66-85 crates/…/expr.rs• L947-987
Relational graph model
The frontend gives relational SQL a graph meaning without inspecting a database schema. A table becomes a node or relationship pattern, and a column becomes a property or a graph id.
Table mapping
The Mappings contract maps a case-insensitive SQL table name to a graph label or relationship type. Without a mapping, the table name itself becomes the graph name, and an alias becomes the lowercased Cypher variable.
A table name shaped like Start_TYPE_End represents a relationship table. Its start and end names become node labels, its middle name becomes the relationship type, and its columns describe relationship or endpoint properties. v$id and v$<table>_id expose graph ids. Endpoint columns can use the endpoint label, _start, or _end as a qualifier.
References: crates/…/sql.rs• L77-110 crates/…/tables.rs• L60-70 crates/…/tables.rs• L117-146 crates/…/tables.rs• L236-315
Join translation
A join chain becomes a directed graph pattern. An equality in ON supplies the relationship type from the relevant column unless Mappings::join_columns supplies an explicit type; the side of that equality determines the direction.
The supported join forms have clear graph meanings:
| SQL form | Graph meaning |
|---|---|
JOIN ... ON a = b | A relationship whose type comes from the join column or mapping |
JOIN ... USING (column) | A relationship named by that column |
NATURAL JOIN | A directed relationship without a named join column |
CROSS JOIN or a comma | A separate pattern |
ON must contain one equality between columns. Filters belong in WHERE. Outer joins and join conditions with other forms are rejected because they do not have this direct graph-pattern meaning.
References: crates/…/sql.rs• L25-39 crates/…/tables.rs• L425-487 crates/…/tables.rs• L566-679 crates/…/tables.rs• L706-790
Query forms
The frontend supports read and write SQL forms that can map to the Helix syntax tree. Unsupported SQL features fail during compilation instead of reaching execution with uncertain meaning.
SELECT translation
SELECT maps to MATCH, an optional WHERE, and a result projection. The select list becomes RETURN; DISTINCT, aliases, wildcards, ORDER BY, and row windows preserve the SQL result shape where the Cypher profile can express it.
Grouping uses a WITH stage when SQL grouping rules differ from Cypher's implicit grouping. This stage carries group keys, projected values, HAVING, and needed ordering expressions before the final result projection. Select-list aliases and one-based positions can be used by GROUP BY and ORDER BY within the supported rules.
The frontend rejects set operations, common table expressions, subqueries, outer joins, and dialect-specific clauses such as QUALIFY or window definitions. These limits keep the output a single graph query with one clear scope.
References: crates/…/select.rs• L74-130 crates/…/select.rs• L209-247 crates/…/select.rs• L248-388 crates/…/select.rs• L390-620
Expression translation
SQL expressions become Helix expressions and resolve their columns against the tables in scope. The supported set covers literals and parameters, arithmetic, boolean and comparison operators, null tests, lists, CASE, IN, BETWEEN, selected LIKE forms, casts, and aggregate or scalar functions.
Function names are adapted to the Cypher profile when SQL and Cypher use different names. The frontend also preserves important SQL behavior where a direct Cypher call would differ, such as string concatenation, null-aware comparisons, SUM over no rows, and one-based SUBSTRING positions. Unsupported subqueries, window functions, qualified function names, and unsupported pattern forms produce compile errors.
References: crates/…/expr.rs• L25-43 crates/…/expr.rs• L68-329 crates/…/expr.rs• L336-505 crates/…/expr.rs• L643-898
Data change statements
SQL data change statements become one Cypher write statement and therefore one transaction. INSERT creates nodes from VALUES; multiple rows use a list-based row flow. An insert into a relationship table finds existing endpoints, then creates the relationship. A missing endpoint creates neither a new node nor the relationship.
The remaining write mappings are direct:
| SQL form | Helix operation |
|---|---|
UPDATE | Match the target rows and set properties |
DELETE | Match and delete the target nodes or relationships |
TRUNCATE | Match all rows of the target and delete them, with optional node detach through CASCADE |
RETURNING | Add a final result projection |
INSERT ... SELECT, upsert forms, multi-table writes, joined deletes, and unsupported dialect options are rejected. Relationship inserts require values that identify both endpoint nodes through endpoint properties or virtual ids.
References: crates/…/dml.rs• L1-14 crates/…/dml.rs• L44-97 crates/…/dml.rs• L100-156 crates/…/dml.rs• L244-407 crates/…/dml.rs• L530-758
Source spans and limits
The frontend treats source location and resource bounds as part of its compile contract. Errors point back to SQL text when possible, and large or deeply nested input stops before it can consume unbounded parser or translation resources.
Source locations
SQL parser locations are converted from one-based line and character positions into byte spans used by Helix query errors. The conversion handles multi-byte text, while expression span calculation avoids recursive stack failure for long operator chains.
The same spans follow translated expressions into invalid-column, unsupported-feature, parameter, and planner errors. When the SQL parser cannot provide a useful node span, the frontend reports the whole trimmed statement.
References: crates/…/sql.rs• L148-165 crates/…/sql.rs• L359-387 crates/…/sql.rs• L390-457 crates/…/spans.rs• L1-45 crates/…/spans.rs• L148-238
Resource limits
The frontend applies limits before and during translation. These limits protect parsing, expression conversion, copied expressions, and comparison of grouped expressions.
The main guarded dimensions are:
- SQL text size and estimated token count.
- Operator count and expression nesting depth.
- Growth from repeated expression copies.
- Printed expression text used to compare grouping expressions.
Exceeding a bound returns a resource-limit compile error. The request does not enter planning or execution in that case.
References: crates/…/sql.rs• L131-146 crates/…/sql.rs• L166-173 crates/…/sql.rs• L214-217 crates/…/sql.rs• L483-494 crates/…/expr.rs• L21-43
Change checklist
When these files or sections are changed, remember to consider these:
- Keep table mappings, relationship-table recognition, endpoint shorthand, and join-type mappings consistent. A name change can alter both pattern shape and column scope.
- Keep placeholder numbering in the translator aligned with positional parameter decoding in the request API.
- Keep SQL expression semantics aligned with the Helix expression kinds and Cypher function names used by binding and planning.
- Preserve select alias, grouping, ordering, and
WITHcontracts together. A change in one stage can change result names or aggregate scope. - Preserve relationship
INSERTendpoint roles andRETURNINGhelper-variable cleanup when changing DML output. - Keep parser locations, byte-span conversion, and error categories aligned so API clients can locate and classify compile failures.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| More than one SQL statement in one request | Translation stops with a multiple-statements error | Accept exactly one parsed statement |
Mixing ? with numeric named placeholders | Parameter names can collide and binding becomes unclear | Reject the mixed form during parameter translation |
| An unqualified column with several tables in scope | The column cannot resolve to a unique graph property | Require a table or alias qualifier |
An outer join or a non-equality ON condition | The request has no supported graph-pattern meaning | Reject the join during FROM translation |
| A relationship insert cannot find an endpoint | No endpoint node or relationship is created | Require both endpoint properties or ids and match existing nodes |
| Input exceeds a parser or translation budget | Compilation stops before planning or execution | Return a resource-limit error |
Key files
| File | Why to read it |
|---|---|
| crates/…/sql.rs | Defines the frontend boundary, mappings, statement dispatch, parameter numbering, and compile entry points |
| crates/…/request.rs | Defines the JSON request contract and integration with CompiledRequest |
| crates/…/tables.rs | Explains how SQL tables, columns, relationship tables, and joins become graph scope and patterns |
| crates/…/select.rs | Shows the read-query contract for projection, grouping, ordering, and row windows |
| crates/…/expr.rs | Shows the SQL expression and function semantics shared with the Helix expression model |
| crates/…/dml.rs | Shows the write-query contract for inserts, updates, deletes, truncation, and returning results |
| crates/…/spans.rs | Defines source-span behavior for nested SQL expressions and parser errors |