-
Notifications
You must be signed in to change notification settings - Fork 29
Support IPC Message custom_metadata in ArrowStreamWriter and ArrowStreamReader (rebased continuation of #283) #424
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
base: main
Are you sure you want to change the base?
Changes from all commits
a123f03
9260dfe
fcb77e5
0177e8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -806,6 +806,11 @@ public ArrowStreamWriter(Stream baseStream, Schema schema, bool leaveOpen, IpcOp | |
| } | ||
|
|
||
| private protected void WriteRecordBatchInternal(RecordBatch recordBatch) | ||
| { | ||
| WriteRecordBatchInternal(recordBatch, customMetadata: null); | ||
| } | ||
|
|
||
| private protected void WriteRecordBatchInternal(RecordBatch recordBatch, IReadOnlyDictionary<string, string> customMetadata) | ||
| { | ||
| // TODO: Truncate buffers with extraneous padding / unused capacity | ||
|
|
||
|
|
@@ -829,6 +834,15 @@ private protected void WriteRecordBatchInternal(RecordBatch recordBatch) | |
|
|
||
| VectorOffset buffersVectorOffset = Builder.EndVector(); | ||
|
|
||
| // Build custom metadata for the Message if provided | ||
| VectorOffset customMetadataVectorOffset = default; | ||
| if (customMetadata != null && customMetadata.Count > 0) | ||
| { | ||
| ValidateCustomMetadata(customMetadata); | ||
| Offset<Flatbuf.KeyValue>[] metadataOffsets = GetMetadataOffsets(customMetadata); | ||
| customMetadataVectorOffset = Flatbuf.Message.CreateCustomMetadataVector(Builder, metadataOffsets); | ||
| } | ||
|
|
||
| // Serialize record batch | ||
|
|
||
| StartingWritingRecordBatch(); | ||
|
|
@@ -840,14 +854,21 @@ private protected void WriteRecordBatchInternal(RecordBatch recordBatch) | |
| variadicCountsOffset); | ||
|
|
||
| long metadataLength = WriteMessage(Flatbuf.MessageHeader.RecordBatch, | ||
| recordBatchOffset, recordBatchBuilder.TotalLength); | ||
| recordBatchOffset, recordBatchBuilder.TotalLength, customMetadataVectorOffset); | ||
|
|
||
| long bufferLength = WriteBufferData(recordBatchBuilder.Buffers); | ||
|
|
||
| FinishedWritingRecordBatch(bufferLength, metadataLength); | ||
| } | ||
|
|
||
| private protected Task WriteRecordBatchInternalAsync(RecordBatch recordBatch, | ||
|
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. My suspicion is that it's better not to have this overload. We don't need it for backwards-compatibility and removing it might avoid an error in a derived class. |
||
| CancellationToken cancellationToken = default) | ||
| { | ||
| return WriteRecordBatchInternalAsync(recordBatch, customMetadata: null, cancellationToken); | ||
| } | ||
|
|
||
| private protected async Task WriteRecordBatchInternalAsync(RecordBatch recordBatch, | ||
| IReadOnlyDictionary<string, string> customMetadata, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (!HasWrittenSchema) | ||
|
|
@@ -870,6 +891,15 @@ private protected async Task WriteRecordBatchInternalAsync(RecordBatch recordBat | |
|
|
||
| VectorOffset buffersVectorOffset = Builder.EndVector(); | ||
|
|
||
| // Build custom metadata for the Message if provided | ||
| VectorOffset customMetadataVectorOffset = default; | ||
| if (customMetadata != null && customMetadata.Count > 0) | ||
| { | ||
| ValidateCustomMetadata(customMetadata); | ||
| Offset<Flatbuf.KeyValue>[] metadataOffsets = GetMetadataOffsets(customMetadata); | ||
| customMetadataVectorOffset = Flatbuf.Message.CreateCustomMetadataVector(Builder, metadataOffsets); | ||
| } | ||
|
|
||
| // Serialize record batch | ||
|
|
||
| StartingWritingRecordBatch(); | ||
|
|
@@ -882,6 +912,7 @@ private protected async Task WriteRecordBatchInternalAsync(RecordBatch recordBat | |
|
|
||
| long metadataLength = await WriteMessageAsync(Flatbuf.MessageHeader.RecordBatch, | ||
| recordBatchOffset, recordBatchBuilder.TotalLength, | ||
| customMetadataVectorOffset, | ||
| cancellationToken).ConfigureAwait(false); | ||
|
|
||
| long bufferLength = await WriteBufferDataAsync(recordBatchBuilder.Buffers, cancellationToken).ConfigureAwait(false); | ||
|
|
@@ -1132,11 +1163,21 @@ public virtual void WriteRecordBatch(RecordBatch recordBatch) | |
| WriteRecordBatchInternal(recordBatch); | ||
| } | ||
|
|
||
| public virtual void WriteRecordBatch(RecordBatch recordBatch, IReadOnlyDictionary<string, string> customMetadata) | ||
| { | ||
| WriteRecordBatchInternal(recordBatch, customMetadata); | ||
|
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. Based on my previous analysis of the original PR, this overload breaks |
||
| } | ||
|
|
||
| public virtual Task WriteRecordBatchAsync(RecordBatch recordBatch, CancellationToken cancellationToken = default) | ||
| { | ||
| return WriteRecordBatchInternalAsync(recordBatch, cancellationToken); | ||
| } | ||
|
|
||
| public virtual Task WriteRecordBatchAsync(RecordBatch recordBatch, IReadOnlyDictionary<string, string> customMetadata, CancellationToken cancellationToken = default) | ||
| { | ||
| return WriteRecordBatchInternalAsync(recordBatch, customMetadata, cancellationToken); | ||
| } | ||
|
|
||
| public void WriteStart() | ||
| { | ||
| if (!HasWrittenStart) | ||
|
|
@@ -1291,6 +1332,25 @@ private VectorOffset GetFieldMetadataOffset(Field field) | |
| return Flatbuf.DictionaryEncoding.CreateDictionaryEncoding(Builder, id, indexOffset, dicType.Ordered); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates that a caller-supplied custom metadata dictionary contains no null keys or values, | ||
| /// so that failures are reported clearly rather than as an opaque exception from the FlatBuffer builder. | ||
| /// </summary> | ||
| private static void ValidateCustomMetadata(IReadOnlyDictionary<string, string> customMetadata) | ||
| { | ||
| foreach (KeyValuePair<string, string> metadatum in customMetadata) | ||
| { | ||
| if (metadatum.Key == null) | ||
| { | ||
| throw new ArgumentException("Custom metadata must not contain null keys.", nameof(customMetadata)); | ||
| } | ||
| if (metadatum.Value == null) | ||
| { | ||
| throw new ArgumentException($"Custom metadata value for key '{metadatum.Key}' must not be null.", nameof(customMetadata)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Offset<Flatbuf.KeyValue>[] GetMetadataOffsets(IReadOnlyDictionary<string, string> metadata) | ||
| { | ||
| Debug.Assert(metadata != null); | ||
|
|
@@ -1347,12 +1407,13 @@ await WriteMessageAsync(Flatbuf.MessageHeader.Schema, schemaOffset, 0, cancellat | |
| /// The number of bytes written to the stream. | ||
| /// </returns> | ||
| private protected long WriteMessage<T>( | ||
| Flatbuf.MessageHeader headerType, Offset<T> headerOffset, int bodyLength) | ||
| Flatbuf.MessageHeader headerType, Offset<T> headerOffset, int bodyLength, | ||
| VectorOffset customMetadataOffset = default) | ||
| where T : struct | ||
| { | ||
| Offset<Flatbuf.Message> messageOffset = Flatbuf.Message.CreateMessage( | ||
| Builder, CurrentMetadataVersion, headerType, headerOffset.Value, | ||
| bodyLength); | ||
| bodyLength, customMetadataOffset); | ||
|
|
||
| Builder.Finish(messageOffset.Value); | ||
|
|
||
|
|
@@ -1376,14 +1437,23 @@ private protected long WriteMessage<T>( | |
| /// <returns> | ||
| /// The number of bytes written to the stream. | ||
| /// </returns> | ||
| private protected virtual ValueTask<long> WriteMessageAsync<T>( | ||
|
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. Consider making this non-virtual or removing it entirely and forcing use of the signature with the |
||
| Flatbuf.MessageHeader headerType, Offset<T> headerOffset, int bodyLength, | ||
| CancellationToken cancellationToken) | ||
| where T : struct | ||
| { | ||
| return WriteMessageAsync(headerType, headerOffset, bodyLength, default, cancellationToken); | ||
| } | ||
|
|
||
| private protected virtual async ValueTask<long> WriteMessageAsync<T>( | ||
| Flatbuf.MessageHeader headerType, Offset<T> headerOffset, int bodyLength, | ||
| VectorOffset customMetadataOffset, | ||
| CancellationToken cancellationToken) | ||
| where T : struct | ||
| { | ||
| Offset<Flatbuf.Message> messageOffset = Flatbuf.Message.CreateMessage( | ||
| Builder, CurrentMetadataVersion, headerType, headerOffset.Value, | ||
| bodyLength); | ||
| bodyLength, customMetadataOffset); | ||
|
|
||
| Builder.Finish(messageOffset.Value); | ||
|
|
||
|
|
||
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.
Fixed in 0177e8b: added a
ValidateCustomMetadatacheck that throws a clearArgumentExceptionfor null keys/values before building the FlatBuffer offsets.