mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(python): return optimize stats from sync table and forward compaction options (#4830)
## Summary This PR fixes return value and argument forwarding inconsistencies in the Python `Table.optimize()` implementation: 1. `LanceTable.optimize()` now returns `OptimizeStats` produced by the underlying optimization process, matching `AsyncTable.optimize()` behavior. Previously, the return value was dropped by `LOOP.run(...)`, causing `table.optimize()` to always return `None`. 2. `Table.optimize` (abstract base class), `LanceTable.optimize`, and `RemoteTable.optimize` now annotate `-> OptimizeStats` return types. 3. `RemoteTable.optimize()` now properly forwards `compaction_options` to the underlying table optimize handle. Previously, `compaction_options` was accepted in the method signature but omitted in the delegation call. Refs #4397. ## Root Cause & Problem - In `python/python/lancedb/table.py`, `LanceTable.optimize()` called `LOOP.run(self._table._do_optimize(...))` as a standalone statement without a `return`. Because `LOOP.run` returns the result of the coroutine, callers of synchronous `table.optimize()` received `None` even though compaction and prune statistics (`OptimizeStats`) were generated. - In `python/python/lancedb/remote/table.py`, `RemoteTable.optimize()` accepted `compaction_options: Optional[CompactionOptions] = None` in its method definition, but the call to `self._table.optimize()` omitted `compaction_options=compaction_options`. - In `Table` (ABC), `optimize()` was missing return type annotations, preventing type checkers from inferring `OptimizeStats`. ## Technical Solution - Updated `LanceTable.optimize()` in `python/python/lancedb/table.py` to `return LOOP.run(...)` and added the return type annotation `-> OptimizeStats:`. - Added `-> OptimizeStats:` return type annotation to `Table.optimize()` in `python/python/lancedb/table.py`. - Updated `RemoteTable.optimize()` in `python/python/lancedb/remote/table.py` to forward `compaction_options=compaction_options` to `self._table.optimize(...)` and added `-> OptimizeStats:` return type annotation. - Added `OptimizeStats` to `TYPE_CHECKING` imports in `python/python/lancedb/remote/table.py`. - Added regression tests in `test_table_signatures.py` to verify argument forwarding (`compaction_options`) and stats return on both local and remote tables. - Updated `test_sync_optimize` and `test_sync_optimize_in_async` in `test_table.py` to assert that non-None `OptimizeStats` containing compaction and prune stats are returned. ## Test Plan - Focused signature and argument forwarding test suite: Ran `pytest python/tests/test_table_signatures.py` (40 passed in 5.05s). - Focused sync and async optimize test suite: Ran `pytest python/tests/test_table.py -k "test_sync_optimize"` (2 passed in 3.30s). Ran `pytest python/tests/test_table.py -k "test_optimize"` (25 passed in 7.69s). - Linting and formatting: Ran `ruff check` on modified files (passed with 0 errors). Ran `ruff format --check` on modified files (passed with 4 files formatted).
This commit is contained in:
@@ -94,6 +94,7 @@ from ..util import infer_vector_column_name
|
||||
if TYPE_CHECKING:
|
||||
import lance
|
||||
import polars as pl
|
||||
from .._lancedb import OptimizeStats
|
||||
|
||||
|
||||
def _deprecated_keyword(old_name: str, new_name: str):
|
||||
@@ -1172,7 +1173,7 @@ class RemoteTable(Table):
|
||||
delete_unverified: bool = False,
|
||||
retrain: bool = False,
|
||||
compaction_options: Optional[CompactionOptions] = None,
|
||||
):
|
||||
) -> OptimizeStats:
|
||||
"""Compact the table's data files via a durable, asynchronous
|
||||
Enterprise job. Pruning old versions and index optimization are not
|
||||
yet supported remotely, so this call skips them and returns
|
||||
|
||||
@@ -3433,7 +3433,7 @@ class Table(ABC):
|
||||
delete_unverified: bool = False,
|
||||
retrain: bool = False,
|
||||
compaction_options: Optional[CompactionOptions] = None,
|
||||
):
|
||||
) -> OptimizeStats:
|
||||
"""
|
||||
Optimize the on-disk data and indices for better performance.
|
||||
|
||||
@@ -3493,6 +3493,15 @@ class Table(ABC):
|
||||
passes. This does not cap the first pass over an existing oversized
|
||||
fragment; see [CompactionOptions][lancedb.table.CompactionOptions].
|
||||
|
||||
Returns
|
||||
-------
|
||||
OptimizeStats
|
||||
The statistics of the optimization operation, containing:
|
||||
- compaction: Statistics from file compaction (fragments and
|
||||
files removed/added).
|
||||
- prune: Statistics from pruning old versions (bytes and old
|
||||
versions removed).
|
||||
|
||||
Notes
|
||||
-----
|
||||
The frequency an application should call optimize is based on the frequency of
|
||||
@@ -6621,7 +6630,7 @@ class LanceTable(Table):
|
||||
delete_unverified: bool = False,
|
||||
retrain: bool = False,
|
||||
compaction_options: Optional[CompactionOptions] = None,
|
||||
):
|
||||
) -> OptimizeStats:
|
||||
"""
|
||||
Optimize the on-disk data and indices for better performance.
|
||||
|
||||
@@ -6666,6 +6675,15 @@ class LanceTable(Table):
|
||||
passes. This does not cap the first pass over an existing oversized
|
||||
fragment; see [CompactionOptions][lancedb.table.CompactionOptions].
|
||||
|
||||
Returns
|
||||
-------
|
||||
OptimizeStats
|
||||
The statistics of the optimization operation, containing:
|
||||
- compaction: Statistics from file compaction (fragments and
|
||||
files removed/added).
|
||||
- prune: Statistics from pruning old versions (bytes and old
|
||||
versions removed).
|
||||
|
||||
Notes
|
||||
-----
|
||||
The frequency an application should call optimize is based on the frequency of
|
||||
@@ -6674,7 +6692,7 @@ class LanceTable(Table):
|
||||
you have added or modified 100,000 or more records or run more than 20 data
|
||||
modification operations.
|
||||
"""
|
||||
LOOP.run(
|
||||
return LOOP.run(
|
||||
self._table._do_optimize(
|
||||
_optimize_cleanup_since_ms(cleanup_older_than, retrain),
|
||||
delete_unverified,
|
||||
@@ -10667,6 +10685,15 @@ class AsyncTable:
|
||||
passes. This does not cap the first pass over an existing oversized
|
||||
fragment; see [CompactionOptions][lancedb.table.CompactionOptions].
|
||||
|
||||
Returns
|
||||
-------
|
||||
OptimizeStats
|
||||
The statistics of the optimization operation, containing:
|
||||
- compaction: Statistics from file compaction (fragments and
|
||||
files removed/added).
|
||||
- prune: Statistics from pruning old versions (bytes and old
|
||||
versions removed).
|
||||
|
||||
Notes
|
||||
-----
|
||||
The frequency an application should call optimize is based on the frequency of
|
||||
|
||||
@@ -6593,7 +6593,10 @@ def test_sync_optimize(mem_db: DBConnection):
|
||||
|
||||
table.add([{"vector": [2.0, 2.0], "item": "baz", "price": 30.0}])
|
||||
assert table.count_rows() == 3
|
||||
table.optimize()
|
||||
opt_stats = table.optimize()
|
||||
assert opt_stats is not None
|
||||
assert opt_stats.compaction is not None
|
||||
assert opt_stats.prune is not None
|
||||
stats = table.index_stats("price_idx")
|
||||
assert stats["num_indexed_rows"] == 3
|
||||
|
||||
@@ -6614,7 +6617,10 @@ async def test_sync_optimize_in_async(mem_db: DBConnection):
|
||||
|
||||
table.add([{"vector": [2.0, 2.0], "item": "baz", "price": 30.0}])
|
||||
assert table.count_rows() == 3
|
||||
table.optimize()
|
||||
opt_stats = table.optimize()
|
||||
assert opt_stats is not None
|
||||
assert opt_stats.compaction is not None
|
||||
assert opt_stats.prune is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -193,16 +193,51 @@ def test_remote_optimize_options():
|
||||
handle.optimize = AsyncMock(side_effect=NotImplementedError("remote optimize"))
|
||||
table = RemoteTable(handle, "test_db")
|
||||
age = timedelta(days=1)
|
||||
compaction_options = MagicMock()
|
||||
with pytest.raises(NotImplementedError, match="remote optimize"):
|
||||
table.optimize(cleanup_older_than=age, delete_unverified=True, retrain=True)
|
||||
table.optimize(
|
||||
cleanup_older_than=age,
|
||||
delete_unverified=True,
|
||||
retrain=True,
|
||||
compaction_options=compaction_options,
|
||||
)
|
||||
handle.optimize.assert_awaited_once_with(
|
||||
cleanup_older_than=age,
|
||||
delete_unverified=True,
|
||||
retrain=True,
|
||||
compaction_options=None,
|
||||
compaction_options=compaction_options,
|
||||
)
|
||||
|
||||
|
||||
def test_sync_table_optimize_returns_stats():
|
||||
# Test LanceTable returns result from _do_optimize
|
||||
handle = MagicMock()
|
||||
expected_stats = MagicMock()
|
||||
handle._do_optimize = AsyncMock(return_value=expected_stats)
|
||||
lance_table = LanceTable.__new__(LanceTable)
|
||||
lance_table._table = handle
|
||||
|
||||
stats = lance_table.optimize()
|
||||
handle._do_optimize.assert_awaited_once()
|
||||
assert stats is expected_stats
|
||||
|
||||
# Test RemoteTable returns result from optimize and forwards compaction_options
|
||||
remote_handle = MagicMock()
|
||||
remote_stats = MagicMock()
|
||||
remote_handle.optimize = AsyncMock(return_value=remote_stats)
|
||||
remote_table = RemoteTable(remote_handle, "test_db")
|
||||
opts = MagicMock()
|
||||
|
||||
stats = remote_table.optimize(compaction_options=opts)
|
||||
remote_handle.optimize.assert_awaited_once_with(
|
||||
cleanup_older_than=None,
|
||||
delete_unverified=False,
|
||||
retrain=False,
|
||||
compaction_options=opts,
|
||||
)
|
||||
assert stats is remote_stats
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"method, old, new, value",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user