Skip to content

fix(query): vector identity follow-up: one-round cold builds, the whole-graph distinct count, cancellation, EXISTS - #2468

Merged
bplatz merged 12 commits into
mainfrom
fix/vector-identity-followup
Nov 21, 2026
Merged

bplatz merged 12 commits into
mainfrom
fix/vector-identity-followup

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #2201. An independent re-review of that PR turned up a handful of things late in its life. The per-query shard pins, the build-once table, the in-place compare and the clippy list went into #2201 itself before it merged. This picks up the rest, starting with two regressions that merge brought to main.

Regressions from #2201

  • A cold identity-table build fetched its arena one shard per round on a residency host (f777ab5e4). On a host that serves reads from a resident tier (the browser build), a read of a shard that isn't resident records the miss and fails, and the caller fetches what was recorded and retries. Building a vector arena's identity table reads every shard, so a cold build failed once per shard. The rounds are capped at 256, so a COUNT(DISTINCT ?v) or group count over an arena of more shards than that failed on that host, where before fix(query): compare vector literals by value, not by arena handle #2201 it answered (by handle). The build now records every shard the resident tier lacks before it starts, as the routed scan cursors do for their remaining leaves, so one retry round fetches the whole arena. No answer changes. a_cold_identity_table_build_fetches_its_arena_in_one_round counts the rounds over eight stored-but-not-resident shards: one now, eight without the change.
  • The whole-graph COUNT(DISTINCT ?o) declined any graph holding a vector (42f77564a). Its directory walk keys objects by (o_type, o_key), which isn't a value for a vector, so fix(query): compare vector literals by value, not by arena handle #2201 sent such a graph to the general pipeline, which scans and keys every triple. It now leaves the vectors out of the walk, as it already did big numbers, and counts them exactly from the arenas' identity tables. Within an arena, the distinct canonical handles of its live handles are its values. Across predicates, only a hash two arenas share needs a component compare. Liveness comes from POST directories, as for big numbers (an arena keeps the handles of retracted facts), and the four helpers that prove it now take the arena's o_type rather than assuming NUM_BIG_OVERFLOW. distinct_object_count_counts_vectors_by_value counts a vector repeated under one predicate and one under two once each, before and after retractions (a rebuild, then an incremental build that leaves a predicate with no live vector), and the fast lane, the general pipeline and the hand count agree. Ignoring liveness answers 4 and 3 for 3 and 2. Skipping the cross-arena compare answers 15 for 14 on the vector fixture. Putting the decline back, the lane never serves.

The rest

  • The identity-table build stops when its query is cancelled (ede710175). It reads the whole arena inside an operator's open, and nothing stopped it. It now looks at the query's cancellation every 1,024 vectors and before each run of repeated hashes, and stops with the cancellation as the error, which QueryError::from_io now returns as Cancelled rather than an internal I/O error. The table's once-only cell stays empty, so the next caller builds it. It still charges no fuel, as decided in fix(query): compare vector literals by value, not by arena handle #2201.
  • The EXISTS semijoin checks the query's key-read slot itself (45457a382). It keys the inner solutions into a hash set and probes it per row, and a failed vector read inside either counted two equal keys apart: EXISTS dropped a row and NOT EXISTS kept one. Only the runner's and the WHERE cursor's checks caught it, after the batch. It now checks after each build batch and each probe batch, as DISTINCT, GROUP BY and the hash join do, and the failed-read test runs FILTER EXISTS and FILTER NOT EXISTS over two arenas too. With the runner's and cursor's checks removed, those answered [] and [f, g] with no error before this, and fail with one after it.
  • A decoded vector hashes its bits in one pass (b6fccade8). FlakeValue's Hash for a vector went through vector_value_hash, which checks every component narrows to f32, narrows them, then hashes. That's right for keys that meet an arena slice, but MINUS, the union's fact dedup and COUNT(DISTINCT *) hash decoded values only against each other, a row at a time. It's now one xxh3 pass over the f64 bits, which its bitwise equality agrees with (and which stays strong on widened f32 components, where FxHasher alone is weak).
  • A bench for keying vectors (3b05a66f4). Nothing benched keying a stored vector, which is how keying one by its Debug text reached main for a while unnoticed. query_hot_vector_keying times a counted DISTINCT, GROUP BY and cross-predicate join on ?v, and a MINUS, on an indexed ledger with one distinct 768-dimension vector per subject, before and after the identity tables are built, plus a per-predicate COUNT(DISTINCT) and, after the tables, the whole-graph COUNT(DISTINCT ?o). It has a regression-budget.json entry like the other query-hot benches. On the box below, its MINUS cell is 18.3 ms on main and 11.4 ms here, and the other cells are within run-to-run noise of main (three interleaved runs each).
  • Two stale doc comments (a2e20b307): the vector equality's Arc::ptr_eq note described a memo that's gone, and o_key_is_predicate_identifying now points at objects_keyed_by_value for what an arena actually holds.

