Skip to content

fix: Reject struct arrays with different fields in ArrayDataConcatenator - #516

Merged
CurtHagenlocher merged 3 commits into
apache:mainfrom
dragonknightk:fix/500
Dec 2, 2026
Merged

CurtHagenlocher merged 3 commits into
apache:mainfrom
dragonknightk:fix/500

Conversation

@dragonknightk

@dragonknightk dragonknightk commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What's Changed

ArrayDataConcatenator now validates nested field compatibility, includes empty nested inputs in child validation, and updates outer result types when a child type changes.

  • Reject mismatched struct and union fields (field count, name, and nullability; plus mode and type ids for unions) instead of dropping or swapping children by position.
  • Reject field nullability mismatches across all other field-bearing containers (lists, list views, fixed-size lists, maps, and run-end encoded arrays), matching Arrow C++.
  • Preserve the first input's item field name for list-like containers, and validate map entries directly in Visit(MapType) so non-default entry field names still concatenate while struct values are checked normally.
  • Pass zero-length child slices for empty inputs in list-like, map, dense-union, run-end encoded, and dictionary arrays so empty+non-empty and all-empty inputs go through recursive validation and widening without merging unused child payloads, appending unused dictionary entries, or downgrading shared sorted map dictionaries.
  • Rebuild the outer container type whenever a concatenated child resolves to a different type (such as widened decimal precision, an unsorted map, or an unordered dictionary), reusing the original type otherwise.

Tests cover mismatched struct and union fields, nullability mismatches across all field-bearing containers, empty nested mismatches (including map keys and both input orders), empty dense-union slices with populated backing children, outer type updates, shared dictionary preservation, custom list/map field names, and zero allocator leaks on exception paths.

Closes #500.

AI Disclosure

Grok Bot, working for dragonknightk, assisted with the changes, the description, and the verification. All content was reviewed by a human, and the submitter understands and can maintain these changes.

## What's Changed

`ArrowArrayConcatenator.Concatenate` checked only the type id and buffer count before concatenating struct arrays. It then combined the children by position and typed the result with the first input's `StructType`, so mismatched fields were dropped, mislabeled, or silently swapped.

Now `Concatenate` throws `ArgumentException` when the struct inputs differ in field count, name, order or nullability, the same way it already does for `FixedSizeList` sizes and decimal scales. The check runs for every struct level, so structs nested in lists and other structs are covered. Child types are still checked when the children are concatenated. Field metadata is not compared. A map's entries struct is still not checked for field names or nullability, so maps that differ only there still concatenate. A struct inside a map value is checked like any other struct.

Union arrays follow the same pattern and are not changed here.

The new tests cover each kind of mismatch above, a third input that differs from the first two, and a mismatch in a struct nested in a list. Maps whose value nullability differs still concatenate, and maps whose value structs differ in field names are rejected. The mismatch cases fail on the old code.

Closes issue 500

Signed-off-by: dragonknightk <37928252+dragonknightk@users.noreply.github.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

Nested struct compatibility checks miss empty inputs, allowing concatenation to produce inconsistent parent and child schemas across lists, list views, and map values.

1 open issue
What changed in this PR

This PR addresses #500 by adding struct-field compatibility checks to array concatenation, preventing mismatched fields from being silently dropped or mislabeled.

Changes:

  • Reject struct inputs with different field counts, names, order, or nullability.
  • Preserve the map-entry name/nullability exemption while checking nested value structs.
  • Add regression tests for field mismatches, later inputs, nested lists, and map behavior.
File Description
test/​Apache.Arrow.Tests/​ArrowArrayConcatenatorTests.cs Adds struct mismatch and map compatibility regression tests.
src/​Apache.Arrow/​Arrays/​ArrayDataConcatenator.cs Adds struct-field validation and preserves the map-entry exemption.

Review effort: Balanced

@CurtHagenlocher CurtHagenlocher left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two other things I think are still not working:

  1. The result's type always comes from the first input, even when a child type was widened.
  2. Nullability is checked for struct fields only, so concatenating a list<int not null> with a list<int null> that contains a null will succeed and the result declares the items are non-nullable even though they contain a null. For nullability, I think we should either reject a mismatch (I think this is what the C++ implementation does) or make the result nullable if any of the inputs are.

