sparrow 0.3.0
Loading...
Searching...
No Matches
array_wrapper.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <memory>
18#include <variant>
19
24
25namespace sparrow
26{
27
28 template <class T>
29 concept layout = requires(T& t) {
30 // inner_value_type
31 typename T::inner_value_type;
32
33 t[std::size_t()];
34 t.size();
35 t.begin();
36 t.end();
37 t.cbegin();
38 t.cend();
39 };
40
41 namespace detail
42 {
43 // Helper struct to allow overloading on the type of ARRAY
44 // to get the data_type for an array. This is needed since
45 // some arrays (for instance run_length_encoded_array)
46 // do not have a inner_value_type, therefore we specialize
47 // this in their respecitve headers.
48 template <class ARRAY>
50 {
51 [[nodiscard]] static constexpr sparrow::data_type get()
52 {
54 }
55 };
56
57 template <class ARRAY>
59 {
60 [[nodiscard]] static constexpr bool get()
61 {
62 return false;
63 }
64 };
65 }
66
71 {
72 public:
73
74 using wrapper_ptr = std::unique_ptr<array_wrapper>;
75
76 virtual ~array_wrapper() = default;
77
81
82 [[nodiscard]] wrapper_ptr clone() const;
83
84 [[nodiscard]] enum data_type data_type() const;
85 [[nodiscard]] bool is_dictionary() const;
86
87 [[nodiscard]] arrow_proxy& get_arrow_proxy();
88 [[nodiscard]] const arrow_proxy& get_arrow_proxy() const;
89
90 protected:
91
92 array_wrapper(enum data_type dt);
93 array_wrapper(const array_wrapper&) = default;
94
95 private:
96
97 enum data_type m_data_type;
98 [[nodiscard]] virtual bool is_dictionary_impl() const = 0;
99 [[nodiscard]] virtual arrow_proxy& get_arrow_proxy_impl() = 0;
100 [[nodiscard]] virtual const arrow_proxy& get_arrow_proxy_impl() const = 0;
101 [[nodiscard]] virtual wrapper_ptr clone_impl() const = 0;
102 };
103
104 template <class T>
106 {
107 public:
108
109 array_wrapper_impl(T&& ar);
110 array_wrapper_impl(T* ar);
111 array_wrapper_impl(std::shared_ptr<T> ar);
112
113 ~array_wrapper_impl() override = default;
114
115 T& get_wrapped();
116 const T& get_wrapped() const;
117
118 private:
119
120 using wrapper_ptr = array_wrapper::wrapper_ptr;
121
122 [[nodiscard]] constexpr enum data_type get_data_type() const;
123
125 [[nodiscard]] bool is_dictionary_impl() const override;
126 [[nodiscard]] arrow_proxy& get_arrow_proxy_impl() override;
127 [[nodiscard]] const arrow_proxy& get_arrow_proxy_impl() const override;
128 [[nodiscard]] wrapper_ptr clone_impl() const override;
129
130 using storage_type = std::variant<value_ptr<T>, std::shared_ptr<T>, T*>;
131 storage_type m_storage;
132 T* p_array;
133 };
134
135 template <class T>
137
138 template <class T>
139 const T& unwrap_array(const array_wrapper&);
140
141 /********************************
142 * array_wrapper implementation *
143 ********************************/
144
145 inline auto array_wrapper::clone() const -> wrapper_ptr
146 {
147 return clone_impl();
148 }
149
151 {
152 return m_data_type;
153 }
154
156 {
157 return is_dictionary_impl();
158 }
159
164
166 {
167 return get_arrow_proxy_impl();
168 }
169
171 : m_data_type(dt)
172 {
173 }
174
175 /*************************************
176 * array_wrapper_impl implementation *
177 *************************************/
178
179 template <class T>
181 : array_wrapper(get_data_type())
182 , m_storage(value_ptr<T>(std::move(ar)))
183 , p_array(std::get<value_ptr<T>>(m_storage).get())
184 {
185 }
186
187 template <class T>
189 : array_wrapper(get_data_type())
190 , m_storage(ar)
191 , p_array(ar)
192 {
193 }
194
195 template <class T>
197 : array_wrapper(get_data_type())
198 , m_storage(std::move(ar))
199 , p_array(std::get<std::shared_ptr<T>>(m_storage).get())
200 {
201 }
202
203 template <class T>
205 {
206 return *p_array;
207 }
208
209 template <class T>
211 {
212 return *p_array;
213 }
214
215 template <class T>
216 constexpr enum data_type array_wrapper_impl<T>::get_data_type() const
217 {
219 }
220
221 template <class T>
222 array_wrapper_impl<T>::array_wrapper_impl(const array_wrapper_impl& rhs)
223 : array_wrapper(rhs)
224 , m_storage(value_ptr<T>(T(rhs.get_wrapped()))) // Always deep copy
225 {
226 p_array = std::visit(
227 [](auto&& arg)
228 {
229 using U = std::decay_t<decltype(arg)>;
230 if constexpr (std::is_same_v<U, T*>)
231 {
232 return arg;
233 }
234 else
235 {
236 return arg.get();
237 }
238 },
239 m_storage
240 );
241 }
242
243 template <class T>
248
249 template <class T>
254
255 template <class T>
260
261 template <class T>
262 auto array_wrapper_impl<T>::clone_impl() const -> wrapper_ptr
263 {
264 return wrapper_ptr{new array_wrapper_impl<T>(*this)};
265 }
266
267 template <class T>
269 {
270 return static_cast<array_wrapper_impl<T>&>(ar).get_wrapped();
271 }
272
273 template <class T>
274 const T& unwrap_array(const array_wrapper& ar)
275 {
276 return static_cast<const array_wrapper_impl<T>&>(ar).get_wrapped();
277 }
278}
wrapper_ptr clone_impl() const override
~array_wrapper_impl() override=default
arrow_proxy & get_arrow_proxy_impl() override
bool is_dictionary_impl() const override
Base class for array type erasure.
wrapper_ptr clone() const
std::unique_ptr< array_wrapper > wrapper_ptr
arrow_proxy & get_arrow_proxy()
virtual const arrow_proxy & get_arrow_proxy_impl() const =0
array_wrapper & operator=(array_wrapper &&)=delete
array_wrapper & operator=(const array_wrapper &)=delete
virtual ~array_wrapper()=default
virtual arrow_proxy & get_arrow_proxy_impl()=0
virtual bool is_dictionary_impl() const =0
array_wrapper(array_wrapper &&)=delete
enum data_type data_type() const
array_wrapper(const array_wrapper &)=default
virtual wrapper_ptr clone_impl() const =0
Proxy class over ArrowArray and ArrowSchema.
static const sparrow::arrow_proxy & get_arrow_proxy(const ARRAY &array)
A value_ptr is a smart pointer that behaves like a value.
Definition memory.hpp:37
T & unwrap_array(array_wrapper &)
data_type
Runtime identifier of arrow data types, usually associated with raw bytes with the associated value.
Provides compile-time information about Arrow data types.
static constexpr sparrow::data_type get()