From a9be4525a046c7b6356087adaf8fbe658995ddba Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Fri, 20 Nov 2026 20:20:34 +0800 Subject: [PATCH] fix(python): accept optional discard_weight in split_hash (#5116) ### Problem `PermutationBuilder.split_hash()` raises `TypeError` when `discard_weight` is omitted because the Python wrapper forwards `None` while the PyO3 binding requires `u64`. The `_lancedb.pyi` stub also disagrees with the native signature: it omits `split_names` and types `discard_weight` as non-optional. ### Behavior The native binding accepts an optional `discard_weight` and treats omitted/`None` as `0`, so `split_hash(..., split_names=[...])` works without passing a discard weight. The stub now matches the binding. A regression test verifies that omitting `discard_weight` assigns every row exactly like explicit `0`. Fixes OSS-2856. --- python/python/lancedb/_lancedb.pyi | 7 ++++- python/python/tests/test_permutation.py | 35 +++++++++++++++++++++++++ python/src/permutation.rs | 5 ++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/python/python/lancedb/_lancedb.pyi b/python/python/lancedb/_lancedb.pyi index 54b9f8083..5a89f100b 100644 --- a/python/python/lancedb/_lancedb.pyi +++ b/python/python/lancedb/_lancedb.pyi @@ -1280,7 +1280,12 @@ class AsyncPermutationBuilder: seed: Optional[int] = None, ) -> "AsyncPermutationBuilder": ... def split_hash( - self, columns: List[str], split_weights: List[int], *, discard_weight: int = 0 + self, + columns: List[str], + split_weights: List[int], + *, + discard_weight: Optional[int] = None, + split_names: Optional[List[str]] = None, ) -> "AsyncPermutationBuilder": ... def split_sequential( self, diff --git a/python/python/tests/test_permutation.py b/python/python/tests/test_permutation.py index 46d6493ed..165172622 100644 --- a/python/python/tests/test_permutation.py +++ b/python/python/tests/test_permutation.py @@ -275,6 +275,41 @@ def test_split_hash(mem_db): assert data["split_id"] == data2["split_id"] # Should be identical +def test_split_hash_default_discard_weight_matches_zero(mem_db): + """Omitting discard_weight must assign every row like discard_weight=0.""" + tbl = mem_db.create_table( + "test_table", + pa.table( + { + "id": range(100), + "category": (["A", "B", "C"] * 34)[:100], + "value": range(100), + } + ), + ) + + default = ( + permutation_builder(tbl) + .split_hash(["category"], [1, 1], split_names=["train", "test"]) + .execute() + ) + explicit_zero = ( + permutation_builder(tbl) + .split_hash( + ["category"], [1, 1], discard_weight=0, split_names=["train", "test"] + ) + .execute() + ) + + default_data = default.search(None).to_arrow().to_pydict() + explicit_data = explicit_zero.search(None).to_arrow().to_pydict() + assert default.count_rows() == 100 + assert default.count_rows() == explicit_zero.count_rows() + assert dict(zip(default_data["row_id"], default_data["split_id"])) == dict( + zip(explicit_data["row_id"], explicit_data["split_id"]) + ) + + def test_split_hash_with_discard(mem_db): """Test hash-based splitting with discard weight.""" tbl = mem_db.create_table( diff --git a/python/src/permutation.rs b/python/src/permutation.rs index 4e07d439e..9cb4da247 100644 --- a/python/src/permutation.rs +++ b/python/src/permutation.rs @@ -123,14 +123,15 @@ impl PyAsyncPermutationBuilder { }) } - #[pyo3(signature = (columns, split_weights, *, discard_weight=0, split_names=None))] + #[pyo3(signature = (columns, split_weights, *, discard_weight=None, split_names=None))] pub fn split_hash( slf: PyRefMut<'_, Self>, columns: Vec, split_weights: Vec, - discard_weight: u64, + discard_weight: Option, split_names: Option>, ) -> PyResult { + let discard_weight = discard_weight.unwrap_or(0); slf.modify(|builder| { builder.with_split_strategy( SplitStrategy::Hash {