Skip to content

[Parquet] Add BooleanArray based row selection - #6624

Closed
XiangpengHao wants to merge 6 commits into
apache:mainfrom
XiangpengHao:row-selector3
Closed

XiangpengHao wants to merge 6 commits into
apache:mainfrom
XiangpengHao:row-selector3

Conversation

@XiangpengHao

@XiangpengHao XiangpengHao commented Oct 24, 2024 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Implement the row selection based on BooleanArray.

The implementation only covers the core operations like union, intersection, and_then, and is already quite large. Once we agree with the generally direction, I'll add remaining implementaions in follow up PRs.

What changes are included in this PR?

I added more benchmark to cover different selection ratios, as suggested by @tustvold and @alamb

Are there any user-facing changes?

@github-actions github-actions Bot added the parquet Changes to the parquet crate label Oct 24, 2024
@XiangpengHao
XiangpengHao force-pushed the row-selector3 branch 2 times, most recently from c597e4a to 42938bf Compare October 25, 2024 15:05
@XiangpengHao
XiangpengHao marked this pull request as ready for review October 25, 2024 15:06
@XiangpengHao

XiangpengHao commented Oct 25, 2024 •

Copy link
Copy Markdown
Contributor Author

The benchmark can take a while to run, here're some of my results visualized by ChatGPT. Note that both x and y axies are in log scale. slice is the current slice-based row selection, boolean is the boolean mask based selection.

Selection ratio 1e-5 means 0.0001% of the rows are selected, 0.3 means 30% are selected, and so on.

image

image

image

image

@tustvold

tustvold commented Oct 26, 2024 •

Copy link
Copy Markdown
Contributor

Perhaps we could model the number of disjoint selections as opposed to raw selectivity, this more accurately models the sorts of selection one gets from evaluation against the page index (which necessarily has page level granularity)

Edit: although the methods benchmarked are those used for late materialization, so perhaps it doesn't matter

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

I've not reviewed in fine detail, but the code looks good to me

@alamb

alamb commented Oct 26, 2024 •

Copy link
Copy Markdown
Contributor

I will try and find time to review this today or tomorrow as well -- thank you @XiangpengHao and @tustvold

@alamb alamb 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 @XiangpengHao -- I think this is an interesting direction to head (use bitmap for selection). However, I am not sure about this exact implementation)

I left some questions below.

One thing that seems like it might be more useful is to switch RowSelection into an enum so we can dynamically switch between the two representations based on what is actually selected

enum RowSelection {
  Ranges(Vec<Selection>),
  Btitmap(..)
}

I realize one challenge with this is that BooleanArray is part of arrow and not available in Parquet directly.