Numbers

instructions:u, median of three runs of three iterations each, on a quiet c7i.8xlarge: an indexed ledger of 100K subjects, each with one distinct vector and one decimal, where every tenth also holds a copy of its vector under a second predicate (so the count compares 10K vectors across arenas). "Before #2201" is b1977e0fe, main just before that merge. Every answer matched across the three builds.

100K × 768 100K × 1536
whole-graph COUNT(DISTINCT ?o) before #2201 121M 122M
main 1.20G 1.48G
this PR 236M (0.20x of main) 282M (0.19x of main)
first run on a fresh index 2.46G → 2.55G (1.04x) 3.92G → 6.26G (1.60x)
MINUS on ?v across two predicates before #2201 976M 1.43G
main 2.35G 4.18G
this PR 1.27G (0.54x of main) 2.01G (0.48x of main)

Wall clock for the count is 190 ms on main and 21 ms here at 768 (230 ms and 29 ms at 1536). Before #2201 it was 3 ms, but it counted by handle there, which is wrong once a vector repeats. In instructions the count is 2–2.3x that older lane, which is the liveness proof and the cross-arena compare.

The first run on a fresh index builds the identity tables. That's the same build the per-predicate COUNT(DISTINCT ?v) already pays on main for its first run (3.39G at 1536), and the two share the tables, so whichever runs first pays for both. With the tables already built, the count's first run is 2.54G against main's 3.92G at 1536. Peak RSS is a little lower than main (391 MB against 424 MB at 768).

Every other cell is within 1% of main with the tables unbuilt: counted DISTINCT, GROUP BY, a join and FILTER EXISTS on ?v, the per-predicate COUNT(DISTINCT ?v), decimal and subject DISTINCT, and a dotProduct top-10. With the tables built, the four ?v cells come to 0.37x–0.72x of main, as they did in #2201.

One thing I left as it is: MINUS is still 1.30x (768) and 1.40x (1536) of before #2201. The hash isn't the cost any more: in a profile, xxh3 is 10% of the run, where FxHasher was 28% before #2201. What grew instead is decoding each vector to key it (the f32 widening and shard parsing), which MINUS, the union's fact dedup and COUNT(DISTINCT *) still do, rather than use the value key the hashed operators use. Moving them over means retyping their own Binding-based keys, which felt like more than this follow-up should take on, but happy to fold it in here if we want that last 30–40% back.

Follow-up: #2483 (moving MINUS, the union's fact dedup and COUNT(DISTINCT *) onto the value key).

Gates

All on one c7i.8xlarge, at this branch's tree: cargo fmt (workspace and testsuite-sparql); clippy --all --all-features --all-targets -D warnings; cargo check for the workspace and for the server's native-only build; the wasm32 clippy lanes and the Node wasm probe; workspace nextest --all-features (16,569 passed, 46 skipped, including the residency-gated test); doctests; testsuite-sparql clippy and tests; the harness coverage test; and the new bench under --test. The server-only cargo check prints one dead-code warning, in fluree-db-server/src/extract/credential.rs, which this doesn't touch.

…na in one round

On a host that serves reads from a resident tier (a browser), a read of a
shard that isn't resident records the miss and fails, and the caller
fetches what was recorded and retries. Building a vector arena's identity
table reads every shard, so a cold build failed once per shard, one retry
round each. The rounds are capped at 256, so a COUNT(DISTINCT ?v) or
group count over an arena of more shards than that failed, where it had
answered (by handle) before the identity tables existed.

The build now records every shard the resident tier lacks before it
starts, as the routed scan cursors record their remaining leaves, so one
retry round fetches the whole arena. No answer changes.

`a_cold_identity_table_build_fetches_its_arena_in_one_round` builds a
table over eight shards that are stored but not resident and counts the
retry rounds: one with the change, eight without it.
… holds vectors

