sparrow
2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Toggle main menu visibility
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
"
21
#include "
sparrow/arrow_interface/arrow_array.hpp
"
22
#include "
sparrow/arrow_interface/arrow_array_stream.hpp
"
23
#include "
sparrow/arrow_interface/arrow_schema.hpp
"
24
#include "
sparrow/c_interface.hpp
"
25
#include "
sparrow/layout/layout_concept.hpp
"
26
27
namespace
sparrow
28
{
56
class
arrow_array_stream_proxy
57
{
58
public
:
59
69
SPARROW_API
arrow_array_stream_proxy
();
70
82
SPARROW_API
explicit
arrow_array_stream_proxy
(
ArrowArrayStream
&& stream);
83
97
SPARROW_API
explicit
arrow_array_stream_proxy
(
ArrowArrayStream
* stream);
98
99
// explicit arrow_array_stream_proxy(ArrowSchema* schema_ptr);
100
101
arrow_array_stream_proxy
(
const
arrow_array_stream_proxy
&) =
delete
;
102
arrow_array_stream_proxy
&
operator=
(
const
arrow_array_stream_proxy
&) =
delete
;
103
104
SPARROW_API
105
arrow_array_stream_proxy
(
arrow_array_stream_proxy
&& other)
noexcept
;
106
SPARROW_API
107
arrow_array_stream_proxy
&
operator=
(
arrow_array_stream_proxy
&& other)
noexcept
;
108
115
SPARROW_API
~arrow_array_stream_proxy
();
116
120
[[nodiscard]]
SPARROW_API
bool
owns_stream
()
const
;
121
136
[[nodiscard]]
SPARROW_API
ArrowArrayStream
*
export_stream
();
137
150
template
<std::ranges::input_range R>
151
requires
layout_or_array<std::ranges::range_value_t<R>
>
152
void
push
(R&& arrays)
153
{
154
arrow_array_stream_private_data
& private_data = get_private_data();
155
156
// Check if we need to create schema from first array
157
if
(private_data.
schema
() ==
nullptr
)
158
{
159
schema_unique_ptr
schema{
new
ArrowSchema
(),
arrow_schema_deleter
{}};
160
copy_schema
(*
get_arrow_schema
(*std::ranges::begin(arrays)), *schema);
161
private_data.
import_schema
(std::move(schema));
162
}
163
164
// Validate schema compatibility for all arrays
165
for
(
const
auto
&
array
: arrays)
166
{
167
if
(!
check_compatible_schema
(*private_data.
schema
(), *
get_arrow_schema
(
array
)))
168
{
169
throw
std::runtime_error(
"Incompatible schema when adding array to ArrowArrayStream"
);
170
}
171
}
172
173
// Import all arrays
174
for
(
auto
&&
array
: std::forward<R>(arrays))
175
{
176
ArrowArray
extracted_array =
extract_arrow_array
(std::move(
array
));
177
array_unique_ptr
array_ptr{
new
ArrowArray
(),
arrow_array_deleter
{}};
178
swap
(*array_ptr, extracted_array);
179
private_data.
import_array
(std::move(array_ptr));
180
}
181
}
182
195
template
<layout_or_array A>
196
void
push
(A&&
array
)
197
{
198
push
(std::ranges::single_view(std::forward<A>(
array
)));
199
}
200
214
SPARROW_API
std::optional<array>
pop
();
215
216
private
:
217
218
std::variant<ArrowArrayStream*, ArrowArrayStream> m_stream;
219
225
[[nodiscard]]
ArrowArrayStream
* get_stream_ptr();
226
232
[[nodiscard]]
const
ArrowArrayStream
* get_stream_ptr()
const
;
233
243
void
throw_if_immutable()
const
;
244
250
[[nodiscard]]
SPARROW_API
arrow_array_stream_private_data
& get_private_data();
251
};
252
}
array.hpp
array_api.hpp
arrow_array.hpp
arrow_array_stream.hpp
Implementation of the Arrow C Stream Interface for streaming data exchange.
arrow_schema.hpp
c_interface.hpp
sparrow::array
Dynamically typed array encapsulating an Arrow layout.
Definition
array_api.hpp:50
sparrow::arrow_array_stream_private_data
Definition
private_data.hpp:29
sparrow::arrow_array_stream_private_data::schema
ArrowSchema * schema()
Definition
private_data.hpp:39
sparrow::arrow_array_stream_private_data::import_schema
void import_schema(schema_unique_ptr &&out_schema)
Definition
private_data.hpp:34
sparrow::arrow_array_stream_private_data::import_array
void import_array(array_unique_ptr &&array)
Definition
private_data.hpp:59
sparrow::arrow_array_stream_proxy::arrow_array_stream_proxy
arrow_array_stream_proxy(const arrow_array_stream_proxy &)=delete
sparrow::arrow_array_stream_proxy::push
void push(R &&arrays)
Adds a range of arrays to the stream.
Definition
arrow_array_stream_proxy.hpp:152
sparrow::arrow_array_stream_proxy::export_stream
SPARROW_API ArrowArrayStream * export_stream()
Export the stream pointer.
sparrow::arrow_array_stream_proxy::arrow_array_stream_proxy
SPARROW_API arrow_array_stream_proxy(ArrowArrayStream &&stream)
Constructs from an existing ArrowArrayStream by taking ownership.
sparrow::arrow_array_stream_proxy::owns_stream
SPARROW_API bool owns_stream() const
Check whether the proxy has ownership of its internal ArrowArrayStream.
sparrow::arrow_array_stream_proxy::arrow_array_stream_proxy
SPARROW_API arrow_array_stream_proxy(arrow_array_stream_proxy &&other) noexcept
sparrow::arrow_array_stream_proxy::~arrow_array_stream_proxy
SPARROW_API ~arrow_array_stream_proxy()
Destructor that releases all resources.
sparrow::arrow_array_stream_proxy::arrow_array_stream_proxy
SPARROW_API arrow_array_stream_proxy()
Constructs a new ArrowArrayStream producer.
sparrow::arrow_array_stream_proxy::pop
SPARROW_API std::optional< array > pop()
Retrieves the next array from the stream.
sparrow::arrow_array_stream_proxy::operator=
SPARROW_API arrow_array_stream_proxy & operator=(arrow_array_stream_proxy &&other) noexcept
sparrow::arrow_array_stream_proxy::operator=
arrow_array_stream_proxy & operator=(const arrow_array_stream_proxy &)=delete
sparrow::arrow_array_stream_proxy::arrow_array_stream_proxy
SPARROW_API arrow_array_stream_proxy(ArrowArrayStream *stream)
Constructs from an existing ArrowArrayStream pointer by referencing it.
sparrow::arrow_array_stream_proxy::push
void push(A &&array)
Adds a single array to the stream.
Definition
arrow_array_stream_proxy.hpp:196
sparrow::layout_or_array
Definition
array_api.hpp:471
SPARROW_API
#define SPARROW_API
Definition
config.hpp:38
layout_concept.hpp
sparrow
Definition
array.hpp:21
sparrow::copy_schema
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.
sparrow::schema_unique_ptr
std::unique_ptr< ArrowSchema, arrow_schema_deleter > schema_unique_ptr
Definition
arrow_schema.hpp:263
sparrow::extract_arrow_array
ArrowArray extract_arrow_array(A &&a)
Extracts the internal ArrowArray structure from the given Array or typed layout.
Definition
array.hpp:98
sparrow::array_unique_ptr
std::unique_ptr< ArrowArray, arrow_array_deleter > array_unique_ptr
Definition
arrow_array.hpp:239
sparrow::check_compatible_schema
bool SPARROW_API check_compatible_schema(const ArrowSchema &schema1, const ArrowSchema &schema2)
sparrow::swap
SPARROW_API void swap(ArrowArray &lhs, ArrowArray &rhs) noexcept
Swaps the contents of the two ArrowArray objects.
sparrow::get_arrow_schema
ArrowSchema * get_arrow_schema(A &a)
Returns a pointer to the internal ArrowSchema of the given array or layout.
Definition
array.hpp:72
ArrowArrayStream
Definition
c_stream_interface.hpp:25
ArrowArray
Definition
c_interface.hpp:43
ArrowSchema
Definition
c_interface.hpp:26
sparrow::arrow_array_deleter
Definition
arrow_array.hpp:235
sparrow::arrow_schema_deleter
Definition
arrow_schema.hpp:259
sparrow
arrow_interface
arrow_array_stream_proxy.hpp
Generated by
1.17.0