Repository navigation
Description
On an indexed native ledger, a FILTER(?x = <iri>) inside a UNION branch can end up comparing against the IRI from an earlier branch's filter. The branch then drops the rows it should keep and keeps rows it should drop. There's no error, the answer is just wrong, and the same data answers correctly before fluree index.
Repro
mkdir -p /tmp/union-repro && cd /tmp/union-repro
cat > data.ttl <<'TTL'
@prefix ex: <http://example.org/> .
ex:w1 a ex:Widget ;
ex:partOf ex:box1 , ex:Widget .
TTL
fluree init
fluree create unindexed && fluree insert unindexed -f data.ttl
fluree create indexed && fluree insert indexed -f data.ttl && fluree index indexedPREFIX ex: <http://example.org/>
SELECT ?b ?x WHERE {
{ ex:w1 a ?x . FILTER(?x = ex:Widget) BIND(1 AS ?b) }
UNION { VALUES ?x { } }
UNION { ex:w1 ex:partOf ?x . FILTER(?x = ex:box1) BIND(3 AS ?b) }
}unindexed:main |
indexed:main |
|
|---|---|---|
| the query above | 1 ex:Widget, 3 ex:box1 |
1 ex:Widget, 3 ex:Widget |
Branch 3 returns ex:Widget, which its own FILTER(?x = ex:box1) should reject, and loses ex:box1. So it filtered on branch 1's IRI.
Some variations, against the same two ledgers:
| Change to the query | indexed:main |
|---|---|
middle branch is a triple that matches nothing ({ ex:w1 ex:missing ?x }) |
wrong, the same way |
| no middle branch (just branches 1 and 3) | right |
| branch 3 on its own | right |
branch 3 filters with sameTerm(?x, ex:box1) |
right |
branch 1 filters with ?x != ex:other |
right |
branch 3 is { ex:w1 ex:partOf ?x . MINUS { ex:w1 ex:partOf ?x . FILTER(?x = ex:box1) } BIND(3 AS ?b) } |
wrong the other way: 3 ex:box1, where it should be 3 ex:Widget |
unindexed:main answers every row right.
Reproduced on main at 7b9164b60 (a dev build of fluree-db-cli with --no-default-features --features iceberg), the same on every run. We first hit it on the 4.2.4 release CLI, where it's intermittent and depends on the data: with an earlier version of this repro (the same shape, longer IRIs), the query above came back wrong on one run and right on the next three against the same ledger, and on a fresh setup the "triple that matches nothing" variant was the one that came back wrong, while 4.2.4 answers the exact data above correctly on each of three fresh setups. That flakiness is a big part of why I think the cause is the one below.
Cause
I think it's the per-query constant→s_id memo in the ?var = <const> fast path. fluree-db-query/src/eval/compare.rs:202-203 keys the variable-free operand by its address:
let key =
crate::context::ConstSidKey::ExprPtr(other_expr as *const Expression as usize);and ConstSidKey::ExprPtr (fluree-db-query/src/context.rs:159-163) says why that's meant to be safe:
Safe as a key only because this memo lives exactly as long as the query (and the expression tree it points into): the pointer is stable across rows and the memo is dropped before any expression could be freed, so a stale/reused address can never be observed.
A correlated UNION breaks that assumption. UnionOperator::next_batch builds a fresh operator tree per branch per input row (fluree-db-query/src/union.rs:324-356, via build_where_operators_seeded), and the FILTER in each tree owns its own clone of the expression (fluree-db-query/src/execute/where_plan.rs:3081-3083, expr.clone()). Each branch tree is closed and dropped before the next one is built, while the memo (const_sid_cache, context.rs:458) lives on the ExecutionContext for the whole query. So branch 1's IRI gets resolved and cached under its clone's address, branch 1's tree is freed, and if branch 3's clone lands at the same address, it reads branch 1's s_id back. Whether the allocator reuses that address depends on what got allocated in between, which would explain why an empty middle branch matters while two branches don't, and why the release build (a different allocator) is intermittent.
It needs an indexed ledger because the fast path only runs when the bound side is a Binding::EncodedSid (compare.rs:187-188). Novelty-only bindings take the ordinary comparison.
By reading only: anything else that builds and drops a subplan per input row mid-query (OPTIONAL's builder, MINUS, EXISTS, GRAPH's correlated inner plan) is exposed in the same way, and the MINUS row above suggests at least MINUS is.
Expected
Each branch filters on its own IRI, the way the unindexed ledger answers: 1 ex:Widget, 3 ex:box1.
The two value keys a few lines further down (ConstSidKey::Iri / ConstSidKey::Sid, compare.rs:266) can't alias, so minimally we could drop ExprPtr and key the variable-free operand by its evaluated IRI or Sid. That costs one eval_to_comparable per row, but still saves the per-row dictionary lookup, which I think was the dominant cost #1296 was after. Or, to keep the pointer key, the memo could move onto the FilterOperator's RowPredicate so it dies with the expression it points into. Either way, the query above against an indexed fixture would make a cheap regression test, though since it hinges on address reuse it may pass on some builds regardless, so a unit test that two different constants can't share a memo entry would probably be the more reliable guard.
Activity