Skip to content
Open
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
35 changes: 29 additions & 6 deletions src/ipc/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,14 @@ class RecordBatchFileReaderImpl<T extends TypeMap = any> extends RecordBatchStre
public open(options?: OpenOptions) {
if (!this.closed && !this._footer) {
this.schema = (this._footer = this._readFooter()).schema;
for (const block of this._footer.dictionaryBatches()) {
block && this._readDictionaryBatch(this._dictionaryIndex++);
const initialDictionaryIds = new Set<number>();
try {
for (const block of this._footer.dictionaryBatches()) {
block && this._readDictionaryBatch(this._dictionaryIndex++, initialDictionaryIds);
}
} catch (error) {
this.cancel();
throw error;
}
}
return super.open(options);
Expand All @@ -646,12 +652,13 @@ class RecordBatchFileReaderImpl<T extends TypeMap = any> extends RecordBatchStre
}
return null;
}
protected _readDictionaryBatch(index: number) {
protected _readDictionaryBatch(index: number, initialDictionaryIds: Set<number>) {
const block = this._footer?.getDictionaryBatch(index);
if (block && this._handle.seek(block.offset)) {
const message = this._reader.readMessage(MessageHeader.DictionaryBatch);
if (message?.isDictionaryBatch()) {
const header = message.header();
registerFileDictionary(header, initialDictionaryIds);
const buffer = this._reader.readMessageBody(message.bodyLength);
const vector = this._loadDictionaryBatch(header, buffer);
this.dictionaries.set(header.id, vector);
Expand Down Expand Up @@ -699,8 +706,14 @@ class AsyncRecordBatchFileReaderImpl<T extends TypeMap = any> extends AsyncRecor
public async open(options?: OpenOptions) {
if (!this.closed && !this._footer) {
this.schema = (this._footer = await this._readFooter()).schema;
for (const block of this._footer.dictionaryBatches()) {
block && await this._readDictionaryBatch(this._dictionaryIndex++);
const initialDictionaryIds = new Set<number>();
try {
for (const block of this._footer.dictionaryBatches()) {
block && await this._readDictionaryBatch(this._dictionaryIndex++, initialDictionaryIds);
}
} catch (error) {
await this.cancel();
throw error;
}
}
return await super.open(options);
Expand All @@ -720,12 +733,13 @@ class AsyncRecordBatchFileReaderImpl<T extends TypeMap = any> extends AsyncRecor
}
return null;
}
protected async _readDictionaryBatch(index: number) {
protected async _readDictionaryBatch(index: number, initialDictionaryIds: Set<number>) {
const block = this._footer?.getDictionaryBatch(index);
if (block && (await this._handle.seek(block.offset))) {
const message = await this._reader.readMessage(MessageHeader.DictionaryBatch);
if (message?.isDictionaryBatch()) {
const header = message.header();
registerFileDictionary(header, initialDictionaryIds);
const buffer = await this._reader.readMessageBody(message.bodyLength);
const vector = this._loadDictionaryBatch(header, buffer);
this.dictionaries.set(header.id, vector);
Expand Down Expand Up @@ -768,6 +782,15 @@ class RecordBatchJSONReaderImpl<T extends TypeMap = any> extends RecordBatchStre
// being executed, so we've broken those out here for readability.
//

/** @ignore */
function registerFileDictionary(header: metadata.DictionaryBatch, initialDictionaryIds: Set<number>) {
if (header.isDelta) { return; }
if (initialDictionaryIds.has(header.id)) {
throw new Error(`Unsupported replacement dictionary in IPC file for id ${header.id}`);
}
initialDictionaryIds.add(header.id);
}

/** @ignore */
function shouldAutoDestroy(self: { autoDestroy: boolean }, options?: OpenOptions) {
return options && (typeof options['autoDestroy'] === 'boolean') ? options['autoDestroy'] : self['autoDestroy'];
Expand Down
53 changes: 52 additions & 1 deletion test/unit/ipc/reader/file-reader-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,61 @@ import {

import {
AsyncRecordBatchFileReader,
Dictionary,
Int32,
RecordBatch,
RecordBatchFileReader,
RecordBatchReader
RecordBatchFileWriter,
RecordBatchReader,
Table,
Utf8,
vectorFromArray
} from 'apache-arrow';

describe('file with a replacement dictionary', () => {
class ReplacementDictionaryWriter extends RecordBatchFileWriter {
protected override _writeDictionaries(batch: RecordBatch) {
// Emit a full dictionary in each batch, even for a previously written ID.
this._seenDictionaries.clear();
return super._writeDictionaries(batch);
}
}

const type = new Dictionary(new Utf8(), new Int32(), 7);
const values = vectorFromArray(['first'], type).concat(vectorFromArray(['second'], type));
const io = new class extends ArrowIOTestHelper {
protected writer(table: Table) { return new ReplacementDictionaryWriter().writeAll(table); }
}(new Table({ values }));

test('rejects the file when opened from bytes', io.buffer((bytes) => {
const reader = RecordBatchReader.from(bytes);
expect(() => reader.open()).toThrow(/replacement dictionar.*7/i);
expect(reader.closed).toBe(true);
}));

test('rejects the file when opened from a file handle', io.fsFileHandle(async (handle) => {
const reader = await RecordBatchReader.from(handle);
try {
await expect(reader.open()).rejects.toThrow(/replacement dictionar.*7/i);
expect(reader.closed).toBe(true);
} finally {
await reader.cancel();
}
}));
});

test('reads each file dictionary even if the reader already has values for that ID', () => {
const strings = new Utf8();
const left = vectorFromArray(['left'], new Dictionary(strings, new Int32(), 7));
const right = vectorFromArray(['right'], new Dictionary(strings, new Int32(), 8));
const bytes = RecordBatchFileWriter.writeAll(new Table({ left, right })).toUint8Array(true);
const reader = RecordBatchReader.from(bytes);
reader.dictionaries.set(7, vectorFromArray(['stale'], strings));
const [batch] = reader;
expect(batch.getChild('left')!.get(0)).toBe('left');
expect(batch.getChild('right')!.get(0)).toBe('right');
});

for (const table of generateRandomTables([10, 20, 30])) {

const io = ArrowIOTestHelper.file(table);
Expand Down