Skip to content

fix(reasoning): a datalog rule whose body is a cycle derives its facts on an indexed ledger - #2376

Closed
bplatz wants to merge 1 commit into
mainfrom
fix/cyclic-bgp-eager
Closed

bplatz wants to merge 1 commit into
mainfrom
fix/cyclic-bgp-eager

Conversation

@bplatz

@bplatz bplatz commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary

A datalog rule whose body is a bare cycle of three or four edges derived nothing once its ledger was indexed, and reported nothing. On a ledger with no index the same rule derived every fact.

CONSTRUCT { ?x ex:taughtByAdvisor ?z }
WHERE { ?x ex:advisor ?y . ?y ex:teacherOf ?z . ?x ex:takesCourse ?z }

Cause

A pattern of exactly three or four triples with variable subjects and objects and fixed predicates is run by CyclicBgpOperator, which binds encoded ids. Its fast path is gated by allow_cursor_fast_path, which does not look at eager materialization, so it also ran inside rule bodies and reasoning queries. Those callers are owed decoded bindings: a rule head built from an encoded binding is dropped (instantiate returns None).

#2271 turned the operator's bounded probes off for eager callers and declared the cascade an encoded lane, but its full-scan path still served them.

Fix

The operator leaves an eager caller to its fallback, the join chain every other pattern runs under reasoning, which decodes what it emits.

Cost

A reasoning query whose whole pattern is a bare 3- or 4-edge cycle no longer takes the cyclic operator. The two such queries in the new test (the triangle itself, and the triangle with a derived fact joined after it) answered correctly before this change, so for direct queries this holds one rule for operators that emit encoded bindings rather than fixing a known wrong answer. Decoding at emit would readmit them; that is not done here.

Queries without reasoning are unaffected.

Test

a_cycle_under_reasoning_is_joined_step_by_step builds an indexed ledger and a never-indexed copy with a rule whose body is a triangle, some of whose closing edges are themselves derived. It checks the derived facts, the direct triangle and a join after it against the copy, that the cyclic operator does not run under reasoning, and that the same triangle without reasoning does take it. Without the fix the rule's query returns 0 rows over the index against 40 on the copy.

…s on an indexed ledger

A pattern of three or four edges that form a cycle is run by the cyclic
operator, which binds encoded ids. Its fast path was taken under eager
materialization too, where a reasoning query and a datalog rule body are
owed decoded bindings: a rule head built from an encoded binding
derives nothing. A rule such as

    CONSTRUCT { ?x ex:taughtByAdvisor ?z }
    WHERE { ?x ex:advisor ?y . ?y ex:teacherOf ?z . ?x ex:takesCourse ?z }

derived every fact on a ledger with no index and none once it was
indexed, with nothing reported.

The operator now leaves an eager caller to its fallback, the join chain
every other pattern runs under reasoning, which decodes what it emits.
A reasoning query whose pattern is a bare cycle loses the operator's
speed with it; the two such queries tested here answered correctly
before, so this is the cost of holding one rule for operators that emit
encoded bindings rather than a fix for them.
@bplatz bplatz added bug Something isn't working as expected area:datalog Reasoner, rules, reasoningDefaults labels Nov 13, 2026
@bplatz
bplatz requested a review from a team November 13, 2026 13:30

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bplatz I'd close this one in favor of #2368. Both fix #2325, and this one is small and safe, but declining eager callers gives up the cyclic join for every reasoning query and rule body that's a bare cycle. Claude measured what that costs on a skewed graph: a top-level reasoning triangle that main already answers correctly goes from 739 ms to 9.49 s (12.8x). A triangle rule body runs 9.36 s here vs 760 ms on #2368. #2368 decodes at emit instead, the direction #2271 took for the join lanes. It also fixes the GROUP BY instance this PR leaves open: #2368's a_sparql_collected_group_derives_one_fact_per_group fails on this branch. What I'd carry over is your test (details inline). One coordination note in case both stay open: they merge without a textual conflict, but this guard runs before #2368's emit setup, so landing both would leave #2368's decode unreachable and its span assertions red.

Full review below:


