diff --git a/js/package-lock.json b/js/package-lock.json index bf0c1e7fa4916..153fbfd7b2aaa 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -13425,7 +13425,7 @@ "progress": "^2.0.3", "shelljs": "^0.8.3", "typedoc-default-themes": "^0.6.0-0", - "typescript": "3.4.x" + "typescript": "3.5.x" }, "dependencies": { "handlebars": { @@ -13447,9 +13447,9 @@ "dev": true }, "typescript": { - "version": "3.4.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz", - "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==", + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.2.tgz", + "integrity": "sha512-7KxJovlYhTX5RaRbUdkAXN1KUZ8PwWlTzQdHV6xNqvuFOs7+WBo10TQUqT19Q/Jz2hk5v9TQDIhyLhhJY4p5AA==", "dev": true } } diff --git a/js/src/builder/buffer.ts b/js/src/builder/buffer.ts index b9f7aa4cb744f..7aa336a2a8036 100644 --- a/js/src/builder/buffer.ts +++ b/js/src/builder/buffer.ts @@ -16,13 +16,12 @@ // under the License. import { memcpy } from '../util/buffer'; -import { BigInt64Array, BigUint64Array } from '../util/compat'; +import { BigIntAvailable, BigInt64Array, BigUint64Array } from '../util/compat'; import { TypedArray, TypedArrayConstructor, BigIntArray, BigIntArrayConstructor } from '../interfaces'; -/** @ignore */ type WideArray = T extends BigIntArray ? Int32Array : Uint32Array; /** @ignore */ type DataValue = T extends TypedArray ? number : T extends BigIntArray ? WideValue : T; /** @ignore */ type WideValue = T extends BigIntArray ? bigint | Int32Array | Uint32Array : never; /** @ignore */ type ArrayCtor = @@ -105,7 +104,7 @@ export class DataBufferBuilder extends BufferBuilder { } /** @ignore */ -export class WideBufferBuilder extends BufferBuilder, DataValue> { +export class WideBufferBuilder extends BufferBuilder> { // @ts-ignore - public buffer64: T; + public buffer64: R; // @ts-ignore - constructor(buffer: T, stride: number) { - const ArrayType = buffer instanceof BigInt64Array ? Int32Array : Uint32Array; - super(new ArrayType(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4) as WideArray, stride); - } - public get ArrayType64(): BigIntArrayConstructor { - return this.buffer instanceof Int32Array ? BigInt64Array : BigUint64Array as any; + protected _ArrayType64: BigIntArrayConstructor; + public get ArrayType64() { + return this._ArrayType64 || (this._ArrayType64 = > (this.buffer instanceof Int32Array ? BigInt64Array : BigUint64Array)); } public set(index: number, value: DataValue) { this.reserve(index - this.length + 1); @@ -180,7 +176,9 @@ export class WideBufferBuilder extends BufferBuilder extends IntBuilder {} export class Int32Builder extends IntBuilder {} /** @ignore */ export class Int64Builder extends IntBuilder { + protected _values: WideBufferBuilder; constructor(options: BuilderOptions) { if (options['nullValues']) { options['nullValues'] = (options['nullValues'] as TNull[]).map(toBigInt); } super(options); - if (BigInt64ArrayAvailable) { - this._values = new WideBufferBuilder(new BigInt64Array(0), 2); - } + this._values = new WideBufferBuilder(new Int32Array(0), 2); } - public get values64() { return (this._values as any).buffer64 as BigInt64Array; } + public get values64() { return this._values.buffer64; } public isValid(value: Int32Array | bigint | TNull) { return super.isValid(toBigInt(value)); } } @@ -58,16 +56,15 @@ export class Uint16Builder extends IntBuilder {} export class Uint32Builder extends IntBuilder {} /** @ignore */ export class Uint64Builder extends IntBuilder { + protected _values: WideBufferBuilder; constructor(options: BuilderOptions) { if (options['nullValues']) { options['nullValues'] = (options['nullValues'] as TNull[]).map(toBigInt); } super(options); - if (BigUint64ArrayAvailable) { - this._values = new WideBufferBuilder(new BigUint64Array(0), 2); - } + this._values = new WideBufferBuilder(new Uint32Array(0), 2); } - public get values64() { return (this._values as any).buffer64 as BigUint64Array; } + public get values64() { return this._values.buffer64; } public isValid(value: Uint32Array | bigint | TNull) { return super.isValid(toBigInt(value)); } } diff --git a/js/test/unit/builders/utils.ts b/js/test/unit/builders/utils.ts index 55d6a7b43ac3c..c6a29a7546eba 100644 --- a/js/test/unit/builders/utils.ts +++ b/js/test/unit/builders/utils.ts @@ -53,7 +53,17 @@ export const int16sNoNulls = (length = 20) => Array.from(new Int16Array(randomBy export const int32sNoNulls = (length = 20) => Array.from(new Int32Array(randomBytes(length * Int32Array.BYTES_PER_ELEMENT).buffer)); export const int64sNoNulls = (length = 20) => Array.from({ length }, (_, i) => { const bn = util.BN.new(new Int32Array(randomBytes(2 * 4).buffer)); - return i % 3 === 0 ? bn : i % 2 === 0 ? bn[Symbol.toPrimitive]() : bn[0]; + // Evenly distribute the three types of arguments we support in the Int64 + // builder + switch (i % 3) { + // Int32Array (util.BN is-a Int32Array) + case 0: return bn; + // BigInt + case 1: return bn[Symbol.toPrimitive]() + // number + case 2: + default: return bn[0]; + } }); export const uint8sNoNulls = (length = 20) => Array.from(new Uint8Array(randomBytes(length * Uint8Array.BYTES_PER_ELEMENT).buffer)); @@ -61,7 +71,17 @@ export const uint16sNoNulls = (length = 20) => Array.from(new Uint16Array(random export const uint32sNoNulls = (length = 20) => Array.from(new Uint32Array(randomBytes(length * Uint32Array.BYTES_PER_ELEMENT).buffer)); export const uint64sNoNulls = (length = 20) => Array.from({ length }, (_, i) => { const bn = util.BN.new(new Uint32Array(randomBytes(2 * 4).buffer)); - return i % 3 === 0 ? bn : i % 2 === 0 ? bn[Symbol.toPrimitive]() : bn[0]; + // Evenly distribute the three types of arguments we support in the Uint64 + // builder + switch (i % 3) { + // UInt32Array (util.BN is-a Uint32Array) + case 0: return bn; + // BigInt + case 1: return bn[Symbol.toPrimitive]() + // number + case 2: + default: return bn[0]; + } }); export const float16sNoNulls = (length = 20) => Array.from(new Uint16Array(randomBytes(length * Uint16Array.BYTES_PER_ELEMENT).buffer)).map((x) => (x - 32767) / 32767); export const float32sNoNulls = (length = 20) => Array.from(new Float32Array(randomBytes(length * Float32Array.BYTES_PER_ELEMENT).buffer));