|
| 1 | +/* |
| 2 | + * Copyright 2026-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/common/utils/arrow/arrow_utils.h" |
| 18 | +namespace paimon { |
| 19 | +Result<std::shared_ptr<arrow::Schema>> ArrowUtils::DataTypeToSchema( |
| 20 | + const std::shared_ptr<arrow::DataType>& data_type) { |
| 21 | + if (data_type->id() != arrow::Type::STRUCT) { |
| 22 | + return Status::Invalid( |
| 23 | + fmt::format("Expected struct data type, actual data type: {}", data_type->ToString())); |
| 24 | + } |
| 25 | + const auto& struct_type = std::static_pointer_cast<arrow::StructType>(data_type); |
| 26 | + return std::make_shared<arrow::Schema>(struct_type->fields()); |
| 27 | +} |
| 28 | + |
| 29 | +Result<std::vector<int32_t>> ArrowUtils::CreateProjection( |
| 30 | + const std::shared_ptr<arrow::Schema>& file_schema, const arrow::FieldVector& read_fields) { |
| 31 | + std::vector<int32_t> target_to_src_mapping; |
| 32 | + target_to_src_mapping.reserve(read_fields.size()); |
| 33 | + for (const auto& field : read_fields) { |
| 34 | + auto src_field_idx = file_schema->GetFieldIndex(field->name()); |
| 35 | + if (src_field_idx < 0) { |
| 36 | + return Status::Invalid( |
| 37 | + fmt::format("Field '{}' not found or duplicate in file schema", field->name())); |
| 38 | + } |
| 39 | + target_to_src_mapping.push_back(src_field_idx); |
| 40 | + } |
| 41 | + return target_to_src_mapping; |
| 42 | +} |
| 43 | + |
| 44 | +Status ArrowUtils::CheckNullabilityMatch(const std::shared_ptr<arrow::Schema>& schema, |
| 45 | + const std::shared_ptr<arrow::Array>& data) { |
| 46 | + auto struct_array = arrow::internal::checked_pointer_cast<arrow::StructArray>(data); |
| 47 | + if (struct_array->num_fields() != schema->num_fields()) { |
| 48 | + return Status::Invalid(fmt::format( |
| 49 | + "CheckNullabilityMatch failed, data field count {} mismatch schema field count {}", |
| 50 | + struct_array->num_fields(), schema->num_fields())); |
| 51 | + } |
| 52 | + for (int32_t i = 0; i < schema->num_fields(); i++) { |
| 53 | + PAIMON_RETURN_NOT_OK(InnerCheckNullabilityMatch(schema->field(i), struct_array->field(i))); |
| 54 | + } |
| 55 | + return Status::OK(); |
| 56 | +} |
| 57 | + |
| 58 | +void ArrowUtils::TraverseArray(const std::shared_ptr<arrow::Array>& array) { |
| 59 | + arrow::Type::type type = array->type()->id(); |
| 60 | + switch (type) { |
| 61 | + case arrow::Type::type::DICTIONARY: { |
| 62 | + auto* dict_array = arrow::internal::checked_cast<arrow::DictionaryArray*>(array.get()); |
| 63 | + [[maybe_unused]] auto dict = dict_array->dictionary(); |
| 64 | + return; |
| 65 | + } |
| 66 | + case arrow::Type::type::STRUCT: { |
| 67 | + auto* struct_array = arrow::internal::checked_cast<arrow::StructArray*>(array.get()); |
| 68 | + for (const auto& field : struct_array->fields()) { |
| 69 | + TraverseArray(field); |
| 70 | + } |
| 71 | + return; |
| 72 | + } |
| 73 | + case arrow::Type::type::MAP: { |
| 74 | + auto* map_array = arrow::internal::checked_cast<arrow::MapArray*>(array.get()); |
| 75 | + TraverseArray(map_array->keys()); |
| 76 | + TraverseArray(map_array->items()); |
| 77 | + return; |
| 78 | + } |
| 79 | + case arrow::Type::type::LIST: { |
| 80 | + auto* list_array = arrow::internal::checked_cast<arrow::ListArray*>(array.get()); |
| 81 | + TraverseArray(list_array->values()); |
| 82 | + return; |
| 83 | + } |
| 84 | + default: |
| 85 | + return; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +Status ArrowUtils::InnerCheckNullabilityMatch(const std::shared_ptr<arrow::Field>& field, |
| 90 | + const std::shared_ptr<arrow::Array>& data) { |
| 91 | + if (PAIMON_UNLIKELY(!field->nullable() && data->null_count() != 0)) { |
| 92 | + return Status::Invalid(fmt::format( |
| 93 | + "CheckNullabilityMatch failed, field {} not nullable while data have null value", |
| 94 | + field->name())); |
| 95 | + } |
| 96 | + auto type = field->type(); |
| 97 | + if (type->id() == arrow::Type::STRUCT) { |
| 98 | + auto struct_type = arrow::internal::checked_pointer_cast<arrow::StructType>(field->type()); |
| 99 | + auto struct_array = arrow::internal::checked_pointer_cast<arrow::StructArray>(data); |
| 100 | + for (int32_t i = 0; i < struct_type->num_fields(); ++i) { |
| 101 | + PAIMON_RETURN_NOT_OK( |
| 102 | + InnerCheckNullabilityMatch(struct_type->field(i), struct_array->field(i))); |
| 103 | + } |
| 104 | + } else if (type->id() == arrow::Type::LIST) { |
| 105 | + auto list_type = arrow::internal::checked_pointer_cast<arrow::ListType>(field->type()); |
| 106 | + auto list_array = arrow::internal::checked_pointer_cast<arrow::ListArray>(data); |
| 107 | + PAIMON_RETURN_NOT_OK( |
| 108 | + InnerCheckNullabilityMatch(list_type->value_field(), list_array->values())); |
| 109 | + } else if (type->id() == arrow::Type::MAP) { |
| 110 | + auto map_type = arrow::internal::checked_pointer_cast<arrow::MapType>(field->type()); |
| 111 | + auto map_array = arrow::internal::checked_pointer_cast<arrow::MapArray>(data); |
| 112 | + PAIMON_RETURN_NOT_OK(InnerCheckNullabilityMatch(map_type->key_field(), map_array->keys())); |
| 113 | + PAIMON_RETURN_NOT_OK( |
| 114 | + InnerCheckNullabilityMatch(map_type->item_field(), map_array->items())); |
| 115 | + } |
| 116 | + return Status::OK(); |
| 117 | +} |
| 118 | +} // namespace paimon |
0 commit comments