-
Notifications
You must be signed in to change notification settings - Fork 4
Add the support of dictionnaries #91
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
Alex-PLACET
wants to merge
16
commits into
sparrow-org:main
Choose a base branch
from
Alex-PLACET:support_dict
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 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b304fc2
Add the support of dictionnaries
Alex-PLACET 715b455
add missing test
Alex-PLACET aca7a21
wip
Alex-PLACET df2a67c
simplify compute_fallback_dictionary_id
Alex-PLACET 150d3d5
add tests
Alex-PLACET c4c33a2
wip
Alex-PLACET 10c003d
wip
Alex-PLACET a516090
Apply suggestions from code review
Alex-PLACET aaddda6
wip
Alex-PLACET a260bdc
Merge branch 'support_dict' of https://github.com/Alex-PLACET/sparrow…
Alex-PLACET 4ae30af
wip
Alex-PLACET c952c39
wip
Alex-PLACET 22ff72c
try support delta dictionary
Alex-PLACET 436f1e7
fix
Alex-PLACET 73b4acd
improvement
Alex-PLACET 339af13
add delta dictionary deserialization test
Alex-PLACET 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
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,87 @@ | ||
| // Copyright 2024 Man Group Operations Limited | ||
| // | ||
| // Licensed 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <map> | ||
| #include <optional> | ||
|
|
||
| #include <sparrow/record_batch.hpp> | ||
|
|
||
| #include "sparrow_ipc/config/config.hpp" | ||
|
|
||
| namespace sparrow_ipc | ||
| { | ||
| /** | ||
| * @brief Caches dictionaries during deserialization. | ||
| * | ||
| * This class stores dictionaries received from DictionaryBatch messages and | ||
| * provides them when reconstructing dictionary-encoded arrays. It handles | ||
| * both delta updates (appending to existing dictionaries) and replacement | ||
| * (overwriting existing dictionaries). | ||
| * | ||
| * Dictionaries are stored as single-column record batches and are referenced | ||
| * by their integer ID. Multiple fields can share the same dictionary by | ||
| * referencing the same ID. | ||
| */ | ||
| class SPARROW_IPC_API dictionary_cache | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Store or update a dictionary. | ||
| * | ||
| * If is_delta is true and a dictionary with the given ID already exists, | ||
| * the new data is appended to the existing dictionary. Otherwise, the | ||
| * dictionary is replaced (or inserted if it doesn't exist). | ||
| * | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @param id The dictionary ID | ||
| * @param batch The dictionary data as a single-column record batch | ||
| * @param is_delta Whether to append (true) or replace (false) | ||
| * @throws std::invalid_argument if batch doesn't have exactly one column | ||
| */ | ||
| void store_dictionary(int64_t id, sparrow::record_batch batch, bool is_delta); | ||
|
|
||
| /** | ||
| * @brief Retrieve a cached dictionary. | ||
| * | ||
| * @param id The dictionary ID to retrieve | ||
| * @return An optional containing the dictionary if found, std::nullopt otherwise | ||
| */ | ||
| std::optional<std::reference_wrapper<const sparrow::record_batch>> get_dictionary(int64_t id) const; | ||
|
|
||
| /** | ||
| * @brief Check if a dictionary is cached. | ||
| * | ||
| * @param id The dictionary ID to check | ||
| * @return true if the dictionary exists in the cache, false otherwise | ||
| */ | ||
| bool contains(int64_t id) const; | ||
|
|
||
| /** | ||
| * @brief Clear all cached dictionaries. | ||
| */ | ||
| void clear(); | ||
|
|
||
| /** | ||
| * @brief Get the number of cached dictionaries. | ||
| * | ||
| * @return The number of dictionaries in the cache | ||
| */ | ||
| size_t size() const; | ||
|
|
||
| private: | ||
| std::map<int64_t, sparrow::record_batch> m_dictionaries; | ||
| }; | ||
| } | ||
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,93 @@ | ||
| // Copyright 2024 Man Group Operations Limited | ||
| // | ||
| // Licensed 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <set> | ||
| #include <vector> | ||
|
|
||
| #include <sparrow/record_batch.hpp> | ||
|
|
||
| #include "sparrow_ipc/config/config.hpp" | ||
|
|
||
| namespace sparrow_ipc | ||
| { | ||
| /** | ||
| * @brief Information about a dictionary used for encoding. | ||
| */ | ||
| struct dictionary_info | ||
| { | ||
| int64_t id; ///< Dictionary identifier | ||
| sparrow::record_batch data; ///< Dictionary values as a single-column record batch | ||
| bool is_ordered; ///< Whether dictionary values are ordered | ||
| bool is_delta; ///< Whether this is a delta update | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Tracks dictionaries during serialization. | ||
| * | ||
| * This class is responsible for discovering dictionary-encoded fields in record batches, | ||
| * extracting their dictionary data, and managing which dictionaries have been emitted | ||
| * to the output stream. | ||
| * | ||
| * Dictionaries must be emitted before any RecordBatch that references them. This class | ||
| * ensures proper ordering by tracking emitted dictionary IDs and providing methods to | ||
| * determine which dictionaries need to be sent before each record batch. | ||
| */ | ||
| class SPARROW_IPC_API dictionary_tracker | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Extract dictionaries from a record batch. | ||
| * | ||
| * Scans all columns in the record batch for dictionary-encoded fields. | ||
| * Returns a vector of dictionaries that need to be emitted before this | ||
| * record batch can be serialized. | ||
| * | ||
| * @param batch The record batch to scan for dictionaries | ||
| * @return Vector of dictionary_info for dictionaries that haven't been emitted yet | ||
| */ | ||
| std::vector<dictionary_info> extract_dictionaries_from_batch(const sparrow::record_batch& batch); | ||
|
|
||
| /** | ||
| * @brief Mark a dictionary as emitted. | ||
| * | ||
| * After a dictionary has been written to the stream, call this method to | ||
| * record that it has been emitted. This prevents re-emission of the same | ||
| * dictionary for subsequent record batches (unless it's a delta update). | ||
| * | ||
| * @param id The dictionary ID that was emitted | ||
| */ | ||
| void mark_emitted(int64_t id); | ||
|
|
||
| /** | ||
| * @brief Check if a dictionary has been emitted. | ||
| * | ||
| * @param id The dictionary ID to check | ||
| * @return true if the dictionary has been emitted, false otherwise | ||
| */ | ||
| bool is_emitted(int64_t id) const; | ||
|
|
||
| /** | ||
| * @brief Reset tracking state. | ||
| * | ||
| * Clears all tracking information. Useful when starting a new stream. | ||
| */ | ||
| void reset(); | ||
|
|
||
| private: | ||
| std::set<int64_t> m_emitted_dict_ids; ///< IDs of dictionaries already emitted | ||
| }; | ||
| } |
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,39 @@ | ||
| // Copyright 2024 Man Group Operations Limited | ||
| // | ||
| // Licensed 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <string_view> | ||
|
|
||
| #include <sparrow/c_interface.hpp> | ||
|
|
||
| #include "sparrow_ipc/config/config.hpp" | ||
|
|
||
| namespace sparrow_ipc | ||
| { | ||
| struct SPARROW_IPC_API dictionary_metadata | ||
| { | ||
| std::optional<int64_t> id; | ||
| bool is_ordered = false; | ||
| }; | ||
|
|
||
| [[nodiscard]] SPARROW_IPC_API int64_t | ||
| compute_fallback_dictionary_id(std::string_view field_name, size_t field_index); | ||
|
|
||
| [[nodiscard]] SPARROW_IPC_API dictionary_metadata | ||
| parse_dictionary_metadata(const ArrowSchema& schema); | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.