Skip to content
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

Add ParquetAccessPlan, unify RowGroup selection and PagePruning selection #10738

Merged
merged 8 commits into from
Jun 6, 2024
399 changes: 399 additions & 0 deletions datafusion/core/src/datasource/physical_plan/parquet/access_plan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,399 @@
// 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 parquet::arrow::arrow_reader::{RowSelection, RowSelector};
use parquet::file::metadata::RowGroupMetaData;

/// A selection of rows and row groups within a ParquetFile to decode.
///
/// A `ParquetAccessPlan` is used to limits the row groups and data pages a `ParquetExec`
/// will read and decode and this improve performance.
///
/// Note that page level pruning based on ArrowPredicate is applied after all of
/// these selections
///
/// # Example
///
/// For example, given a Parquet file with 4 row groups, a `ParquetAccessPlan`
/// can be used to specify skipping row group 0 and 2, scanning a range of rows
/// in row group 1, and scanning all rows in row group 3 as follows:
///
/// ```rust
/// # use parquet::arrow::arrow_reader::{RowSelection, RowSelector};
/// # use datafusion::datasource::physical_plan::parquet::ParquetAccessPlan;
/// // Default to scan all row groups
/// let mut access_plan = ParquetAccessPlan::new_all(4);
/// access_plan.skip(0); // skip row group
/// // Use parquet reader RowSelector to specify scanning rows 100-200 and 350-400
/// let row_selection = RowSelection::from(vec![
/// RowSelector::skip(100),
/// RowSelector::select(100),
/// RowSelector::skip(150),
/// RowSelector::select(50),
/// ]);
alamb marked this conversation as resolved.
Show resolved Hide resolved
/// access_plan.scan_selection(1, row_selection);
/// access_plan.skip(2); // skip row group 2
/// // row group 3 is scanned by default
/// ```
///
/// The resulting plan would look like:
///
/// ```text
/// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
///
/// │ │ SKIP
///
/// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
/// Row Group 0
/// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
/// ┌────────────────┐ SCAN ONLY ROWS
/// │└────────────────┘ │ 100-200
/// ┌────────────────┐ 350-400
/// │└────────────────┘ │
/// ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
/// Row Group 1
/// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
/// SKIP
/// │ │
///
/// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
/// Row Group 2
/// ┌───────────────────┐
/// │ │ SCAN ALL ROWS
/// │ │
/// │ │
/// └───────────────────┘
/// Row Group 3
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ParquetAccessPlan {
/// How to access the i-th row group
row_groups: Vec<RowGroupAccess>,
}

/// Describes how the parquet reader will access a row group
#[derive(Debug, Clone, PartialEq)]
pub enum RowGroupAccess {
/// Do not read the row group at all
Skip,
/// Read all rows from the row group
Scan,
/// Scan only the specified rows within the row group
Selection(RowSelection),
}

impl RowGroupAccess {
/// Return true if this row group should be scanned
pub fn should_scan(&self) -> bool {
match self {
RowGroupAccess::Skip => false,
RowGroupAccess::Scan | RowGroupAccess::Selection(_) => true,
}
}
}

