Developer API
OpenCypher Query Engine.
KGL includes an in-memory OpenCypher query engine. There is no external database — the .kgl file system is the store. Queries run directly against a Graph loaded from disk.
Python API
from kgl.model import Graph
from kgl.query.engine import QueryEngine
graph = Graph.load("./")
engine = QueryEngine(graph)
results = engine.run("""
MATCH (p:Person)-[r:mentors]->(q:Person)
WHERE p.name = "Alice Nguyen"
RETURN p.name, q.name, r.started
""")
for row in results:
print(row)
# {"p.name": "Alice Nguyen", "q.name": "Diana Park", "r.started": "2023-06"}
engine.run(query_str) returns list[dict[str, Any]]. Each dict maps return variable names (or aliases) to their values.
Supported syntax
Phase 1 — core implemented
| Clause | Example |
|---|---|
MATCH (n:Type) | MATCH (n:Person) |
MATCH (n)-[r:REL]->(m) | Directed edge with type |
MATCH (n)-[r]->(m) | Directed edge, any type |
MATCH (n)-[r]-(m) | Undirected edge |
WHERE n.field = "value" | Equality filter |
WHERE n.field != "value" | Inequality filter |
WHERE n.field CONTAINS "val" | Substring filter |
WHERE n.field STARTS WITH "val" | Prefix filter |
WHERE expr AND expr | Logical AND |
WHERE expr OR expr | Logical OR |
RETURN n | Return full node binding |
RETURN n.field | Return field value |
ORDER BY n.field ASC|DESC | Sort results |
LIMIT n | Truncate results |
Phase 2 — extended planned
OPTIONAL MATCH— missing match returns null row, not empty resultWITH— pipeline intermediate resultsSKIP n— offset results
Phase 3 — aggregations planned
COUNT(n),COUNT(*)COLLECT(n.field)DISTINCT- Variable-length paths:
(n)-[*1..3]->(m)
CLI usage
kgl query ./ "MATCH (p:Person)-[r:mentors]->(q:Person) RETURN p.name, q.name, r.started"
p.name q.name r.started
──────────── ────────── ──────────
Alice Nguyen Diana Park 2023-06
kgl query ./ "MATCH (n:Person) RETURN n.name" --format json
kgl query ./ "MATCH (n:Person) RETURN n.name" --format csv
Architecture
The query engine has three independently testable components:
- Lexer (
kgl/query/lexer.py) — tokenises an OpenCypher string into typed tokens - Parser (
kgl/query/parser.py) — recursive-descent parser producing an AST - Engine (
kgl/query/engine.py) — executes a parsed query against aGraph
Execution strategy
For v0.1, the engine uses brute-force enumeration. This is acceptable for human-scale graphs (thousands of nodes, not millions).
- Seed — enumerate all candidate nodes/edges matching the first
MATCHpattern - Extend — for each candidate, extend the pattern along each additional path segment
- Filter — apply
WHEREclause to each binding - Project — evaluate
RETURNexpressions - Post-process — apply
ORDER BYandLIMIT
Known limitations vs full OpenCypher
| Feature | Status |
|---|---|
Variable-length paths [*1..n] | not supported |
Aggregations (COUNT, COLLECT) | not supported |
WITH clause | not supported |
OPTIONAL MATCH | not supported |
MERGE, CREATE, DELETE | not applicable (read-only) |
UNWIND | not supported |
Property filters in patterns (n {key: "val"}) | not supported |
IN operator | not supported |