diff --git a/src/ipc/reader.ts b/src/ipc/reader.ts index d0edafd1..df03972c 100644 --- a/src/ipc/reader.ts +++ b/src/ipc/reader.ts @@ -507,6 +507,9 @@ class RecordBatchStreamReaderImpl extends RecordBatchRe const { _reader: reader } = this; while (message = this._readNextMessageAndValidate()) { if (message.isSchema()) { + if (this.schema) { + throw new Error(`Expected a DictionaryBatch or RecordBatch message in the IPC stream, but was a second Schema message.`); + } this.reset(message.header()); } else if (message.isRecordBatch()) { this._recordBatchIndex++; @@ -582,6 +585,9 @@ class AsyncRecordBatchStreamReaderImpl extends RecordBa const { _reader: reader } = this; while (message = await this._readNextMessageAndValidate()) { if (message.isSchema()) { + if (this.schema) { + throw new Error(`Expected a DictionaryBatch or RecordBatch message in the IPC stream, but was a second Schema message.`); + } await this.reset(message.header()); } else if (message.isRecordBatch()) { this._recordBatchIndex++; diff --git a/test/unit/ipc/reader/second-schema-tests.ts b/test/unit/ipc/reader/second-schema-tests.ts new file mode 100644 index 00000000..9c949e4a --- /dev/null +++ b/test/unit/ipc/reader/second-schema-tests.ts @@ -0,0 +1,102 @@ +// 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 { + Dictionary, + Int32, + Message, + MessageHeader, + RecordBatchReader, + RecordBatchStreamWriter, + Table, + Utf8, + tableFromArrays, + vectorFromArray +} from 'apache-arrow'; + +/** Splits an encapsulated IPC stream into its messages, framing each by its bodyLength. */ +const messagesOf = (bytes: Uint8Array) => { + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + const messages: { bytes: Uint8Array; header: MessageHeader }[] = []; + let position = 0; + while (position + 8 <= bytes.length) { + const metadataLength = view.getInt32(position + 4, true); + if (metadataLength === 0) break; + const message = Message.decode(bytes.subarray(position + 8, position + 8 + metadataLength)); + const end = position + 8 + metadataLength + message.bodyLength; + messages.push({ bytes: bytes.subarray(position, end), header: message.headerType }); + position = end; + } + return { messages, eos: bytes.subarray(position) }; +}; + +const concat = (...chunks: Uint8Array[]) => { + const bytes = new Uint8Array(chunks.reduce((n, c) => n + c.byteLength, 0)); + chunks.reduce((offset, c) => (bytes.set(c, offset), offset + c.byteLength), 0); + return bytes; +}; + +const EOS = Uint8Array.of(0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0); + +describe('an IPC stream with a second Schema message', () => { + + // A dictionary column ['a', 'b', 'a'] is a Schema, a DictionaryBatch and a RecordBatch. + const dictionaryStream = RecordBatchStreamWriter.writeAll( + new Table({ d: vectorFromArray(['a', 'b', 'a'], new Dictionary(new Utf8(), new Int32(), 0)) }) + ).toUint8Array(true); + const { messages: [schema, dictionary, batch], eos } = messagesOf(dictionaryStream); + + // Another stream's Schema message, for a column d of Int32 with no dictionary. + const otherSchema = messagesOf( + RecordBatchStreamWriter.writeAll(tableFromArrays({ d: Int32Array.of(0) })).toUint8Array(true) + ).messages.find((m) => m.header === MessageHeader.Schema)!; + + test('is rejected after the dictionary batch', () => { + const bytes = concat(schema.bytes, dictionary.bytes, otherSchema.bytes, batch.bytes, eos.byteLength ? eos : EOS); + expect(() => [...RecordBatchReader.from(bytes)]).toThrow(/second Schema/i); + }); + + test('is rejected after the first record batch', () => { + const bytes = concat(schema.bytes, dictionary.bytes, batch.bytes, otherSchema.bytes, eos.byteLength ? eos : EOS); + expect(() => [...RecordBatchReader.from(bytes)]).toThrow(/second Schema/i); + }); + + test('is rejected after the schema alone', () => { + const bytes = concat(schema.bytes, otherSchema.bytes, dictionary.bytes, batch.bytes, eos.byteLength ? eos : EOS); + expect(() => [...RecordBatchReader.from(bytes)]).toThrow(/second Schema/i); + }); + + test('is rejected by the async stream reader', async () => { + const bytes = concat(schema.bytes, dictionary.bytes, otherSchema.bytes, batch.bytes, eos.byteLength ? eos : EOS); + async function* source() { yield bytes; } + const reader = await RecordBatchReader.from(source()); + await reader.open(); + await expect(reader.next()).rejects.toThrow(/second Schema/i); + }); + + test('still reads a valid dictionary stream', () => { + const [batch] = [...RecordBatchReader.from(dictionaryStream)]; + expect(Array.from(batch.getChild('d')!)).toEqual(['a', 'b', 'a']); + }); + + test('still reads a stream lazily through the iterator', () => { + // Iterating a reader that was not opened consumes its first Schema message. + const reader = RecordBatchReader.from(dictionaryStream); + const [batch] = [...reader]; + expect(Array.from(batch.getChild('d')!)).toEqual(['a', 'b', 'a']); + }); +});