Repository navigation
Description
Version
- Fluree DB v4.2.5 (
fluree --version→fluree 4.2.5), release binary for macOS on Apple silicon - macOS 26.6, in-memory server:
fluree server run --memory
Summary
Inside one group, a FILTER NOT EXISTS { … } over the default graph stops filtering as soon as a GRAPH <g> { … } block appears earlier in the group. The same query with the GRAPH block written last gives the right answer. The JSON-LD form (["graph", …] before ["not-exists", …]) behaves the same way. No error is raised.
Steps
A standalone script that reproduces all of this is at the end.
-
Create a ledger and insert (TriG):
PREFIX ex: <http://example.org/> ex:alice a ex:Person ; ex:knows ex:bob . ex:bob a ex:Person . ex:carol a ex:Person . GRAPH ex:g { ex:note ex:label "anything" . } -
Q1,
GRAPHblock last:PREFIX ex: <http://example.org/> SELECT ?p WHERE { ?p a ex:Person . FILTER(?p != ex:alice) FILTER NOT EXISTS { ex:alice ex:knows ?p } GRAPH ex:g { ?n ex:label ?l } }
-
Q2, the same query with the
GRAPHblock first:PREFIX ex: <http://example.org/> SELECT ?p WHERE { GRAPH ex:g { ?n ex:label ?l } ?p a ex:Person . FILTER(?p != ex:alice) FILTER NOT EXISTS { ex:alice ex:knows ?p } }
-
Q3, JSON-LD with the graph pattern first:
{ "@context": {"ex": "http://example.org/"}, "select": ["?p"], "where": [ ["graph", "ex:g", {"@id": "?n", "ex:label": "?l"}], {"@id": "?p", "@type": "ex:Person"}, ["filter", "(!= ?p ex:alice)"], ["not-exists", {"@id": "ex:alice", "ex:knows": {"@id": "?p"}}] ] }
Expected
All three return ex:carol only. A filter applies to its whole group wherever it is written, and the pattern inside NOT EXISTS sits outside the GRAPH block, so it is matched against the default graph, where ex:alice ex:knows ex:bob is stored.
Observed
| Query | Result |
|---|---|
| Q1 (GRAPH last) | ex:carol |
| Q2 (GRAPH first) | ex:bob, ex:carol |
| Q3 (JSON-LD, graph first) | ex:bob, ex:carol |
A positive pattern in the same position (ex:alice ex:knows ?p without NOT EXISTS) matches correctly, so only the negation is affected. It looks as if the negated pattern is evaluated against the named graph that the earlier block selected.
Why it matters
The answer contains rows the query was written to exclude, silently. It also reaches Datalog rules: a rule body that reads a named graph and then negates concludes facts it should not.
Workaround
Write the GRAPH block after the negation.
Existing reports
None found (searched open and closed issues and pull requests). Different problems in the same area: #1317 (closed, GRAPH <g> leaking the default graph after a WITH <g> DELETE WHERE), #2289 (open, policy conditions reading the default graph instead of the judged fact's graph), #2075 (open, readers that still read one graph in a scope of several). The v5.0.0-alpha.2 release notes do not mention it.
GRAPH <g>leaks the default graph afterWITH <g> DELETE WHEREon a not-yet-existing named graph followed by an upsert #1317- f:query policy conditions may read the default graph instead of the judged fact's graph #2289
- Scopes of several graphs: a one-graph witness type, and the readers that still read one graph #2075
Reproduction script
Self-contained: starts a throwaway in-memory server on a loopback port, loads the data, runs the queries, stops the server
#!/usr/bin/env bash
# Usage: FLUREE=/path/to/fluree bash repro.sh
# Needs the Fluree CLI (tested with v4.2.5) on PATH or in $FLUREE, and curl.
# Starts a throwaway in-memory server on a loopback port and stops it on exit.
set -euo pipefail
FLUREE="${FLUREE:-fluree}"
PORT="${PORT:-58090}"
API="http://127.0.0.1:$PORT/v1/fluree"
"$FLUREE" --version
"$FLUREE" server run --memory --listen-addr "127.0.0.1:$PORT" --log-level error >/dev/null 2>&1 &
SERVER=$!
trap 'kill $SERVER 2>/dev/null || true' EXIT
until curl -sf "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; do sleep 0.1; done
# post <path> <body> [content-type] [accept]
post() {
curl -s -X POST "$API/$1" -H "Content-Type: ${3:-application/json}" -H "Accept: ${4:-application/json}" --data-binary "$2"
echo
}
sparql() { post "query/$1" "$2" application/sparql-query application/sparql-results+json; }
# FILTER NOT EXISTS is not applied when a GRAPH block is written before it.
post create '{"ledger":"repro"}' >/dev/null
# (SPARQL-style PREFIX, because curl reads a body that starts with "@" as a file name.)
post insert/repro:main 'PREFIX ex: <http://example.org/>
ex:alice a ex:Person ; ex:knows ex:bob .
ex:bob a ex:Person .
ex:carol a ex:Person .
GRAPH ex:g { ex:note ex:label "anything" . }' application/trig >/dev/null
echo "--- Q1: GRAPH block written last. Expected and observed: carol only."
sparql repro:main 'PREFIX ex: <http://example.org/>
SELECT ?p WHERE {
?p a ex:Person .
FILTER(?p != ex:alice)
FILTER NOT EXISTS { ex:alice ex:knows ?p }
GRAPH ex:g { ?n ex:label ?l }
}'
echo "--- Q2: the same query with the GRAPH block written first. Expected: carol only."
sparql repro:main 'PREFIX ex: <http://example.org/>
SELECT ?p WHERE {
GRAPH ex:g { ?n ex:label ?l }
?p a ex:Person .
FILTER(?p != ex:alice)
FILTER NOT EXISTS { ex:alice ex:knows ?p }
}'
echo "--- Q3: the same in JSON-LD, graph pattern first. Expected: carol only."
post query/repro:main '{
"@context": {"ex": "http://example.org/"},
"select": ["?p"],
"where": [
["graph", "ex:g", {"@id": "?n", "ex:label": "?l"}],
{"@id": "?p", "@type": "ex:Person"},
["filter", "(!= ?p ex:alice)"],
["not-exists", {"@id": "ex:alice", "ex:knows": {"@id": "?p"}}]
]
}'
Activity