Skip to content

Datalog: a rule body reads a missing graph, another ledger or a graph source as empty, so NOT EXISTS over it holds for everyone, silently #2459

Open

Description

@aaj3f

A rule body that reads a graph the rules can't see gets an empty input, with no error, and since rules have NOT EXISTS now (#2122) an empty input under a negation reads as "absent for everyone". So a wiring mistake, whether a typo'd graph IRI, a graph that lives in another ledger, or a graph source alias, comes back as a full set of conclusions that look perfectly plausible. I had Claude re-check this against current main (55ecf81da) and repro it on the v4.2.6 CLI (the code path below hasn't changed since), because it's the one datalog behaviour I'm most nervous about for anyone running stored rules over data that's spread across graphs and ledgers.

What happens

One rule, deriving ex:Candidate for an account with no exclusion:

{
  "@context": {"ex": "http://example.org/"},
  "where": [
    {"@id": "?a", "@type": "ex:Account"},
    ["not-exists", ["graph", "<G>", {"@id": "?a", "ex:excluded": "?x"}]]
  ],
  "insert": {"@id": "?a", "@type": "ex:Candidate"}
}

Ledger data has ex:alice and ex:bob as ex:Accounts, plus ex:alice ex:excluded true in the named graph ex:exclusions. Ledger other has the same exclusion in its default graph. Querying data for ex:Candidate with "reasoning": "datalog" and the rule as a query-time rule:

<G> Candidates What I'd expect
http://example.org/exclusions (exists) bob bob
http://example.org/exclusionz (typo) alice, bob an error
other:main (another ledger) alice, bob an error (or bob, if bodies could read it)
a registered R2RML graph source every subject an error (or a real read)

The graph source row is from an earlier probe on a build of the #2122 stack (f57cd415f), where a NOT EXISTS over a graph source's rows marked all four test subjects clean while the same pattern without rules saw the rows. I haven't re-run that one on main, but it goes through the same path as the typo (below).

It's the same over a dataset. With "from": ["data:main", "other:main"] and the rule reading ex:excluded from the default graph, a plain query joins across both ledgers and finds alice's exclusion, but the rule sees only the first from ledger, so alice and bob both come back as candidates. Swap the order and the rule sees only other, which has no accounts, so nobody is a candidate.

By contrast, a rule head naming a missing graph is refused, which is exactly what I'd want the body to do:

error: Query error: Invalid query: datalog rule _:query_rule_0 derives into graph <http://example.org/typo>, which this ledger does not have. A rule derives only into an existing named graph; transact data into the graph to create it

Where it comes from (on 55ecf81da)

  • fluree-db-query/src/datalog_rules/execute.rs:870-882: every rule body runs with ContextConfig { dict_novelty, path_pair_limit, cancellation, evaluation_time, ..Default::default() }, so with no dataset and no R2RML provider, against the one GraphDbRef the rules run over.
  • fluree-db-query/src/graph.rs:839-866: in single-db mode, a GRAPH <iri> that isn't a registered user graph, a name of the default graph or a known R2RML graph source queues no work, so it's empty (graph.rs:19, "Graph-not-found produces empty result (not an error)"). That's right for SPARQL. With no provider in the body's context, a graph source alias lands in the same bucket (graph.rs:847-854).
  • fluree-db-query/src/datalog_rules/validate.rs:215-223: validation walks into a body's Pattern::Graph without looking at its IRI. Only Pattern::R2rml is refused (validate.rs:238), and a GRAPH <alias> in a rule body never becomes one, because that rewrite happens when a query's dataset is resolved, not when a rule is parsed.
  • fluree-db-query/src/datalog_rules/execute.rs:505-528: HeadGraphs::resolve is the head-side check the body doesn't have.
  • fluree-db-query/src/execute/runner.rs:363-370: derived facts are computed over the one db the query prepares on, which for a dataset is its primary view (fluree-db-api/src/view/dataset.rs:159-171, chosen in view/query.rs:1706-1716).
  • docs/query/datalog-rules.md:341-343 says graph-source patterns are refused in rule bodies, which is true for the pattern kinds validate.rs sees but not for GRAPH <graph-source>.

What I think we want

The minimum, and I think the part worth doing first, is to refuse loudly the way heads already are: when the rule set loads (or on a body's first run), a constant GRAPH <iri> anywhere in a body, including inside not-exists/exists, a UNION branch or a subquery, that isn't a user graph of the ledger the rules run against (or that ledger's own name) fails the query, naming the rule and the graph. It would help if the message says which case it is, e.g. " is a graph source; rule bodies read only this ledger's own graphs" or " names another ledger". A GRAPH ?g stays as it is. For a dataset of several ledgers, refusing a rule set that would silently read only the primary (or at least flagging it in the tracked reasoning block) seems like the same kind of fix.

One wrinkle: a rule set written before its input graph has any data would start failing. I think that's the right trade, since the head side already makes it, but it's probably worth a sentence in datalog-rules.md.

The fuller fix, evaluating bodies over the dataset (each member at its own t) and over graph sources, is the scope question already on #2075 ("Datalog rules over a default graph of several graphs see the primary graph's facts only"). I'd leave that decision there and do the refusal here, since it's small and what it prevents is a wrong answer that looks right. Happy to talk through it if the refusal seems too strict.

Repro (local CLI, any ledger names):

fluree create data && fluree create other
fluree insert data '{"@context":{"ex":"http://example.org/"},"@graph":[{"@id":"ex:alice","@type":"ex:Account"},{"@id":"ex:bob","@type":"ex:Account"}]}'
fluree insert data '{"@context":{"ex":"http://example.org/"},"@id":"ex:exclusions","@graph":[{"@id":"ex:alice","ex:excluded":true}]}'
fluree insert other '{"@context":{"ex":"http://example.org/"},"@graph":[{"@id":"ex:alice","ex:excluded":true}]}'
# swap G for http://example.org/exclusions, http://example.org/exclusionz, other:main
fluree query data '{"@context":{"ex":"http://example.org/"},"select":"?a","where":{"@id":"?a","@type":"ex:Candidate"},"reasoning":"datalog","rules":[{"@context":{"ex":"http://example.org/"},"where":[{"@id":"?a","@type":"ex:Account"},["not-exists",["graph","G",{"@id":"?a","ex:excluded":"?x"}]]],"insert":{"@id":"?a","@type":"ex:Candidate"}}]}'

Activity

  1. added
    bug
    area:datalog
    on Nov 19, 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

    area:datalogbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions