Skip to content
This repository was archived by the owner on Nov 21, 2026. It is now read-only.

Python functions callable from SQL and openCypher, and record event callbacks - #391

Merged
tae898 merged 6 commits into
mainfrom
udf-callbacks-386
Oct 21, 2026
Merged

tae898 merged 6 commits into
mainfrom
udf-callbacks-386

Conversation

@tae898

@tae898 tae898 commented Oct 21, 2026

Copy link
Copy Markdown

Closes #386.

Two things no query language can express, which the other embedded databases' Python APIs offer (sqlite3, DuckDB and Kuzu/LadybugDB register Python functions; sqlite3/apsw expose change hooks).

  • Database.register_function(name, fn, *, min_args=None, max_args=None, deterministic=False, replace=False) and unregister_function(name): a Java adapter (PythonFunctions.java, extends SQLFunctionAbstract) registered in the engine's DefaultSQLFunctionFactory; SQL calls it by name, openCypher with a dotted name (py.slugify) because its validator rejects unknown bare names. Arguments and results go through the bindings' type conversion; an exception in fn (even KeyboardInterrupt) becomes one query error naming the function, never a JVM failure. Registration is process-wide in the engine: a Database removes the functions it registered when it closes, and functions should be registered before concurrent queries start. Measured cost: about 5 to 10 microseconds per call (20,000 rows, best of 5, SELECT plus_one(n) against SELECT n + 1; the docs say how).
  • Database.on(event, fn, type_name=None) -> EventHandle (remove(), context manager) for before_/after_ create, update, delete over the engine's record events (PythonRecordEvents.java, one adapter for the six listener interfaces); a before_* callback returning False vetoes the change; a type filter matches that type only.
  • Not supported: aggregate functions, read events, Python functions in the openCypher validator's function list.
  • Docs: api/database.md, guide/core/queries.md, guide/core/database.md, java-api-coverage.md, bridge.md, architecture.md, contributing.md, testing.md; example 28 (script, page, README, index, nav).
  • Tests: test_python_callbacks.py, 34 cases (functions in SELECT and WHERE, NULLs, arity, errors surfacing, unregister, openCypher; each event firing once with the record, type filter, veto, remove); no skips.

Checked locally against the 26.10.2 engine with the bridge jar rebuilt: the new tests with test_core, test_bridge_fixes, test_database_utils, test_async_executor and test_cypher (129 passed), bandit (low/low), pre-commit, the strict docs build.

🤖 Generated with Claude Code

@tae898
tae898 force-pushed the udf-callbacks-386 branch 2 times, most recently from 4622017 to 0855b60 Compare October 21, 2026 07:26
tae898 and others added 6 commits October 21, 2026 18:06
…allbacks (#386)

Database.register_function() / unregister_function() register a Python callable as an engine SQLFunction
(bridge class PythonFunctions): SELECT and WHERE in SQL, and openCypher through a namespaced name such as
py.slugify (its validator rejects an unknown bare name). An exception in the function is an ArcadeDBError naming
the function, never a JVM crash. Database.on() / EventHandle.remove() run Python callbacks on before/after
create, update and delete (bridge class PythonRecordEvents), database-wide or per type; a before callback that
returns False vetoes the change, and an exception fails the operation. Tests, API and guide docs, coverage,
bridge and testing docs, and example 27 in the same change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…base is released with its last result (#386)

The registry, the function proxies and the event proxies hold the Database through a weak reference: the engine holds the proxies, so a strong reference would keep the Database alive until a gc pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…th tools/ab.py; Example 28 prints the cost on the reader's machine (#386)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eak), readable error for an unconvertible return, Ctrl-C always a KeyboardInterrupt, Document.save()/delete() raise ArcadeDBError; docs: read-only begin(), the ~30 bytes per numeric argument JPype note (#386)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…r-call figure in the examples README (#386)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python functions callable from SQL and Cypher, and record change callbacks

1 participant