Skip to content

Semi-naive datalog recursion, and transitive closures as property paths - #2121

Merged
bplatz merged 3 commits into
feat/datalog-dependency-schedulingfrom
feat/datalog-semi-naive
Oct 15, 2026
Merged

bplatz merged 3 commits into
feat/datalog-dependency-schedulingfrom
feat/datalog-semi-naive

Conversation

@bplatz

@bplatz bplatz commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

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:

  • Semi-naive evaluation. After a recursive group's first pass, a rule re-reads only what its group derived in the previous pass. Each pass then costs roughly what it derives.
  • Transitive closures as property paths. A recursive rule that is alone in its group and shaped as a transitive closure runs once, as a property path, instead of iterating.

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. A VALUES seed binds that atom's variables to the matching facts from the previous pass, and the rest of the body reads the full overlay.

    • A nonlinear rule with two recursive patterns, like ?x anc ?y . ?y anc ?z, runs twice per pass, so a new fact in either position is extended.
    • The seed replaces the pattern when it matches 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 to do the matching.
    • A rule that reads its group's facts inside a UNION, EXISTS, subquery, property path or trailing VALUES block re-runs its whole body every pass, as before.
    • So does a rule that is not repeatable. A volatile function such as STRUUID() gives new rows for old matches every pass, and a seed carries no transaction metadata for t/op to read.
  • Closure compilation (closure.rs). Over the facts visible when the group starts, each rewritten body derives exactly what the rule's fixpoint derives:

    Rule Runs once as
    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)
    • T may be written in either direction. The body must be exactly those two triple patterns with constant predicates, and the head exactly R(x,z).
    • The nonlinear form keeps its last hop as a triple pattern. R+(x,z) would re-derive one-hop asserted facts the rule never derives, and miss 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.
  • Scheduling (execute.rs). Each active group carries the range of facts it derived in its previous pass. RuleSchedule::WholeProgram still runs every whole body every round and stays the differential reference.

  • Dependency footprint. It now also covers a rule body's trailing VALUES block and its binds.

  • datalog_fixpoint bench. Adds iterated_linear_recursion, a linear recursion that cannot compile to a path, so semi-naive iteration stays measured.

  • Docs. docs/query/datalog-rules.md covers both behaviors and how to write rules that get them. docs/design/rules-engine.md gives the algorithms and why they derive the same facts.

Behavior changes

  • Completed results are unchanged. A capped run can keep different partial facts.
  • A transitive-closure group reports one pass in reasoning.iterations.
  • In fluree-db-query, DatalogExecutionResult gains body_rows, the rows rule bodies returned. rule_body_runs counts 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.

Program #2120 Semi-naive Plus closure paths
Linear recursion 534 ms 84.3 ms 37.2 ms
Nonlinear recursion 121 ms 93.5 ms 24.0 ms
Linear recursion feeding 12 non-recursive rules 1.30 s 880 ms 839 ms

iterated_linear_recursion is 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:

Program Without seeding With seeding
Iterated linear recursion 635 ms 111 ms

Tests

  • it_datalog_semi_naive:
    • A recursive rule calling STRUUID() hits the pass limit like the reference, and one filtering on t(?z) derives the complete closure.
    • On a 24-node chain, each ancestor fact comes from exactly one body row; the whole-program schedule returns several times as many rows.
    • A nonlinear rule runs once per recursive pattern in each pass after the first.
    • A rule whose recursive read sits in a UNION, EXISTS, subquery or ? path still reaches the full closure.
    • A literal constant and a repeated variable in a recursive pattern still match exactly.
  • it_datalog_closure_paths:
    • All five shapes run in one pass and derive exactly what the whole-program schedule derives. The graph has a cycle, a self-loop, branching, asserted and literal-valued facts of the closure predicate, and edges in a named graph.
    • Cycle and self-loop reachability survive, and named-graph edges stay out of the default-graph closure.
    • A left-linear closure over a literal object iterates and derives the literal-valued facts.
    • Five rules one step away from a closure keep iterating and still match.
    • The nested JSON-LD form the policy contract tests use compiles. Those tests therefore check policy visibility over a path-derived closure.
  • 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 in UNION and EXISTS.

Each new test was confirmed to fail with the code it pins broken:

  • Semi-naive: no seeding at all, seeding only the first recursive pattern, ignoring nested reads, always replacing the pattern, skipping the repeated-variable check, and seeding non-repeatable rules. Each of the four nested shapes fails on its own.
  • Closures: skipping the literal check, the R+(x,z) nonlinear form, ignoring the step's direction, compiling rules that share a group, and iterating after the closure pass.

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.
@bplatz bplatz added enhancement New feature or request area:datalog Reasoner, rules, reasoningDefaults labels Oct 15, 2026
@bplatz
bplatz requested a review from a team October 15, 2026 03:41
@bplatz
bplatz added this pull request to stack #2125 October 15, 2026 09:45
@bplatz
bplatz removed this pull request from stack #2125 October 15, 2026 11:34
@bplatz
bplatz merged commit 9e7e229 into feat/datalog-dependency-scheduling Oct 15, 2026
19 checks passed
@bplatz
bplatz deleted the feat/datalog-semi-naive branch October 15, 2026 11:36
@bplatz

bplatz commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #2120, which now carries dependency scheduling, semi-naive recursion and closures as paths as one PR. GitHub shows this one as merged because its commits now sit in #2120's branch; nothing reached main. Review on #2120.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:datalog Reasoner, rules, reasoningDefaults enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant