This is an automated email from the ASF dual-hosted git repository.
domoritz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 3d1324e862 GH-39255: [JS] Allow customization of schema when passing
vectors to table constructor (#39256)
3d1324e862 is described below
commit 3d1324e86231fbf6799ba5ea22604072857776b1
Author: Dominik Moritz <[email protected]>
AuthorDate: Wed Jan 3 10:53:00 2024 +0200
GH-39255: [JS] Allow customization of schema when passing vectors to table
constructor (#39256)
Merge after #39254.
* Closes: #39255
---
js/src/builder/largebinary.ts | 2 +-
js/src/table.ts | 6 ++++--
js/test/unit/table-tests.ts | 17 +++++++++++++++++
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/js/src/builder/largebinary.ts b/js/src/builder/largebinary.ts
index 59aa7144d2..f737349ac1 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<TNull = any> extends
VariableWidthBuilder<LargeBinary, TNull> {
constructor(opts: BuilderOptions<LargeBinary, TNull>) {
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 58518257b3..00f4a4cfe0 100644
--- a/js/src/table.ts
+++ b/js/src/table.ts
@@ -73,6 +73,8 @@ export class Table<T extends TypeMap = any> {
constructor(...batches: readonly RecordBatch<T>[]);
constructor(...columns: { [P in keyof T]: Vector<T[P]> }[]);
constructor(...columns: { [P in keyof T]: Data<T[P]> | DataProps<T[P]>
}[]);
+ constructor(schema: Schema<T>, ...columns: { [P in keyof T]: Vector<T[P]>
}[]);
+ constructor(schema: Schema<T>, ...columns: { [P in keyof T]: Data<T[P]> |
DataProps<T[P]> }[]);
constructor(schema: Schema<T>, data?: RecordBatch<T> | RecordBatch<T>[]);
constructor(schema: Schema<T>, data?: RecordBatch<T> | RecordBatch<T>[],
offsets?: Uint32Array);
constructor(...args: any[]) {
@@ -112,8 +114,8 @@ export class Table<T extends TypeMap = any> {
} 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 6b34124abc..094988c052 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<number>(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<number>(10)));
const f32s = new Float32Array(arange(new Array<number>(10)));