This is an automated email from the ASF dual-hosted git repository.

kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new ea0e701bb9 GH-46146: [C++] Merge metadata in 
SchemaBuidler::AddMetadata (#46654)
ea0e701bb9 is described below

commit ea0e701bb932cb37a2f3bd43abb32225f5fc1370
Author: Ziy <[email protected]>
AuthorDate: Mon Jun 2 09:48:27 2025 +0800

    GH-46146: [C++] Merge metadata in SchemaBuidler::AddMetadata (#46654)
    
    ### Rationale for this change
    
    `arrow::SchemaBuidler::AddMetadata()` replaces metadata not adds metadata.
    
    ### What changes are included in this PR?
    
    Add metadata when calling `SchemaBuidler::AddMetadata()`.
    
    ### Are these changes tested?
    
    Manually build pass.
    
    ### Are there any user-facing changes?
    
    **This PR includes breaking changes to public APIs.**
    
    `arrow::SchemaBuidler::AddMetadata()` adds not replaces the given metadata. 
If an existing code calls `AddMetadata()` multiple times, the behavior will be 
changed.
    
    * GitHub Issue: #46146
    
    Authored-by: Ziy1-Tan <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 cpp/src/arrow/type.cc      |  3 ++-
 cpp/src/arrow/type_test.cc | 16 +++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/cpp/src/arrow/type.cc b/cpp/src/arrow/type.cc
index bd14e878d9..221071b4f0 100644
--- a/cpp/src/arrow/type.cc
+++ b/cpp/src/arrow/type.cc
@@ -2641,7 +2641,8 @@ Status SchemaBuilder::AddSchemas(const 
std::vector<std::shared_ptr<Schema>>& sch
 }
 
 Status SchemaBuilder::AddMetadata(const KeyValueMetadata& metadata) {
-  impl_->metadata_ = metadata.Copy();
+  impl_->metadata_ =
+      impl_->metadata_ ? impl_->metadata_->Merge(metadata) : metadata.Copy();
   return Status::OK();
 }
 
diff --git a/cpp/src/arrow/type_test.cc b/cpp/src/arrow/type_test.cc
index 7610be8a47..cb17f1ac3f 100644
--- a/cpp/src/arrow/type_test.cc
+++ b/cpp/src/arrow/type_test.cc
@@ -746,25 +746,31 @@ TEST(TestSchemaBuilder, WithMetadata) {
   auto f0 = field("f0", int32());
   auto f1 = field("f1", uint8(), false);
   auto metadata = key_value_metadata({{"foo", "bar"}});
+  auto metadata2 = key_value_metadata({{"foo2", "bar2"}});
+  auto merged_metadata = metadata->Merge(*metadata2);
 
   SchemaBuilder builder;
   ASSERT_OK(builder.AddMetadata(*metadata));
   ASSERT_OK_AND_ASSIGN(auto schema, builder.Finish());
   AssertSchemaEqual(schema, ::arrow::schema({})->WithMetadata(metadata));
 
+  ASSERT_OK(builder.AddMetadata(*metadata2));
+  ASSERT_OK_AND_ASSIGN(schema, builder.Finish());
+  AssertSchemaEqual(schema, 
::arrow::schema({})->WithMetadata(merged_metadata));
+
   ASSERT_OK(builder.AddField(f0));
   ASSERT_OK_AND_ASSIGN(schema, builder.Finish());
-  AssertSchemaEqual(schema, ::arrow::schema({f0})->WithMetadata(metadata));
+  AssertSchemaEqual(schema, 
::arrow::schema({f0})->WithMetadata(merged_metadata));
 
-  SchemaBuilder other_builder{::arrow::schema({})->WithMetadata(metadata)};
+  SchemaBuilder 
other_builder{::arrow::schema({})->WithMetadata(merged_metadata)};
   ASSERT_OK(other_builder.AddField(f1));
   ASSERT_OK_AND_ASSIGN(schema, other_builder.Finish());
-  AssertSchemaEqual(schema, ::arrow::schema({f1})->WithMetadata(metadata));
+  AssertSchemaEqual(schema, 
::arrow::schema({f1})->WithMetadata(merged_metadata));
 
   other_builder.Reset();
-  ASSERT_OK(other_builder.AddField(f1->WithMetadata(metadata)));
+  ASSERT_OK(other_builder.AddField(f1->WithMetadata(merged_metadata)));
   ASSERT_OK_AND_ASSIGN(schema, other_builder.Finish());
-  AssertSchemaEqual(schema, ::arrow::schema({f1->WithMetadata(metadata)}));
+  AssertSchemaEqual(schema, 
::arrow::schema({f1->WithMetadata(merged_metadata)}));
 }
 
 TEST(TestSchemaBuilder, IncrementalConstruction) {

Reply via email to