Skip to content

Datalog rule with a cyclic body (triangle) derives nothing on an indexed ledger #2325

Closed

Description

@bplatz

Found while working on #2271. Reproduces on main (8308ba1), independent of that PR.

Symptom

A datalog rule whose body is a triangle derives no facts once the ledger is indexed. A never-indexed copy of the same ledger derives all of them. Nothing errors, so the derived facts are silently missing.

CONSTRUCT { ?x ex:inTriangleWith ?y }
WHERE { ?x ex:knows ?y . ?y ex:knows ?z . ?z ex:knows ?x }

Data: a→b→c→a committed and indexed, then d→e→f→d committed after the index (in novelty). SELECT ?x ?y WHERE { ?x ex:inTriangleWith ?y } with reasoning: datalog:

Ledger Rows
never indexed 6 (all six edges)
indexed 0, including the indexed triangle
indexed, FLUREE_CYCLIC_BGP=0 6

FLUREE_DISABLE_QUERY_FAST_PATHS=1 does not change it, because it doesn't disable the cyclic BGP operator. Disabling the cyclic operator is what fixes it.

Likely area (cause not established)

Rule bodies run with eager materialization (datalog_rules/execute.rs, pass_db.eager = true), so rule heads expect decoded Sid/Lit bindings. CyclicBgpOperator takes the fast path whenever allow_cursor_fast_path holds, and that doesn't consider eager. The likeliest cause is the operator emitting encoded bindings that head instantiation cannot build a fact from. #2271 keeps the cascade's probes off eager callers (its lane is LaneOutput::Encoded). On main admission declined all eager probes. Rows are missing either way, so the scan/emit path is the suspect, not the probes. Not confirmed.

Open question: whether a top-level reasoning query (not a rule body) that joins a cyclic BGP's output with derived Sid bindings loses rows the same way.

Repro test

#[tokio::test(flavor = "current_thread")]
async fn cyclic_rule_body_derives_on_an_indexed_ledger() {
    let fluree = FlureeBuilder::memory().build_memory();
    let ctx = json!({"ex": "http://example.org/ns/"});
    let indexed = json!({"@context": ctx, "@graph": [
        {"@id": "ex:a", "ex:knows": {"@id": "ex:b"}},
        {"@id": "ex:b", "ex:knows": {"@id": "ex:c"}},
        {"@id": "ex:c", "ex:knows": {"@id": "ex:a"}}
    ]});
    let novelty = json!({"@context": ctx, "@graph": [
        {"@id": "ex:d", "ex:knows": {"@id": "ex:e"}},
        {"@id": "ex:e", "ex:knows": {"@id": "ex:f"}},
        {"@id": "ex:f", "ex:knows": {"@id": "ex:d"}}
    ]});
    let rule = json!({
        "@context": {"f": "https://ns.flur.ee/db#"},
        "@id": "http://example.org/ns/rule/triangle",
        "f:rule": {"@type": "f:sparql", "@value":
            "PREFIX ex: <http://example.org/ns/>\nCONSTRUCT { ?x ex:inTriangleWith ?y } \
             WHERE { ?x ex:knows ?y . ?y ex:knows ?z . ?z ex:knows ?x }"}
    });
    let id = "it/cyclic-rule:main";
    let ledger = genesis_ledger_for_fluree(&fluree, id);
    let ledger = fluree.insert(ledger, &indexed).await.unwrap().ledger;
    fluree.insert(ledger, &rule).await.unwrap();
    fluree.reindex(id, ReindexOptions::default()).await.unwrap();
    let ledger = fluree.ledger(id).await.unwrap();
    fluree.insert(ledger, &novelty).await.unwrap();

    let view = fluree.db(id).await.unwrap();
    let q = json!({"@context": ctx, "select": ["?x", "?y"],
        "where": [{"@id": "?x", "ex:inTriangleWith": "?y"}], "reasoning": "datalog"});
    let result = fluree.query(&view, QueryInput::JsonLd(&q)).await.unwrap();
    let rows = normalize_rows(&result.to_jsonld(&view.snapshot).unwrap());
    assert_eq!(rows.len(), 6, "{rows:?}"); // 0 on main; 6 with FLUREE_CYCLIC_BGP=0
}

The fix needs a SPARQL-surface twin of the test, per the shared-IR convention.

Activity

  1. added
    bug
    P1
    area:query
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1area:querybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions