diff --git a/python/python/lancedb/permutation.py b/python/python/lancedb/permutation.py index 1656548ec..d92a5a22d 100644 --- a/python/python/lancedb/permutation.py +++ b/python/python/lancedb/permutation.py @@ -3,6 +3,7 @@ import copy import json +import operator import os from deprecation import deprecated @@ -1470,9 +1471,11 @@ class Permutation: returned dict is not merged into the row: return every column to keep. A mapped permutation always yields a list of dicts (one per row) for each - batch, which is the same shape as the default "python" format. ``batched`` - only controls what ``fn`` receives. Rows keep their position, so a callback - cannot add, drop, or reorder rows. + batch, which is the same shape as the default "python" format. An integer + index unwraps that list and returns the one dict. ``take_offsets`` and + ``__getitems__`` still return the list. ``batched`` only controls what + ``fn`` receives. Rows keep their position, so a callback cannot add, drop, + or reorder rows. Since ``map`` decides the output format it cannot be combined with [with_format][lancedb.permutation.Permutation.with_format] or @@ -1548,7 +1551,12 @@ class Permutation: The returned value is passed through the permutation's current transform, so `with_format`, `with_transform`, and `map` affect this method in the - same way they affect iteration. + same way they affect iteration. Offsets are positions in the current + permutation, including any `with_skip` or `with_take` window, for both an + identity permutation and one backed by a permutation table. An offset + outside that window raises an error. The batch is returned as-is, + including a one-element list. For a single row, use + [__getitem__][lancedb.permutation.Permutation.__getitem__]. """ self._ensure_open() @@ -1560,13 +1568,57 @@ class Permutation: def __getitem__(self, index: int) -> Any: """ - Returns a single row from the permutation by offset + Return the row at `index`. + + `index` follows Python sequence indexing. `0` is the first row and a + negative value counts from the end. An index past either end raises + `IndexError`. A non-integer, such as a slice or a list, raises + `TypeError`. + + The row is read with + [take_offsets][lancedb.permutation.Permutation.take_offsets] and passed + through the current transform + ([with_format][lancedb.permutation.Permutation.with_format], + [with_transform][lancedb.permutation.Permutation.with_transform], or + [map][lancedb.permutation.Permutation.map]). + + A transform that returns a `list` is treated as one item per row, and + this method returns that single item. The default `"python"` format and + [map][lancedb.permutation.Permutation.map] therefore return one `dict`. + `"torch"` returns one per-row dict of tensors and `"torch_row"` returns + one tensor. If the list length is not one, the list is returned + unchanged. That keeps a custom transform which does not emit one value + per row intact. + + Every other return type is returned as the transform produced it, for + the one requested row. `"arrow"` stays a one-row RecordBatch. + `"pandas"` and `"polars"` stay one-row frames. `"numpy"` stays a + one-row ndarray. `"python_col"`, `"torch_dict"`, and `"torch_col"` + stay a dict or tensor covering that single row. + + [__getitems__][lancedb.permutation.Permutation.__getitems__] and + `take_offsets` still return the transformed batch. A list-producing + transform stays a list there, including when only one offset is + requested, and negative offsets are not accepted. """ - return self.take_offsets([index]) + resolved = operator.index(index) + length = len(self) + if resolved < 0: + resolved += length + if resolved < 0 or resolved >= length: + raise IndexError(f"permutation index {index} is out of range") + + result = self.take_offsets([resolved]) + if isinstance(result, list) and len(result) == 1: + return result[0] + return result def __getitems__(self, indices: list[int]) -> Any: """ - Returns rows from the permutation by offset + Return rows for `indices` as one transformed batch. + + Unlike [__getitem__][lancedb.permutation.Permutation.__getitem__], this + does not unwrap a one-element list into a single row. """ return self.take_offsets(indices) diff --git a/python/python/tests/test_permutation.py b/python/python/tests/test_permutation.py index add877582..a032226c8 100644 --- a/python/python/tests/test_permutation.py +++ b/python/python/tests/test_permutation.py @@ -1713,6 +1713,255 @@ def test_getitems_invalid_offset(some_permutation: Permutation): some_permutation.__getitems__([999999]) +def test_getitem_returns_single_row(some_permutation: Permutation): + """An integer index returns one row, matching the first batched item.""" + row = some_permutation[0] + batched = some_permutation.__getitems__([0]) + + assert isinstance(batched, list) + assert len(batched) == 1 + assert isinstance(row, dict) + assert row == batched[0] + assert row == some_permutation.take_offsets([0])[0] + assert some_permutation.with_format("python")[0] == row + assert ( + some_permutation[-1] + == some_permutation.__getitems__([some_permutation.num_rows - 1])[0] + ) + + +def test_getitems_two_indices_stay_a_list(some_permutation: Permutation): + """Batched access still returns one list entry per requested offset.""" + rows = some_permutation.__getitems__([0, 1]) + + assert isinstance(rows, list) + assert len(rows) == 2 + assert rows[0] == some_permutation[0] + assert rows[1] == some_permutation[1] + + +def test_getitem_arrow_format_is_one_row_batch(some_permutation: Permutation): + batch = some_permutation.with_format("arrow")[0] + + assert isinstance(batch, pa.RecordBatch) + assert batch.num_rows == 1 + assert batch.to_pylist() == [some_permutation[0]] + + several = some_permutation.with_format("arrow").__getitems__([0, 1]) + assert isinstance(several, pa.RecordBatch) + assert several.num_rows == 2 + + +def test_getitem_python_col_format(some_permutation: Permutation): + np = pytest.importorskip("numpy") + row = some_permutation[0] + + columnar = some_permutation.with_format("python_col")[0] + assert columnar == {"id": [row["id"]], "value": [row["value"]]} + assert some_permutation[np.int64(0)] == row + + +def test_getitem_numpy_format(some_permutation: Permutation): + """The numpy format is built with ``batch.to_pandas().to_numpy()``.""" + np = pytest.importorskip("numpy") + pytest.importorskip("pandas") + row = some_permutation[0] + + array = some_permutation.with_format("numpy")[0] + assert isinstance(array, np.ndarray) + assert array.shape == (1, some_permutation.num_columns) + assert array[0, 0] == row["id"] + assert array[0, 1] == row["value"] + + +def test_getitem_pandas_format(some_permutation: Permutation): + pd = pytest.importorskip("pandas") + + frame = some_permutation.with_format("pandas")[0] + + assert isinstance(frame, pd.DataFrame) + assert len(frame) == 1 + assert frame.to_dict(orient="records") == [some_permutation[0]] + + +def test_getitem_polars_format(some_permutation: Permutation): + pl = pytest.importorskip("polars") + + frame = some_permutation.with_format("polars")[0] + + assert isinstance(frame, pl.DataFrame) + assert frame.shape == (1, some_permutation.num_columns) + assert frame.to_dicts() == [some_permutation[0]] + + +def test_getitem_torch_formats(some_permutation: Permutation): + torch = pytest.importorskip("torch") + + per_row = some_permutation.with_format("torch")[0] + assert isinstance(per_row, dict) + assert set(per_row) == {"id", "value"} + assert isinstance(per_row["id"], torch.Tensor) + assert per_row["id"].shape == () + assert int(per_row["id"]) == some_permutation[0]["id"] + + row_tensor = some_permutation.with_format("torch_row")[0] + assert isinstance(row_tensor, torch.Tensor) + assert row_tensor.shape == (some_permutation.num_columns,) + + columns = some_permutation.with_format("torch_dict")[0] + assert isinstance(columns, dict) + assert columns["id"].shape == (1,) + assert int(columns["id"][0]) == some_permutation[0]["id"] + + stacked = some_permutation.with_format("torch_col")[0] + assert isinstance(stacked, torch.Tensor) + assert stacked.shape == (some_permutation.num_columns, 1) + + rows = some_permutation.with_format("torch").__getitems__([0, 1]) + assert isinstance(rows, list) + assert len(rows) == 2 + assert rows[0]["id"].shape == () + + +def test_getitem_custom_transform(mem_db): + """Lists of one row unwrap; other custom return values stay intact.""" + table = mem_db.create_table( + "getitem_custom", pa.table({"id": range(4), "value": range(4)}) + ) + permutation = Permutation.identity(table) + + def ids(batch: pa.RecordBatch) -> list[int]: + return batch.column("id").to_pylist() + + listed = permutation.with_transform(ids) + assert listed[0] == 0 + assert listed[-1] == 3 + assert listed.__getitems__([0, 1]) == [0, 1] + assert listed.take_offsets([2, 0]) == [2, 0] + + def as_batch(batch: pa.RecordBatch) -> pa.RecordBatch: + return batch + + kept = permutation.with_transform(as_batch)[1] + assert isinstance(kept, pa.RecordBatch) + assert kept.num_rows == 1 + assert kept.column("id").to_pylist() == [1] + + def pair(batch: pa.RecordBatch) -> tuple[int, int]: + row = batch.to_pylist()[0] + return (row["id"], row["value"]) + + assert permutation.with_transform(pair)[2] == (2, 2) + + def not_a_row_list(batch: pa.RecordBatch) -> list[int]: + return [batch.num_rows, batch.num_columns] + + assert permutation.with_transform(not_a_row_list)[0] == [1, 2] + + +def test_getitem_negative_and_out_of_range(mem_db): + table = mem_db.create_table( + "getitem_bounds", pa.table({"id": range(5), "value": range(5, 10)}) + ) + permutation = Permutation.identity(table) + + assert permutation[0] == {"id": 0, "value": 5} + assert permutation[-1] == {"id": 4, "value": 9} + assert permutation[-5] == permutation[0] + assert permutation[4] == permutation[-1] + + with pytest.raises(IndexError, match="out of range"): + permutation[5] + with pytest.raises(IndexError, match="out of range"): + permutation[-6] + + with pytest.raises(TypeError): + permutation["0"] + with pytest.raises(TypeError): + permutation[0:2] + with pytest.raises(TypeError): + permutation[[0, 1]] + + # Multi-row reads keep raw offsets and still reject negatives. + with pytest.raises(OverflowError): + permutation.__getitems__([-1]) + with pytest.raises(OverflowError): + permutation.take_offsets([-1]) + + +def _iter_rows(permutation: Permutation) -> list: + rows = [] + for batch in permutation.iter(32, skip_last_batch=False): + rows.extend(batch) + return rows + + +def test_identity_window_matches_iteration(mem_db): + """Identity skip/take uses one index space for reads and iteration.""" + table = mem_db.create_table( + "identity_window", pa.table({"id": range(10), "value": range(10, 20)}) + ) + identity = Permutation.identity(table) + windows = [ + identity.with_skip(3).with_take(4), + identity.with_skip(3), + identity.with_take(4), + ] + + for window in windows: + iterated = _iter_rows(window) + last = len(iterated) - 1 + assert window.num_rows == len(iterated) + assert window[0] == iterated[0] + assert window[-1] == iterated[last] + assert window.__getitems__([0, last]) == [iterated[0], iterated[last]] + assert window.take_offsets([0, last]) == [iterated[0], iterated[last]] + assert window.take_offsets([last, 0]) == [iterated[last], iterated[0]] + with pytest.raises(Exception): + window.take_offsets([len(iterated)]) + with pytest.raises(Exception): + window.__getitems__([0, len(iterated)]) + with pytest.raises(IndexError, match="out of range"): + window[len(iterated)] + + skipped = identity.with_skip(3).with_take(4) + assert skipped[0] == {"id": 3, "value": 13} + assert skipped[-1] == {"id": 6, "value": 16} + assert [row["id"] for row in _iter_rows(skipped)] == [3, 4, 5, 6] + + +def test_identity_window_dataloader(mem_db): + torch = pytest.importorskip("torch") + table = mem_db.create_table("identity_loader", pa.table({"id": range(10)})) + window = Permutation.identity(table).with_skip(3).with_take(4) + + one_at_a_time = list(torch.utils.data.DataLoader(window, batch_size=None)) + assert [row["id"] for row in one_at_a_time] == [3, 4, 5, 6] + + batches = list(torch.utils.data.DataLoader(window, batch_size=2, shuffle=False)) + assert [[int(value) for value in batch["id"]] for batch in batches] == [ + [3, 4], + [5, 6], + ] + + +def test_getitem_negative_respects_skip_window(some_permutation: Permutation): + """Negative indexes count from the end of a skip/take window.""" + window = some_permutation.with_skip(100).with_take(200) + last = window.num_rows - 1 + + assert window[0] == window.__getitems__([0])[0] + assert window[-1] == window.__getitems__([last])[0] + assert window[0] == some_permutation.__getitems__([100])[0] + assert window[-1] == some_permutation.__getitems__([100 + last])[0] + with pytest.raises(IndexError, match="out of range"): + window[window.num_rows] + with pytest.raises(IndexError, match="out of range"): + window[-window.num_rows - 1] + with pytest.raises(Exception): + window.take_offsets([window.num_rows]) + + def test_take_offsets(some_permutation: Permutation): result = some_permutation.take_offsets([0, 1, 2]) @@ -2006,6 +2255,44 @@ def test_map_take_offsets(some_permutation: Permutation): assert calls == [] +def test_getitem_unwraps_mapped_row(some_permutation: Permutation): + """map() yields one dict per row, so an integer index unwraps that dict.""" + last = some_permutation.num_rows - 1 + source = some_permutation.take_offsets([0, 1, last]) + + for mapped in ( + some_permutation.map(_double_value_row), + some_permutation.map(_double_value_batch, batched=True), + ): + assert mapped[0] == _double_value_row(source[0]) + assert mapped[-1] == _double_value_row(source[2]) + assert mapped.take_offsets([0]) == [mapped[0]] + assert mapped.__getitems__([0]) == [mapped[0]] + assert mapped.__getitems__([0, 1]) == [ + _double_value_row(source[0]), + _double_value_row(source[1]), + ] + with pytest.raises(IndexError, match="out of range"): + mapped[mapped.num_rows] + with pytest.raises(IndexError, match="out of range"): + mapped[-mapped.num_rows - 1] + + chained = some_permutation.map(_double_value_row).map( + _double_value_batch, batched=True + ) + assert chained[1]["value"] == source[1]["value"] * 4 + assert chained.take_offsets([1]) == [chained[1]] + + window_source = some_permutation.take_offsets([2, 4]) + for window in ( + some_permutation.with_skip(2).with_take(3).map(_double_value_row), + some_permutation.map(_double_value_row).with_skip(2).with_take(3), + ): + assert window[0] == _double_value_row(window_source[0]) + assert window[-1] == _double_value_row(window_source[1]) + assert window.take_offsets([0, 2]) == [window[0], window[-1]] + + def test_map_validates_output(some_permutation: Permutation): """Test .map() rejects callback output that would break the batch shape.""" with pytest.raises(ValueError, match="returned 1 values for column `id`"): diff --git a/rust/lancedb/src/dataloader/permutation/reader.rs b/rust/lancedb/src/dataloader/permutation/reader.rs index 015c3a17d..446d14f5f 100644 --- a/rust/lancedb/src/dataloader/permutation/reader.rs +++ b/rust/lancedb/src/dataloader/permutation/reader.rs @@ -508,9 +508,30 @@ impl PermutationReader { )?; Self::load_batch(&self.base_table, row_ids, selection).await } else { + // No permutation table: offsets address the base table. Shift by the + // skip and reject anything outside the skip/take window, which is the + // same index space `count_rows` and `read` use. A missing map entry + // on the permutation-table path is an error, so an offset past this + // window is an error too. + let start = self.offset.unwrap_or(0); + let absolute_offsets = offsets + .iter() + .map(|offset| { + if *offset >= self.available_rows { + Err(Error::InvalidInput { + message: format!( + "Offset {offset} is outside the permutation window of length {}", + self.available_rows + ), + }) + } else { + Ok(start + *offset) + } + }) + .collect::>>()?; let table = Table::from(self.base_table.clone()); let batches = table - .take_offsets(offsets.to_vec()) + .take_offsets(absolute_offsets) .preserve_order() .select(selection.clone()) .execute() @@ -950,6 +971,116 @@ mod tests { assert_eq!(idx_values, vec![0, 2, 0, 4, 6]); } + #[tokio::test] + async fn test_take_offsets_identity_window_matches_iteration() { + let base_table = lance_datagen::gen_batch() + .col("idx", lance_datagen::array::step::()) + .into_mem_table("tbl", RowCount::from(10), BatchCount::from(1)) + .await; + let base = base_table.base_table().clone(); + + let window = PermutationReader::identity(base.clone()) + .await + .unwrap() + .with_offset(3) + .await + .unwrap() + .with_limit(4) + .await + .unwrap(); + assert_eq!(window.count_rows(), 4); + + let iterated = collect_from_stream::( + window + .read( + Select::All, + QueryExecutionOptions { + max_batch_length: 10, + ..Default::default() + }, + ) + .await + .unwrap(), + "idx", + ) + .await; + assert_eq!(iterated, vec![3, 4, 5, 6]); + + let taken = window.take_offsets(&[0, 3, 1], Select::All).await.unwrap(); + let taken_idx = taken + .column(0) + .as_primitive::() + .values() + .to_vec(); + assert_eq!(taken_idx, vec![iterated[0], iterated[3], iterated[1]]); + assert!(window.take_offsets(&[4], Select::All).await.is_err()); + assert!(window.take_offsets(&[0, 4], Select::All).await.is_err()); + + let skipped = PermutationReader::identity(base.clone()) + .await + .unwrap() + .with_offset(3) + .await + .unwrap(); + assert_eq!(skipped.count_rows(), 7); + let skipped_batch = skipped.take_offsets(&[0, 6], Select::All).await.unwrap(); + assert_eq!( + skipped_batch + .column(0) + .as_primitive::() + .values() + .to_vec(), + vec![3, 9] + ); + assert!(skipped.take_offsets(&[7], Select::All).await.is_err()); + + let limited = PermutationReader::identity(base) + .await + .unwrap() + .with_limit(2) + .await + .unwrap(); + assert_eq!(limited.count_rows(), 2); + let limited_batch = limited.take_offsets(&[0, 1], Select::All).await.unwrap(); + assert_eq!( + limited_batch + .column(0) + .as_primitive::() + .values() + .to_vec(), + vec![0, 1] + ); + assert!(limited.take_offsets(&[2], Select::All).await.is_err()); + } + + #[tokio::test] + async fn test_take_offsets_permutation_window_rejects_past_end() { + let (base_table, row_ids_table, row_ids) = setup_permutation_tables(6).await; + let reader = PermutationReader::try_from_tables( + base_table.base_table().clone(), + row_ids_table.base_table().clone(), + 0, + ) + .await + .unwrap() + .with_offset(1) + .await + .unwrap() + .with_limit(2) + .await + .unwrap(); + + assert_eq!(reader.count_rows(), 2); + let batch = reader.take_offsets(&[0, 1], Select::All).await.unwrap(); + let idx_values = batch + .column(0) + .as_primitive::() + .values() + .to_vec(); + assert_eq!(idx_values, vec![row_ids[1] as i32, row_ids[2] as i32]); + assert!(reader.take_offsets(&[2], Select::All).await.is_err()); + } + #[tokio::test] async fn test_take_offsets_caches_offset_map() { let (base_table, row_ids_table, row_ids) = setup_permutation_tables(10).await;