34#include "iceberg/iceberg_export.h"
35#include "iceberg/result.h"
45 using ProjectBatchState = std::shared_ptr<void>;
47 using ProjectBatchFunction =
auto (*)(
ArrowArray* input_batch,
48 std::span<const int32_t> row_indices,
50 -> Result<ArrowArray>;
56 static void RegisterProjectBatchFunction(ProjectBatchFunction project_batch_function);
59 static bool HasProjectBatchFunction();
62 static auto ResolveProjectBatchFunction() -> ProjectBatchFunction;
75 static Result<ProjectionContext> Make(
const Schema& input_schema,
76 const Schema& output_schema,
77 ProjectBatchFunction project_batch_function);
86 const Schema& input_schema()
const;
88 const Schema& output_schema()
const;
94 std::span<const int32_t> selected_field_indices()
const;
96 ProjectBatchFunction project_batch_function()
const;
98 ProjectBatchState& project_batch_state();
105 const Schema* input_schema_ =
nullptr;
106 const Schema* output_schema_ =
nullptr;
107 std::vector<int32_t> selected_field_indices_;
110 ProjectBatchFunction project_batch_function_ =
nullptr;
111 ProjectBatchState project_batch_state_;
115template <
typename Source>
117 { source.Close() } -> std::same_as<Status>;
118 { source.Next() } -> std::same_as<Result<std::optional<ArrowArray>>>;
119 { source.Schema() } -> std::same_as<Result<ArrowSchema>>;
124template <ArrowArrayStreamProv
ider Source>
125struct ArrowArrayStreamPrivateData {
126 std::unique_ptr<Source> source;
127 std::string last_error;
129 explicit ArrowArrayStreamPrivateData(std::unique_ptr<Source> src)
130 : source(
std::move(src)) {}
132 ~ArrowArrayStreamPrivateData() {
133 if (source !=
nullptr) {
134 std::ignore = source->Close();
139template <ArrowArrayStreamProv
ider Source>
141 if (stream ==
nullptr || stream->private_data ==
nullptr) {
146 static_cast<ArrowArrayStreamPrivateData<Source>*
>(stream->private_data);
147 auto schema_result = private_data->source->Schema();
148 if (!schema_result.has_value()) {
149 private_data->last_error = schema_result.error().message;
154 *out = std::move(schema_result.value());
158template <ArrowArrayStreamProv
ider Source>
160 if (stream ==
nullptr || stream->private_data ==
nullptr) {
165 static_cast<ArrowArrayStreamPrivateData<Source>*
>(stream->private_data);
166 auto next_result = private_data->source->Next();
167 if (!next_result.has_value()) {
168 private_data->last_error = next_result.error().message;
173 auto& optional_array = next_result.value();
174 if (optional_array.has_value()) {
175 *out = std::move(optional_array.value());
183template <ArrowArrayStreamProv
ider Source>
185 if (stream ==
nullptr || stream->private_data ==
nullptr) {
190 static_cast<ArrowArrayStreamPrivateData<Source>*
>(stream->private_data);
191 return private_data->last_error.empty() ? nullptr : private_data->last_error.c_str();
194template <ArrowArrayStreamProv
ider Source>
196 if (stream ==
nullptr || stream->private_data ==
nullptr) {
200 delete static_cast<ArrowArrayStreamPrivateData<Source>*
>(stream->private_data);
201 stream->private_data =
nullptr;
202 stream->release =
nullptr;
208template <ArrowArrayStreamProv
ider Source>
209Result<ArrowArrayStream> MakeArrowArrayStream(std::unique_ptr<Source> source) {
210 if (source ==
nullptr) {
211 return InvalidArgument(
"Cannot make ArrowArrayStream from null source");
215 std::make_unique<detail::ArrowArrayStreamPrivateData<Source>>(std::move(source));
217 .get_next = detail::GetNext<Source>,
218 .get_last_error = detail::GetLastError<Source>,
219 .release = detail::Release<Source>,
220 .private_data = private_data.release()};
225ICEBERG_EXPORT Result<ArrowArrayStream> MakeArrowArrayStream(
226 std::unique_ptr<Reader> reader);
241ICEBERG_EXPORT Result<ArrowArray> ProjectBatch(
ArrowArray* input_batch,
242 std::span<const int32_t> row_indices,
243 ProjectionContext& projection);
Cached state for ProjectBatch over one input/output schema pair.
Definition arrow_c_data_util_internal.h:43
A schema for a Table.
Definition schema.h:51
Concept for sources that can be wrapped as ArrowArrayStreams.
Definition arrow_c_data_util_internal.h:116
Definition arrow_c_data.h:79
Definition arrow_c_data.h:57
Definition arrow_c_data.h:41