🔄 Request changes (recommend closing in favor of #2368).

@bplatz the diagnosis here is spot on, and the test is genuinely good (more inline). The issue is the fix's altitude. Sending eager callers to the fallback keeps them correct, but it costs a top-level reasoning triangle 12.8x against main on a graph with hubs, a query main already got right. It costs cyclic rule bodies 8.7x to 12.3x against #2368, which derives the same facts by decoding at emit. It also leaves the GROUP BY instance of the same defect in place: a SPARQL rule that groups in a subquery with no aggregate, or with GROUP_CONCAT, still derives nothing on an indexed ledger. Claude ran #2368's test for it against this branch: ex:activeIn Indexed, left: []. Two small things if it did go ahead: the body has no Fixes #2325, so the issue would stay open; and its !store.has_span(CYCLIC) assertion pins this design, so it and #2368 can't both stay green.

Adherence to repo commitments:

  • Patterns/abstractions: ⚠️ Reuses the existing fallback rather than inventing anything, but goes against #2271's decode-under-eager direction for lanes that emit encoded ids.
  • Performance (speed first, memory second): ✖ CRITICAL. A top-level reasoning triangle is 738.9 → 9490.2 ms against main (12.84x). Rule bodies are 8.65x to 12.31x slower than #2368 (Claude's c7i.4xlarge, 15 samples per cell). No change without reasoning.
  • Deployment targets: ✔ n/a. Shared fluree-db-query code, nothing host-specific.
  • Testing: ✔ The new test runs in grp_reasoning and fails with the guard removed (Claude's mutation: left: [] over the index), checked against a never-indexed copy. ⚠️ Nothing covers the GROUP BY instance.
  • Conventions: ✔ Thorough commit body, CI green on fd5ca5397 (run 43366060082). ⚠️ No Fixes #2325.

Verified at branch HEAD fd5ca5397 on a box: cross-applied suites (this PR's test on #2368's code, #2368's tests on this code), a mutation check of the guard, probes, and the three-way timing, plus CI run 43366060082 green.

Just be sure this doesn't merge alongside #2368. My preference is closing it once its test has moved over there.

// query, a datalog rule body) is owed decoded bindings, as from every
// other operator that emits encoded ones: a rule head built from an
// encoded binding derives nothing. The fallback chain decodes.
if ctx.eager_materialization {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL (performance). Declining every eager caller sends reasoning queries whose pattern is a bare cycle, and every cyclic rule body, from the cyclic join to the nested-loop chain. On a graph with hubs that chain is an order of magnitude slower.

The mechanism: the fallback is the nested-loop chain. For every ?x ex:knows ?y row it probes ?y's out-edges and then checks each closing edge, so a hub's in- and out-edges multiply into per-row probes. The cyclic join scans each predicate once into in-memory relations, semi-join-prunes them to a fixpoint (cyclic_bgp.rs:1605-1666) and enumerates the cycle from those indexes.

Claude measured it on a c7i.4xlarge: 20,000 nodes, 10 hubs with 1,000 in- and 1,000 out-edges, 100,000 random edges, indexed with 1% after the index, release build, 15 samples per cell.

  • A top-level reasoning triangle that main answers correctly in 738.9 ms takes 9490.2 ms here (12.84x).
  • The triangle rule body takes 9358.3 ms here against 760.4 ms on fix(query): a cyclic datalog rule body derives facts on an indexed ledger #2368, which derives the same 7,583 facts.
  • A uniform triangle takes 831.3 ms against 96.1 ms, and a uniform square 454.7 ms against 41.6 ms.
  • Without reasoning nothing moves (511.6 vs 500.8 ms; that path only gains one boolean test, so the 2% is binary layout).

The fix is the one the body names and sets aside: decode at emit, as #2368 does, so eager callers keep the operator. My recommendation is to close this PR in favor of #2368 and carry the test over (next comment).

}
drop(guard);
assert!(
!store.has_span(CYCLIC),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, but the part of this PR I'd keep. This test is good. Its triangle closes on an object-only IRI (?z), so on #2368 it decodes through EncObj::to_binding's IRI_REF arm, a path #2368's own tests don't reach. Some of its closing edges are derived by another rule, and it checks against a never-indexed copy with a non-reasoning positive control. Claude ran it against #2368's code with span assertions off and every row passes. Only this !store.has_span(CYCLIC) fails there, by design. Moved to #2368 with the assertion flipped to store.has_span(CYCLIC), it pins the design that lands.

bplatz added a commit that referenced this pull request Nov 13, 2026
…es the cyclic join

Carried over from #2376 with its span assertion flipped: the cyclic join
runs under reasoning and decodes what it emits. The closing variable is an
object-only IRI, which decodes through the object arm, and some closing
edges are derived by a second rule; the rows are checked against a
never-indexed copy.
@bplatz

bplatz commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2368, which decodes at emit and keeps the cyclic join for reasoning queries and rule bodies. The test here moved there as a_cycle_under_reasoning_takes_the_cyclic_join with its span assertion flipped.

@bplatz bplatz closed this Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:datalog Reasoner, rules, reasoningDefaults bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants