Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ versions may change the store format and the API; the release notes say how to u
### Fixed

- A spatial `FILTER` (`geof:sfWithin`, `geof:distance` and the other relations) over a geometry reached by a property path with an alternative, `?`, `*` or `+` (`?x (a|b/c)/geo:asWKT ?p`) gave every candidate geometry for every row, including those outside, repeated (#48). The parser joins such a path to the rest through a blank node, which the spatial rewrite didn't count as a link. Answers now match the evaluator's.
- The same for a GeoSPARQL relation written as a triple (`?g geo:sfWithin ?rg`) on a geometry reached by such a path (`?x (a|b)/geo:hasGeometry ?g`): every geometry for every row (#54).

## 0.1.0-rc.1 - 2026-11-17

Expand Down
78 changes: 75 additions & 3 deletions crates/tripletrail-spatial/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2523,9 +2523,12 @@ impl<'a> Rewriter<'a> {
}
out
}
G::Join { left, right } => G::Join {
left: b(self.walk(*left)),
right: b(self.walk(*right)),
G::Join { left, right } => match linked_through_paths(G::Join { left, right }) {
G::Join { left, right } => G::Join {
left: b(self.walk(*left)),
right: b(self.walk(*right)),
},
other => self.walk(other),
},
G::LeftJoin {
left,
Expand Down Expand Up @@ -2633,6 +2636,75 @@ impl<'a> Rewriter<'a> {
}
}

/// The join `p` with the blank nodes that link a property path to its siblings written as
/// variables (#48 follow-up). The parser joins `?x (a|b)/geo:hasGeometry ?g` as a path to a
/// blank node and a triple from it; a rewritten BGP projects its variables only, so a blank
/// node would no longer link the two. The variables' names start with `__tt_path_`; a
/// `SELECT` projects its own variables, so they are not returned.
fn linked_through_paths(p: GraphPattern) -> GraphPattern {
fn ends(p: &GraphPattern, out: &mut HashSet<String>) {
match p {
GraphPattern::Join { left, right } => {
ends(left, out);
ends(right, out);
}
GraphPattern::Path {
subject, object, ..
} => {
for t in [subject, object] {
if let TermPattern::BlankNode(b) = t {
out.insert(b.as_str().to_owned());
}
}
}
_ => {}
}
}
fn term(t: TermPattern, linked: &HashSet<String>) -> TermPattern {
match t {
TermPattern::BlankNode(b) if linked.contains(b.as_str()) => {
Variable::new_unchecked(format!("__tt_path_{}", b.as_str())).into()
}
other => other,
}
}
fn rename(p: GraphPattern, linked: &HashSet<String>) -> GraphPattern {
match p {
GraphPattern::Join { left, right } => GraphPattern::Join {
left: Box::new(rename(*left, linked)),
right: Box::new(rename(*right, linked)),
},
GraphPattern::Path {
subject,
path,
object,
} => GraphPattern::Path {
subject: term(subject, linked),
path,
object: term(object, linked),
},
GraphPattern::Bgp { patterns } => GraphPattern::Bgp {
patterns: patterns
.into_iter()
.map(|t| TriplePattern {
subject: term(t.subject, linked),
predicate: t.predicate,
object: term(t.object, linked),
})
.collect(),
},
other => other,
}
}
let mut linked = HashSet::new();
ends(&p, &mut linked);
if linked.is_empty() {
p
} else {
rename(p, &linked)
}
}

/// The services of TripleTrail's own that read sub-selects from the store (`st:`, window,
/// routing and path search).
pub const BUILT_IN_SERVICES: [&str; 4] = [
Expand Down
51 changes: 51 additions & 0 deletions crates/tripletrail/tests/spatial_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,54 @@ fn spatial_filters_keep_paths_joined_through_blank_nodes() {
["?x=<https://example.org/a>", "?x=<https://example.org/b>"]
);
}

/// The relation as a property (`?g geo:sfWithin ?rg`) after a path ending in a blank node:
/// the same rows as with the path's end written as a variable. (The evaluator alone infers
/// no relation triples, so the written-out form is the reference.)
#[test]
fn relation_triples_keep_paths_joined_through_blank_nodes() {
let e = Engine::new_tripletrail();
e.load(RdfFormat::Turtle, DATA.as_bytes()).unwrap();
let prefixes = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX ex: <https://example.org/>";
for (path, written) in [
(
"(ex:ref|ex:part)/geo:hasGeometry",
"(ex:ref|ex:part) ?m . ?m geo:hasGeometry",
),
("ex:ref?/geo:hasGeometry", "ex:ref? ?m . ?m geo:hasGeometry"),
(
"ex:part*/geo:hasGeometry",
"ex:part* ?m . ?m geo:hasGeometry",
),
(
"ex:part+/geo:hasGeometry",
"ex:part+ ?m . ?m geo:hasGeometry",
),
(
"(ex:part/ex:part?)?/geo:hasGeometry",
"(ex:part/ex:part?)? ?m . ?m geo:hasGeometry",
),
] {
for relation in ["geo:sfWithin", "geo:sfIntersects", "geo:ehInside"] {
let q = |p: &str| {
format!(
"{prefixes} SELECT ?x ?g WHERE {{ ex:region geo:hasGeometry ?rg . \
?x a ex:P ; {p} ?g . ?g {relation} ?rg }}"
)
};
let want = rows(&e, &q(written));
assert!(!want.is_empty(), "{written} {relation}");
assert_eq!(rows(&e, &q(path)), want, "{path} {relation}");
}
// The blank node written as a variable for the rewrite is not returned.
let star = format!(
"{prefixes} SELECT * WHERE {{ ex:region geo:hasGeometry ?rg . \
?x a ex:P ; {path} ?g . ?g geo:sfWithin ?rg }}"
);
assert!(
rows(&e, &star).iter().all(|r| !r.contains("__tt")),
"{path}"
);
}
}
Loading