forked from alibaba/paimon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavro_direct_encoder.cpp
More file actions
375 lines (323 loc) · 16.3 KB
/
Copy pathavro_direct_encoder.cpp
File metadata and controls
375 lines (323 loc) · 16.3 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// Adapted from Apache Iceberg C++
// https://github.com/apache/iceberg-cpp/blob/main/src/iceberg/avro/avro_direct_encoder.cc
#include "paimon/format/avro/avro_direct_encoder.h"
#include <algorithm>
#include <cstring>
#include "arrow/api.h"
#include "arrow/type.h"
#include "arrow/util/checked_cast.h"
#include "fmt/format.h"
#include "paimon/common/utils/date_time_utils.h"
#include "paimon/format/avro/avro_utils.h"
#include "paimon/result.h"
namespace paimon::avro {
namespace {
// Utility struct for union branch information
struct UnionBranches {
size_t null_index;
size_t value_index;
::avro::NodePtr value_node;
};
Result<UnionBranches> ValidateUnion(const ::avro::NodePtr& union_node) {
if (PAIMON_UNLIKELY(union_node->leaves() != 2)) {
return Status::Invalid(
fmt::format("Union must have exactly 2 branches, got {}", union_node->leaves()));
}
const auto& branch_0 = union_node->leafAt(0);
const auto& branch_1 = union_node->leafAt(1);
if (branch_0->type() == ::avro::AVRO_NULL && branch_1->type() != ::avro::AVRO_NULL) {
return UnionBranches{.null_index = 0, .value_index = 1, .value_node = branch_1};
}
if (branch_1->type() == ::avro::AVRO_NULL && branch_0->type() != ::avro::AVRO_NULL) {
return Status::Invalid(
"Unexpected: In paimon, we expect the null branch to be the first branch in a union.");
}
return Status::Invalid("Union must have exactly one null branch");
}
} // namespace
Status AvroDirectEncoder::EncodeArrowToAvro(const ::avro::NodePtr& avro_node,
const arrow::Array& array, int64_t row_index,
::avro::Encoder* encoder, EncodeContext* ctx) {
if (PAIMON_UNLIKELY(row_index < 0 || row_index >= array.length())) {
return Status::Invalid(
fmt::format("Row index {} out of bounds {}", row_index, array.length()));
}
const bool is_null = array.IsNull(row_index);
if (avro_node->type() == ::avro::AVRO_UNION) {
PAIMON_ASSIGN_OR_RAISE(UnionBranches branches, ValidateUnion(avro_node));
if (is_null) {
encoder->encodeUnionIndex(branches.null_index);
encoder->encodeNull();
return Status::OK();
}
encoder->encodeUnionIndex(branches.value_index);
return EncodeArrowToAvro(branches.value_node, array, row_index, encoder, ctx);
}
if (is_null) {
return Status::Invalid("Null value in non-nullable field");
}
switch (avro_node->type()) {
case ::avro::AVRO_BOOL: {
const auto& bool_array =
arrow::internal::checked_cast<const arrow::BooleanArray&>(array);
encoder->encodeBool(bool_array.Value(row_index));
return Status::OK();
}
case ::avro::AVRO_INT: {
// AVRO_INT can represent: int32, date (days since epoch)
switch (array.type()->id()) {
case arrow::Type::INT8: {
const auto& int8_array =
arrow::internal::checked_cast<const arrow::Int8Array&>(array);
encoder->encodeInt(int8_array.Value(row_index));
return Status::OK();
}
case arrow::Type::INT16: {
const auto& int16_array =
arrow::internal::checked_cast<const arrow::Int16Array&>(array);
encoder->encodeInt(int16_array.Value(row_index));
return Status::OK();
}
case arrow::Type::INT32: {
const auto& int32_array =
arrow::internal::checked_cast<const arrow::Int32Array&>(array);
encoder->encodeInt(int32_array.Value(row_index));
return Status::OK();
}
case arrow::Type::DATE32: {
const auto& date_array =
arrow::internal::checked_cast<const arrow::Date32Array&>(array);
encoder->encodeInt(date_array.Value(row_index));
return Status::OK();
}
default:
return Status::Invalid(
fmt::format("AVRO_INT expects Int32Array or Date32Array, got {}",
array.type()->ToString()));
}
}
case ::avro::AVRO_LONG: {
// AVRO_LONG can represent: int64, timestamp
switch (array.type()->id()) {
case arrow::Type::INT64: {
const auto& int64_array =
arrow::internal::checked_cast<const arrow::Int64Array&>(array);
encoder->encodeLong(int64_array.Value(row_index));
return Status::OK();
}
case arrow::Type::TIMESTAMP: {
const auto& timestamp_array =
arrow::internal::checked_cast<const arrow::TimestampArray&>(array);
int64_t timestamp = timestamp_array.Value(row_index);
auto ts_type =
arrow::internal::checked_pointer_cast<arrow::TimestampType>(array.type());
arrow::TimeUnit::type unit = ts_type->unit();
const auto& logical_type = avro_node->logicalType().type();
// NOTE: Java Avro only support TIMESTAMP_MILLIS && TIMESTAMP_MICROS
if (((logical_type == ::avro::LogicalType::TIMESTAMP_MILLIS ||
logical_type == ::avro::LogicalType::LOCAL_TIMESTAMP_MILLIS) &&
unit == arrow::TimeUnit::MILLI) ||
((logical_type == ::avro::LogicalType::TIMESTAMP_MICROS ||
logical_type == ::avro::LogicalType::LOCAL_TIMESTAMP_MICROS) &&
unit == arrow::TimeUnit::MICRO) ||
((logical_type == ::avro::LogicalType::TIMESTAMP_NANOS ||
logical_type == ::avro::LogicalType::LOCAL_TIMESTAMP_NANOS) &&
unit == arrow::TimeUnit::NANO)) {
encoder->encodeLong(timestamp);
} else if ((logical_type == ::avro::LogicalType::TIMESTAMP_MILLIS ||
logical_type == ::avro::LogicalType::LOCAL_TIMESTAMP_MILLIS) &&
unit == arrow::TimeUnit::SECOND) {
// for arrow second, we need to convert it to avro millisecond
encoder->encodeLong(
timestamp *
DateTimeUtils::CONVERSION_FACTORS[DateTimeUtils::MILLISECOND]);
} else {
return Status::Invalid(
fmt::format("Unsupported timestamp type with avro logical type {} and "
"arrow time unit {}.",
AvroUtils::ToString(avro_node->logicalType()),
DateTimeUtils::GetArrowTimeUnitStr(unit)));
}
return Status::OK();
}
default:
return Status::Invalid(
fmt::format("AVRO_LONG expects Int64Array, or TimestampArray, got {}",
array.type()->ToString()));
}
}
case ::avro::AVRO_FLOAT: {
const auto& float_array =
arrow::internal::checked_cast<const arrow::FloatArray&>(array);
encoder->encodeFloat(float_array.Value(row_index));
return Status::OK();
}
case ::avro::AVRO_DOUBLE: {
const auto& double_array =
arrow::internal::checked_cast<const arrow::DoubleArray&>(array);
encoder->encodeDouble(double_array.Value(row_index));
return Status::OK();
}
case ::avro::AVRO_STRING: {
const auto& string_array =
arrow::internal::checked_cast<const arrow::StringArray&>(array);
std::string_view value = string_array.GetView(row_index);
encoder->encodeString(std::string(value));
return Status::OK();
}
case ::avro::AVRO_BYTES: {
// Handle DECIMAL
if (avro_node->logicalType().type() == ::avro::LogicalType::DECIMAL) {
const auto& decimal_array =
arrow::internal::checked_cast<const arrow::Decimal128Array&>(array);
std::string_view decimal_value = decimal_array.GetView(row_index);
ctx->assign(decimal_value.begin(), decimal_value.end());
// Arrow Decimal128 bytes are in little-endian order, Avro requires big-endian
std::reverse(ctx->begin(), ctx->end());
encoder->encodeBytes(ctx->data(), ctx->size());
return Status::OK();
}
// Handle regular BYTES
const auto& binary_array =
arrow::internal::checked_cast<const arrow::BinaryArray&>(array);
std::string_view value = binary_array.GetView(row_index);
encoder->encodeBytes(reinterpret_cast<const uint8_t*>(value.data()), value.size());
return Status::OK();
}
case ::avro::AVRO_RECORD: {
if (PAIMON_UNLIKELY(array.type()->id() != arrow::Type::STRUCT)) {
return Status::Invalid(fmt::format("AVRO_RECORD expects StructArray, got {}",
array.type()->ToString()));
}
const auto& struct_array =
arrow::internal::checked_cast<const arrow::StructArray&>(array);
const size_t num_fields = avro_node->leaves();
if (PAIMON_UNLIKELY(struct_array.num_fields() != static_cast<int>(num_fields))) {
return Status::Invalid(fmt::format(
"Field count mismatch: Arrow struct has {} fields, Avro node has {} fields",
struct_array.num_fields(), num_fields));
}
for (size_t i = 0; i < num_fields; ++i) {
const auto& field_node = avro_node->leafAt(i);
const auto& field_array = struct_array.field(static_cast<int>(i));
PAIMON_RETURN_NOT_OK(
EncodeArrowToAvro(field_node, *field_array, row_index, encoder, ctx));
}
return Status::OK();
}
case ::avro::AVRO_ARRAY: {
const auto& element_node = avro_node->leafAt(0);
// Handle ListArray
if (array.type()->id() == arrow::Type::LIST) {
const auto& list_array =
arrow::internal::checked_cast<const arrow::ListArray&>(array);
const auto start = list_array.value_offset(row_index);
const auto end = list_array.value_offset(row_index + 1);
const auto length = end - start;
encoder->arrayStart();
if (length > 0) {
encoder->setItemCount(length);
const auto& values = list_array.values();
for (int64_t i = start; i < end; ++i) {
encoder->startItem();
PAIMON_RETURN_NOT_OK(
EncodeArrowToAvro(element_node, *values, i, encoder, ctx));
}
}
encoder->arrayEnd();
return Status::OK();
} else if (array.type()->id() == arrow::Type::MAP &&
AvroUtils::HasMapLogicalType(avro_node)) {
// Handle MapArray (for Avro maps with non-string keys)
if (PAIMON_UNLIKELY(element_node->type() != ::avro::AVRO_RECORD ||
element_node->leaves() != 2)) {
return Status::Invalid(
fmt::format("Expected AVRO_RECORD for map key-value pair, got {}",
AvroUtils::ToString(element_node)));
}
const auto& map_array =
arrow::internal::checked_cast<const arrow::MapArray&>(array);
const auto start = map_array.value_offset(row_index);
const auto end = map_array.value_offset(row_index + 1);
const auto length = end - start;
encoder->arrayStart();
if (length > 0) {
encoder->setItemCount(length);
const auto& keys = map_array.keys();
const auto& values = map_array.items();
// The element_node should be a RECORD with "key" and "value" fields
for (int64_t i = start; i < end; ++i) {
const auto& key_node = element_node->leafAt(0);
const auto& value_node = element_node->leafAt(1);
encoder->startItem();
PAIMON_RETURN_NOT_OK(EncodeArrowToAvro(key_node, *keys, i, encoder, ctx));
PAIMON_RETURN_NOT_OK(
EncodeArrowToAvro(value_node, *values, i, encoder, ctx));
}
}
encoder->arrayEnd();
return Status::OK();
}
return Status::Invalid(fmt::format(
"AVRO_ARRAY must map to ListArray or MapArray, got {}", array.type()->ToString()));
}
case ::avro::AVRO_MAP: {
if (PAIMON_UNLIKELY(array.type()->id() != arrow::Type::MAP)) {
return Status::Invalid(
fmt::format("AVRO_MAP expects MapArray, got {}", array.type()->ToString()));
}
const auto& map_array = arrow::internal::checked_cast<const arrow::MapArray&>(array);
const auto start = map_array.value_offset(row_index);
const auto end = map_array.value_offset(row_index + 1);
const auto length = end - start;
encoder->mapStart();
if (length > 0) {
encoder->setItemCount(length);
const auto& keys = map_array.keys();
const auto& values = map_array.items();
const auto& value_node = avro_node->leafAt(1);
if (PAIMON_UNLIKELY(keys->type()->id() != arrow::Type::STRING)) {
return Status::Invalid(fmt::format("AVRO_MAP keys must be StringArray, got {}",
keys->type()->ToString()));
}
for (int64_t i = start; i < end; ++i) {
encoder->startItem();
const auto& string_array =
arrow::internal::checked_cast<const arrow::StringArray&>(*keys);
std::string_view key_value = string_array.GetView(i);
encoder->encodeString(std::string(key_value));
PAIMON_RETURN_NOT_OK(EncodeArrowToAvro(value_node, *values, i, encoder, ctx));
}
}
encoder->mapEnd();
return Status::OK();
}
case ::avro::AVRO_NULL:
case ::avro::AVRO_UNION:
// Already handled above
return Status::Invalid(fmt::format("Unexpected Avro type handling: {}",
::avro::toString(avro_node->type())));
default:
return Status::Invalid(
fmt::format("Unsupported Avro type: {}", ::avro::toString(avro_node->type())));
}
}
} // namespace paimon::avro