Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions js/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Table<T extends { [key: string]: DataType } = any> {
return this;
}

const data: RecordBatch<T>[] = [];
let data: RecordBatch<T>[] = [];
let schema: Schema<T> | undefined = undefined;
let offsets: Uint32Array | undefined = undefined;

Expand All @@ -102,18 +102,8 @@ export class Table<T extends { [key: string]: DataType } = any> {
} else if (x && typeof x === 'object') {
const keys = Object.keys(x) as (keyof T)[];
const vecs = keys.map((k) => new Vector(x[k]));
const empty = keys.reduce((empty, key, i) => {
empty[key] = vecs[i].data[0].slice(0, 0);
return empty;
}, Object.create(null) as { [P in keyof T]: Data<T[P]> });
data.push(...keys
.reduce((batches, name, i) => {
vecs[i].data.forEach((data, j) => {
(batches[j] || (batches[j] = { ...empty }))[name] = data;
});
return batches;
}, new Array<{ [P in keyof T]: Data<T[P]> }>())
.map((batch) => new RecordBatch<T>(batch)));
const s = new Schema(keys.map((k, i) => new Field(String(k), vecs[i].type)));
[schema, data] = distributeVectorsIntoRecordBatches(s, vecs);
}
});

Expand Down
36 changes: 18 additions & 18 deletions js/test/unit/table-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,29 @@ describe(`Table`, () => {
const i32 = makeVector(i32s);
const f32 = makeVector(f32s);

// expect(i32.name).toBe('i32');
// expect(f32.name).toBe('f32');
expect(i32).toHaveLength(i32s.length);
expect(f32).toHaveLength(f32s.length);
// expect(i32.nullable).toBe(true);
// expect(f32.nullable).toBe(true);
expect(i32.nullCount).toBe(0);
expect(f32.nullCount).toBe(0);

const table = new Table({ i32, f32 });
const i32Field = table.schema.fields[0];
const f32Field = table.schema.fields[1];

expect(table.numRows).toBe(20);

expect(i32Field.name).toBe('i32');
expect(f32Field.name).toBe('f32');
expect(i32).toHaveLength(i32s.length);
expect(f32).toHaveLength(i32s.length); // new length should be the same as the longest sibling
expect(i32Field.nullable).toBe(true);

const i32Vector = table.getChild('i32')!;
const f32Vector = table.getChild('f32')!;

expect(i32Vector).toHaveLength(i32s.length);
expect(f32Vector).toHaveLength(i32s.length); // new length should be the same as the longest sibling
expect(i32Field.nullable).toBe(false);
expect(f32Field.nullable).toBe(true); // true, with 12 additional nulls
expect(i32.nullCount).toBe(0);
expect(f32.nullCount).toBe(i32s.length - f32s.length);
expect(i32Vector.nullCount).toBe(0);
expect(f32Vector.nullCount).toBe(i32s.length - f32s.length);

const f32Expected = makeData({
type: f32.type,
Expand All @@ -207,8 +209,8 @@ describe(`Table`, () => {
nullBitmap: new Uint8Array(8).fill(255, 0, 1),
});

expect(i32).toEqualVector(makeVector(i32s));
expect(f32).toEqualVector(new Vector([f32Expected]));
expect(i32Vector).toEqualVector(makeVector(i32s));
expect(f32Vector).toEqualVector(new Vector([f32Expected]));
});

test(`creates a new Table from Columns with different lengths and number of inner chunks`, () => {
Expand All @@ -219,12 +221,8 @@ describe(`Table`, () => {
const i32 = makeVector(i32s);
const f32 = makeVector(f32s.slice(0, 8)).concat(makeVector(f32s.slice(8, 16)));

// expect(i32.name).toBe('i32');
// expect(f32.name).toBe('f32');
expect(i32).toHaveLength(i32s.length);
expect(f32).toHaveLength(f32s.length);
// expect(i32.nullable).toBe(true);
// expect(f32.nullable).toBe(true);
expect(i32.nullCount).toBe(0);
expect(f32.nullCount).toBe(0);

Expand All @@ -234,11 +232,13 @@ describe(`Table`, () => {
const i32Renamed = table.getChild('i32Renamed')!;
const f32Renamed = table.getChild('f32Renamed')!;

expect(table.numRows).toBe(20);

expect(i32RenamedField.name).toBe('i32Renamed');
expect(f32RenamedField.name).toBe('f32Renamed');
expect(i32Renamed).toHaveLength(i32s.length);
expect(f32Renamed).toHaveLength(i32s.length); // new length should be the same as the longest sibling
expect(i32RenamedField.nullable).toBe(true);
expect(i32RenamedField.nullable).toBe(false);
expect(f32RenamedField.nullable).toBe(true); // true, with 4 additional nulls
expect(i32Renamed.nullCount).toBe(0);
expect(f32Renamed.nullCount).toBe(i32s.length - f32s.length);
Expand All @@ -251,8 +251,8 @@ describe(`Table`, () => {
nullBitmap: new Uint8Array(8).fill(255, 0, 2),
});

expect(i32).toEqualVector(makeVector(i32s));
expect(f32).toEqualVector(new Vector([f32Expected]));
expect(i32Renamed).toEqualVector(makeVector(i32s));
expect(f32Renamed).toEqualVector(new Vector([f32Expected]));
});

test(`creates a new Table from Typed Arrays`, () => {
Expand Down