A public API to register procedures
Summary
Applications that embed Grafeo cannot add their own procedures: BuiltinProcedures is built internally (crates/grafeo-engine/src/procedures.rs, BuiltinProcedures::new) and GrafeoDB has no way to register a Procedure.
Example
let db = GrafeoDB::new_in_memory();
db.register_procedure(Arc::new(MyProc))?; // name "test.my.proc", its signature and rows
db.execute_cypher("CALL test.my.proc('a') YIELD out RETURN out")?;
Direction
A registration method on GrafeoDB (or on Config) that takes an Arc<dyn Procedure>. Built-in names cannot be replaced, grafeo.procedures() lists registered procedures, and argument and YIELD checks stay the existing ones. The Procedure trait is already public.
The 50 CALL scenarios of the openCypher TCK 2024.2 (Call1-6) register test procedures this way. With such an API a TCK harness can run them, and their argument and YIELD error scenarios exercise the existing validation.
Affected profiles
- [X] embedded (facade, Python, Node.js, C and friends)
- [ ] edge (WASM, grafeo-web)
- [X] server (grafeo-server)
Contributing
- [X] I would like to work on this myself.
Working on a design, will update on what I will settle on for now, will probably be next release though, this one has a different scope.
Here is the design I settled on for 0.6.5 (so next release end of dec/early jan).
It ships as an experimental API, so details may still change in a patch release.
Registering a procedure. A Rust procedure is registered on the database's configuration before it opens:
let config = Config::persistent("amsterdam.grafeo").with_procedure(Arc::new(MyLabels));
let db = GrafeoDB::with_config(config)?;
The set of procedures is fixed for as long as the database is open. Registering after open is not offered, which keeps every call free of locking and keeps a running or prepared query from seeing the set change. The Procedure trait and the types it uses are exported from a new grafeo::procedures module.
Names.
- A registered procedure has a namespace of its own:
myapp.score, called asCALL myapp.score(...). - The namespaces
grafeo,dbandsearchare reserved for built in procedures, so a built in procedure can never be replaced. This holds in every build, including builds without the optional features. - Names match exactly, and two names that differ only in case are refused.
- A registered name cannot be taken by
CREATE PROCEDUREor dropped byDROP PROCEDURE. A database file that already holds a stored procedure of that name does not open until the stored one is renamed or dropped. - A name that breaks a rule fails the open with an error that says which rule.
Calls.
- A registered procedure goes through the same checks as a built in one: its arguments are checked and filled with their defaults before it runs, a YIELD of a column it does not have fails before it runs, and a session without external access refuses a procedure that declares it reaches outside the database.
CALL grafeo.procedures()lists it with its parameters and columns.- It reads the graph as the calling query sees it, including the writes of the query's own transaction.
- In 0.6.5 a registered procedure only reads the graph: one that declares it writes is refused. Writing procedures need a writer in the procedure context, which is a separate design.
- To stop at the query timeout, a procedure polls the exported
Tickerin its work loops. - What it returns counts against the query's memory limit. A result whose rows do not match its columns fails with an error instead of a panic.
Python. The same is available from Python through the constructor:
db = grafeo.GrafeoDB(
"amsterdam.grafeo",
procedures=[grafeo.Procedure("myapp.labels", labels, columns=["label"])],
)
- The function receives a context and its arguments by name, and returns rows as tuples.
- The context reads nodes and edges at the query's view and offers
check_deadline(). - An exception raised by the function fails the query with a
GrafeoErrorthat names the procedure and the exception. - Calling the database from inside the function is refused rather than left to hang; the context is the way in.
Who builds what. If you would like to take the Rust part (the registry, the name rules, the grafeo::procedures module and its tests), that would be very welcome. We will build the Python part on top of it, since it changes how the Python binding handles the GIL. The cases the tests should cover are below; each row is one test, and the oracle is a registered myapp.labels built like grafeo.labels.
Test cases
Each row is a test in grafeo-engine/tests (a new file, a mod line in tests/main.rs), unless it says otherwise.
| # | Case | Expected |
|---|---|---|
| 1 | CALL myapp.labels() in GQL, Cypher (backticked, and unquoted after the Cypher fix) and SQL/PGQ, with and without YIELD |
Equal to grafeo.labels |
| 2 | Names labels, grafeo.x, GRAFEO.x, db.x, Db.x, search.x |
Open fails, naming the rule |
| 3 | search.text in a build without text-index |
Open fails (reserved in every build) |
| 4 | myapp..x, .x, x., my-app.x, 1app.x |
Open fails |
| 5 | myapp.labels registered twice; myapp.labels and MyApp.Labels |
Open fails |
| 6 | CALL MyApp.Labels() |
Unknown procedure (42002), with the listing hint |
| 7 | CREATE PROCEDURE myapp.labels plain, IF NOT EXISTS, OR REPLACE, and inside a transaction |
Already exists; no-op; refused; already exists |
| 8 | DROP PROCEDURE myapp.labels |
Refused, naming it as registered |
| 9 | A file holding stored myapp.labels, opened with the registration / without it |
Open fails, naming both / the stored one runs |
| 10 | A stored procedure whose body calls myapp.labels(), on a database with and without the registration |
Equal to the oracle / unknown procedure at the call |
| 11 | A procedure declaring GraphAccess::Writes |
Open fails |
| 12 | ExternalAccess::Reads on a session without external access: direct, through a stored body, EXPLAIN |
42501 GRAFEO-Q008 before planning, in all three |
| 13 | A read-only session and a read-only transaction | Runs |
| 14 | Missing required argument, wrong type, outside domain, unknown map key, too many positional | The same errors a built-in with the same ParameterDefs gives (oracle: a built-in) |
| 15 | YIELD of an unknown column | no_such_yield_column at planning; a counter shows compute never ran |
| 16 | compute returns other columns, or a row of another width |
A typed error, no panic |
| 17 | Inside a transaction after INSERT (:Person) |
Sees Person (oracle) |
| 18 | A concurrent commit after the call has started (a barrier in compute) |
Not seen |
| 19 | A USE graph, and a historical view where CALL is allowed |
Reads that graph and that view (oracle) |
| 20 | A procedure that polls Ticker, past query_timeout (the deadline probe) |
Timeout, no partial answer |
| 21 | A result past the query memory limit | S001 |
| 22 | compute returns an error of a given kind |
The caller gets that kind and code |
| 23 | compute panics |
The panic reaches the caller; afterwards a query runs and the read horizon advances |
| 24 | grafeo.procedures() |
Lists myapp.labels with its description, parameters, columns, writes false and its access, sorted; YIELD works |
| 25 | Drop the database, then with a session held drop the database and then the session | The procedure's Arc count is back to the test's own |
| 26 | Two databases from clones of one Config; a third without the call |
Both run it and list it; the third does not |
| 27 | The edge and lpg profiles (no algos) |
Registration and CALL work |
| 28 | Facade doc example: register, call, list | Compiles and runs (doctest) |
I'm taking the Rust part of your design (registry, name rules, grafeo::procedures, the tests); the PR will go against the 0.6.5 branch once it's cut.