Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions lance_ray/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
)
Expand Down
44 changes: 43 additions & 1 deletion tests/test_distributed_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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
import numpy as np
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
Expand Down Expand Up @@ -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()
}
Loading