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
345
346
347
348
|
// 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_run_end.h"
#include "arrow/array/builder_primitive.h"
#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>
#include "arrow/scalar.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/int_util_overflow.h"
#include "arrow/util/logging.h"
#include "arrow/util/ree_util.h"
namespace arrow20 {
namespace internal {
RunCompressorBuilder::RunCompressorBuilder(MemoryPool* pool,
std::shared_ptr<ArrayBuilder> inner_builder,
std::shared_ptr<DataType> type)
: ArrayBuilder(pool), inner_builder_(std::move(inner_builder)) {}
RunCompressorBuilder::~RunCompressorBuilder() = default;
void RunCompressorBuilder::Reset() {
current_run_length_ = 0;
current_value_.reset();
inner_builder_->Reset();
UpdateDimensions();
}
Status RunCompressorBuilder::ResizePhysical(int64_t capacity) {
RETURN_NOT_OK(inner_builder_->Resize(capacity));
UpdateDimensions();
return Status::OK();
}
Status RunCompressorBuilder::AppendNulls(int64_t length) {
if (ARROW_PREDICT_FALSE(length == 0)) {
return Status::OK();
}
if (ARROW_PREDICT_FALSE(current_run_length_ == 0)) {
// Open a new NULL run
DCHECK_EQ(current_value_, NULLPTR);
current_run_length_ = length;
} else if (current_value_ == NULLPTR) {
// Extend the currently open NULL run
current_run_length_ += length;
} else {
// Close then non-NULL run
ARROW_RETURN_NOT_OK(WillCloseRun(current_value_, current_run_length_));
ARROW_RETURN_NOT_OK(inner_builder_->AppendScalar(*current_value_));
UpdateDimensions();
// Open a new NULL run
current_value_.reset();
current_run_length_ = length;
}
return Status::OK();
}
Status RunCompressorBuilder::AppendEmptyValues(int64_t length) {
if (ARROW_PREDICT_FALSE(length == 0)) {
return Status::OK();
}
// Empty values are usually appended as placeholders for future values, so
// we make no attempt at making the empty values appended now part of the
// current run. Each AppendEmptyValues() creates its own run of the given length.
ARROW_RETURN_NOT_OK(FinishCurrentRun());
{
ARROW_RETURN_NOT_OK(WillCloseRunOfEmptyValues(length));
ARROW_RETURN_NOT_OK(inner_builder_->AppendEmptyValue());
UpdateDimensions();
}
// Current run remains cleared after FinishCurrentRun() as we don't want to
// extend it with empty values potentially coming in the future.
return Status::OK();
}
Status RunCompressorBuilder::AppendScalar(const Scalar& scalar, int64_t n_repeats) {
if (ARROW_PREDICT_FALSE(n_repeats == 0)) {
return Status::OK();
}
if (ARROW_PREDICT_FALSE(current_run_length_ == 0)) {
// Open a new run
current_value_ = scalar.is_valid ? scalar.shared_from_this() : NULLPTR;
current_run_length_ = n_repeats;
} else if ((current_value_ == NULLPTR && !scalar.is_valid) ||
(current_value_ != NULLPTR && current_value_->Equals(scalar))) {
// Extend the currently open run
current_run_length_ += n_repeats;
} else {
// Close the current run
ARROW_RETURN_NOT_OK(WillCloseRun(current_value_, current_run_length_));
ARROW_RETURN_NOT_OK(current_value_ ? inner_builder_->AppendScalar(*current_value_)
: inner_builder_->AppendNull());
UpdateDimensions();
// Open a new run
current_value_ = scalar.is_valid ? scalar.shared_from_this() : NULLPTR;
current_run_length_ = n_repeats;
}
return Status::OK();
}
Status RunCompressorBuilder::AppendScalars(const ScalarVector& scalars) {
if (scalars.empty()) {
return Status::OK();
}
RETURN_NOT_OK(ArrayBuilder::AppendScalars(scalars));
UpdateDimensions();
return Status::OK();
}
Status RunCompressorBuilder::AppendRunCompressedArraySlice(
const ArraySpan& run_compressed_array, int64_t offset, int64_t length) {
DCHECK(!has_open_run());
RETURN_NOT_OK(inner_builder_->AppendArraySlice(run_compressed_array, offset, length));
UpdateDimensions();
return Status::OK();
}
Status RunCompressorBuilder::FinishCurrentRun() {
if (current_run_length_ > 0) {
// Close the current run
ARROW_RETURN_NOT_OK(WillCloseRun(current_value_, current_run_length_));
ARROW_RETURN_NOT_OK(current_value_ ? inner_builder_->AppendScalar(*current_value_)
: inner_builder_->AppendNull());
UpdateDimensions();
// Clear the current run
current_value_.reset();
current_run_length_ = 0;
}
return Status::OK();
}
Status RunCompressorBuilder::FinishInternal(std::shared_ptr<ArrayData>* out) {
ARROW_RETURN_NOT_OK(FinishCurrentRun());
return inner_builder_->FinishInternal(out);
}
} // namespace internal
// ----------------------------------------------------------------------
// RunEndEncodedBuilder
RunEndEncodedBuilder::ValueRunBuilder::ValueRunBuilder(
MemoryPool* pool, const std::shared_ptr<ArrayBuilder>& value_builder,
const std::shared_ptr<DataType>& value_type, RunEndEncodedBuilder& ree_builder)
: RunCompressorBuilder(pool, value_builder, value_type), ree_builder_(ree_builder) {}
RunEndEncodedBuilder::RunEndEncodedBuilder(
MemoryPool* pool, const std::shared_ptr<ArrayBuilder>& run_end_builder,
const std::shared_ptr<ArrayBuilder>& value_builder, std::shared_ptr<DataType> type)
: ArrayBuilder(pool), type_(internal::checked_pointer_cast<RunEndEncodedType>(type)) {
auto value_run_builder =
std::make_shared<ValueRunBuilder>(pool, value_builder, type_->value_type(), *this);
value_run_builder_ = value_run_builder.get();
children_ = {run_end_builder, std::move(value_run_builder)};
UpdateDimensions(0, 0);
null_count_ = 0;
}
Status RunEndEncodedBuilder::ResizePhysical(int64_t capacity) {
RETURN_NOT_OK(value_run_builder_->ResizePhysical(capacity));
RETURN_NOT_OK(run_end_builder().Resize(capacity));
UpdateDimensions(committed_logical_length_, 0);
return Status::OK();
}
void RunEndEncodedBuilder::Reset() {
value_run_builder_->Reset();
run_end_builder().Reset();
UpdateDimensions(0, 0);
}
Status RunEndEncodedBuilder::AppendNulls(int64_t length) {
RETURN_NOT_OK(value_run_builder_->AppendNulls(length));
UpdateDimensions(committed_logical_length_, value_run_builder_->open_run_length());
return Status::OK();
}
Status RunEndEncodedBuilder::AppendEmptyValues(int64_t length) {
RETURN_NOT_OK(value_run_builder_->AppendEmptyValues(length));
DCHECK_EQ(value_run_builder_->open_run_length(), 0);
UpdateDimensions(committed_logical_length_, 0);
return Status::OK();
}
Status RunEndEncodedBuilder::AppendScalar(const Scalar& scalar, int64_t n_repeats) {
if (scalar.type->id() == Type::RUN_END_ENCODED) {
return AppendScalar(*internal::checked_cast<const RunEndEncodedScalar&>(scalar).value,
n_repeats);
}
RETURN_NOT_OK(value_run_builder_->AppendScalar(scalar, n_repeats));
UpdateDimensions(committed_logical_length_, value_run_builder_->open_run_length());
return Status::OK();
}
Status RunEndEncodedBuilder::AppendScalars(const ScalarVector& scalars) {
RETURN_NOT_OK(this->ArrayBuilder::AppendScalars(scalars));
UpdateDimensions(committed_logical_length_, value_run_builder_->open_run_length());
return Status::OK();
}
template <typename RunEndCType>
Status RunEndEncodedBuilder::DoAppendArraySlice(const ArraySpan& array, int64_t offset,
int64_t length) {
ARROW_DCHECK(offset + length <= array.length);
DCHECK_GT(length, 0);
DCHECK(!value_run_builder_->has_open_run());
ree_util::RunEndEncodedArraySpan<RunEndCType> ree_span(array, array.offset + offset,
length);
const int64_t physical_offset = ree_span.PhysicalIndex(0);
const int64_t physical_length =
ree_span.PhysicalIndex(ree_span.length() - 1) + 1 - physical_offset;
RETURN_NOT_OK(ReservePhysical(physical_length));
// Append all the run ends from array sliced by offset and length
for (auto it = ree_span.iterator(0, physical_offset); !it.is_end(ree_span); ++it) {
const int64_t run_end = committed_logical_length_ + it.run_length();
RETURN_NOT_OK(DoAppendRunEnd<RunEndCType>(run_end));
UpdateDimensions(run_end, 0);
}
// Append all the values directly
RETURN_NOT_OK(value_run_builder_->AppendRunCompressedArraySlice(
ree_util::ValuesArray(array), physical_offset, physical_length));
return Status::OK();
}
Status RunEndEncodedBuilder::AppendArraySlice(const ArraySpan& array, int64_t offset,
int64_t length) {
ARROW_DCHECK(array.type->Equals(type_));
// Ensure any open run is closed before appending the array slice.
RETURN_NOT_OK(value_run_builder_->FinishCurrentRun());
if (length == 0) {
return Status::OK();
}
switch (type_->run_end_type()->id()) {
case Type::INT16:
RETURN_NOT_OK(DoAppendArraySlice<int16_t>(array, offset, length));
break;
case Type::INT32:
RETURN_NOT_OK(DoAppendArraySlice<int32_t>(array, offset, length));
break;
case Type::INT64:
RETURN_NOT_OK(DoAppendArraySlice<int64_t>(array, offset, length));
break;
default:
return Status::Invalid("Invalid type for run ends array: ", type_->run_end_type());
}
return Status::OK();
}
std::shared_ptr<DataType> RunEndEncodedBuilder::type() const { return type_; }
Status RunEndEncodedBuilder::FinishInternal(std::shared_ptr<ArrayData>* out) {
// Finish the values array before so we can close the current run and append
// the last run-end.
std::shared_ptr<ArrayData> values_data;
RETURN_NOT_OK(value_run_builder_->FinishInternal(&values_data));
auto values_array = MakeArray(values_data);
ARROW_ASSIGN_OR_RAISE(auto run_ends_array, run_end_builder().Finish());
ARROW_ASSIGN_OR_RAISE(auto ree_array,
RunEndEncodedArray::Make(length_, run_ends_array, values_array));
*out = std::move(ree_array->data());
return Status::OK();
}
Status RunEndEncodedBuilder::FinishCurrentRun() {
RETURN_NOT_OK(value_run_builder_->FinishCurrentRun());
UpdateDimensions(length_, 0);
return Status::OK();
}
template <typename RunEndCType>
Status RunEndEncodedBuilder::DoAppendRunEnd(int64_t run_end) {
constexpr auto max = std::numeric_limits<RunEndCType>::max();
if (ARROW_PREDICT_FALSE(run_end > max)) {
return Status::Invalid("Run end value must fit on run ends type but ", run_end, " > ",
max, ".");
}
return internal::checked_cast<typename CTypeTraits<RunEndCType>::BuilderType*>(
children_[0].get())
->Append(static_cast<RunEndCType>(run_end));
}
Status RunEndEncodedBuilder::AppendRunEnd(int64_t run_end) {
switch (type_->run_end_type()->id()) {
case Type::INT16:
RETURN_NOT_OK(DoAppendRunEnd<int16_t>(run_end));
break;
case Type::INT32:
RETURN_NOT_OK(DoAppendRunEnd<int32_t>(run_end));
break;
case Type::INT64:
RETURN_NOT_OK(DoAppendRunEnd<int64_t>(run_end));
break;
default:
return Status::Invalid("Invalid type for run ends array: ", type_->run_end_type());
}
return Status::OK();
}
Status RunEndEncodedBuilder::CloseRun(int64_t run_length) {
// TODO(felipecrv): gracefully fragment runs bigger than INT32_MAX
if (ARROW_PREDICT_FALSE(run_length > std::numeric_limits<int32_t>::max())) {
return Status::Invalid(
"Run-length of run-encoded arrays must fit in a 32-bit signed integer.");
}
int64_t run_end;
if (internal::AddWithOverflow(committed_logical_length_, run_length, &run_end)) {
return Status::Invalid("Run end value must fit on run ends type.");
}
RETURN_NOT_OK(AppendRunEnd(/*run_end=*/run_end));
UpdateDimensions(run_end, 0);
return Status::OK();
}
ArrayBuilder& RunEndEncodedBuilder::run_end_builder() { return *children_[0]; }
ArrayBuilder& RunEndEncodedBuilder::value_builder() { return *children_[1]; }
} // namespace arrow20
|