|
| 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/shredding/map_shared_shredding_schema_utils.h" |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "arrow/type.h" |
| 22 | +#include "arrow/util/key_value_metadata.h" |
| 23 | +#include "gtest/gtest.h" |
| 24 | +#include "paimon/common/data/shredding/map_shared_shredding_utils.h" |
| 25 | +#include "paimon/testing/utils/testharness.h" |
| 26 | + |
| 27 | +namespace paimon::test { |
| 28 | + |
| 29 | +TEST(MapSharedShreddingSchemaUtilsTest, AttachMetadataToSchemaBasic) { |
| 30 | + MapSharedShreddingFieldMeta tags_meta; |
| 31 | + tags_meta.name_to_id = {{"host", 0}, {"region", 1}}; |
| 32 | + tags_meta.field_to_columns = {{0, {0}}, {1, {1}}}; |
| 33 | + tags_meta.num_columns = 2; |
| 34 | + tags_meta.max_row_width = 2; |
| 35 | + |
| 36 | + auto id_metadata = std::make_shared<arrow::KeyValueMetadata>(); |
| 37 | + id_metadata->Append("paimon.field.id", "1"); |
| 38 | + auto schema_metadata = std::make_shared<arrow::KeyValueMetadata>(); |
| 39 | + schema_metadata->Append("schema.key", "schema.value"); |
| 40 | + auto schema = arrow::schema( |
| 41 | + {arrow::field("id", arrow::int32(), true, id_metadata), |
| 42 | + arrow::field("tags", arrow::struct_({arrow::field("__field_mapping", |
| 43 | + arrow::list(arrow::int32()), true), |
| 44 | + arrow::field("__col_0", arrow::utf8(), true), |
| 45 | + arrow::field("__col_1", arrow::utf8(), true)}))}, |
| 46 | + schema_metadata); |
| 47 | + |
| 48 | + ASSERT_OK_AND_ASSIGN(auto updated_schema, MapSharedShreddingSchemaUtils::AttachMetadataToSchema( |
| 49 | + schema, {{"tags", tags_meta}}, "none")); |
| 50 | + |
| 51 | + ASSERT_TRUE(updated_schema->metadata()->Equals(*schema_metadata)); |
| 52 | + ASSERT_TRUE(updated_schema->field(0)->metadata()->Equals(*id_metadata)); |
| 53 | + |
| 54 | + auto tags_metadata = updated_schema->GetFieldByName("tags")->metadata()->Copy(); |
| 55 | + ASSERT_TRUE(MapSharedShreddingUtils::HasShreddingMetadata(tags_metadata)); |
| 56 | + ASSERT_OK_AND_ASSIGN(auto deserialized, |
| 57 | + MapSharedShreddingUtils::DeserializeMetadata(tags_metadata, "none")); |
| 58 | + ASSERT_EQ(deserialized, tags_meta); |
| 59 | +} |
| 60 | + |
| 61 | +TEST(MapSharedShreddingSchemaUtilsTest, AttachMetadataToSchemaMissingField) { |
| 62 | + MapSharedShreddingFieldMeta tags_meta; |
| 63 | + tags_meta.name_to_id = {{"host", 0}}; |
| 64 | + tags_meta.field_to_columns = {{0, {0}}}; |
| 65 | + tags_meta.num_columns = 1; |
| 66 | + tags_meta.max_row_width = 1; |
| 67 | + |
| 68 | + auto schema = arrow::schema({arrow::field("id", arrow::int32())}); |
| 69 | + |
| 70 | + ASSERT_NOK_WITH_MSG(MapSharedShreddingSchemaUtils::AttachMetadataToSchema( |
| 71 | + schema, {{"tags", tags_meta}}, "none"), |
| 72 | + "Shared-shredding field 'tags' not found in physical schema."); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(MapSharedShreddingSchemaUtilsTest, AttachMetadataToSchemaPreservesExistingFieldMetadata) { |
| 76 | + MapSharedShreddingFieldMeta tags_meta; |
| 77 | + tags_meta.name_to_id = {{"host", 0}}; |
| 78 | + tags_meta.field_to_columns = {{0, {0}}}; |
| 79 | + tags_meta.num_columns = 1; |
| 80 | + tags_meta.max_row_width = 1; |
| 81 | + |
| 82 | + auto tags_metadata = std::make_shared<arrow::KeyValueMetadata>(); |
| 83 | + tags_metadata->Append("paimon.field.id", "7"); |
| 84 | + tags_metadata->Append("description", "original tags field"); |
| 85 | + auto schema = arrow::schema({arrow::field( |
| 86 | + "tags", |
| 87 | + arrow::struct_({arrow::field("__field_mapping", arrow::list(arrow::int32()), true), |
| 88 | + arrow::field("__col_0", arrow::utf8(), true)}), |
| 89 | + true, tags_metadata)}); |
| 90 | + |
| 91 | + ASSERT_OK_AND_ASSIGN(auto updated_schema, MapSharedShreddingSchemaUtils::AttachMetadataToSchema( |
| 92 | + schema, {{"tags", tags_meta}}, "none")); |
| 93 | + |
| 94 | + auto updated_metadata = updated_schema->field(0)->metadata()->Copy(); |
| 95 | + ASSERT_EQ(updated_metadata->value(updated_metadata->FindKey("paimon.field.id")), "7"); |
| 96 | + ASSERT_EQ(updated_metadata->value(updated_metadata->FindKey("description")), |
| 97 | + "original tags field"); |
| 98 | + ASSERT_TRUE(MapSharedShreddingUtils::HasShreddingMetadata(updated_metadata)); |
| 99 | +} |
| 100 | + |
| 101 | +TEST(MapSharedShreddingSchemaUtilsTest, ExtractMetadataFromFieldBasic) { |
| 102 | + MapSharedShreddingFieldMeta tags_meta; |
| 103 | + tags_meta.name_to_id = {{"host", 0}, {"region", 1}}; |
| 104 | + tags_meta.field_to_columns = {{0, {0}}, {1, {1}}}; |
| 105 | + tags_meta.num_columns = 2; |
| 106 | + tags_meta.max_row_width = 2; |
| 107 | + |
| 108 | + auto metadata = std::make_shared<arrow::KeyValueMetadata>(); |
| 109 | + ASSERT_OK(MapSharedShreddingUtils::SerializeMetadata(tags_meta, "none", metadata.get())); |
| 110 | + auto field = arrow::field( |
| 111 | + "tags", |
| 112 | + arrow::struct_({arrow::field("__field_mapping", arrow::list(arrow::int32()), true), |
| 113 | + arrow::field("__col_0", arrow::utf8(), true), |
| 114 | + arrow::field("__col_1", arrow::utf8(), true)}), |
| 115 | + true, metadata); |
| 116 | + |
| 117 | + ASSERT_OK_AND_ASSIGN(auto parsed_meta, |
| 118 | + MapSharedShreddingSchemaUtils::ExtractMetadataFromField(field, "none")); |
| 119 | + ASSERT_EQ(parsed_meta, tags_meta); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace paimon::test |
0 commit comments