sparrow
2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Toggle main menu visibility
Loading...
Searching...
No Matches
builder_example.cpp
Go to the documentation of this file.
1
#include <array>
2
#include <cassert>
3
#include <list>
4
#include <string>
5
#include <tuple>
6
#include <vector>
7
8
#include <
sparrow/builder.hpp
>
9
10
void
primitve_array
()
11
{
13
// using initializer_list
14
auto
arr =
sparrow::build
({1, 2, 3, 4, 5});
16
// using vector
17
std::vector<int> v{1, 2, 3, 4, 5};
18
auto
arr2 =
sparrow::build
(v);
20
// using list
21
std::list<int> l{1, 2, 3, 4, 5};
22
auto
arr3 =
sparrow::build
(l);
24
// using any range
25
auto
iota = std::views::iota(1, 6)
26
| std::views::transform(
27
[](
int
i)
28
{
29
return
static_cast<
int
>
(i);
30
}
31
);
32
auto
arr4 =
sparrow::build
(iota);
34
// all of the arrays above are equivalent to the manually built array
35
auto
arr5 =
sparrow::primitive_array<int>
({1, 2, 3, 4, 5});
36
assert(arr == arr2);
37
assert(arr == arr3);
38
assert(arr == arr4);
39
assert(arr == arr5);
41
}
42
43
void
primitve_array_with_nulls
()
44
{
46
// using initializer_list (here we have to explicitly specify the type when using an
47
// initializer list with nulls)
48
auto
arr =
sparrow::build<sparrow::nullable<int>
>({1, 2,
sparrow::nullval
, 4, 5});
50
// using vector
51
std::vector<sparrow::nullable<int>> v{1, 2,
sparrow::nullval
, 4, 5};
52
auto
arr2 =
sparrow::build
(v);
54
// using list
55
std::list<sparrow::nullable<int>> l{1, 2,
sparrow::nullval
, 4, 5};
56
auto
arr3 =
sparrow::build
(l);
58
// using any range
59
auto
iota = std::views::iota(1, 6)
60
| std::views::transform(
61
[](
int
i) ->
sparrow::nullable<int>
62
{
63
return
i == 3 ?
sparrow::nullable<int>
{} :
sparrow::nullable<int>
{i};
64
}
65
);
66
auto
arr4 =
sparrow::build
(iota);
67
// all of the arrays above are equivalent to the manually built array
68
std::vector<std::size_t> where_nulls{2};
69
sparrow::u8_buffer<int>
values{1, 2, 3, 4, 5};
70
auto
arr5 =
sparrow::primitive_array<int>
(std::move(values), where_nulls);
71
assert(arr == arr2);
72
assert(arr == arr3);
73
assert(arr == arr4);
75
}
76
77
void
list_of_strings
()
78
{
80
// [["hello", "world","!"], ["Another", "sentence"]]
81
std::vector<std::vector<std::string>> v{{
"hello"
,
"world"
,
"!"
}, {
"Another"
,
"sentence"
}};
82
auto
arr =
sparrow::build
(v);
84
}
85
86
void
list_of_strings_with_nulls
()
87
{
89
// [["hello", "world","!"],NULL , ["Another", "sentence"]]
90
using
string_vector = std::vector<std::string>;
91
using
nullable_string_vector =
sparrow::nullable<string_vector>
;
92
std::vector<nullable_string_vector> v{
93
nullable_string_vector{string_vector{
"hello"
,
"world"
,
"!"
}},
94
nullable_string_vector{},
95
nullable_string_vector{string_vector{
"Another"
,
"sentence"
}}
96
};
97
auto
arr =
sparrow::build
(v);
99
}
100
101
void
list_of_struct
()
102
{
104
/*
105
[
106
[
107
(1, 2.5),
108
(2, 3.5)
109
],
110
[
111
(3, 5.5),
112
(5, 6.5),
113
(6, 7.5)
114
],
115
[
116
(7, 8.5)
117
]
118
]
119
*/
120
std::vector<std::vector<std::tuple<int, float>>> v{
121
{std::tuple<int, float>{1, 2.5f}, std::tuple<int, float>{2, 3.5f}},
122
{std::tuple<int, float>{3, 5.5f}, std::tuple<int, float>{5, 6.5f}, std::tuple<int, float>{6, 7.5f}},
123
{std::tuple<int, float>{7, 8.5f}}
124
};
125
auto
arr =
sparrow::build
(v);
127
}
128
129
void
fixed_sized_list_strings
()
130
{
132
std::vector<std::array<std::string, 2>> v{{
"hello"
,
"world"
}, {
"Another"
,
"sentence"
}, {
"This"
,
"is"
}};
133
auto
arr =
sparrow::build
(v);
135
}
136
137
void
fixed_sized_list_of_union
()
138
{
140
using
variant_type = std::variant<int, float>;
141
using
array_type = std::array<variant_type, 2>;
142
std::vector<array_type> v{{1, 2.5f}, {2, 3.5f}, {3, 4.5f}};
143
auto
arr =
sparrow::build
(v);
145
}
146
147
void
dict_encoded_variable_sized_binary
()
148
{
150
sparrow::dict_encode<std::vector<std::string>
> v{std::vector<std::string>{
151
"hello"
,
152
"world"
,
153
"hello"
,
154
"world"
,
155
"hello"
,
156
}};
157
auto
arr =
sparrow::build
(v);
159
}
160
161
void
run_end_encoded_variable_sized_binary
()
162
{
164
sparrow::run_end_encode<std::vector<std::string>
> v{std::vector<std::string>{
165
"hello"
,
166
"hello"
,
167
"hello"
,
168
"world"
,
169
"world"
,
170
}};
171
auto
arr =
sparrow::build
(v);
173
}
174
175
void
struct_array
()
176
{
178
using
tuple_type = std::tuple<int, std::array<std::string, 2>,
sparrow::nullable<float>
>;
179
std::vector<tuple_type> v{
180
{1, {
"hello"
,
"world"
}, 2.5f},
181
{2, {
"Another"
,
"sentence"
},
sparrow::nullval
},
182
{3, {
"This"
,
"is"
}, 3.5f}
183
};
184
auto
arr =
sparrow::build
(v);
186
}
187
188
void
sparse_union_array
()
189
{
191
using
variant_type = std::variant<int, std::array<std::string, 2>,
sparrow::nullable<float>
>;
192
std::vector<variant_type> v{
193
int
{1},
194
std::array<std::string, 2>{{
"A"
,
"sentence"
}},
195
sparrow::nullable<float>
{2.5f},
196
sparrow::nullable<float>
{}
197
};
198
auto
arr =
sparrow::build
(v);
200
}
201
202
int
main
()
203
{
204
primitve_array
();
205
primitve_array_with_nulls
();
206
list_of_strings
();
207
list_of_strings_with_nulls
();
208
list_of_struct
();
209
fixed_sized_list_strings
();
210
fixed_sized_list_of_union
();
211
dict_encoded_variable_sized_binary
();
212
run_end_encoded_variable_sized_binary
();
213
struct_array
();
214
sparse_union_array
();
215
216
return
0;
217
}
builder.hpp
dict_encoded_variable_sized_binary
void dict_encoded_variable_sized_binary()
Definition
builder_example.cpp:147
run_end_encoded_variable_sized_binary
void run_end_encoded_variable_sized_binary()
Definition
builder_example.cpp:161
sparse_union_array
void sparse_union_array()
Definition
builder_example.cpp:188
list_of_strings_with_nulls
void list_of_strings_with_nulls()
Definition
builder_example.cpp:86
struct_array
void struct_array()
Definition
builder_example.cpp:175
list_of_strings
void list_of_strings()
Definition
builder_example.cpp:77
fixed_sized_list_strings
void fixed_sized_list_strings()
Definition
builder_example.cpp:129
primitve_array_with_nulls
void primitve_array_with_nulls()
Definition
builder_example.cpp:43
list_of_struct
void list_of_struct()
Definition
builder_example.cpp:101
primitve_array
void primitve_array()
Definition
builder_example.cpp:10
fixed_sized_list_of_union
void fixed_sized_list_of_union()
Definition
builder_example.cpp:137
main
int main()
Definition
builder_example.cpp:202
sparrow::dict_encode
Definition
builder_utils.hpp:74
sparrow::nullable
Definition
nullable.hpp:312
sparrow::run_end_encode
Definition
builder_utils.hpp:89
sparrow::u8_buffer
This buffer class is used as storage buffer for all sparrow arrays.
Definition
u8_buffer.hpp:116
sparrow::build
constexpr auto build(T &&t, OPTION_FLAGS &&...)
function to create a sparrow array from arbitrary nested combinations of ranges, tuples,...
Definition
builder.hpp:80
sparrow::nullval
constexpr nullval_t nullval(0)
sparrow::primitive_array
primitive_array_impl< T, Ext, T2 > primitive_array
Array of values of whose type has fixed binary size.
Definition
primitive_array.hpp:61
examples
builder_example.cpp
Generated by
1.17.0