grafeo icon indicating copy to clipboard operation
grafeo copied to clipboard

SPARQL `path+` (OneOrMore) returns no transitive closure — one missing endpoint projection

Open teipsum opened this issue 5 months ago • 0 comments

Summary

The SPARQL one-or-more property path operator (path+, OneOrMore) does not return a transitive closure. For a variable endpoint it returns only the direct (1-hop) neighbours, replicated once per depth branch, with the true multi-hop endpoints replaced by the first intermediate hop and with duplicate rows that DISTINCT fails to collapse. The five sibling property-path operators — sequence /, alternative |, inverse ^, zero-or-more *, and zero-or-one ? — are all correct; only + is affected.

Root cause is a single missing call: OneOrMore does not project each fixed-depth branch to its endpoint columns the way ZeroOrMore and ZeroOrOne do, so the union branches reach Distinct with heterogeneous schemas.

Environment

  • grafeo main (reproduced at commit 4ebae02f)
  • Also present in the published 0.5.42 crate/wheel
  • Features: sparql, triple-store (the rdf spec-test gate)

Reproduction

Graph: a -> b -> c -> d -> e plus a branch c -> x, all over predicate p.

INSERT DATA {
  <http://ex.org/a> <http://ex.org/p> <http://ex.org/b> .
  <http://ex.org/b> <http://ex.org/p> <http://ex.org/c> .
  <http://ex.org/c> <http://ex.org/p> <http://ex.org/d> .
  <http://ex.org/d> <http://ex.org/p> <http://ex.org/e> .
  <http://ex.org/c> <http://ex.org/p> <http://ex.org/x> .
}

Open-ended closure

SELECT ?s ?o WHERE { ?s <http://ex.org/p>+ ?o }
  • Expected (13 distinct pairs — the full transitive closure): (a,b) (a,c) (a,d) (a,e) (a,x) (b,c) (b,d) (b,e) (b,x) (c,d) (c,e) (c,x) (d,e)
  • Actual: 13 rows but only the 5 direct edges, each repeated once per depth branch — (a,b)×5 (b,c)×4 (c,d)×2 (d,e)×1 (c,x)×1 — and every transitive pair (a,c) (a,d) (a,e) (a,x) (b,d) (b,e) (b,x) (c,e) is missing. SELECT DISTINCT ?s ?o returns 5 rows instead of 13.

Bound subject

SELECT ?o WHERE { <http://ex.org/a> <http://ex.org/p>+ ?o }
  • Expected: b, c, d, e, x
  • Actual: 5 rows, all b (the direct neighbour, replicated).

Cycle (termination is fine)

# a <-> b
SELECT ?s ?o WHERE { ?s <http://ex.org/p>+ ?o }
  • Expected (4 distinct pairs): (a,a) (a,b) (b,a) (b,b)
  • Actual: 100 rows — (a,b)×50 and (b,a)×50. The query terminates (the expansion is bounded; see the depth caveat below), it just leaks duplicates and drops the (a,a) / (b,b) cyclic returns.

Root cause

In crates/grafeo-engine/src/query/translators/sparql.rs, translate_one_or_more_path builds one branch per depth 1..=MAX_DEPTH and pushes each branch raw into the Union:

for depth in 1..=MAX_DEPTH {
    let branch = self.translate_fixed_depth_path(inner_path, &subject, &object, &graph, depth)?;
    branches.push(branch);          // <-- no endpoint projection
}

translate_zero_or_more_path and translate_zero_or_one_path instead wrap every depth branch in self.project_path_endpoints(&subject, &object, branch) before pushing it. That projection strips the per-hop intermediate columns so all branches share the endpoint schema.

Without it, the depth-N branch is wider than the depth-1 branch (it carries a column per intermediate hop). The union output schema is seeded from branch 0 (the narrow depth-1 branch), and the Distinct operator keys on each chunk's own width while copying into the narrow branch-0-sized builder. The result: the visible object column receives the first intermediate hop rather than the true endpoint, and rows that reach the same endpoint at different depths keep different (wider) keys, so they are never de-duplicated. ZeroOrMore and ZeroOrOne escape this purely because they apply the endpoint projection.

Honest scope / caveats

These are deliberately not claimed to be fixed here:

  1. Bounded depth, not unbounded ALP. path+ (like path*) expands to a bounded MAX_DEPTH = 50 hops plus Distinct; there is no visited-set, so this is not the unbounded ALP of SPARQL 1.1 §18.4. Closures longer than 50 hops are truncated. This bound is shared with path* and is orthogonal to the endpoint-projection defect described here — fixing the projection does not make + spec-complete-ALP conformant.

  2. Fully-bound SELECT ?s form. A query that projects a variable absent from a fully-bound path pattern, e.g. SELECT ?s WHERE { <http://ex.org/a> <http://ex.org/p>+ <http://ex.org/b> }, raises GRAFEO-X001: Internal error: Variable 's' not found in input columns. This is identical for :p* and :p?, i.e. shared with the already-correct operators, so it is a separate normalization item and is not addressed here. ASK and SELECT * over fully-bound paths already work.

Suggested fix

Apply the same endpoint projection the sibling operators use — a one-line change at the single call site, reusing the existing project_path_endpoints helper. No reflexive 0-hop branch is added, because path+ excludes the zero-length path (SPARQL 1.1 §9.1). Happy to open a PR (with result-level regression oracles over the graph above) — see the linked PR.

teipsum avatar Jun 11 '26 22:06 teipsum