refactor(sdk): share Optimize submission and option conversion

This commit is contained in:
ChilePiquin
2026-11-19 15:40:44 -08:00
parent d4a3683aa9
commit 1a4af3d7d9
11 changed files with 202 additions and 114 deletions
+3 -3
View File
@@ -3163,7 +3163,7 @@ class Table(ABC):
"""
def optimize_async(self, **options: Unpack[OptimizeOptions]) -> Job[OptimizeResult]:
"""Submit remote compaction and return a durable job after acceptance.
"""Submit remote Optimize and return a durable job after acceptance.
Uses SQL OPTIMIZE with the table handle's branch and namespace. Supports
the four per-job settings in
@@ -3185,7 +3185,7 @@ class Table(ABC):
>>> result = job.wait() # doctest: +SKIP
>>> print(result.rows_succeeded) # doctest: +SKIP
"""
raise NotImplementedError("compaction jobs are only supported on remote tables")
raise NotImplementedError("Optimize jobs are only supported on remote tables")
@abstractmethod
def optimize(self, **options: Unpack[OptimizeOptions]) -> OptimizeStats:
@@ -9579,7 +9579,7 @@ class AsyncTable:
async def optimize_async(
self, **options: Unpack[OptimizeOptions]
) -> AsyncJob[OptimizeResult]:
"""Submit remote compaction; await the returned job for actual results.
"""Submit remote Optimize; await the returned job for actual results.
See [Table.optimize_async][lancedb.table.Table.optimize_async] for options,
permissions, completion semantics and compatibility requirements.
+26 -15
View File
@@ -7543,6 +7543,7 @@ def test_remote_function_registration_transports_max_concurrency():
@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["optimize", "optimize_async"])
@pytest.mark.parametrize("asynchronous", [False, True])
@pytest.mark.parametrize(
"options,sql_options",
@@ -7573,7 +7574,7 @@ def test_remote_function_registration_transports_max_concurrency():
],
)
async def test_remote_optimize_forwards_per_job_data_options(
asynchronous, options, sql_options
asynchronous, method, options, sql_options
):
flight = pytest.importorskip("pyarrow.flight")
@@ -7584,25 +7585,26 @@ async def test_remote_optimize_forwards_per_job_data_options(
def do_get(self, context, ticket):
self.tickets.append(ticket.ticket)
return flight.RecordBatchStream(
pa.table({"ok": pa.array([], type=pa.bool_())})
)
return flight.RecordBatchStream(pa.table({"job_id": ["optimize-job"]}))
server = AckServer()
with _mock_remote_sql_connection(server) as kwargs:
if asynchronous:
db = await lancedb.connect_async(**kwargs)
table = await db.create_table("test", [{"id": 1}])
stats = await table.optimize(**options)
stats = await getattr(table, method)(**options)
else:
table = lancedb.connect(**kwargs).create_table("test", [{"id": 1}])
stats = table.optimize(**options)
stats = getattr(table, method)(**options)
assert len(server.tickets) == 1
assert b"OPTIMIZE" in server.tickets[0]
assert all(option in server.tickets[0] for option in sql_options)
if not sql_options:
assert b"WITH (" not in server.tickets[0]
assert stats.prune.bytes_removed == 0
if method == "optimize":
assert stats.prune.bytes_removed == 0
else:
assert stats.id == "optimize-job"
def test_remote_optimize_does_not_drop_options_after_server_rejection():
@@ -7646,9 +7648,7 @@ def test_remote_optimize_skips_prune_and_index():
def do_get(self, context, ticket):
self.tickets.append(ticket.ticket)
return flight.RecordBatchStream(
pa.table({"ok": pa.array([], type=pa.bool_())})
)
return flight.RecordBatchStream(pa.table({"job_id": ["optimize-job"]}))
server = AckServer()
with _mock_remote_table_with_sql(server) as table:
@@ -8523,8 +8523,13 @@ async def test_remote_compaction_job_result_and_reopen(asynchronous, outcome):
assert b"target_rows_per_fragment = 100" in server.tickets[0]
@pytest.mark.asyncio
@pytest.mark.parametrize("asynchronous", [False, True])
@pytest.mark.parametrize("method", ["optimize", "optimize_async"])
@pytest.mark.parametrize("ids", [[], [None], [""], ["a", "b"]])
def test_remote_compaction_rejects_ambiguous_receipt_without_resubmission(ids):
async def test_remote_optimize_rejects_ambiguous_receipt_without_resubmission(
asynchronous, method, ids
):
flight = pytest.importorskip("pyarrow.flight")
class Server(flight.FlightServerBase):
@@ -8540,10 +8545,16 @@ def test_remote_compaction_rejects_ambiguous_receipt_without_resubmission(ids):
server = Server()
with _mock_remote_sql_connection(server) as kwargs:
db = lancedb.connect(**kwargs)
table = db.create_table("test", [{"id": 1}])
with pytest.raises(Exception, match="inspect jobs before retrying"):
table.optimize_async()
if asynchronous:
db = await lancedb.connect_async(**kwargs)
table = await db.create_table("test", [{"id": 1}])
with pytest.raises(Exception, match="inspect jobs before retrying"):
await getattr(table, method)()
else:
db = lancedb.connect(**kwargs)
table = db.create_table("test", [{"id": 1}])
with pytest.raises(Exception, match="inspect jobs before retrying"):
getattr(table, method)()
assert server.calls == 1