…aConcatenator

## What's Changed

`ArrayDataConcatenator` now validates nested field compatibility, includes empty nested inputs in child validation, and updates outer result types when a child type changes.

- Reject mismatched struct and union fields (field count, name, and nullability; plus mode and type ids for unions) instead of dropping or swapping children by position.
- Reject field nullability mismatches across all other field-bearing containers (lists, list views, fixed-size lists, maps, and run-end encoded arrays), matching Arrow C++.
- Preserve the first input's item field name for list-like containers, and validate map entries directly in `Visit(MapType)` so non-default entry field names still concatenate while struct values are checked normally.
- Pass zero-length child slices for empty inputs in list-like, map, run-end encoded, and dictionary arrays so empty+non-empty and all-empty inputs go through recursive validation and widening without appending unused dictionary entries or downgrading shared sorted map dictionaries.
- Rebuild the outer container type whenever a concatenated child resolves to a different type (such as widened decimal precision, an unsorted map, or an unordered dictionary), reusing the original type otherwise.

Tests cover mismatched struct and union fields, nullability mismatches across all field-bearing containers, empty nested mismatches (including map keys and both input orders), outer type updates, shared dictionary preservation, custom list/map field names, and zero allocator leaks on exception paths.

Closes issue 500

Signed-off-by: DragonKnightK <37928252+dragonknightk@users.noreply.github.com>

@dragonknightk dragonknightk left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@CurtHagenlocher Thanks for the thorough review - those were really good catches.

I agreed with all of them, especially that the empty-input gap, stale outer types, and nullability mismatch aren't limited to structs. For nullability, I went with rejecting mismatches across all field-bearing containers to match Arrow C++ (while keeping the first input's child field name for list-like containers so conventional names like item and element still work), rather than silently widening or risking a non-nullable array that holds nulls.

A quick note on why the diff grew quite a bit from the first version: fixing empty-input validation, outer type propagation, and field nullability consistently instead of patching only structs and lists touches every nested container in ArrayDataConcatenator (List/LargeList, ListView/LargeListView, FixedSizeList, Map, Struct, Union, RunEndEncoded, and Dictionary). While doing that, I kept the existing type checks in place, added cleanup on exception paths so partially concatenated children don't leak allocator memory, and made sure shared sorted-map dictionaries aren't downgraded or copied unnecessarily. Over half of the added lines are tests covering those cases across the container types.

Hopefully this puts all of the nested concatenation edge cases on a solid footing. PTAL!

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 dense-union slices still include populated backing children, causing unnecessary dictionary merges that can overflow int8 indices and throw OverflowException.

1 open issue
1 resolved since last review

Review effort: Balanced

…aConcatenator

## What's Changed

`ArrayDataConcatenator` now validates nested field compatibility, includes empty nested inputs in child validation, and updates outer result types when a child type changes.

- Reject mismatched struct and union fields (field count, name, and nullability; plus mode and type ids for unions) instead of dropping or swapping children by position.
- Reject field nullability mismatches across all other field-bearing containers (lists, list views, fixed-size lists, maps, and run-end encoded arrays), matching Arrow C++.
- Preserve the first input's item field name for list-like containers, and validate map entries directly in `Visit(MapType)` so non-default entry field names still concatenate while struct values are checked normally.
- Pass zero-length child slices for empty inputs in list-like, map, dense-union, run-end encoded, and dictionary arrays so empty+non-empty and all-empty inputs go through recursive validation and widening without merging unused child payloads, appending unused dictionary entries, or downgrading shared sorted map dictionaries.
- Rebuild the outer container type whenever a concatenated child resolves to a different type (such as widened decimal precision, an unsorted map, or an unordered dictionary), reusing the original type otherwise.

Tests cover mismatched struct and union fields, nullability mismatches across all field-bearing containers, empty nested mismatches (including map keys and both input orders), empty dense-union slices with populated backing children, outer type updates, shared dictionary preservation, custom list/map field names, and zero allocator leaks on exception paths.

Closes issue 500

Signed-off-by: DragonKnightK <37928252+dragonknightk@users.noreply.github.com>

@CurtHagenlocher CurtHagenlocher left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

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.

ArrayDataConcatenator joins struct arrays whose field names, order, nullability or count differ

3 participants