forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASoA.h
More file actions
4010 lines (3490 loc) · 197 KB
/
ASoA.h
File metadata and controls
4010 lines (3490 loc) · 197 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
#ifndef O2_FRAMEWORK_ASOA_H_
#define O2_FRAMEWORK_ASOA_H_
#include "Framework/Pack.h"
#include "Framework/FunctionalHelpers.h"
#include "Headers/DataHeader.h"
#include "Headers/DataHeaderHelpers.h"
#include "Framework/CompilerBuiltins.h"
#include "Framework/Traits.h"
#include "Framework/Expressions.h"
#include "Framework/ArrowTypes.h"
#include "Framework/ArrowTableSlicingCache.h"
#include "Framework/SliceCache.h"
#include "Framework/VariantHelpers.h"
#include <arrow/table.h>
#include <arrow/array.h>
#include <arrow/util/config.h>
#include <gandiva/selection_vector.h>
#include <array>
#include <cassert>
#include <fmt/format.h>
#include <concepts>
#include <cstring>
#include <gsl/span>
#include <limits>
namespace o2::framework
{
using ListVector = std::vector<std::vector<int64_t>>;
std::string cutString(std::string&& str);
std::string strToUpper(std::string&& str);
} // namespace o2::framework
namespace o2::soa
{
void accessingInvalidIndexFor(const char* getter);
void dereferenceWithWrongType(const char* getter, const char* target);
void missingFilterDeclaration(int hash, int ai);
void notBoundTable(const char* tableName);
} // namespace o2::soa
namespace o2::soa
{
/// Generic identifier for a table type
struct TableRef {
consteval TableRef()
: label_hash{0},
desc_hash{0},
origin_hash{0},
version{0}
{
}
consteval TableRef(uint32_t _label, uint32_t _desc, uint32_t _origin, uint32_t _version)
: label_hash{_label},
desc_hash{_desc},
origin_hash{_origin},
version{_version}
{
}
uint32_t label_hash;
uint32_t desc_hash;
uint32_t origin_hash;
uint32_t version;
constexpr bool operator==(TableRef const& other) const noexcept
{
return (this->label_hash == other.label_hash) &&
(this->desc_hash == other.desc_hash) &&
(this->origin_hash == other.origin_hash) &&
(this->version == other.version);
}
constexpr bool descriptionCompatible(TableRef const& other) const noexcept
{
return this->desc_hash == other.desc_hash;
}
constexpr bool descriptionCompatible(uint32_t _desc_hash) const noexcept
{
return this->desc_hash == _desc_hash;
}
constexpr TableRef(TableRef const&) = default;
constexpr TableRef& operator=(TableRef const&) = default;
constexpr TableRef(TableRef&&) = default;
constexpr TableRef& operator=(TableRef&&) = default;
};
/// Helpers to manipulate TableRef arrays
template <size_t N1, size_t N2, std::array<TableRef, N1> ar1, std::array<TableRef, N2> ar2>
consteval auto merge()
{
constexpr const int duplicates = std::ranges::count_if(ar2.begin(), ar2.end(), [&](TableRef const& a) { return std::any_of(ar1.begin(), ar1.end(), [&](TableRef const& e) { return e == a; }); });
std::array<TableRef, N1 + N2 - duplicates> out;
auto pos = std::copy(ar1.begin(), ar1.end(), out.begin());
std::copy_if(ar2.begin(), ar2.end(), pos, [&](TableRef const& a) { return std::none_of(ar1.begin(), ar1.end(), [&](TableRef const& e) { return e == a; }); });
return out;
}
template <size_t N1, size_t N2, std::array<TableRef, N1> ar1, std::array<TableRef, N2> ar2, typename L>
consteval auto merge_if(L l)
{
constexpr const int to_remove = std::ranges::count_if(ar1.begin(), ar1.end(), [&](TableRef const& a) { return !l(a); });
constexpr const int duplicates = std::ranges::count_if(ar2.begin(), ar2.end(), [&](TableRef const& a) { return std::any_of(ar1.begin(), ar1.end(), [&](TableRef const& e) { return e == a; }) || !l(a); });
std::array<TableRef, N1 + N2 - duplicates - to_remove> out;
auto pos = std::copy_if(ar1.begin(), ar1.end(), out.begin(), [&](TableRef const& a) { return l(a); });
std::copy_if(ar2.begin(), ar2.end(), pos, [&](TableRef const& a) { return std::none_of(ar1.begin(), ar1.end(), [&](TableRef const& e) { return e == a; }) && l(a); });
return out;
}
template <size_t N, std::array<TableRef, N> ar, typename L>
consteval auto remove_if(L l)
{
constexpr const int to_remove = std::ranges::count_if(ar.begin(), ar.end(), [&l](TableRef const& e) { return l(e); });
std::array<TableRef, N - to_remove> out;
std::copy_if(ar.begin(), ar.end(), out.begin(), [&l](TableRef const& e) { return !l(e); });
return out;
}
template <size_t N1, size_t N2, std::array<TableRef, N1> ar1, std::array<TableRef, N2> ar2>
consteval auto intersect()
{
constexpr const int duplicates = std::ranges::count_if(ar2.begin(), ar2.end(), [&](TableRef const& a) { return std::any_of(ar1.begin(), ar1.end(), [&](TableRef const& e) { return e == a; }); });
std::array<TableRef, duplicates> out;
std::copy_if(ar1.begin(), ar1.end(), out.begin(), [](TableRef const& a) { return std::find(ar2.begin(), ar2.end(), a) != ar2.end(); });
return out;
}
template <typename T, typename... Ts>
consteval auto mergeOriginals()
requires(sizeof...(Ts) == 1)
{
using T1 = framework::pack_head_t<framework::pack<Ts...>>;
return merge<T::originals.size(), T1::originals.size(), T::originals, T1::originals>();
}
template <typename T, typename... Ts>
consteval auto mergeOriginals()
requires(sizeof...(Ts) > 1)
{
constexpr auto tail = mergeOriginals<Ts...>();
return merge<T::originals.size(), tail.size(), T::originals, tail>();
}
template <typename T, typename... Ts>
requires(sizeof...(Ts) == 1)
consteval auto intersectOriginals()
{
using T1 = framework::pack_head_t<framework::pack<Ts...>>;
return intersect<T::originals.size(), T1::originals.size(), T::originals, T1::originals>();
}
template <typename T, typename... Ts>
requires(sizeof...(Ts) > 1)
consteval auto intersectOriginals()
{
constexpr auto tail = intersectOriginals<Ts...>();
return intersect<T::originals.size(), tail.size(), T::originals, tail>();
}
} // namespace o2::soa
namespace o2::soa
{
struct Binding;
template <typename T>
concept not_void = requires { !std::same_as<T, void>; };
/// column identification concepts
template <typename C>
concept is_persistent_column = requires(C c) { c.mColumnIterator; };
template <typename C>
constexpr bool is_persistent_v = is_persistent_column<C>;
template <typename C>
using is_persistent_column_t = std::conditional_t<is_persistent_column<C>, std::true_type, std::false_type>;
template <typename C>
concept is_self_index_column = not_void<typename C::self_index_t> && std::same_as<typename C::self_index_t, std::true_type>;
template <typename C>
concept is_index_column = !is_self_index_column<C> && requires(C c, o2::soa::Binding b) {
{ c.setCurrentRaw(b) } -> std::same_as<bool>;
requires std::same_as<decltype(c.mBinding), o2::soa::Binding>;
};
template <typename C>
using is_external_index_t = typename std::conditional_t<is_index_column<C>, std::true_type, std::false_type>;
template <typename C>
using is_self_index_t = typename std::conditional_t<is_self_index_column<C>, std::true_type, std::false_type>;
} // namespace o2::soa
namespace o2::aod
{
/// Base type for table metadata
template <typename D, typename... Cs>
struct TableMetadata {
using columns = framework::pack<Cs...>;
using persistent_columns_t = framework::selected_pack<soa::is_persistent_column_t, Cs...>;
using external_index_columns_t = framework::selected_pack<soa::is_external_index_t, Cs...>;
using internal_index_columns_t = framework::selected_pack<soa::is_self_index_t, Cs...>;
template <typename Key, typename... PCs>
static consteval std::array<bool, sizeof...(PCs)> getMap(framework::pack<PCs...>)
{
return std::array<bool, sizeof...(PCs)>{[]() {
if constexpr (requires { PCs::index_targets.size(); }) {
return Key::template isIndexTargetOf<PCs::index_targets.size(), PCs::index_targets>();
} else {
return false;
}
}()...};
}
template <typename Key>
static consteval int getIndexPosToKey()
{
return getIndexPosToKey_impl<Key, framework::pack_size(persistent_columns_t{}), getMap<Key>(persistent_columns_t{})>();
}
template <typename Key, size_t N, std::array<bool, N> map>
static consteval int getIndexPosToKey_impl()
{
constexpr const auto pos = std::find(map.begin(), map.end(), true);
if constexpr (pos != map.end()) {
return std::distance(map.begin(), pos);
} else {
return -1;
}
}
};
template <typename D>
struct MetadataTrait {
using metadata = void;
};
/// Special struc to map the string hash back to the string and wrap a string literal into the
/// type signature
template <uint32_t H>
struct Hash {
static constexpr uint32_t hash = H;
static constexpr char const* const str{""};
};
/// Filter TableRef array for compatibility with Key table
template <size_t N, std::array<soa::TableRef, N> ar, typename Key>
consteval auto filterForKey()
{
constexpr std::array<bool, N> test = []<size_t... Is>(std::index_sequence<Is...>) {
return std::array<bool, N>{(Key::template hasOriginal<ar[Is]>() || (o2::aod::MetadataTrait<o2::aod::Hash<ar[Is].desc_hash>>::metadata::template getIndexPosToKey<Key>() >= 0))...};
}(std::make_index_sequence<N>());
constexpr int correct = std::ranges::count(test.begin(), test.end(), true);
std::array<soa::TableRef, correct> out;
std::ranges::copy_if(ar.begin(), ar.end(), out.begin(), [&test](soa::TableRef const& r) { return test[std::distance(ar.begin(), std::find(ar.begin(), ar.end(), r))]; });
return out;
}
/// Pre-declare Hash specialization for a generic string
#define O2HASH(_Str_) \
template <> \
struct Hash<_Str_ ""_h> { \
static constexpr uint32_t hash = _Str_ ""_h; \
static constexpr char const* const str{_Str_}; \
};
/// Pre-declare Hash specialization for an origin string
#define O2ORIGIN(_Str_) \
template <> \
struct Hash<_Str_ ""_h> { \
static constexpr header::DataOrigin origin{_Str_}; \
static constexpr uint32_t hash = _Str_ ""_h; \
static constexpr char const* const str{_Str_}; \
};
/// Compile-time function to extract version from table signature string "DESC/#"
static inline constexpr uint32_t version(const char* const str)
{
if (str[0] == '\0') {
return 0;
}
size_t len = 0;
uint32_t res = 0;
while (str[len] != '/' && str[len] != '\0') {
++len;
}
if (str[len - 1] == '\0') {
return -1;
}
for (auto i = len + 1; str[i] != '\0'; ++i) {
res = res * 10 + (int)(str[i] - '0');
}
return res;
}
/// Compile-time functions to extract description from table signature string "DESC/#"
static inline constexpr std::string_view description_str(const char* const str)
{
size_t len = 0;
while (len < 15 && str[len] != '/') {
++len;
}
return std::string_view{str, len};
}
static inline constexpr header::DataDescription description(const char* const str)
{
size_t len = 0;
while (len < 15 && str[len] != '/') {
++len;
}
char out[16];
for (auto i = 0; i < 16; ++i) {
out[i] = 0;
}
std::memcpy(out, str, len);
return {out};
}
// Helpers to get strings from TableRef
template <soa::TableRef R>
consteval const char* label()
{
return o2::aod::Hash<R.label_hash>::str;
}
template <soa::TableRef R>
consteval const char* origin_str()
{
return o2::aod::Hash<R.origin_hash>::str;
}
template <soa::TableRef R>
consteval header::DataOrigin origin()
{
return o2::aod::Hash<R.origin_hash>::origin;
}
template <soa::TableRef R>
consteval const char* signature()
{
return o2::aod::Hash<R.desc_hash>::str;
}
/// hash identification concepts
template <typename T>
concept is_aod_hash = requires(T t) { t.hash; t.str; };
template <typename T>
concept is_origin_hash = is_aod_hash<T> && requires(T t) { t.origin; };
/// convert TableRef to a DPL source specification
template <soa::TableRef R>
static constexpr auto sourceSpec()
{
return fmt::format("{}/{}/{}/{}", label<R>(), origin_str<R>(), description_str(signature<R>()), R.version);
}
} // namespace o2::aod
namespace o2::soa
{
template <aod::is_aod_hash L, aod::is_aod_hash D, aod::is_origin_hash O, typename... Ts>
class Table;
/// Type-checking index column binding
struct Binding {
void const* ptr = nullptr;
size_t hash = 0;
std::span<TableRef const> refs;
template <typename T>
void bind(T const* table)
{
ptr = table;
hash = o2::framework::TypeIdHelpers::uniqueId<T>();
refs = std::span{T::originals};
}
template <typename T>
T const* get() const
{
if (hash == o2::framework::TypeIdHelpers::uniqueId<T>()) {
return static_cast<T const*>(ptr);
}
return nullptr;
}
};
template <typename... C>
auto createFieldsFromColumns(framework::pack<C...>)
{
return std::vector<std::shared_ptr<arrow::Field>>{C::asArrowField()...};
}
using SelectionVector = std::vector<int64_t>;
template <typename T>
concept has_parent_t = not_void<typename T::parent_t>;
template <typename T>
concept is_metadata = framework::base_of_template<aod::TableMetadata, T>;
template <typename T>
concept is_metadata_trait = framework::specialization_of_template<aod::MetadataTrait, T>;
template <typename T>
concept has_metadata = is_metadata_trait<T> && not_void<typename T::metadata>;
template <typename T>
concept has_extension = is_metadata<T> && not_void<typename T::extension_table_t>;
template <typename T>
concept has_configurable_extension = has_extension<T> && requires(T t) { typename T::configurable_t; requires std::same_as<std::true_type, typename T::configurable_t>; };
template <typename T>
concept is_spawnable_column = std::same_as<typename T::spawnable_t, std::true_type>;
template <typename B, typename E>
struct EquivalentIndex {
constexpr static bool value = false;
};
template <aod::is_aod_hash A, aod::is_aod_hash B>
struct EquivalentIndexNG {
constexpr static bool value = false;
};
template <typename B, typename E>
constexpr bool is_index_equivalent_v = EquivalentIndex<B, E>::value || EquivalentIndex<E, B>::value;
template <aod::is_aod_hash A, aod::is_aod_hash B>
constexpr bool is_ng_index_equivalent_v = EquivalentIndexNG<A, B>::value || EquivalentIndexNG<B, A>::value;
/// Policy class for columns which are chunked. This
/// will make the compiler take the most generic (and
/// slow approach).
struct Chunked {
constexpr static bool chunked = true;
};
/// Policy class for columns which are known to be fully
/// inside a chunk. This will generate optimal code.
struct Flat {
constexpr static bool chunked = false;
};
/// unwrapper
template <typename T>
struct unwrap {
using type = T;
};
template <typename T>
struct unwrap<std::vector<T>> {
using type = T;
};
template <>
struct unwrap<bool> {
using type = char;
};
template <typename T>
using unwrap_t = typename unwrap<T>::type;
/// Iterator on a single column.
/// FIXME: the ChunkingPolicy for now is fixed to Flat and is a mere boolean
/// which is used to switch off slow "chunking aware" parts. This is ok for
/// now, but most likely we should move the whole chunk navigation logic there.
template <typename T, typename ChunkingPolicy = Chunked>
class ColumnIterator : ChunkingPolicy
{
static constexpr char SCALE_FACTOR = std::same_as<std::decay_t<T>, bool> ? 3 : 0;
public:
/// Constructor of the column iterator. Notice how it takes a pointer
/// to the ChunkedArray (for the data store) and to the index inside
/// it. This means that a ColumnIterator is actually only available
/// as part of a RowView.
ColumnIterator(arrow::ChunkedArray const* column)
: mColumn{column},
mCurrent{nullptr},
mCurrentPos{nullptr},
mLast{nullptr},
mFirstIndex{0},
mCurrentChunk{0},
mOffset{0}
{
auto array = getCurrentArray();
mCurrent = reinterpret_cast<unwrap_t<T> const*>(array->values()->data()) + (mOffset >> SCALE_FACTOR);
mLast = mCurrent + array->length();
}
ColumnIterator() = default;
ColumnIterator(ColumnIterator<T, ChunkingPolicy> const&) = default;
ColumnIterator<T, ChunkingPolicy>& operator=(ColumnIterator<T, ChunkingPolicy> const&) = default;
ColumnIterator(ColumnIterator<T, ChunkingPolicy>&&) = default;
ColumnIterator<T, ChunkingPolicy>& operator=(ColumnIterator<T, ChunkingPolicy>&&) = default;
/// Move the iterator to the next chunk.
void nextChunk() const
{
auto previousArray = getCurrentArray();
mFirstIndex += previousArray->length();
mCurrentChunk++;
auto array = getCurrentArray();
mCurrent = reinterpret_cast<unwrap_t<T> const*>(array->values()->data()) + (mOffset >> SCALE_FACTOR) - (mFirstIndex >> SCALE_FACTOR);
mLast = mCurrent + array->length() + (mFirstIndex >> SCALE_FACTOR);
}
void prevChunk() const
{
auto previousArray = getCurrentArray();
mFirstIndex -= previousArray->length();
mCurrentChunk--;
auto array = getCurrentArray();
mCurrent = reinterpret_cast<unwrap_t<T> const*>(array->values()->data()) + (mOffset >> SCALE_FACTOR) - (mFirstIndex >> SCALE_FACTOR);
mLast = mCurrent + array->length() + (mFirstIndex >> SCALE_FACTOR);
}
void moveToChunk(int chunk)
{
if (mCurrentChunk < chunk) {
while (mCurrentChunk != chunk) {
nextChunk();
}
} else {
while (mCurrentChunk != chunk) {
prevChunk();
}
}
}
/// Move the iterator to the end of the column.
void moveToEnd()
{
mCurrentChunk = mColumn->num_chunks() - 1;
auto array = getCurrentArray();
mFirstIndex = mColumn->length() - array->length();
mCurrent = reinterpret_cast<unwrap_t<T> const*>(array->values()->data()) + (mOffset >> SCALE_FACTOR) - (mFirstIndex >> SCALE_FACTOR);
mLast = mCurrent + array->length() + (mFirstIndex >> SCALE_FACTOR);
}
decltype(auto) operator*() const
requires std::same_as<bool, std::decay_t<T>>
{
checkSkipChunk();
return (*(mCurrent - (mOffset >> SCALE_FACTOR) + ((*mCurrentPos + mOffset) >> SCALE_FACTOR)) & (1 << ((*mCurrentPos + mOffset) & 0x7))) != 0;
}
decltype(auto) operator*() const
requires((!std::same_as<bool, std::decay_t<T>>) && std::same_as<arrow_array_for_t<T>, arrow::ListArray>)
{
checkSkipChunk();
auto list = std::static_pointer_cast<arrow::ListArray>(mColumn->chunk(mCurrentChunk));
auto offset = list->value_offset(*mCurrentPos - mFirstIndex);
auto length = list->value_length(*mCurrentPos - mFirstIndex);
return gsl::span{mCurrent + mFirstIndex + offset, mCurrent + mFirstIndex + (offset + length)};
}
decltype(auto) operator*() const
requires((!std::same_as<bool, std::decay_t<T>>) && !std::same_as<arrow_array_for_t<T>, arrow::ListArray>)
{
checkSkipChunk();
return *(mCurrent + (*mCurrentPos >> SCALE_FACTOR));
}
// Move to the chunk which containts element pos
ColumnIterator<T>& moveToPos()
{
checkSkipChunk();
return *this;
}
mutable unwrap_t<T> const* mCurrent;
int64_t const* mCurrentPos;
mutable unwrap_t<T> const* mLast;
arrow::ChunkedArray const* mColumn;
mutable int mFirstIndex;
mutable int mCurrentChunk;
mutable int mOffset;
private:
void checkSkipChunk() const
requires((ChunkingPolicy::chunked == true) && std::same_as<arrow_array_for_t<T>, arrow::ListArray>)
{
auto list = std::static_pointer_cast<arrow::ListArray>(mColumn->chunk(mCurrentChunk));
if (O2_BUILTIN_UNLIKELY(*mCurrentPos - mFirstIndex >= list->length())) {
nextChunk();
}
}
void checkSkipChunk() const
requires((ChunkingPolicy::chunked == true) && !std::same_as<arrow_array_for_t<T>, arrow::ListArray>)
{
if (O2_BUILTIN_UNLIKELY(((mCurrent + (*mCurrentPos >> SCALE_FACTOR)) >= mLast))) {
nextChunk();
}
}
void checkSkipChunk() const
requires(ChunkingPolicy::chunked == false)
{
}
/// get pointer to mCurrentChunk chunk
auto getCurrentArray() const
requires(std::same_as<arrow_array_for_t<T>, arrow::FixedSizeListArray>)
{
std::shared_ptr<arrow::Array> chunkToUse = mColumn->chunk(mCurrentChunk);
mOffset = chunkToUse->offset();
chunkToUse = std::dynamic_pointer_cast<arrow::FixedSizeListArray>(chunkToUse)->values();
return std::static_pointer_cast<arrow_array_for_t<value_for_t<T>>>(chunkToUse);
}
auto getCurrentArray() const
requires(std::same_as<arrow_array_for_t<T>, arrow::ListArray>)
{
std::shared_ptr<arrow::Array> chunkToUse = mColumn->chunk(mCurrentChunk);
mOffset = chunkToUse->offset();
chunkToUse = std::dynamic_pointer_cast<arrow::ListArray>(chunkToUse)->values();
mOffset = chunkToUse->offset();
return std::static_pointer_cast<arrow_array_for_t<value_for_t<T>>>(chunkToUse);
}
auto getCurrentArray() const
requires(!std::same_as<arrow_array_for_t<T>, arrow::FixedSizeListArray> && !std::same_as<arrow_array_for_t<T>, arrow::ListArray>)
{
std::shared_ptr<arrow::Array> chunkToUse = mColumn->chunk(mCurrentChunk);
mOffset = chunkToUse->offset();
return std::static_pointer_cast<arrow_array_for_t<T>>(chunkToUse);
}
};
template <typename T, typename INHERIT>
struct Column {
using inherited_t = INHERIT;
Column(ColumnIterator<T> const& it)
: mColumnIterator{it}
{
}
Column() = default;
Column(Column const&) = default;
Column& operator=(Column const&) = default;
Column(Column&&) = default;
Column& operator=(Column&&) = default;
using type = T;
static constexpr const char* const& columnLabel() { return INHERIT::mLabel; }
ColumnIterator<T> const& getIterator() const
{
return mColumnIterator;
}
static auto asArrowField()
{
return std::make_shared<arrow::Field>(inherited_t::mLabel, framework::expressions::concreteArrowType(framework::expressions::selectArrowType<type>()));
}
/// FIXME: rather than keeping this public we should have a protected
/// non-const getter and mark this private.
ColumnIterator<T> mColumnIterator;
};
/// The purpose of this class is to store the lambda which is associated to the
/// method call.
template <typename F, typename INHERIT>
struct DynamicColumn {
using inherited_t = INHERIT;
static constexpr const char* const& columnLabel() { return INHERIT::mLabel; }
};
template <typename INHERIT>
struct IndexColumn {
using inherited_t = INHERIT;
static constexpr const char* const& columnLabel() { return INHERIT::mLabel; }
};
template <typename INHERIT>
struct MarkerColumn {
using inherited_t = INHERIT;
static constexpr const char* const& columnLabel() { return INHERIT::mLabel; }
};
template <size_t M = 0>
struct Marker : o2::soa::MarkerColumn<Marker<M>> {
using type = size_t;
using base = o2::soa::MarkerColumn<Marker<M>>;
constexpr inline static auto value = M;
Marker() = default;
Marker(Marker const&) = default;
Marker(Marker&&) = default;
Marker& operator=(Marker const&) = default;
Marker& operator=(Marker&&) = default;
Marker(arrow::ChunkedArray const*) {}
constexpr inline auto mark()
{
return value;
}
static constexpr const char* mLabel = "Marker";
};
template <int64_t START = 0, int64_t END = -1>
struct Index : o2::soa::IndexColumn<Index<START, END>> {
using base = o2::soa::IndexColumn<Index<START, END>>;
constexpr inline static int64_t start = START;
constexpr inline static int64_t end = END;
Index() = default;
Index(Index const&) = default;
Index(Index&&) = default;
Index& operator=(Index const&) = default;
Index& operator=(Index&&) = default;
Index(arrow::ChunkedArray const*)
{
}
constexpr inline int64_t rangeStart()
{
return START;
}
constexpr inline int64_t rangeEnd()
{
return END;
}
[[nodiscard]] int64_t index() const
{
return index<0>();
}
[[nodiscard]] int64_t filteredIndex() const
{
return index<1>();
}
[[nodiscard]] int64_t globalIndex() const
{
return index<0>() + offsets<0>();
}
template <int N = 0>
[[nodiscard]] int64_t index() const
{
return *std::get<N>(rowIndices);
}
template <int N = 0>
[[nodiscard]] int64_t offsets() const
{
return *std::get<N>(rowOffsets);
}
void setIndices(std::tuple<int64_t const*, int64_t const*> indices)
{
rowIndices = indices;
}
void setOffsets(std::tuple<uint64_t const*> offsets)
{
rowOffsets = offsets;
}
static constexpr const char* mLabel = "Index";
using type = int64_t;
std::tuple<int64_t const*, int64_t const*> rowIndices;
/// The offsets within larger tables. Currently only
/// one level of nesting is supported.
std::tuple<uint64_t const*> rowOffsets;
};
template <typename C>
concept is_indexing_column = requires(C& c) {
c.rowIndices;
c.rowOffsets;
};
template <typename C>
concept is_dynamic_column = requires(C& c) {
c.boundIterators;
};
template <typename C>
concept is_marker_column = requires { &C::mark; };
template <typename T>
using is_dynamic_t = std::conditional_t<is_dynamic_column<T>, std::true_type, std::false_type>;
template <typename T>
concept is_column = is_persistent_column<T> || is_dynamic_column<T> || is_indexing_column<T> || is_marker_column<T>;
template <typename T>
using is_indexing_t = std::conditional_t<is_indexing_column<T>, std::true_type, std::false_type>;
struct IndexPolicyBase {
/// Position inside the current table
int64_t mRowIndex = 0;
/// Offset within a larger table
uint64_t mOffset = 0;
};
struct RowViewSentinel {
int64_t const index;
};
struct FilteredIndexPolicy : IndexPolicyBase {
// We use -1 in the IndexPolicyBase to indicate that the index is
// invalid. What will validate the index is the this->setCursor()
// which happens below which will properly setup the first index
// by remapping the filtered index 0 to whatever unfiltered index
// it belongs to.
FilteredIndexPolicy(gsl::span<int64_t const> selection, int64_t rows, uint64_t offset = 0)
: IndexPolicyBase{-1, offset},
mSelectedRows(selection),
mMaxSelection(selection.size()),
nRows{rows}
{
this->setCursor(0);
}
void resetSelection(gsl::span<int64_t const> selection)
{
mSelectedRows = selection;
mMaxSelection = selection.size();
this->setCursor(0);
}
FilteredIndexPolicy() = default;
FilteredIndexPolicy(FilteredIndexPolicy&&) = default;
FilteredIndexPolicy(FilteredIndexPolicy const&) = default;
FilteredIndexPolicy& operator=(FilteredIndexPolicy const&) = default;
FilteredIndexPolicy& operator=(FilteredIndexPolicy&&) = default;
[[nodiscard]] std::tuple<int64_t const*, int64_t const*>
getIndices() const
{
return std::make_tuple(&mRowIndex, &mSelectionRow);
}
[[nodiscard]] std::tuple<uint64_t const*>
getOffsets() const
{
return std::make_tuple(&mOffset);
}
void limitRange(int64_t start, int64_t end)
{
this->setCursor(start);
if (end >= 0) {
mMaxSelection = std::min(end, mMaxSelection);
}
}
void setCursor(int64_t i)
{
mSelectionRow = i;
updateRow();
}
void moveByIndex(int64_t i)
{
mSelectionRow += i;
updateRow();
}
friend bool operator==(FilteredIndexPolicy const& lh, FilteredIndexPolicy const& rh)
{
return lh.mSelectionRow == rh.mSelectionRow;
}
bool operator==(RowViewSentinel const& sentinel) const
{
return O2_BUILTIN_UNLIKELY(mSelectionRow == sentinel.index);
}
/// Move iterator to one after the end. Since this is a view
/// we move the mSelectionRow to one past the view size and
/// the mRowIndex to one past the last entry in the selection
void moveToEnd()
{
this->mSelectionRow = this->mMaxSelection;
this->mRowIndex = -1;
}
[[nodiscard]] auto getSelectionRow() const
{
return mSelectionRow;
}
[[nodiscard]] auto size() const
{
return mMaxSelection;
}
[[nodiscard]] auto raw_size() const
{
return nRows;
}
private:
inline void updateRow()
{
this->mRowIndex = O2_BUILTIN_LIKELY(mSelectionRow < mMaxSelection) ? mSelectedRows[mSelectionRow] : -1;
}
gsl::span<int64_t const> mSelectedRows;
int64_t mSelectionRow = 0;
int64_t mMaxSelection = 0;
int64_t nRows = 0;
};
struct DefaultIndexPolicy : IndexPolicyBase {
/// Needed to be able to copy the policy
DefaultIndexPolicy() = default;
DefaultIndexPolicy(DefaultIndexPolicy&&) = default;
DefaultIndexPolicy(DefaultIndexPolicy const&) = default;
DefaultIndexPolicy& operator=(DefaultIndexPolicy const&) = default;
DefaultIndexPolicy& operator=(DefaultIndexPolicy&&) = default;
/// mMaxRow is one behind the last row, so effectively equal to the number of
/// rows @a nRows. Offset indicates that the index is actually part of
/// a larger
DefaultIndexPolicy(int64_t nRows, uint64_t offset)
: IndexPolicyBase{0, offset},
mMaxRow(nRows)
{
}
DefaultIndexPolicy(FilteredIndexPolicy const& other)
: IndexPolicyBase{0, other.mOffset},
mMaxRow(other.raw_size())
{
}
void limitRange(int64_t start, int64_t end)
{
this->setCursor(start);
if (end >= 0) {
mMaxRow = std::min(end, mMaxRow);
}
}
[[nodiscard]] std::tuple<int64_t const*, int64_t const*>
getIndices() const
{
return std::make_tuple(&mRowIndex, &mRowIndex);
}
[[nodiscard]] std::tuple<uint64_t const*>
getOffsets() const
{
return std::make_tuple(&mOffset);
}
void setCursor(int64_t i)
{
this->mRowIndex = i;
}
void moveByIndex(int64_t i)
{
this->mRowIndex += i;
}
void moveToEnd()
{
this->setCursor(mMaxRow);
}
friend bool operator==(DefaultIndexPolicy const& lh, DefaultIndexPolicy const& rh)
{