Project
Loading...
Searching...
No Matches
IndexBuilderHelpers.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_FRAMEWORK_INDEXBUILDERHELPERS_H_
13#define O2_FRAMEWORK_INDEXBUILDERHELPERS_H_
14#include "arrow/array.h"
15#include <arrow/chunked_array.h>
16#include <arrow/builder.h>
17#include <arrow/memory_pool.h>
18#include <string>
19#include <memory>
20#include <type_traits>
21
22namespace o2::framework
23{
25
27 ChunkedArrayIterator(std::shared_ptr<arrow::ChunkedArray> source);
28 virtual ~ChunkedArrayIterator() = default;
29
30 std::shared_ptr<arrow::ChunkedArray> mSource;
31 size_t mPosition = 0;
32 int mChunk = 0;
33 size_t mOffset = 0;
34 std::shared_ptr<arrow::Int32Array> mCurrentArray = nullptr;
35 int const* mCurrent = nullptr;
36 int const* mLast = nullptr;
37 size_t mFirstIndex = 0;
38
39 std::shared_ptr<arrow::Int32Array> getCurrentArray();
40 void nextChunk();
41 void prevChunk();
42 int valueAt(size_t pos);
43};
44
46 SelfIndexColumnBuilder(const char* name, arrow::MemoryPool* pool);
47 virtual ~SelfIndexColumnBuilder() = default;
48
49 template <typename C>
50 inline std::shared_ptr<arrow::ChunkedArray> result() const
51 {
52 std::shared_ptr<arrow::Array> array;
53 auto status = static_cast<arrow::Int32Builder*>(mBuilder.get())->Finish(&array);
54 if (!status.ok()) {
56 }
57
58 return std::make_shared<arrow::ChunkedArray>(array);
59 }
60 std::shared_ptr<arrow::Field> field() const;
61 template <typename C>
62 inline bool find(int)
63 {
64 return true;
65 }
66
67 template <typename C>
68 inline void fill(int idx)
69 {
70 (void)static_cast<arrow::Int32Builder*>(mBuilder.get())->Append(idx);
71 }
72
73 std::string mColumnName;
74 std::shared_ptr<arrow::DataType> mArrowType;
75 std::unique_ptr<arrow::ArrayBuilder> mBuilder = nullptr;
76};
77
79{
80 public:
81 IndexColumnBuilder(std::shared_ptr<arrow::ChunkedArray> source, const char* name, int listSize, arrow::MemoryPool* pool);
82 ~IndexColumnBuilder() override = default;
83
84 template <typename C>
85 inline std::shared_ptr<arrow::ChunkedArray> result() const
86 {
87 if constexpr (std::same_as<typename C::type, std::vector<int>>) {
88 return resultMulti();
89 } else if constexpr (std::same_as<typename C::type, int[2]>) {
90 return resultSlice();
91 } else {
92 return resultSingle();
93 }
94 }
95
96 template <typename C>
97 inline bool find(int idx)
98 {
99 if constexpr (std::same_as<typename C::type, std::vector<int>>) {
100 return findMulti(idx);
101 } else if constexpr (std::same_as<typename C::type, int[2]>) {
102 return findSlice(idx);
103 } else {
104 return findSingle(idx);
105 }
106 }
107
108 template <typename C>
109 inline void fill(int idx)
110 {
111 ++mResultSize;
112 if constexpr (std::same_as<typename C::type, std::vector<int>>) {
113 fillMulti(idx);
114 } else if constexpr (std::same_as<typename C::type, int[2]>) {
115 fillSlice(idx);
116 } else {
117 fillSingle(idx);
118 }
119 }
120
121 private:
122 arrow::Status preSlice();
123 arrow::Status preFind();
124
125 bool findSingle(int idx);
126 bool findSlice(int idx);
127 bool findMulti(int idx);
128
129 void fillSingle(int idx);
130 void fillSlice(int idx);
131 void fillMulti(int idx);
132
133 std::shared_ptr<arrow::ChunkedArray> resultSingle() const;
134 std::shared_ptr<arrow::ChunkedArray> resultSlice() const;
135 std::shared_ptr<arrow::ChunkedArray> resultMulti() const;
136
137 int mListSize = 1;
138 arrow::ArrayBuilder* mValueBuilder = nullptr;
139 std::unique_ptr<arrow::ArrayBuilder> mListBuilder = nullptr;
140
141 size_t mSourceSize = 0;
142 size_t mResultSize = 0;
143
144 std::shared_ptr<arrow::NumericArray<arrow::Int32Type>> mValuesArrow = nullptr;
145 std::shared_ptr<arrow::NumericArray<arrow::Int64Type>> mCounts = nullptr;
146 std::vector<int> mValues;
147 std::vector<std::vector<int>> mIndices;
148 int mFillOffset = 0;
149 int mValuePos = 0;
150};
151
152std::shared_ptr<arrow::Table> makeArrowTable(const char* label, std::vector<std::shared_ptr<arrow::ChunkedArray>>&& columns, std::vector<std::shared_ptr<arrow::Field>>&& fields);
153} // namespace o2::framework
154
155#endif // O2_FRAMEWORK_INDEXBUILDERHELPERS_H_
uint16_t pos
Definition RawData.h:3
std::shared_ptr< arrow::ChunkedArray > result() const
~IndexColumnBuilder() override=default
GLenum array
Definition glcorearb.h:4274
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::shared_ptr< arrow::Table > makeArrowTable(const char *label, std::vector< std::shared_ptr< arrow::ChunkedArray > > &&columns, std::vector< std::shared_ptr< arrow::Field > > &&fields)
std::shared_ptr< arrow::Int32Array > getCurrentArray()
std::shared_ptr< arrow::Int32Array > mCurrentArray
virtual ~ChunkedArrayIterator()=default
std::shared_ptr< arrow::ChunkedArray > mSource
std::unique_ptr< arrow::ArrayBuilder > mBuilder
std::shared_ptr< arrow::DataType > mArrowType
std::shared_ptr< arrow::Field > field() const
std::shared_ptr< arrow::ChunkedArray > result() const