paleolimbot commented on code in PR #317:
URL: https://github.com/apache/arrow-nanoarrow/pull/317#discussion_r1401019835


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -0,0 +1,366 @@
+// 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.
+
+#ifndef NANOARROW_TESTING_HPP_INCLUDED
+#define NANOARROW_TESTING_HPP_INCLUDED
+
+#include <iostream>
+#include <string>
+
+#include "nanoarrow.hpp"
+
+namespace nanoarrow {
+
+namespace testing {
+
+class TestingJSON {
+ public:
+  static ArrowErrorCode WriteBatch(std::ostream& out, const ArrowSchema* 
schema,
+                                   ArrowArrayView* value) {
+    // Make sure we have a struct
+    if (std::string(schema->format) != "+s") {
+      return EINVAL;
+    }
+
+    out << "{";
+
+    // Write length
+    out << R"("count": )" << value->length;
+
+    // Write children
+    out << R"(, "columns": )";
+    NANOARROW_RETURN_NOT_OK(WriteChildren(out, schema, value));
+
+    out << "}";
+    return NANOARROW_OK;
+  }
+
+  static ArrowErrorCode WriteColumn(std::ostream& out, const ArrowSchema* 
field,
+                                    ArrowArrayView* value) {
+    out << "{";
+
+    // Write schema->name (may be null)
+    if (field->name == nullptr) {
+      out << R"("name": null)";
+    } else {
+      out << R"("name": ")" << field->name << R"(")";
+    }
+
+    // Write length
+    out << R"(, "count": )" << value->length;
+
+    // Write the VALIDITY element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_NA:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        break;
+      default:
+        out << R"(, "VALIDITY": )";
+        WriteBitmap(out, value->buffer_views[0].data.as_uint8, value->length);

Review Comment:
   That's a great point...the example files in apache/arrow-testing seem to all 
have `[1, 1, 1, 1]` although I agree that it might be nice to know if the 
bitmap was allocated or not. When nanoarrow gets plugged into the integration 
tests we'll find out in a hurry if that assumption is correct!



##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -0,0 +1,366 @@
+// 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.
+
+#ifndef NANOARROW_TESTING_HPP_INCLUDED
+#define NANOARROW_TESTING_HPP_INCLUDED
+
+#include <iostream>
+#include <string>
+
+#include "nanoarrow.hpp"
+
+namespace nanoarrow {
+
+namespace testing {
+
+class TestingJSON {
+ public:
+  static ArrowErrorCode WriteBatch(std::ostream& out, const ArrowSchema* 
schema,
+                                   ArrowArrayView* value) {
+    // Make sure we have a struct
+    if (std::string(schema->format) != "+s") {
+      return EINVAL;
+    }
+
+    out << "{";
+
+    // Write length
+    out << R"("count": )" << value->length;
+
+    // Write children
+    out << R"(, "columns": )";
+    NANOARROW_RETURN_NOT_OK(WriteChildren(out, schema, value));
+
+    out << "}";
+    return NANOARROW_OK;
+  }
+
+  static ArrowErrorCode WriteColumn(std::ostream& out, const ArrowSchema* 
field,
+                                    ArrowArrayView* value) {
+    out << "{";
+
+    // Write schema->name (may be null)
+    if (field->name == nullptr) {
+      out << R"("name": null)";
+    } else {
+      out << R"("name": ")" << field->name << R"(")";
+    }
+
+    // Write length
+    out << R"(, "count": )" << value->length;
+
+    // Write the VALIDITY element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_NA:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        break;
+      default:
+        out << R"(, "VALIDITY": )";
+        WriteBitmap(out, value->buffer_views[0].data.as_uint8, value->length);
+        break;
+    }
+
+    // Write the TYPE_ID element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_SPARSE_UNION:
+      case NANOARROW_TYPE_DENSE_UNION:
+        out << R"(, "TYPE_ID": )";
+        NANOARROW_RETURN_NOT_OK(WriteOffsetOrTypeID<int8_t>(out, 
value->buffer_views[0]));
+        break;
+      default:
+        break;
+    }
+
+    // Write the OFFSET element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_BINARY:
+      case NANOARROW_TYPE_STRING:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_LIST:
+        out << R"(, "OFFSET": )";
+        NANOARROW_RETURN_NOT_OK(
+            WriteOffsetOrTypeID<int32_t>(out, value->buffer_views[1]));
+        break;
+      case NANOARROW_TYPE_LARGE_LIST:
+      case NANOARROW_TYPE_LARGE_BINARY:
+      case NANOARROW_TYPE_LARGE_STRING:
+        out << R"(, "OFFSET": )";
+        NANOARROW_RETURN_NOT_OK(
+            WriteOffsetOrTypeID<int64_t>(out, value->buffer_views[1]));
+        break;
+      default:
+        break;
+    }
+
+    // Write the DATA element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_NA:
+      case NANOARROW_TYPE_STRUCT:
+      case NANOARROW_TYPE_LIST:
+      case NANOARROW_TYPE_LARGE_LIST:
+      case NANOARROW_TYPE_FIXED_SIZE_LIST:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        break;
+      default:
+        out << R"(, "DATA": )";
+        NANOARROW_RETURN_NOT_OK(WriteData(out, value));
+        break;
+    }
+
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_STRUCT:
+      case NANOARROW_TYPE_LIST:
+      case NANOARROW_TYPE_LARGE_LIST:
+      case NANOARROW_TYPE_FIXED_SIZE_LIST:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        out << R"(, "children": )";
+        NANOARROW_RETURN_NOT_OK(WriteChildren(out, field, value));
+        break;
+      default:
+        break;
+    }
+
+    out << "}";
+    return NANOARROW_OK;
+  }
+
+ private:
+  static void WriteBitmap(std::ostream& out, const uint8_t* bits, int64_t 
length) {
+    if (length == 0) {
+      out << "[]";
+      return;
+    }
+
+    out << "[";
+
+    if (bits == nullptr) {
+      out << "1";
+      for (int64_t i = 1; i < length; i++) {
+        out << ", 1";
+      }
+    } else {
+      out << static_cast<int32_t>(ArrowBitGet(bits, 0));
+      for (int64_t i = 1; i < length; i++) {
+        out << ", " << static_cast<int32_t>(ArrowBitGet(bits, i));
+      }
+    }
+
+    out << "]";
+  }
+
+  template <typename T>
+  static ArrowErrorCode WriteOffsetOrTypeID(std::ostream& out, ArrowBufferView 
content) {
+    if (content.size_bytes == 0) {
+      out << "[]";
+      return NANOARROW_OK;
+    }
+
+    const T* values = reinterpret_cast<const T*>(content.data.data);
+    int64_t n_values = content.size_bytes / sizeof(T);
+
+    out << "[";
+
+    if (sizeof(T) == sizeof(int64_t)) {
+      out << R"(")" << values[0] << R"(")";
+      for (int64_t i = 1; i < n_values; i++) {
+        out << R"(, ")" << values[i] << R"(")";
+      }
+    } else {
+      out << values[0];
+      for (int64_t i = 1; i < n_values; i++) {
+        out << ", " << static_cast<int64_t>(values[i]);
+      }
+    }
+
+    out << "]";
+    return NANOARROW_OK;
+  }
+
+  static ArrowErrorCode WriteData(std::ostream& out, ArrowArrayView* value) {
+    if (value->length == 0) {
+      out << "[]";
+      return NANOARROW_OK;
+    }
+
+    out << "[";
+
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_BOOL:
+      case NANOARROW_TYPE_INT8:
+      case NANOARROW_TYPE_UINT8:
+      case NANOARROW_TYPE_INT16:
+      case NANOARROW_TYPE_UINT16:
+      case NANOARROW_TYPE_INT32:
+      case NANOARROW_TYPE_UINT32:
+        // Regular JSON integers
+        out << ArrowArrayViewGetIntUnsafe(value, 0);
+        for (int64_t i = 1; i < value->length; i++) {
+          out << ", " << ArrowArrayViewGetIntUnsafe(value, i);
+        }
+        break;
+      case NANOARROW_TYPE_INT64:
+        // Strings
+        out << R"(")" << ArrowArrayViewGetIntUnsafe(value, 0) << R"(")";
+        for (int64_t i = 1; i < value->length; i++) {
+          out << R"(, ")" << ArrowArrayViewGetIntUnsafe(value, i) << R"(")";
+        }
+        break;
+      case NANOARROW_TYPE_UINT64:
+        // Strings

Review Comment:
   I'll clarify this...I was trying to highlight the `"quotedness"` of the 
values here.



##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -0,0 +1,366 @@
+// 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.
+
+#ifndef NANOARROW_TESTING_HPP_INCLUDED
+#define NANOARROW_TESTING_HPP_INCLUDED
+
+#include <iostream>
+#include <string>
+
+#include "nanoarrow.hpp"
+
+namespace nanoarrow {
+
+namespace testing {
+
+class TestingJSON {
+ public:
+  static ArrowErrorCode WriteBatch(std::ostream& out, const ArrowSchema* 
schema,
+                                   ArrowArrayView* value) {
+    // Make sure we have a struct
+    if (std::string(schema->format) != "+s") {
+      return EINVAL;
+    }
+
+    out << "{";
+
+    // Write length
+    out << R"("count": )" << value->length;
+
+    // Write children
+    out << R"(, "columns": )";
+    NANOARROW_RETURN_NOT_OK(WriteChildren(out, schema, value));
+
+    out << "}";
+    return NANOARROW_OK;
+  }
+
+  static ArrowErrorCode WriteColumn(std::ostream& out, const ArrowSchema* 
field,
+                                    ArrowArrayView* value) {
+    out << "{";
+
+    // Write schema->name (may be null)
+    if (field->name == nullptr) {
+      out << R"("name": null)";
+    } else {
+      out << R"("name": ")" << field->name << R"(")";
+    }
+
+    // Write length
+    out << R"(, "count": )" << value->length;
+
+    // Write the VALIDITY element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_NA:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        break;
+      default:
+        out << R"(, "VALIDITY": )";
+        WriteBitmap(out, value->buffer_views[0].data.as_uint8, value->length);
+        break;
+    }
+
+    // Write the TYPE_ID element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_SPARSE_UNION:
+      case NANOARROW_TYPE_DENSE_UNION:
+        out << R"(, "TYPE_ID": )";
+        NANOARROW_RETURN_NOT_OK(WriteOffsetOrTypeID<int8_t>(out, 
value->buffer_views[0]));
+        break;
+      default:
+        break;
+    }
+
+    // Write the OFFSET element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_BINARY:
+      case NANOARROW_TYPE_STRING:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_LIST:
+        out << R"(, "OFFSET": )";
+        NANOARROW_RETURN_NOT_OK(
+            WriteOffsetOrTypeID<int32_t>(out, value->buffer_views[1]));
+        break;
+      case NANOARROW_TYPE_LARGE_LIST:
+      case NANOARROW_TYPE_LARGE_BINARY:
+      case NANOARROW_TYPE_LARGE_STRING:
+        out << R"(, "OFFSET": )";
+        NANOARROW_RETURN_NOT_OK(
+            WriteOffsetOrTypeID<int64_t>(out, value->buffer_views[1]));
+        break;
+      default:
+        break;
+    }
+
+    // Write the DATA element if required
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_NA:
+      case NANOARROW_TYPE_STRUCT:
+      case NANOARROW_TYPE_LIST:
+      case NANOARROW_TYPE_LARGE_LIST:
+      case NANOARROW_TYPE_FIXED_SIZE_LIST:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        break;
+      default:
+        out << R"(, "DATA": )";
+        NANOARROW_RETURN_NOT_OK(WriteData(out, value));
+        break;
+    }
+
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_STRUCT:
+      case NANOARROW_TYPE_LIST:
+      case NANOARROW_TYPE_LARGE_LIST:
+      case NANOARROW_TYPE_FIXED_SIZE_LIST:
+      case NANOARROW_TYPE_DENSE_UNION:
+      case NANOARROW_TYPE_SPARSE_UNION:
+        out << R"(, "children": )";
+        NANOARROW_RETURN_NOT_OK(WriteChildren(out, field, value));
+        break;
+      default:
+        break;
+    }
+
+    out << "}";
+    return NANOARROW_OK;
+  }
+
+ private:
+  static void WriteBitmap(std::ostream& out, const uint8_t* bits, int64_t 
length) {
+    if (length == 0) {
+      out << "[]";
+      return;
+    }
+
+    out << "[";
+
+    if (bits == nullptr) {
+      out << "1";
+      for (int64_t i = 1; i < length; i++) {
+        out << ", 1";
+      }
+    } else {
+      out << static_cast<int32_t>(ArrowBitGet(bits, 0));
+      for (int64_t i = 1; i < length; i++) {
+        out << ", " << static_cast<int32_t>(ArrowBitGet(bits, i));
+      }
+    }
+
+    out << "]";
+  }
+
+  template <typename T>
+  static ArrowErrorCode WriteOffsetOrTypeID(std::ostream& out, ArrowBufferView 
content) {
+    if (content.size_bytes == 0) {
+      out << "[]";
+      return NANOARROW_OK;
+    }
+
+    const T* values = reinterpret_cast<const T*>(content.data.data);
+    int64_t n_values = content.size_bytes / sizeof(T);
+
+    out << "[";
+
+    if (sizeof(T) == sizeof(int64_t)) {
+      out << R"(")" << values[0] << R"(")";
+      for (int64_t i = 1; i < n_values; i++) {
+        out << R"(, ")" << values[i] << R"(")";
+      }
+    } else {
+      out << values[0];
+      for (int64_t i = 1; i < n_values; i++) {
+        out << ", " << static_cast<int64_t>(values[i]);
+      }
+    }
+
+    out << "]";
+    return NANOARROW_OK;
+  }
+
+  static ArrowErrorCode WriteData(std::ostream& out, ArrowArrayView* value) {
+    if (value->length == 0) {
+      out << "[]";
+      return NANOARROW_OK;
+    }
+
+    out << "[";
+
+    switch (value->storage_type) {
+      case NANOARROW_TYPE_BOOL:
+      case NANOARROW_TYPE_INT8:
+      case NANOARROW_TYPE_UINT8:
+      case NANOARROW_TYPE_INT16:
+      case NANOARROW_TYPE_UINT16:
+      case NANOARROW_TYPE_INT32:
+      case NANOARROW_TYPE_UINT32:
+        // Regular JSON integers
+        out << ArrowArrayViewGetIntUnsafe(value, 0);
+        for (int64_t i = 1; i < value->length; i++) {
+          out << ", " << ArrowArrayViewGetIntUnsafe(value, i);
+        }
+        break;
+      case NANOARROW_TYPE_INT64:

Review Comment:
   It's subtle, but it generates `"1234567"` rather than `1234567`. I'll make 
sure the comments make that more clear!



##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -0,0 +1,278 @@
+// 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 <functional>
+#include <sstream>
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include "nanoarrow_testing.hpp"
+
+using nanoarrow::testing::TestingJSON;
+
+void TestColumn(std::function<ArrowErrorCode(ArrowSchema*)> type_expr,
+                std::function<ArrowErrorCode(ArrowArray*)> append_expr,
+                ArrowErrorCode (*test_expr)(std::ostream&, const ArrowSchema*,
+                                            ArrowArrayView*),
+                const std::string& expected_json) {
+  std::stringstream ss;
+
+  nanoarrow::UniqueSchema schema;
+  ASSERT_EQ(type_expr(schema.get()), NANOARROW_OK);
+  nanoarrow::UniqueArray array;
+  ASSERT_EQ(ArrowArrayInitFromSchema(array.get(), schema.get(), nullptr), 
NANOARROW_OK);
+  ASSERT_EQ(ArrowArrayStartAppending(array.get()), NANOARROW_OK);
+  ASSERT_EQ(append_expr(array.get()), NANOARROW_OK);
+  ASSERT_EQ(ArrowArrayFinishBuildingDefault(array.get(), nullptr), 
NANOARROW_OK);
+
+  nanoarrow::UniqueArrayView array_view;
+  ASSERT_EQ(ArrowArrayViewInitFromSchema(array_view.get(), schema.get(), 
nullptr),
+            NANOARROW_OK);
+  ASSERT_EQ(ArrowArrayViewSetArray(array_view.get(), array.get(), nullptr), 
NANOARROW_OK);
+
+  ASSERT_EQ(test_expr(ss, schema.get(), array_view.get()), NANOARROW_OK);
+  EXPECT_EQ(ss.str(), expected_json);
+}
+
+TEST(NanoarrowTestingTest, NanoarrowTestingTestColumnNull) {
+  TestColumn(
+      [](ArrowSchema* schema) {
+        return ArrowSchemaInitFromType(schema, NANOARROW_TYPE_NA);
+      },
+      [](ArrowArray* array) { return NANOARROW_OK; }, 
&TestingJSON::WriteColumn,
+      R"({"name": null, "count": 0})");

Review Comment:
   That's a great idea! I'll probably keep these tests (bare minimum to get 
100% coverage over the code) and add in those tests as well (for full type 
coverage!).



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