diff --git a/CHANGELOG.md b/CHANGELOG.md index c610b35..051f0fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,10 @@ versions may change the store format and the API; the release notes say how to u - Web UI: every page uses the full width of the window (the Query, Map, Shapes and admin pages stopped at 1,400 pixels), and the fields outside the top bar (search, number and date boxes, text areas) take the page's colours instead of the browser's grey, most visible in the dark theme. - `ORDER BY DESC(tt:cosine(?v, "[…]")) LIMIT k`, or `ORDER BY DESC(?score)` with `?score` bound to that cosine, is answered from the copy of the vectors that `tt:nearest` reads (#236): the query is evaluated again with `?v` kept to the vectors most similar, each one compared, so the answer stays exact and other patterns and filters still apply. It works on the union default graph and for a caller limited to some graphs. Among 100,000 vectors of 768 numbers: 40 ms instead of 6.2 s; at most two rounds, so a filter that keeps one row in a hundred costs about 130 ms instead of 93 ms. A query with its own dataset or under row-level rules, with an `OFFSET` and `LIMIT` past a sixteenth of the vectors, or over a property that also has plain strings in the form of a vector, is evaluated as before. See [vector search](docs/vector-search.md#functions). +### 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. + ## 0.1.0-rc.1 - 2026-11-17 The first release candidate under the name TripleTrail, for trying it in place of Fuseki and Oxigraph. diff --git a/crates/tripletrail-spatial/src/cost.rs b/crates/tripletrail-spatial/src/cost.rs index cc6afbb..3485f3a 100644 --- a/crates/tripletrail-spatial/src/cost.rs +++ b/crates/tripletrail-spatial/src/cost.rs @@ -71,10 +71,17 @@ pub(crate) fn triple_names(t: &TriplePattern) -> HashSet { } /// The names a join operand shares with its siblings: the variables and blank nodes of a -/// basic graph pattern, the in-scope variables of anything else. +/// basic graph pattern or a property path (the parser joins `?x (a|b)/c ?y` as a path and a +/// triple through a blank node, #48), the in-scope variables of anything else. pub(crate) fn names(p: &GraphPattern) -> HashSet { match p { GraphPattern::Bgp { patterns } => patterns.iter().flat_map(triple_names).collect(), + GraphPattern::Path { + subject, object, .. + } => [term_name(subject), term_name(object)] + .into_iter() + .flatten() + .collect(), other => { let mut out = HashSet::new(); other.on_in_scope_variable(|v| { diff --git a/crates/tripletrail/tests/spatial_paths.rs b/crates/tripletrail/tests/spatial_paths.rs new file mode 100644 index 0000000..d1664eb --- /dev/null +++ b/crates/tripletrail/tests/spatial_paths.rs @@ -0,0 +1,88 @@ +//! Spatial filters over geometries reached by property paths (#48): the parser joins +//! `?x (a|b/c)/geo:asWKT ?p` as a path and a triple through a blank node, and the spatial +//! rewrite must keep the two together. + +use tripletrail::Engine; +use tripletrail::oxigraph::io::RdfFormat; +use tripletrail::oxigraph::sparql::QueryResults; + +const DATA: &str = r#" +@prefix geo: . +@prefix ex: . +ex:region geo:hasGeometry ex:rg . ex:rg geo:asWKT "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))"^^geo:wktLiteral . +ex:a a ex:P ; geo:hasGeometry ex:ga . ex:ga geo:asWKT "POINT(1 1)"^^geo:wktLiteral . +ex:b a ex:P ; ex:ref ex:rb . ex:rb geo:hasGeometry ex:gb . ex:gb geo:asWKT "POINT(2 2)"^^geo:wktLiteral . +ex:c a ex:P ; ex:ref ex:rc . ex:rc geo:hasGeometry ex:gc . ex:gc geo:asWKT "POINT(20 20)"^^geo:wktLiteral . +ex:d a ex:P ; ex:part ex:d1 . ex:d1 ex:part ex:d2 . ex:d2 geo:hasGeometry ex:gd . ex:gd geo:asWKT "POINT(3 3)"^^geo:wktLiteral . +ex:e a ex:P ; ex:part ex:e1 . ex:e1 geo:hasGeometry ex:ge . ex:ge geo:asWKT "POINT(30 3)"^^geo:wktLiteral . +"#; + +fn rows(e: &Engine, q: &str) -> Vec { + let QueryResults::Solutions(s) = e.query(q).unwrap() else { + panic!() + }; + let mut out: Vec = s + .map(|r| { + r.unwrap() + .iter() + .map(|(v, t)| format!("{v}={t}")) + .collect::>() + .join(" ") + }) + .collect(); + out.sort(); + out +} + +#[test] +fn spatial_filters_keep_paths_joined_through_blank_nodes() { + let tt = Engine::new_tripletrail(); + tt.load(RdfFormat::Turtle, DATA.as_bytes()).unwrap(); + let ox = Engine::new_oxigraph().unwrap(); + ox.load(RdfFormat::Turtle, DATA.as_bytes()).unwrap(); + let prefixes = "PREFIX geof: + PREFIX geo: + PREFIX ex: + PREFIX uom: "; + let paths = [ + "(geo:hasGeometry|ex:ref/geo:hasGeometry)/geo:asWKT", + "ex:ref?/geo:hasGeometry/geo:asWKT", + "ex:part*/geo:hasGeometry/geo:asWKT", + "ex:part+/geo:hasGeometry/geo:asWKT", + "(ex:ref|ex:part)/geo:hasGeometry/geo:asWKT", + "ex:ref/geo:hasGeometry/geo:asWKT", + "geo:hasGeometry/geo:asWKT", + ]; + let tests = [ + "FILTER(geof:sfWithin(?p, ?w))", + "FILTER(geof:sfIntersects(?w, ?p))", + "FILTER(!geof:sfDisjoint(?p, ?w))", + "FILTER(geof:sfWithin(?p, ?w) && ?x != ex:a)", + "FILTER(geof:distance(?p, ?w, uom:metre) < 1)", + ]; + for path in paths { + for test in tests { + let q = format!( + "{prefixes} SELECT ?x WHERE {{ ex:region geo:hasGeometry/geo:asWKT ?w . \ + ?x a ex:P ; {path} ?p {test} }}" + ); + assert_eq!(rows(&tt, &q), rows(&ox, &q), "{path} {test}"); + } + // The relation the other way round: the region found from the point's geometry. + let q = format!( + "{prefixes} SELECT ?x ?r WHERE {{ ?x a ex:P ; {path} ?p . \ + ?r geo:hasGeometry/geo:asWKT ?w FILTER(geof:sfContains(?w, ?p) && ?r != ?x) }}" + ); + assert_eq!(rows(&tt, &q), rows(&ox, &q), "{path} contains"); + } + // The issue's query: a and b, once each. + let q = format!( + "{prefixes} SELECT ?x WHERE {{ ex:region geo:hasGeometry/geo:asWKT ?w . \ + ?x a ex:P ; {} ?p FILTER(geof:sfWithin(?p, ?w)) }}", + paths[0] + ); + assert_eq!( + rows(&tt, &q), + ["?x=", "?x="] + ); +}