Skip to content

fix: Allow a StructType with no fields - #514

Merged
CurtHagenlocher merged 3 commits into
apache:mainfrom
CurtHagenlocher:fix/empty-struct-type
Nov 27, 2026
Merged

CurtHagenlocher merged 3 commits into
apache:mainfrom
CurtHagenlocher:fix/empty-struct-type

Conversation

@CurtHagenlocher

@CurtHagenlocher CurtHagenlocher commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What's Changed

The Arrow format allows a struct, or a union, with no children. pyarrow's struct<> is an example. Apache.Arrow refused these in three places, and this PR fixes all three:

  • NestedType: the list constructor treated an empty field list like null and threw ArgumentNullException("fields"). It now throws only for null. StructType and UnionType are the only types that use this constructor.
  • IPC reader (MessageSerializer): a field with no children has childFields == null. The Struct_ and Union cases now build the type with no children instead of asserting that childFields is non-null.
  • C data interface (CArrowSchemaExporter): a nested type with zero children is now exported as n_children = 0 with a null children pointer, which the C data interface allows. It used to throw NotSupportedException.

Because the type change also allows a union with no children, two union paths needed fixing as well (from review):

  • C data import (CArrowSchemaImporter): the empty type-id list that the exporter writes (+us: / +ud:) failed to import, because the parser always read at least one code.
  • IPC reader: a union's typeIds is optional in Schema.fbs, and without it the ids are the children's offsets. Reading a union without the vector threw ArgumentNullException, with or without children. It now defaults to 0..n-1.

The new EmptyStructTests cover:

  • the type and the array, including slicing and validity
  • round-trips through the IPC stream format, the IPC file format and the C data interface, both as a top-level column and as the item type of a list
  • concatenation
  • an empty union through the C data interface and the IPC stream format, in both modes
  • a schema flatbuffer whose union omits typeIds, with zero and with two children

EmptyStructPythonTests (in the PythonNet collection, which CI runs against pyarrow) checks interop with pyarrow in both directions:

  • a batch with struct<>, list<struct<>> and an int column through the C data interface, pyarrow to C# and C# to pyarrow (pyarrow validates with full=True and compares schema and values)
  • the same batch through the IPC stream format, written by each side and read by the other
  • sparse_union<> and dense_union<> through the C data interface, both directions

All eight fail against main's source and pass with this change (locally with pyarrow 24, on net8.0, net462 and net472).

Closes #513.

🤖 Generated with Claude Code

The Arrow format allows a struct (and a union) with no children, but
NestedType's list constructor refused an empty list as if it were null, the
IPC reader asserted that a struct field had children, and the C data
interface exporter threw for a nested type with zero children.

An empty field list is now accepted, the IPC reader builds a struct or union
with no children from a field with none, and the C exporter exports
n_children = 0 with a null children pointer. Round-trips through the IPC
stream and file formats and the C data interface, as a column and inside a
list, are covered by tests; interop with pyarrow 24 was checked by hand in
both directions.

Closes apache#513.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty unions still fail IPC deserialization when optional type IDs are omitted and C schema import when exported with an empty type-ID list.

2 open issues
What changed in this PR

This PR addresses #513 by allowing zero-field structs in Apache.Arrow, supporting representations such as empty Avro records and interoperability with other Arrow implementations.

Changes:

  • Allow empty field collections for structs and unions while still rejecting null.
  • Handle childless types in IPC deserialization and C schema export.
  • Add empty-struct tests for validity, slicing, concatenation, and round-trips.
File Description
test/​Apache.Arrow.Tests/​EmptyStructTests.cs Adds empty-struct behavior and interoperability tests, plus empty-union construction coverage.
src/​Apache.Arrow/​Types/​NestedType.cs Permits empty field collections and documents the behavior.
src/​Apache.Arrow/​Ipc/​MessageSerializer.cs Supplies empty child arrays when reading childless structs and unions.
src/​Apache.Arrow/​C/​CArrowSchemaExporter.cs Exports zero-child nested types with a null children pointer.

Review effort: Balanced

Addresses review on apache#514:
- CArrowSchemaImporter parsed a union's type-id list with a do/while, so
  the "+us:" / "+ud:" that the exporter writes for a union with no
  children failed with InvalidDataException. An empty list now imports.
- The IPC reader passed a union's typeIds straight to UnionType, but the
  schema makes typeIds optional: without it, the ids in the type vector
  are the children's offsets. Reading a union without the vector threw
  ArgumentNullException, with or without children; it now defaults to
  0..n-1.

Tests: C schema round-trip of an empty union in both modes, IPC
round-trip of an empty union column, and a hand-built schema with the
typeIds vector omitted, with zero and two children.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CurtHagenlocher
CurtHagenlocher requested a lite review from Copilot November 26, 2026 20:30
Interop with pyarrow was only checked by hand. EmptyStructPythonTests runs
it in the PythonNet collection, which CI runs against pyarrow:

- a batch with struct<>, list<struct<>> and an int column, pyarrow to C#
  and C# to pyarrow through the C data interface (pyarrow validates with
  full=True and compares schema and values);
- the same batch through the IPC stream format, written by each side and
  read by the other;
- sparse_union<> and dense_union<> through the C data interface, both
  directions.

All eight fail against main's source and pass with this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The supplied review evidence reports no active issues for the change adding support for empty nested Arrow types.

No open issues 🎉

2 resolved since last review

Review effort: Lite

@CurtHagenlocher
CurtHagenlocher merged commit aad9dc1 into apache:main Nov 27, 2026
14 checks passed
@CurtHagenlocher
CurtHagenlocher deleted the fix/empty-struct-type branch November 27, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A StructType with no fields is refused, and cannot be read over IPC or exported over the C data interface

2 participants