Skip to content

Commit 73a2fa9

Browse files
committed
fix stream -> file, file -> stream, add tests
1 parent 304e75d commit 73a2fa9

2 files changed

Lines changed: 34 additions & 23 deletions

File tree

js/src/ipc/writer/binary.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class RecordBatchSerializer extends VectorVisitor {
268268
const { data, length } = vector;
269269
const { offset, values, valueOffsets } = data;
270270
const firstOffset = valueOffsets[0];
271-
const lastOffset = valueOffsets[valueOffsets.length - 1];
271+
const lastOffset = valueOffsets[length];
272272
const byteLength = Math.min(lastOffset - firstOffset, values.byteLength - firstOffset);
273273
// Push in the order FlatList types read their buffers
274274
// valueOffsets buffer first
@@ -536,9 +536,9 @@ export class TypeSerializer extends TypeVisitor {
536536
function concatBuffersWithMetadata(byteLength: number, buffers: Uint8Array[], buffersMeta: BufferMetadata[]) {
537537
const data = new Uint8Array(byteLength);
538538
for (let bufferIndex = -1, buffersLen = buffers.length; ++bufferIndex < buffersLen;) {
539-
const { buffer, byteLength } = buffers[bufferIndex];
540-
const { offset: byteOffset } = buffersMeta[bufferIndex];
541-
data.set(new Uint8Array(buffer, 0, byteLength), byteOffset);
539+
const { offset } = buffersMeta[bufferIndex];
540+
const { buffer, byteOffset, byteLength } = buffers[bufferIndex];
541+
data.set(new Uint8Array(buffer, byteOffset, byteLength), offset);
542542
}
543543
return data;
544544
}

js/test/integration/validate-tests.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const jsonAndArrowPaths = toArray(zip(
5555
.filter(([p1, p2]) => p1 !== undefined && p2 !== undefined) as [string, string][];
5656

5757
expect.extend({
58-
toEqualVector(v1: any, v2: any) {
58+
toEqualVector([v1, format1, columnName]: [any, string, string], [v2, format2]: [any, string]) {
5959

6060
const format = (x: any, y: any, msg= ' ') => `${
6161
this.utils.printExpected(x)}${
@@ -102,7 +102,7 @@ expect.extend({
102102
return {
103103
pass: allFailures.every(({ failures }) => failures.length === 0),
104104
message: () => [
105-
`${v1.name}: (${format('json', 'arrow', ' !== ')})\n`,
105+
`${columnName}: (${format(format1, format2, ' !== ')})\n`,
106106
...allFailures.map(({ failures, title }) =>
107107
!failures.length ? `` : [`${title}:`, ...failures].join(`\n`))
108108
].join('\n')
@@ -119,8 +119,10 @@ describe(`Integration`, () => {
119119
describe(path.join(dir, name), () => {
120120
testReaderIntegration(json, arrowBuffer);
121121
testTableFromBuffersIntegration(json, arrowBuffer);
122-
testTableToBuffersIntegration('file')(json, arrowBuffer);
123-
testTableToBuffersIntegration('stream')(json, arrowBuffer);
122+
testTableToBuffersIntegration('json', 'file')(json, arrowBuffer);
123+
testTableToBuffersIntegration('json', 'stream')(json, arrowBuffer);
124+
testTableToBuffersIntegration('binary', 'file')(json, arrowBuffer);
125+
testTableToBuffersIntegration('binary', 'stream')(json, arrowBuffer);
124126
});
125127
}
126128
});
@@ -134,8 +136,11 @@ function testReaderIntegration(jsonData: any, arrowBuffer: Uint8Array) {
134136
expect(jsonRecordBatch.length).toEqual(binaryRecordBatch.length);
135137
expect(jsonRecordBatch.numCols).toEqual(binaryRecordBatch.numCols);
136138
for (let i = -1, n = jsonRecordBatch.numCols; ++i < n;) {
137-
(jsonRecordBatch.getChildAt(i) as any).name = jsonRecordBatch.schema.fields[i].name;
138-
(expect(jsonRecordBatch.getChildAt(i)) as any).toEqualVector(binaryRecordBatch.getChildAt(i));
139+
const v1 = jsonRecordBatch.getChildAt(i);
140+
const v2 = binaryRecordBatch.getChildAt(i);
141+
const name = jsonRecordBatch.schema.fields[i].name;
142+
(expect([v1, `json`, name]) as any)
143+
.toEqualVector([v2, `binary`]);
139144
}
140145
}
141146
});
@@ -149,25 +154,31 @@ function testTableFromBuffersIntegration(jsonData: any, arrowBuffer: Uint8Array)
149154
expect(jsonTable.length).toEqual(binaryTable.length);
150155
expect(jsonTable.numCols).toEqual(binaryTable.numCols);
151156
for (let i = -1, n = jsonTable.numCols; ++i < n;) {
152-
(jsonTable.getColumnAt(i) as any).name = jsonTable.schema.fields[i].name;
153-
(expect(jsonTable.getColumnAt(i)) as any).toEqualVector(binaryTable.getColumnAt(i));
157+
const v1 = jsonTable.getColumnAt(i);
158+
const v2 = binaryTable.getColumnAt(i);
159+
const name = jsonTable.schema.fields[i].name;
160+
(expect([v1, `json`, name]) as any)
161+
.toEqualVector([v2, `binary`]);
154162
}
155163
});
156164
}
157165

158-
function testTableToBuffersIntegration(arrowFormat: 'stream' | 'file') {
166+
function testTableToBuffersIntegration(srcFormat: 'json' | 'binary', arrowFormat: 'stream' | 'file') {
159167
return function testTableToBuffersIntegration(jsonData: any, arrowBuffer: Uint8Array) {
160-
test(`serializing json to binary reports the same values as the original binary arrow table`, () => {
168+
test(`serializing ${srcFormat} to a ${arrowFormat} reports the same values as the alternate format`, () => {
161169
expect.hasAssertions();
162-
const fromJSON = Table.from(jsonData);
163-
const serialized = fromJSON.serialize('binary', arrowFormat === 'stream');
164-
const jsonTable = Table.from(serialized);
165-
const binaryTable = Table.from(arrowBuffer);
166-
expect(jsonTable.length).toEqual(binaryTable.length);
167-
expect(jsonTable.numCols).toEqual(binaryTable.numCols);
168-
for (let i = -1, n = jsonTable.numCols; ++i < n;) {
169-
(jsonTable.getColumnAt(i) as any).name = jsonTable.schema.fields[i].name;
170-
(expect(jsonTable.getColumnAt(i)) as any).toEqualVector(binaryTable.getColumnAt(i));
170+
const refFormat = srcFormat === 'json' ? `binary` : `json`;
171+
const refTable = Table.from(refFormat === `json` ? jsonData : arrowBuffer);
172+
const srcTable = Table.from(srcFormat === `json` ? jsonData : arrowBuffer);
173+
const dstTable = Table.from(srcTable.serialize(`binary`, arrowFormat === `stream`));
174+
expect(dstTable.length).toEqual(refTable.length);
175+
expect(dstTable.numCols).toEqual(refTable.numCols);
176+
for (let i = -1, n = dstTable.numCols; ++i < n;) {
177+
const v1 = dstTable.getColumnAt(i);
178+
const v2 = refTable.getColumnAt(i);
179+
const name = dstTable.schema.fields[i].name;
180+
(expect([v1, srcFormat, name]) as any)
181+
.toEqualVector([v2, refFormat]);
171182
}
172183
});
173184
}

0 commit comments

Comments
 (0)