-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Add custom metadata support for IPC messages and RecordBatch #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rustyconover
wants to merge
3
commits into
apache:main
Choose a base branch
from
Query-farm:feat_recordbatch_metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -82,7 +82,8 @@ export class Message<T extends MessageHeader = any> { | |||||
| const bodyLength: bigint = _message.bodyLength()!; | ||||||
| const version: MetadataVersion = _message.version(); | ||||||
| const headerType: MessageHeader = _message.headerType(); | ||||||
| const message = new Message(bodyLength, version, headerType); | ||||||
| const metadata = decodeMessageCustomMetadata(_message); | ||||||
| const message = new Message(bodyLength, version, headerType, undefined, metadata); | ||||||
| message._createHeader = decodeMessageHeader(_message, headerType); | ||||||
| return message; | ||||||
| } | ||||||
|
|
@@ -98,22 +99,35 @@ export class Message<T extends MessageHeader = any> { | |||||
| } else if (message.isDictionaryBatch()) { | ||||||
| headerOffset = DictionaryBatch.encode(b, message.header() as DictionaryBatch); | ||||||
| } | ||||||
|
|
||||||
| // Encode custom metadata if present (must be done before startMessage) | ||||||
| const customMetadataOffset = !(message.metadata && message.metadata.size > 0) ? -1 : | ||||||
| _Message.createCustomMetadataVector(b, [...message.metadata].map(([k, v]) => { | ||||||
| const key = b.createString(`${k}`); | ||||||
| const val = b.createString(`${v}`); | ||||||
| _KeyValue.startKeyValue(b); | ||||||
| _KeyValue.addKey(b, key); | ||||||
| _KeyValue.addValue(b, val); | ||||||
| return _KeyValue.endKeyValue(b); | ||||||
| })); | ||||||
|
|
||||||
| _Message.startMessage(b); | ||||||
| _Message.addVersion(b, MetadataVersion.V5); | ||||||
| _Message.addHeader(b, headerOffset); | ||||||
| _Message.addHeaderType(b, message.headerType); | ||||||
| _Message.addBodyLength(b, BigInt(message.bodyLength)); | ||||||
| if (customMetadataOffset !== -1) { _Message.addCustomMetadata(b, customMetadataOffset); } | ||||||
| _Message.finishMessageBuffer(b, _Message.endMessage(b)); | ||||||
| return b.asUint8Array(); | ||||||
| } | ||||||
|
|
||||||
| /** @nocollapse */ | ||||||
| public static from(header: Schema | RecordBatch | DictionaryBatch, bodyLength = 0) { | ||||||
| public static from(header: Schema | RecordBatch | DictionaryBatch, bodyLength = 0, metadata?: Map<string, string>) { | ||||||
| if (header instanceof Schema) { | ||||||
| return new Message(0, MetadataVersion.V5, MessageHeader.Schema, header); | ||||||
| } | ||||||
| if (header instanceof RecordBatch) { | ||||||
| return new Message(bodyLength, MetadataVersion.V5, MessageHeader.RecordBatch, header); | ||||||
| return new Message(bodyLength, MetadataVersion.V5, MessageHeader.RecordBatch, header, metadata); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| if (header instanceof DictionaryBatch) { | ||||||
| return new Message(bodyLength, MetadataVersion.V5, MessageHeader.DictionaryBatch, header); | ||||||
|
|
@@ -126,24 +140,27 @@ export class Message<T extends MessageHeader = any> { | |||||
| protected _bodyLength: number; | ||||||
| protected _version: MetadataVersion; | ||||||
| protected _compression: BodyCompression | null; | ||||||
| protected _metadata: Map<string, string>; | ||||||
| public get type() { return this.headerType; } | ||||||
| public get version() { return this._version; } | ||||||
| public get headerType() { return this._headerType; } | ||||||
| public get compression() { return this._compression; } | ||||||
| public get bodyLength() { return this._bodyLength; } | ||||||
| public get metadata() { return this._metadata; } | ||||||
| declare protected _createHeader: MessageHeaderDecoder; | ||||||
| public header() { return this._createHeader<T>(); } | ||||||
| public isSchema(): this is Message<MessageHeader.Schema> { return this.headerType === MessageHeader.Schema; } | ||||||
| public isRecordBatch(): this is Message<MessageHeader.RecordBatch> { return this.headerType === MessageHeader.RecordBatch; } | ||||||
| public isDictionaryBatch(): this is Message<MessageHeader.DictionaryBatch> { return this.headerType === MessageHeader.DictionaryBatch; } | ||||||
|
|
||||||
| constructor(bodyLength: bigint | number, version: MetadataVersion, headerType: T, header?: any) { | ||||||
| constructor(bodyLength: bigint | number, version: MetadataVersion, headerType: T, header?: any, metadata?: Map<string, string>) { | ||||||
| this._version = version; | ||||||
| this._headerType = headerType; | ||||||
| this.body = new Uint8Array(0); | ||||||
| this._compression = header?.compression; | ||||||
| header && (this._createHeader = () => header); | ||||||
| this._bodyLength = bigIntToNumber(bodyLength); | ||||||
| this._metadata = metadata || new Map(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -468,6 +485,17 @@ function decodeCustomMetadata(parent?: _Schema | _Field | null) { | |||||
| return data; | ||||||
| } | ||||||
|
|
||||||
| /** @ignore */ | ||||||
| function decodeMessageCustomMetadata(message: _Message) { | ||||||
| const data = new Map<string, string>(); | ||||||
| for (let entry, key, i = -1, n = Math.trunc(message.customMetadataLength()); ++i < n;) { | ||||||
| if ((entry = message.customMetadata(i)) && (key = entry.key()) != null) { | ||||||
| data.set(key, entry.value()!); | ||||||
| } | ||||||
| } | ||||||
| return data; | ||||||
| } | ||||||
|
|
||||||
| /** @ignore */ | ||||||
| function decodeIndexType(_type: _Int) { | ||||||
| return new Int(_type.isSigned(), _type.bitWidth() as IntBitWidth); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the Message reads the metadata from the RecordBatch, this can all be simplified. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // 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 { readFileSync } from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { tableFromIPC, RecordBatch } from 'apache-arrow'; | ||
|
|
||
| // Path to the test file with message-level metadata | ||
| // Use process.cwd() since tests are run from project root | ||
| const testFilePath = path.resolve(process.cwd(), 'test/data/test_message_metadata.arrow'); | ||
|
|
||
| describe('RecordBatch message metadata', () => { | ||
| const buffer = readFileSync(testFilePath); | ||
| const table = tableFromIPC(buffer); | ||
|
|
||
| test('should read RecordBatch metadata from IPC file', () => { | ||
| expect(table.batches).toHaveLength(3); | ||
|
|
||
| for (let i = 0; i < table.batches.length; i++) { | ||
| const batch = table.batches[i]; | ||
| expect(batch).toBeInstanceOf(RecordBatch); | ||
| expect(batch.metadata).toBeInstanceOf(Map); | ||
| expect(batch.metadata.size).toBeGreaterThan(0); | ||
|
|
||
| // Verify specific metadata keys exist | ||
| expect(batch.metadata.has('batch_index')).toBe(true); | ||
| expect(batch.metadata.has('batch_id')).toBe(true); | ||
| expect(batch.metadata.has('producer')).toBe(true); | ||
|
|
||
| // Verify batch_index matches the batch position | ||
| expect(batch.metadata.get('batch_index')).toBe(String(i)); | ||
| expect(batch.metadata.get('batch_id')).toBe(`batch_${String(i).padStart(4, '0')}`); | ||
| } | ||
| }); | ||
|
|
||
| test('should read unicode metadata values', () => { | ||
| const batch = table.batches[0]; | ||
| expect(batch.metadata.has('unicode_test')).toBe(true); | ||
| expect(batch.metadata.get('unicode_test')).toBe('Hello 世界 🌍 مرحبا'); | ||
| }); | ||
|
|
||
| test('should handle empty metadata values', () => { | ||
| const batch = table.batches[0]; | ||
| expect(batch.metadata.has('optional_field')).toBe(true); | ||
| expect(batch.metadata.get('optional_field')).toBe(''); | ||
| }); | ||
|
|
||
| test('should read JSON metadata values', () => { | ||
| const batch = table.batches[0]; | ||
| expect(batch.metadata.has('batch_info_json')).toBe(true); | ||
| const jsonStr = batch.metadata.get('batch_info_json')!; | ||
| const parsed = JSON.parse(jsonStr); | ||
| expect(parsed.batch_number).toBe(0); | ||
| expect(parsed.processing_stage).toBe('final'); | ||
| expect(parsed.tags).toEqual(['validated', 'complete']); | ||
| }); | ||
|
|
||
| describe('metadata preservation', () => { | ||
| test('should preserve metadata through slice()', () => { | ||
| const batch = table.batches[0]; | ||
| const sliced = batch.slice(0, 2); | ||
| expect(sliced.metadata).toBeInstanceOf(Map); | ||
| expect(sliced.metadata.size).toBe(batch.metadata.size); | ||
| expect(sliced.metadata.get('batch_index')).toBe(batch.metadata.get('batch_index')); | ||
| }); | ||
|
|
||
| test('should preserve metadata through select()', () => { | ||
| const batch = table.batches[0]; | ||
| const selected = batch.select(['id', 'name']); | ||
| expect(selected.metadata).toBeInstanceOf(Map); | ||
| expect(selected.metadata.size).toBe(batch.metadata.size); | ||
| expect(selected.metadata.get('batch_index')).toBe(batch.metadata.get('batch_index')); | ||
| }); | ||
|
|
||
| test('should preserve metadata through selectAt()', () => { | ||
| const batch = table.batches[0]; | ||
| const selectedAt = batch.selectAt([0, 1]); | ||
| expect(selectedAt.metadata).toBeInstanceOf(Map); | ||
| expect(selectedAt.metadata.size).toBe(batch.metadata.size); | ||
| expect(selectedAt.metadata.get('batch_index')).toBe(batch.metadata.get('batch_index')); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the second metadata argument is only relevant if we're serializing a RecordBatch message, can we just use its metadata field instead?