diff --git a/napi/Cargo.lock b/napi/Cargo.lock index 8ce37f53..65bbf5e7 100644 --- a/napi/Cargo.lock +++ b/napi/Cargo.lock @@ -410,8 +410,11 @@ name = "databricks-sql-kernel-napi" version = "0.2.0" dependencies = [ "arrow-array", + "arrow-buffer", + "arrow-data", "arrow-ipc", "arrow-schema", + "arrow-select", "chrono", "databricks-sql-kernel", "futures", diff --git a/napi/Cargo.toml b/napi/Cargo.toml index b046bec2..61c99fcd 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -102,6 +102,19 @@ futures = { version = "0.3", default-features = false, features = ["std"] } arrow-array = "57" arrow-schema = "57" arrow-ipc = "57" +# Copy-into-cage result handoff (`zerocopy.rs`): we read each `RecordBatch`'s +# `ArrayData` buffer tree and copy each buffer into a fresh in-cage +# (V8-allocated) napi `ArrayBuffer`. `arrow-data` names `ArrayData`, +# `arrow-buffer` names `Buffer` / `NullBuffer` / `MutableBuffer`. Both arrive +# transitively via `arrow-array`; declared explicitly (same `57` line) so we +# can name the types directly without leaning on a re-export. +arrow-data = "57" +arrow-buffer = "57" +# `arrow-select::concat::concat_batches` compacts a sliced (non-zero-offset) +# batch into an offset-0 copy before the copycage handoff — the JS importer's +# `makeData` cannot apply a logical element offset, so the kernel normalises +# offsets to 0 here. Same `57` line as the rest of the arrow deps. +arrow-select = "57" # Error envelope serialization. Already a kernel transitive dep; the # direct declaration is so we can use `serde_json::json!` in diff --git a/napi/index.d.ts b/napi/index.d.ts index 0b042121..d329f560 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -669,6 +669,21 @@ export declare class AsyncResultHandle { * `Statement.fetchNextBatch()` payload for the same query. */ fetchNextBatch(): Promise + /** + * Copy-into-cage variant of `fetchNextBatch` (Flavour A): hands the + * batch's Arrow buffers to JS as in-cage (V8-allocated) `ArrayBuffer`s + * with the bytes `memcpy`'d in, instead of serialising to Arrow IPC + * bytes. Mirrors `Statement::fetch_next_batch_copycage` exactly — this + * is the handle the *async query path* (`awaitResult()`) exposes, so + * the driver's normal `executeStatement` flow can use copycage rather + * than silently falling back to IPC. See `crate::zerocopy::CopyCageBatch`. + * + * Any offset-sliced array is compacted to offset 0 first + * (`zerocopy::compact_to_offset_zero`); the JS importer's `makeData` + * cannot apply a logical element offset. Returns `null` at end of + * stream, exactly like `fetchNextBatch`. + */ + fetchNextBatchCopycage(): Promise<{ numRows: number, columns: any[] } | null> /** * Result schema as an Arrow IPC payload (schema header only, * no record-batch message). Available before any batches have @@ -1007,6 +1022,30 @@ export declare class Statement { * succeed or fail consistently. */ fetchNextBatch(): Promise + /** + * Copy-into-cage variant of `fetchNextBatch` (Flavour A): hands the + * batch's Arrow buffers to JS as in-cage (V8-allocated) `ArrayBuffer`s + * with the bytes `memcpy`'d in, instead of serialising to Arrow IPC + * bytes (`fetchNextBatch`). See `crate::zerocopy::CopyCageBatch`. + * + * Cage-safe (works under the V8 sandbox / Electron) and GC-clean — V8 + * owns the bytes, so no Arc/finalizer is needed. Costs one plain copy + * per buffer but still avoids the IPC path's serialize + decode passes. + * + * Any offset-sliced array is compacted to offset 0 first + * (`zerocopy::compact_to_offset_zero`), because the JS importer's + * `makeData` cannot apply a logical element offset. + * + * Returns `null` at end of stream, exactly like `fetchNextBatch`. Same + * error/auto-close/finished semantics — only the result encoding + * differs, so the two are interchangeable per-fetch. + * + * `ts_return_type` pins a structural shape in the generated d.ts: + * `CopyCageBatch` is a manual `ToNapiValue` type (not a `#[napi(object)]` + * struct), so napi-rs has no interface to emit for it. The driver + * re-types the value precisely via `KernelArrowImport.ts`. + */ + fetchNextBatchCopycage(): Promise<{ numRows: number, columns: any[] } | null> /** * Result schema as an Arrow IPC payload (schema header only, no * record-batch message). Available before any batches have been diff --git a/napi/src/async_statement.rs b/napi/src/async_statement.rs index 863b9369..de220830 100644 --- a/napi/src/async_statement.rs +++ b/napi/src/async_statement.rs @@ -474,6 +474,60 @@ impl AsyncResultHandle { .await } + /// Copy-into-cage variant of `fetchNextBatch` (Flavour A): hands the + /// batch's Arrow buffers to JS as in-cage (V8-allocated) `ArrayBuffer`s + /// with the bytes `memcpy`'d in, instead of serialising to Arrow IPC + /// bytes. Mirrors `Statement::fetch_next_batch_copycage` exactly — this + /// is the handle the *async query path* (`awaitResult()`) exposes, so + /// the driver's normal `executeStatement` flow can use copycage rather + /// than silently falling back to IPC. See `crate::zerocopy::CopyCageBatch`. + /// + /// Any offset-sliced array is compacted to offset 0 first + /// (`zerocopy::compact_to_offset_zero`); the JS importer's `makeData` + /// cannot apply a logical element offset. Returns `null` at end of + /// stream, exactly like `fetchNextBatch`. + #[napi(ts_return_type = "Promise<{ numRows: number, columns: any[] } | null>")] + pub async fn fetch_next_batch_copycage( + &self, + ) -> napi::Result> { + let inner = Arc::clone(&self.inner); + guarded(async move { + let mut guard = inner.lock().await; + let stream = guard + .as_mut() + .ok_or_else(|| closed_err("AsyncResultHandle"))?; + let maybe_batch = stream.next_batch().await.map_err(napi_err_from_kernel)?; + let Some(batch) = maybe_batch else { + return Ok(None); + }; + let record_batch = match batch { + ResultBatch::Arrow(rb) => rb, + _ => { + return Err(napi_err_from_kernel( + KernelError::invalid_argument().message(format!( + "non-Arrow ResultBatch variant from kernel — \ + databricks-sea-native v{} cannot decode this batch \ + shape, upgrade the binding", + env!("CARGO_PKG_VERSION"), + )), + )); + } + }; + // Normalise any offset-sliced array to offset 0 — the JS importer + // builds every `Data` at offset 0 and cannot honour a logical + // element offset. Inline (non-sliced) batches return unchanged. + let record_batch = crate::zerocopy::compact_to_offset_zero(record_batch.clone()) + .map_err(|e| { + napi_err_from_kernel( + KernelError::internal() + .message(format!("copycage offset-0 compaction failed: {e}")), + ) + })?; + Ok(Some(crate::zerocopy::CopyCageBatch::new(record_batch))) + }) + .await + } + /// Result schema as an Arrow IPC payload (schema header only, /// no record-batch message). Available before any batches have /// been fetched. Sync because the body has no `.await` — diff --git a/napi/src/lib.rs b/napi/src/lib.rs index ee134cfa..a068b10b 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -51,6 +51,7 @@ pub(crate) mod result; pub(crate) mod runtime; pub(crate) mod statement; pub(crate) mod util; +pub(crate) mod zerocopy; /// Returns the native binding's crate version (`CARGO_PKG_VERSION`). /// diff --git a/napi/src/statement.rs b/napi/src/statement.rs index ce81814d..e6714208 100644 --- a/napi/src/statement.rs +++ b/napi/src/statement.rs @@ -381,6 +381,77 @@ impl Statement { .await } + /// Copy-into-cage variant of `fetchNextBatch` (Flavour A): hands the + /// batch's Arrow buffers to JS as in-cage (V8-allocated) `ArrayBuffer`s + /// with the bytes `memcpy`'d in, instead of serialising to Arrow IPC + /// bytes (`fetchNextBatch`). See `crate::zerocopy::CopyCageBatch`. + /// + /// Cage-safe (works under the V8 sandbox / Electron) and GC-clean — V8 + /// owns the bytes, so no Arc/finalizer is needed. Costs one plain copy + /// per buffer but still avoids the IPC path's serialize + decode passes. + /// + /// Any offset-sliced array is compacted to offset 0 first + /// (`zerocopy::compact_to_offset_zero`), because the JS importer's + /// `makeData` cannot apply a logical element offset. + /// + /// Returns `null` at end of stream, exactly like `fetchNextBatch`. Same + /// error/auto-close/finished semantics — only the result encoding + /// differs, so the two are interchangeable per-fetch. + /// + /// `ts_return_type` pins a structural shape in the generated d.ts: + /// `CopyCageBatch` is a manual `ToNapiValue` type (not a `#[napi(object)]` + /// struct), so napi-rs has no interface to emit for it. The driver + /// re-types the value precisely via `KernelArrowImport.ts`. + #[napi(ts_return_type = "Promise<{ numRows: number, columns: any[] } | null>")] + pub async fn fetch_next_batch_copycage( + &self, + ) -> napi::Result> { + let inner = Arc::clone(&self.inner); + let finished = Arc::clone(&self.finished); + guarded(async move { + let mut guard = inner.lock().await; + let inner_state = guard.as_mut().ok_or_else(statement_closed_err)?; + let maybe_batch = inner_state + .next_batch() + .await + .map_err(napi_err_from_kernel)?; + let Some(batch) = maybe_batch else { + finished.store(true, Ordering::Release); + return Ok(None); + }; + let record_batch = match batch { + ResultBatch::Arrow(rb) => rb, + _ => { + return Err(napi_err_from_kernel( + KernelError::invalid_argument().message(format!( + "non-Arrow ResultBatch variant from kernel — \ + databricks-sql-kernel-napi v{} cannot decode this batch \ + shape, upgrade the binding", + env!("CARGO_PKG_VERSION"), + )), + )); + } + }; + // Normalise any offset-sliced array to offset 0 — the JS importer + // builds every `Data` at offset 0 and cannot honour a logical + // element offset, so a sliced array would read the wrong rows. + // Inline (non-sliced) Databricks batches return unchanged. + let record_batch = crate::zerocopy::compact_to_offset_zero(record_batch.clone()) + .map_err(|e| { + napi_err_from_kernel( + KernelError::internal() + .message(format!("copycage offset-0 compaction failed: {e}")), + ) + })?; + // `RecordBatch::clone` is a shallow Arc bump (no buffer copy); the + // owned batch lives until the bytes are memcpy'd into the cage in + // `CopyCageBatch`'s `ToNapiValue`, after which it drops — V8 owns + // the copies, so no finalizer keeps the native memory alive. + Ok(Some(crate::zerocopy::CopyCageBatch::new(record_batch))) + }) + .await + } + /// Result schema as an Arrow IPC payload (schema header only, no /// record-batch message). Available before any batches have been /// fetched, and remains available after `close()` — the kernel diff --git a/napi/src/zerocopy.rs b/napi/src/zerocopy.rs new file mode 100644 index 00000000..806528c1 --- /dev/null +++ b/napi/src/zerocopy.rs @@ -0,0 +1,225 @@ +// Copyright (c) 2026 Databricks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Copy-into-cage Arrow handoff across the napi boundary (Flavour A). +//! +//! The default `fetch_next_batch` path (`result.rs::encode_ipc_stream`) +//! re-serialises every `RecordBatch` to Arrow IPC bytes and copies them +//! into V8 — three O(uncompressed-result-size) passes (serialize → napi +//! Buffer copy → JS `RecordBatchReader` decode) that the Python binding +//! avoids entirely via pyarrow's Arrow C Data Interface pointer handoff. +//! +//! This module is the Node analogue. Instead of IPC bytes, it ships the +//! batch's Arrow buffers across the boundary as **in-cage** (V8-allocated) +//! `ArrayBuffer`s: each Arrow buffer is `memcpy`'d into a fresh +//! `napi_create_arraybuffer(len)` allocation inside V8's heap. This is +//! cage-safe (works under the V8 sandbox / Electron, which reject external +//! buffers) and GC-clean — V8 owns the bytes, so no Arc/finalizer is +//! needed. It costs one plain copy per buffer but still avoids the IPC +//! path's serialize + decode passes. +//! +//! The JS side (`lib/kernel/KernelArrowImport.ts`) rebuilds an +//! `apache-arrow` `RecordBatch` from these buffers via `makeData`, driven +//! by the result schema it already decodes once. The buffer→slot mapping +//! and `offset` semantics mirror Arrow's own C Data Interface export +//! (`arrow-data/src/ffi.rs`), so the reconstructed batch is byte-identical +//! to what `RecordBatchReader` would have produced from the IPC stream. +//! +//! The caller (`statement.rs` / `async_statement.rs`) guarantees the batch +//! is offset-0 compact (any sliced array is concat-compacted before it +//! reaches here), so `build_node` never has to apply a logical `offset` +//! other than 0 to the values — the JS importer's `makeData` cannot honour +//! a non-zero offset. + +use arrow_array::{Array, RecordBatch}; +use arrow_buffer::{Buffer, MutableBuffer, NullBuffer}; +use arrow_data::ArrayData; +use arrow_schema::ArrowError; +use arrow_select::concat::concat_batches; +use napi::bindgen_prelude::{ToNapiValue, TypeName}; +use napi::{sys, Env, JsObject, NapiRaw, Result as NapiResult, ValueType}; + +/// Does any array in the batch (recursively, including child arrays of +/// struct / list / map) carry a non-zero logical `offset`? Such an array is +/// a *slice* into a shared backing buffer; the JS importer's `makeData` +/// always builds at offset 0, so a sliced array would silently read from +/// the wrong element. The kernel compacts these to offset 0 before the +/// copycage handoff (see [`compact_to_offset_zero`]). +fn array_data_has_offset(data: &ArrayData) -> bool { + data.offset() != 0 || data.child_data().iter().any(array_data_has_offset) +} + +/// Return an offset-0 compact copy of `batch` if any of its arrays (or their +/// children) is offset-sliced; otherwise return the batch unchanged. +/// +/// `concat_batches` over a single-element slice rematerialises every array +/// into a fresh, offset-0 backing buffer — the cheap, correct way to drop a +/// logical offset that `makeData` cannot honour. Copycage already copies +/// every buffer into the cage, so this extra compaction only costs anything +/// in the (rare) sliced case, and even then it is one in-process Arrow copy. +pub(crate) fn compact_to_offset_zero(batch: RecordBatch) -> Result { + let needs_compaction = batch + .columns() + .iter() + .any(|c| array_data_has_offset(&c.to_data())); + if !needs_compaction { + return Ok(batch); + } + let schema = batch.schema(); + concat_batches(&schema, std::slice::from_ref(&batch)) +} + +/// A `RecordBatch` whose buffers cross the napi boundary as in-cage copies +/// (Flavour A). Owns the batch (a cheap Arc-based `RecordBatch::clone`) so +/// it can be returned from an `async fn`; the actual JS-value construction +/// happens in `ToNapiValue::to_napi_value`, which runs on the JS thread +/// where a real `Env` is available. Each per-buffer `ArrayBuffer` is +/// V8-allocated and the bytes are `memcpy`'d in, so it is cage-safe and +/// needs no finalizer. +pub struct CopyCageBatch { + pub(crate) batch: RecordBatch, +} + +impl CopyCageBatch { + pub(crate) fn new(batch: RecordBatch) -> Self { + Self { batch } + } +} + +/// Align a `NullBuffer` to `data_offset` so a single element `offset` on +/// the JS-side `Data` applies uniformly to values and validity — the exact +/// contract Arrow's own C Data Interface export uses. Lifted from +/// `arrow-data/src/ffi.rs::align_nulls`: +/// * offsets already match → return the buffer as-is (zero-copy) +/// * `data_offset == 0` → bit-slice to a fresh offset-0 buffer +/// * otherwise → repack into an aligned buffer +/// +/// In practice the caller compacts every batch to offset 0 before it +/// reaches here, so the first/second arm fire; the third arm is retained +/// for defence-in-depth (it matches upstream Arrow exactly). +fn align_nulls(data_offset: usize, nulls: Option<&NullBuffer>) -> Option { + let nulls = nulls?; + if data_offset == nulls.offset() { + // Validity already starts at the same logical zero as values. + Some(nulls.buffer().clone()) + } else if data_offset == 0 { + Some(nulls.inner().sliced()) + } else { + let buf = MutableBuffer::collect_bool(data_offset + nulls.len(), |idx| { + idx >= data_offset && nulls.is_valid(idx - data_offset) + }); + Some(buf.into()) + } +} + +/// Create an in-cage (V8-allocated) `ArrayBuffer` and copy an Arrow +/// `Buffer`'s bytes into it (Flavour A). `create_arraybuffer(len)` routes to +/// `napi_create_arraybuffer`, which allocates inside V8's heap — cage-safe, +/// and V8 owns the bytes so no finalizer / Arc-bump is needed (the source +/// `buf` is dropped right after the copy). One plain `memcpy` per buffer. +/// +/// `create_arraybuffer(0)` yields a fresh, distinct empty `ArrayBuffer` +/// (a null data pointer for which `AsMut` returns an empty slice), so the +/// copy below is a harmless no-op for zero-length buffers — no special +/// case needed, and no buffer is ever aliased or shared. +fn arraybuffer_copy_to_cage(env: &Env, buf: &Buffer) -> NapiResult { + let src = buf.as_slice(); + let mut value = env.create_arraybuffer(src.len())?; + let dst: &mut [u8] = value.as_mut(); + dst.copy_from_slice(src); + Ok(value.into_raw()) +} + +/// Recursively build the JS descriptor object for one `ArrayData` node: +/// +/// ```text +/// { length, offset, nullCount, +/// validity?: ArrayBuffer, // omitted when the node has no null buffer +/// buffers: ArrayBuffer[], // the type's data buffers, in Arrow order +/// children: Node[] } // child ArrayData (struct fields / list values) +/// ``` +/// +/// The buffer→slot mapping is left to the JS importer, which keys off the +/// result schema's per-field `DataType`; here we just expose the raw tree +/// in canonical Arrow order (`ArrayData::buffers()` / `child_data()`). +fn build_node(env: &Env, data: &ArrayData) -> NapiResult { + let mut node = env.create_object()?; + node.set_named_property("length", env.create_int64(data.len() as i64)?)?; + node.set_named_property("offset", env.create_int64(data.offset() as i64)?)?; + node.set_named_property("nullCount", env.create_int64(data.null_count() as i64)?)?; + + if let Some(validity) = align_nulls(data.offset(), data.nulls()) { + node.set_named_property("validity", arraybuffer_copy_to_cage(env, &validity)?)?; + } + + let data_buffers = data.buffers(); + let mut buffers = env.create_array_with_length(data_buffers.len())?; + for (i, buf) in data_buffers.iter().enumerate() { + buffers.set_element(i as u32, arraybuffer_copy_to_cage(env, buf)?)?; + } + node.set_named_property("buffers", buffers)?; + + let child_data = data.child_data(); + let mut children = env.create_array_with_length(child_data.len())?; + for (i, child) in child_data.iter().enumerate() { + children.set_element(i as u32, build_node(env, child)?)?; + } + node.set_named_property("children", children)?; + + Ok(node) +} + +/// Build `{ numRows, columns: Node[] }` for `batch`, minting each per-buffer +/// `ArrayBuffer` as an in-cage copy. +unsafe fn batch_to_napi_value( + raw_env: sys::napi_env, + batch: RecordBatch, +) -> NapiResult { + let env = unsafe { Env::from_raw(raw_env) }; + + let mut out = env.create_object()?; + out.set_named_property("numRows", env.create_int64(batch.num_rows() as i64)?)?; + + let mut columns = env.create_array_with_length(batch.num_columns())?; + for (i, column) in batch.columns().iter().enumerate() { + let data = column.to_data(); + columns.set_element(i as u32, build_node(&env, &data)?)?; + } + out.set_named_property("columns", columns)?; + + Ok(unsafe { out.raw() }) +} + +/// Surfaces in the generated `index.d.ts` as the (opaque-to-TS) return type +/// of `fetchNextBatchCopycage`. The JS importer (`KernelArrowImport.ts`) +/// re-types the value structurally, so a plain `object` here is sufficient. +impl TypeName for CopyCageBatch { + fn type_name() -> &'static str { + "CopyCageBatch" + } + + fn value_type() -> ValueType { + ValueType::Object + } +} + +impl ToNapiValue for CopyCageBatch { + /// Build the `{ numRows, columns: Node[] }` descriptor with in-cage + /// (V8-allocated, `memcpy`'d) `ArrayBuffer`s — cage-safe and + /// finalizer-free. Runs on the JS thread, so the `Env` reconstructed + /// here is the live napi environment needed to mint `ArrayBuffer`s. + unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> NapiResult { + unsafe { batch_to_napi_value(raw_env, val.batch) } + } +}