kszucs commented on a change in pull request #8088: URL: https://github.com/apache/arrow/pull/8088#discussion_r484757463
########## File path: cpp/src/arrow/util/converter.h ########## @@ -0,0 +1,281 @@ +// 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 { + +using internal::checked_cast; +using internal::checked_pointer_cast; + +template <typename Input, typename Options> +class ArrayConverter { + public: + using InputType = Input; + using OptionsType = Options; + + ArrayConverter(const std::shared_ptr<DataType>& type, + std::shared_ptr<ArrayBuilder> builder, Options options) + : sp_type_(type), sp_builder_(builder), options_(options) {} + + virtual ~ArrayConverter() = default; + const std::shared_ptr<ArrayBuilder>& builder() const { return sp_builder_; } + const std::shared_ptr<DataType>& type() const { return sp_type_; } + Options options() const { return options_; } + + virtual Status Init() { return Status::OK(); } + virtual Status Reserve(int64_t additional_capacity) = 0; + virtual Status Append(InputType value) = 0; + virtual Status AppendNull() = 0; + virtual Status Extend(Input seq, int64_t size) = 0; + virtual Result<std::shared_ptr<Array>> Finish() = 0; + + protected: + const std::shared_ptr<DataType> sp_type_; + std::shared_ptr<ArrayBuilder> sp_builder_; + Options options_; +}; + +template <typename T, typename BaseConverter, + typename BuilderType = typename TypeTraits<T>::BuilderType> +class TypedArrayConverter : public BaseConverter { Review comment: ```cpp class PyStructArrayConverter : public PyArrayConverter ``` Would definitely be nicer and in case of StructType we can indeed omit the template parameter, although we still need a type template parameter for the rest of the converters because of type specific intermediate types (c_type, PyBytesView/string_view) so I'm not sure how could we further simplify the API. > I think it'd be better to reuse MakeBuilder for consistency then visit the resulting builder to hydrate the converter hierarchy. For example, we could make sp_builder_ an argument to Init(). That was the original implementation and my first attempt as well, but using the current factory and constructing the converters and builders manually ended up in a cleaner code - I'm open to your suggestion 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. For queries about this service, please contact Infrastructure at: [email protected]
