diff --git a/lance_ray/index.py b/lance_ray/index.py index 6474531b..7bc3c757 100755 --- a/lance_ray/index.py +++ b/lance_ray/index.py @@ -697,23 +697,9 @@ def create_scalar_index( if name is None: name = f"{column}_idx" - if _index_exists(dataset, name): - if not replace: - raise ValueError( - f"Index with name '{name}' already exists. Set replace=True " - "to replace it." - ) - if not use_segment_workflow: - # Lance 4.0.0: fragment_ids + replace=True may hit an unimplemented path. - # Keep the existing driver-side replacement behavior for the legacy workflow. - dataset.drop_index(name) - dataset = LanceDataset( - dataset_uri, - **_dataset_load_kwargs( - merged_storage_options, namespace_kwargs, block_size - ), - ) - + # Run every non-destructive precondition before the replacement below: + # dropping an existing index is irreversible here, so a call that fails + # validation must not leave the dataset without its previous index. fragments = dataset.get_fragments() if not fragments: raise ValueError("Dataset contains no fragments") @@ -737,6 +723,26 @@ def create_scalar_index( requested_num_segments, ) + if _index_exists(dataset, name): + if not replace: + raise ValueError( + f"Index with name '{name}' already exists. Set replace=True " + "to replace it." + ) + if not use_segment_workflow: + # Lance 4.0.0: fragment_ids + replace=True may hit an unimplemented path. + # Keep the existing driver-side replacement behavior for the legacy workflow. + dataset.drop_index(name) + dataset = LanceDataset( + dataset_uri, + **_dataset_load_kwargs( + merged_storage_options, namespace_kwargs, block_size + ), + ) + fragments = dataset.get_fragments() + if fragment_ids is not None: + fragments = [f for f in fragments if f.fragment_id in fragment_ids] + fragment_batches = _distribute_fragments_balanced( fragments, num_segments=requested_num_segments, logger=logger ) diff --git a/tests/test_distributed_indexing.py b/tests/test_distributed_indexing.py index e37af66d..a548ecf3 100755 --- a/tests/test_distributed_indexing.py +++ b/tests/test_distributed_indexing.py @@ -4,7 +4,7 @@ import tempfile from collections.abc import Iterator from pathlib import Path -from typing import Any, cast +from typing import Any, Literal, cast import lance import lance_ray as lr @@ -12,6 +12,7 @@ import pyarrow as pa import pytest import ray +from lance.dataset import IndexConfig from lance_ray.search import _scanner_accepts_index_segments from packaging import version from ray.data import Dataset @@ -2109,3 +2110,44 @@ def test_build_distributed_vector_index_trains_on_ray_task(tmp_path: Path) -> No q = [random.gauss(0, 1) for _ in range(128)] result = updated_dataset.to_table(nearest={"column": "vector", "q": q, "k": 5}) assert result.num_rows == 5 + + +@pytest.mark.parametrize( + "index_type", + ["BTREE", IndexConfig("SCALAR", {})], + ids=["segment-workflow", "legacy-workflow"], +) +def test_failed_validation_keeps_existing_index( + temp_dir: str, index_type: Literal["BTREE"] | IndexConfig +) -> None: + """A replace that fails validation must not drop the existing index. + + The legacy ``IndexConfig`` workflow replaces the index on the driver, so + every precondition has to run before that drop; otherwise the dataset is + left without an index and no rebuild happens. + """ + ds = generate_multi_fragment_dataset( + temp_dir, num_fragments=2, rows_per_fragment=50 + ) + lr.create_scalar_index( + uri=ds.uri, + column="id", + index_type="BTREE", + name="keep_me", + num_workers=2, + ) + + with pytest.raises(ValueError, match="do not exist"): + lr.create_scalar_index( + uri=ds.uri, + column="id", + index_type=index_type, + name="keep_me", + fragment_ids=[999], + replace=True, + num_workers=2, + ) + + assert "keep_me" in { + index.name for index in lance.dataset(ds.uri).describe_indices() + }