test: lock in that delete keeps table indices (#5168)

`AsyncTable.delete` does not drop indexes. On current main the reported
failure does not reproduce: delete forwards to Lance `dataset.delete`,
and index retention comes from Lance (the retention change in
lance-format/lance#4033, which closed lance-format/lance#3940), not from
recreating indexes after the delete. Nothing in this repo's CI currently
asserts it, so these tests lock that behavior in at the LanceDB level:

- Vector (IVF_FLAT), BTree, and full-text indexes survive deleting a
subset of rows, `delete("true")`, and a later insert. After the partial
delete, `list_indices` and `index_stats` still report indexed rows, and
scalar, full-text, and vector search return the live rows and omit
deleted ones.
- The reported empty-table sequence: `connect_async` to a directory,
create an empty table from a schema, `create_index(column="txt",
config=BTree())` before any rows, add 10 rows, then `delete(where="txt =
'1'")`. `list_indices()` still has one BTree on `txt`, the table has 9
rows, and the same holds after `open_table`.

This is an alternative to the approach in #2471, which saved and
recreated every index inside `delete`. That was declined by a maintainer
because it leaves a window with no indexes, and it is unnecessary now
that Lance keeps them.

Tests only: `rust/lancedb/src/table/delete.rs` and
`python/python/tests/test_table.py`. Both are deterministic (single IVF
partition, exact vectors, distinct FTS tokens) and run in well under a
second.

Refs #2434

## Test plan

- [x] `cargo test --features remote -p lancedb --lib
table::delete::tests::delete_keeps_vector_scalar_and_fts_indices` (5
runs)
- [x] `cargo clippy --features remote -p lancedb --tests -- -D
warnings`, `cargo fmt --all -- --check`
- [x] `pytest` on the two new Python tests (5 runs)
- [x] `ruff format --check .` and `ruff check .` in `python/`

Written with Cursor (Grok).

Co-authored-by: Preston Zhao <195942578+canyang25@users.noreply.github.com>
This commit is contained in:
Preston Zhao
2026-11-30 19:12:12 +08:00
committed by GitHub
co-authored by Preston Zhao
parent 53ae53877d
commit 3e31cd4eab
2 changed files with 232 additions and 5 deletions
+93 -1
View File
@@ -23,7 +23,7 @@ from unittest.mock import MagicMock, patch
import lancedb
from lancedb import table as table_module
from lancedb.dependencies import _PANDAS_AVAILABLE
from lancedb.index import BTree, FTS, HnswFlat, HnswPq, HnswSq, IvfPq
from lancedb.index import BTree, FTS, HnswFlat, HnswPq, HnswSq, IvfFlat, IvfPq
import numpy as np
import polars as pl
import pyarrow as pa
@@ -3394,6 +3394,98 @@ async def test_delete_expr_async(mem_db_async: AsyncConnection):
assert sorted((await table.to_arrow())["id"].to_pylist()) == [1, 2]
@pytest.mark.asyncio
async def test_async_delete_keeps_indices(mem_db_async: AsyncConnection):
table = await mem_db_async.create_table(
"indexed_delete",
[
{
"id": row_id,
"category": "keep" if row_id < 8 else "drop",
"text": f"row{row_id}",
"vector": [float(row_id), 0.0, 0.0, 0.0],
}
for row_id in range(16)
],
)
await table.create_index(
"vector", config=IvfFlat(num_partitions=1), name="vector_idx"
)
await table.create_index("category", config=BTree(), name="category_idx")
await table.create_index("text", config=FTS(), name="text_idx")
expected = [
("category_idx", "BTree"),
("text_idx", "FTS"),
("vector_idx", "IvfFlat"),
]
async def names():
indices = await table.list_indices()
return sorted((index.name, index.index_type) for index in indices)
async def ids_of(query):
return sorted((await query.to_arrow())["id"].to_pylist())
assert (await table.delete("category = 'drop'")).num_deleted_rows == 8
assert await names() == expected
for index in await table.list_indices():
assert index.num_indexed_rows > 0
stats = await table.index_stats(index.name)
assert stats is not None and stats.num_indexed_rows > 0
kept = table.query().where("category = 'keep'").limit(100)
assert await ids_of(kept) == list(range(8))
assert await ids_of(table.query().nearest_to_text("row0", columns="text")) == [0]
assert await ids_of(table.query().nearest_to_text("row8", columns="text")) == []
assert await ids_of(table.query().nearest_to([0.0, 0.0, 0.0, 0.0]).limit(1)) == [0]
assert (await table.delete("true")).num_deleted_rows == 8
assert await table.count_rows() == 0
assert await names() == expected
await table.add(
[
{
"id": 20,
"category": "fresh",
"text": "row20",
"vector": [20.0, 0.0, 0.0, 0.0],
}
]
)
assert await names() == expected
fresh = table.query().where("category = 'fresh'").limit(100)
assert await ids_of(fresh) == [20]
assert await ids_of(table.query().nearest_to_text("row20", columns="text")) == [20]
assert await ids_of(table.query().nearest_to_text("row0", columns="text")) == []
nearest = table.query().nearest_to([20.0, 0.0, 0.0, 0.0]).limit(1)
assert await ids_of(nearest) == [20]
@pytest.mark.asyncio
async def test_async_delete_keeps_btree_created_before_rows(tmp_path):
schema = pa.schema([pa.field("pk", pa.string()), pa.field("txt", pa.string())])
db = await lancedb.connect_async(tmp_path)
table = await db.create_table("test_table", schema=schema)
await table.create_index(column="txt", config=BTree())
values = [str(row_id) for row_id in range(10)]
await table.add(pa.table({"pk": values, "txt": values}))
await table.delete(where="txt = '1'")
async def assert_one_btree(opened):
assert await opened.count_rows() == 9
indices = await opened.list_indices()
assert len(indices) == 1
index = indices[0]
assert index.name == "txt_idx"
assert index.index_type == "BTree"
assert list(index.columns) == ["txt"]
await assert_one_btree(table)
reopened = await (await lancedb.connect_async(tmp_path)).open_table("test_table")
await assert_one_btree(reopened)
def test_update(mem_db: DBConnection):
table = mem_db.create_table(
"my_table",