HuaHuaY commented on code in PR #50252:
URL: https://github.com/apache/arrow/pull/50252#discussion_r4281552314


##########
cpp/src/parquet/variant/builder.h:
##########
@@ -0,0 +1,331 @@
+// 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
+
+#include <cstdint>
+#include <memory>
+#include <string_view>
+#include <vector>
+
+#include "arrow/buffer.h"
+#include "arrow/extension/parquet_variant.h"
+#include "arrow/memory_pool.h"
+#include "arrow/type_fwd.h"
+#include "parquet/platform.h"
+
+namespace parquet::variant {
+
+using ::arrow::Array;
+using ::arrow::BinaryViewArray;
+using ::arrow::Buffer;
+using ::arrow::DataType;
+using ::arrow::MemoryPool;
+using ::arrow::StructArray;
+using ::arrow::extension::VariantArray;
+
+class VariantMetadataView;
+class VariantObjectBuilder;
+class VariantListBuilder;
+class VariantValueRowBuilder;
+
+struct PARQUET_EXPORT EncodedVariantValue {
+  std::shared_ptr<Buffer> metadata;
+  std::shared_ptr<Buffer> value;
+};
+
+class PARQUET_EXPORT VariantBuilder {
+ public:
+  explicit VariantBuilder(MemoryPool* pool = ::arrow::default_memory_pool(),
+                          bool validate = true);
+  ~VariantBuilder();
+  VariantBuilder(const VariantBuilder&) = delete;
+  VariantBuilder& operator=(const VariantBuilder&) = delete;
+  VariantBuilder(VariantBuilder&&) noexcept;
+  VariantBuilder& operator=(VariantBuilder&&) noexcept;
+
+  void ReserveFieldNames(int64_t capacity);
+  uint32_t AddFieldName(std::string_view name);
+
+  void AppendVariantNull();

Review Comment:
   After two days of experimenting with refactoring, I’ve concluded that 
`VariantBuilder` should internally own its metadata builder.
   
   Otherwise, handling reset and object reuse after a `Finish` operation 
becomes difficult. If `VariantBuilder` were to merely borrow a read-only 
metadata instance, it would be forced to throw an error whenever it encounters 
a new key in an object. Conversely, if it were to borrow a writable metadata 
instance, any failure during the final `finish` step would necessitate a highly 
complex rollback process for the metadata. 
   That said, I will expose the `metadata builder`—this way, users who need to 
construct metadata before building `Variant` values ​​can utilize 
`VariantValueRowBuilder`, or we can provide other, more convenient interfaces 
in the future.



##########
cpp/src/parquet/variant/builder.h:
##########
@@ -0,0 +1,331 @@
+// 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
+
+#include <cstdint>
+#include <memory>
+#include <string_view>
+#include <vector>
+
+#include "arrow/buffer.h"
+#include "arrow/extension/parquet_variant.h"
+#include "arrow/memory_pool.h"
+#include "arrow/type_fwd.h"
+#include "parquet/platform.h"
+
+namespace parquet::variant {
+
+using ::arrow::Array;
+using ::arrow::BinaryViewArray;
+using ::arrow::Buffer;
+using ::arrow::DataType;
+using ::arrow::MemoryPool;
+using ::arrow::StructArray;
+using ::arrow::extension::VariantArray;
+
+class VariantMetadataView;
+class VariantObjectBuilder;
+class VariantListBuilder;
+class VariantValueRowBuilder;
+
+struct PARQUET_EXPORT EncodedVariantValue {
+  std::shared_ptr<Buffer> metadata;
+  std::shared_ptr<Buffer> value;
+};
+
+class PARQUET_EXPORT VariantBuilder {
+ public:
+  explicit VariantBuilder(MemoryPool* pool = ::arrow::default_memory_pool(),
+                          bool validate = true);
+  ~VariantBuilder();
+  VariantBuilder(const VariantBuilder&) = delete;
+  VariantBuilder& operator=(const VariantBuilder&) = delete;
+  VariantBuilder(VariantBuilder&&) noexcept;
+  VariantBuilder& operator=(VariantBuilder&&) noexcept;
+
+  void ReserveFieldNames(int64_t capacity);
+  uint32_t AddFieldName(std::string_view name);
+
+  void AppendVariantNull();

Review Comment:
   After two days of experimenting with refactoring, I’ve concluded that 
`VariantBuilder` should internally own its metadata builder.
   
   Otherwise, handling reset and object reuse after a `Finish` operation 
becomes difficult. If `VariantBuilder` were to merely borrow a read-only 
metadata instance, it would be forced to throw an error whenever it encounters 
a new key in an object. Conversely, if it were to borrow a writable metadata 
instance, any failure during the final `finish` step would necessitate a highly 
complex rollback process for the metadata. 
   
   That said, I will expose the `metadata builder`—this way, users who need to 
construct metadata before building `Variant` values ​​can utilize 
`VariantValueRowBuilder`, or we can provide other, more convenient interfaces 
in the future.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to