Skip to content

ipc: add Arrow IPC file transport for inputs and query results - #87

Merged
Xuanwo merged 16 commits into
mainfrom
xuanwo/go-arrow-ipc-c-foundation
Nov 22, 2026
Merged

Xuanwo merged 16 commits into
mainfrom
xuanwo/go-arrow-ipc-c-foundation

Conversation

@Xuanwo

@Xuanwo Xuanwo commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Why

lancedb-go has to run with CGO_ENABLED=0, and Arrow Go's arrow/cdata package is behind a cgo build tag, so the Arrow C Data interface is not usable there. The C ABI also only accepts a single batch as C Data input, and its result conversion drops the projected schema of an empty result and turns a stream error into LANCEDB_UNKNOWN.

What it adds

Six additive entry points. No existing symbol, struct layout, return convention or ownership rule changes.

Symbol Contract
lancedb_query_execute_with_status, lancedb_vector_query_execute_with_status Consume the builder like the existing execute entries and report the SDK status and text instead of a NULL pointer. The existing entries share the implementation and still return a pointer.
lancedb_query_result_to_ipc_file Consumes the result and writes its stream to one Arrow IPC file. Keeps the stream's projected schema, so a result without batches still describes its columns. Batches are written in stream order and zero-row batches stay batches. The bytes are handed over only after the footer is written, so an SDK error after any batch leaves the output empty.
lancedb_table_schema_ipc_file Borrows the table and returns a valid IPC file with the full schema and no batch.
lancedb_record_batch_reader_from_ipc_file Decodes borrowed (bytes, len) into an owned reader over every batch and/or the Arrow C ABI schema. All batches are decoded before the call returns, so a corrupt batch fails before a table can be written. Bad bytes are LANCEDB_INVALID_INPUT with a decoding message.
lancedb_ipc_buffer_free Frees the file bytes of a two-field buffer and zeroes it, so freeing twice is a no-op and the caller never keeps a stale pointer.

The new entries clear their writable outputs and the error message on entry. A NULL pointer with len == 0 is the same empty input as zero bytes; a NULL pointer with bytes is an invalid argument.

Decoding and the preparation of the requested C outputs run inside one panic barrier, and neither output is written unless every requested conversion succeeded. An input whose schema cannot be represented in the C data interface, for example a field name with a NUL byte, is a normal LANCEDB_INVALID_INPUT with both outputs clear. Before the Arrow reader can allocate from file metadata, the decoder rejects a size that cannot describe a buffer, a footer that does not fit the input, and any footer-declared record-batch or dictionary block whose checked offset plus metadata plus body length ends outside the borrowed bytes; valid files are not limited by any new cap. A decoder panic is contained as LANCEDB_INVALID_INPUT instead of unwinding across the C boundary.

The IPC buffer is an anonymous struct typedef, like the other public C structs: a named struct plus typedef makes Doxygen/Breathe register the declaration twice, which fails the docs build.

Dependencies

