Skip to content

FILTER NOT EXISTS is not applied when a GRAPH block is written before it #2328

Closed

Description

@rnwesley

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.

  1. 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" . }
    
  2. Q1, GRAPH block 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 }
    }
  3. Q2, the same query with the GRAPH block 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 }
    }
  4. 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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions