Repository navigation
fix(label_list): bound index build memory with a sorted stream - #8840
Conversation
aa6cc44 to
c806b8e
Compare
c806b8e to
f4a4ba8
Compare
westonpace
left a comment
There was a problem hiding this comment.
The label list index is a pretty shallow wrapper around a bitmap index. I fear this PR introduces a lot of duplicate code.
Originally the bitmap index did not require ordered input and we added spilling so that once a bitmap was large enough, we spilled to disk but the spill was kind of janky.
In #6371 we changed this so that the bitmap builder does require ordered input, and we don't need to spill (more accurately, we deferred the spill to datafusion's sort).
Now we have the label list index, which accepts unordered input, and so we are introducing a new hand-rolled spilling mechanism (which has a different memory budget and config).
Could we instead unnest and then sort, and then use a non-spilling building. This way the label list builder remains a thin wrapper around the bitmap builder, instead of having a bunch of private architecture behind it.
8fef930 to
48e6a06
Compare
LabelList materialized its whole label-to-bitmap map during build, update, merge and remap, so peak memory scaled with distinct-label cardinality and large list columns caused out-of-memory failures. Build and update now unnest, sort with DataFusion, and feed bitmap's existing non-spilling streaming builder, so spilling is DataFusion's and is bounded by LANCE_MEM_POOL_SIZE instead of an index-specific budget. Bitmap's algorithms are split from output-file ownership -- build_index_map, remap_index_map and merge_index_maps each take a &mut BitmapBatchWriter -- so LabelList attaches its list_nulls buffer to the same file and each of its four paths becomes a thin wrapper. Update joins through bitmap's merge-join rather than rewriting the existing index into scratch. Also fixes a latent overflow in the shared bitmap writer: its flush threshold was the i32 offset ceiling itself and charged only serialized bitmaps, so a high-cardinality column with tiny bitmaps could exceed the keys column's offsets before ever tripping a flush. It now charges keys as well and flushes at 32 MiB, which changes the physical batching of every bitmap index file written from here on. The on-disk format and index version are unchanged. Build is bounded end to end. Update, merge and remap keep an O(distinct labels) floor, because opening a BitmapIndex loads every key into an in-memory BTreeMap -- this bounds aggregation state, not process RSS.
48e6a06 to
bc20b8b
Compare
There was a problem hiding this comment.
This revision changes only the old_data_filter comment; executable and test code are identical to the previously reviewed head. The clarified stale-posting limitation is pre-existing and this PR does not expand its exposure, so no change is requested here. The sorted-input Bitmap rewrite and its spill, compatibility, null, update, remap, and merge evidence remain valid.
The non-blocking risk remains that LANCE_MEM_POOL_SIZE bounds DataFusion sort memory, not total process RSS. Fresh builds no longer retain all distinct-label postings, but update, merge, and remap still keep the loaded O(distinct labels) key map resident alongside caches, list-null state, the current posting, and output buffers. Operators should treat the pool as a sort budget rather than an end-to-end memory ceiling.
westonpace
left a comment
There was a problem hiding this comment.
Thanks for adopting this approach just to keep things simpler and thanks for taking this on!
## Summary
With stable row IDs enabled, a `LABEL_LIST` index can match an updated
row against its previous label after index optimization, even though the
stored value is correct. This change applies `old_data_filter` to
existing label postings and list-level null rows before merging
replacement data.
## Original scenario and impact
This was found while evaluating stable row IDs for
`_row_last_updated_at_version` in an embedded LanceDB application that
uses a `list<string>` column for access-control labels. The reporting
application's production tables do not enable stable row IDs; the
reproductions use synthetic data.
In the original 50-row example, changing one row's `acl` from
`["Anonymous"]` to `["Manager"]` correctly reduced the results for
`array_has_any(acl, ['Anonymous'])` from 50 to 49. After
`table.optimize()`, the count returned to 50, including the updated row
with its current `["Manager"]` value. This occurred with both `update`
and `merge_insert`, and with both filtered scans and vector searches
using prefiltering.
Applications that rely on this predicate to enforce access restrictions,
without independently checking the current permissions, could therefore
return a row after its previous access label has been revoked. This is a
query-correctness defect with potential application-level authorization
impact. The reproductions do not establish a bypass of built-in
authentication or a production disclosure.
The behavior was reproduced with LanceDB 0.37.1 and 0.38.0, and with
source-built Lance at `2602724cf6256ff8f55571805fdc5b0614d706b8`.
## Minimal reproduction
The problem reduces to two rows, one list column, one `LABEL_LIST`
index, and an update to one row. A BTREE index, vector search, and file
compaction are not required; `optimize_indices()` alone reproduces it.
```python
import lance
import pyarrow as pa
def test_old_label_is_removed(tmp_path):
ds = lance.write_dataset(
pa.table({"labels": [["old"], ["keep"]]}),
tmp_path,
enable_stable_row_ids=True,
)
ds.create_scalar_index("labels", "LABEL_LIST")
old = "array_has_any(labels, ['old'])"
ds.update({"labels": "['new']"}, where=old)
assert ds.to_table(filter=old).num_rows == 0
ds.optimize.optimize_indices()
assert ds.to_table(filter=old, use_scalar_index=False).num_rows == 0
assert ds.to_table(filter=old).num_rows == 0
```
Before the fix, the final assertion fails with `1 == 0`: the indexed
query returns the row containing `["new"]`, while the forced scan
correctly returns no rows. The corresponding test with stable row IDs
disabled passes.
## Cause and fix
`LabelListIndex::update` received but discarded `old_data_filter`,
allowing obsolete memberships to survive index updates. Pass that filter
through to the existing bitmap builder and also apply it to the old
`list_nulls` bitmap. Prune old state before adding replacements, since
replacement rows may reuse the same stable row IDs.
This addresses the existing correctness limitation documented in #8840.
It preserves the streaming build approach and does not change the public
API or index file format.
The Python regression covers stable row IDs enabled and disabled across
two fragments. The Rust regression covers row-ID and fragment filters,
null elements, null lists, empty lists, and preservation of unchanged
rows.
## Validation
- Python LabelList tests: 11 passed.
- Rust LabelList tests: 18 passed.
- Additional local investigation scripts: all 32 comparison cases and
all eight original Lance probe configurations passed with the patched
Lance build.
- Rust formatting, Ruff checks for the changed Python file, and commit
hooks passed.
The full workspace test suite, workspace-wide Clippy, and full Python
lint target have not been run. Post-fix validation used the locally
built Lance library; the released LanceDB wheels were not modified.
## Existing indices
This prevents stale memberships from being retained during future
updates. It does not automatically repair an index that already contains
them. In the affected synthetic dataset, opening it with the patched
library still returned the stale match; rebuilding the index with
`create_scalar_index("acl", "LABEL_LIST", replace=True)` restored the
correct result.
Please consider whether the conditional authorization impact warrants
`critical-fix` treatment and a backport.
Reported and fixed by Manabu TERADA (@terapyon).
LabelList materialized its whole label-to-bitmap map during build, update,
merge and remap, so peak memory scaled with distinct-label cardinality and
large list columns caused out-of-memory failures.
Build and update now unnest, sort with DataFusion, and feed bitmap's existing
non-spilling streaming builder, so spilling is DataFusion's and is bounded by
LANCE_MEM_POOL_SIZE instead of an index-specific budget. Bitmap's algorithms
are split from output-file ownership -- build_index_map, remap_index_map and
merge_index_maps each take a &mut BitmapBatchWriter -- so LabelList attaches
its list_nulls buffer to the same file and each of its four paths becomes a
thin wrapper. Update joins through bitmap's merge-join rather than rewriting
the existing index into scratch.
Also fixes a latent overflow in the shared bitmap writer: its flush threshold
was the i32 offset ceiling itself and charged only serialized bitmaps, so a
high-cardinality column with tiny bitmaps could exceed the keys column's
offsets before ever tripping a flush. It now charges keys as well and flushes
at 32 MiB, which changes the physical batching of every bitmap index file
written from here on. The on-disk format and index version are unchanged.
Build is bounded end to end. Update, merge and remap keep an O(distinct
labels) floor, because opening a BitmapIndex loads every key into an
in-memory BTreeMap -- this bounds aggregation state, not process RSS.