From f780522dd1f456af5631199a1001144148d05a9e Mon Sep 17 00:00:00 2001 From: David Li Date: Fri, 28 Jun 2024 02:33:21 -0400 Subject: [PATCH] Finish up C++ tests --- cpp/src/arrow/extension/opaque.cc | 2 +- cpp/src/arrow/extension/opaque_test.cc | 48 +++++++++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/cpp/src/arrow/extension/opaque.cc b/cpp/src/arrow/extension/opaque.cc index face065a6396f..3d9a46b4c8cab 100644 --- a/cpp/src/arrow/extension/opaque.cc +++ b/cpp/src/arrow/extension/opaque.cc @@ -95,7 +95,7 @@ Result> OpaqueType::Deserialize( std::shared_ptr OpaqueType::MakeArray(std::shared_ptr data) const { DCHECK_EQ(data->type->id(), Type::EXTENSION); - DCHECK_EQ("arrow.unknown", + DCHECK_EQ("arrow.opaque", internal::checked_cast(*data->type).extension_name()); return std::make_shared(data); } diff --git a/cpp/src/arrow/extension/opaque_test.cc b/cpp/src/arrow/extension/opaque_test.cc index dcfc37b8da32a..719ff8880349b 100644 --- a/cpp/src/arrow/extension/opaque_test.cc +++ b/cpp/src/arrow/extension/opaque_test.cc @@ -20,18 +20,17 @@ #include "arrow/extension/opaque.h" #include "arrow/extension_type.h" +#include "arrow/io/memory.h" +#include "arrow/ipc/reader.h" +#include "arrow/ipc/writer.h" +#include "arrow/record_batch.h" +#include "arrow/testing/extension_type.h" #include "arrow/testing/gtest_util.h" #include "arrow/type_fwd.h" #include "arrow/util/checked_cast.h" namespace arrow { -TEST(OpaqueType, Registered) { - // We need a registered dummy type at runtime to allow for IPC deserialization - auto registered_type = GetExtensionType("arrow.opaque"); - ASSERT_EQ(Type::EXTENSION, registered_type->type_id); -} - TEST(OpaqueType, Basics) { auto type = internal::checked_pointer_cast( extension::opaque(null(), "type", "vendor")); @@ -88,7 +87,14 @@ TEST(OpaqueType, Equals) { ASSERT_NE(*type4, *type3); } -TEST(OpaqueType, CreateFromArray) {} +TEST(OpaqueType, CreateFromArray) { + auto type = internal::checked_pointer_cast( + extension::opaque(binary(), "geometry", "adbc.postgresql")); + auto storage = ArrayFromJSON(binary(), R"(["foobar", null])"); + auto array = ExtensionType::WrapArray(type, storage); + ASSERT_EQ(2, array->length()); + ASSERT_EQ(1, array->null_count()); +} void CheckDeserialize(const std::string& serialized, const std::shared_ptr& expected) { @@ -152,6 +158,32 @@ TEST(OpaqueType, MetadataRoundTrip) { } } -TEST(OpaqueType, BatchRoundTrip) {} +TEST(OpaqueType, BatchRoundTrip) { + auto type = internal::checked_pointer_cast( + extension::opaque(binary(), "geometry", "adbc.postgresql")); + ExtensionTypeGuard guard(type); + + auto storage = ArrayFromJSON(binary(), R"(["foobar", null])"); + auto array = ExtensionType::WrapArray(type, storage); + auto batch = + RecordBatch::Make(schema({field("field", type)}), array->length(), {array}); + + std::shared_ptr written; + { + 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 batch_reader; + ASSERT_OK_AND_ASSIGN(batch_reader, ipc::RecordBatchStreamReader::Open(&reader)); + ASSERT_OK(batch_reader->ReadNext(&written)); + } + + ASSERT_EQ(*batch->schema(), *written->schema()); + ASSERT_BATCHES_EQUAL(*batch, *written); +} } // namespace arrow