-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathKernels.h
More file actions
242 lines (219 loc) · 8.07 KB
/
Kernels.h
File metadata and controls
242 lines (219 loc) · 8.07 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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef O2_FRAMEWORK_KERNELS_H_
#define O2_FRAMEWORK_KERNELS_H_
#include "Framework/BasicOps.h"
#include "Framework/TableBuilder.h"
#if __has_include(<arrow/config.h>)
#include <arrow/config.h>
#endif
#if __has_include(<arrow/util/config.h>)
#include <arrow/util/config.h>
#endif
#include <arrow/compute/kernel.h>
#include <arrow/status.h>
#include <arrow/util/visibility.h>
#include <arrow/util/variant.h>
#include <string>
#if (ARROW_VERSION < 1000000)
namespace arrow
{
using Datum = compute::Datum;
}
#endif
namespace o2::framework
{
#if (ARROW_VERSION < 1000000)
template <typename T>
struct ARROW_EXPORT GroupByOptions {
std::string columnName;
T size;
};
/// Build ranges
template <typename T, typename ARRAY>
class ARROW_EXPORT SortedGroupByKernel : public arrow::compute::UnaryKernel
{
public:
explicit SortedGroupByKernel(GroupByOptions<T> options = {}) : mOptions(options){};
arrow::Status Call(arrow::compute::FunctionContext*,
arrow::compute::Datum const& table,
arrow::compute::Datum* outputRanges) override
{
using namespace arrow;
if (table.kind() == arrow::compute::Datum::TABLE) {
auto atable = util::get<std::shared_ptr<arrow::Table>>(table.value);
auto columnIndex = atable->schema()->GetFieldIndex(mOptions.columnName);
auto chunkedArray = atable->column(columnIndex);
return doGrouping(chunkedArray, outputRanges);
};
return arrow::Status::OK();
};
#pragma GCC diagnostic push
#ifdef __clang__
#pragma GCC diagnostic ignored "-Winconsistent-missing-override"
#endif // __clang__
std::shared_ptr<arrow::DataType> out_type() const final
{
return mType;
}
#pragma GCC diagnostic pop
private:
arrow::Status doGrouping(std::shared_ptr<arrow::ChunkedArray> chunkedArray, arrow::compute::Datum* outputRanges)
{
o2::framework::TableBuilder builder;
auto writer = builder.persist<T, T, T>({"start", "count", "index"});
auto zeroChunk = std::static_pointer_cast<ARRAY>(chunkedArray->chunk(0));
if (zeroChunk->length() == 0) {
*outputRanges = std::move(builder.finalize());
return arrow::Status::OK();
}
T currentIndex = 0;
T currentCount = 0;
T currentOffset = 0;
for (auto ci = 0; ci < chunkedArray->num_chunks(); ++ci) {
auto chunk = chunkedArray->chunk(ci);
T const* data = std::static_pointer_cast<ARRAY>(chunk)->raw_values();
for (auto ai = 0; ai < chunk->length(); ++ai) {
if (currentIndex == data[ai]) {
currentCount++;
} else {
writer(0, currentOffset, currentCount, currentIndex);
currentOffset += currentCount;
while (data[ai] - currentIndex > 1) {
writer(0, currentOffset, 0, ++currentIndex);
}
currentIndex++;
currentCount = 1;
}
}
}
writer(0, currentOffset, currentCount, currentIndex);
while (currentIndex < mOptions.size - 1) {
writer(0, currentOffset, 0, ++currentIndex);
}
*outputRanges = std::move(builder.finalize());
return arrow::Status::OK();
}
std::shared_ptr<arrow::DataType> mType;
GroupByOptions<T> mOptions;
};
/// Slice a given table is a vector of tables each containing a slice.
/// @a outputSlices the arrow tables in which the original @a inputTable
/// is split into.
/// @a offset the offset in the original table at which the corresponding
/// slice was split.
/// Slice a given table is a vector of tables each containing a slice.
template <typename T>
arrow::Status sliceByColumn(std::string const& key,
std::shared_ptr<arrow::Table> const& input,
T size,
std::vector<arrow::compute::Datum>* outputSlices,
std::vector<uint64_t>* offsets = nullptr)
{
arrow::compute::Datum inputTable{input};
// build all the ranges on the fly.
arrow::compute::Datum outRanges;
auto table = arrow::util::get<std::shared_ptr<arrow::Table>>(inputTable.value);
o2::framework::SortedGroupByKernel<T, soa::arrow_array_for_t<T>> kernel{GroupByOptions<T>{key, size}};
ARROW_RETURN_NOT_OK(kernel.Call(nullptr, inputTable, &outRanges));
auto ranges = arrow::util::get<std::shared_ptr<arrow::Table>>(outRanges.value);
outputSlices->reserve(ranges->num_rows());
if (offsets) {
offsets->reserve(ranges->num_rows());
}
auto startChunks = ranges->column(0);
assert(startChunks->num_chunks() == 1);
auto countChunks = ranges->column(1);
assert(countChunks->num_chunks() == 1);
auto startData = std::static_pointer_cast<soa::arrow_array_for_t<T>>(startChunks->chunk(0))->raw_values();
auto countData = std::static_pointer_cast<soa::arrow_array_for_t<T>>(countChunks->chunk(0))->raw_values();
for (auto ri = 0; ri < ranges->num_rows(); ++ri) {
auto start = startData[ri];
auto count = countData[ri];
auto schema = table->schema();
std::vector<std::shared_ptr<arrow::ChunkedArray>> slicedColumns;
slicedColumns.reserve(schema->num_fields());
// if (count != 0) {
for (auto ci = 0; ci < schema->num_fields(); ++ci) {
slicedColumns.emplace_back(table->column(ci)->Slice(start, count));
}
// }
outputSlices->emplace_back(arrow::compute::Datum(arrow::Table::Make(table->schema(), slicedColumns)));
if (offsets) {
offsets->emplace_back(start);
}
}
return arrow::Status::OK();
}
#else
/// Slice a given table in a vector of tables each containing a slice.
/// @a slices the arrow tables in which the original @a input
/// is split into.
/// @a offset the offset in the original table at which the corresponding
/// slice was split.
template <typename T>
auto sliceByColumn(char const* key,
std::shared_ptr<arrow::Table> const& input,
T fullSize,
std::vector<arrow::Datum>* slices,
std::vector<uint64_t>* offsets = nullptr)
{
arrow::Datum value_counts;
auto options = arrow::compute::CountOptions::Defaults();
ARROW_ASSIGN_OR_RAISE(value_counts,
arrow::compute::CallFunction("value_counts", {input->GetColumnByName(key)},
&options));
auto pair = static_cast<arrow::StructArray>(value_counts.array());
auto values = static_cast<arrow::NumericArray<typename detail::ConversionTraits<T>::ArrowType>>(pair.field(0)->data());
auto counts = static_cast<arrow::NumericArray<arrow::Int64Type>>(pair.field(1)->data());
// create slices and offsets
auto offset = 0;
auto count = 0;
auto size = values.length();
std::shared_ptr<arrow::Schema> schema(input->schema());
std::vector<std::shared_ptr<arrow::ChunkedArray>> sliceArray;
auto injectSlice = [&](T count) {
for (auto ci = 0; ci < schema->num_fields(); ++ci) {
sliceArray.emplace_back(input->column(ci)->Slice(offset, count));
}
slices->emplace_back(arrow::Datum(arrow::Table::Make(schema, sliceArray)));
if (offsets) {
offsets->emplace_back(offset);
}
sliceArray.clear();
};
auto current = 0;
auto v = values.Value(0);
while (v - current >= 1) {
injectSlice(0);
++current;
}
for (auto r = 0; r < size - 1; ++r) {
count = counts.Value(r);
injectSlice(count);
offset += count;
auto nextValue = values.Value(r + 1);
auto value = values.Value(r);
while (nextValue - value > 1) {
injectSlice(0);
++value;
}
}
injectSlice(counts.Value(size - 1));
if (values.Value(size - 1) < fullSize - 1) {
for (auto v = values.Value(size - 1) + 1; v < fullSize; ++v) {
injectSlice(0);
}
}
return arrow::Status::OK();
}
#endif
} // namespace o2::framework
#endif // O2_FRAMEWORK_KERNELS_H_