You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "paleolimbot (via GitHub)" <gi...@apache.org> on 2023/12/01 16:29:28 UTC

[PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

paleolimbot opened a new pull request, #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330

   (no comment)


-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417075948


##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -170,12 +171,13 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestColumnFloat) {
         return ArrowSchemaInitFromType(schema, NANOARROW_TYPE_FLOAT);
       },
       [](ArrowArray* array) {
+        NANOARROW_RETURN_NOT_OK(ArrowArrayAppendNull(array, 1));
         NANOARROW_RETURN_NOT_OK(ArrowArrayAppendDouble(array, 0.1234));
         NANOARROW_RETURN_NOT_OK(ArrowArrayAppendDouble(array, 1.2345));
         return NANOARROW_OK;
       },
       &WriteColumnJSON,
-      R"({"name": null, "count": 2, "VALIDITY": [1, 1], "DATA": [0.123, 1.235]})");
+      R"({"name": null, "count": 3, "VALIDITY": [0, 1, 1], "DATA": [0.000, 0.123, 1.235]})");

Review Comment:
   The fixed output precision is amusing. Hopefully it's not a problem for integration tests?



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417583444


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -1661,6 +1714,305 @@ class TestingJSONReader {
   }
 };
 
+/// \brief Integration testing comparison utility
+///
+/// Utility to compare ArrowSchema, ArrowArray, and ArrowArrayStream instances.
+/// This should only be used in the context of integration testing as the
+/// comparison logic is specific to the integration testing JSON files and
+/// specification. Notably:
+///
+/// - Map types are considered equal regardless of the child names "entries",
+///   "key", and "value".
+/// - Float32 and Float64 values are only compared to 3 decimal places.
+class TestingJSONComparison {
+ private:
+  // Internal representation of a human-readable inequality
+  struct Difference {
+    std::string path;
+    std::string actual;
+    std::string expected;
+  };
+
+ public:
+  /// \brief Returns the number of differences found by the previous call
+  size_t num_differences() const { return differences_.size(); }
+
+  /// \brief Dump a human-readable summary of differences to out
+  void WriteDifferences(std::ostream& out) {
+    for (const auto& difference : differences_) {
+      out << "Path: " << difference.path << "\n";
+      out << "- " << difference.actual << "\n";
+      out << "+ " << difference.expected << "\n";
+      out << "\n";
+    }
+  }
+
+  /// \brief Clear any existing differences
+  void ClearDifferences() { differences_.clear(); }
+
+  /// \brief Compare a stream of record batches
+  ///
+  /// Compares actual against expected using the following strategy:
+  ///
+  /// - Compares schemas for equality, returning if differences were found
+  /// - Compares pairs of record batches, returning if one stream finished
+  ///   before another.
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareArrayStream(ArrowArrayStream* actual, ArrowArrayStream* expected,
+                                    ArrowError* error = nullptr) {
+    // Read both schemas
+    nanoarrow::UniqueSchema actual_schema;
+    nanoarrow::UniqueSchema expected_schema;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_schema(actual, actual_schema.get()),
+                                       error);
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        expected->get_schema(expected, expected_schema.get()), error);
+
+    // Compare them and return if they are not equal
+    NANOARROW_RETURN_NOT_OK(
+        CompareSchema(expected_schema.get(), actual_schema.get(), error, "Schema"));
+    if (num_differences() > 0) {
+      return NANOARROW_OK;
+    }
+
+    // Keep a record of the schema to compare batches
+    NANOARROW_RETURN_NOT_OK(SetSchema(expected_schema.get(), error));
+
+    int64_t n_batches = -1;
+    nanoarrow::UniqueArray actual_array;
+    nanoarrow::UniqueArray expected_array;
+    do {
+      n_batches++;
+      std::string batch_label = std::string("Batch ") + std::to_string(n_batches);
+
+      // Read a batch from each stream
+      actual_array.reset();
+      expected_array.reset();
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_next(actual, actual_array.get()),
+                                         error);
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+          expected->get_next(expected, expected_array.get()), error);
+
+      // Check the finished/unfinished status of both streams
+      if (actual_array->release == nullptr && expected_array->release != nullptr) {
+        differences_.push_back({batch_label, "finished stream", "unfinished stream"});
+        return NANOARROW_OK;
+      }
+
+      if (actual_array->release != nullptr && expected_array->release == nullptr) {
+        differences_.push_back({batch_label, "unfinished stream", "finished stream"});
+        return NANOARROW_OK;
+      }
+
+      // If both streams are done, break
+      if (actual_array->release == nullptr) {
+        break;
+      }
+
+      // Compare this batch
+      NANOARROW_RETURN_NOT_OK(
+          CompareBatch(actual_array.get(), expected_array.get(), error, batch_label));
+    } while (true);
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Compare a top-level ArrowSchema struct
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareSchema(const ArrowSchema* actual, const ArrowSchema* expected,
+                               ArrowError* error = nullptr,

Review Comment:
   It's null in the tests (and in general, the `error` member can always be null in nanoarrow if the caller doesn't want/need verbose output)



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417080918


