-
Notifications
You must be signed in to change notification settings - Fork 9
Add serialization helpers for Schema and RecordBatch #100
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // 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. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using Apache.Arrow.Flatbuf; | ||
| using Apache.Arrow.Ipc; | ||
|
|
||
| namespace Apache.Arrow | ||
| { | ||
| /// <summary> | ||
| /// Helpers for serializing partial Arrow structures to and from buffers. | ||
| /// </summary> | ||
| public static class ArrowSerializationHelpers | ||
| { | ||
| public static Schema DeserializeSchema(ReadOnlyMemory<byte> serializedSchema) | ||
| { | ||
| ArrowMemoryReaderImplementation implementation = new ArrowMemoryReaderImplementation(serializedSchema, null); | ||
| return implementation.Schema; | ||
| } | ||
|
|
||
| public static RecordBatch DeserializeRecordBatch(Schema schema, ReadOnlyMemory<byte> serializedRecordBatch) | ||
| { | ||
| ArrowMemoryReaderImplementation implementation = new ArrowMemoryReaderImplementation(schema, serializedRecordBatch, null); | ||
| return implementation.ReadNextRecordBatch(); | ||
| } | ||
|
|
||
| public static byte[] SerializeSchema(Schema schema) | ||
| { | ||
| using (var stream = new MemoryStream()) | ||
| { | ||
| var writer = new SchemaWriter(stream, schema); | ||
| writer.WriteSchema(schema); | ||
| return stream.ToArray(); | ||
| } | ||
| } | ||
|
|
||
| public static byte[] SerializeRecordBatch(RecordBatch recordBatch) | ||
| { | ||
| using (var stream = new MemoryStream()) | ||
| { | ||
| var writer = new SchemaWriter(stream, recordBatch.Schema); | ||
| writer.WriteBatch(recordBatch); | ||
| return stream.ToArray(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This class handles writing schemas | ||
|
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. It also writes RecordBatches. I can't think of a better name for it though. 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. I guess I didn't look that closely at the code I moved ;). |
||
| /// </summary> | ||
| internal class SchemaWriter : ArrowStreamWriter | ||
| { | ||
| internal SchemaWriter(Stream baseStream, Schema schema) : base(baseStream, schema) | ||
| { | ||
| } | ||
|
|
||
| public void WriteSchema(Schema schema) | ||
| { | ||
| var offset = base.SerializeSchema(schema); | ||
| WriteMessage(MessageHeader.Schema, offset, 0); | ||
| } | ||
|
|
||
| public void WriteBatch(RecordBatch recordBatch) | ||
| { | ||
| HasWrittenSchema = true; // Avoid serializing the schema | ||
| WriteRecordBatch(recordBatch); | ||
| WriteEnd(); | ||
| } | ||
|
|
||
| private protected override void StartingWritingDictionary() | ||
| { | ||
| throw new InvalidOperationException("Dictionary batches not supported"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,6 +184,7 @@ private unsafe static void ConvertRecordBatch(ExportedAllocationOwner sharedOwne | |
|
|
||
| cArray->n_buffers = 1; | ||
| cArray->buffers = (byte**)sharedOwner.Allocate(IntPtr.Size); | ||
| cArray->buffers[0] = null; | ||
|
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. This was already checked-in as a separate change. Will rebase if necessary. |
||
|
|
||
| cArray->n_children = batch.ColumnCount; | ||
| cArray->children = null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // 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. | ||
|
|
||
| using System.IO; | ||
| using Apache.Arrow.Ipc; | ||
| using Xunit; | ||
|
|
||
| namespace Apache.Arrow.Tests | ||
| { | ||
| public class SerializationHelperTests | ||
| { | ||
| [Fact] | ||
| public void SchemaRoundTrip() | ||
| { | ||
| RecordBatch originalBatch = TestData.CreateSampleRecordBatch(100); | ||
| var serialized = ArrowSerializationHelpers.SerializeSchema(originalBatch.Schema); | ||
| var deserialized = ArrowSerializationHelpers.DeserializeSchema(serialized); | ||
|
|
||
| SchemaComparer.Compare(originalBatch.Schema, deserialized); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RecordBatchRoundTrip() | ||
| { | ||
| RecordBatch originalBatch = TestData.CreateSampleRecordBatch(100, createDictionaryArray: false); | ||
| var serialized = ArrowSerializationHelpers.SerializeRecordBatch(originalBatch); | ||
| var deserialized = ArrowSerializationHelpers.DeserializeRecordBatch(originalBatch.Schema, serialized); | ||
|
|
||
| ArrowReaderVerifier.CompareBatches(originalBatch, deserialized); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConcatSchemaAndBatchWrite() | ||
| { | ||
| RecordBatch originalBatch = TestData.CreateSampleRecordBatch(100, createDictionaryArray: false); | ||
| var schema = ArrowSerializationHelpers.SerializeSchema(originalBatch.Schema); | ||
| var serialized = ArrowSerializationHelpers.SerializeRecordBatch(originalBatch); | ||
|
|
||
| var buffer = new byte[schema.Length + serialized.Length]; | ||
| System.Array.Copy(schema, buffer, schema.Length); | ||
| System.Array.Copy(serialized, 0, buffer, schema.Length, serialized.Length); | ||
|
|
||
| using (var stream = new MemoryStream(buffer)) | ||
| using (var reader = new ArrowStreamReader(stream)) | ||
| { | ||
| var deserialized = reader.ReadNextRecordBatch(); | ||
| ArrowReaderVerifier.CompareBatches(originalBatch, deserialized); | ||
| } | ||
| } | ||
| } | ||
| } |
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.
Should we just mark this as obsolete now? Otherwise there's no indication that users should switch to something else. Either way, it would be good to add a comment to direct users to use
ArrowSerializationHelpers.SerializeSchemainstead if we don't want people using this.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.
Yeah; I'd been thinking that marking it obsolete would break binary compatibility but of course that's not true so we may as well do it now.