Skip to content

fix(spatial): infer relation triples inside EXISTS - #57

Merged
MathiasVDA merged 2 commits into
mainfrom
claude/project-thread-n3oul3
Nov 19, 2026
Merged

MathiasVDA merged 2 commits into
mainfrom
claude/project-thread-n3oul3

Conversation

@MathiasVDA

@MathiasVDA MathiasVDA commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #56.

Cause

The spatial rewrite walked graph patterns, but not the patterns inside expressions. A relation triple inside FILTER EXISTS { ?x geo:hasGeometry ?g . ?g geo:sfWithin ?rg } therefore matched only asserted geo:sfWithin triples. With none in the data, EXISTS kept no rows (0 instead of 648 on the review probe) and NOT EXISTS kept every row.

Fix

Rewriter::walk_exists rewrites the pattern of each EXISTS in a FILTER, a BIND and an OPTIONAL's condition, the same way as any other pattern.

Tests

tests/spatial_paths.rs gains relation_triples_inside_exists_are_inferred. It runs 4 bodies (plain, sequence path, alternative path, feature-to-feature) through each of FILTER EXISTS, FILTER NOT EXISTS and BIND(EXISTS …) FILTER(?in). It compares the results with the join written out, and the complement for NOT EXISTS. The test fails without the fix.

The spatial crate's tests and the tripletrail spatial test files pass. fmt and clippy are clean.

Docs

  • docs/spatial.md: relation triples work inside EXISTS.
  • CHANGELOG (Fixed).

The spatial rewrite walked graph patterns but not the patterns inside
expressions, so `FILTER EXISTS { ?g geo:sfWithin ?rg }` saw only asserted
relation triples: EXISTS kept no row and NOT EXISTS every row. The
patterns of EXISTS in FILTER, BIND and OPTIONAL's condition are now
rewritten as any other.

Closes #56
Inside EXISTS the rewritten relation was planned as at top level: its
geometry lookups and index candidates ran for the whole store once per
outer row (about 4.5 ms a row; 25 x 6,000 rows timed out). Now, inside
EXISTS, the relation is joined LATERAL after the rest of its group, its
filters are not index-planned, and a feature's default geometry is
tested with a lookup, so each row's values are bound.
@MathiasVDA
MathiasVDA merged commit 1430024 into main Nov 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GeoSPARQL relation triples inside FILTER (NOT) EXISTS match nothing

1 participant