|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/extension/uuid.h" |
| 19 | + |
| 20 | +#include "arrow/testing/matchers.h" |
| 21 | + |
| 22 | +#include "arrow/io/memory.h" |
| 23 | +#include "arrow/ipc/reader.h" |
| 24 | +#include "arrow/ipc/test_common.h" |
| 25 | +#include "arrow/testing/gtest_util.h" |
| 26 | +#include "arrow/util/key_value_metadata.h" |
| 27 | + |
| 28 | +#include "arrow/testing/extension_type.h" |
| 29 | + |
| 30 | +namespace arrow { |
| 31 | + |
| 32 | +using arrow::ipc::test::RoundtripBatch; |
| 33 | + |
| 34 | +TEST(TestUuuidExtensionType, ExtensionTypeTest) { |
| 35 | + auto type = uuid(); |
| 36 | + ASSERT_EQ(type->id(), Type::EXTENSION); |
| 37 | + |
| 38 | + const auto& ext_type = static_cast<const ExtensionType&>(*type); |
| 39 | + std::string serialized = ext_type.Serialize(); |
| 40 | + |
| 41 | + ASSERT_OK_AND_ASSIGN(auto deserialized, |
| 42 | + ext_type.Deserialize(fixed_size_binary(16), serialized)); |
| 43 | + ASSERT_TRUE(deserialized->Equals(*type)); |
| 44 | + ASSERT_FALSE(deserialized->Equals(*fixed_size_binary(16))); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(TestUuuidExtensionType, RoundtripBatch) { |
| 48 | + auto ext_type = extension::uuid(); |
| 49 | + auto exact_ext_type = internal::checked_pointer_cast<extension::UuidType>(ext_type); |
| 50 | + auto arr = ArrayFromJSON(fixed_size_binary(16), R"(["abcdefghijklmnop", null])"); |
| 51 | + auto ext_arr = ExtensionType::WrapArray(ext_type, arr); |
| 52 | + |
| 53 | + // Pass extension array, expect getting back extension array |
| 54 | + std::shared_ptr<RecordBatch> read_batch; |
| 55 | + auto ext_field = field(/*name=*/"f0", /*type=*/ext_type); |
| 56 | + auto batch = RecordBatch::Make(schema({ext_field}), ext_arr->length(), {ext_arr}); |
| 57 | + RoundtripBatch(batch, &read_batch); |
| 58 | + CompareBatch(*batch, *read_batch, /*compare_metadata=*/true); |
| 59 | + |
| 60 | + // Pass extension metadata and storage array, expect getting back extension array |
| 61 | + std::shared_ptr<RecordBatch> read_batch2; |
| 62 | + auto ext_metadata = |
| 63 | + key_value_metadata({{"ARROW:extension:name", exact_ext_type->extension_name()}, |
| 64 | + {"ARROW:extension:metadata", ""}}); |
| 65 | + ext_field = field(/*name=*/"f0", /*type=*/exact_ext_type->storage_type(), |
| 66 | + /*nullable=*/true, /*metadata=*/ext_metadata); |
| 67 | + auto batch2 = RecordBatch::Make(schema({ext_field}), arr->length(), {arr}); |
| 68 | + RoundtripBatch(batch2, &read_batch2); |
| 69 | + CompareBatch(*batch, *read_batch2, /*compare_metadata=*/true); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace arrow |
0 commit comments