##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -1107,3 +1147,290 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestFieldUnion) {
   TestTypeError(R"({"name": "union", "mode": "NOT_A_MODE", "typeIds": []})",
                 "Type[name=='union'] mode must be 'DENSE' or 'SPARSE'");
 }
+
+TEST(NanoarrowTestingTest, NanoarrowTestingTestSchemaComparison) {
+  nanoarrow::UniqueSchema actual;
+  nanoarrow::UniqueSchema expected;
+  TestingJSONComparison comparison;
+  std::stringstream msg;
+
+  // Start with two identical schemas and ensure there are no differences
+  ArrowSchemaInit(actual.get());
+  ASSERT_EQ(ArrowSchemaSetTypeStruct(actual.get(), 1), NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaSetType(actual->children[0], NANOARROW_TYPE_NA), NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaDeepCopy(actual.get(), expected.get()), NANOARROW_OK);
+
+  ASSERT_EQ(comparison.CompareSchema(actual.get(), expected.get()), NANOARROW_OK);
+  EXPECT_EQ(comparison.num_differences(), 0);
+  comparison.WriteDifferences(msg);
+  EXPECT_EQ(msg.str(), "");
+  msg.str("");
+  comparison.ClearDifferences();

Review Comment:
   You could factor this out in a helper function:
   ```suggestion
     AssertSchemasCompareEqual(actual.get(), expected.get());
   ```



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417082982


##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -409,9 +433,8 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestFieldNested) {
       },
       [](ArrowArray* array) { return NANOARROW_OK; }, &WriteFieldJSON,
       R"({"name": null, "nullable": true, "type": {"name": "struct"}, "children": [)"
-      R"({"name": null, "nullable": true, "type": {"name": "null"}, "children": [], "metadata": null}, )"
-      R"({"name": null, "nullable": true, "type": {"name": "utf8"}, "children": [], "metadata": null}], )"
-      R"("metadata": null})");
+      R"({"name": null, "nullable": true, "type": {"name": "null"}, "children": []}, )"
+      R"({"name": null, "nullable": true, "type": {"name": "utf8"}, "children": []}]})");

Review Comment:
   Are there tests for struct fields with non-empty metadata?



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417583935


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -574,7 +583,52 @@ class TestingJSONWriter {
     return NANOARROW_OK;
   }
 
-  ArrowErrorCode WriteString(std::ostream& out, ArrowStringView value) {
+  void WriteIntMaybeNull(std::ostream& out, ArrowArrayView* view, int64_t i) {

Review Comment:
   I updated everything I could see to add `const`!



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417079894


##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -1107,3 +1147,290 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestFieldUnion) {
   TestTypeError(R"({"name": "union", "mode": "NOT_A_MODE", "typeIds": []})",
                 "Type[name=='union'] mode must be 'DENSE' or 'SPARSE'");
 }
+
+TEST(NanoarrowTestingTest, NanoarrowTestingTestSchemaComparison) {
+  nanoarrow::UniqueSchema actual;
+  nanoarrow::UniqueSchema expected;
+  TestingJSONComparison comparison;
+  std::stringstream msg;
+
+  // Start with two identical schemas and ensure there are no differences
+  ArrowSchemaInit(actual.get());
+  ASSERT_EQ(ArrowSchemaSetTypeStruct(actual.get(), 1), NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaSetType(actual->children[0], NANOARROW_TYPE_NA), NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaDeepCopy(actual.get(), expected.get()), NANOARROW_OK);
+
+  ASSERT_EQ(comparison.CompareSchema(actual.get(), expected.get()), NANOARROW_OK);
+  EXPECT_EQ(comparison.num_differences(), 0);
+  comparison.WriteDifferences(msg);
+  EXPECT_EQ(msg.str(), "");
+  msg.str("");
+  comparison.ClearDifferences();
+
+  // With different top-level flags
+  actual->flags = ARROW_FLAG_MAP_KEYS_SORTED;
+  ASSERT_EQ(comparison.CompareSchema(actual.get(), expected.get()), NANOARROW_OK);
+  EXPECT_EQ(comparison.num_differences(), 1);
+  comparison.WriteDifferences(msg);
+  EXPECT_EQ(msg.str(), "Path: \n- .flags: 4\n+ .flags: 2\n\n");
+  msg.str("");

Review Comment:
   You could factor this out in a helper function:
   ```suggestion
     AssertSchemasCompareUnequal(actual.get(), expected.get(),
       /*expected_num_differences=*/ 1,
       /*expected_msg=*/ "Path: \n- .flags: 4\n+ .flags: 2\n\n");
   ```



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417573981


##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -409,9 +433,8 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestFieldNested) {
       },
       [](ArrowArray* array) { return NANOARROW_OK; }, &WriteFieldJSON,
       R"({"name": null, "nullable": true, "type": {"name": "struct"}, "children": [)"
-      R"({"name": null, "nullable": true, "type": {"name": "null"}, "children": [], "metadata": null}, )"
-      R"({"name": null, "nullable": true, "type": {"name": "utf8"}, "children": [], "metadata": null}], )"
-      R"("metadata": null})");
+      R"({"name": null, "nullable": true, "type": {"name": "null"}, "children": []}, )"
+      R"({"name": null, "nullable": true, "type": {"name": "utf8"}, "children": []}]})");

Review Comment:
   Good catch!



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417056897


##########
extensions/nanoarrow_ipc/scripts/test_integration_test_util.sh:
##########
@@ -0,0 +1,86 @@
+# 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.
+
+if [ ${VERBOSE:-0} -gt 0 ]; then

Review Comment:
   @kou Would you like to review this shell script?



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417068138


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -1661,6 +1714,305 @@ class TestingJSONReader {
   }
 };
 
+/// \brief Integration testing comparison utility
+///
+/// Utility to compare ArrowSchema, ArrowArray, and ArrowArrayStream instances.
+/// This should only be used in the context of integration testing as the
+/// comparison logic is specific to the integration testing JSON files and
+/// specification. Notably:
+///
+/// - Map types are considered equal regardless of the child names "entries",
+///   "key", and "value".
+/// - Float32 and Float64 values are only compared to 3 decimal places.
+class TestingJSONComparison {
+ private:
+  // Internal representation of a human-readable inequality
+  struct Difference {
+    std::string path;
+    std::string actual;
+    std::string expected;
+  };
+
+ public:
+  /// \brief Returns the number of differences found by the previous call
+  size_t num_differences() const { return differences_.size(); }
+
+  /// \brief Dump a human-readable summary of differences to out
+  void WriteDifferences(std::ostream& out) {
+    for (const auto& difference : differences_) {
+      out << "Path: " << difference.path << "\n";
+      out << "- " << difference.actual << "\n";
+      out << "+ " << difference.expected << "\n";
+      out << "\n";
+    }
+  }
+
+  /// \brief Clear any existing differences
+  void ClearDifferences() { differences_.clear(); }
+
+  /// \brief Compare a stream of record batches
+  ///
+  /// Compares actual against expected using the following strategy:
+  ///
+  /// - Compares schemas for equality, returning if differences were found
+  /// - Compares pairs of record batches, returning if one stream finished
+  ///   before another.
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareArrayStream(ArrowArrayStream* actual, ArrowArrayStream* expected,
+                                    ArrowError* error = nullptr) {
+    // Read both schemas
+    nanoarrow::UniqueSchema actual_schema;
+    nanoarrow::UniqueSchema expected_schema;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_schema(actual, actual_schema.get()),
+                                       error);
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        expected->get_schema(expected, expected_schema.get()), error);
+
+    // Compare them and return if they are not equal
+    NANOARROW_RETURN_NOT_OK(
+        CompareSchema(expected_schema.get(), actual_schema.get(), error, "Schema"));
+    if (num_differences() > 0) {
+      return NANOARROW_OK;
+    }
+
+    // Keep a record of the schema to compare batches
+    NANOARROW_RETURN_NOT_OK(SetSchema(expected_schema.get(), error));
+
+    int64_t n_batches = -1;
+    nanoarrow::UniqueArray actual_array;
+    nanoarrow::UniqueArray expected_array;
+    do {
+      n_batches++;
+      std::string batch_label = std::string("Batch ") + std::to_string(n_batches);
+
+      // Read a batch from each stream
+      actual_array.reset();
+      expected_array.reset();
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_next(actual, actual_array.get()),
+                                         error);
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+          expected->get_next(expected, expected_array.get()), error);
+
+      // Check the finished/unfinished status of both streams
+      if (actual_array->release == nullptr && expected_array->release != nullptr) {
+        differences_.push_back({batch_label, "finished stream", "unfinished stream"});
+        return NANOARROW_OK;
+      }
+
+      if (actual_array->release != nullptr && expected_array->release == nullptr) {
+        differences_.push_back({batch_label, "unfinished stream", "finished stream"});
+        return NANOARROW_OK;
+      }
+
+      // If both streams are done, break
+      if (actual_array->release == nullptr) {
+        break;
+      }
+
+      // Compare this batch
+      NANOARROW_RETURN_NOT_OK(
+          CompareBatch(actual_array.get(), expected_array.get(), error, batch_label));
+    } while (true);
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Compare a top-level ArrowSchema struct
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareSchema(const ArrowSchema* actual, const ArrowSchema* expected,
+                               ArrowError* error = nullptr,

Review Comment:
   Do we ever expect to pass a null error pointer here? Otherwise you could simplify these signatures.



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417581147


##########
src/nanoarrow/nanoarrow_testing_test.cc:
##########
@@ -170,12 +171,13 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestColumnFloat) {
         return ArrowSchemaInitFromType(schema, NANOARROW_TYPE_FLOAT);
       },
       [](ArrowArray* array) {
+        NANOARROW_RETURN_NOT_OK(ArrowArrayAppendNull(array, 1));
         NANOARROW_RETURN_NOT_OK(ArrowArrayAppendDouble(array, 0.1234));
         NANOARROW_RETURN_NOT_OK(ArrowArrayAppendDouble(array, 1.2345));
         return NANOARROW_OK;
       },
       &WriteColumnJSON,
-      R"({"name": null, "count": 2, "VALIDITY": [1, 1], "DATA": [0.123, 1.235]})");
+      R"({"name": null, "count": 3, "VALIDITY": [0, 1, 1], "DATA": [0.000, 0.123, 1.235]})");

