Skip to content

fix(datalog): refuse a rule body that reads a graph its rules cannot see - #2463

Open
aaj3f wants to merge 5 commits into
mainfrom
fix/datalog-body-graphs
Open

aaj3f wants to merge 5 commits into
mainfrom
fix/datalog-body-graphs

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #2459

Problem

A datalog rule body runs against one graph of one ledger, with no dataset and no graph-source reader. In that context a GRAPH <iri> naming anything other than a graph of that ledger yields no rows, which is SPARQL's answer for a graph outside the dataset but a wrong one for a rule: under a negation an empty input reads as "absent for everyone". So a typo'd graph IRI, another ledger, or a graph source in a body derived a full, plausible set of conclusions, silently. The same happened over a default graph that merges several graphs ("from": ["data:main", "other:main"]): a plain query joins across both, while the rules read only the first and conclude as if the others' facts were absent. Swap the order and the answer changes.

A rule head naming a missing graph was already refused (HeadGraphs::resolve); bodies had no counterpart, and the validator walked into Pattern::Graph without looking at its name.

What changed

  • A rule body is refused when it names a graph its rules cannot read, before the rules run, naming the rule and the graph and saying which case it is: a graph the ledger does not have as of the query's t, another ledger ('other:main'), a graph source, or a system graph. The check covers every constant GRAPH in a body, at any depth: under not-exists/FILTER NOT EXISTS, in an EXISTS expression, a UNION branch, a subquery, and a GRAPH whose group reads no fact. GRAPH ?g is unchanged, and the ledger's own address (any spelling) still reads its default graph. A graph a commit after t dropped fails as the graph operator already fails it (GraphDropped, 404).
  • A rule set over a default graph of several graphs is refused (two ledgers in from, two FROM clauses, or two graphs of one ledger), rather than answering over the first graph only. An empty rule set refuses nothing, so a ledger whose config turns datalog on but holds no rules answers multi-ledger queries as before. Whether rules should read every member stays on Scopes of several graphs: a one-graph witness type, and the readers that still read one graph #2075.
  • One classifier for what GRAPH <iri> reads in a single-ledger view. single_ledger_graph (fluree-db-query/src/context.rs) now backs both the graph operator's single_db_user_graph_id / single_db_names_default_graph / refuse_dropped_graph and the rule check, so a body is accepted exactly when the operator would read the graph it names. The no-drops path does the same work as before (graph_position + visible_at, plus one has_drops() check).
  • The rule footprint records the constant graphs a body names (RuleFootprint::read_graphs), from the walk the stratification analysis already does, rather than a second walk.
  • The check runs before the reasoning cache lookup. The cache key does not cover how many graphs the default graph merges, so a one-graph materialization would otherwise be served to a several-graph query with the same primary (the tests pin this).
  • PrepareConfig carries the query's graph-source reader (with_graph_sources), used only to label a refused name as a graph source; the refusal itself does not depend on it.
  • Docs: docs/query/datalog-rules.md (the graph-source bullet was true only for the pattern kinds the validator sees; new "What a body can read" section, including that a rule set written before its input graph has data fails until the graph exists), and a breaking-change entry in docs/reference/compatibility.md.

Breaking changes

  • A rule body naming a graph the ledger does not have, another ledger, a graph source or a system graph used to match nothing; the query now fails with a 400 naming the rule and the graph. That includes a rule set written before the graph it reads has data.
  • A query whose default graph merges several graphs and has datalog rules to run (stored, f:rulesSource, or query-time) now fails with a 400 instead of running them over the first graph. it_rules_cross_ledger::dataset_query_pulls_rules_from_model_ledger pinned the old behavior over from: [data, other]; it now reads the second ledger as a named graph (the same multi-ledger dataset path) and asserts the refusal for the two-member default graph.

Tests

