diff --git a/python/python/lancedb/remote/table.py b/python/python/lancedb/remote/table.py index 013ec3826..72cf1f124 100644 --- a/python/python/lancedb/remote/table.py +++ b/python/python/lancedb/remote/table.py @@ -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 diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 14aa3f30b..eb98bc72a 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -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 diff --git a/python/python/tests/test_table.py b/python/python/tests/test_table.py index 5997092ce..d8ffbc4b7 100644 --- a/python/python/tests/test_table.py +++ b/python/python/tests/test_table.py @@ -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 diff --git a/python/python/tests/test_table_signatures.py b/python/python/tests/test_table_signatures.py index 41a688068..8da760edb 100644 --- a/python/python/tests/test_table_signatures.py +++ b/python/python/tests/test_table_signatures.py @@ -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", [