forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrowTableSlicingCache.cxx
More file actions
300 lines (273 loc) · 10.2 KB
/
ArrowTableSlicingCache.cxx
File metadata and controls
300 lines (273 loc) · 10.2 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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// 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.
#include "Framework/ArrowTableSlicingCache.h"
#include "Framework/RuntimeError.h"
#include <arrow/compute/api_aggregate.h>
#include <arrow/compute/kernel.h>
#include <arrow/table.h>
namespace o2::framework
{
void updatePairList(Cache& list, std::string const& binding, std::string const& key, bool enabled = true)
{
auto locate = std::find_if(list.begin(), list.end(), [&binding, &key](auto const& entry) { return (entry.binding == binding) && (entry.key == key); });
if (locate == list.end()) {
list.emplace_back(binding, key, enabled);
} else if (!locate->enabled && enabled) {
locate->enabled = true;
}
}
std::pair<int64_t, int64_t> SliceInfoPtr::getSliceFor(int value) const
{
int64_t offset = 0;
if (values.empty()) {
return {offset, 0};
}
return {(*offsets)[value], (*sizes)[value]};
}
gsl::span<const int64_t> SliceInfoUnsortedPtr::getSliceFor(int value) const
{
if (values.empty()) {
return {};
}
if (value > values[values.size() - 1]) {
return {};
}
return {(*groups)[value].data(), (*groups)[value].size()};
}
void ArrowTableSlicingCacheDef::setCaches(Cache&& bsks)
{
bindingsKeys = bsks;
}
void ArrowTableSlicingCacheDef::setCachesUnsorted(Cache&& bsks)
{
bindingsKeysUnsorted = bsks;
}
ArrowTableSlicingCache::ArrowTableSlicingCache(Cache&& bsks, Cache&& bsksUnsorted)
: bindingsKeys{bsks},
bindingsKeysUnsorted{bsksUnsorted}
{
values.resize(bindingsKeys.size());
counts.resize(bindingsKeys.size());
offsets.resize(bindingsKeys.size());
sizes.resize(bindingsKeys.size());
valuesUnsorted.resize(bindingsKeysUnsorted.size());
groups.resize(bindingsKeysUnsorted.size());
}
void ArrowTableSlicingCache::setCaches(Cache&& bsks, Cache&& bsksUnsorted)
{
bindingsKeys = bsks;
bindingsKeysUnsorted = bsksUnsorted;
values.clear();
values.resize(bindingsKeys.size());
counts.clear();
counts.resize(bindingsKeys.size());
offsets.clear();
offsets.resize(bindingsKeys.size());
sizes.clear();
sizes.resize(bindingsKeys.size());
valuesUnsorted.clear();
valuesUnsorted.resize(bindingsKeysUnsorted.size());
groups.clear();
groups.resize(bindingsKeysUnsorted.size());
}
arrow::Status ArrowTableSlicingCache::updateCacheEntry(int pos, std::shared_ptr<arrow::Table> const& table)
{
if (table->num_rows() == 0) {
values[pos].reset();
counts[pos].reset();
offsets[pos].clear();
sizes[pos].clear();
return arrow::Status::OK();
}
auto& [b, k, e] = bindingsKeys[pos];
if (!e) {
throw runtime_error_f("Disabled cache %s/%s update requested", b.c_str(), k.c_str());
}
validateOrder(bindingsKeys[pos], table);
arrow::Datum value_counts;
auto options = arrow::compute::ScalarAggregateOptions::Defaults();
ARROW_ASSIGN_OR_RAISE(value_counts,
arrow::compute::CallFunction("value_counts", {table->GetColumnByName(bindingsKeys[pos].key)},
&options));
auto pair = static_cast<arrow::StructArray>(value_counts.array());
values[pos].reset();
counts[pos].reset();
values[pos] = std::make_shared<arrow::NumericArray<arrow::Int32Type>>(pair.field(0)->data());
counts[pos] = std::make_shared<arrow::NumericArray<arrow::Int64Type>>(pair.field(1)->data());
int maxValue = 0;
for (auto i = values[pos]->length() - 1; i >= 0; --i) {
if (values[pos]->Value(i) < 0) {
continue;
} else {
maxValue = values[pos]->Value(i);
break;
}
}
offsets[pos].resize(maxValue + 1);
sizes[pos].resize(maxValue);
std::fill(offsets[pos].begin(), offsets[pos].end(), 0);
std::fill(sizes[pos].begin(), sizes[pos].end(), 0);
int64_t offset = 0;
for (auto i = 0U; i < values[pos]->length(); ++i) {
auto value = values[pos]->Value(i);
if (value >= 0) {
offsets[pos][value] = offset;
sizes[pos][value] = counts[pos]->Value(i);
}
offset += counts[pos]->Value(i);
}
offsets[pos][maxValue] = offset;
return arrow::Status::OK();
}
arrow::Status ArrowTableSlicingCache::updateCacheEntryUnsorted(int pos, const std::shared_ptr<arrow::Table>& table)
{
valuesUnsorted[pos].clear();
groups[pos].clear();
if (table->num_rows() == 0) {
return arrow::Status::OK();
}
auto& [b, k, e] = bindingsKeysUnsorted[pos];
if (!e) {
throw runtime_error_f("Disabled unsorted cache %s/%s update requested", b.c_str(), k.c_str());
}
auto column = table->GetColumnByName(k);
auto row = 0;
for (auto iChunk = 0; iChunk < column->num_chunks(); ++iChunk) {
auto chunk = static_cast<arrow::NumericArray<arrow::Int32Type>>(column->chunk(iChunk)->data());
for (auto iElement = 0; iElement < chunk.length(); ++iElement) {
auto v = chunk.Value(iElement);
if (v >= 0) {
if (std::find(valuesUnsorted[pos].begin(), valuesUnsorted[pos].end(), v) == valuesUnsorted[pos].end()) {
valuesUnsorted[pos].push_back(v);
}
if ((int)groups[pos].size() <= v) {
groups[pos].resize(v + 1);
}
(groups[pos])[v].push_back(row);
}
++row;
}
}
std::sort(valuesUnsorted[pos].begin(), valuesUnsorted[pos].end());
return arrow::Status::OK();
}
std::pair<int, bool> ArrowTableSlicingCache::getCachePos(const Entry& bindingKey) const
{
auto pos = getCachePosSortedFor(bindingKey);
if (pos != -1) {
return {pos, true};
}
pos = getCachePosUnsortedFor(bindingKey);
if (pos != -1) {
return {pos, false};
}
throw runtime_error_f("%s/%s not found neither in sorted or unsorted cache", bindingKey.binding.c_str(), bindingKey.key.c_str());
}
int ArrowTableSlicingCache::getCachePosSortedFor(Entry const& bindingKey) const
{
auto locate = std::find_if(bindingsKeys.begin(), bindingsKeys.end(), [&](Entry const& bk) { return (bindingKey.binding == bk.binding) && (bindingKey.key == bk.key); });
if (locate != bindingsKeys.end()) {
return std::distance(bindingsKeys.begin(), locate);
}
return -1;
}
int ArrowTableSlicingCache::getCachePosUnsortedFor(Entry const& bindingKey) const
{
auto locate_unsorted = std::find_if(bindingsKeysUnsorted.begin(), bindingsKeysUnsorted.end(), [&](Entry const& bk) { return (bindingKey.binding == bk.binding) && (bindingKey.key == bk.key); });
if (locate_unsorted != bindingsKeysUnsorted.end()) {
return std::distance(bindingsKeysUnsorted.begin(), locate_unsorted);
}
return -1;
}
SliceInfoPtr ArrowTableSlicingCache::getCacheFor(Entry const& bindingKey) const
{
auto [p, s] = getCachePos(bindingKey);
if (!s) {
throw runtime_error_f("%s/%s is found in unsorted cache", bindingKey.binding.c_str(), bindingKey.key.c_str());
}
if (!bindingsKeys[p].enabled) {
throw runtime_error_f("Disabled cache %s/%s is requested", bindingKey.binding.c_str(), bindingKey.key.c_str());
}
return getCacheForPos(p);
}
SliceInfoUnsortedPtr ArrowTableSlicingCache::getCacheUnsortedFor(const Entry& bindingKey) const
{
auto [p, s] = getCachePos(bindingKey);
if (s) {
throw runtime_error_f("%s/%s is found in sorted cache", bindingKey.binding.c_str(), bindingKey.key.c_str());
}
if (!bindingsKeysUnsorted[p].enabled) {
throw runtime_error_f("Disabled unsorted cache %s/%s is requested", bindingKey.binding.c_str(), bindingKey.key.c_str());
}
return getCacheUnsortedForPos(p);
}
SliceInfoPtr ArrowTableSlicingCache::getCacheForPos(int pos) const
{
if (values[pos] == nullptr && counts[pos] == nullptr) {
return {
{}, //
{}, //
nullptr, //
nullptr //
};
}
return {
{reinterpret_cast<int const*>(values[pos]->values()->data()), static_cast<size_t>(values[pos]->length())}, //
{reinterpret_cast<int64_t const*>(counts[pos]->values()->data()), static_cast<size_t>(counts[pos]->length())}, //
&(offsets[pos]), //
&(sizes[pos]) //
};
}
SliceInfoUnsortedPtr ArrowTableSlicingCache::getCacheUnsortedForPos(int pos) const
{
return {
{reinterpret_cast<int const*>(valuesUnsorted[pos].data()), valuesUnsorted[pos].size()},
&(groups[pos]) //
};
}
void ArrowTableSlicingCache::validateOrder(Entry const& bindingKey, const std::shared_ptr<arrow::Table>& input)
{
auto const& [target, key, enabled] = bindingKey;
auto column = input->GetColumnByName(key);
auto array0 = static_cast<arrow::NumericArray<arrow::Int32Type>>(column->chunk(0)->data());
int32_t prev = 0;
int32_t cur = array0.Value(0);
int32_t lastNeg = cur < 0 ? cur : 0;
int32_t lastPos = cur < 0 ? -1 : cur;
for (auto i = 0; i < column->num_chunks(); ++i) {
auto array = static_cast<arrow::NumericArray<arrow::Int32Type>>(column->chunk(i)->data());
for (auto e = 0; e < array.length(); ++e) {
prev = cur;
if (prev >= 0) {
lastPos = prev;
} else {
lastNeg = prev;
}
cur = array.Value(e);
if (cur >= 0) {
if (lastPos > cur) {
throw runtime_error_f("Table %s index %s is not sorted: next value %d < previous value %d!", target.c_str(), key.c_str(), cur, lastPos);
}
if (lastPos == cur && prev < 0) {
throw runtime_error_f("Table %s index %s has a group with index %d that is split by %d", target.c_str(), key.c_str(), cur, prev);
}
} else {
if (lastNeg < cur) {
throw runtime_error_f("Table %s index %s is not sorted: next negative value %d > previous negative value %d!", target.c_str(), key.c_str(), cur, lastNeg);
}
if (lastNeg == cur && prev >= 0) {
throw runtime_error_f("Table %s index %s has a group with index %d that is split by %d", target.c_str(), key.c_str(), cur, prev);
}
}
}
}
}
} // namespace o2::framework