fluree-db-api/tests/it_datalog_body_graphs.rs (in grp_reasoning), on the issue's fixture extended so every case answers differently (an exclusion in data's default graph, in its ex:exclusions graph, and in other's default graph):

  • GRAPH <ex:exclusions>, the ledger's own address, and GRAPH ?g derive the right candidates;
  • <ex:exclusionz>, another ledger (pure address and IRI/address ambiguous spellings) and txn-meta are refused with a 400 naming the rule and the graph;
  • every placement on both surfaces (JSON-LD positive graph, union, query subquery, a graph holding only a bind; SPARQL FILTER NOT EXISTS, !EXISTS, UNION, sub-SELECT); a stored rule on the SPARQL query surface;
  • a rule set that reads a graph before it exists fails, then runs once the graph is written;
  • from: [data, other], [other, data] and [data, data#exclusions] with rules are refused, after a one-graph query has cached its materialization; with no rules the multi-ledger datalog query answers as before; SPARQL FROM/FROM twin with stored rules in both orders.

it_iceberg_local_fs::a_rule_body_naming_a_graph_source_is_refused: a registered R2RML source (and an unmapped one) named in a rule body is refused with the graph-source message, on the ledger path and with the source in the dataset's fromNamed, for JSON-LD and SPARQL rules, while the same GRAPH in a plain dataset query reads the source's rows.

Non-vacuity, each mutation committed-then-restored: skipping the body-graph loop fails the five body-graph tests and the graph-source test (the three others pass); disabling the several-graphs refusal fails exactly the two several-graphs tests; moving the check after the cache lookup fails rules_over_a_default_graph_of_several_graphs_are_refused (the cached one-graph answer is served).

The issue's repro, on a CLI built from this branch (and on the v4.2.4 CLI, where <…exclusionz> and other:main each answered alice and bob): <…exclusions> still derives bob only; <…exclusionz> and other:main fail naming the rule and the graph. Over HTTP (fluree server run, POST /v1/fluree/query/data:main) those two are 400 err:db/InvalidQuery, and so is from: ["data:main", "other:main"] with the rule.

Suites run locally, all green: grp_reasoning 285, it_reasoning_lane_cache 2, grp_query_reason 106, grp_graphsource 283, grp_ledger 311 (the graph-drop tests the shared classifier now serves), grp_query 614 (1 ignored), grp_query_sparql 578 (3 ignored), fluree-db-query lib 1861 (2 ignored), it_iceberg_local_fs 54; re-run after merging origin/main for the ones this touches. cargo clippy -p fluree-db-query -p fluree-db-api --all-features --all-targets --no-deps -- -D warnings is clean. The workspace-wide gate is CI's.

Performance: the graph operator asks the shared classifier once per row; on a ledger that never dropped a graph it does what it did before (graph_position + visible_at) plus one has_drops(). The rule check runs once per reasoning query with a non-empty rule set, one registry lookup per constant body graph, ahead of materialization.

Remaining / out of scope

  • Which rules a dataset query runs: today, the stored and configured rules of its first ledger only. With FROM <other> FROM <data> and rules stored in data alone, there is nothing to run and nothing is refused. That is the "should rules see every member" decision on Scopes of several graphs: a one-graph witness type, and the readers that still read one graph #2075.
  • A GRAPH ?g whose ?g a VALUES binds to a name the ledger does not have still yields no rows, as SPARQL specifies for a variable graph.
  • OWL 2 RL materialization over a several-graph default graph reads the primary graph only too; it adds no negation, so the result is missing entailments rather than wrong conclusions, and it isn't touched here.

🤖 Generated with Claude Code

https://claude.ai/code/session_018gr44QJuUPpGdwWfXE9Bz9

aaj3f and others added 3 commits November 19, 2026 12:18
…ger view

The graph operator decides what `GRAPH <iri>` reads, in a view of one
ledger with no dataset, through three `ExecutionContext` methods that each
re-derived the graph position and read the registry on their own. A datalog
rule body is about to be checked against the same question before its rules
run, and two copies of that answer would be free to drift: a body could be
accepted for a name the operator reads as empty, or refused for one it reads.

`single_ledger_graph` answers it once: the default graph (the ledger's own
address, in any spelling), a user graph there as of `t`, a graph a later
commit dropped (`GraphDropped`), or a name it reads no graph under, and why
(`UnreadGraph`). The three methods now delegate to it. The operator calls
them once per row, so the drop lookup stays behind `has_drops()`: a ledger
that never dropped a graph does the same work as before, `graph_position`
plus `visible_at`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44QJuUPpGdwWfXE9Bz9
A rule body runs against one graph of one ledger, with no dataset and no
graph-source reader. There, a `GRAPH <iri>` naming anything but a graph of
that ledger yields no rows: SPARQL's answer for a graph outside the dataset,
but a wrong one for a rule, because under a negation an empty input reads as
"absent for everyone". A typo'd graph IRI, another ledger or a graph source
in a body derived a full, plausible set of conclusions without a word. So
did a default graph that merges several graphs (`from` naming two ledgers):
a plain query reads them all, the rules read the first, and swapping the
order changed the answer. Rule heads naming a missing graph were already
refused; bodies were not, and the validator walked into `Pattern::Graph`
without looking at its name.

Before the rules run, a rule set is now refused, naming the rule and the
graph, when a body names (at any depth: under a negation, in an EXISTS
expression, a UNION branch or a subquery) a graph the ledger does not have
as of the query's `t`, another ledger, a graph source, or a system graph,
judged by the same table the graph operator reads by. `GRAPH ?g` and the
ledger's own address are unchanged. A rule set over a default graph of
several graphs is refused too; an empty rule set refuses nothing, so a
ledger whose config enables datalog without rules answers as before.

- The rule footprint records each constant graph a body names
  (`read_graphs`), from the walk stratification already does.
- The check runs before the reasoning cache lookup: the cache key does not
  cover how many graphs the default graph merges, so a one-graph
  materialization would otherwise be served to a several-graph query.
- `PrepareConfig` carries the query's graph-source reader, used only to say
  in the refusal that a name is a graph source.
- The cross-ledger dataset test that ran rules over `from: [data, other]`
  now reads the second ledger as a named graph, and asserts the refusal.
- Docs: what a body can read, including that a rule set written before its
  input graph has data fails until the graph exists; a compatibility entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44QJuUPpGdwWfXE9Bz9
@aaj3f aaj3f added bug Something isn't working as expected breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section area:datalog Reasoner, rules, reasoningDefaults labels Nov 19, 2026
@aaj3f
aaj3f requested a review from bplatz November 19, 2026 17:24
aaj3f and others added 2 commits November 19, 2026 12:43
A query that runs rules over a default graph of several ledgers is now
refused, so pin down the supported way to compare what rules derive with
another ledger: the rules run over the default graph alone, and the query
reads the other ledger as a named graph of its dataset (`fromNamed` /
`FROM NAMED`, `GRAPH`), both pinned with `@t:`, on both surfaces. The rule
docs show the pattern beside the refusal.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44QJuUPpGdwWfXE9Bz9

@bplatz bplatz 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.

Approving. Please review the inline comments before merging.

Optional: the body defers the rules-over-every-member question to #2075; marking it Follow-up: #2075 (docs/contributing/issue-linking.md) makes the deferral linkable.

QueryError::InvalidQuery(format!(
"this query's default graph merges several graphs, but datalog rules ({}) run over \
one graph: they would read only ledger '{}' and derive as if the other graphs' \
facts were absent. Run the rules over a default graph of one graph; a rule body can \

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.

This also fires when datalog comes from the first ledger's f:reasoningDefaults and the query never asked for reasoning, so every from: [this, other] query on a ledger with configured datalog and stored rules now fails, including ones that read no derived predicate. Where f:overrideControl denies overrides, the query can't opt out at all. Could the message name the way out ("reasoning": "none" where overrides are allowed, or reading the other ledgers through fromNamed, as the new docs example does), and the compatibility entry call out the configured-reasoning case?

if rule_set.is_empty() {
return Ok(());
}
if scope.multi_default_graph {

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.

multi_default_graph is false when the dataset has no default graph at all (fromNamed / FROM NAMED only). DataSetDb::primary() then falls back to the first named graph, so the rules run over that one graph as if it were the default graph, the same one-of-several answer this refuses for from: [a, b]. Should a rule set over an empty default graph be refused too, or does something else already stop it?

graph. `GRAPH ?g { … }` ranges over the ledger's named graphs.

A body naming anything else fails the query, naming the rule and the graph:
a graph the ledger does not have as of `t` (a typo'd IRI, say), another

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: "does not have as of t" is stronger than the check. The registry also holds graphs first written after t (visible_at only looks at drops), so a query at a t before the graph's first write accepts the body and reads the graph as empty, while the same rule at the head before the graph exists is refused. Either word it as "a graph the ledger has never had, or has dropped by t", or note the time-travel case. Same wording at line 348 and in compatibility.md.

let base = GraphDbRef::new(db_ref.snapshot, db_ref.g_id, overlay, db_ref.t);
let graph_sources = crate::r2rml_provider!(self);
let inputs = RuleInputScope {
multi_default_graph: super::union_default_dataset(db, &executable)

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: this re-derives the several-graphs test by hand, while the query paths take planning.multi_default_graph from PlanningContext::with_dataset_scope, whose docs make it the one definition. PlanningContext::current().with_dataset_scope(union.as_ref()).multi_default_graph keeps explain and query from disagreeing if that definition changes.

aaj3f added a commit that referenced this pull request Nov 20, 2026
… ledger in fromNamed

The dataset cases of the_tally_names_the_data_commit_the_rules_ran_over
ran datalog rules over `from: [data, other]`, a default graph of two
ledgers. #2463 refuses that shape (rules run over one graph), so the test
would fail whichever of the two lands second. The other ledger is now a
named graph, the shape both accept, and the query still goes through the
dataset path, so "for a dataset it names the primary ledger" is still what
it checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44QJuUPpGdwWfXE9Bz9

This branch has not been deployed

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

Labels

area:datalog Reasoner, rules, reasoningDefaults breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants