Skip to content

query: support Arrow C Data Stream (ArrowArrayStream) for streaming reads and writes #129

Open

Description

@Xuanwo

Context & SDK Reference

Currently lancedb-c materializes entire datasets in memory on both read and write boundaries:

  • Read path (src/query.rs): lancedb_query_result_to_arrow collects the entire SendableRecordBatchStream into Vec<RecordBatch> via try_collect(), and lancedb_query_result_to_ipc_file buffers a complete Arrow IPC file in memory.
  • Write path (src/table.rs): lancedb_record_batch_reader_from_arrow takes an already-materialized array of FFI_ArrowArray* batches.

For large tables, full-table scans, exports, and streaming ingest (in both C/C++ and lancedb-go), buffering all batches in memory causes unnecessary peak memory usage and OOM risk.

arrow-array::ffi_stream::FFI_ArrowArrayStream (already available in the pinned arrow-array crate) provides standard zero-copy Arrow C Stream Interface (struct ArrowArrayStream) bridging in both directions.

Proposed C API Scope

  • Streaming read: Add lancedb_query_result_to_arrow_stream(LanceDBQueryResult* result, struct ArrowArrayStream* stream_out, char** error_message) (or returning an allocated stream handle) that wraps SendableRecordBatchStream into an FFI_ArrowArrayStream, pulling batches on demand from the Tokio runtime and propagating stream errors via ArrowArrayStream.get_last_error.
  • Streaming write: Add lancedb_record_batch_reader_from_arrow_stream(struct ArrowArrayStream* stream, LanceDBRecordBatchReader** reader_out, char** error_message) that consumes an Arrow C Stream (ArrowArrayStreamReader) so create_table, add, and merge_insert can ingest streams lazily without buffering all batches upfront.
  • Declare struct ArrowArrayStream in include/lancedb.h guarded by #ifndef ARROW_C_STREAM_INTERFACE (matching the existing #ifndef ARROW_C_DATA_INTERFACE pattern for ArrowSchema / ArrowArray).

Non-Goals

  • Removing existing lancedb_query_result_to_arrow, lancedb_query_result_to_ipc_file, or lancedb_record_batch_reader_from_arrow APIs.

Acceptance Criteria

  • Both ArrowArrayStream export (query result -> C consumer) and import (C producer -> create_table / add / merge_insert) work end-to-end through include/lancedb.h.
  • Stream errors from the Rust async stream are surfaced through ArrowArrayStream::get_last_error, and early consumer release (stream->release) cleanly drops the underlying Rust stream without leaking memory or hanging runtime tasks.
  • C++ Catch2 tests (using Arrow C++ ImportRecordBatchReader / ExportRecordBatchReader) verify multi-batch streaming reads, streaming writes, early termination, and error propagation under valgrind.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions