Skip to content

GH-14736: [C++] Propagate struct validity into union children - #51017

Open
Hei-MaoM wants to merge 1 commit into
apache:mainfrom
Hei-MaoM:codex/gh-14736-union-flatten-validity
Open

GH-14736: [C++] Propagate struct validity into union children#51017
Hei-MaoM wants to merge 1 commit into
apache:mainfrom
Hei-MaoM:codex/gh-14736-union-flatten-validity

Conversation

@Hei-MaoM

@Hei-MaoM Hei-MaoM commented Aug 27, 2026

Copy link
Copy Markdown

Rationale for this change

StructArray::GetFlattenedField currently combines a struct's validity
bitmap with a child by assigning it to buffer 0. Union arrays do not have
top-level validity buffers, so flattening a union field aborts even when
the parent bitmap is all-valid. When the parent contains nulls, that
validity must instead be represented by the union's child arrays.

What changes are included in this PR?

  • Preserve the union's null top-level validity buffer and its type codes.
  • Apply parent validity to every aligned child of a sparse union.
  • Rebuild dense-union child segments in logical order so parent-null slots
    are null without invalidating valid slots that share the same original
    child offset.
  • Preserve non-decreasing dense-union offsets and handle sliced arrays.
  • Add sparse and dense regression tests.

Are these changes tested?

  • cmake --build /build/cpp --target arrow-array-test -j 4
  • /build/cpp/debug/arrow-array-test --gtest_filter=StructArray.FlattenSparseUnion:StructArray.FlattenDenseUnionWithSharedOffsets --gtest_brief=1
  • /build/cpp/debug/arrow-array-test --gtest_brief=1
    • 1062 passed
    • 1 skipped because the local build does not enable the required threading configuration
  • clang-format 18.1.8
  • cpplint 1.6.1
  • git diff --check

Are there any user-facing changes?

Yes. StructArray::GetFlattenedField and callers such as struct_field
and Flatten no longer crash when selecting a union field from a nullable
struct. Parent nulls are represented in the appropriate union children.
There are no public API changes.

This PR contains a "Critical Fix". It fixes a crash on valid input.

Closes #14736.

AI assistance disclosure

I used OpenAI Codex to help investigate the issue, draft parts of the
implementation and tests, and run verification. I reviewed the final diff
and am responsible for understanding, debugging, and maintaining the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] "struct_field" kernel crashing when selecting a Union field and parent StructArray has a validity bitmap

1 participant