diff --git a/docs/web-tests/py/test_ingestion_examples.py b/docs/web-tests/py/test_ingestion_examples.py index 81a853ee0..d686d3387 100644 --- a/docs/web-tests/py/test_ingestion_examples.py +++ b/docs/web-tests/py/test_ingestion_examples.py @@ -1,6 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright The LanceDB Authors +import pytest + def test_create_table_from_dicts(): from tempfile import TemporaryDirectory @@ -361,6 +363,45 @@ def test_create_table_from_iterator(): assert table.count_rows() == 10 +def test_add_from_record_batch_reader(tmp_db): + from pyarrow import record_batch + + batches = [record_batch({"id": [1, 2]}), record_batch({"id": [3, 4]})] + table = tmp_db.create_table("sync_events", schema=batches[0].schema) + previous_version = table.version + + # --8<-- [start:add_from_record_batch_reader] + from pyarrow import RecordBatchReader + + reader = RecordBatchReader.from_batches(table.schema, batches) + result = table.add(reader) + print("Committed version", result.version) + # --8<-- [end:add_from_record_batch_reader] + + assert table.version == result.version == previous_version + 1 + assert table.count_rows() == 4 + + +@pytest.mark.asyncio +async def test_add_from_record_batch_reader_async(mem_db_async): + from pyarrow import record_batch + + batches = [record_batch({"id": [1, 2]}), record_batch({"id": [3, 4]})] + table = await mem_db_async.create_table("async_events", schema=batches[0].schema) + previous_version = await table.version() + + # --8<-- [start:add_from_record_batch_reader_async] + from pyarrow import RecordBatchReader + + reader = RecordBatchReader.from_batches(await table.schema(), batches) + result = await table.add(reader) + print("Committed version", result.version) + # --8<-- [end:add_from_record_batch_reader_async] + + assert await table.version() == result.version == previous_version + 1 + assert await table.count_rows() == 4 + + def test_update_operation(): from tempfile import TemporaryDirectory diff --git a/docs/web/ingestion/insert.mdx b/docs/web/ingestion/insert.mdx index 7633584c6..c05b0be3e 100644 --- a/docs/web/ingestion/insert.mdx +++ b/docs/web/ingestion/insert.mdx @@ -14,6 +14,8 @@ import { } from '/snippets/tables.mdx'; import { PyAddFromDataset as AddFromDataset, + PyAddFromRecordBatchReader, + PyAddFromRecordBatchReaderAsync, PyCreateTableFromIterator as CreateTableFromIterator, } from '/snippets/ingestion_examples.mdx'; @@ -89,6 +91,26 @@ The language APIs differ: the TypeScript example appends each constructed Arrow batch, while the Rust example supplies an Arrow reader when creating the table. These are input-format examples, not equivalent parallel-write benchmarks. +### Append an Arrow batch reader {#from-record-batch-reader} + +Pass a `pyarrow.RecordBatchReader` to append multiple batches in one Python +`add()` call. A successful append commits the reader's batches together as one +table version. If each flush needs its own commit acknowledgment, call `add()` +separately for each flush and wait for it to succeed. + +Each example uses an open table and an iterable of `pyarrow.RecordBatch` objects +that match the table's schema. For LanceDB Enterprise, check the +[multipart upload requirements](#bulk-ingests-into-a-remote-table). + + + +{PyAddFromRecordBatchReader} + + +{PyAddFromRecordBatchReaderAsync} + + + ### Control write parallelism {#write-parallelism} Python's `add()` can estimate parallelism for materialized inputs. A streaming @@ -199,12 +221,18 @@ retry budget never makes ambiguous commits safe to replay. This section applies only to LanceDB Enterprise tables (`db://` connections). LanceDB Embedded writes are unaffected. -Large measured inputs, unknown-length streams, and explicit parallel uploads use -multipart writes when the server supports them. The client slices large batches -before IPC encoding and buffers each part for replay. A disconnected request, -read timeout, or configured retryable staging status retries exactly the same -upload ID, part ID, and IPC bytes. `RetryConfig.statuses` controls status retries; -an empty list disables them. The default includes `500`, `502`, `503`, and `504`. +With server protocol `0.4.0` or later, the client uses multipart writes for large +measured inputs, unknown-length streams such as a non-empty Python +`RecordBatchReader`, and explicit parallel uploads. Multipart writes require a +supporting catalog, such as a native directory or manifest catalog. Federated +catalogs reject multipart writes. With an older protocol, the client sends the +append in a single insert request, subject to the server's request limits. + +The client slices large batches before IPC encoding and buffers each part for +replay. On a disconnected request, read timeout, or configured retryable staging +status, the client retries the part with the same upload ID, part ID, and IPC +bytes. `RetryConfig.statuses` controls status retries. An empty list disables +them. The default includes `500`, `502`, `503`, and `504`. The server replaces that part's staged transaction, so losing its response does not append the rows twice. Exhausting the part retry budget aborts the uncommitted session; it does not restart the entire upload. diff --git a/docs/web/snippets/ingestion_examples.mdx b/docs/web/snippets/ingestion_examples.mdx index f5e75db0a..a4b6c2e5c 100644 --- a/docs/web/snippets/ingestion_examples.mdx +++ b/docs/web/snippets/ingestion_examples.mdx @@ -2,6 +2,10 @@ export const PyAddFromDataset = "import pyarrow as pa\nimport pyarrow.parquet as pq\n\ndata_path = \"input.parquet\"\npq.write_table(pa.table({\"id\": [1, 2], \"name\": [\"Alice\", \"Bob\"]}), data_path)\nimport pyarrow.dataset as ds\n\ndataset = ds.dataset(data_path, format=\"parquet\")\ntable = db.create_table(\"my_table\", schema=dataset.schema, mode=\"overwrite\")\ntable.add(dataset)\n\nprint(\"Rows:\", table.count_rows())\n"; +export const PyAddFromRecordBatchReader = "from pyarrow import RecordBatchReader\n\nreader = RecordBatchReader.from_batches(table.schema, batches)\nresult = table.add(reader)\nprint(\"Committed version\", result.version)\n"; + +export const PyAddFromRecordBatchReaderAsync = "from pyarrow import RecordBatchReader\n\nreader = RecordBatchReader.from_batches(await table.schema(), batches)\nresult = await table.add(reader)\nprint(\"Committed version\", result.version)\n"; + export const PyAddProgress = "from tempfile import TemporaryDirectory\n\nimport lancedb\n\ndef report_progress(progress):\n print(f\"Rows written: {progress['output_rows']}\")\n\nwith TemporaryDirectory(prefix=\"lancedb-ingestion-\") as uri:\n db = lancedb.connect(uri)\n table = db.create_table(\"users\", [{\"id\": 1, \"name\": \"Alice\"}])\n table.add(\n [{\"id\": 2, \"name\": \"Bob\"}, {\"id\": 3, \"name\": \"Charlie\"}],\n progress=report_progress,\n )\n rows = sorted(table.search().to_list(), key=lambda row: row[\"id\"])\n print(\"After insertion:\", rows)\n"; export const PyAppendRows = "from tempfile import TemporaryDirectory\n\nimport lancedb\n\nwith TemporaryDirectory(prefix=\"lancedb-ingestion-\") as uri:\n db = lancedb.connect(uri)\n table = db.create_table(\"users\", [{\"id\": 1, \"name\": \"Alice\"}])\n table.add([{\"id\": 2, \"name\": \"Bob\"}, {\"id\": 3, \"name\": \"Charlie\"}])\n rows = sorted(table.search().to_list(), key=lambda row: row[\"id\"])\n print(rows)\n"; diff --git a/python/python/lancedb/remote/table.py b/python/python/lancedb/remote/table.py index 1d462f082..013ec3826 100644 --- a/python/python/lancedb/remote/table.py +++ b/python/python/lancedb/remote/table.py @@ -777,7 +777,7 @@ class RemoteTable(Table): - pandas.DataFrame - - pyarrow.Table or pyarrow.RecordBatch + - pyarrow.Table, pyarrow.RecordBatch, or pyarrow.RecordBatchReader mode: str The mode to use when writing the data. Valid values are "append" and "overwrite". diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 3303024cf..9b3cabbf6 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -2594,7 +2594,7 @@ class Table(ABC): - pandas.DataFrame - - pyarrow.Table or pyarrow.RecordBatch + - pyarrow.Table, pyarrow.RecordBatch, or pyarrow.RecordBatchReader mode: str The mode to use when writing the data. Valid values are "append" and "overwrite". Overwrite adopts explicitly declared blob @@ -8545,7 +8545,7 @@ class AsyncTable: - pandas.DataFrame - - pyarrow.Table or pyarrow.RecordBatch + - pyarrow.Table, pyarrow.RecordBatch, or pyarrow.RecordBatchReader mode: str The mode to use when writing the data. Valid values are "append" and "overwrite". Overwrite adopts explicitly declared blob