The whole-graph COUNT(DISTINCT ?o) walks OPST leaflet directories, and a
vector's key is a handle, not a value, so it declined on any graph holding
a vector: the general pipeline then scanned and keyed every triple. It now
counts the vectors the walk leaves out from their arenas' identity tables,
as it already counts big numbers from theirs. Within an arena the distinct
canonical handles of the live handles are its values; across predicates
only a hash two arenas share needs a component compare. Liveness is proved
from POST directories, as for big numbers, and the four helpers that prove
it now take the arena's o_type rather than assuming NUM_BIG_OVERFLOW.

The count builds each vector predicate's identity table on first use, once
per store, as the per-predicate count already does.

`distinct_object_count_counts_vectors_by_value` counts a vector repeated
under one predicate and one shared by two once each, before and after
retractions (a rebuild, then an incremental build that leaves a predicate
with no live vector). Ignoring liveness answers 4 and 3 for 3 and 2;
skipping the cross-arena compare answers 15 for 14 on the vector fixture;
with the decline put back, the lane never serves the vector graph.
… is cancelled

Building a vector arena's identity table reads the whole arena, inside an
operator's open, and nothing stopped it: a query cancelled or timed out
during the first value-keyed count after an index publish read on until
the arena ended. The build now looks at the query's cancellation every
1,024 vectors and before each run of repeated hashes, and stops with the
cancellation's reason, which `QueryError::from_io` returns as `Cancelled`
rather than an I/O failure. The table's once-only cell is left empty, so
the next caller builds it. The build still charges no fuel.

Tests: a cancelled build stops and leaves no table (with the check
removed it builds anyway); a cancelled read stays a cancellation (without
the new arm it is an internal error).
…iled to read

The semijoin keys the inner solutions into a hash set and probes it per
row, and a failed vector read inside either counted two equal keys apart:
EXISTS dropped a row and NOT EXISTS kept one. Only the runner and the WHERE
cursor caught it, after the batch. It now checks the query's key-read slot
after each build batch and after each probe batch, before any row is
decided, as DISTINCT, GROUP BY and the hash join do.

The failed-read test now runs EXISTS and NOT EXISTS over two arenas too.
With the runner and cursor checks removed, they answered [] and [f, g]
without an error before this change, and fail with one after it.
The vector equality's `Arc::ptr_eq` shortcut described a decode memo that
no longer exists; it is a cheap identity check before the component
compare. `o_key_is_predicate_identifying` now says it is a property of the
o_type, not of a given arena, and points at `objects_keyed_by_value` for
an arena's contents (a legacy decimal arena keeps scale variants under two
handles).
No bench covered keying a vector, so a regression on the embedding shape
(one distinct 768-dimension vector per subject) could land unnoticed, as
keying a decoded vector by its Debug text did. `query_hot_vector_keying`
times a counted DISTINCT, GROUP BY and cross-predicate join on `?v`, and a
MINUS, on an indexed ledger before the arenas' identity tables exist, then
the three keying cells and a per-predicate COUNT(DISTINCT) after one has
built them. Its regression budget matches the other query-hot benches'.
`FlakeValue`'s `Hash` for a vector went through `vector_value_hash`, which
checks that every component narrows to `f32`, narrows them, then hashes:
right for keys that meet an arena slice, but MINUS, the union's fact dedup
and COUNT(DISTINCT *) hash decoded values only against each other, once or
more per row. A MINUS on a vector across 100K 768-dimension vectors came
to 2.35G instructions, against 976M before #2201; with this it is 1.27G.
`FlakeValue` now hashes a vector's `f64` bits with one `xxh3` pass, which
its bitwise equality agrees with, and stays strong on widened `f32`
components (`FxHasher` alone is weak on their zero low bits).
@aaj3f aaj3f added bug Something isn't working as expected area:query Query execution, planning, fast paths, overlay, result formatting performance Performance improvement (release notes: Performance) labels Nov 19, 2026
@aaj3f
aaj3f requested a review from bplatz November 19, 2026 19:26

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to unblock. Please address the inline comments before merging.

On the open question in the description: yes, please fold the MINUS / fact-dedup / COUNT(DISTINCT *) keying onto value keys into this PR.

Comment thread fluree-db-query/src/fast_count.rs Resolved
Comment thread fluree-db-query/src/fast_count.rs Resolved
Comment thread fluree-db-query/src/fast_count.rs Resolved
Comment thread fluree-db-query/src/fast_count.rs Resolved
Comment thread fluree-db-core/src/value.rs Resolved
The directory walk leaves out the arena-keyed objects, big numbers and
vectors, and each kind is counted from its arenas. Counting big numbers only
when some NumBig arena held entries, and vectors only when the graph held
vectors, let rows of a kind with no arena to count them (an inconsistent
index) add nothing, so the count came back low where it declined before.