/// A selection of rows, similar to [`RowSelection`], but based on a boolean array
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BooleanRowSelection {
selector: BooleanBuffer,

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.

One question I have while reviewing this PR is "why not just use BooleanArray directly?"

It seems like most of these methods would be generally useful on BooleanArray (or perhaps as kernels) -- and in fact most of them are already (like union is | and intersection is &).

Another thing that might be interesting to do if we really care about performance is to look into implementing in-place updates -- maybe something like https://docs.rs/arrow/latest/arrow/compute/fn.binary_mut.html for BooleanArray)

@XiangpengHao

Copy link
Copy Markdown
Contributor Author

switch RowSelection into an enum so we can dynamically switch

That should work, my initial thoughts was to only use bitmap-based selectors when evaluating predicates (i.e., when filter pushdown is enabled); and always do range-based selectors otherwise.

"why not just use BooleanArray directly?"

I agree that most of the functionality overlaps with BooleanBuffer and can be implemented there. Will try to file a separate pr to extract the most common ones to BooleanBuffer in the next few days.

@alamb

alamb commented Nov 15, 2024

Copy link
Copy Markdown
Contributor

Marking as draft as I believe we plan additonal work for this PR


#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
unsafe fn boolean_buffer_and_then_bmi2(

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.

FWIW, if you ever encountered a situation where and_then show up in the flamegraph, here's the implementation that will speed up it by 5-10x. cc @alamb @zhuqi-lucas who are working on filter pushdown

I admit the code is much less readable than the fallback implementation, but it boils down to use the PDEP instructions available in almost all x86 machines. Rest of the code are just work to make sure alignment, handle edge cases, optimize for branching etc.

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.

I definitely struggled with this when thinking how to do fallback.

@alamb

alamb commented Nov 21, 2025

Copy link
Copy Markdown
Contributor

I started trying to incorporate this code in to the parquet reader:

unsafe { boolean_buffer_and_then_bmi2(left, right) }
} else {
// Fallback to the original implementation
boolean_buffer_and_then_fallback(left, right)

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.

When I was trying to use this code in #8902, I could not find this fallback.

@XiangpengHao did it in liquid cache:

@github-actions

Copy link
Copy Markdown

Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.

@github-actions github-actions Bot added the Stale label Jun 27, 2026
@github-actions github-actions Bot closed this Jul 5, 2026
alamb added a commit that referenced this pull request Jul 24, 2026
)

# Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->

- Closes #10140 
- closes #8844
- closes #6624 from @XiangpengHao
- Past attempts: #8902

# Rationale for this change

`RowSelection` currently stores selections as `Vec<RowSelector>` (16
bytes per selector). This is compact for long runs, but expensive for
scattered matches. With ~35% isolated single-row hits, it uses about
11.2 bytes per input row. A `BooleanBuffer` uses 1 bit per input row,
about 90x less memory.

The reader can also choose the `Mask` strategy, which converts selectors
back into a bitmap. When the caller already had a bitmap, this
conversion round-trip is unnecessary.

**This PR lets `RowSelection` preserve a caller-provided bitmap and pass
it directly to mask execution.**

This is not intended to claim broad DataFusion / TPC-DS / ClickBench
speedups. Current common DataFusion SQL paths generally do not naturally
produce bitmap-backed `RowSelection`s. The practical benefit is for
integrations that already have a row-level bitmap and need Parquet to
consume it without materializing a large selector list.

# What changes are included in this PR?

`RowSelection` can now be backed by either `Vec<RowSelector>` or
`BooleanBuffer`. New public construction:

```rust
pub fn RowSelection::from_boolean_buffer(mask: BooleanBuffer) -> Self;
impl From<BooleanBuffer> for RowSelection;
```

Methods that can work directly on the bitmap now do so:

- `iter()` still returns `&RowSelector` (non-breaking); mask-backed
selections lazily materialize a selector cache on first call, while
internal hot paths bypass it and use the `BooleanBuffer` / `MaskRunIter`
directly
- `row_count` / `skipped_row_count` use a cached popcount
- `selects_any` uses `set_indices().next()`
- `trim` preserves mask backing via `BooleanBuffer::slice`
- `intersection` / `union` on `Mask`+`Mask` use `BitAnd` / `BitOr`
- `split_off` on a mask uses `BooleanBuffer::slice` (`O(1)`, both halves
stay mask-backed)
- `limit` slices at the selected-row boundary via
`find_nth_set_bit_position`, staying mask-backed
- `offset` finds the first selected row to keep via
`find_nth_set_bit_position` and rebuilds only the mask buffer, avoiding
selector materialization
- `and_then` applies the inner selection over the mask's set positions,
returning a mask-backed result
- `FromIterator<RowSelection>` concatenates `BooleanBuffer`s when every
input is mask-backed

Mixed inputs, and existing selector-backed inputs, still use the
existing selector helpers. Existing callers keep the same behavior.

**The reader (`ReadPlanBuilder::build`) passes a mask-backed selection
straight to `RowSelectionCursor::new_mask_from_buffer`, so it skips
rebuilding the bitmap from selectors.**

`Auto` resolution works directly on the bitmap (early-exit run
counting), without converting the backing.

Integrates with #10288: both mask cursor constructors carry
`LoadedRowRanges`, so a caller-provided bitmap stays within loaded pages
when page pruning skips pages.

Also adds `MaskRunIter` + `RowSelection::as_mask` for zero-allocation
RLE iteration over a mask, the `row_selector_boolean_buffer` benchmark,
and `read_auto` / mask-backed input modes in `row_selection_cursor`.

# Are these changes tested?

Yes. This PR extends the existing `RowSelection` unit tests with
coverage for:

- constructing from `BooleanBuffer`, including empty and all-unset masks
- `From<BooleanBuffer>`
- preserving mask backing across clone, `split_off`, `limit`, `offset`,
`and_then`, and all-mask `FromIterator<RowSelection>`
- falling back to selector backing for mixed-backed concatenation
- equality between equivalent selector-backed and mask-backed selections
- mask-backed `intersection` / `union`, including uneven-length inputs
- fuzz-style equivalence between mask-backed selections and the existing
`from_filters` selector path
- sparse-page regression tests from a non-byte-aligned
`BooleanBuffer::slice(...)`, under both `Mask` and `Auto` policies
(#10288 integration seam)
- mask chunks stop at `LoadedRowRanges` boundaries
- fuzz tests for `boolean_mask_from_selectors` and `trim_mask`
(including non-zero offsets)
- remaining branches checked with `cargo llvm-cov`

# Are there any user-facing changes?

No breaking API changes. New public APIs:
`RowSelection::from_boolean_buffer`, `From<BooleanBuffer>`,
`RowSelection::as_mask`, `MaskRunIter`.

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet Changes to the parquet crate Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants