Skip to content

Commit

Permalink
Add RowSelection::iter(), Into<Vec<RowSelector>> and example (#3173)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb authored Nov 23, 2022
1 parent f749e1d commit cea5146
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion parquet/src/arrow/arrow_reader/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ impl RowSelector {
/// A typical use-case would be using the [`PageIndex`] to filter out rows
/// that don't satisfy a predicate
///
/// # Example
/// ```
/// use parquet::arrow::arrow_reader::{RowSelection, RowSelector};
///
/// let selectors = vec![
/// RowSelector { row_count: 5, skip: true },
/// RowSelector { row_count: 5, skip: false },
/// RowSelector { row_count: 5, skip: false },
/// RowSelector { row_count: 5, skip: true },
/// ];
///
/// // Creating a selection will combine adjacent selectors
/// let selection: RowSelection = selectors.into();
///
/// let expected = vec![
/// RowSelector { row_count: 5, skip: true },
/// RowSelector { row_count: 10, skip: false },
/// RowSelector { row_count: 5, skip: true },
/// ];
///
/// let actual: Vec<RowSelector> = selection.into();
/// assert_eq!(actual, expected);
/// ```
///
/// [`PageIndex`]: [crate::file::page_index::index::PageIndex]
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct RowSelection {
Expand Down Expand Up @@ -243,7 +267,6 @@ impl RowSelection {
selectors: remaining,
}
}

/// Given a [`RowSelection`] computed under `self`, returns the [`RowSelection`]
/// representing their conjunction
///
Expand Down Expand Up @@ -347,6 +370,12 @@ impl RowSelection {
}
self
}

/// Returns an iterator over the [`RowSelector`]s for this
/// [`RowSelection`].
pub fn iter(&self) -> impl Iterator<Item = &RowSelector> {
self.selectors.iter()
}
}

impl From<Vec<RowSelector>> for RowSelection {
Expand All @@ -355,6 +384,12 @@ impl From<Vec<RowSelector>> for RowSelection {
}
}

impl From<RowSelection> for Vec<RowSelector> {
fn from(r: RowSelection) -> Self {
r.selectors
}
}

impl From<RowSelection> for VecDeque<RowSelector> {
fn from(r: RowSelection) -> Self {
r.selectors.into()
Expand Down Expand Up @@ -789,6 +824,23 @@ mod tests {
}
}

#[test]
fn test_iter() {
// use the iter() API to show it does what is expected and
// avoid accidental deletion
let selectors = vec![
RowSelector::select(3),
RowSelector::skip(33),
RowSelector::select(4),
];

let round_tripped = RowSelection::from(selectors.clone())
.iter()
.cloned()
.collect::<Vec<_>>();
assert_eq!(selectors, round_tripped);
}

#[test]
fn test_scan_ranges() {
let index = vec![
Expand Down

0 comments on commit cea5146

Please sign in to comment.