wjones127 commented on code in PR #12775: URL: https://github.com/apache/arrow/pull/12775#discussion_r895451563
########## cpp/examples/arrow/rapidjson_row_converter.cc: ########## @@ -0,0 +1,590 @@ +// 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 <arrow/api.h> +#include <arrow/result.h> +#include <arrow/table_builder.h> +#include <arrow/type_traits.h> +#include <arrow/util/iterator.h> +#include <arrow/util/logging.h> +#include <arrow/visit_array_inline.h> + +#include <rapidjson/document.h> +#include <rapidjson/stringbuffer.h> +#include <rapidjson/writer.h> + +#include <cassert> +#include <iostream> +#include <vector> + +// Transforming dynamic row data into Arrow data +// When building connectors to other data systems, it's common to receive data in +// row-based structures. While the row_wise_conversion_example.cc shows how to +// handle this conversion for fixed schemas, this example demonstrates how to +// writer converters for arbitrary schemas. +// +// As an example, this conversion is between Arrow and rapidjson::Documents. +// +// We use the following helpers and patterns here: +// * arrow::ToRowConverter and arrow::FromRowConverter, which provide additional +// conversion methods given a basic converter +// * arrow::VisitArrayInline and arrow::VisitTypeInline for implementing a visitor +// pattern with Arrow to handle different array types +// * arrow::enable_if_primitive_ctype to create a template method that handles +// * conversion for Arrow types that have corresponding C types (bool, integer, +// float). + +rapidjson::Value kNullJsonSingleton = rapidjson::Value(); + +/// \brief Builder that holds state for a single conversion. +/// +/// Implements Visit() methods for each type of Arrow Array that set the values +/// of the corresponding fields in each row. +class RowBatchBuilder { + public: + explicit RowBatchBuilder(int64_t num_rows) : field_(nullptr) { + // Reserve all of the space required up-front to avoid unnecessary resizing + rows_.reserve(num_rows); + + for (int64_t i = 0; i < num_rows; ++i) { + rows_.push_back(rapidjson::Document()); + rows_[i].SetObject(); + } + } + + /// \brief Set which field to convert. + void SetField(std::shared_ptr<arrow::Field> field) { field_ = std::move(field); } Review Comment: I was using a pointer because `field_` is initially nullptr. I could switch to `const arrow::Field*` to avoid the overhead, or maybe take a `const util::optional<arrow::Field>&`. Not sure what's preferable. -- 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]
