Repository navigation
Semi-naive datalog recursion, and transitive closures as property paths - #2121
Merged
bplatz merged 3 commits intoOct 15, 2026
Merged
Conversation
After a recursive group's first pass, a rule can only derive something new from a match that uses a fact the group derived in the previous pass. Each rule whose reads of its group's facts are all top-level triple patterns now re-runs once per such pattern, with that pattern seeded by a VALUES block of the previous pass's matching facts while the rest of the body reads everything derived so far. A nonlinear rule with two recursive patterns runs twice per pass, so a new fact in either position is extended. The seed replaces the pattern when it matches facts exactly as a scan would; a literal or scan-encoded IRI constant, a datatype constraint or a variable predicate keeps the pattern behind the seed. A rule that reads its group's facts inside a UNION, EXISTS, subquery, property path or the trailing VALUES block re-runs its whole body every pass, as before. The whole-program schedule still runs whole bodies and stays the differential reference. DatalogExecutionResult gains body_rows, the rows rule bodies returned, which shows re-derivation directly. The dependency footprint now also reads the trailing VALUES block and its binds.
A recursive group of one rule shaped as a transitive closure now runs once, as a body with a one-or-more property path, instead of iterating. Over the facts visible when the group starts, the rewritten body derives exactly what the rule's fixpoint derives: T(x,y) . R(y,z) -> R(x,z) runs as T+(x,y) . R(y,z) R(x,y) . T(y,z) -> R(x,z) runs as R(x,y) . T+(y,z) R(x,y) . R(y,z) -> R(x,z) runs as R+(x,y) . R(y,z) T may be written in either direction. The body must be exactly the two triple patterns over three distinct variables with constant predicates and no datatype constraint, and the head exactly R(x,z). The nonlinear form keeps its last hop as a triple pattern so it neither re-derives one-hop asserted facts nor misses literal-valued ones. The path operator follows only IRI-valued objects. The left-linear path ends at the head's object, so that shape compiles only after a scan finds no literal object of T's predicate. A traversal that hits the path operator's visited-node limit falls back to iteration. The datalog_fixpoint bench gains iterated_linear_recursion, a linear recursion that cannot compile to a path, so semi-naive iteration stays measured now that the other recursive programs run as paths.
Seeding a recursive rule with only the previous pass's facts assumes that re-running its body over old matches would give the same rows. A rule whose footprint is not repeatable breaks that: a volatile function such as STRUUID() gives new rows for old matches every pass, so seeding stopped it early and reported a completed fixpoint the whole-program reference never reaches; and a seed carries no transaction metadata, so a filter on t(?z) dropped every match after the first pass and derived an incomplete closure that still reported completion. Such rules now re-run their whole body every pass, as before semi-naive evaluation.
Contributor
Author
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.
Summary
Recursive datalog rules stop re-deriving what they already know. Each pass of a recursive group used to re-run every rule body over everything derived so far, so a pass re-derived every fact the group already had. Two changes:
Stacked on #2120.
Changes
Semi-naive re-evaluation (
fluree-db-query/src/datalog_rules/delta.rs). A rule's recursive atoms are its top-level triple patterns that may match one of its group's writes. After the group's first pass, the rule runs once per recursive atom. AVALUESseed binds that atom's variables to the matching facts from the previous pass, and the rest of the body reads the full overlay.?x anc ?y . ?y anc ?z, runs twice per pass, so a new fact in either position is extended.UNION,EXISTS, subquery, property path or trailingVALUESblock re-runs its whole body every pass, as before.STRUUID()gives new rows for old matches every pass, and a seed carries no transaction metadata fort/opto read.Closure compilation (
closure.rs). Over the facts visible when the group starts, each rewritten body derives exactly what the rule's fixpoint derives:T(x,y) . R(y,z) → R(x,z)T+(x,y) . R(y,z)R(x,y) . T(y,z) → R(x,z)R(x,y) . T+(y,z)R(x,y) . R(y,z) → R(x,z)R+(x,y) . R(y,z)Tmay be written in either direction. The body must be exactly those two triple patterns with constant predicates, and the head exactlyR(x,z).R+(x,z)would re-derive one-hop asserted facts the rule never derives, and miss literal-valued ones.T's predicate.Scheduling (
execute.rs). Each active group carries the range of facts it derived in its previous pass.RuleSchedule::WholeProgramstill runs every whole body every round and stays the differential reference.Dependency footprint. It now also covers a rule body's trailing
VALUESblock and its binds.datalog_fixpointbench. Addsiterated_linear_recursion, a linear recursion that cannot compile to a path, so semi-naive iteration stays measured.Docs.
docs/query/datalog-rules.mdcovers both behaviors and how to write rules that get them.docs/design/rules-engine.mdgives the algorithms and why they derive the same facts.Behavior changes
reasoning.iterations.fluree-db-query,DatalogExecutionResultgainsbody_rows, the rows rule bodies returned.rule_body_runscounts each seeded run separately.Benchmark
FLUREE_BENCH_SCALE=small cargo bench -p fluree-db-api --bench datalog_fixpoint, 100 chains of 16 nodes, same laptop:The first three programs are transitive closures, so their final column measures closure compilation on top of semi-naive evaluation.
iterated_linear_recursionis a linear recursion with a filter that keeps it off the path lane. It isolates semi-naive evaluation; the "without seeding" run is this branch with every rule re-running its whole body, which is #2120's evaluation:Tests
it_datalog_semi_naive:STRUUID()hits the pass limit like the reference, and one filtering ont(?z)derives the complete closure.UNION,EXISTS, subquery or?path still reaches the full closure.it_datalog_closure_paths:it_datalog_rule_schedule: the random differential test grows to 60 programs, each with a literal-valued fact. New shapes are three-pattern chains, variable predicates, repeated variables, constant subjects, and reads nested inUNIONandEXISTS.Each new test was confirmed to fail with the code it pins broken:
R+(x,z)nonlinear form, ignoring the step's direction, compiling rules that share a group, and iterating after the closure pass.