You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "wgtmac (via GitHub)" <gi...@apache.org> on 2025/03/28 07:17:50 UTC

[PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

wgtmac opened a new pull request, #53:
URL: https://github.com/apache/iceberg-cpp/pull/53

   (no comment)


-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020725819


##########
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 <optional>
+#include <string>
+
+#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,

Review Comment:
   That makes sense. Actually there are in different namespaces (the former one is in the anonymous namespace). I have renamed them to `ToArrowSchema` to be consistent.
   
   W.r.t. the order of arguments, those with default values must be placed at the end. Since they are all internal functions, I have just removed any default argument and put the out param to the end.



##########
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 <optional>
+#include <string>
+
+#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,

Review Comment:
   That makes sense. Actually they are in different namespaces (the former one is in the anonymous namespace). I have renamed them to `ToArrowSchema` to be consistent.
   
   W.r.t. the order of arguments, those with default values must be placed at the end. Since they are all internal functions, I have just removed any default argument and put the out param to the end.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020353890


##########
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:
   I've switched to use std::optional<int32_t> for now.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "zhjwpku (via GitHub)" <gi...@apache.org>.
zhjwpku commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2018520396


##########
src/iceberg/arrow_c_data.h:
##########
@@ -66,6 +71,47 @@ struct ArrowArray {
   void* private_data;
 };
 
-}  // extern "C"
-
 #endif  // ARROW_C_DATA_INTERFACE
+
+#ifndef ARROW_C_STREAM_INTERFACE
+#  define ARROW_C_STREAM_INTERFACE
+
+struct ArrowArrayStream {

Review Comment:
   Should we mention [0] in the \file description?
   
   I have a feeling that this is not related to PR $titile, so maybe another PR for this?
   
   [0] https://arrow.apache.org/docs/format/CStreamInterface.html



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020682771


##########
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 <optional>
+#include <string>
+
+#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,

Review Comment:
   `ConvertToArrowSchema` and `ToArrowSchema` are a bit confusing to me.  Do we need to make the API naming more clear or split them in different namespaces?
   
   ---
   
   By the way, do we have a policy for our API args' order?
   
   `input_field_a, out, input_field_b, input_field_c` seems not good to me and hard to follow.
   
   Is it widely adopted in cpp community?



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo merged PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53


-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2018863030


##########
src/iceberg/arrow_c_data.h:
##########
@@ -66,6 +71,47 @@ struct ArrowArray {
   void* private_data;
 };
 
-}  // extern "C"
-
 #endif  // ARROW_C_DATA_INTERFACE
+
+#ifndef ARROW_C_STREAM_INTERFACE
+#  define ARROW_C_STREAM_INTERFACE
+
+struct ArrowArrayStream {

Review Comment:
   I have to add this, otherwise compiling files with headers from `nanoarrow` will complain missing definition of `ArrowArrayStream`.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "zhjwpku (via GitHub)" <gi...@apache.org>.
zhjwpku commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2019624696


##########
src/iceberg/arrow_c_data.h:
##########
@@ -66,6 +71,47 @@ struct ArrowArray {
   void* private_data;
 };
 
-}  // extern "C"
-
 #endif  // ARROW_C_DATA_INTERFACE
+
+#ifndef ARROW_C_STREAM_INTERFACE
+#  define ARROW_C_STREAM_INTERFACE
+
+struct ArrowArrayStream {

Review Comment:
   OK, then the patch LGTM.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020725819


##########
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 <optional>
+#include <string>
+
+#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,

Review Comment:
   That makes sense. I have renamed them to `ToArrowSchema` to be consistent.
   
   W.r.t. the order of arguments, those with default values must be placed at the end. Since they are all internal functions, I have just removed any default argument and put the out param to the end.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#issuecomment-2760522323

   @lidavidm @zhjwpku PTAL :)


-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020353401


##########
src/iceberg/arrow_c_data.h:
##########
@@ -66,6 +71,47 @@ struct ArrowArray {
   void* private_data;
 };
 
-}  // extern "C"
-
 #endif  // ARROW_C_DATA_INTERFACE
+
+#ifndef ARROW_C_STREAM_INTERFACE
+#  define ARROW_C_STREAM_INTERFACE
+
+struct ArrowArrayStream {

Review Comment:
   I have removed ArrowArrayStream. I think internally we can just include `nanoarrow.h` so we don't have to add ArrowArrayStream and confuse the downstream.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#issuecomment-2766339693

   Thank you @Xuanwo!


-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020353646


##########
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:
   Good suggestion! Added `matchers.h` for this.



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2018858127


##########
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:
   Good question! In practice, parquet-cpp rejects negative `field_id`: https://github.com/apache/arrow/blob/618ef501a21375abfaeee19e393eb64dee83ef0d/cpp/src/parquet/arrow/schema.cc#L248-L278
   
   @Fokko @rdblue Has this been discussed before? Should we restrict `field_id` to be non-negative from the spec? 



-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#issuecomment-2764970288

   Thanks @lidavidm @zhjwpku! I believe I have addressed all the comments now.


-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#issuecomment-2765292992

   @Fokko @Xuanwo Could you help review and merge it? Thanks!


-- 
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-unsubscribe@iceberg.apache.org

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


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


Re: [PR] feat: convert iceberg schema to arrow schema [iceberg-cpp]

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #53:
URL: https://github.com/apache/iceberg-cpp/pull/53#discussion_r2020048862


##########
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:
   Hmm, should field ID 0 be valid then though?



-- 
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-unsubscribe@iceberg.apache.org

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


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