This branch is merged onto main after the 0.42 SDK upgrade (#78), and keeps main's pins unchanged: lancedb v0.42.0-beta.21 and lance v15.0.0-beta.25 git tags, the Arrow 59 crates, and arrow-ipc = "59.2.0" beside them so the lock resolves 59.3.3 like every other Arrow crate. The vector builder's nprobes call is fallible as on main, and main's SQL predicate handling is preserved: a DataFusion expression wins over the SQL filter on both paths, and the explain paths honor the SQL filter.

Still open and not required:

  • Run C API work on the runtime's worker threads, not the caller's stack #76 (owned runtime dispatch) would apply to the new entries' get_runtime().block_on calls the same way it applies to the existing ones.
  • query: add vector query post-filter #82's vector setter belongs in the shared run_vector_query this PR extracts.
  • The FTS/hybrid branch (xuanwo/c-fts-hybrid-migration, committed on 861a706) adds full_text_search fields, setters and two apply calls in src/query.rs. Whichever side lands second keeps those two apply calls: here they belong in run_plain_query and run_vector_query, which are the single builder implementations both execute entries share.

Validation

Local, at head 74ad4f2 (main 2b2b3df, 0.42.0-beta.21 / Arrow 59.3.3), Arrow C++ 26.0.1, Rust 1.98.1:

  • cargo fmt --check, cargo clippy --lib, cargo check --lib clean; cargo test --lib 4/4 (batch order including a zero-row batch, SDK stream error after valid batches keeping the original error, footer bound, synthetic panic containment).
  • cmake -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release + ninja, then ctest --test-dir build -j4 --output-on-failure: 9/9 suites passed, 7 of them under Valgrind (table and query: ERROR SUMMARY: 0 errors, definitely lost: 0 bytes).
  • cargo test --lib: 6/6, covering batch order with a zero-row batch, a stream error after valid batches, the footer and block-range bounds including checked-overflow cases, the C-schema conversion failure with both outputs clear, and the panic barrier; ninja docs with warnings as errors succeeded at the merge head, whose public header is unchanged since.
  • New Catch2 cases run through the public header only and pass at this head: table 430 assertions, query 5140, vector query 1065. They include the decode guards (short input, footer that does not fit, oversized size, a footer that parses but cannot be read, blocks that end outside the input), a valid file with a dictionary-encoded column that decodes with its encoding intact, a file whose schema the C ABI cannot carry with both outputs left clear, and the null-output branches of both status executors, because the coverage job only runs the C suites and cannot see the internal Rust tests.
  • Coverage of the new decoder, measured with the job's own instrumented Debug build and the same nine suites on this host's toolchain: src/ipc.rs goes from 20 missed lines to 5, and the total from 90.85% to 91.32% (2882/3156). On the CI line set that is about 2801/3069 ≈ 91.3% against the 90.79% baseline. The five lines that remain are the negative-length guard, which a valid footer cannot reach, the panic path, and one ? continuation line.
  • ninja docs (Doxygen + Sphinx with warnings as errors) succeeded at the merge head.
  • The crate also compiles against Upgrade lancedb to 0.42.0-beta.21 #78's head 485081e in a scratch worktree with only the fallible nprobes line adapted.

Hosted CI is running on the new head; the vector projection assertion that failed on the previous head (_distance is added by the SDK after the projected column) is corrected.

A consumer without the Arrow C Data interface, such as a pure Go program
where Arrow Go's cdata package sits behind a cgo build tag, cannot use the
current single-batch input and C Data result conversion. Such a consumer
also cannot see the projected schema of an empty result or the SDK error
text of a query that never started.

Add six additive entry points beside the existing ones:

- lancedb_query_execute_with_status and
  lancedb_vector_query_execute_with_status report the SDK status and text
  instead of a NULL pointer. The existing execute entries share the same
  implementation and keep their pointer-return convention.
- lancedb_query_result_to_ipc_file writes the SDK stream to one Arrow IPC
  file and hands the bytes over only after the footer is written, so a
  stream error after any batch yields no file. The projected schema is
  kept when the result has no batch, and zero-row batches stay batches.
- lancedb_table_schema_ipc_file writes a valid file with the table schema
  and no batch.
- lancedb_record_batch_reader_from_ipc_file decodes a borrowed file into an
  owned reader over every batch and optionally the Arrow C ABI schema. All
  batches are decoded before returning, so a corrupt batch fails before a
  table can be written.
- lancedb_ipc_buffer_free frees the file bytes and zeroes the buffer, so
  freeing twice is a no-op.

The IPC file format and the SDK stream interface are the same on the pinned
0.33 SDK and on the 0.42 upgrade, so this slice does not depend on it.
Decoding borrowed bytes at the C boundary now rejects what it cannot read
before the Arrow reader allocates anything: a size that cannot describe a
Rust slice, an announced footer that does not fit the input, and a NULL
pointer with no bytes, which joins zero bytes as empty input. A panic
inside the decoder is caught at the entry point, so a malformed batch
cannot unwind across the C boundary; it reports LANCEDB_INVALID_INPUT with
a message like any other unreadable file.

Unit tests cover the parts a public C caller cannot reach: batch order
including a zero-row batch, an SDK stream error after valid batches keeping
the original error, the footer bound, and the panic barrier with a
synthetic panic instead of crafted input.

The public vector query file keeps the distance column the SDK adds to a
projection, so its test expects key followed by _distance. Start-error and
invalid-input tests now assert the actual SDK diagnostic instead of only a
non-success status.

The IPC buffer typedef is anonymous, matching the other C structs in the
public header: a named struct and its typedef make Doxygen and Breathe
register the declaration twice, which fails the docs build.
The coverage job showed the new IPC entry points with branches no public
caller reached: a too-short input, a footer larger than the input, a size
that cannot describe a buffer, and the missing result pointer of both
status executors. Each of those is a documented input contract, so cover it
through the public header instead of leaving it to the internal tests.

The remaining uncovered lines are error arms a public caller cannot reach
on purpose (a decoder panic, an SDK stream that fails mid-stream, an Arrow
encoding failure of an SDK-provided schema).
Keep the slice on main's dependency pins: resolve the manifest to the git
lancedb v0.42.0-beta.21 and lance v15.0.0-beta.25 tags with the Arrow 59
crates, and add arrow-ipc 59.2.0 beside them so the lock resolves the same
59.3.3 every other Arrow crate uses.

In src/query.rs keep the shared run_vector_query helper instead of main's
inline copy, and make its nprobes call fallible like main's, so the vector
builder keeps one implementation for both execute entry points. The SQL
predicate handling stays as main has it: a DataFusion expression takes
precedence over the SQL filter on both paths, and the explain paths honor
the SQL filter too.
Decoding already prepared both requested C outputs inside the panic
barrier, but only a unit test proved that an input whose schema cannot be
exported to the C data interface is a normal error. A field name with a NUL
byte is valid Arrow and survives an IPC round trip, while the C data
interface carries names as C strings, so it is the ordinary way such an
input reaches the boundary.

Cover it on both sides: an internal test asserts that the file decodes and
only the C schema conversion fails, and a public test writes such a file
with the Arrow C++ writer and asserts LANCEDB_INVALID_INPUT with both
outputs left clear. The error text now says the file is unusable instead of
claiming it could not be read, which is what happens when only the
conversion fails.
The coverage report for the merged head showed the new decode guard only
partly exercised: the unreadable-footer arm, the dictionary-block loop and
the "ends outside the file" arm never ran, because every test file was a
plain two-batch utf8 file with an intact footer.

Cover them with ordinary inputs through the public header: a file whose
announced footer length fits but whose footer bytes are not a footer, a
valid file with a dictionary-encoded column that must decode with its
dictionary encoding intact, and a file with an intact footer whose middle
was dropped so the blocks it declares no longer fit. The panic message fits
one line now, which removes two instrumented lines without changing where
the panic is contained.

Remaining uncovered lines are the negative-length guard, which a valid
footer cannot reach, and the panic path itself, which only a decoder panic
reaches; both stay covered by internal tests.
The example crate has its own lock, and it did not gain the arrow-ipc edge
that the root manifest now declares, so `cargo metadata --locked` there
would have to rewrite the lock. Add the edge and keep every pinned version
as it was.
The optional outputs are already covered for a plain file; the dictionary
file now also asks for the reader alone and the schema alone, so the
one-at-a-time contract is exercised with a non-trivial type.
The packaging matrix keeps the targets whose artifacts the release matrix
consumes: Linux amd64/arm64, macOS arm64 and Windows amd64/arm64. The
Intel runner entry only built darwin-amd64 and is removed here so the
packaging workflow no longer needs an Intel-specific runner.
…slice

Two conflicts:
- src/query.rs kept the shared run_vector_query instead of main's inline
  copy, and gained main's post-filter call there. The SQL filter still
  yields to a DataFusion expression and nprobes stays fallible.
- tests/test_table.cpp kept both sides: the Arrow IPC input case and the
  schema-evolution helpers and case from main.

Everything else merged cleanly, including the raw enum entry validation,
the vector post-filter setter and explain path, and the history, tag and
index listing APIs. Dependency pins and both locks are unchanged.
src/query.rs kept one plain and one vector executor for both the legacy and
the status/IPC entry points. The full-text search from main is applied in
those shared executors, next to the post-filter, the checked distance and
the SQL filter that still yields to a DataFusion expression; probes stay
fallible. The imports merge both sides. The hybrid EXPLAIN caveat and the
index statistics arrive unchanged from main.

tests/test_fts.cpp gains one focused case: the plain full-text result and
the fused hybrid result go through the status executors and the IPC file,
reusing the existing FTS fixture and the shared IPC reader instead of
duplicating the broad search tests.

Everything else merged cleanly, including the C17 consumer and the FTS test
registration. Dependency pins and both locks are unchanged.
@Xuanwo
Xuanwo merged commit 94e84c2 into main Nov 22, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant