wjones127 commented on code in PR #12775:
URL: https://github.com/apache/arrow/pull/12775#discussion_r895484820


##########
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); }
+
+  /// \brief Retrieve converted rows from builder.
+  std::vector<rapidjson::Document> Rows() && { return std::move(rows_); }
+
+  // Default implementation
+  arrow::Status Visit(const arrow::Array& array) {
+    return arrow::Status::NotImplemented(
+        "Can not convert to json document for array of type ", 
array.type()->ToString());
+  }
+
+  // Handles booleans, integers, floats
+  template <typename ArrayType, typename DataClass = typename 
ArrayType::TypeClass>
+  arrow::enable_if_primitive_ctype<DataClass, arrow::Status> Visit(
+      const ArrayType& array) {
+    for (int64_t i = 0; i < array.length(); ++i) {
+      if (!array.IsNull(i)) {
+        rapidjson::Value str_key(field_->name().c_str(), 
rows_[i].GetAllocator());
+        rows_[i].AddMember(str_key, array.Value(i), rows_[i].GetAllocator());
+      }
+    }
+    return arrow::Status::OK();
+  }
+
+  arrow::Status Visit(const arrow::StringArray& array) {
+    for (int64_t i = 0; i < array.length(); ++i) {
+      if (!array.IsNull(i)) {
+        rapidjson::Value str_key(field_->name().c_str(), 
rows_[i].GetAllocator());
+        // StringArray.Value returns a string view
+        auto value_view = array.Value(i);
+        rapidjson::Value value;
+        value.SetString(value_view.data(),
+                        static_cast<rapidjson::SizeType>(value_view.size()),
+                        rows_[i].GetAllocator());
+        rows_[i].AddMember(str_key, value, rows_[i].GetAllocator());
+      }
+    }
+    return arrow::Status::OK();
+  }
+
+  arrow::Status Visit(const arrow::StructArray& array) {
+    const arrow::StructType* type = array.struct_type();
+
+    RowBatchBuilder child_builder(rows_.size());

Review Comment:
   The expectation is that the class is constructed with the correct number of 
rows, so `rows_.size() == array.length()`. But I think it's worth adding an 
`assert` here to make that explicit.



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to