Skip to content
Draft
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
7 changes: 7 additions & 0 deletions src/ipc/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ abstract class RecordBatchReaderImpl<T extends TypeMap = any> implements RecordB
children = this._loadVectors(header, body, this.schema.fields);
}

for (const [index, child] of children.entries()) {
if (child.length !== header.length) {
const name = this.schema.fields[index]?.name ?? `#${index}`;
throw new Error(`Column '${name}' has length ${child.length} but the record batch declares ${header.length} rows`);
}
}

const data = makeData({ type: new Struct(this.schema.fields), length: header.length, children });
return new RecordBatch(this.schema, data, messageMetadata);
}
Expand Down
70 changes: 70 additions & 0 deletions test/unit/ipc/reader/field-node-length-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import {
Int32,
RecordBatchFileWriter,
RecordBatchReader,
RecordBatchStreamWriter,
Table,
vectorFromArray
} from 'apache-arrow';

describe('a record batch whose field node length disagrees with the batch', () => {

const table = new Table({
x: vectorFromArray([10, 20, 30, 40], new Int32()),
y: vectorFromArray([1, 2, 3, 4], new Int32())
});

// The field nodes of the batch are [{ length: 4, nullCount: 0 }, { length: 4, nullCount: 0 }]
// as little-endian int64 values.
const nodes = new Uint8Array(new BigInt64Array([4n, 0n, 4n, 0n]).buffer);

const placeOf = (bytes: Uint8Array, pattern: Uint8Array) => {
const places: number[] = [];
for (let at = 0; at + pattern.length <= bytes.length; ++at) {
if (pattern.every((byte, index) => bytes[at + index] === byte)) places.push(at);
}
if (places.length !== 1) throw new Error(`expected 1 location of the field nodes, found ${places.length}`);
return places[0];
};

const withLengthOfX = (bytes: Uint8Array, length: bigint) => {
const copy = bytes.slice();
new DataView(copy.buffer, copy.byteOffset).setBigInt64(placeOf(copy, nodes), length, true);
return copy;
};

for (const [format, bytes] of <const>[
['stream', RecordBatchStreamWriter.writeAll(table).toUint8Array(true)],
['file', RecordBatchFileWriter.writeAll(table).toUint8Array(true)]
]) {
for (const length of [2n, 0n]) {
test(`rejects a ${format} whose 'x' field node declares ${length} rows`, () => {
expect(() => [...RecordBatchReader.from(withLengthOfX(bytes, length))])
.toThrow(/'x'.*length (2|0).*(batch|declares 4)/i);
});
}
}

test('still reads a batch whose field nodes match the batch length', () => {
const bytes = RecordBatchStreamWriter.writeAll(table).toUint8Array(true);
const [batch] = [...RecordBatchReader.from(bytes)];
expect(Array.from(batch.getChild('x')!)).toEqual([10, 20, 30, 40]);
});
});