|
| 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/data/shredding/map_shared_shredding_schema_utils.h" |
| 18 | + |
| 19 | +#include "arrow/c/bridge.h" |
| 20 | +#include "arrow/type.h" |
| 21 | +#include "arrow/util/key_value_metadata.h" |
| 22 | +#include "fmt/format.h" |
| 23 | +#include "paimon/common/data/shredding/map_shared_shredding_utils.h" |
| 24 | +#include "paimon/common/utils/arrow/status_utils.h" |
| 25 | + |
| 26 | +namespace paimon { |
| 27 | + |
| 28 | +Result<std::unique_ptr<::ArrowSchema>> MapSharedShreddingSchemaUtils::LogicalToPhysicalSchema( |
| 29 | + std::unique_ptr<::ArrowSchema> logical_schema, |
| 30 | + const std::map<std::string, int32_t>& field_to_num_columns) { |
| 31 | + if (!logical_schema) { |
| 32 | + return Status::Invalid("logical schema is null"); |
| 33 | + } |
| 34 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Schema> arrow_logical_schema, |
| 35 | + arrow::ImportSchema(logical_schema.get())); |
| 36 | + // Type guard: verify all fields in field_to_num_columns are actually MAP type |
| 37 | + for (const auto& [field_name, num_columns] : field_to_num_columns) { |
| 38 | + std::shared_ptr<arrow::Field> field = arrow_logical_schema->GetFieldByName(field_name); |
| 39 | + if (!field) { |
| 40 | + return Status::Invalid( |
| 41 | + fmt::format("Field '{}' not found in logical schema.", field_name)); |
| 42 | + } |
| 43 | + if (field->type()->id() != arrow::Type::MAP) { |
| 44 | + return Status::Invalid( |
| 45 | + fmt::format("Field '{}' is expected to be MAP type, but got '{}'.", field_name, |
| 46 | + field->type()->name())); |
| 47 | + } |
| 48 | + } |
| 49 | + std::shared_ptr<arrow::Schema> physical_schema = |
| 50 | + MapSharedShreddingUtils::LogicalToPhysicalSchema(arrow_logical_schema, |
| 51 | + field_to_num_columns); |
| 52 | + auto c_schema = std::make_unique<::ArrowSchema>(); |
| 53 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*physical_schema, c_schema.get())); |
| 54 | + return c_schema; |
| 55 | +} |
| 56 | + |
| 57 | +Result<std::unique_ptr<::ArrowSchema>> MapSharedShreddingSchemaUtils::AttachMetadataToSchema( |
| 58 | + std::unique_ptr<::ArrowSchema> physical_schema, |
| 59 | + const std::map<std::string, MapSharedShreddingFieldMeta>& field_name_to_meta, |
| 60 | + const std::string& compression) { |
| 61 | + if (!physical_schema) { |
| 62 | + return Status::Invalid("physical schema is null"); |
| 63 | + } |
| 64 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Schema> arrow_physical_schema, |
| 65 | + arrow::ImportSchema(physical_schema.get())); |
| 66 | + |
| 67 | + arrow::FieldVector updated_fields = arrow_physical_schema->fields(); |
| 68 | + for (const auto& [field_name, field_meta] : field_name_to_meta) { |
| 69 | + int32_t field_index = arrow_physical_schema->GetFieldIndex(field_name); |
| 70 | + if (field_index < 0) { |
| 71 | + return Status::Invalid(fmt::format( |
| 72 | + "Shared-shredding field '{}' not found in physical schema.", field_name)); |
| 73 | + } |
| 74 | + |
| 75 | + const auto& field = arrow_physical_schema->field(field_index); |
| 76 | + auto metadata = field->metadata() ? field->metadata()->Copy() |
| 77 | + : std::make_shared<arrow::KeyValueMetadata>(); |
| 78 | + PAIMON_RETURN_NOT_OK( |
| 79 | + MapSharedShreddingUtils::SerializeMetadata(field_meta, compression, metadata.get())); |
| 80 | + updated_fields[field_index] = field->WithMetadata(metadata); |
| 81 | + } |
| 82 | + |
| 83 | + auto updated_schema = |
| 84 | + arrow::schema(std::move(updated_fields), arrow_physical_schema->metadata()); |
| 85 | + auto c_schema = std::make_unique<::ArrowSchema>(); |
| 86 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*updated_schema, c_schema.get())); |
| 87 | + return c_schema; |
| 88 | +} |
| 89 | + |
| 90 | +Result<MapSharedShreddingFieldMeta> MapSharedShreddingSchemaUtils::ExtractMetadataFromField( |
| 91 | + std::unique_ptr<::ArrowSchema> physical_schema, const std::string& field_name, |
| 92 | + const std::string& compression) { |
| 93 | + if (!physical_schema) { |
| 94 | + return Status::Invalid("physical schema is null"); |
| 95 | + } |
| 96 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Schema> arrow_physical_schema, |
| 97 | + arrow::ImportSchema(physical_schema.get())); |
| 98 | + const auto& field = arrow_physical_schema->GetFieldByName(field_name); |
| 99 | + if (!field) { |
| 100 | + return Status::Invalid( |
| 101 | + fmt::format("Shared-shredding field '{}' not found in physical schema.", field_name)); |
| 102 | + } |
| 103 | + |
| 104 | + auto metadata = |
| 105 | + field->metadata() ? field->metadata()->Copy() : std::shared_ptr<arrow::KeyValueMetadata>(); |
| 106 | + return MapSharedShreddingUtils::DeserializeMetadata(metadata, compression); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace paimon |
0 commit comments