-
Notifications
You must be signed in to change notification settings - Fork 4
Add serialize #22
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
Merged
JohanMabille
merged 1 commit into
QuantStack:main
from
Alex-PLACET:serialize_record_batch
Sep 25, 2025
Merged
Add serialize #22
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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
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,64 @@ | ||
| #pragma once | ||
|
|
||
| #include <ostream> | ||
| #include <ranges> | ||
| #include <vector> | ||
|
|
||
| #include <sparrow/record_batch.hpp> | ||
|
|
||
| #include "Message_generated.h" | ||
| #include "sparrow_ipc/config/config.hpp" | ||
| #include "sparrow_ipc/magic_values.hpp" | ||
| #include "sparrow_ipc/serialize_utils.hpp" | ||
| #include "sparrow_ipc/utils.hpp" | ||
|
|
||
| namespace sparrow_ipc | ||
| { | ||
| /** | ||
| * @brief Serializes a collection of record batches into a binary format. | ||
| * | ||
| * This function takes a collection of record batches and serializes them into a single | ||
| * binary representation following the Arrow IPC format. The serialization includes: | ||
| * - Schema message (derived from the first record batch) | ||
| * - All record batch data | ||
| * - End-of-stream marker | ||
| * | ||
| * @tparam R Container type that holds record batches (must support empty(), operator[], begin(), end()) | ||
| * @param record_batches Collection of record batches to serialize. All batches must have identical | ||
| * schemas. | ||
| * | ||
| * @return std::vector<uint8_t> Binary serialized data containing schema, record batches, and | ||
| * end-of-stream marker. Returns empty vector if input collection is empty. | ||
| * | ||
| * @throws std::invalid_argument If record batches have inconsistent schemas or if the collection | ||
| * contains batches that cannot be serialized together. | ||
| * | ||
| * @pre All record batches in the collection must have the same schema | ||
| * @pre The container R must not be empty when consistency checking is required | ||
| */ | ||
| template <std::ranges::input_range R> | ||
| requires std::same_as<std::ranges::range_value_t<R>, sparrow::record_batch> | ||
| std::vector<uint8_t> serialize(const R& record_batches) | ||
| { | ||
| if (record_batches.empty()) | ||
| { | ||
| return {}; | ||
| } | ||
| if (!utils::check_record_batches_consistency(record_batches)) | ||
| { | ||
| throw std::invalid_argument( | ||
| "All record batches must have the same schema to be serialized together." | ||
| ); | ||
| } | ||
| std::vector<uint8_t> serialized_schema = serialize_schema_message(record_batches[0]); | ||
| std::vector<uint8_t> serialized_record_batches = serialize_record_batches_without_schema_message(record_batches); | ||
| serialized_schema.insert( | ||
| serialized_schema.end(), | ||
| std::make_move_iterator(serialized_record_batches.begin()), | ||
| std::make_move_iterator(serialized_record_batches.end()) | ||
| ); | ||
| // End of stream message | ||
| serialized_schema.insert(serialized_schema.end(), end_of_stream.begin(), end_of_stream.end()); | ||
| return serialized_schema; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
I think you want a streaming interface at some point, so that not all record batches have to be materialized before serializing.
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.
Yes, but it's a first step, we will support streaming later in another PR
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.
Ok, but all the internals in the PR are architected around the idea of serializing all batches at once to a single vector. So you'll have to refactor all of 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.
The "low level" methods work on single batches, so I guess we could add a method accepting a single batch that would directly call them. Or if we can optimize this implementation such that serializing a vector of a single record_batch is equivalent to serializing a record_batch directly, we can add something like:
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.
This is addressed in the next PR