Skip to content

GH-51029: [C++] Fix MapArray validation with unknown null count - #51093

Open
AnuragRaut08 wants to merge 2 commits into
apache:mainfrom
AnuragRaut08:fix/maparray-cast-validity
Open

GH-51029: [C++] Fix MapArray validation with unknown null count#51093
AnuragRaut08 wants to merge 2 commits into
apache:mainfrom
AnuragRaut08:fix/maparray-cast-validity

Conversation

@AnuragRaut08

@AnuragRaut08 AnuragRaut08 commented Aug 31, 2026

Copy link
Copy Markdown

Rationale for this change

MapArray validation uses MayHaveNulls() to check that the map child and keys contain no nulls. When an all-valid validity bitmap is present with an unknown null count, MayHaveNulls() can report that the array may contain nulls even though all values are valid.

During a cast of a MapArray, this can cause validation to return an invalid status which is passed to ARROW_CHECK_OK, resulting in a process abort instead of a recoverable error.

What changes are included in this PR?

  • Use GetNullCount() instead of MayHaveNulls() when validating the MapArray child and keys.
  • Add a regression test covering an all-valid validity bitmap with kUnknownNullCount.
  • Cover both the filter()cast() path and an explicitly constructed all-valid keys bitmap.

Are these changes tested?

Yes.

Added and passed:

  • Cast.CastMapWithAllValidKeysBitmap
  • Existing Cast.CastMap test

The complete arrow-array-test suite was also run successfully with 1060 tests passed and 1 skipped.

Are there any user-facing changes?

Yes. Casting affected MapArray values no longer aborts the process when the map keys have an all-valid validity bitmap with an unknown null count.

This PR contains a "Critical Fix".

This fixes a bug that can cause a process crash when casting a valid MapArray whose keys carry an all-valid validity bitmap with an unknown null count.

Fixes GH-51029.

@AnuragRaut08
AnuragRaut08 requested a review from pitrou as a code owner August 31, 2026 06:25
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51029 has been automatically assigned in GitHub to PR creator.

@pitrou pitrou 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.

Thanks for doing this @AnuragRaut08 ! See comments below.

Also, there are other uses of MayHaveNulls in array_nested.cc, shouldn't we fix them as well?

Comment on lines -899 to +905
if (pair_data->MayHaveNulls()) {
// Use GetNullCount() rather than MayHaveNulls() so that an all-valid
// validity bitmap (null_count == kUnknownNullCount but every bit set)
// is not mistakenly treated as containing nulls. MayHaveNulls() returns
// true whenever a bitmap is present and null_count != 0, which includes
// kUnknownNullCount (-1); GetNullCount() actually counts the bits and
// caches the result. See GH-51029.
if (pair_data->GetNullCount() != 0) {

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.

I don't think there's a need for this lengthy comment, let's just remove it?

Comment thread .gitignore
MANIFEST
compile_commands.json
build.ninja
build/

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.

This change is unrelated, can you revert it?

Comment on lines +3845 to +3847
// GH-51029: An all-valid validity bitmap with an unknown null count must
// not be treated as containing nulls when validating MapArray keys.
TEST(Cast, CastMapWithAllValidKeysBitmap) {

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.

This is a rather complicated way of exercising this bug.
Why not just set null_count to kUnknownNullCount directly?
(also then it can be moved to array_list_test.cc because it won't depend on compute anymore)

@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 31, 2026

@Reranko05 Reranko05 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also fix the lint by using:-
python -m pre_commit run clang-format --files <file_path>

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++][Python] Casting a MapArray whose keys child carries an all-valid validity bitmap aborts the process (residual case of GH-38553)

3 participants