This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new e352c4d6 fix(ipc): Write the actual null count to each FieldNode (#974)
e352c4d6 is described below

commit e352c4d6e01b70e7b0bad1973c9ad52f2f4ced00
Author: Rusty Conover <[email protected]>
AuthorDate: Sun Oct 18 23:28:53 2026 -0400

    fix(ipc): Write the actual null count to each FieldNode (#974)
    
    `ArrowIpcEncoderCollectArray()` copied `ArrowArrayView.null_count` into
    each `FieldNode` as it was. That is wrong in two cases:
    
    - **Unknown null count.** In the C data interface, `null_count` may be
    -1 when it hasn't been computed. A zero-copy slice often looks like
    this. The IPC format has no such value: `Message.fbs` documents
    `FieldNode.null_count` as "the number of observed nulls", and
    `Columnar.rst` treats the null count as a property of the array that is
    "as large as the array length". The encoder wrote `null_count: -1`.
    - **Null type.** Every slot of a null array is null, so its null count
    is its length. Arrow C++ enforces this (`ValidateArray` reports "Null
    array null_count unequal to its length", and `NullArray` forces
    `null_count = length`). A producer may still leave a null array's
    `null_count` at 0, and the encoder wrote that 0.
    
    The encoder now writes the length for a null array. For other types it
    computes the null count with `ArrowArrayViewComputeNullCount()` when the
    count is negative. The change is in `ArrowIpcEncoderCollectArray()`,
    which also collects dictionary values, so it applies to
    `DictionaryBatch` messages as well. The top-level record batch check
    already handled -1.
    
    I found this when a DuckDB extension started writing its IPC with
    nanoarrow and Apache.Arrow for .NET read the stream. Arrow C++ happens
    to accept both cases: it maps -1 to `kUnknownNullCount` and recomputes
    it, and `NullArray` overwrites the count. Apache.Arrow 23.0.0 (.NET)
    does not:
    
    - `null_count: -1` throws `InvalidDataException: Null count of -1 must
    be >= 0 and <= 2147483647`. Older versions read the column as having no
    nulls, so NULLs were silently lost.
    - A null child with `null_count: 0` throws `ArgumentException: Length
    must equal null count` from `NullArray..ctor`.
    
    With this change, both batches read correctly in .NET.
    
    Tests: `NanoarrowIpcEncoderWritesActualNullCounts` encodes `struct<i:
    int32, n: null>` with `i = [1, null, 3]`, `i.null_count = -1` and
    `n.null_count = 0`. It decodes the message and checks the field-node
    null counts, which must be 1 and 3. On `main` the test fails with -1 and
    0. `ctest` passes (Linux aarch64, Debug, 393 tests), including under
    ASan/UBSan with clang. The changed files are formatted with clang-format
    16.0.6, the pre-commit version.
    
    This PR's code and description were written by AI (Claude Code) and
    reviewed by a human maintainer.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
---
 src/nanoarrow/ipc/encoder.c       | 11 ++++++-
 src/nanoarrow/ipc/encoder_test.cc | 68 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/src/nanoarrow/ipc/encoder.c b/src/nanoarrow/ipc/encoder.c
index ab7f0b60..2fe14713 100644
--- a/src/nanoarrow/ipc/encoder.c
+++ b/src/nanoarrow/ipc/encoder.c
@@ -970,7 +970,16 @@ static ArrowErrorCode ArrowIpcEncoderEncodeBodyCompression(
 static ArrowErrorCode ArrowIpcEncoderCollectArray(struct 
ArrowIpcEncoderPrivate* private,
                                                   const struct ArrowArrayView* 
array_view,
                                                   struct ArrowError* error) {
-  struct ns(FieldNode) node = {array_view->length, array_view->null_count};
+  // FieldNode.null_count is the number of nulls. The C data interface's 
"unknown"
+  // null count (-1) can't be written, and every element of a null array is 
null.
+  int64_t null_count = array_view->null_count;
+  if (array_view->storage_type == NANOARROW_TYPE_NA) {
+    null_count = array_view->length;
+  } else if (null_count < 0) {
+    null_count = ArrowArrayViewComputeNullCount(array_view);
+  }
+
+  struct ns(FieldNode) node = {array_view->length, null_count};
   NANOARROW_RETURN_NOT_OK_WITH_ERROR(
       ArrowBufferAppend(&private->nodes, &node, sizeof(node)), error);
   for (int64_t b = 0; b < array_view->array->n_buffers; ++b) {
diff --git a/src/nanoarrow/ipc/encoder_test.cc 
b/src/nanoarrow/ipc/encoder_test.cc
index 00fd278e..b6e7e4ac 100644
--- a/src/nanoarrow/ipc/encoder_test.cc
+++ b/src/nanoarrow/ipc/encoder_test.cc
@@ -705,6 +705,74 @@ static void TestCompressedRecordBatchRoundtrip(enum 
ArrowIpcCompressionType code
   EXPECT_EQ(decoder->codec, NANOARROW_IPC_COMPRESSION_TYPE_NONE);
 }
 
+TEST(NanoarrowIpcTest, NanoarrowIpcEncoderWritesActualNullCounts) {
+  // A record batch of struct<i: int32, n: null> with three rows: i = [1, 
null, 3]
+  struct ArrowError error;
+  nanoarrow::UniqueSchema schema;
+  ASSERT_EQ(ArrowSchemaInitFromType(schema.get(), NANOARROW_TYPE_STRUCT), 
NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaAllocateChildren(schema.get(), 2), NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaInitFromType(schema->children[0], NANOARROW_TYPE_INT32),
+            NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaSetName(schema->children[0], "i"), NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaInitFromType(schema->children[1], NANOARROW_TYPE_NA),
+            NANOARROW_OK);
+  ASSERT_EQ(ArrowSchemaSetName(schema->children[1], "n"), NANOARROW_OK);
+
+  nanoarrow::UniqueArray array;
+  ASSERT_EQ(ArrowArrayInitFromSchema(array.get(), schema.get(), &error), 
NANOARROW_OK);
+  ASSERT_EQ(ArrowArrayStartAppending(array.get()), NANOARROW_OK);
+  for (int32_t i = 1; i <= 3; ++i) {
+    if (i == 2) {
+      ASSERT_EQ(ArrowArrayAppendNull(array->children[0], 1), NANOARROW_OK);
+    } else {
+      ASSERT_EQ(ArrowArrayAppendInt(array->children[0], i), NANOARROW_OK);
+    }
+    ASSERT_EQ(ArrowArrayAppendNull(array->children[1], 1), NANOARROW_OK);
+    ASSERT_EQ(ArrowArrayFinishElement(array.get()), NANOARROW_OK);
+  }
+  ASSERT_EQ(ArrowArrayFinishBuildingDefault(array.get(), &error), 
NANOARROW_OK);
+
+  // A producer may leave the null count unknown (-1), and may not count the
+  // elements of a null array as nulls
+  array->children[0]->null_count = -1;
+  array->children[1]->null_count = 0;
+
+  nanoarrow::UniqueArrayView array_view;
+  ASSERT_EQ(ArrowArrayViewInitFromSchema(array_view.get(), schema.get(), 
&error),
+            NANOARROW_OK);
+  ASSERT_EQ(ArrowArrayViewSetArray(array_view.get(), array.get(), &error), 
NANOARROW_OK)
+      << error.message;
+
+  nanoarrow::ipc::UniqueEncoder encoder;
+  ASSERT_EQ(ArrowIpcEncoderInit(encoder.get()), NANOARROW_OK);
+  nanoarrow::UniqueBuffer message, body;
+  ASSERT_EQ(ArrowIpcEncoderEncodeSimpleRecordBatch(encoder.get(), 
array_view.get(),
+                                                   body.get(), &error),
+            NANOARROW_OK)
+      << error.message;
+  ASSERT_EQ(
+      ArrowIpcEncoderFinalizeBuffer(encoder.get(), /*encapsulate=*/true, 
message.get()),
+      NANOARROW_OK);
+
+  // The decoded array view carries the null counts from the message's field 
nodes
+  nanoarrow::ipc::UniqueDecoder decoder;
+  ASSERT_EQ(ArrowIpcDecoderInit(decoder.get()), NANOARROW_OK);
+  ASSERT_EQ(ArrowIpcDecoderSetSchema(decoder.get(), schema.get(), &error), 
NANOARROW_OK)
+      << error.message;
+  struct ArrowBufferView message_view = {{message->data}, message->size_bytes};
+  ASSERT_EQ(ArrowIpcDecoderDecodeHeader(decoder.get(), message_view, &error),
+            NANOARROW_OK)
+      << error.message;
+  struct ArrowArrayView* decoded;
+  struct ArrowBufferView body_view = {{body->data}, body->size_bytes};
+  ASSERT_EQ(
+      ArrowIpcDecoderDecodeArrayView(decoder.get(), body_view, -1, &decoded, 
&error),
+      NANOARROW_OK)
+      << error.message;
+  EXPECT_EQ(decoded->children[0]->null_count, 1);
+  EXPECT_EQ(decoded->children[1]->null_count, 3);
+}
+
 TEST(NanoarrowIpcTest, NanoarrowIpcEncoderUncompressedRecordBatchAllocation) {
   nanoarrow::ipc::UniqueEncoder encoder;
   ASSERT_EQ(ArrowIpcEncoderInit(encoder.get()), NANOARROW_OK);

Reply via email to