sparrow 0.9.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
23
24namespace sparrow
25{
26
27 template <class T>
28 concept layout = requires(T& t) {
29 // inner_value_type
30 typename T::inner_value_type;
31
32 t[std::size_t()];
33 t.size();
34 t.begin();
35 t.end();
36 t.cbegin();
37 t.cend();
38 };
39
40 namespace detail
41 {
42 // Helper struct to allow overloading on the type of ARRAY
43 // to get the data_type for an array. This is needed since
44 // some arrays (for instance run_length_encoded_array)
45 // do not have a inner_value_type, therefore we specialize
46 // this in their respecitve headers.
47 template <class ARRAY>
49 {
50 [[nodiscard]] static constexpr sparrow::data_type get()
51 {
53 }
54 };
55
56 template <class ARRAY>
58 {
59 [[nodiscard]] static constexpr bool get()
60 {
61 return false;
62 }
63 };
64 }
65
70 {
71 public:
72
73 using wrapper_ptr = std::unique_ptr<array_wrapper>;
74
75 virtual ~array_wrapper() = default;
76
80
81 [[nodiscard]] wrapper_ptr clone() const;
82
83 [[nodiscard]] enum data_type data_type() const;
84 [[nodiscard]] bool is_dictionary() const;
85
86 [[nodiscard]] arrow_proxy& get_arrow_proxy();
87 [[nodiscard]] const arrow_proxy& get_arrow_proxy() const;
88
89 protected:
90
91 array_wrapper(enum data_type dt);
92 array_wrapper(const array_wrapper&) = default;
93
94 private:
95
96 enum data_type m_data_type;
97 [[nodiscard]] virtual bool is_dictionary_impl() const = 0;
98 [[nodiscard]] virtual arrow_proxy& get_arrow_proxy_impl() = 0;
99 [[nodiscard]] virtual const arrow_proxy& get_arrow_proxy_impl() const = 0;
100 [[nodiscard]] virtual wrapper_ptr clone_impl() const = 0;
101 };
102
103 template <class T>
105 {
106 public:
107
108 array_wrapper_impl(T&& ar);
109 array_wrapper_impl(T* ar);
110 array_wrapper_impl(std::shared_ptr<T> ar);
111
112 ~array_wrapper_impl() override = default;
113
114 T& get_wrapped();
115 const T& get_wrapped() const;
116
117 private:
118
119 using wrapper_ptr = array_wrapper::wrapper_ptr;
120
121 [[nodiscard]] constexpr enum data_type get_data_type() const;
122
124 [[nodiscard]] bool is_dictionary_impl() const override;
125 [[nodiscard]] arrow_proxy& get_arrow_proxy_impl() override;
126 [[nodiscard]] const arrow_proxy& get_arrow_proxy_impl() const override;
127 [[nodiscard]] wrapper_ptr clone_impl() const override;
128
129 using storage_type = std::variant<value_ptr<T>, std::shared_ptr<T>, T*>;
130 storage_type m_storage;
131 T* p_array;
132 };
133
134 template <class T>
136
137 template <class T>
138 const T& unwrap_array(const array_wrapper&);
139
140 /********************************
141 * array_wrapper implementation *
142 ********************************/
143
144 inline auto array_wrapper::clone() const -> wrapper_ptr
145 {
146 return clone_impl();
147 }
148
150 {
151 return m_data_type;
152 }
153
155 {
156 return is_dictionary_impl();
157 }
158
163
165 {
166 return get_arrow_proxy_impl();
167 }
168
170 : m_data_type(dt)
171 {
172 }
173
174 /*************************************
175 * array_wrapper_impl implementation *
176 *************************************/
177
178 template <class T>
180 : array_wrapper(get_data_type())
181 , m_storage(value_ptr<T>(std::move(ar)))
182 , p_array(std::get<value_ptr<T>>(m_storage).get())
183 {
184 }
185
186 template <class T>
188 : array_wrapper(get_data_type())
189 , m_storage(ar)
190 , p_array(ar)
191 {
192 }
193
194 template <class T>
196 : array_wrapper(get_data_type())
197 , m_storage(std::move(ar))
198 , p_array(std::get<std::shared_ptr<T>>(m_storage).get())
199 {
200 }
201
202 template <class T>
204 {
205 return *p_array;
206 }
207
208 template <class T>
210 {
211 return *p_array;
212 }
213
214 template <class T>
215 constexpr enum data_type array_wrapper_impl<T>::get_data_type() const
216 {
218 }
219
220 template <class T>
221 array_wrapper_impl<T>::array_wrapper_impl(const array_wrapper_impl& rhs)
222 : array_wrapper(rhs)
223 , m_storage(value_ptr<T>(T(rhs.get_wrapped())))
224 , p_array(
225 std::visit(
226 [](auto&& arg)
227 {
228 using U = std::decay_t<decltype(arg)>;
229 if constexpr (std::is_same_v<U, T*>)
230 {
231 return arg;
232 }
233 else
234 {
235 return arg.get();
236 }
237 },
238 m_storage
239 )
240 ) // Always deep copy
241 {
242 }
243
244 template <class T>
249
250 template <class T>
255
256 template <class T>
261
262 template <class T>
263 auto array_wrapper_impl<T>::clone_impl() const -> wrapper_ptr
264 {
265 return wrapper_ptr{new array_wrapper_impl<T>(*this)};
266 }
267
268 template <class T>
270 {
271 return static_cast<array_wrapper_impl<T>&>(ar).get_wrapped();
272 }
273
274 template <class T>
275 const T& unwrap_array(const array_wrapper& ar)
276 {
277 return static_cast<const array_wrapper_impl<T>&>(ar).get_wrapped();
278 }
279}
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:36
T & unwrap_array(array_wrapper &)
visit_result_t< F > visit(F &&func, const array_wrapper &ar)
Definition dispatch.hpp:47
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()