sparrow 0.6.0
Loading...
Searching...
No Matches
primitive_array_impl.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 implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15
16#pragma once
17
26
27namespace sparrow
28{
29 template <trivial_copyable_type T>
31
32 template <trivial_copyable_type T>
52
53 template <trivial_copyable_type T>
54 class primitive_array_impl final : public mutable_array_bitmap_base<primitive_array_impl<T>>,
56 {
57 public:
58
62 using size_type = std::size_t;
63
65 using inner_value_type = typename inner_types::inner_value_type;
66 using inner_reference = typename inner_types::inner_reference;
67 using inner_const_reference = typename inner_types::inner_const_reference;
68
69 using pointer = typename inner_types::pointer;
70 using const_pointer = typename inner_types::const_pointer;
71
72 using value_iterator = typename base_type::value_iterator;
73 using const_value_iterator = typename base_type::const_value_iterator;
74
76
101 template <class... Args>
103 explicit primitive_array_impl(Args&&... args)
104 : base_type(create_proxy(std::forward<Args>(args)...))
105 , access_class_type(this->get_arrow_proxy(), DATA_BUFFER_INDEX)
106 {
107 }
108
112 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
114 std::initializer_list<inner_value_type> init,
115 std::optional<std::string_view> name = std::nullopt,
116 std::optional<METADATA_RANGE> metadata = std::nullopt
117 )
118 : base_type(create_proxy(init, std::move(name), std::move(metadata)))
119 , access_class_type(this->get_arrow_proxy(), DATA_BUFFER_INDEX)
120 {
121 }
122
125
128
129 private:
130
131 template <input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
132 static arrow_proxy create_proxy(
133 size_type n,
134 std::optional<std::string_view> name = std::nullopt,
135 std::optional<METADATA_RANGE> metadata = std::nullopt
136 );
137
138 template <
139 validity_bitmap_input VALIDITY_RANGE = validity_bitmap,
140 input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
141 static auto create_proxy(
142 u8_buffer<T>&& data_buffer,
143 size_t size,
144 VALIDITY_RANGE&& bitmaps = validity_bitmap{},
145 std::optional<std::string_view> name = std::nullopt,
146 std::optional<METADATA_RANGE> metadata = std::nullopt
147 ) -> arrow_proxy;
148
149 // range of values (no missing values)
150 template <std::ranges::input_range R, input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
151 requires std::convertible_to<std::ranges::range_value_t<R>, T>
152 static auto create_proxy(
153 R&& range,
154 std::optional<std::string_view> name = std::nullopt,
155 std::optional<METADATA_RANGE> metadata = std::nullopt
156 ) -> arrow_proxy;
157
158 template <class U, input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
159 requires std::convertible_to<U, T>
160 static arrow_proxy create_proxy(
161 size_type n,
162 const U& value = U{},
163 std::optional<std::string_view> name = std::nullopt,
164 std::optional<METADATA_RANGE> metadata = std::nullopt
165 );
166
167 // range of values, validity_bitmap_input
168 template <
169 std::ranges::input_range R,
170 validity_bitmap_input R2,
171 input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
172 requires(std::convertible_to<std::ranges::range_value_t<R>, T>)
173 static arrow_proxy create_proxy(
174 R&&,
175 R2&&,
176 std::optional<std::string_view> name = std::nullopt,
177 std::optional<METADATA_RANGE> metadata = std::nullopt
178 );
179
180 // range of nullable values
181 template <std::ranges::input_range R, input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
182 requires std::is_same_v<std::ranges::range_value_t<R>, nullable<T>>
183 static arrow_proxy create_proxy(
184 R&&,
185 std::optional<std::string_view> name = std::nullopt,
186 std::optional<METADATA_RANGE> metadata = std::nullopt
187 );
188
194
195 // Modifiers
196
201
202 static constexpr size_type DATA_BUFFER_INDEX = 1;
203
205 friend base_type;
208 };
209
210 /********************************************************
211 * primitive_array_impl implementation *
212 ********************************************************/
213
214 template <trivial_copyable_type T>
216 : base_type(std::move(proxy_param))
217 , access_class_type(this->get_arrow_proxy(), DATA_BUFFER_INDEX)
218 {
219 }
220
221 template <trivial_copyable_type T>
223 : base_type(rhs)
224 , access_class_type(this->get_arrow_proxy(), DATA_BUFFER_INDEX)
225 {
226 }
227
228 template <trivial_copyable_type T>
230 {
232 access_class_type::reset_proxy(this->get_arrow_proxy());
233 return *this;
234 }
235
236 template <trivial_copyable_type T>
238 : base_type(std::move(rhs))
239 , access_class_type(this->get_arrow_proxy(), DATA_BUFFER_INDEX)
240 {
241 }
242
243 template <trivial_copyable_type T>
245 {
246 base_type::operator=(std::move(rhs));
247 access_class_type::reset_proxy(this->get_arrow_proxy());
248 return *this;
249 }
250
251 template <trivial_copyable_type T>
252 template <validity_bitmap_input VALIDITY_RANGE, input_metadata_container METADATA_RANGE>
253 auto primitive_array_impl<T>::create_proxy(
254 u8_buffer<T>&& data_buffer,
255 size_t size,
256 VALIDITY_RANGE&& bitmap_input,
257 std::optional<std::string_view> name,
258 std::optional<METADATA_RANGE> metadata
259 ) -> arrow_proxy
260 {
261 validity_bitmap bitmap = ensure_validity_bitmap(size, std::forward<VALIDITY_RANGE>(bitmap_input));
262 const auto null_count = bitmap.null_count();
263
264 // create arrow schema and array
267 std::move(name), // name
268 std::move(metadata), // metadata
269 std::nullopt, // flags
270 nullptr, // children
271 repeat_view<bool>(true, 0), // children_ownership
272 nullptr, // dictionary
273 true // dictionary ownership
274 );
275
276 std::vector<buffer<uint8_t>> buffers(2);
277 buffers[0] = std::move(bitmap).extract_storage();
278 buffers[1] = std::move(data_buffer).extract_storage();
279
280 // create arrow array
282 static_cast<std::int64_t>(size), // length
283 static_cast<int64_t>(null_count),
284 0, // offset
285 std::move(buffers),
286 nullptr, // children
287 repeat_view<bool>(true, 0), // children_ownership
288 nullptr, // dictionary,
289 true // dictionary ownership
290 );
291 return arrow_proxy(std::move(arr), std::move(schema));
292 }
293
294 template <trivial_copyable_type T>
295 template <std::ranges::input_range VALUE_RANGE, validity_bitmap_input VALIDITY_RANGE, input_metadata_container METADATA_RANGE>
296 requires(std::convertible_to<std::ranges::range_value_t<VALUE_RANGE>, T>)
297 arrow_proxy primitive_array_impl<T>::create_proxy(
298 VALUE_RANGE&& values,
299 VALIDITY_RANGE&& validity_input,
300 std::optional<std::string_view> name,
301 std::optional<METADATA_RANGE> metadata
302 )
303 {
304 auto size = static_cast<size_t>(std::ranges::distance(values));
306 std::forward<VALUE_RANGE>(values)
307 );
308 return create_proxy(
309 std::move(data_buffer),
310 size,
311 std::forward<VALIDITY_RANGE>(validity_input),
312 std::move(name),
313 std::move(metadata)
314 );
315 }
316
317 template <trivial_copyable_type T>
318 template <class U, input_metadata_container METADATA_RANGE>
319 requires std::convertible_to<U, T>
320 arrow_proxy primitive_array_impl<T>::create_proxy(
321 size_type n,
322 const U& value,
323 std::optional<std::string_view> name,
324 std::optional<METADATA_RANGE> metadata
325 )
326 {
327 // create data_buffer
328 u8_buffer<T> data_buffer(n, value);
329 return create_proxy(std::move(data_buffer), std::move(name), std::move(metadata));
330 }
331
332 template <trivial_copyable_type T>
333 template <std::ranges::input_range R, input_metadata_container METADATA_RANGE>
334 requires std::convertible_to<std::ranges::range_value_t<R>, T>
335 arrow_proxy primitive_array_impl<T>::create_proxy(
336 R&& range,
337 std::optional<std::string_view> name,
338 std::optional<METADATA_RANGE> metadata
339 )
340 {
341 const std::size_t n = range_size(range);
342 const auto iota = std::ranges::iota_view{std::size_t(0), n};
343 std::ranges::transform_view iota_to_is_non_missing(
344 iota,
345 [](std::size_t)
346 {
347 return true;
348 }
349 );
350 return self_type::create_proxy(
351 std::forward<R>(range),
352 std::move(iota_to_is_non_missing),
353 std::move(name),
354 std::move(metadata)
355 );
356 }
357
358 // range of nullable values
359 template <trivial_copyable_type T>
360 template <std::ranges::input_range R, input_metadata_container METADATA_RANGE>
361 requires std::is_same_v<std::ranges::range_value_t<R>, nullable<T>>
362 arrow_proxy primitive_array_impl<T>::create_proxy(
363 R&& range,
364 std::optional<std::string_view> name,
365 std::optional<METADATA_RANGE> metadata
366 )
367 {
368 // split into values and is_non_null ranges
369 auto values = range
370 | std::views::transform(
371 [](const auto& v)
372 {
373 return v.get();
374 }
375 );
376 auto is_non_null = range
377 | std::views::transform(
378 [](const auto& v)
379 {
380 return v.has_value();
381 }
382 );
383 return self_type::create_proxy(values, is_non_null, std::move(name), std::move(metadata));
384 }
385}
array_bitmap_base_impl & operator=(const array_bitmap_base_impl &)
std::conditional_t< is_mutable, mutable_array_base< D >, array_crtp_base< D > > base_type
Proxy class over ArrowArray and ArrowSchema.
Data access class for trivial copyable types.
constexpr void resize_values(size_t new_length, const T &value)
constexpr value_iterator erase_values(const_value_iterator pos, size_t count)
static u8_buffer< T > make_data_buffer(RANGE &&r)
constexpr inner_reference value(size_t i)
constexpr value_iterator insert_values(const_value_iterator pos, InputIt first, InputIt last)
constexpr const_value_iterator value_cend() const
pointer_iterator< inner_const_pointer > const_value_iterator
constexpr value_iterator insert_value(const_value_iterator pos, T value, size_t count)
pointer_iterator< inner_pointer > value_iterator
constexpr const_value_iterator value_cbegin() const
constexpr size_type null_count() const noexcept
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
Definition nullable.hpp:280
typename base_type::value_iterator value_iterator
primitive_array_impl & operator=(primitive_array_impl &&)
typename base_type::const_value_iterator const_value_iterator
typename inner_types::pointer pointer
primitive_array_impl< T > self_type
primitive_array_impl(Args &&... args)
Constructs an array of trivial copyable type, with the passed range of values and an optional bitmap.
mutable_array_bitmap_base< primitive_array_impl< T > > base_type
primitive_array_impl(primitive_array_impl &&)
typename inner_types::inner_reference inner_reference
primitive_array_impl(const primitive_array_impl &)
typename inner_types::inner_const_reference inner_const_reference
primitive_array_impl(std::initializer_list< inner_value_type > init, std::optional< std::string_view > name=std::nullopt, std::optional< METADATA_RANGE > metadata=std::nullopt)
Constructs a primitive array from an initializer_list of raw values.
typename inner_types::const_pointer const_pointer
array_inner_types< self_type > inner_types
details::primitive_data_access< T > access_class_type
typename inner_types::inner_value_type inner_value_type
primitive_array_impl & operator=(const primitive_array_impl &)
A view that repeats a value a given number of times.
This buffer class is use as storage buffer for all sparrow arrays.
Definition u8_buffer.hpp:75
constexpr bool excludes_copy_and_move_ctor_v
Definition mp_utils.hpp:507
array_bitmap_base_impl< D, true > mutable_array_bitmap_base
Convenient typedef to be used as a crtp base class for arrays using a mutable validity buffer.
ArrowSchema make_arrow_schema(F format, N name, std::optional< M > metadata, std::optional< std::unordered_set< ArrowFlag > > flags, ArrowSchema **children, const CHILDREN_OWNERSHIP &children_ownership, ArrowSchema *dictionary, bool dictionary_ownership)
Creates an ArrowSchema owned by a unique_ptr and holding the provided data.
constexpr std::string_view data_type_format_of()
ArrowArray make_arrow_array(int64_t length, int64_t null_count, int64_t offset, B buffers, ArrowArray **children, const CHILDREN_OWNERSHIP &children_ownership, ArrowArray *dictionary, bool dictionary_ownership)
Creates an ArrowArray.
dynamic_bitset< std::uint8_t > validity_bitmap
validity_bitmap ensure_validity_bitmap(std::size_t size, R &&validity_input)
std::size_t range_size(R &&r)
Definition ranges.hpp:33
typename data_access_type::inner_value_type inner_value_type
typename data_access_type::value_iterator value_iterator
typename data_access_type::inner_const_pointer const_pointer
typename data_access_type::inner_const_reference inner_const_reference
typename data_access_type::inner_reference inner_reference
typename data_access_type::const_value_iterator const_value_iterator
nullable< inner_const_reference, bitmap_const_reference > const_reference
Base class for array_inner_types specialization.
Traits class that must be specialized by array classes inheriting from array_crtp_base.