impl ParquetAccessPlan {
/// Create a new `ParquetAccessPlan` that scans all row groups
pub fn new_all(row_group_count: usize) -> Self {
Self {
row_groups: vec![RowGroupAccess::Scan; row_group_count],
}
}

/// Create a new `ParquetAccessPlan` that scans no row groups
pub fn new_none(row_group_count: usize) -> Self {
Self {
row_groups: vec![RowGroupAccess::Skip; row_group_count],
}
}

/// Create a new `ParquetAccessPlan` from the specified [`RowGroupAccess`]es
pub fn new(row_groups: Vec<RowGroupAccess>) -> Self {
Self { row_groups }
}

/// Set the i-th row group to the specified [`RowGroupAccess`]
pub fn set(&mut self, idx: usize, access: RowGroupAccess) {
self.row_groups[idx] = access;
}

/// skips the i-th row group (should not be scanned)
pub fn skip(&mut self, idx: usize) {
self.set(idx, RowGroupAccess::Skip);
}

/// Return true if the i-th row group should be scanned
pub fn should_scan(&self, idx: usize) -> bool {
self.row_groups[idx].should_scan()
}

/// Set to scan only the [`RowSelection`] in the specified row group.
///
/// Behavior is different depending on the existing access
/// * [`RowGroupAccess::Skip`]: does nothing
/// * [`RowGroupAccess::Scan`]: Updates to scan only the rows in the `RowSelection`
/// * [`RowGroupAccess::Selection`]: Updates to scan only the intersection of the existing selection and the new selection
pub fn scan_selection(&mut self, idx: usize, selection: RowSelection) {
self.row_groups[idx] = match &self.row_groups[idx] {
// already skipping the entire row group
RowGroupAccess::Skip => RowGroupAccess::Skip,
RowGroupAccess::Scan => RowGroupAccess::Selection(selection),
RowGroupAccess::Selection(existing_selection) => {
RowGroupAccess::Selection(existing_selection.intersection(&selection))
}
}
}

/// Return the overall `RowSelection` for all scanned row groups
///
/// This is used to compute the row selection for the parquet reader. See
/// [`ArrowReaderBuilder::with_row_selection`] for more details.
///
/// Returns
/// * `None` if there are no [`RowGroupAccess::Selection`]
/// * `Some(selection)` if there are [`RowGroupAccess::Selection`]s
///
/// The returned selection represents which rows to scan across any row
/// row groups which are not skipped.
///
/// # Example
///
/// Given an access plan like this:
///
/// ```text
/// Scan (scan all row group 0)
/// Skip (skip row group 1)
/// Select 50-100 (scan rows 50-100 in row group 2)
/// ```
///
/// Assuming each row group has 1000 rows, the resulting row selection would
/// be the rows to scan in row group 0 and 2:
///
/// ```text
/// Select 1000 (scan all rows in row group 0)
/// Select 50-100 (scan rows 50-100 in row group 2)
/// ```
///
/// Note there is no entry for the (entirely) skipped row group 1.
///
/// [`ArrowReaderBuilder::with_row_selection`]: parquet::arrow::arrow_reader::ArrowReaderBuilder::with_row_selection
pub fn into_overall_row_selection(
self,
row_group_meta_data: &[RowGroupMetaData],
) -> Option<RowSelection> {
assert_eq!(row_group_meta_data.len(), self.row_groups.len());
if !self
.row_groups
.iter()
.any(|rg| matches!(rg, RowGroupAccess::Selection(_)))
Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm. I don't think this is correct. Unless all the RowGroupAccess is Skip we can simply return none here. Otherwise, we should still build RowSelection for the Scan Access.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good question.

I think the reasoning here is that actually correct because any RowGroupAccess that is Skip is filtered by skipping the row groups itself (and thus there are no rows to select).

I will improve the documentation and add some tests that show how this works

Copy link
Contributor

Choose a reason for hiding this comment

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

What about all the RowGroupAccess is Scan then(which means all the row group should be selected)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same reasoning applies there (that the entire rowgroup is scanned)

The intuition is that entire row groups are filtered out using Skip and Scan -- an overall RowSelection is only useful if there is any parts within a row group which can be filtered out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to clarify with comments and some test updates in 8d44ed2 -- let me know what you think

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I think the example and the following code in L214 - L218 is quite misleading.

BTW, I did follow the code in /path/to/arrow-rs/src/index.crates.io-6f17d22bba15001f/parquet-51.0.0/src/arrow/async_reader/mod.rs:601's poll_next method, namely the following code:

                    let row_count = self.metadata.row_group(row_group_idx).num_rows() as usize;

                    let selection = self.selection.as_mut().map(|s| s.split_off(row_count));

                    let fut = reader
                        .read_row_group(
                            row_group_idx,
                            selection,
                            self.projection.clone(),
                            self.batch_size,
                        )
                        .boxed();

I'm not sure I understand the code correctly, but it looks to me that the row_selection only consider one row group per parquet file? Otherwise, I believe the row_count(or the split_off point) should be the row count has been accumulated with all the previous row groups?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am somewhat confused too -- I will look into this more carefully

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand the code correctly, but it looks to me that the row_selection only consider one row group per parquet file? Otherwise, I believe the row_count(or the split_off point) should be the row count has been accumulated with all the previous row groups?

Yes, I think that is correct

I have updated the example in a76f95a to more accurately reflect what is going on, which I think makes the behavior clearer

I will also make a PR to the arrow repo trying to clarify with an example as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

apache/arrow-rs#5850 to further improve the parquet crate docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW this so subtle it turns out my unit tests don't get the edge cases correct. I added error checking and more tests as part of #10813

{
return None;
}

let total_selection: RowSelection = self
.row_groups
.into_iter()
.zip(row_group_meta_data.iter())
.flat_map(|(rg, rg_meta)| {
match rg {
RowGroupAccess::Skip => vec![],
RowGroupAccess::Scan => {
// need a row group access to scan the entire row group (need row group counts)
vec![RowSelector::select(rg_meta.num_rows() as usize)]
}
RowGroupAccess::Selection(selection) => {
let selection: Vec<RowSelector> = selection.into();
selection
}
}
})
.collect();

Some(total_selection)
}

/// Return an iterator over the row group indexes that should be scanned
pub fn row_group_index_iter(&self) -> impl Iterator<Item = usize> + '_ {
self.row_groups.iter().enumerate().filter_map(|(idx, b)| {
if b.should_scan() {
Some(idx)
} else {
None
}
})
}

