Return Cypher query results as graph objects#873
Open
paracycle wants to merge 1 commit into
Open
Conversation
2e6a202 to
bc2a231
Compare
Add an object-returning `Rubydex::Query#run(graph)` alongside the existing
string-returning `Query#render`. Where `render` formats rows into a table
or JSON string, `run` returns an `Array<Hash>` whose values are real Ruby
objects — including `Rubydex::Declaration`/`Definition`/`Document` handles
for node columns — so callers can navigate the graph directly instead of
re-parsing formatted text.
- cypher-parser 0.4: add `GraphProvider#node_id` and a `CypherValue::Node
{ id, label, name }` variant carrying an opaque node id.
- rubydex schema: implement `node_id` and `NodeRef::decode` over the
`decl:/def:/doc:<u64>` id scheme.
- FFI: add `rdx_query_run_rows`/`rdx_result_set_free` exposing a structured
`CResultSet`/`CCell` result instead of a preformatted string.
- Gem: `Query#run` builds `Array<Hash>`, decoding node cells back into the
appropriate Declaration/Definition/Document handles.
9394f29 to
e5bb1da
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal
Build on the Cypher query engine from #868 so callers get matched graph nodes back as first-class objects, not as formatted text they have to re-parse or iterate over.
#868 added
Rubydex::Query#render(graph, format), which runs a query and returns atable/jsonstring — great for the CLI and humans, but a dead end for programmatic callers: to actually use a matched class or method you'd have to parse the formatted output, then look the node back up in the graph by name. This PR adds an object-returning sibling,Query#run(graph), that hands you the matched nodes directly.What changes
Rubydex::Query#render(graph, format = :table)— unchanged: rows → formattedString.Rubydex::Query#run(graph)— new: rows →Array<Hash>, where the values are real Ruby objects. A column that binds a node (e.g.RETURN n) comes back as a liveRubydex::Declaration/Definition/Documenthandle; scalar columns come back as plain Ruby values.So instead of:
you write:
The query language does the matching and filtering; you get the resulting nodes back ready to use, without writing your own traversal/iteration to turn names back into graph objects.
How it works
cypher-parser0.4: the generic executor learns to carry node identity.GraphProvidergainsnode_id, andCypherValuegains aNode { id, label, name }variant, so a bound node survives execution as an opaque id rather than being flattened to text. (Still dependency-free; rubydex-agnostic.)query::cypher::schema): implementsnode_idandNodeRef::decodeover the existingdecl:/def:/doc:<u64>id scheme, mapping between graph nodes and that opaque id.rubydex-sys): newrdx_query_run_rows/rdx_result_set_freereturn a structuredCResultSetofCCells (scalars + node handles) instead of a preformatted string.ext/rubydex/graph.c):Query#runwalks theCResultSetinto anArray<Hash>, decoding each node cell back into the appropriateDeclaration/Definition/Documenthandle.Why this matters for both APIs
CypherValue::Node), so anyGraphProvider-backed consumer — Rust callers, the FFI layer, future language servers/tools — can get matched nodes back by id and resolve them to their own representation, rather than being limited to formatted strings.In short: the query does the matching; callers get the nodes, not a transcript of them.
Verification
cargo build/cargo testgreen; clippy clean.rake compilegreen;rake ruby_test TEST=test/graph_test.rb→ 96 runs, 0 failures (includes new object-result tests).