Review Comment:
   The JSON in the testing repo looks like this, although the "silently truncate by default" has always bothered me (particularly since I use it for comparing arrays), so I made the fixed precision something you have to try to do.



##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -1661,6 +1714,305 @@ class TestingJSONReader {
   }
 };
 
+/// \brief Integration testing comparison utility
+///
+/// Utility to compare ArrowSchema, ArrowArray, and ArrowArrayStream instances.
+/// This should only be used in the context of integration testing as the
+/// comparison logic is specific to the integration testing JSON files and
+/// specification. Notably:
+///
+/// - Map types are considered equal regardless of the child names "entries",
+///   "key", and "value".
+/// - Float32 and Float64 values are only compared to 3 decimal places.
+class TestingJSONComparison {
+ private:
+  // Internal representation of a human-readable inequality
+  struct Difference {
+    std::string path;
+    std::string actual;
+    std::string expected;
+  };
+
+ public:
+  /// \brief Returns the number of differences found by the previous call
+  size_t num_differences() const { return differences_.size(); }
+
+  /// \brief Dump a human-readable summary of differences to out
+  void WriteDifferences(std::ostream& out) {
+    for (const auto& difference : differences_) {
+      out << "Path: " << difference.path << "\n";
+      out << "- " << difference.actual << "\n";
+      out << "+ " << difference.expected << "\n";
+      out << "\n";
+    }
+  }
+
+  /// \brief Clear any existing differences
+  void ClearDifferences() { differences_.clear(); }
+
+  /// \brief Compare a stream of record batches
+  ///
+  /// Compares actual against expected using the following strategy:
+  ///
+  /// - Compares schemas for equality, returning if differences were found
+  /// - Compares pairs of record batches, returning if one stream finished
+  ///   before another.
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareArrayStream(ArrowArrayStream* actual, ArrowArrayStream* expected,
+                                    ArrowError* error = nullptr) {
+    // Read both schemas
+    nanoarrow::UniqueSchema actual_schema;
+    nanoarrow::UniqueSchema expected_schema;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_schema(actual, actual_schema.get()),
+                                       error);
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        expected->get_schema(expected, expected_schema.get()), error);
+
+    // Compare them and return if they are not equal
+    NANOARROW_RETURN_NOT_OK(
+        CompareSchema(expected_schema.get(), actual_schema.get(), error, "Schema"));
+    if (num_differences() > 0) {
+      return NANOARROW_OK;
+    }
+
+    // Keep a record of the schema to compare batches
+    NANOARROW_RETURN_NOT_OK(SetSchema(expected_schema.get(), error));
+
+    int64_t n_batches = -1;
+    nanoarrow::UniqueArray actual_array;
+    nanoarrow::UniqueArray expected_array;
+    do {
+      n_batches++;
+      std::string batch_label = std::string("Batch ") + std::to_string(n_batches);
+
+      // Read a batch from each stream
+      actual_array.reset();
+      expected_array.reset();
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_next(actual, actual_array.get()),
+                                         error);
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+          expected->get_next(expected, expected_array.get()), error);
+
+      // Check the finished/unfinished status of both streams
+      if (actual_array->release == nullptr && expected_array->release != nullptr) {
+        differences_.push_back({batch_label, "finished stream", "unfinished stream"});
+        return NANOARROW_OK;
+      }
+
+      if (actual_array->release != nullptr && expected_array->release == nullptr) {
+        differences_.push_back({batch_label, "unfinished stream", "finished stream"});
+        return NANOARROW_OK;
+      }
+
+      // If both streams are done, break
+      if (actual_array->release == nullptr) {
+        break;
+      }
+
+      // Compare this batch
+      NANOARROW_RETURN_NOT_OK(
+          CompareBatch(actual_array.get(), expected_array.get(), error, batch_label));
+    } while (true);
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Compare a top-level ArrowSchema struct
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareSchema(const ArrowSchema* actual, const ArrowSchema* expected,
+                               ArrowError* error = nullptr,
+                               const std::string& path = "") {
+    // Compare the top-level schema "manually" because (1) map type needs special-cased
+    // comparison and (2) it's easier to read the output if differences are separated
+    // by field.
+    ArrowSchemaView actual_view;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaViewInit(&actual_view, actual, nullptr),
+                                       error);
+
+    ArrowSchemaView expected_view;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        ArrowSchemaViewInit(&expected_view, expected, nullptr), error);
+
+    if (actual_view.type != NANOARROW_TYPE_STRUCT ||
+        expected_view.type != NANOARROW_TYPE_STRUCT) {
+      ArrowErrorSet(error, "Top-level schema must be struct");
+      return EINVAL;
+    }
+
+    // (Purposefully ignore the name field at the top level)
+
+    // Compare flags
+    if (actual->flags != expected->flags) {
+      differences_.push_back({path,
+                              std::string(".flags: ") + std::to_string(actual->flags),
+                              std::string(".flags: ") + std::to_string(expected->flags)});
+    }
+
+    // Compare children
+    if (actual->n_children != expected->n_children) {
+      differences_.push_back(
+          {path, std::string(".n_children: ") + std::to_string(actual->n_children),
+           std::string(".n_children: ") + std::to_string(expected->n_children)});
+    } else {
+      for (int64_t i = 0; i < expected->n_children; i++) {
+        NANOARROW_RETURN_NOT_OK(CompareField(
+            actual->children[i], expected->children[i], error,
+            path + std::string(".children[") + std::to_string(i) + std::string("]")));
+      }
+    }
+
+    // Compare metadata
+    std::stringstream ss;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteMetadata(ss, actual->metadata),
+                                       error);
+    std::string actual_metadata = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteMetadata(ss, expected->metadata),
+                                       error);
+    std::string expected_metadata = ss.str();
+
+    if (actual_metadata != expected_metadata) {
+      differences_.push_back({path, std::string(".metadata: ") + actual_metadata,
+                              std::string(".metadata: ") + expected_metadata});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Set the ArrowSchema to be used to for future calls to CompareBatch().
+  ArrowErrorCode SetSchema(const ArrowSchema* schema, ArrowError* error = nullptr) {
+    schema_.reset();
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaDeepCopy(schema, schema_.get()), error);
+    actual_.reset();
+    expected_.reset();
+
+    NANOARROW_RETURN_NOT_OK(
+        ArrowArrayViewInitFromSchema(actual_.get(), schema_.get(), error));
+    NANOARROW_RETURN_NOT_OK(
+        ArrowArrayViewInitFromSchema(expected_.get(), schema_.get(), error));
+
+    if (actual_->storage_type != NANOARROW_TYPE_STRUCT) {
+      ArrowErrorSet(error, "Can't SetSchema() with non-struct");
+      return EINVAL;
+    }
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Compare a top-level ArrowArray struct
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareBatch(const ArrowArray* actual, const ArrowArray* expected,
+                              ArrowError* error = nullptr, const std::string& path = "") {
+    NANOARROW_RETURN_NOT_OK(ArrowArrayViewSetArray(expected_.get(), expected, error));
+    NANOARROW_RETURN_NOT_OK(ArrowArrayViewSetArray(actual_.get(), actual, error));
+
+    if (actual->offset != expected->offset) {
+      differences_.push_back({path, ".offset: " + std::to_string(actual->offset),
+                              ".offset: " + std::to_string(expected->offset)});
+    }
+
+    if (actual->length != expected->length) {
+      differences_.push_back({path, ".length: " + std::to_string(actual->length),
+                              ".length: " + std::to_string(expected->length)});
+    }
+
+    // ArrowArrayViewSetArray() ensured that number of children of both match schema
+    for (int64_t i = 0; i < expected_->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(CompareColumn(
+          schema_->children[i], actual_->children[i], expected_->children[i], error,
+          path + std::string(".children[") + std::to_string(i) + "]"));
+    }
+
+    return NANOARROW_OK;
+  }
+
+ private:
+  TestingJSONWriter writer_;
+  std::vector<Difference> differences_;
+  nanoarrow::UniqueSchema schema_;
+  nanoarrow::UniqueArrayView actual_;
+  nanoarrow::UniqueArrayView expected_;
+
+  ArrowErrorCode CompareField(ArrowSchema* actual, ArrowSchema* expected,
+                              ArrowError* error, const std::string& path = "") {
+    // Preprocess both fields such that map types have canonical names
+    nanoarrow::UniqueSchema actual_copy;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaDeepCopy(actual, actual_copy.get()),
+                                       error);
+    nanoarrow::UniqueSchema expected_copy;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaDeepCopy(expected, expected_copy.get()),
+                                       error);
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ForceMapNamesCanonical(actual_copy.get()), error);
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ForceMapNamesCanonical(expected_copy.get()),
+                                       error);
+    return CompareFieldBase(actual_copy.get(), expected_copy.get(), error, path);
+  }
+
+  ArrowErrorCode CompareFieldBase(ArrowSchema* actual, ArrowSchema* expected,
+                                  ArrowError* error, const std::string& path = "") {
+    std::stringstream ss;
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteField(ss, expected), error);
+    std::string expected_json = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteField(ss, actual), error);
+    std::string actual_json = ss.str();
+
+    if (actual_json != expected_json) {
+      differences_.push_back({path, actual_json, expected_json});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode CompareColumn(ArrowSchema* schema, ArrowArrayView* actual,
+                               ArrowArrayView* expected, ArrowError* error,
+                               const std::string& path = "") {
+    std::stringstream ss;
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteColumn(ss, schema, expected), error);
+    std::string expected_json = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteColumn(ss, schema, actual), error);
+    std::string actual_json = ss.str();
+
+    if (actual_json != expected_json) {
+      differences_.push_back({path, actual_json, expected_json});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode ForceMapNamesCanonical(ArrowSchema* schema) {
+    ArrowSchemaView view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&view, schema, nullptr));
+
+    if (view.type == NANOARROW_TYPE_MAP) {
+      NANOARROW_RETURN_NOT_OK(ArrowSchemaSetName(schema->children[0], "entries"));
+      NANOARROW_RETURN_NOT_OK(
+          ArrowSchemaSetName(schema->children[0]->children[0], "key"));
+      NANOARROW_RETURN_NOT_OK(
+          ArrowSchemaSetName(schema->children[0]->children[1], "value"));
+    }
+
+    for (int64_t i = 0; i < schema->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(ForceMapNamesCanonical(schema->children[i]));
+    }

