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:
Xuanwo
2026-11-20 20:20:34 +08:00
committed by GitHub
parent 4ac2728e73
commit a9be4525a0
3 changed files with 44 additions and 3 deletions
+35
View File
@@ -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(