|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <arrow/c/bridge.h> |
| 21 | +#include <arrow/json/from_string.h> |
| 22 | +#include <avro/Compiler.hh> |
| 23 | +#include <avro/Generic.hh> |
| 24 | +#include <avro/Node.hh> |
| 25 | +#include <avro/Types.hh> |
| 26 | +#include <gmock/gmock.h> |
| 27 | +#include <gtest/gtest.h> |
| 28 | +#include <iceberg/arrow/arrow_error_transform_internal.h> |
| 29 | +#include <iceberg/avro/avro_data_util_internal.h> |
| 30 | +#include <iceberg/avro/avro_schema_util_internal.h> |
| 31 | +#include <iceberg/schema.h> |
| 32 | +#include <iceberg/schema_internal.h> |
| 33 | +#include <iceberg/schema_util.h> |
| 34 | +#include <iceberg/type.h> |
| 35 | +#include <iceberg/util/macros.h> |
| 36 | + |
| 37 | +#include "matchers.h" |
| 38 | + |
| 39 | +namespace iceberg::avro { |
| 40 | + |
| 41 | +void VerifyAppendDatumToBuilder(const Schema& iceberg_schema, |
| 42 | + const ::avro::NodePtr& avro_node, |
| 43 | + const std::vector<::avro::GenericDatum>& avro_data, |
| 44 | + std::string_view expected_array_json) { |
| 45 | + // Create 1 to 1 projection |
| 46 | + auto projection_result = Project(iceberg_schema, avro_node, /*prune_source=*/false); |
| 47 | + ASSERT_THAT(projection_result, IsOk()); |
| 48 | + auto projection = std::move(projection_result.value()); |
| 49 | + |
| 50 | + // Create arrow schema and array builder |
| 51 | + ArrowSchema arrow_c_schema; |
| 52 | + ASSERT_THAT(ToArrowSchema(iceberg_schema, &arrow_c_schema), IsOk()); |
| 53 | + auto arrow_schema = ::arrow::ImportSchema(&arrow_c_schema).ValueOrDie(); |
| 54 | + auto arrow_struct_type = std::make_shared<::arrow::StructType>(arrow_schema->fields()); |
| 55 | + auto builder = ::arrow::MakeBuilder(arrow_struct_type).ValueOrDie(); |
| 56 | + |
| 57 | + // Call AppendDatumToBuilder repeatedly to append the datum |
| 58 | + for (const auto& avro_datum : avro_data) { |
| 59 | + ASSERT_THAT(AppendDatumToBuilder(avro_node, avro_datum, projection, iceberg_schema, |
| 60 | + builder.get()), |
| 61 | + IsOk()); |
| 62 | + } |
| 63 | + |
| 64 | + // Verify the result |
| 65 | + auto array = builder->Finish().ValueOrDie(); |
| 66 | + auto expected_array = |
| 67 | + ::arrow::json::ArrayFromJSONString(arrow_struct_type, expected_array_json) |
| 68 | + .ValueOrDie(); |
| 69 | + ASSERT_TRUE(array->Equals(*expected_array)); |
| 70 | +} |
| 71 | + |
| 72 | +TEST(AvroDataTest, AppendIntDatumToBuilder) { |
| 73 | + Schema iceberg_schema( |
| 74 | + {SchemaField::MakeRequired(/*field_id=*/1, "a", std::make_shared<IntType>())}); |
| 75 | + ::avro::NodePtr avro_node; |
| 76 | + EXPECT_THAT(ToAvroNodeVisitor{}.Visit(iceberg_schema, &avro_node), IsOk()); |
| 77 | + |
| 78 | + std::vector<::avro::GenericDatum> avro_data; |
| 79 | + for (int i = 0; i < 3; ++i) { |
| 80 | + ::avro::GenericDatum avro_datum(avro_node); |
| 81 | + avro_datum.value<::avro::GenericRecord>().fieldAt(0).value<int32_t>() = i; |
| 82 | + avro_data.push_back(avro_datum); |
| 83 | + } |
| 84 | + |
| 85 | + ASSERT_NO_FATAL_FAILURE(VerifyAppendDatumToBuilder( |
| 86 | + iceberg_schema, avro_node, avro_data, R"([{"a": 0}, {"a": 1}, {"a": 2}])")); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(AvroDataTest, AppendStringDatumToBuilder) { |
| 90 | + Schema iceberg_schema( |
| 91 | + {SchemaField::MakeRequired(/*field_id=*/1, "a", std::make_shared<StringType>())}); |
| 92 | + ::avro::NodePtr avro_node; |
| 93 | + EXPECT_THAT(ToAvroNodeVisitor{}.Visit(iceberg_schema, &avro_node), IsOk()); |
| 94 | + |
| 95 | + std::vector<::avro::GenericDatum> avro_data; |
| 96 | + for (int i = 0; i < 3; ++i) { |
| 97 | + ::avro::GenericDatum avro_datum(avro_node); |
| 98 | + avro_datum.value<::avro::GenericRecord>().fieldAt(0).value<std::string>() = |
| 99 | + "a" + std::to_string(i); |
| 100 | + avro_data.push_back(avro_datum); |
| 101 | + } |
| 102 | + |
| 103 | + ASSERT_NO_FATAL_FAILURE( |
| 104 | + VerifyAppendDatumToBuilder(iceberg_schema, avro_node, avro_data, |
| 105 | + R"([{"a": "a0"}, {"a": "a1"}, {"a": "a2"}])")); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace iceberg::avro |
0 commit comments