Skip to content

fix(arrow/compute): allow casting nullable struct fields to non-nullable when no nulls - #1363

Merged
zeroshade merged 1 commit into
apache:mainfrom
sankalpsthakur:fix/133-struct-cast-nullable
Oct 13, 2026
Merged

zeroshade merged 1 commit into
apache:mainfrom
sankalpsthakur:fix/133-struct-cast-nullable

Conversation

@sankalpsthakur

Copy link
Copy Markdown
Contributor

Rationale for this change

Casting a struct with a nullable field to a struct type where that field is non-nullable always failed with arrow.ErrType, even when the field held no nulls. The C++ implementation only rejects the cast when the field actually has nulls (scalar_cast_nested.cc).

What changes are included in this PR?

CastStruct no longer rejects nullable to non-nullable fields while matching field names. It checks each selected field's null count at cast time instead, and returns arrow.ErrInvalid with the same message as C++ when the field has nulls.

Are these changes tested?

Yes. TestStructToDifferentNullabilityStruct now covers a field with no nulls (cast succeeds), fields with nulls (ErrInvalid), and a slice that skips the null rows (cast succeeds).

  • go test ./arrow/compute/...: ok
  • go vet ./arrow/compute/... and gofmt -l arrow/compute: clean

Are there any user-facing changes?

Yes. These casts now succeed when the field has no nulls. When it has nulls, the error is now arrow.ErrInvalid instead of arrow.ErrType, matching C++.

Fixes #133

AI/LLM disclosure

  • AI coding tools (Claude Code) were used to write this change, the test updates and this description.
  • The change was reviewed and the commands above were run locally before submitting.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM — this matches the C++ struct cast (scalar_cast_nested.cc): reject only when a nullable source field actually has nulls, with the same Invalid message. Locally the full arrow/compute/... suite passes, and a checked-allocator probe of the new error path (failure on the first and on a later field) shows no leaks.

Smaller observations

  • arrow/compute/cast_test.go — the successful cases (out3, out4) only assert length/no error; consider asserting the cast output (e.g. compare against the expected struct via array.Equal) and that the result's field is non-nullable.
  • The error type changes from arrow.ErrType to arrow.ErrInvalid for this case; callers matching errors.Is(err, arrow.ErrType) will see different behavior, so it's worth calling out in the release notes.

This review was drafted by an AI-assisted tool and
confirmed by an Apache Arrow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Arrow handles maintainer review:
CONTRIBUTING.md.

…ble when no nulls

Casting a struct whose field is nullable to a struct whose matching field is
non-nullable always failed with ErrType, even when the field held no nulls.
Check the field's null count at cast time instead, as the C++ implementation
does: the cast succeeds when the field has no nulls and returns ErrInvalid
when it does.
@zeroshade
zeroshade force-pushed the fix/133-struct-cast-nullable branch from 15d3b0e to a44d190 Compare October 13, 2026 13:43
@zeroshade
zeroshade merged commit 85916dd into apache:main Oct 13, 2026
23 checks passed
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.

structs: Support casting nullable fields to non-nullable if there are no null values

2 participants