kszucs commented on a change in pull request #8088:
URL: https://github.com/apache/arrow/pull/8088#discussion_r492187679



##########
File path: cpp/src/arrow/util/converter.h
##########
@@ -0,0 +1,348 @@
+// 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 <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/array.h"
+#include "arrow/builder.h"
+#include "arrow/chunked_array.h"
+#include "arrow/status.h"
+#include "arrow/type.h"
+#include "arrow/type_traits.h"
+#include "arrow/util/checked_cast.h"
+
+#include "arrow/visitor_inline.h"
+
+namespace arrow {
+namespace internal {
+
+template <typename Input, typename Options, typename Self>
+class Converter {
+ public:
+  using InputType = Input;
+  using OptionsType = Options;
+
+  virtual ~Converter() = default;
+
+  virtual Status Initialize(std::shared_ptr<DataType> type,
+                            std::shared_ptr<ArrayBuilder> builder,
+                            const std::vector<std::shared_ptr<Self>>& children,
+                            OptionsType options) {
+    type_ = std::move(type);
+    builder_ = std::move(builder);
+    children_ = std::move(children);
+    options_ = std::move(options);
+    return Init();
+  }
+
+  virtual Status Init() { return Status::OK(); }
+
+  virtual Status Append(InputType value) {
+    return Status::NotImplemented("Converter not implemented for type ",
+                                  type()->ToString());
+  }
+
+  const std::shared_ptr<ArrayBuilder>& builder() const { return builder_; }
+
+  const std::shared_ptr<DataType>& type() const { return type_; }
+
+  OptionsType options() const { return options_; }
+
+  const std::vector<std::shared_ptr<Self>>& children() const { return 
children_; }
+
+  Status Reserve(int64_t additional_capacity) {
+    return builder_->Reserve(additional_capacity);
+  }
+
+  Status AppendNull() { return builder_->AppendNull(); }
+
+  virtual Result<std::shared_ptr<Array>> ToArray() { return 
builder_->Finish(); }
+
+  virtual Result<std::shared_ptr<Array>> ToArray(int64_t length) {
+    ARROW_ASSIGN_OR_RAISE(auto arr, this->ToArray());
+    return arr->Slice(0, length);
+  }
+
+ protected:
+  std::shared_ptr<DataType> type_;
+  std::shared_ptr<ArrayBuilder> builder_;
+  std::vector<std::shared_ptr<Self>> children_;
+  OptionsType options_;
+};
+
+template <typename T, typename BaseConverter>
+class PrimitiveConverter : public BaseConverter {
+ public:
+  using BuilderType = typename TypeTraits<T>::BuilderType;
+
+  Status Init() override {
+    primitive_type_ = checked_cast<const T*>(this->type_.get());
+    primitive_builder_ = checked_cast<BuilderType*>(this->builder_.get());
+    return Status::OK();
+  }
+
+ protected:
+  const T* primitive_type_;
+  BuilderType* primitive_builder_;
+};
+
+template <typename T, typename BaseConverter>
+class ListConverter : public BaseConverter {

Review comment:
       Updated.

##########
File path: cpp/src/arrow/util/converter.h
##########
@@ -0,0 +1,348 @@
+// 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 <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/array.h"
+#include "arrow/builder.h"
+#include "arrow/chunked_array.h"
+#include "arrow/status.h"
+#include "arrow/type.h"
+#include "arrow/type_traits.h"
+#include "arrow/util/checked_cast.h"
+
+#include "arrow/visitor_inline.h"
+
+namespace arrow {
+namespace internal {
+
+template <typename Input, typename Options, typename Self>
+class Converter {
+ public:
+  using InputType = Input;
+  using OptionsType = Options;
+
+  virtual ~Converter() = default;
+
+  virtual Status Initialize(std::shared_ptr<DataType> type,
+                            std::shared_ptr<ArrayBuilder> builder,
+                            const std::vector<std::shared_ptr<Self>>& children,
+                            OptionsType options) {
+    type_ = std::move(type);
+    builder_ = std::move(builder);
+    children_ = std::move(children);
+    options_ = std::move(options);
+    return Init();
+  }
+
+  virtual Status Init() { return Status::OK(); }
+
+  virtual Status Append(InputType value) {
+    return Status::NotImplemented("Converter not implemented for type ",
+                                  type()->ToString());
+  }
+
+  const std::shared_ptr<ArrayBuilder>& builder() const { return builder_; }
+
+  const std::shared_ptr<DataType>& type() const { return type_; }
+
+  OptionsType options() const { return options_; }
+
+  const std::vector<std::shared_ptr<Self>>& children() const { return 
children_; }
+
+  Status Reserve(int64_t additional_capacity) {
+    return builder_->Reserve(additional_capacity);
+  }
+
+  Status AppendNull() { return builder_->AppendNull(); }
+
+  virtual Result<std::shared_ptr<Array>> ToArray() { return 
builder_->Finish(); }
+
+  virtual Result<std::shared_ptr<Array>> ToArray(int64_t length) {
+    ARROW_ASSIGN_OR_RAISE(auto arr, this->ToArray());
+    return arr->Slice(0, length);
+  }
+
+ protected:
+  std::shared_ptr<DataType> type_;
+  std::shared_ptr<ArrayBuilder> builder_;
+  std::vector<std::shared_ptr<Self>> children_;
+  OptionsType options_;
+};
+
+template <typename T, typename BaseConverter>
+class PrimitiveConverter : public BaseConverter {
+ public:
+  using BuilderType = typename TypeTraits<T>::BuilderType;
+
+  Status Init() override {
+    primitive_type_ = checked_cast<const T*>(this->type_.get());
+    primitive_builder_ = checked_cast<BuilderType*>(this->builder_.get());
+    return Status::OK();
+  }
+
+ protected:
+  const T* primitive_type_;
+  BuilderType* primitive_builder_;
+};
+
+template <typename T, typename BaseConverter>
+class ListConverter : public BaseConverter {
+ public:
+  using BuilderType = typename TypeTraits<T>::BuilderType;
+
+  Status Init() override {
+    list_type_ = checked_cast<const T*>(this->type_.get());
+    list_builder_ = checked_cast<BuilderType*>(this->builder_.get());
+    value_converter_ = this->children_[0];
+    return Status::OK();
+  }
+
+ protected:
+  const T* list_type_;
+  BuilderType* list_builder_;
+  std::shared_ptr<BaseConverter> value_converter_;
+};
+
+template <typename BaseConverter>
+class StructConverter : public BaseConverter {
+ public:
+  Status Init() override {
+    struct_type_ = checked_cast<const StructType*>(this->type_.get());
+    struct_builder_ = checked_cast<StructBuilder*>(this->builder_.get());
+    return Status::OK();
+  }
+
+ protected:
+  const StructType* struct_type_;
+  StructBuilder* struct_builder_;
+};
+
+template <typename U, typename BaseConverter>
+class DictionaryConverter : public BaseConverter {
+ public:
+  using BuilderType = DictionaryBuilder<U>;
+
+  Status Init() override {
+    dict_type_ = checked_cast<const DictionaryType*>(this->type_.get());
+    value_type_ = checked_cast<const U*>(dict_type_->value_type().get());
+    value_builder_ = checked_cast<BuilderType*>(this->builder_.get());
+    return Status::OK();
+  }
+
+ protected:
+  const DictionaryType* dict_type_;
+  const U* value_type_;
+  BuilderType* value_builder_;
+};
+
+template <typename Converter, template <typename...> class ConverterTrait>
+struct MakeConverterImpl;
+
+template <typename Converter, template <typename...> class ConverterTrait>
+static Result<std::shared_ptr<Converter>> MakeConverter(
+    std::shared_ptr<DataType> type, MemoryPool* pool,
+    typename Converter::OptionsType options) {
+  std::shared_ptr<Converter> out;
+  MakeConverterImpl<Converter, ConverterTrait> visitor = {type, pool, options, 
&out};
+  ARROW_RETURN_NOT_OK(VisitTypeInline(*type, &visitor));
+  return out;
+}

Review comment:
       Updated.




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

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


Reply via email to