Skip to content

GH-45819: [C++] Add OptionalBitmapAnd utility#49848

Open
Shockp wants to merge 1 commit intoapache:mainfrom
Shockp:feat-optional-bitmap-and
Open

GH-45819: [C++] Add OptionalBitmapAnd utility#49848
Shockp wants to merge 1 commit intoapache:mainfrom
Shockp:feat-optional-bitmap-and

Conversation

@Shockp
Copy link
Copy Markdown
Contributor

@Shockp Shockp commented Apr 23, 2026

Rationale for this change

In Arrow, null bitmaps are optional and represented by nullptr when a column contains no nulls. Previously, conjoining two optional bitmaps required downstream code to manually handle the nullptr checks and memory allocations. This change centralizes that logic into a single, highly optimized utility function.

What changes are included in this PR?

  • Added OptionalBitmapAnd to cpp/src/arrow/util/bitmap_ops.h and bitmap_ops.cc.
  • The implementation safely handles the four possible memory permutations with minimal allocations:
    1. Both buffers nullptr: Returns nullptr.
    2. Left is nullptr: Returns a copy of the right buffer.
    3. Right is nullptr: Returns a copy of the left buffer.
    4. Both valid: Defers to the low-level BitmapAnd function.

Are these changes tested?

Yes. I added a new test case (OptionalBitmapAnd) to cpp/src/arrow/util/bitmap_test.cc that explicitly verifies the correct buffer allocation and bitwise output for all four memory states.

Are there any user-facing changes?

No. This simply exposes a new C++ utility for internal development.

Closes #45819

@Shockp Shockp force-pushed the feat-optional-bitmap-and branch 3 times, most recently from 7fd57ca to 28d69c7 Compare April 23, 2026 11:14
Copy link
Copy Markdown
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this @Shockp ! Here are some comments.

Also can you find places in the codebase where this can be put to use?

Comment thread cpp/src/arrow/util/bitmap_ops.cc
Comment thread cpp/src/arrow/util/bitmap_ops.h Outdated
Comment thread cpp/src/arrow/util/bitmap_test.cc Outdated
Comment thread testing
@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Apr 27, 2026
@Shockp Shockp force-pushed the feat-optional-bitmap-and branch from 4973fdd to 2741418 Compare April 27, 2026 14:52
@Shockp
Copy link
Copy Markdown
Contributor Author

Shockp commented Apr 27, 2026

Reverted the testing submodule, added the out_offset default, implemented the zero-copy slicing for byte-aligned offsets, and updated the tests to use BitmapFromVector for non-trivial inputs. Let me know how it looks! @pitrou

Quick question on the API: I noticed the other functions in bitmap_ops.h (like BitmapAnd, BitmapOr, etc.) don't currently have out_offset = 0 as a default. Would it be worth unifying those for consistency in a separate, follow-up PR, or is it safer to leave the existing signatures as-is?

@Shockp Shockp force-pushed the feat-optional-bitmap-and branch 2 times, most recently from 20af324 to 9e05f24 Compare April 27, 2026 16:38
This commit introduces the `OptionalBitmapAnd` utility, which provides
an optimized bitwise AND operation for Arrow bitmaps.

Key changes:
- Added `OptionalBitmapAnd` function in `bitmap_ops.h` and `bitmap_ops.cc`.
- Implemented optimizations to avoid allocations and use slicing when bitmaps
  are byte-aligned and either the left or right bitmap is missing.
- Added comprehensive unit tests in `bitmap_test.cc` covering all permutations
  of offsets, lengths, and missing bitmaps.
@Shockp Shockp force-pushed the feat-optional-bitmap-and branch from 960feb9 to 8972ab0 Compare April 27, 2026 16:49
@Shockp
Copy link
Copy Markdown
Contributor Author

Shockp commented Apr 27, 2026

Sorry, I had some conflicts with my local branch while trying to pass the lint CI. I just squashed all the changes of the PR in a single commit.

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++] Add OptionalBitmapAnd utility

2 participants