From b3cd5b2c6afe27a39266c3d81d373fb9c6a18eeb Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Sat, 2 Dec 2023 13:36:15 -0500 Subject: [PATCH] Get rid of toNumber --- js/src/builder/binary.ts | 4 ++-- js/src/builder/buffer.ts | 9 +-------- js/src/builder/largeutf8.ts | 20 ++++++++++++++++++-- js/src/type.ts | 10 +++++----- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/js/src/builder/binary.ts b/js/src/builder/binary.ts index bcdefab9d3e66..3c12ddf34abb0 100644 --- a/js/src/builder/binary.ts +++ b/js/src/builder/binary.ts @@ -42,11 +42,11 @@ export class BinaryBuilder extends VariableWidthBuilder ((((Math.ceil(len) * BPE) + 63) & ~63) || 64) / BPE; @@ -129,14 +128,8 @@ export class BitmapBufferBuilder extends DataBufferBuilder { export class OffsetsBufferBuilder extends DataBufferBuilder { constructor(type: T) { super(new type.OffsetArrayType(1), 1); - this.toNumber = type.OffsetArrayType === BigInt64Array ? BigInt : bigIntToNumber as any; } - /** - * The correct number constructor for the buffer type. - */ - public toNumber: ((number: number | bigint) => T['TOffsetArray'] extends BigInt64Array ? bigint : number); - public append(value: T['TOffsetArray'][0]) { return this.set(this.length - 1, value); } @@ -151,7 +144,7 @@ export class OffsetsBufferBuilder extends DataBufferBuilder< } public flush(length = this.length - 1) { if (length > this.length) { - this.set(length - 1, this.toNumber(0)); + this.set(length - 1, this.BYTES_PER_ELEMENT > 4 ? 0n : 0); } return super.flush(length + 1); } diff --git a/js/src/builder/largeutf8.ts b/js/src/builder/largeutf8.ts index 002bb8265a73d..0de97a4586e6e 100644 --- a/js/src/builder/largeutf8.ts +++ b/js/src/builder/largeutf8.ts @@ -38,7 +38,23 @@ export class LargeUtf8Builder extends VariableWidthBuilder, pendingLength: number): void { } + // TODO: move to largeBinaryBuilder when implemented + // protected _flushPending(pending: Map, pendingLength: number): void { } + protected _flushPending(pending: Map, pendingLength: number) { + const offsets = this._offsets; + const data = this._values.reserve(pendingLength).buffer; + let offset = 0; + for (const [index, value] of pending) { + if (value === undefined) { + offsets.set(index, 0n); + } else { + const length = value.length; + data.set(value, offset); + offsets.set(index, BigInt(length)); + offset += length; + } + } + } } -(LargeUtf8Builder.prototype as any)._flushPending = (BinaryBuilder.prototype as any)._flushPending; +// (LargeUtf8Builder.prototype as any)._flushPending = (LargeBinaryBuilder.prototype as any)._flushPending; diff --git a/js/src/type.ts b/js/src/type.ts index b66b277a19d0c..6223d0316f17a 100644 --- a/js/src/type.ts +++ b/js/src/type.ts @@ -236,7 +236,7 @@ Object.defineProperty(Float32.prototype, 'ArrayType', { value: Float32Array }); Object.defineProperty(Float64.prototype, 'ArrayType', { value: Float64Array }); /** @ignore */ -export interface Binary extends DataType { TArray: Uint8Array; TValue: Uint8Array; ArrayType: TypedArrayConstructor } +export interface Binary extends DataType { TArray: Uint8Array; TOffsetArray: Int32Array; TValue: Uint8Array; ArrayType: TypedArrayConstructor; OffsetArrayType: TypedArrayConstructor } /** @ignore */ export class Binary extends DataType { constructor() { @@ -478,13 +478,13 @@ export class Duration extends DataType { } /** @ignore */ -export class DurationSecond extends Duration { constructor() { super(TimeUnit.SECOND); }} +export class DurationSecond extends Duration { constructor() { super(TimeUnit.SECOND); } } /** @ignore */ -export class DurationMillisecond extends Duration { constructor() { super(TimeUnit.MILLISECOND); }} +export class DurationMillisecond extends Duration { constructor() { super(TimeUnit.MILLISECOND); } } /** @ignore */ -export class DurationMicrosecond extends Duration { constructor() { super(TimeUnit.MICROSECOND); }} +export class DurationMicrosecond extends Duration { constructor() { super(TimeUnit.MICROSECOND); } } /** @ignore */ -export class DurationNanosecond extends Duration { constructor() { super(TimeUnit.NANOSECOND); }} +export class DurationNanosecond extends Duration { constructor() { super(TimeUnit.NANOSECOND); } } /** @ignore */