sparrow 1.3.0
Loading...
Searching...
No Matches
arrow_array_stream_proxy.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#pragma once
16
17#include <ranges>
18
19#include "sparrow/array.hpp"
20#include "sparrow/array_api.hpp"
26
27namespace sparrow
28{
57 {
58 public:
59
70
83
98
99 // explicit arrow_array_stream_proxy(ArrowSchema* schema_ptr);
100
103
106
129
142 template <std::ranges::input_range R>
144 void push(R&& arrays)
145 {
146 arrow_array_stream_private_data& private_data = *get_private_data();
147
148 // Check if we need to create schema from first array
149 if (private_data.schema() == nullptr)
150 {
152 copy_schema(*get_arrow_schema(*std::ranges::begin(arrays)), *schema);
153 private_data.import_schema(std::move(schema));
154 }
155
156 // Validate schema compatibility for all arrays
157 for (const auto& array : arrays)
158 {
159 if (!check_compatible_schema(*private_data.schema(), *get_arrow_schema(array)))
160 {
161 throw std::runtime_error("Incompatible schema when adding array to ArrowArrayStream");
162 }
163 }
164
165 // Import all arrays
166 for (auto&& array : std::forward<R>(arrays))
167 {
168 ArrowArray extracted_array = extract_arrow_array(std::move(array));
170 swap(*array_ptr, extracted_array);
171 private_data.import_array(std::move(array_ptr));
172 }
173 }
174
187 template <layout A>
188 void push(A&& array)
189 {
190 push(std::ranges::single_view(std::forward<A>(array)));
191 }
192
206 SPARROW_API std::optional<array> pop();
207
208 private:
209
210 std::variant<ArrowArrayStream*, ArrowArrayStream> m_stream;
211
217 [[nodiscard]] ArrowArrayStream* get_stream_ptr();
218
224 [[nodiscard]] const ArrowArrayStream* get_stream_ptr() const;
225
235 void throw_if_immutable() const;
236
242 [[nodiscard]] SPARROW_API const arrow_array_stream_private_data* get_private_data() const;
243
249 [[nodiscard]] SPARROW_API arrow_array_stream_private_data* get_private_data();
250 };
251}
Implementation of the Arrow C Stream Interface for streaming data exchange.
Dynamically typed array encapsulating an Arrow layout.
Definition array_api.hpp:43
void import_schema(schema_unique_ptr &&out_schema)
void import_array(array_unique_ptr &&array)
arrow_array_stream_proxy(const arrow_array_stream_proxy &)=delete
arrow_array_stream_proxy(arrow_array_stream_proxy &&other) noexcept
SPARROW_API ArrowArrayStream * export_stream()
Export the stream pointer.
SPARROW_API arrow_array_stream_proxy(ArrowArrayStream &&stream)
Constructs from an existing ArrowArrayStream by taking ownership.
SPARROW_API ~arrow_array_stream_proxy()
Destructor that releases all resources.
void push(R &&arrays)
Adds a range of arrays to the stream.
SPARROW_API arrow_array_stream_proxy()
Constructs a new ArrowArrayStream producer.
SPARROW_API std::optional< array > pop()
Retrieves the next array from the stream.
arrow_array_stream_proxy & operator=(const arrow_array_stream_proxy &)=delete
arrow_array_stream_proxy & operator=(arrow_array_stream_proxy &&other) noexcept
SPARROW_API arrow_array_stream_proxy(ArrowArrayStream *stream)
Constructs from an existing ArrowArrayStream pointer by referencing it.
void push(A &&array)
Adds a single array to the stream.
Concept for layouts.
#define SPARROW_API
Definition config.hpp:38
SPARROW_API void copy_schema(const ArrowSchema &source, ArrowSchema &target)
Fills the target ArrowSchema with a deep copy of the data from the source ArrowSchema.
std::unique_ptr< ArrowSchema, arrow_schema_deleter > schema_unique_ptr
ArrowArray extract_arrow_array(A &&a)
Extracts the internal ArrowArray structure from the given Array or typed layout.
Definition array.hpp:98
std::unique_ptr< ArrowArray, arrow_array_deleter > array_unique_ptr
bool SPARROW_API check_compatible_schema(const ArrowSchema &schema1, const ArrowSchema &schema2)
SPARROW_API void swap(ArrowArray &lhs, ArrowArray &rhs) noexcept
Swaps the contents of the two ArrowArray objects.
ArrowSchema * get_arrow_schema(A &a)
Returns a pointer to the internal ArrowSchema of the given array or layout.
Definition array.hpp:72