Skip to content

Datalog: run rule bodies under the query's rdfs/owl2ql rewrites #2139

Open

Description

@bplatz

Behaviour

Rule bodies run with reasoning off. rdfs and owl2ql are query rewrites, not materialized facts, so a rule body never sees the subclass, subproperty, inverse or domain/range entailments those modes give the outer query. OWL 2 RL entailments do reach rule bodies, because they are facts in the derived overlay.

For a rule that negates or counts, that difference turns a missing entailment into a contradiction. With ex:Manager rdfs:subClassOf ex:Employee and "reasoning": ["rdfs", "datalog"], a rule deriving ex:Contractor for anyone with NOT EXISTS { ?x a ex:Employee } concluded that a stored manager was a contractor, while the same response returned her as an ex:Employee. A rule counting employees disagreed with the query's own COUNT. The outer query also widens rule conclusions, so a manager derived by a rule met the same contradiction.

#2122 refuses such a program: under rdfs or owl2ql without owl2rl, a program with a rule that negates or aggregates fails with a 400 naming the rule (refuse_non_monotone_rules_under_rewrites in fluree-db-query/src/execute/reasoning_prep.rs). With owl2rl on, those entailments are materialized and the rewrite is skipped (rewrite_glue.rs), so rules and query read the same facts.

Positive programs still run under rdfs or owl2ql alone, but derive less than the query's patterns match: a rule over ?x a ex:Employee does not fire for a stored ex:Manager.

Proposal

Run rule bodies under the query's rdfs/owl2ql rewrites, so a rule reads what the query reads, and lift the refusal.

What it takes

  • Footprints have to follow the hierarchy. Rules are ordered and checked for cycles by the predicates and classes they read and write, as written. Under rdfs, a rule negating ?x a ex:Employee also reads ex:Manager facts, so a rule deriving ex:Manager has to finish first. Without expanding reads through subclass and subproperty links, and through owl2ql's inverse and domain/range rewrites, the stratification is wrong and the contradiction comes back in a different place. Cycle detection (-not->, -agg->) needs the same expansion.
  • Which hierarchy. The rewrite reads the schema hierarchy from the stored data. A rule that derives rdfs:subClassOf or rdfs:subPropertyOf facts would not widen anything; that should be stated, or such programs ordered or refused.
  • Semi-naive seeding. A rewritten pattern becomes a UNION, and only top-level patterns are seeded (datalog_rules/delta.rs), so a recursive rule that reads a rewritten pattern would re-run its whole body every pass. Either seed through the rewrite or measure and accept the cost.
  • Caching. The reasoning cache key leaves out rdfs and owl2ql today. Once they change what rules derive, the key has to include them.
  • Behavior change. Positive programs under rdfs/owl2ql alone would derive more than they do now. That needs a line in the PR body and the docs.

Acceptance

  • The refusal goes, and the tests that pin it invert into agreement checks against a plain query: it_datalog_negation (negation_under_rdfs_or_owl2ql_without_owl2rl_is_refused, sparql_negation_under_rdfs_without_owl2rl_is_refused, negation_under_a_ledger_default_rdfs_is_refused) and it_datalog_aggregates::a_count_under_rdfs_without_owl2rl_is_refused.
  • A rule deriving members of a subclass, read by a negating or counting rule through the superclass, runs first, with that rule listed last and first.
  • docs/query/datalog-rules.md ("rdfs and owl2ql need owl2rl") and docs/query/reasoning.md ("Combined modes") describe the new behavior.

Follow-up from #2122 (review comment).

Activity

  1. added
    enhancement
    P3
    area:datalog
    on Oct 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

    P3area:datalogenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions