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


##########
test/test_util.h:
##########
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#define EXPECT_OK(value) \

Review Comment:
   Note that you can write a GMock matcher using one of the macros GMock 
provides, then you can do something like `ASSERT_THAT(Foo(), IsOk())`. A macro 
might be simpler, though.



##########
src/iceberg/schema_internal.cc:
##########
@@ -0,0 +1,193 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/schema_internal.h"
+
+#include <format>
+#include <string>
+
+#include <nanoarrow/nanoarrow.h>
+
+#include "iceberg/expected.h"
+#include "iceberg/schema.h"
+
+namespace iceberg {
+
+namespace {
+
+constexpr const char* kArrowExtensionName = "ARROW:extension:name";
+constexpr const char* kArrowExtensionMetadata = "ARROW:extension:metadata";
+
+// Convert an Iceberg type to Arrow schema. Return value is Nanoarrow error 
code.
+ArrowErrorCode ConvertToArrowSchema(const Type& type, ArrowSchema* schema, 
bool optional,
+                                    std::string_view name = "", int32_t 
field_id = -1) {
+  ArrowBuffer metadata_buffer;
+  NANOARROW_RETURN_NOT_OK(ArrowMetadataBuilderInit(&metadata_buffer, nullptr));
+  if (field_id > 0) {

Review Comment:
   BTW, where in the Iceberg spec does it say that field IDs must be nonzero? 
(I don't even see a bit width defined for field ID in the spec...)
   
   `optional` might be a more idiomatic way to write the type but this works 
(if we can find a reference for the field ID type)



##########
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:
   technically you should `ASSERT_NO_FATAL_FAILURE(CheckArrowField(...))`



-- 
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