-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(parquet): RowSelection can be backed by a BooleanBuffer
#10141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
f550754
add mask row selection
haohuaijin 9b869ed
update
haohuaijin 45db359
improve partial eq
haohuaijin 7f40e67
Merge branch 'main' into export-mask
haohuaijin e642eb6
add benchmark
haohuaijin 023827b
refactor: improve mask handling in RowSelection and add tests for mas…
haohuaijin 590cb93
improve mask auto selection strategy
haohuaijin 1720537
Merge branch 'main' into export-mask
haohuaijin 7b102f4
Merge branch 'main' into export-mask
alamb fff3ab3
fix performance and avoid break change
haohuaijin 0ffbf26
fix ci
haohuaijin 3121682
test inline
haohuaijin ee2df31
Merge branch 'main' into export-mask
haohuaijin e799224
make code clearly
haohuaijin 5da1b04
make code clearly
haohuaijin 812ee93
Merge branch 'main' into export-mask
haohuaijin be22472
Merge branch 'main' into export-mask
haohuaijin 8ab9632
Merge branch 'main' into export-mask
haohuaijin fb0aaf3
fix merge conflict
haohuaijin 7175deb
Merge branch 'main' into export-mask
haohuaijin 66271f2
Merge branch 'main' into export-mask
haohuaijin 8c217d8
Merge branch 'main' into export-mask
haohuaijin 496d7cb
perf: address review feedback on mask-backed RowSelection
haohuaijin 325d774
bench: add Auto mode and mask-backed inputs to row_selection_cursor
haohuaijin 1759c41
fix: clippy enum_variant_names in row_selection_cursor bench
haohuaijin fb06f89
fmt
haohuaijin 6c98404
Merge branch 'main' into export-mask
haohuaijin 3f89ed0
update mask_run_count
haohuaijin 5313948
Merge branch 'main' into export-mask
haohuaijin d4fa2be
test: sparse-page regression coverage for BooleanBuffer-backed masks
haohuaijin 9052c57
test: cover remaining mask-backed selection branches
haohuaijin 86610b5
apply suggestion
haohuaijin deb2610
Merge branch 'main' into export-mask
haohuaijin bdf2726
apply suggestion
haohuaijin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow_array::{ArrayRef, BooleanArray, Int32Array, RecordBatch}; | ||
| use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder}; | ||
| use arrow_schema::{DataType, Field, Schema}; | ||
| use bytes::Bytes; | ||
| use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; | ||
| use parquet::arrow::ArrowWriter; | ||
| use parquet::arrow::arrow_reader::{ParquetRecordBatchReaderBuilder, RowSelection}; | ||
| use rand::rngs::StdRng; | ||
| use rand::{Rng, SeedableRng}; | ||
| use std::hint; | ||
| use std::sync::Arc; | ||
|
|
||
| const TOTAL_ROWS: usize = 3_000_000; | ||
| const SELECTIVITY_CASES: &[Selectivity] = &[ | ||
| Selectivity::new("select01", 1, 100), | ||
| Selectivity::new("select10", 1, 10), | ||
| Selectivity::new("select33", 1, 3), | ||
| Selectivity::new("select80", 4, 5), | ||
| ]; | ||
|
|
||
| /// Generates a deterministic random row mask with the specified selectivity. | ||
| fn generate_random_row_selection(total_rows: usize, selectivity: Selectivity) -> BooleanBuffer { | ||
| let mut rng = StdRng::seed_from_u64(0x5E1EC7_u64 ^ selectivity.seed()); | ||
| let bools: Vec<bool> = (0..total_rows) | ||
| .map(|_| rng.random_bool(selectivity.ratio())) | ||
| .collect(); | ||
| BooleanBuffer::from(bools) | ||
| } | ||
|
|
||
| fn generate_fragmented_selection(total_rows: usize, selectivity: Selectivity) -> BooleanBuffer { | ||
| let mut builder = BooleanBufferBuilder::new(total_rows); | ||
| for row in 0..total_rows { | ||
| builder.append(row % selectivity.denominator < selectivity.numerator); | ||
| } | ||
| builder.finish() | ||
| } | ||
|
|
||
| fn generate_clustered_selection(total_rows: usize, selectivity: Selectivity) -> BooleanBuffer { | ||
| const RUN: usize = 8 * 1024; | ||
| const JITTER: usize = RUN / 2; | ||
| let mut rng = StdRng::seed_from_u64(0xC1057E_u64 ^ selectivity.seed()); | ||
| let mut builder = BooleanBufferBuilder::new(total_rows); | ||
| let mut rows_remaining = total_rows; | ||
| let mut run_idx = 0usize; | ||
|
|
||
| while rows_remaining != 0 { | ||
| let run_len = rng | ||
| .random_range((RUN - JITTER)..=(RUN + JITTER)) | ||
| .min(rows_remaining); | ||
| let selected = run_idx % selectivity.denominator < selectivity.numerator; | ||
| builder.append_n(run_len, selected); | ||
| rows_remaining -= run_len; | ||
| run_idx += 1; | ||
| } | ||
| builder.finish() | ||
| } | ||
|
|
||
| fn boolean_array(mask: &BooleanBuffer) -> BooleanArray { | ||
| BooleanArray::new(mask.clone(), None) | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let patterns = build_patterns(); | ||
|
|
||
| let mut construction = c.benchmark_group("row_selector_boolean_buffer/construction"); | ||
| for case in &patterns { | ||
| construction.bench_with_input( | ||
| BenchmarkId::new("from_filters", case.label()), | ||
| &case.mask, | ||
| |b, mask| { | ||
| b.iter(|| { | ||
| let array = boolean_array(mask); | ||
| let selection = RowSelection::from_filters(&[array]); | ||
| hint::black_box(selection); | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| construction.bench_with_input( | ||
| BenchmarkId::new("from_boolean_buffer", case.label()), | ||
| &case.mask, | ||
| |b, mask| { | ||
| b.iter(|| { | ||
| let selection = RowSelection::from_boolean_buffer(mask.clone()); | ||
| hint::black_box(selection); | ||
| }) | ||
| }, | ||
| ); | ||
| } | ||
| construction.finish(); | ||
|
|
||
| let parquet_data = write_parquet_file(TOTAL_ROWS); | ||
| let mut reader = c.benchmark_group("row_selector_boolean_buffer/reader"); | ||
| for case in &patterns { | ||
| reader.bench_with_input( | ||
| BenchmarkId::new("from_filters", case.label()), | ||
| &case.mask, | ||
| |b, mask| { | ||
| b.iter(|| { | ||
| let selection = RowSelection::from_filters(&[boolean_array(mask)]); | ||
| let rows = read_rows(&parquet_data, selection); | ||
| hint::black_box(rows); | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| reader.bench_with_input( | ||
| BenchmarkId::new("from_boolean_buffer", case.label()), | ||
| &case.mask, | ||
| |b, mask| { | ||
| b.iter(|| { | ||
| let selection = RowSelection::from_boolean_buffer(mask.clone()); | ||
| let rows = read_rows(&parquet_data, selection); | ||
| hint::black_box(rows); | ||
| }) | ||
| }, | ||
| ); | ||
| } | ||
| reader.finish(); | ||
| } | ||
|
|
||
| fn build_patterns() -> Vec<BenchCase> { | ||
| let mut patterns = Vec::new(); | ||
| for selectivity in SELECTIVITY_CASES { | ||
| patterns.push(BenchCase::new( | ||
| "fragmented", | ||
| *selectivity, | ||
| generate_fragmented_selection(TOTAL_ROWS, *selectivity), | ||
| )); | ||
| patterns.push(BenchCase::new( | ||
| "clustered", | ||
| *selectivity, | ||
| generate_clustered_selection(TOTAL_ROWS, *selectivity), | ||
| )); | ||
| patterns.push(BenchCase::new( | ||
| "random", | ||
| *selectivity, | ||
| generate_random_row_selection(TOTAL_ROWS, *selectivity), | ||
| )); | ||
| } | ||
| patterns | ||
| } | ||
|
|
||
| fn write_parquet_file(total_rows: usize) -> Bytes { | ||
| let schema = Arc::new(Schema::new(vec![Field::new( | ||
| "value", | ||
| DataType::Int32, | ||
| false, | ||
| )])); | ||
| let values: ArrayRef = Arc::new(Int32Array::from_iter_values( | ||
| (0..total_rows).map(|row| row as i32), | ||
| )); | ||
| let batch = RecordBatch::try_new(schema.clone(), vec![values]).unwrap(); | ||
|
|
||
| let mut writer = ArrowWriter::try_new(Vec::new(), schema, None).unwrap(); | ||
| writer.write(&batch).unwrap(); | ||
| Bytes::from(writer.into_inner().unwrap()) | ||
| } | ||
|
|
||
| fn read_rows(parquet_data: &Bytes, selection: RowSelection) -> usize { | ||
| let reader = ParquetRecordBatchReaderBuilder::try_new(parquet_data.clone()) | ||
| .unwrap() | ||
| .with_row_selection(selection) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| reader.map(|batch| batch.unwrap().num_rows()).sum::<usize>() | ||
| } | ||
|
|
||
| struct BenchCase { | ||
| pattern: &'static str, | ||
| selectivity: Selectivity, | ||
| mask: BooleanBuffer, | ||
| } | ||
|
|
||
| impl BenchCase { | ||
| fn new(pattern: &'static str, selectivity: Selectivity, mask: BooleanBuffer) -> Self { | ||
| Self { | ||
| pattern, | ||
| selectivity, | ||
| mask, | ||
| } | ||
| } | ||
|
|
||
| fn label(&self) -> String { | ||
| format!("{}/{}", self.pattern, self.selectivity.label) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct Selectivity { | ||
| label: &'static str, | ||
| numerator: usize, | ||
| denominator: usize, | ||
| } | ||
|
|
||
| impl Selectivity { | ||
| const fn new(label: &'static str, numerator: usize, denominator: usize) -> Self { | ||
| Self { | ||
| label, | ||
| numerator, | ||
| denominator, | ||
| } | ||
| } | ||
|
|
||
| fn ratio(self) -> f64 { | ||
| self.numerator as f64 / self.denominator as f64 | ||
| } | ||
|
|
||
| fn seed(self) -> u64 { | ||
| ((self.numerator as u64) << 32) ^ self.denominator as u64 | ||
| } | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.