forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_dict.cc
More file actions
344 lines (289 loc) · 11.9 KB
/
Copy pathbuilder_dict.cc
File metadata and controls
344 lines (289 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// 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.
#include "arrow/array/builder_dict.h"
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
#include <vector>
#include "arrow/array.h"
#include "arrow/buffer.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/hashing.h"
#include "arrow/util/logging.h"
#include "arrow/visitor_inline.h"
namespace arrow {
using internal::checked_cast;
// ----------------------------------------------------------------------
// DictionaryType unification
template <typename T, typename Out = void>
using enable_if_memoize = typename std::enable_if<
!std::is_same<typename internal::DictionaryTraits<T>::MemoTableType, void>::value,
Out>::type;
template <typename T, typename Out = void>
using enable_if_no_memoize = typename std::enable_if<
std::is_same<typename internal::DictionaryTraits<T>::MemoTableType, void>::value,
Out>::type;
struct UnifyDictionaryValues {
MemoryPool* pool_;
std::shared_ptr<DataType> value_type_;
const std::vector<const DictionaryType*>& types_;
const std::vector<const Array*>& dictionaries_;
std::shared_ptr<Array>* out_values_;
std::vector<std::vector<int32_t>>* out_transpose_maps_;
template <typename T>
enable_if_no_memoize<T, Status> Visit(const T&) {
// Default implementation for non-dictionary-supported datatypes
return Status::NotImplemented("Unification of ", value_type_,
" dictionaries is not implemented");
}
template <typename T>
enable_if_memoize<T, Status> Visit(const T&) {
using ArrayType = typename TypeTraits<T>::ArrayType;
using DictTraits = typename internal::DictionaryTraits<T>;
using MemoTableType = typename DictTraits::MemoTableType;
MemoTableType memo_table(pool_);
if (out_transpose_maps_ != nullptr) {
out_transpose_maps_->clear();
out_transpose_maps_->reserve(types_.size());
}
// Build up the unified dictionary values and the transpose maps
for (size_t i = 0; i < types_.size(); ++i) {
const ArrayType& values = checked_cast<const ArrayType&>(*dictionaries_[i]);
if (out_transpose_maps_ != nullptr) {
std::vector<int32_t> transpose_map;
transpose_map.reserve(values.length());
for (int64_t i = 0; i < values.length(); ++i) {
int32_t dict_index = memo_table.GetOrInsert(values.GetView(i));
transpose_map.push_back(dict_index);
}
out_transpose_maps_->push_back(std::move(transpose_map));
} else {
for (int64_t i = 0; i < values.length(); ++i) {
memo_table.GetOrInsert(values.GetView(i));
}
}
}
// Build unified dictionary array
std::shared_ptr<ArrayData> data;
RETURN_NOT_OK(DictTraits::GetDictionaryArrayData(pool_, value_type_, memo_table,
0 /* start_offset */, &data));
*out_values_ = MakeArray(data);
return Status::OK();
}
};
Status DictionaryType::Unify(MemoryPool* pool, const std::vector<const DataType*>& types,
const std::vector<const Array*>& dictionaries,
std::shared_ptr<DataType>* out_type,
std::shared_ptr<Array>* out_dictionary,
std::vector<std::vector<int32_t>>* out_transpose_maps) {
if (types.size() == 0) {
return Status::Invalid("need at least one input type");
}
if (types.size() != dictionaries.size()) {
return Status::Invalid("expecting the same number of types and dictionaries");
}
std::vector<const DictionaryType*> dict_types;
dict_types.reserve(types.size());
for (const auto& type : types) {
if (type->id() != Type::DICTIONARY) {
return Status::TypeError("input types must be dictionary types");
}
dict_types.push_back(checked_cast<const DictionaryType*>(type));
}
// XXX Should we check the ordered flag?
auto value_type = dict_types[0]->value_type();
for (size_t i = 0; i < types.size(); ++i) {
if (!(dictionaries[i]->type()->Equals(*value_type) &&
dict_types[i]->value_type()->Equals(*value_type))) {
return Status::TypeError("dictionary value types were not all consistent");
}
if (dictionaries[i]->null_count() != 0) {
return Status::TypeError("input types have null values");
}
}
std::shared_ptr<Array> values;
{
UnifyDictionaryValues visitor{pool, value_type, dict_types,
dictionaries, &values, out_transpose_maps};
RETURN_NOT_OK(VisitTypeInline(*value_type, &visitor));
}
// Build unified dictionary type with the right index type
std::shared_ptr<DataType> index_type;
if (values->length() <= std::numeric_limits<int8_t>::max()) {
index_type = int8();
} else if (values->length() <= std::numeric_limits<int16_t>::max()) {
index_type = int16();
} else if (values->length() <= std::numeric_limits<int32_t>::max()) {
index_type = int32();
} else {
index_type = int64();
}
*out_type = arrow::dictionary(index_type, values->type());
*out_dictionary = values;
return Status::OK();
}
// ----------------------------------------------------------------------
// DictionaryBuilder
class internal::DictionaryMemoTable::DictionaryMemoTableImpl {
struct MemoTableInitializer {
std::shared_ptr<DataType> value_type_;
std::unique_ptr<MemoTable>* memo_table_;
template <typename T>
enable_if_no_memoize<T, Status> Visit(const T&) {
return Status::NotImplemented("Initialization of ", value_type_,
" memo table is not implemented");
}
template <typename T>
enable_if_memoize<T, Status> Visit(const T&) {
using MemoTable = typename internal::DictionaryTraits<T>::MemoTableType;
// TODO(fsaintjacques): Propagate memory pool
memo_table_->reset(new MemoTable(default_memory_pool(), 0));
return Status::OK();
}
};
struct ArrayValuesInserter {
DictionaryMemoTableImpl* impl_;
const Array& values_;
template <typename T>
Status Visit(const T& type) {
using ArrayType = typename TypeTraits<T>::ArrayType;
return InsertValues(type, checked_cast<const ArrayType&>(values_));
}
private:
template <typename DType, typename ArrayType>
enable_if_no_memoize<DType, Status> InsertValues(const DType& type,
const ArrayType&) {
return Status::NotImplemented("Inserting array values of ", type,
" is not implemented");
}
template <typename DType, typename ArrayType>
enable_if_memoize<DType, Status> InsertValues(const DType&, const ArrayType& array) {
for (int64_t i = 0; i < array.length(); ++i) {
ARROW_IGNORE_EXPR(impl_->GetOrInsert(array.GetView(i)));
}
return Status::OK();
}
};
struct ArrayDataGetter {
std::shared_ptr<DataType> value_type_;
MemoTable* memo_table_;
MemoryPool* pool_;
int64_t start_offset_;
std::shared_ptr<ArrayData>* out_;
template <typename T>
enable_if_no_memoize<T, Status> Visit(const T&) {
return Status::NotImplemented("Getting array data of ", value_type_,
" is not implemented");
}
template <typename T>
enable_if_memoize<T, Status> Visit(const T&) {
using ConcreteMemoTable = typename internal::DictionaryTraits<T>::MemoTableType;
auto memo_table = static_cast<ConcreteMemoTable*>(memo_table_);
return internal::DictionaryTraits<T>::GetDictionaryArrayData(
pool_, value_type_, *memo_table, start_offset_, out_);
}
};
public:
explicit DictionaryMemoTableImpl(const std::shared_ptr<DataType>& type)
: type_(type), memo_table_(nullptr) {
MemoTableInitializer visitor{type_, &memo_table_};
ARROW_IGNORE_EXPR(VisitTypeInline(*type_, &visitor));
}
Status InsertValues(const Array& array) {
if (!array.type()->Equals(*type_)) {
return Status::Invalid("Array value type does not match memo type: ",
array.type()->ToString());
}
ArrayValuesInserter visitor{this, array};
return VisitTypeInline(*array.type(), &visitor);
}
template <typename T>
int32_t GetOrInsert(const T& value) {
using ConcreteMemoTable = typename internal::DictionaryTraits<
typename CTypeTraits<T>::ArrowType>::MemoTableType;
return static_cast<ConcreteMemoTable*>(memo_table_.get())->GetOrInsert(value);
}
int32_t GetOrInsert(const util::string_view& value) {
return static_cast<BinaryMemoTable*>(memo_table_.get())->GetOrInsert(value);
}
Status GetArrayData(MemoryPool* pool, int64_t start_offset,
std::shared_ptr<ArrayData>* out) {
ArrayDataGetter visitor{type_, memo_table_.get(), pool, start_offset, out};
return VisitTypeInline(*type_, &visitor);
}
int32_t size() const { return memo_table_->size(); }
private:
std::shared_ptr<DataType> type_;
std::unique_ptr<MemoTable> memo_table_;
};
internal::DictionaryMemoTable::DictionaryMemoTable(const std::shared_ptr<DataType>& type)
: impl_(new DictionaryMemoTableImpl(type)) {}
internal::DictionaryMemoTable::DictionaryMemoTable(
const std::shared_ptr<Array>& dictionary)
: impl_(new DictionaryMemoTableImpl(dictionary->type())) {
ARROW_IGNORE_EXPR(impl_->InsertValues(*dictionary));
}
internal::DictionaryMemoTable::~DictionaryMemoTable() = default;
int32_t internal::DictionaryMemoTable::GetOrInsert(const bool& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const int8_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const int16_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const int32_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const int64_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const uint8_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const uint16_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const uint32_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const uint64_t& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const float& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const double& value) {
return impl_->GetOrInsert(value);
}
int32_t internal::DictionaryMemoTable::GetOrInsert(const util::string_view& value) {
return impl_->GetOrInsert(value);
}
Status internal::DictionaryMemoTable::GetArrayData(MemoryPool* pool, int64_t start_offset,
std::shared_ptr<ArrayData>* out) {
return impl_->GetArrayData(pool, start_offset, out);
}
Status internal::DictionaryMemoTable::InsertValues(const Array& array) {
return impl_->InsertValues(array);
}
int32_t internal::DictionaryMemoTable::size() const { return impl_->size(); }
} // namespace arrow