Skip to content

feat(napi): copy-into-cage Arrow result handoff mode - #156

Closed
msrathore-db wants to merge 1 commit into
mainfrom
msrathore/copycage-final
Closed

msrathore-db wants to merge 1 commit into
mainfrom
msrathore/copycage-final

Conversation

@msrathore-db

Copy link
Copy Markdown
Collaborator

What

Adds a third result-handoff path to the napi binding alongside the default Arrow-IPC encode: copy-into-cage.

Instead of re-serialising each RecordBatch to an Arrow IPC stream (StreamWriter → Vec<u8> → JS RecordBatchReader decode — three O(result-size) passes), copy-into-cage allocates a V8-owned ArrayBuffer per Arrow buffer (napi_create_arraybuffer, inside the V8 heap / "cage") and memcpys the bytes in, plus a small descriptor tree. The JS side rebuilds the RecordBatch via apache-arrow's makeData.

Why copy-into-cage (vs borrowed/external zero-copy)

  • Cage-safe — works under the V8 sandbox / Electron, where external (off-heap) ArrayBuffers are rejected.
  • Finalizer-free — V8 owns the bytes, so there's no Arc/finalizer lifetime coupling to native memory and no use-after-free or mutable-aliasing surface.
  • It costs one memcpy per buffer but still skips IPC's serialize and JS-decode passes. On a 26.7 GiB TPC-DS result, the per-step handoff cost is ~half of IPC's (one pass vs ~two: serialize + copy).

Surface

  • Statement.fetchNextBatchCopycage() and AsyncResultHandle.fetchNextBatchCopycage() (both fetch paths).
  • Returns a descriptor { numRows, columns: [{ length, offset, nullCount, validity?, buffers, children }] } whose shape is identical to what the IPC path decodes to, so the driver importer is the only consumer that needs to know the difference.
  • offset != 0 is compacted to offset-0 via concat_batches (defensive; arrow-rs folds offset into buffers at to_data()).
  • Adds the arrow-select dependency for concat_batches.

Opt-in: the driver selects the mode; the default fetchNextBatch (IPC) path is unchanged.

Verification

  • Byte-identical to the IPC path across all 21 Databricks type families + edge cases (empty/all-null/empty-array/map/empty-string, NaN/±Inf/-0, decimal incl. 38-digit & negative, interval, variant, deeply-nested).
  • Multi-batch 5,000,000-row integrity (exact count + sum + order); concurrency (overlapping + identical statements).
  • cargo fmt + clippy --all-targets clean; cargo test green.

This pull request and its description were written by Isaac.

Adds a third result-handoff path to the napi binding alongside the
default IPC encode: "copy-into-cage". Instead of re-serialising each
RecordBatch to an Arrow IPC stream (StreamWriter -> Vec<u8> -> JS
RecordBatchReader decode, three O(result-size) passes), it allocates a
V8-owned ArrayBuffer per Arrow buffer (napi_create_arraybuffer, in the
V8 heap/"cage") and memcpy's the bytes in, plus a small descriptor tree.
The JS side rebuilds the RecordBatch via apache-arrow's makeData.

Why copy-into-cage (vs borrowed/external zero-copy): it is cage-safe
(works under the V8 sandbox / Electron, where external buffers are
rejected), finalizer-free (V8 owns the bytes; no Arc lifetime coupling
to native memory), and has no use-after-free / mutable-aliasing surface.
It costs one memcpy per buffer but still skips IPC's serialize and
JS-decode passes — on a 26.7 GiB TPC-DS result its handoff is ~half of
IPC's (one pass vs ~two).

Surface:
- Statement.fetchNextBatchCopycage() and AsyncResultHandle
  .fetchNextBatchCopycage() (both fetch paths), returning a descriptor
  { numRows, columns: [{ length, offset, nullCount, validity?, buffers,
  children }] } whose shape is identical to what the IPC path would
  decode to.
- offset!=0 is compacted via concat_batches (defensive; arrow-rs folds
  offset into buffers at to_data()).
- arrow-select dependency added for concat_batches.

Opt-in: the driver selects it; the default path is unchanged IPC.
Byte-identical to the IPC path across all tested types + edge cases.

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
@github-actions

Copy link
Copy Markdown

This PR has been marked as Stale because it has been open for 30 days with no activity. If you would like the PR to remain open, please remove the stale label or comment on the PR.

@github-actions github-actions Bot added the Stale label Jul 23, 2026
@github-actions

Copy link
Copy Markdown

This PR was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions Bot closed this Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant