wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020352941


##########
test/arrow_test.cc:
##########
@@ -59,4 +67,192 @@ TEST(ArrowCDataTest, CheckArrowSchemaAndArrayByNanoarrow) {
   EXPECT_EQ(name_column->GetScalar(2).ValueOrDie()->ToString(), "c");
 }
 
+struct ToArrowSchemaParam {
+  std::shared_ptr<Type> iceberg_type;
+  bool optional = true;
+  std::shared_ptr<arrow::DataType> arrow_type;
+};
+
+class ToArrowSchemaTest : public ::testing::TestWithParam<ToArrowSchemaParam> 
{};
+
+TEST_P(ToArrowSchemaTest, PrimitiveType) {
+  constexpr std::string_view kFieldName = "foo";
+  constexpr int32_t kFieldId = 1024;
+  const auto& param = GetParam();
+  Schema schema(
+      /*schema_id=*/0,
+      {param.optional ? SchemaField::MakeOptional(kFieldId, 
std::string(kFieldName),
+                                                  param.iceberg_type)
+                      : SchemaField::MakeRequired(kFieldId, 
std::string(kFieldName),
+                                                  param.iceberg_type)});
+  ArrowSchema arrow_schema;
+  ASSERT_OK(ToArrowSchema(schema, &arrow_schema));
+
+  auto imported_schema = ::arrow::ImportSchema(&arrow_schema).ValueOrDie();
+  ASSERT_EQ(imported_schema->num_fields(), 1);
+
+  auto field = imported_schema->field(0);
+  ASSERT_EQ(field->name(), kFieldName);
+  ASSERT_EQ(field->nullable(), param.optional);
+  ASSERT_TRUE(field->type()->Equals(param.arrow_type));
+
+  auto metadata = field->metadata();
+  ASSERT_TRUE(metadata->Contains(kFieldIdKey));
+  ASSERT_EQ(metadata->Get(kFieldIdKey), std::to_string(kFieldId));
+}
+
+INSTANTIATE_TEST_SUITE_P(
+    SchemaConversion, ToArrowSchemaTest,
+    ::testing::Values(
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<BooleanType>(),
+                           .optional = false,
+                           .arrow_type = ::arrow::boolean()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<IntType>(),
+                           .optional = true,
+                           .arrow_type = ::arrow::int32()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<LongType>(),
+                           .arrow_type = ::arrow::int64()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<FloatType>(),
+                           .arrow_type = ::arrow::float32()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<DoubleType>(),
+                           .arrow_type = ::arrow::float64()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<DecimalType>(10, 
2),
+                           .arrow_type = ::arrow::decimal128(10, 2)},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<DateType>(),
+                           .arrow_type = ::arrow::date32()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<TimeType>(),
+                           .arrow_type = 
::arrow::time64(arrow::TimeUnit::MICRO)},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<TimestampType>(),
+                           .arrow_type = 
::arrow::timestamp(arrow::TimeUnit::MICRO)},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<TimestampType>(),
+                           .arrow_type = 
::arrow::timestamp(arrow::TimeUnit::MICRO)},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<StringType>(),
+                           .arrow_type = ::arrow::utf8()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<BinaryType>(),
+                           .arrow_type = ::arrow::binary()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<UuidType>(),
+                           .arrow_type = ::arrow::extension::uuid()},
+        ToArrowSchemaParam{.iceberg_type = std::make_shared<FixedType>(20),
+                           .arrow_type = ::arrow::fixed_size_binary(20)}));
+
+namespace {
+
+void CheckArrowField(const ::arrow::Field& field, ::arrow::Type::type type_id,
+                     std::string_view name, bool nullable, int32_t field_id) {
+  ASSERT_EQ(field.name(), name);
+  ASSERT_EQ(field.nullable(), nullable);
+  ASSERT_EQ(field.type()->id(), type_id);
+
+  auto metadata = field.metadata();
+  ASSERT_TRUE(metadata != nullptr);
+  ASSERT_TRUE(metadata->Contains(kFieldIdKey));
+  ASSERT_EQ(metadata->Get(kFieldIdKey), std::to_string(field_id));
+}
+
+}  // namespace
+
+TEST(ToArrowSchemaTest, StructType) {
+  constexpr int32_t kStructFieldId = 1;
+  constexpr int32_t kIntFieldId = 2;
+  constexpr int32_t kStrFieldId = 3;
+
+  constexpr std::string_view kStructFieldName = "struct_field";
+  constexpr std::string_view kIntFieldName = "int_field";
+  constexpr std::string_view kStrFieldName = "str_field";
+
+  auto struct_type = std::make_shared<StructType>(std::vector<SchemaField>{
+      SchemaField::MakeRequired(kIntFieldId, std::string(kIntFieldName),
+                                std::make_shared<IntType>()),
+      SchemaField::MakeOptional(kStrFieldId, std::string(kStrFieldName),
+                                std::make_shared<StringType>())});
+  Schema schema(
+      /*schema_id=*/0, {SchemaField::MakeRequired(
+                           kStructFieldId, std::string(kStructFieldName), 
struct_type)});
+
+  ArrowSchema arrow_schema;
+  ASSERT_OK(ToArrowSchema(schema, &arrow_schema));
+
+  auto imported_schema = ::arrow::ImportSchema(&arrow_schema).ValueOrDie();
+  ASSERT_EQ(imported_schema->num_fields(), 1);
+
+  auto field = imported_schema->field(0);
+  CheckArrowField(*field, ::arrow::Type::STRUCT, kStructFieldName, 
/*nullable=*/false,

Review Comment:
   You're right. I forgot to add them.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to