mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
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.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user