diff --git a/js/src/builder/largebinary.ts b/js/src/builder/largebinary.ts index 59aa7144d20a1..f737349ac1c49 100644 --- a/js/src/builder/largebinary.ts +++ b/js/src/builder/largebinary.ts @@ -24,7 +24,7 @@ import { VariableWidthBuilder, BuilderOptions } from '../builder.js'; export class LargeBinaryBuilder extends VariableWidthBuilder { constructor(opts: BuilderOptions) { super(opts); - this._values = new BufferBuilder(new Uint8Array(0)); + this._values = new BufferBuilder(Uint8Array); } public get byteLength(): number { let size = this._pendingLength + (this.length * 4); diff --git a/js/src/table.ts b/js/src/table.ts index 58518257b30cb..00f4a4cfe0a14 100644 --- a/js/src/table.ts +++ b/js/src/table.ts @@ -73,6 +73,8 @@ export class Table { constructor(...batches: readonly RecordBatch[]); constructor(...columns: { [P in keyof T]: Vector }[]); constructor(...columns: { [P in keyof T]: Data | DataProps }[]); + constructor(schema: Schema, ...columns: { [P in keyof T]: Vector }[]); + constructor(schema: Schema, ...columns: { [P in keyof T]: Data | DataProps }[]); constructor(schema: Schema, data?: RecordBatch | RecordBatch[]); constructor(schema: Schema, data?: RecordBatch | RecordBatch[], offsets?: Uint32Array); constructor(...args: any[]) { @@ -112,8 +114,8 @@ export class Table { } else if (typeof x === 'object') { const keys = Object.keys(x) as (keyof T)[]; const vecs = keys.map((k) => new Vector([x[k]])); - const schema = new Schema(keys.map((k, i) => new Field(String(k), vecs[i].type, vecs[i].nullCount > 0))); - const [, batches] = distributeVectorsIntoRecordBatches(schema, vecs); + const batchSchema = schema ?? new Schema(keys.map((k, i) => new Field(String(k), vecs[i].type, vecs[i].nullCount > 0))); + const [, batches] = distributeVectorsIntoRecordBatches(batchSchema, vecs); return batches.length === 0 ? [new RecordBatch(x)] : batches; } } diff --git a/js/test/unit/table-tests.ts b/js/test/unit/table-tests.ts index 6b34124abcaba..094988c052b6e 100644 --- a/js/test/unit/table-tests.ts +++ b/js/test/unit/table-tests.ts @@ -151,6 +151,23 @@ describe(`Table`, () => { expect(i32).toEqualVector(makeVector(i32s)); }); + test(`creates a new Table from a Typed Array and force nullable`, () => { + const i32s = new Int32Array(arange(new Array(10))); + const i32 = makeVector([i32s]); + expect(i32).toHaveLength(i32s.length); + expect(i32.nullCount).toBe(0); + + const table = new Table(new Schema([new Field('i32', new Int32, true)]), { i32 }); + const i32Field = table.schema.fields[0]; + + expect(i32Field.name).toBe('i32'); + expect(i32).toHaveLength(i32s.length); + expect(i32Field.nullable).toBe(true); + expect(i32.nullCount).toBe(0); + + expect(i32).toEqualVector(makeVector(i32s)); + }); + test(`creates a new Table from Typed Arrays`, () => { const i32s = new Int32Array(arange(new Array(10))); const f32s = new Float32Array(arange(new Array(10)));