/// Return a vec of all row group indexes to scan
pub fn row_group_indexes(&self) -> Vec<usize> {
self.row_group_index_iter().collect()
}

/// Return the total number of row groups (not the total number or groups to
/// scan)
pub fn len(&self) -> usize {
self.row_groups.len()
}

/// Return true if there are no row groups
pub fn is_empty(&self) -> bool {
self.row_groups.is_empty()
}

/// Get a reference to the inner accesses
pub fn inner(&self) -> &[RowGroupAccess] {
&self.row_groups
}

/// Covert into the inner row group accesses
pub fn into_inner(self) -> Vec<RowGroupAccess> {
self.row_groups
}
}

#[cfg(test)]
mod test {
use super::*;
use parquet::basic::LogicalType;
use parquet::file::metadata::ColumnChunkMetaData;
use parquet::schema::types::{SchemaDescPtr, SchemaDescriptor};
use std::sync::{Arc, OnceLock};

#[test]
fn test_overall_row_selection_only_scans() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these tests because I found the existing tests don't cover the case where there is a mix of scan some entire row groups and only scan the rows of another (happens when the statistics can't be extracted or there is some error evaluating the page pruning predicate only on some row groups)

assert_eq!(
overall_row_selection(vec![
RowGroupAccess::Scan,
RowGroupAccess::Scan,
RowGroupAccess::Scan,
RowGroupAccess::Scan,
]),
None
);
}

#[test]
fn test_overall_row_selection_only_skips() {
assert_eq!(
overall_row_selection(vec![
RowGroupAccess::Skip,
RowGroupAccess::Skip,
RowGroupAccess::Skip,
RowGroupAccess::Skip,
]),
None
);
}
#[test]
fn test_overall_row_selection_mixed_1() {
assert_eq!(
overall_row_selection(vec![
RowGroupAccess::Scan,
RowGroupAccess::Selection(
vec![RowSelector::select(5), RowSelector::skip(7)].into()
),
RowGroupAccess::Skip,
RowGroupAccess::Skip,
]),
Some(
vec![
// select the entire first row group
RowSelector::select(10),
// selectors from the second row group
RowSelector::select(5),
RowSelector::skip(7)
]
.into()
)
);
}

#[test]
fn test_overall_row_selection_mixed_2() {
assert_eq!(
overall_row_selection(vec![
RowGroupAccess::Skip,
RowGroupAccess::Scan,
RowGroupAccess::Selection(
vec![RowSelector::select(5), RowSelector::skip(7)].into()
),
RowGroupAccess::Scan,
]),
Some(
vec![
// select the entire second row group
RowSelector::select(20),
// selectors from the third row group
RowSelector::select(5),
RowSelector::skip(7),
// select the entire fourth row group
RowSelector::select(40),
]
.into()
)
);
}

/// Computes the overall row selection for the given row group access list
fn overall_row_selection(
row_group_access: Vec<RowGroupAccess>,
) -> Option<RowSelection> {
let access_plan = ParquetAccessPlan::new(row_group_access);
access_plan.into_overall_row_selection(row_group_metadata())
}

static ROW_GROUP_METADATA: OnceLock<Vec<RowGroupMetaData>> = OnceLock::new();

/// [`RowGroupMetaData`] that returns 4 row groups with 10, 20, 30, 40 rows
/// respectively
fn row_group_metadata() -> &'static [RowGroupMetaData] {
ROW_GROUP_METADATA.get_or_init(|| {
let schema_descr = get_test_schema_descr();
let row_counts = [10, 20, 30, 40];

row_counts
.into_iter()
.map(|num_rows| {
let column = ColumnChunkMetaData::builder(schema_descr.column(0))
.set_num_values(num_rows)
.build()
.unwrap();

RowGroupMetaData::builder(schema_descr.clone())
.set_num_rows(num_rows)
.set_column_metadata(vec![column])
.build()
.unwrap()
})
.collect()
})
}

/// Single column schema with a single column named "a" of type `BYTE_ARRAY`/`String`
fn get_test_schema_descr() -> SchemaDescPtr {
use parquet::basic::Type as PhysicalType;
use parquet::schema::types::Type as SchemaType;
let field = SchemaType::primitive_type_builder("a", PhysicalType::BYTE_ARRAY)
.with_logical_type(Some(LogicalType::String))
.build()
.unwrap();
let schema = SchemaType::group_type_builder("schema")
.with_fields(vec![Arc::new(field)])
.build()
.unwrap();
Arc::new(SchemaDescriptor::new(Arc::new(schema)))
}
}
Loading