// math.gl // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors import * as arrow from 'apache-arrow'; import type {Buffers} from 'apache-arrow/data'; import {getRecordBatchMetadata, createRecordBatchWithMetadata} from './arrow-record-batch'; import {createArrowData, getArrowVariadicBuffers} from './arrow-data'; import {isArrowRecordBatch, isArrowTable} from './arrow-column-utils'; /** Arrow JS typed array used in Arrow internal buffer slots. */ type ArrowTypedArray = | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; /** Arrow JS object shapes supported by splitArrowBuffers. */ export type SplitArrowBuffersInput = | arrow.Table | arrow.RecordBatch | arrow.Vector | arrow.Data; /** Options for splitArrowBuffers. */ export type SplitArrowBuffersOptions = { /** * Buffer copy mode. * - 'none': do not copy any typed arrays. * - 'sliced': copy only typed arrays that view a larger ArrayBuffer. * - 'all': copy every Arrow internal typed array. */ copy?: 'none' | 'sliced' | 'all'; /** Also copy views whose backing buffer is used by any of these retained Arrow objects. */ retain?: readonly SplitArrowBuffersInput[]; }; /** Copy ownership is scoped to one outgoing Arrow object. */ type SplitArrowBuffersContext = SplitArrowBuffersOptions & { /** Reuses a copy when multiple columns or dictionaries reference the same typed-array view. */ copies: WeakMap; /** Buffer identity catches aliases even when views cover disjoint byte ranges. */ retainedBuffers: Set; }; /** * Rebuilds an Arrow object so sliced internal typed-array buffers are split into standalone * ArrayBuffers, while whole-buffer typed arrays are reused by default. * @param input Arrow table, record batch, vector, or data node to rebuild. * @param options Buffer splitting options. * @returns A real Arrow object of the same kind with transfer-safe internal buffers. */ export function splitArrowBuffers( input: arrow.Table, options?: SplitArrowBuffersOptions ): arrow.Table; export function splitArrowBuffers( input: arrow.RecordBatch, options?: SplitArrowBuffersOptions ): arrow.RecordBatch; export function splitArrowBuffers( input: arrow.Vector, options?: SplitArrowBuffersOptions ): arrow.Vector; export function splitArrowBuffers( input: arrow.Data, options?: SplitArrowBuffersOptions ): arrow.Data; export function splitArrowBuffers( input: T, options: SplitArrowBuffersOptions = {} ): T { const retainedBuffers = new Set(); const visited = new Set(); for (const retained of options.retain ?? []) { const chunks = isArrowTable(retained) ? retained.batches.map(batch => batch.data) : isArrowVector(retained) ? retained.data : [isArrowRecordBatch(retained) ? retained.data : retained]; for (const data of chunks) collectArrowBuffers(data, retainedBuffers, visited); } const context: SplitArrowBuffersContext = {...options, copies: new WeakMap(), retainedBuffers}; if (isArrowTable(input)) { return splitArrowTableBuffersInternal(input, context) as T; } if (isArrowRecordBatch(input)) { return splitArrowRecordBatchBuffers(input, context) as T; } if (isArrowVector(input)) { return splitArrowVectorBuffers(input, context) as T; } return splitArrowDataBuffers(input, context) as T; } /** * Rebuilds an Arrow table so sliced internal typed-array buffers are split into standalone * ArrayBuffers, while whole-buffer typed arrays are reused by default. * @param arrowTable Arrow table to rebuild without mutating the original table. * @param options Buffer splitting options. * @returns A real Arrow table with transfer-safe internal buffers. */ export function splitArrowTableBuffers( arrowTable: arrow.Table, options?: SplitArrowBuffersOptions ): arrow.Table { return splitArrowBuffers(arrowTable, options); } /** * Tests whether a value is an Arrow vector. * @param input Value to test. * @returns True when the value is an Arrow vector. */ function isArrowVector(input: SplitArrowBuffersInput): input is arrow.Vector { return 'data' in input && Array.isArray(input.data); } /** * Rebuilds an Arrow table by rebuilding each record batch. * @param arrowTable Arrow table to rebuild. * @param options Buffer splitting options. * @returns Rebuilt Arrow table. */ function splitArrowTableBuffersInternal( arrowTable: arrow.Table, options: SplitArrowBuffersContext ): arrow.Table { const recordBatches = arrowTable.batches.map(recordBatch => splitArrowRecordBatchBuffers(recordBatch, options) ); return new arrow.Table(arrowTable.schema, recordBatches); } /** * Rebuilds an Arrow record batch by rebuilding its data node. * @param recordBatch Arrow record batch to rebuild. * @param options Buffer splitting options. * @returns Rebuilt Arrow record batch. */ function splitArrowRecordBatchBuffers( recordBatch: arrow.RecordBatch, options: SplitArrowBuffersContext ): arrow.RecordBatch { const data = splitArrowDataBuffers(recordBatch.data, options); return createRecordBatchWithMetadata( recordBatch.schema, data, getRecordBatchMetadata(recordBatch) ); } /** * Recursively rebuilds Arrow Data and isolates requested internal typed-array buffers. * @param data Arrow data node to rebuild. * @param options Buffer splitting options. * @returns Rebuilt Arrow data node. */ function splitArrowDataBuffers( data: arrow.Data, options: SplitArrowBuffersContext ): arrow.Data { const children = data.children.map(childData => splitArrowDataBuffers(childData, options)); const dictionary = data.dictionary ? splitArrowVectorBuffers(data.dictionary, options) : undefined; const buffers: Partial> = { [arrow.BufferType.OFFSET]: splitArrowBuffer(data.buffers[arrow.BufferType.OFFSET], options), [arrow.BufferType.DATA]: splitArrowBuffer(data.buffers[arrow.BufferType.DATA], options), [arrow.BufferType.VALIDITY]: splitArrowBuffer(data.buffers[arrow.BufferType.VALIDITY], options), [arrow.BufferType.TYPE]: splitArrowBuffer(data.buffers[arrow.BufferType.TYPE], options) }; return createArrowData( data.type, data.offset, data.length, // @ts-expect-error _nullCount is protected. Reuse it to preserve lazy null-count state. data._nullCount, buffers, children, dictionary, getArrowVariadicBuffers(data).map(buffer => splitArrowBuffer(buffer, options)) ); } /** * Rebuilds an Arrow vector by rebuilding each underlying Data chunk. * @param vector Arrow vector to rebuild. * @param options Buffer splitting options. * @returns Rebuilt Arrow vector. */ function splitArrowVectorBuffers( vector: arrow.Vector, options: SplitArrowBuffersContext ): arrow.Vector { return new arrow.Vector(vector.data.map(data => splitArrowDataBuffers(data, options))); } /** Collects backing buffers from nested children, dictionaries, and view types. */ function collectArrowBuffers( data: arrow.Data, buffers: Set, visited: Set ): void { if (visited.has(data)) return; visited.add(data); for (const view of [...Object.values(data.buffers), ...getArrowVariadicBuffers(data)]) { if (isArrowTypedArray(view)) buffers.add(view.buffer); } for (const child of data.children) collectArrowBuffers(child, buffers, visited); for (const chunk of data.dictionary?.data ?? []) collectArrowBuffers(chunk, buffers, visited); } /** * Copies a typed-array view when requested or when it does not span its full backing buffer. * @param array Typed array from an Arrow buffer slot. * @param options Buffer splitting options. * @returns The original typed array or a copied typed array with a standalone buffer. */ function splitArrowBuffer(array: T, options: SplitArrowBuffersContext): T { if (!isArrowTypedArray(array)) { return array; } const sliced = array.byteOffset !== 0 || array.byteLength !== array.buffer.byteLength; if ( !options.retainedBuffers.has(array.buffer) && options.copy !== 'all' && (options.copy === 'none' || !sliced) ) { return array; } let copy = options.copies.get(array); if (!copy) { copy = array instanceof Uint8Array ? new Uint8Array(array) : array.slice(); options.copies.set(array, copy); } return copy as T; } /** * Tests whether a value is an Arrow typed array instead of a DataView. * @param value Value to test. * @returns True when the value is one of the Arrow internal typed-array buffer types. */ function isArrowTypedArray(value: unknown): value is ArrowTypedArray { return ArrayBuffer.isView(value) && !(value instanceof DataView); }