|
| 1 | +/* |
| 2 | + * Copyright 2024-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/format/avro/avro_adaptor.h" |
| 18 | + |
| 19 | +#include <cstddef> |
| 20 | +#include <utility> |
| 21 | + |
| 22 | +#include "arrow/api.h" |
| 23 | +#include "arrow/array/array_base.h" |
| 24 | +#include "arrow/array/array_binary.h" |
| 25 | +#include "arrow/array/array_nested.h" |
| 26 | +#include "arrow/array/array_primitive.h" |
| 27 | +#include "arrow/util/checked_cast.h" |
| 28 | +#include "avro/GenericDatum.hh" |
| 29 | +#include "avro/Node.hh" |
| 30 | +#include "avro/ValidSchema.hh" |
| 31 | +#include "paimon/status.h" |
| 32 | + |
| 33 | +namespace paimon::avro { |
| 34 | + |
| 35 | +Result<std::vector<::avro::GenericDatum>> AvroAdaptor::ConvertArrayToGenericDatums( |
| 36 | + const std::shared_ptr<arrow::Array>& array, const ::avro::ValidSchema& avro_schema) const { |
| 37 | + std::vector<::avro::GenericDatum> datums; |
| 38 | + for (int32_t i = 0; i < array->length(); i++) { |
| 39 | + PAIMON_ASSIGN_OR_RAISE(::avro::GenericDatum datum, |
| 40 | + ConvertArrayToGenericDatum(avro_schema, array, i)); |
| 41 | + datums.push_back(datum); |
| 42 | + } |
| 43 | + return datums; |
| 44 | +} |
| 45 | + |
| 46 | +Result<::avro::GenericDatum> AvroAdaptor::ConvertArrayToGenericDatum( |
| 47 | + const ::avro::ValidSchema& avro_schema, const std::shared_ptr<arrow::Array>& arrow_array, |
| 48 | + int32_t row_idx) const { |
| 49 | + std::shared_ptr<arrow::StructArray> struct_array = |
| 50 | + arrow::internal::checked_pointer_cast<arrow::StructArray>(arrow_array); |
| 51 | + if (struct_array == nullptr) { |
| 52 | + return Status::Invalid("arrow array should be struct array"); |
| 53 | + } |
| 54 | + ::avro::GenericDatum datum(avro_schema.root()); |
| 55 | + auto& record = datum.value<::avro::GenericRecord>(); |
| 56 | + const auto& node = avro_schema.root(); |
| 57 | + const auto& fields = type_->fields(); |
| 58 | + for (size_t i = 0; i < fields.size(); i++) { |
| 59 | + std::shared_ptr<arrow::Array> field_array = struct_array->field(i); |
| 60 | + switch (fields[i]->type()->id()) { |
| 61 | + case arrow::Type::type::BOOL: { |
| 62 | + auto array = |
| 63 | + arrow::internal::checked_pointer_cast<arrow::BooleanArray>(field_array); |
| 64 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 65 | + SetValue<bool, arrow::BooleanArray>(row_idx, array, &datum); |
| 66 | + record.setFieldAt(i, datum); |
| 67 | + break; |
| 68 | + } |
| 69 | + case arrow::Type::type::INT8: { |
| 70 | + auto array = arrow::internal::checked_pointer_cast<arrow::Int8Array>(field_array); |
| 71 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 72 | + // avro only support int32_t and int64_t |
| 73 | + SetValue<int32_t, arrow::Int8Array>(row_idx, array, &datum); |
| 74 | + record.setFieldAt(i, datum); |
| 75 | + break; |
| 76 | + } |
| 77 | + case arrow::Type::type::INT16: { |
| 78 | + auto array = arrow::internal::checked_pointer_cast<arrow::Int16Array>(field_array); |
| 79 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 80 | + // avro only support int32_t and int64_t |
| 81 | + SetValue<int32_t, arrow::Int16Array>(row_idx, array, &datum); |
| 82 | + record.setFieldAt(i, datum); |
| 83 | + break; |
| 84 | + } |
| 85 | + case arrow::Type::type::INT32: { |
| 86 | + auto array = arrow::internal::checked_pointer_cast<arrow::Int32Array>(field_array); |
| 87 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 88 | + SetValue<int32_t, arrow::Int32Array>(row_idx, array, &datum); |
| 89 | + record.setFieldAt(i, datum); |
| 90 | + break; |
| 91 | + } |
| 92 | + case arrow::Type::type::INT64: { |
| 93 | + auto array = arrow::internal::checked_pointer_cast<arrow::Int64Array>(field_array); |
| 94 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 95 | + SetValue<int64_t, arrow::Int64Array>(row_idx, array, &datum); |
| 96 | + record.setFieldAt(i, datum); |
| 97 | + break; |
| 98 | + } |
| 99 | + case arrow::Type::type::FLOAT: { |
| 100 | + auto array = arrow::internal::checked_pointer_cast<arrow::FloatArray>(field_array); |
| 101 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 102 | + SetValue<float, arrow::FloatArray>(row_idx, array, &datum); |
| 103 | + record.setFieldAt(i, datum); |
| 104 | + break; |
| 105 | + } |
| 106 | + case arrow::Type::type::DOUBLE: { |
| 107 | + auto array = arrow::internal::checked_pointer_cast<arrow::DoubleArray>(field_array); |
| 108 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 109 | + SetValue<double, arrow::DoubleArray>(row_idx, array, &datum); |
| 110 | + record.setFieldAt(i, datum); |
| 111 | + break; |
| 112 | + } |
| 113 | + case arrow::Type::type::STRING: { |
| 114 | + auto array = arrow::internal::checked_pointer_cast<arrow::StringArray>(field_array); |
| 115 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 116 | + SetValue<std::string, arrow::StringArray>(row_idx, array, &datum); |
| 117 | + record.setFieldAt(i, datum); |
| 118 | + break; |
| 119 | + } |
| 120 | + case arrow::Type::type::BINARY: { |
| 121 | + auto array = arrow::internal::checked_pointer_cast<arrow::BinaryArray>(field_array); |
| 122 | + auto datum = ::avro::GenericDatum(avro_schema.root()->leafAt(i)); |
| 123 | + SetValue<std::vector<uint8_t>, arrow::BinaryArray>(row_idx, array, &datum); |
| 124 | + record.setFieldAt(i, datum); |
| 125 | + break; |
| 126 | + } |
| 127 | + case arrow::Type::type::STRUCT: { |
| 128 | + ::avro::ValidSchema leaf_schema(node->leafAt(i)); |
| 129 | + PAIMON_ASSIGN_OR_RAISE( |
| 130 | + ::avro::GenericDatum datum, |
| 131 | + ConvertArrayToGenericDatum(leaf_schema, field_array, row_idx)); |
| 132 | + record.setFieldAt(i, datum); |
| 133 | + break; |
| 134 | + } |
| 135 | + default: { |
| 136 | + return Status::Invalid("unsupported type"); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + return datum; |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace paimon::avro |
0 commit comments