Review Comment:
   Good catch!



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417064256


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -574,7 +583,52 @@ class TestingJSONWriter {
     return NANOARROW_OK;
   }
 
-  ArrowErrorCode WriteString(std::ostream& out, ArrowStringView value) {
+  void WriteIntMaybeNull(std::ostream& out, ArrowArrayView* view, int64_t i) {

Review Comment:
   Should you mark the view `const` here and in other such functions?
   ```suggestion
     void WriteIntMaybeNull(std::ostream& out, const ArrowArrayView* view, int64_t i) {
   ```



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417598425


##########
extensions/nanoarrow_ipc/src/apps/integration_test_util.cc:
##########
@@ -0,0 +1,219 @@
+// 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 <cerrno>
+#include <deque>
+#include <iostream>
+#include <string>
+#include <unordered_map>
+#include <utility>
+
+#include "nanoarrow/nanoarrow_ipc.hpp"
+#include "nanoarrow/nanoarrow_testing.hpp"
+
+void print_help() {
+  std::cerr << "nanoarrow version " << ArrowNanoarrowVersion() << "\n";
+  std::cerr << "  Usage: integration_test_util convert\n";
+  std::cerr << "           --from [json|ipc] [file or -]\n";
+  std::cerr << "           [[--to [json] [-]] OR [--check [json|ipc] [file or -]]]\n";
+}
+
+class ArgumentParser {

Review Comment:
   I removed this file for now (snapshotted here: https://gist.github.com/paleolimbot/ec2a2067198f0de1901c107c783d3b26 )...`bash` + this is probably not how these tests should run in the repo. I'll do another PR for this...the IPC tests already have something set up to read the IPC files from the testing repo and it might be easiest just to put these tests there.



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat: Add Array/Schema/ArrayStream comparison utility to testing helpers [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot merged PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330


-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417072517


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -1661,6 +1714,305 @@ class TestingJSONReader {
   }
 };
 
+/// \brief Integration testing comparison utility
+///
+/// Utility to compare ArrowSchema, ArrowArray, and ArrowArrayStream instances.
+/// This should only be used in the context of integration testing as the
+/// comparison logic is specific to the integration testing JSON files and
+/// specification. Notably:
+///
+/// - Map types are considered equal regardless of the child names "entries",
+///   "key", and "value".
+/// - Float32 and Float64 values are only compared to 3 decimal places.
+class TestingJSONComparison {
+ private:
+  // Internal representation of a human-readable inequality
+  struct Difference {
+    std::string path;
+    std::string actual;
+    std::string expected;
+  };
+
+ public:
+  /// \brief Returns the number of differences found by the previous call
+  size_t num_differences() const { return differences_.size(); }
+
+  /// \brief Dump a human-readable summary of differences to out
+  void WriteDifferences(std::ostream& out) {
+    for (const auto& difference : differences_) {
+      out << "Path: " << difference.path << "\n";
+      out << "- " << difference.actual << "\n";
+      out << "+ " << difference.expected << "\n";
+      out << "\n";
+    }
+  }
+
+  /// \brief Clear any existing differences
+  void ClearDifferences() { differences_.clear(); }
+
+  /// \brief Compare a stream of record batches
+  ///
+  /// Compares actual against expected using the following strategy:
+  ///
+  /// - Compares schemas for equality, returning if differences were found
+  /// - Compares pairs of record batches, returning if one stream finished
+  ///   before another.
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareArrayStream(ArrowArrayStream* actual, ArrowArrayStream* expected,
+                                    ArrowError* error = nullptr) {
+    // Read both schemas
+    nanoarrow::UniqueSchema actual_schema;
+    nanoarrow::UniqueSchema expected_schema;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_schema(actual, actual_schema.get()),
+                                       error);
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        expected->get_schema(expected, expected_schema.get()), error);
+
+    // Compare them and return if they are not equal
+    NANOARROW_RETURN_NOT_OK(
+        CompareSchema(expected_schema.get(), actual_schema.get(), error, "Schema"));
+    if (num_differences() > 0) {
+      return NANOARROW_OK;
+    }
+
+    // Keep a record of the schema to compare batches
+    NANOARROW_RETURN_NOT_OK(SetSchema(expected_schema.get(), error));
+
+    int64_t n_batches = -1;
+    nanoarrow::UniqueArray actual_array;
+    nanoarrow::UniqueArray expected_array;
+    do {
+      n_batches++;
+      std::string batch_label = std::string("Batch ") + std::to_string(n_batches);
+
+      // Read a batch from each stream
+      actual_array.reset();
+      expected_array.reset();
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(actual->get_next(actual, actual_array.get()),
+                                         error);
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+          expected->get_next(expected, expected_array.get()), error);
+
+      // Check the finished/unfinished status of both streams
+      if (actual_array->release == nullptr && expected_array->release != nullptr) {
+        differences_.push_back({batch_label, "finished stream", "unfinished stream"});
+        return NANOARROW_OK;
+      }
+
+      if (actual_array->release != nullptr && expected_array->release == nullptr) {
+        differences_.push_back({batch_label, "unfinished stream", "finished stream"});
+        return NANOARROW_OK;
+      }
+
+      // If both streams are done, break
+      if (actual_array->release == nullptr) {
+        break;
+      }
+
+      // Compare this batch
+      NANOARROW_RETURN_NOT_OK(
+          CompareBatch(actual_array.get(), expected_array.get(), error, batch_label));
+    } while (true);
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Compare a top-level ArrowSchema struct
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareSchema(const ArrowSchema* actual, const ArrowSchema* expected,
+                               ArrowError* error = nullptr,
+                               const std::string& path = "") {
+    // Compare the top-level schema "manually" because (1) map type needs special-cased
+    // comparison and (2) it's easier to read the output if differences are separated
+    // by field.
+    ArrowSchemaView actual_view;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaViewInit(&actual_view, actual, nullptr),
+                                       error);
+
+    ArrowSchemaView expected_view;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        ArrowSchemaViewInit(&expected_view, expected, nullptr), error);
+
+    if (actual_view.type != NANOARROW_TYPE_STRUCT ||
+        expected_view.type != NANOARROW_TYPE_STRUCT) {
+      ArrowErrorSet(error, "Top-level schema must be struct");
+      return EINVAL;
+    }
+
+    // (Purposefully ignore the name field at the top level)
+
+    // Compare flags
+    if (actual->flags != expected->flags) {
+      differences_.push_back({path,
+                              std::string(".flags: ") + std::to_string(actual->flags),
+                              std::string(".flags: ") + std::to_string(expected->flags)});
+    }
+
+    // Compare children
+    if (actual->n_children != expected->n_children) {
+      differences_.push_back(
+          {path, std::string(".n_children: ") + std::to_string(actual->n_children),
+           std::string(".n_children: ") + std::to_string(expected->n_children)});
+    } else {
+      for (int64_t i = 0; i < expected->n_children; i++) {
+        NANOARROW_RETURN_NOT_OK(CompareField(
+            actual->children[i], expected->children[i], error,
+            path + std::string(".children[") + std::to_string(i) + std::string("]")));
+      }
+    }
+
+    // Compare metadata
+    std::stringstream ss;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteMetadata(ss, actual->metadata),
+                                       error);
+    std::string actual_metadata = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteMetadata(ss, expected->metadata),
+                                       error);
+    std::string expected_metadata = ss.str();
+
+    if (actual_metadata != expected_metadata) {
+      differences_.push_back({path, std::string(".metadata: ") + actual_metadata,
+                              std::string(".metadata: ") + expected_metadata});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Set the ArrowSchema to be used to for future calls to CompareBatch().
+  ArrowErrorCode SetSchema(const ArrowSchema* schema, ArrowError* error = nullptr) {
+    schema_.reset();
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaDeepCopy(schema, schema_.get()), error);
+    actual_.reset();
+    expected_.reset();
+
+    NANOARROW_RETURN_NOT_OK(
+        ArrowArrayViewInitFromSchema(actual_.get(), schema_.get(), error));
+    NANOARROW_RETURN_NOT_OK(
+        ArrowArrayViewInitFromSchema(expected_.get(), schema_.get(), error));
+
+    if (actual_->storage_type != NANOARROW_TYPE_STRUCT) {
+      ArrowErrorSet(error, "Can't SetSchema() with non-struct");
+      return EINVAL;
+    }
+
+    return NANOARROW_OK;
+  }
+
+  /// \brief Compare a top-level ArrowArray struct
+  ///
+  /// Returns NANOARROW_OK if the comparison ran without error. Callers must
+  /// query num_differences() to obtain the result of the comparison on success.
+  ArrowErrorCode CompareBatch(const ArrowArray* actual, const ArrowArray* expected,
+                              ArrowError* error = nullptr, const std::string& path = "") {
+    NANOARROW_RETURN_NOT_OK(ArrowArrayViewSetArray(expected_.get(), expected, error));
+    NANOARROW_RETURN_NOT_OK(ArrowArrayViewSetArray(actual_.get(), actual, error));
+
+    if (actual->offset != expected->offset) {
+      differences_.push_back({path, ".offset: " + std::to_string(actual->offset),
+                              ".offset: " + std::to_string(expected->offset)});
+    }
+
+    if (actual->length != expected->length) {
+      differences_.push_back({path, ".length: " + std::to_string(actual->length),
+                              ".length: " + std::to_string(expected->length)});
+    }
+
+    // ArrowArrayViewSetArray() ensured that number of children of both match schema
+    for (int64_t i = 0; i < expected_->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(CompareColumn(
+          schema_->children[i], actual_->children[i], expected_->children[i], error,
+          path + std::string(".children[") + std::to_string(i) + "]"));
+    }
+
+    return NANOARROW_OK;
+  }
+
+ private:
+  TestingJSONWriter writer_;
+  std::vector<Difference> differences_;
+  nanoarrow::UniqueSchema schema_;
+  nanoarrow::UniqueArrayView actual_;
+  nanoarrow::UniqueArrayView expected_;
+
+  ArrowErrorCode CompareField(ArrowSchema* actual, ArrowSchema* expected,
+                              ArrowError* error, const std::string& path = "") {
+    // Preprocess both fields such that map types have canonical names
+    nanoarrow::UniqueSchema actual_copy;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaDeepCopy(actual, actual_copy.get()),
+                                       error);
+    nanoarrow::UniqueSchema expected_copy;
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaDeepCopy(expected, expected_copy.get()),
+                                       error);
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ForceMapNamesCanonical(actual_copy.get()), error);
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ForceMapNamesCanonical(expected_copy.get()),
+                                       error);
+    return CompareFieldBase(actual_copy.get(), expected_copy.get(), error, path);
+  }
+
+  ArrowErrorCode CompareFieldBase(ArrowSchema* actual, ArrowSchema* expected,
+                                  ArrowError* error, const std::string& path = "") {
+    std::stringstream ss;
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteField(ss, expected), error);
+    std::string expected_json = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteField(ss, actual), error);
+    std::string actual_json = ss.str();
+
+    if (actual_json != expected_json) {
+      differences_.push_back({path, actual_json, expected_json});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode CompareColumn(ArrowSchema* schema, ArrowArrayView* actual,
+                               ArrowArrayView* expected, ArrowError* error,
+                               const std::string& path = "") {
+    std::stringstream ss;
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteColumn(ss, schema, expected), error);
+    std::string expected_json = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_.WriteColumn(ss, schema, actual), error);
+    std::string actual_json = ss.str();
+
+    if (actual_json != expected_json) {
+      differences_.push_back({path, actual_json, expected_json});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode ForceMapNamesCanonical(ArrowSchema* schema) {
+    ArrowSchemaView view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&view, schema, nullptr));
+
+    if (view.type == NANOARROW_TYPE_MAP) {
+      NANOARROW_RETURN_NOT_OK(ArrowSchemaSetName(schema->children[0], "entries"));
+      NANOARROW_RETURN_NOT_OK(
+          ArrowSchemaSetName(schema->children[0]->children[0], "key"));
+      NANOARROW_RETURN_NOT_OK(
+          ArrowSchemaSetName(schema->children[0]->children[1], "value"));
+    }
+
+    for (int64_t i = 0; i < schema->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(ForceMapNamesCanonical(schema->children[i]));
+    }

