jorisvandenbossche commented on code in PR #8510: URL: https://github.com/apache/arrow/pull/8510#discussion_r1154511324
########## cpp/src/arrow/extension/fixed_shape_tensor_test.cc: ########## @@ -0,0 +1,220 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#ifdef ARROW_JSON +#include "arrow/extension/fixed_shape_tensor.h" + +#include "arrow/testing/matchers.h" + +#include "arrow/array/array_nested.h" +#include "arrow/array/array_primitive.h" +#include "arrow/io/memory.h" +#include "arrow/ipc/reader.h" +#include "arrow/ipc/writer.h" +#include "arrow/record_batch.h" +#include "arrow/tensor.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/util/key_value_metadata.h" + +namespace arrow { + +using FixedShapeTensorType = extension::FixedShapeTensorType; +using extension::fixed_shape_tensor; +using extension::FixedShapeTensorArray; + +class TestExtensionType : public ::testing::Test { + public: + void SetUp() override { + shape_ = {3, 3, 4}; + cell_shape_ = {3, 4}; + value_type_ = int64(); + cell_type_ = fixed_size_list(value_type_, 12); + dim_names_ = {"x", "y"}; + ext_type_ = internal::checked_pointer_cast<ExtensionType>( + fixed_shape_tensor(value_type_, cell_shape_, {}, dim_names_)); + values_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}; + values_partial_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; + shape_partial_ = {2, 3, 4}; + tensor_strides_ = {96, 32, 8}; + cell_strides_ = {32, 8}; + serialized_ = R"({"shape":[3,4],"dim_names":["x","y"]})"; + } + + protected: + std::vector<int64_t> shape_; + std::vector<int64_t> shape_partial_; + std::vector<int64_t> cell_shape_; + std::shared_ptr<DataType> value_type_; + std::shared_ptr<DataType> cell_type_; + std::vector<std::string> dim_names_; + std::shared_ptr<ExtensionType> ext_type_; + std::vector<int64_t> values_; + std::vector<int64_t> values_partial_; + std::vector<int64_t> tensor_strides_; + std::vector<int64_t> cell_strides_; + std::string serialized_; +}; + +auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch, + std::shared_ptr<RecordBatch>* out) { + ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create()); + ASSERT_OK(ipc::WriteRecordBatchStream({batch}, ipc::IpcWriteOptions::Defaults(), + out_stream.get())); + + ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish()); + + io::BufferReader reader(complete_ipc_stream); + std::shared_ptr<RecordBatchReader> batch_reader; + ASSERT_OK_AND_ASSIGN(batch_reader, ipc::RecordBatchStreamReader::Open(&reader)); + ASSERT_OK(batch_reader->ReadNext(out)); +}; + +TEST_F(TestExtensionType, CheckDummyRegistration) { + // We need a registered dummy type at runtime to allow for IPC deserialization + auto registered_type = GetExtensionType("arrow.fixed_shape_tensor"); + ASSERT_TRUE(registered_type->type_id == Type::EXTENSION); +} + +TEST_F(TestExtensionType, CreateExtensionType) { + auto exact_ext_type = internal::checked_pointer_cast<FixedShapeTensorType>(ext_type_); + + // Test ExtensionType methods + ASSERT_EQ(ext_type_->extension_name(), "arrow.fixed_shape_tensor"); + ASSERT_TRUE(ext_type_->Equals(*exact_ext_type)); + ASSERT_FALSE(ext_type_->Equals(*cell_type_)); + ASSERT_TRUE(ext_type_->storage_type()->Equals(*cell_type_)); + ASSERT_EQ(ext_type_->Serialize(), serialized_); + ASSERT_OK_AND_ASSIGN(auto ds, + ext_type_->Deserialize(ext_type_->storage_type(), serialized_)); + auto deserialized = std::reinterpret_pointer_cast<ExtensionType>(ds); + ASSERT_TRUE(deserialized->Equals(*ext_type_)); + + // Test FixedShapeTensorType methods + ASSERT_EQ(exact_ext_type->id(), Type::EXTENSION); + ASSERT_EQ(exact_ext_type->ndim(), cell_shape_.size()); + ASSERT_EQ(exact_ext_type->shape(), cell_shape_); + ASSERT_EQ(exact_ext_type->dim_names(), dim_names_); + + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, testing::HasSubstr("Invalid: permutation size must match shape size."), + FixedShapeTensorType::Make(value_type_, cell_shape_, {0})); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, testing::HasSubstr("Invalid: dim_names size must match shape size."), + FixedShapeTensorType::Make(value_type_, cell_shape_, {}, {"x"})); +} + +TEST_F(TestExtensionType, EqualsCases) { + auto ext_type_permutation_1 = fixed_shape_tensor(int64(), {3, 4}, {0, 1}, {"x", "y"}); + auto ext_type_permutation_2 = fixed_shape_tensor(int64(), {3, 4}, {1, 0}, {"x", "y"}); + auto ext_type_no_permutation = fixed_shape_tensor(int64(), {3, 4}, {}, {"x", "y"}); + + ASSERT_TRUE(ext_type_permutation_1->Equals(ext_type_permutation_1)); + + ASSERT_FALSE(fixed_shape_tensor(int32(), {3, 4}, {}, {"x", "y"}) + ->Equals(ext_type_no_permutation)); + ASSERT_FALSE(fixed_shape_tensor(int64(), {2, 4}, {}, {"x", "y"}) + ->Equals(ext_type_no_permutation)); + ASSERT_FALSE(fixed_shape_tensor(int64(), {3, 4}, {}, {"H", "W"}) + ->Equals(ext_type_no_permutation)); + + ASSERT_TRUE(ext_type_no_permutation->Equals(ext_type_permutation_1)); + ASSERT_TRUE(ext_type_permutation_1->Equals(ext_type_no_permutation)); + ASSERT_FALSE(ext_type_no_permutation->Equals(ext_type_permutation_2)); + ASSERT_FALSE(ext_type_permutation_2->Equals(ext_type_no_permutation)); + ASSERT_FALSE(ext_type_permutation_1->Equals(ext_type_permutation_2)); + ASSERT_FALSE(ext_type_permutation_2->Equals(ext_type_permutation_1)); +} + +TEST_F(TestExtensionType, CreateFromArray) { + auto exact_ext_type = internal::checked_pointer_cast<FixedShapeTensorType>(ext_type_); + + std::vector<std::shared_ptr<Buffer>> buffers = {nullptr, Buffer::Wrap(values_)}; + auto arr_data = std::make_shared<ArrayData>(value_type_, values_.size(), buffers, 0, 0); + auto arr = std::make_shared<Int64Array>(arr_data); + ASSERT_OK_AND_ASSIGN(auto fsla_arr, FixedSizeListArray::FromArrays(arr, cell_type_)); + auto data = fsla_arr->data(); + data->type = ext_type_; + auto ext_arr = exact_ext_type->MakeArray(data); + ASSERT_EQ(ext_arr->length(), shape_[0]); + ASSERT_EQ(ext_arr->null_count(), 0); +} + +void CheckSerializationRoundtrip(const std::shared_ptr<DataType>& ext_type) { + auto fst_type = internal::checked_pointer_cast<FixedShapeTensorType>(ext_type); + auto serialized = fst_type->Serialize(); + ASSERT_OK_AND_ASSIGN(auto deserialized, + fst_type->Deserialize(fst_type->storage_type(), serialized)); + ASSERT_TRUE(fst_type->Equals(*deserialized)); +} + +void CheckDeserializationRaises(const std::shared_ptr<DataType>& storage_type, + const std::string& serialized, + const std::string& expected_message) { + auto fst_type = internal::checked_pointer_cast<FixedShapeTensorType>( + fixed_shape_tensor(int64(), {3, 4})); + EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr(expected_message), + fst_type->Deserialize(storage_type, serialized)); +} + +TEST_F(TestExtensionType, MetadataSerializationRoundtrip) { + CheckSerializationRoundtrip(ext_type_); + CheckSerializationRoundtrip(fixed_shape_tensor(value_type_, {}, {}, {})); + CheckSerializationRoundtrip(fixed_shape_tensor(value_type_, {0}, {}, {})); + CheckSerializationRoundtrip(fixed_shape_tensor(value_type_, {1}, {0}, {"x"})); + CheckSerializationRoundtrip( + fixed_shape_tensor(value_type_, {256, 256, 3}, {0, 1, 2}, {"H", "W", "C"})); + CheckSerializationRoundtrip( + fixed_shape_tensor(value_type_, {256, 256, 3}, {2, 0, 1}, {"C", "H", "W"})); + + auto storage_type = fixed_size_list(int64(), 12); + CheckDeserializationRaises(boolean(), R"({"shape":[3,4]})", + "Expected FixedSizeList storage type, got bool"); + CheckDeserializationRaises(storage_type, R"({"dim_names":["x","y"]})", + "Invalid serialized JSON data"); + CheckDeserializationRaises(storage_type, R"({"shape":(3,4)})", + "Invalid serialized JSON data"); + CheckDeserializationRaises(storage_type, R"({"shape":[3,4],"permutation":[1,0,2]})", + "Invalid permutation"); + CheckDeserializationRaises(storage_type, R"({"shape":[3],"dim_names":["x","y"]})", + "Invalid dim_names"); +} + +TEST_F(TestExtensionType, RoudtripBatch) { + auto exact_ext_type = internal::checked_pointer_cast<FixedShapeTensorType>(ext_type_); + + std::vector<std::shared_ptr<Buffer>> buffers = {nullptr, Buffer::Wrap(values_)}; + auto arr_data = std::make_shared<ArrayData>(value_type_, values_.size(), buffers, 0, 0); + auto arr = std::make_shared<Int64Array>(arr_data); + ASSERT_OK_AND_ASSIGN(auto fsla_arr, FixedSizeListArray::FromArrays(arr, cell_type_)); + auto data = fsla_arr->data(); + data->type = ext_type_; + auto ext_arr = exact_ext_type->MakeArray(data); + + auto ext_metadata = + key_value_metadata({{"ARROW:extension:name", exact_ext_type->extension_name()}, + {"ARROW:extension:metadata", serialized_}}); + auto ext_field = field("f0", exact_ext_type, true, ext_metadata); + auto batch = RecordBatch::Make(schema({ext_field}), ext_arr->length(), {ext_arr}); Review Comment: And compare_metadata can be false then -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
