From 819009fd888eccd95c0477b25d264ecf0412a4d0 Mon Sep 17 00:00:00 2001 From: Sealdot <48669444+Sealdot@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:50:01 +0800 Subject: [PATCH] fix: Reject record batch columns shorter than the batch Throw when a field node declares a length other than the record batch row count instead of backfilling the column with nulls. AI assistance: Cognition Devin CLI agent (SWE-2 High). --- src/ipc/reader.ts | 7 ++ .../ipc/reader/field-node-length-tests.ts | 70 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/unit/ipc/reader/field-node-length-tests.ts diff --git a/src/ipc/reader.ts b/src/ipc/reader.ts index d0edafd1..85fd491a 100644 --- a/src/ipc/reader.ts +++ b/src/ipc/reader.ts @@ -378,6 +378,13 @@ abstract class RecordBatchReaderImpl 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); } diff --git a/test/unit/ipc/reader/field-node-length-tests.ts b/test/unit/ipc/reader/field-node-length-tests.ts new file mode 100644 index 00000000..26b343bd --- /dev/null +++ b/test/unit/ipc/reader/field-node-length-tests.ts @@ -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 [ + ['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]); + }); +});