sparrow 0.3.0
Loading...
Searching...
No Matches
arrow_schema.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 <cstdint>
18#include <memory>
19#if defined(__cpp_lib_format)
20# include <format>
21# include <ostream>
22#endif
23
27
28namespace sparrow
29{
52 template <class F, class N, class M>
53 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
54 && std::constructible_from<arrow_schema_private_data::NameType, N>
55 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
56 [[nodiscard]] ArrowSchema make_arrow_schema(
57 F format,
58 N name,
59 M metadata,
60 std::optional<ArrowFlag> flags,
61 int64_t n_children,
62 ArrowSchema** children,
63 ArrowSchema* dictionary
64 );
65
70
76
77 template <class F, class N, class M>
78 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
79 && std::constructible_from<arrow_schema_private_data::NameType, N>
80 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
82 ArrowSchema& schema,
83 F format,
84 N name,
85 M metadata,
86 std::optional<ArrowFlag> flags,
87 int64_t n_children,
88 ArrowSchema** children,
89 ArrowSchema* dictionary
90 )
91 {
92 SPARROW_ASSERT_TRUE(n_children >= 0);
93 SPARROW_ASSERT_TRUE(n_children > 0 ? children != nullptr : children == nullptr);
94 SPARROW_ASSERT_FALSE(format.empty());
95 if (children)
96 {
97 for (int64_t i = 0; i < n_children; ++i)
98 {
99 SPARROW_ASSERT_FALSE(children[i] == nullptr);
100 }
101 }
102
103 schema.flags = flags.has_value() ? static_cast<int64_t>(flags.value()) : 0;
104 schema.n_children = n_children;
105
107 std::move(format),
108 std::move(name),
109 std::move(metadata),
110 static_cast<std::size_t>(n_children)
111 );
112
113 const auto private_data = static_cast<arrow_schema_private_data*>(schema.private_data);
114 schema.format = private_data->format_ptr();
115 schema.name = private_data->name_ptr();
116 schema.metadata = private_data->metadata_ptr();
117 schema.children = children;
118 schema.dictionary = dictionary;
120 }
121
122 template <class F, class N, class M>
123 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
124 && std::constructible_from<arrow_schema_private_data::NameType, N>
125 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
127 F format,
128 N name,
129 M metadata,
130 std::optional<ArrowFlag> flags,
131 int64_t n_children,
132 ArrowSchema** children,
133 ArrowSchema* dictionary
134 )
135 {
136 SPARROW_ASSERT_TRUE(n_children >= 0);
137 SPARROW_ASSERT_TRUE(n_children > 0 ? children != nullptr : children == nullptr);
138 SPARROW_ASSERT_FALSE(format.empty());
139 if (children)
140 {
141 for (int64_t i = 0; i < n_children; ++i)
142 {
143 SPARROW_ASSERT_FALSE(children[i] == nullptr);
144 }
145 }
146
147 ArrowSchema schema{};
148 fill_arrow_schema(schema, format, name, metadata, flags, n_children, children, dictionary);
149 return schema;
150 };
151
153 {
154 return make_arrow_schema(std::string_view("n"), "", "", std::nullopt, 0, nullptr, nullptr);
155 }
156
161
165 SPARROW_API void copy_schema(const ArrowSchema& source, ArrowSchema& target);
166
173 [[nodiscard]]
174 inline ArrowSchema copy_schema(const ArrowSchema& source)
175 {
176 ArrowSchema target{};
177 copy_schema(source, target);
178 return target;
179 }
180
186 {
188 swap(source, target);
189 return target;
190 }
191
197 {
198 return move_schema(std::move(source));
199 }
200}
201
202#if defined(__cpp_lib_format)
203
204template <>
205struct std::formatter<ArrowSchema>
206{
207 constexpr auto parse(std::format_parse_context& ctx)
208 {
209 return ctx.begin(); // Simple implementation
210 }
211
212 auto format(const ArrowSchema& obj, std::format_context& ctx) const
213 {
214 std::string children_str = std::format("{}", static_cast<void*>(obj.children));
215 for (int i = 0; i < obj.n_children; ++i)
216 {
217 children_str += std::format("\n-{}", static_cast<void*>(obj.children[i]));
218 }
219
220 const std::string format = obj.format ? obj.format : "nullptr";
221 const std::string name = obj.name ? obj.name : "nullptr";
222 const std::string metadata = obj.metadata ? obj.metadata : "nullptr";
223
224 return std::format_to(
225 ctx.out(),
226 "ArrowSchema - ptr address: {}\n- format: {}\n- name: {}\n- metadata: {}\n- flags: {}\n- n_children: {}\n- children: {}\n- dictionary: {}\n- release: {}\n- private_data: {}\n",
227 static_cast<const void*>(&obj),
228 format,
229 name,
230 metadata,
231 obj.flags,
232 obj.n_children,
233 children_str,
234 static_cast<const void*>(obj.dictionary),
235 static_cast<const void*>(std::addressof(obj.release)),
236 obj.private_data
237 );
238 }
239};
240
241inline std::ostream& operator<<(std::ostream& os, const ArrowSchema& value)
242{
243 os << std::format("{}", value);
244 return os;
245}
246
247#endif
Private data for ArrowSchema.
#define SPARROW_API
Definition config.hpp:38
#define SPARROW_ASSERT_TRUE(expr__)
#define SPARROW_ASSERT_FALSE(expr__)
ArrowSchema make_arrow_schema(F format, N name, M metadata, std::optional< ArrowFlag > flags, int64_t n_children, ArrowSchema **children, ArrowSchema *dictionary)
Creates an ArrowSchema owned by a unique_ptr and holding the provided data.
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_API void release_arrow_schema(ArrowSchema *schema)
Release function to use for the ArrowSchema.release member.
SPARROW_API void swap(ArrowArray &lhs, ArrowArray &rhs)
Swaps the contents of the two ArrowArray objects.
SPARROW_API void empty_release_arrow_schema(ArrowSchema *schema)
Empty release function to use for the ArrowSchema.release member.
ArrowSchema move_schema(ArrowSchema &&source)
Moves the content of source into a stack-allocated array, and reset the source to an empty ArrowSchem...
std::ostream & operator<<(std::ostream &stream, T n)
Definition large_int.hpp:93
void fill_arrow_schema(ArrowSchema &schema, F format, N name, M metadata, std::optional< ArrowFlag > flags, int64_t n_children, ArrowSchema **children, ArrowSchema *dictionary)
ArrowSchema make_empty_arrow_schema()
int64_t flags
const char * metadata
int64_t n_children
const char * name
void * private_data
const char * format
struct ArrowSchema * dictionary
void(* release)(struct ArrowSchema *)
struct ArrowSchema ** children