The walk now tallies the rows of vector-only leaflets apart from the rest it
leaves out, and distinct_objects_of_walk counts each kind it left rows of:
the other rows go to the big-number count as they always did, which declines
without an arena, and vector rows need the graph's vectors. A kind with no
rows left out is not counted, so a graph of vectors with no big numbers does
not consult them. The comments that still said a graph holding vectors
declines now describe the vector count.

rows_the_walk_leaves_out_are_counted_or_decline covers each case; counting a
missing big-number count as 0, as the gate on the arenas did, fails it.
…round

The whole-graph distinct count compares the vectors of every hash two arenas
share, reading each through the shard cache in hash order. On a residency
host, once an earlier query had built the identity tables and the shards had
left the resident tier, each shard the compare read failed in turn and cost
its own retry round, and past the retry budget's cap the count failed where
it fell back before.

BinaryIndexStore::register_missing_vector_shards records every shard holding
one of a set of handles that the resident tier lacks, through the arena's
register_missing_shards_of (the per-handle sibling of the build's
register_missing_shards). The count calls it for the handles of every shared
hash before its first read, so one retry round fetches them all.

registered_vector_reads_fetch_their_shards_in_one_round reads one vector of
each of eight shards out of shard order after registering them: one round,
where without the registration it took eight. The residency arena fixture
and the fetch-and-retry loop are shared with the build's one-round test.
Both callers of group_count_v6 fell back to the general pipeline on any
error, so a cancelled identity-table build, an exhausted budget or a
residency miss declined instead of ending the query or asking for a fetch.
answer_or_decline raises those, the errors the general pipeline would meet
too, and declines on the rest, the group count's own failures.

a_fast_path_declines_only_on_its_own_failures pins which errors decline.
… check

The cross-arena compare calls vector_eq_f32, the comparison the identity
build uses, rather than its own copy. f64_bytes holds the one unsafe f64
byte view beside f32_bytes, for vector_value_hash and vector_bits_hash. The
gate tests' vector count check is check_liveness with the exact site as a
parameter.
@bplatz

bplatz commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Moving the MINUS / fact dedup / COUNT(DISTINCT *) value-key rework out to #2483 rather than folding it in here, so this can go in with the release. The review items are in (27d717b, d00baea, 9280c0e, 5142a53), plus the whole-graph bench cell (b7fced6).

@bplatz
bplatz merged commit f5cf335 into main Nov 21, 2026
21 checks passed
@bplatz
bplatz deleted the fix/vector-identity-followup branch November 21, 2026 21:00
bplatz added a commit that referenced this pull request Nov 21, 2026
Brings #2453 (a GRAPH scope's columns leave as they are when nothing
decodes), #2455 (a governed graph source honors its model's override
control), #2467 (R2RML scan join emit; iceberg top-k read budget) and
#2468 (vector identity follow-up: the whole-graph distinct-object count
counts vectors by value, a cold identity-table build fetches its arena
in one round, and the build stops on cancellation).

Conflicts:
- fast_count.rs: main's vector count on next's lead walk.
  count_distinct_lead_groups_inner tallies vector-only leaflets apart
  from the rest, and the NumBig liveness helpers take an ArenaRange (a
  store's NumBig o_types, or the vector o_type alone), so they serve
  count_distinct_vector_objects as main's generalized helpers do.
  count_distinct_objects_for_predicate takes next's tracker and main's
  cancellation, as do its callers in fast_predicate_scalar_agg.rs and
  fast_whole_graph_agg.rs.
- fast_group_count_firsts.rs: next's pooled-graph guard with main's
  answer_or_decline.
- o_type.rs: next's NumBig wording plus main's paragraph on arena
  contents.
- fluree_ext.rs: next's SourcePolicy and model-instance check, with
  main's async config and the model's override control.
- core lib.rs: main's MemoryCharge re-export; annotation_index has no
  home on next.
- Cargo.toml, regression-budget.json: both benches.

Ports:
- vector.rs: register_missing asks ContentStore::get_local whether a
  shard is resident; next has no resolve_cached_bytes, and a shard's
  cid is not optional.
- Tests: the residency vector arena stores its shards through
  write_test_shard_in, and BinaryIndexStore::empty() takes no
  directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:query Query execution, planning, fast paths, overlay, result formatting bug Something isn't working as expected performance Performance improvement (release notes: Performance)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants