Skip to content

Commit 8c1462d

Browse files
rustyconoverclaude
andcommitted
fix: throw on schema mismatch in RecordBatchStreamWriter.write()
Previously, writing a RecordBatch with a mismatched schema to a writer with autoDestroy=true (the default) would silently close the writer and drop the batch, causing data loss with no error or warning. Now throws an Error with both the expected and received schemas, making the mismatch immediately visible to the caller. Closes #388 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 15b0f0b commit 8c1462d

2 files changed

Lines changed: 150 additions & 2 deletions

File tree

src/ipc/writer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ import * as flatbuffers from 'flatbuffers';
4343

4444
export interface RecordBatchStreamWriterOptions {
4545
/**
46-
*
46+
* Whether to automatically close the writer when `finish()` is called.
47+
* When true (default), the writer closes after a single stream of batches.
48+
* When false, calling `finish()` resets the writer, allowing multiple
49+
* streams with different schemas to be written to the same sink.
4750
*/
4851
autoDestroy?: boolean;
4952
/**
@@ -200,7 +203,11 @@ export class RecordBatchWriter<T extends TypeMap = any> extends ReadableInterop<
200203

201204
if (schema && !compareSchemas(schema, this._schema)) {
202205
if (this._started && this._autoDestroy) {
203-
return this.close();
206+
throw new Error(
207+
`RecordBatch schema does not match the writer's schema.\n` +
208+
` Expected: ${this._schema}\n` +
209+
` Received: ${schema}`
210+
);
204211
}
205212
this.reset(this._sink, schema);
206213
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
import '../../../jest-extensions.js';
19+
import {
20+
Field,
21+
Float64,
22+
Int32,
23+
makeData,
24+
RecordBatch,
25+
RecordBatchStreamWriter,
26+
Schema,
27+
Struct,
28+
Utf8,
29+
} from 'apache-arrow';
30+
31+
function createBatch(schema: Schema, data: Record<string, any>, length: number): RecordBatch {
32+
const children = schema.fields.map(field => {
33+
const d = data[field.name];
34+
if (field.type instanceof Utf8) {
35+
return makeData({
36+
type: field.type,
37+
data: Buffer.from(d.values),
38+
valueOffsets: new Int32Array(d.offsets),
39+
});
40+
}
41+
return makeData({ type: field.type, data: d });
42+
});
43+
const structData = makeData({
44+
type: new Struct(schema.fields),
45+
length,
46+
nullCount: 0,
47+
children,
48+
});
49+
return new RecordBatch(schema, structData);
50+
}
51+
52+
describe('RecordBatchStreamWriter schema mismatch behavior', () => {
53+
54+
const schemaA = new Schema([
55+
new Field('id', new Int32()),
56+
new Field('name', new Utf8()),
57+
]);
58+
59+
const schemaB = new Schema([
60+
new Field('x', new Float64()),
61+
new Field('y', new Float64()),
62+
]);
63+
64+
function makeBatchA(): RecordBatch {
65+
return createBatch(schemaA, {
66+
id: new Int32Array([1, 2, 3]),
67+
name: { values: 'foobarbaz', offsets: [0, 3, 6, 9] },
68+
}, 3);
69+
}
70+
71+
function makeBatchB(): RecordBatch {
72+
return createBatch(schemaB, {
73+
x: new Float64Array([1.1, 2.2]),
74+
y: new Float64Array([3.3, 4.4]),
75+
}, 2);
76+
}
77+
78+
test('default (autoDestroy=true): writing a mismatched batch throws an error', () => {
79+
const writer = new RecordBatchStreamWriter(); // autoDestroy defaults to true
80+
const batchA = makeBatchA();
81+
const batchB = makeBatchB();
82+
83+
// Write the first batch — this establishes the schema
84+
writer.write(batchA);
85+
86+
// Write a batch with a different schema — this should throw
87+
expect(() => writer.write(batchB)).toThrow(
88+
'RecordBatch schema does not match the writer\'s schema.'
89+
);
90+
});
91+
92+
test('autoDestroy=false: writing a mismatched batch resets the schema and writes both batches as separate streams', async () => {
93+
const writer = new RecordBatchStreamWriter({ autoDestroy: false });
94+
const batchA = makeBatchA();
95+
const batchB = makeBatchB();
96+
97+
// Write first batch
98+
writer.write(batchA);
99+
100+
// Write batch with different schema — with autoDestroy=false,
101+
// this resets the writer to the new schema and writes the batch
102+
writer.write(batchB);
103+
writer.close();
104+
105+
// The output contains two separate IPC streams.
106+
// RecordBatchReader.readAll should be able to read both.
107+
const { RecordBatchReader } = await import('apache-arrow');
108+
const buffer = await writer.toUint8Array();
109+
const readers = [];
110+
for await (const reader of RecordBatchReader.readAll(buffer)) {
111+
readers.push(new (await import('apache-arrow')).Table(await reader.readAll()));
112+
}
113+
114+
expect(readers).toHaveLength(2);
115+
expect(readers[0].numRows).toBe(3);
116+
expect(readers[0].schema.fields.map(f => f.name)).toEqual(['id', 'name']);
117+
expect(readers[1].numRows).toBe(2);
118+
expect(readers[1].schema.fields.map(f => f.name)).toEqual(['x', 'y']);
119+
});
120+
121+
test('writing to a closed writer throws an error', () => {
122+
const writer = new RecordBatchStreamWriter();
123+
const batchA = makeBatchA();
124+
125+
writer.write(batchA);
126+
writer.close();
127+
128+
// Now try to write again — the writer is closed
129+
expect(() => writer.write(makeBatchA())).toThrow();
130+
});
131+
132+
test('schema mismatch error includes both schemas', () => {
133+
const writer = new RecordBatchStreamWriter();
134+
const batchA = makeBatchA();
135+
const batchB = makeBatchB();
136+
137+
writer.write(batchA);
138+
139+
expect(() => writer.write(batchB)).toThrow(/Expected:.*\n.*Received:/);
140+
});
141+
});

0 commit comments

Comments
 (0)