bkietz commented on a change in pull request #8050:
URL: https://github.com/apache/arrow/pull/8050#discussion_r482179398
##########
File path: cpp/src/arrow/ipc/dictionary.cc
##########
@@ -143,10 +143,43 @@ int DictionaryFieldMapper::num_fields() const { return
impl_->num_fields(); }
// DictionaryMemo implementation
struct DictionaryMemo::Impl {
- // Map of dictionary id to dictionary array
- std::unordered_map<int64_t, std::shared_ptr<ArrayData>> id_to_dictionary_;
+ // Map of dictionary id to dictionary array(s) (several in case of deltas)
+ std::unordered_map<int64_t, ArrayDataVector> id_to_dictionary_;
std::unordered_map<int64_t, std::shared_ptr<DataType>> id_to_type_;
DictionaryFieldMapper mapper_;
+
+ Result<decltype(id_to_dictionary_)::iterator> FindDictionary(int64_t id) {
+ auto it = id_to_dictionary_.find(id);
+ if (it == id_to_dictionary_.end()) {
+ return Status::KeyError("Dictionary with id ", id, " not found");
+ }
+ return it;
+ }
+
+ Result<std::shared_ptr<ArrayData>> ReifyDictionary(int64_t id, MemoryPool*
pool) {
+ ARROW_ASSIGN_OR_RAISE(auto it, FindDictionary(id));
+ ArrayDataVector* data_vector = &it->second;
+
+ DCHECK(!data_vector->empty());
+ if (data_vector->size() > 1) {
+ // There are deltas, we need to concatenate them to the first dictionary.
+ ArrayVector to_combine;
+ to_combine.reserve(data_vector->size());
+ // IMPORTANT: At this point, the dictionary data may be untrusted.
+ // We need to validate it, as concatenation can crash on invalid or
+ // corrupted data. Full validation is necessary for certain types
+ // (for example nested dictionaries).
Review comment:
IIUC it's also necessary for `string`. If not, it would probably be
worthwhile to add a fast branch for dictionaries of primitives
##########
File path: cpp/src/arrow/ipc/dictionary.cc
##########
@@ -143,10 +143,43 @@ int DictionaryFieldMapper::num_fields() const { return
impl_->num_fields(); }
// DictionaryMemo implementation
struct DictionaryMemo::Impl {
- // Map of dictionary id to dictionary array
- std::unordered_map<int64_t, std::shared_ptr<ArrayData>> id_to_dictionary_;
+ // Map of dictionary id to dictionary array(s) (several in case of deltas)
+ std::unordered_map<int64_t, ArrayDataVector> id_to_dictionary_;
std::unordered_map<int64_t, std::shared_ptr<DataType>> id_to_type_;
DictionaryFieldMapper mapper_;
+
+ Result<decltype(id_to_dictionary_)::iterator> FindDictionary(int64_t id) {
+ auto it = id_to_dictionary_.find(id);
+ if (it == id_to_dictionary_.end()) {
+ return Status::KeyError("Dictionary with id ", id, " not found");
+ }
+ return it;
+ }
+
+ Result<std::shared_ptr<ArrayData>> ReifyDictionary(int64_t id, MemoryPool*
pool) {
+ ARROW_ASSIGN_OR_RAISE(auto it, FindDictionary(id));
+ ArrayDataVector* data_vector = &it->second;
+
+ DCHECK(!data_vector->empty());
+ if (data_vector->size() > 1) {
+ // There are deltas, we need to concatenate them to the first dictionary.
+ ArrayVector to_combine;
+ to_combine.reserve(data_vector->size());
+ // IMPORTANT: At this point, the dictionary data may be untrusted.
+ // We need to validate it, as concatenation can crash on invalid or
+ // corrupted data. Full validation is necessary for certain types
+ // (for example nested dictionaries).
+ // XXX: this won't work if there are unresolved nested dictionaries.
Review comment:
Would it be worthwhile to add a test verifying that an unresolved nested
dictionary will emit an error instead of crashing?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]