-
Notifications
You must be signed in to change notification settings - Fork 0
/
avro_arrow_adapter.cc
250 lines (220 loc) · 8.95 KB
/
avro_arrow_adapter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <sstream>
#include <avro.h>
#include "arrow/api.h"
#include "arrow/table_builder.h"
using namespace arrow;
#define RETURN_AVRO_ERROR(res) \
do { \
if (res != 0) { \
return Status::IOError(avro_strerror()); \
} \
} while (false) \
std::shared_ptr<DataType> get_arrow_type(avro_schema_t schema) {
auto avro_type = schema->type;
switch (avro_type) {
case AVRO_BOOLEAN:
return boolean();
case AVRO_INT32:
return int32();
case AVRO_INT64:
return int64();
case AVRO_FLOAT:
return float32();
case AVRO_DOUBLE:
return float64();
case AVRO_STRING:
return utf8();
case AVRO_BYTES:
return binary();
case AVRO_FIXED: {
auto length = avro_schema_fixed_size(schema);
return fixed_size_binary(length);
}
case AVRO_ARRAY: {
auto elem_type = get_arrow_type(avro_schema_array_items(schema));
return list(elem_type);
}
case AVRO_MAP: {
auto value_type = get_arrow_type(avro_schema_map_values(schema));
auto fields = {field("key", utf8()), field("value", value_type)};
return list(struct_(fields));
}
case AVRO_UNION: {
size_t size = avro_schema_union_size(schema);
auto fields = std::vector<std::shared_ptr<Field>>();
std::vector<uint8_t> type_codes;
for (size_t idx = 0; idx < size; idx++) {
auto child = avro_schema_union_branch(schema, idx);
auto f = field("_union_" + std::to_string(idx),
get_arrow_type(child));
fields.push_back(f);
type_codes.push_back((uint8_t) idx);
};
return union_(fields, type_codes);
}
case AVRO_RECORD: {
size_t size = avro_schema_record_size(schema);
auto fields = std::vector<std::shared_ptr<Field>>();
for (size_t idx = 0; idx < size; idx++) {
auto child = avro_schema_record_field_get_by_index(schema, idx);
auto elem_type = get_arrow_type(child);
auto name = avro_schema_record_field_name(schema, idx);
auto f = field(name, elem_type);
fields.insert(fields.end(), f);
}
return struct_(fields);
}
case AVRO_NULL:return null();
case AVRO_ENUM: break;
case AVRO_LINK: break;
}
// TODO: unhandled case ??
return null();
}
std::shared_ptr<Schema> avro_to_arrow(avro_schema_t schema) {
size_t size = avro_schema_record_size(schema);
auto fields = std::vector<std::shared_ptr<Field>>();
for (size_t idx = 0; idx < size; idx++) {
auto child = avro_schema_record_field_get_by_index(schema, idx);
auto elem_type = get_arrow_type(child);
auto name = avro_schema_record_field_name(schema, idx);
auto f = field(name, elem_type);
fields.insert(fields.end(), f);
}
return std::make_shared<Schema>(fields, nullptr);;
}
Status generic_read(const avro_value_t val, ArrayBuilder *builder);
Status read_string(const avro_value_t val, ArrayBuilder *builder) {
size_t str_size;
const char *c_str = NULL;
RETURN_AVRO_ERROR(avro_value_get_string(&val, &c_str, &str_size));
return static_cast<StringBuilder *>(builder)->Append(c_str);
}
Status read_binary(const avro_value_t val, ArrayBuilder *builder) {
size_t buf_len;
const void *buf;
RETURN_AVRO_ERROR(avro_value_get_bytes(&val, &buf, &buf_len));
return static_cast<BinaryBuilder *>(builder)->Append(reinterpret_cast<const uint8_t *>(buf), buf_len);
};
Status read_fixed(const avro_value_t val, ArrayBuilder *builder) {
size_t buf_len;
const void *buf = NULL;
RETURN_AVRO_ERROR(avro_value_get_fixed(&val, &buf, &buf_len));
return static_cast<FixedSizeBinaryBuilder *>(builder)->Append(reinterpret_cast<const uint8_t *>(buf));
}
Status read_int32(const avro_value_t val, ArrayBuilder *builder) {
int32_t out;
RETURN_AVRO_ERROR(avro_value_get_int(&val, &out));
return static_cast<Int32Builder *>(builder)->Append(out);
}
Status read_int64(const avro_value_t val, ArrayBuilder *builder) {
int64_t out;
RETURN_AVRO_ERROR(avro_value_get_long(&val, &out));
return static_cast<Int64Builder *>(builder)->Append(out);
}
Status read_float64(const avro_value_t val, ArrayBuilder *builder) {
double out;
RETURN_AVRO_ERROR(avro_value_get_double(&val, &out));
return static_cast<DoubleBuilder *>(builder)->Append(out);
}
Status read_float32(avro_value_t val, ArrayBuilder *builder) {
float out;
RETURN_AVRO_ERROR(avro_value_get_float(&val, &out));
return static_cast<FloatBuilder *>(builder)->Append(out);
}
Status read_bool(const avro_value_t val, ArrayBuilder *builder) {
int temp;
RETURN_AVRO_ERROR(avro_value_get_boolean(&val, &temp));
auto out = (bool) temp;
return static_cast<BooleanBuilder *>(builder)->Append(out);
}
Status read_array(const avro_value_t val, ArrayBuilder *builder) {
size_t array_size;
size_t i;
const char *map_key = NULL;
avro_value_t child;
// ArrayBuilder *child_builder;//
RETURN_AVRO_ERROR(avro_value_get_size(&val, &array_size));
static_cast<ListBuilder *>(builder)->Append(true);
auto child_builder = static_cast<ListBuilder *>(builder)->value_builder();
for (i = 0; i < array_size; i++) {
RETURN_AVRO_ERROR(avro_value_get_by_index(&val, i, &child, &map_key));
RETURN_NOT_OK(generic_read(child, child_builder));
}
return Status::OK();
};
Status read_map(const avro_value_t val, ArrayBuilder *builder) {
size_t num_values, i;
auto listB = static_cast<ListBuilder *>(builder);
auto structB = static_cast<StructBuilder *>(listB->child(0));
auto keyBuilder = static_cast<StringBuilder *>(structB->child(0));
auto valueBuilder = structB->child(1);
avro_value_t child;
const char *map_key;
avro_value_get_size(&val, &num_values);
listB->Append(true);
for (i = 0; i < num_values; i++) {
structB->Append(true);
RETURN_AVRO_ERROR(avro_value_get_by_index(&val, i, &child, &map_key));
RETURN_NOT_OK(keyBuilder->Append(map_key));
RETURN_NOT_OK(generic_read(child, valueBuilder));
}
return Status::OK();
}
Status read_record(const avro_value_t val, ArrayBuilder *builder) {
avro_value_t child;
ArrayBuilder *child_builder;
StructBuilder *typed_builder;
Status result;
int container_length = builder->num_children();
typed_builder = static_cast<StructBuilder *>(builder);
RETURN_NOT_OK(typed_builder->Append());
for (auto i = 0; i < container_length; i++) {
avro_value_get_by_index(&val, i, &child, NULL);
child_builder = typed_builder->child(i);
RETURN_NOT_OK(generic_read(child, child_builder));
}
return Status::OK();
}
Status generic_read(const avro_value_t val, ArrayBuilder *builder) {
// Generic avro type read dispatcher. Dispatches to the various specializations by AVRO_TYPE.
// This is used by the various readers for complex types"""
auto avro_type = avro_value_get_type(&val);
switch (avro_type) {
case AVRO_BOOLEAN:
return read_bool(val, builder);
case AVRO_INT32:
return read_int32(val, builder);
case AVRO_INT64:
return read_int64(val, builder);
case AVRO_FLOAT:
return read_float32(val, builder);
case AVRO_DOUBLE:
return read_float64(val, builder);
case AVRO_STRING:
return read_string(val, builder);
case AVRO_BYTES:
return read_binary(val, builder);
case AVRO_FIXED:
return read_fixed(val, builder);
case AVRO_MAP:
return read_map(val, builder);
case AVRO_RECORD:
return read_record(val, builder);
case AVRO_ARRAY:
return read_array(val, builder);
default:
std::stringstream ss;
ss << "Unhandled avro type: " << avro_type;
return Status::NotImplemented(ss.str());
}
}
Status read_record(const avro_value_t val, RecordBatchBuilder *builder) {
avro_value_t child;
int container_length = builder->num_fields();
for (auto i = 0; i < container_length; i++) {
avro_value_get_by_index(&val, i, &child, NULL);
RETURN_NOT_OK(generic_read(child, builder->GetField(i)));
}
return Status::OK();
}