Review Comment:
   Recurse into dictionary as well?



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417061053


##########
extensions/nanoarrow_ipc/src/apps/integration_test_util.cc:
##########
@@ -0,0 +1,219 @@
+// 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 <cerrno>
+#include <deque>
+#include <iostream>
+#include <string>
+#include <unordered_map>
+#include <utility>
+
+#include "nanoarrow/nanoarrow_ipc.hpp"
+#include "nanoarrow/nanoarrow_testing.hpp"
+
+void print_help() {
+  std::cerr << "nanoarrow version " << ArrowNanoarrowVersion() << "\n";
+  std::cerr << "  Usage: integration_test_util convert\n";
+  std::cerr << "           --from [json|ipc] [file or -]\n";
+  std::cerr << "           [[--to [json] [-]] OR [--check [json|ipc] [file or -]]]\n";
+}
+
+class ArgumentParser {

Review Comment:
   Not saying you should change this, but there are multiple header-only C++ libraries available for argument parsing such as https://github.com/jarro2783/cxxopts , https://github.com/jermp/cmd_line_parser , https://github.com/tanakh/cmdline or https://github.com/p-ranav/argparse .
   
   You could also write this in Python and plug into the underlying C code using `ctypes` or `cffi`. This would allow yanking the separate shell script.



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#discussion_r1417599551


##########
extensions/nanoarrow_ipc/scripts/test_integration_test_util.sh:
##########
@@ -0,0 +1,86 @@
+# 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.
+
+if [ ${VERBOSE:-0} -gt 0 ]; then

Review Comment:
   See below, I removed this for now and will do another PR. This was useful for ensuring I had all the pieces required, but this is probably not how the tests should run in the repo.



-- 
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-unsubscribe@arrow.apache.org

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


Re: [PR] feat(extensions/nanoarrow_ipc): Add integration testing utility executable [arrow-nanoarrow]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #330:
URL: https://github.com/apache/arrow-nanoarrow/pull/330#issuecomment-1839382020

   ## [Codecov](https://app.codecov.io/gh/apache/arrow-nanoarrow/pull/330?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   All modified and coverable lines are covered by tests :white_check_mark:
   > Comparison is base [(`4744498`)](https://app.codecov.io/gh/apache/arrow-nanoarrow/commit/4744498daf166bc3ae8d34d157a2598002447d4e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) 87.83% compared to head [(`ecbeb37`)](https://app.codecov.io/gh/apache/arrow-nanoarrow/pull/330?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) 89.10%.
   > Report is 3 commits behind head on main.
   
   
   <details><summary>Additional details and impacted files</summary>
   
   
   ```diff
   @@            Coverage Diff             @@
   ##             main     #330      +/-   ##
   ==========================================
   + Coverage   87.83%   89.10%   +1.27%     
   ==========================================
     Files          72        4      -68     
     Lines       11114      101   -11013     
   ==========================================
   - Hits         9762       90    -9672     
   + Misses       1352       11    -1341     
   ```
   
   
   
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/arrow-nanoarrow/pull/330?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   


-- 
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-unsubscribe@arrow.apache.org

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