|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use parquet::arrow::arrow_reader::{RowSelection, RowSelector}; |
| 19 | + |
| 20 | +/// Specifies a selection of rows and row groups within a ParquetFile to decode. |
| 21 | +/// |
| 22 | +/// A `ParquetAccessPlan` is used to limits the row groups and data pages a `ParquetExec` |
| 23 | +/// will read and decode and this improve performance. |
| 24 | +/// |
| 25 | +/// Note that page level pruning based on ArrowPredicate is applied after all of |
| 26 | +/// these selections |
| 27 | +/// |
| 28 | +/// This looks like: |
| 29 | +/// (TODO diagram) |
| 30 | +#[derive(Debug, Clone, PartialEq)] |
| 31 | +pub struct ParquetAccessPlan { |
| 32 | + /// How to access the i-th row group |
| 33 | + row_groups: Vec<RowGroupAccess>, |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Debug, Clone, PartialEq)] |
| 37 | +pub enum RowGroupAccess { |
| 38 | + /// The row group should not be read at all |
| 39 | + Skip, |
| 40 | + /// The row group should be scanned fully |
| 41 | + Scan, |
| 42 | + /// Only the specified rows within the row group should be scanned |
| 43 | + Selection(RowSelection), |
| 44 | +} |
| 45 | + |
| 46 | +impl RowGroupAccess { |
| 47 | + /// return true if this row group should be scanned |
| 48 | + pub fn should_scan(&self) -> bool { |
| 49 | + match self { |
| 50 | + RowGroupAccess::Skip => false, |
| 51 | + RowGroupAccess::Scan | RowGroupAccess::Selection(_) => true, |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl ParquetAccessPlan { |
| 57 | + /// Create a new `ParquetAccessPlan` to scan all row groups |
| 58 | + pub fn new_all(row_group_count: usize) -> Self { |
| 59 | + Self { |
| 60 | + row_groups: vec![RowGroupAccess::Scan; row_group_count], |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// Set the i-th row group to false (should not be scanned) |
| 65 | + pub fn do_not_scan(&mut self, idx: usize) { |
| 66 | + self.row_groups[idx] = RowGroupAccess::Skip; |
| 67 | + } |
| 68 | + |
| 69 | + /// Return true if the i-th row group should be scanned |
| 70 | + pub fn should_scan(&self, idx: usize) -> bool { |
| 71 | + self.row_groups[idx].should_scan() |
| 72 | + } |
| 73 | + |
| 74 | + /// Set to scan only the [`RowSelection`] in the specified row group. |
| 75 | + /// |
| 76 | + /// Based on the existing row groups plan: |
| 77 | + /// * Skip: does nothing |
| 78 | + /// * Scan: Updates to scan only the rows in the `RowSelection` |
| 79 | + /// * Selection: Updates to scan only the specified in the exising selection and the new selection |
| 80 | + pub fn scan_selection(&mut self, idx: usize, selection: RowSelection) { |
| 81 | + self.row_groups[idx] = match &self.row_groups[idx] { |
| 82 | + // already skipping the entire row group |
| 83 | + RowGroupAccess::Skip => RowGroupAccess::Skip, |
| 84 | + RowGroupAccess::Scan => RowGroupAccess::Selection(selection), |
| 85 | + RowGroupAccess::Selection(existing_selection) => { |
| 86 | + RowGroupAccess::Selection(existing_selection.intersection(&selection)) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Return the overall RowSelection for all scanned row groups, if |
| 92 | + /// there are any RowGroupAccess::Selection; |
| 93 | + /// |
| 94 | + /// |
| 95 | + /// TODO better doc / explanation |
| 96 | + pub fn overall_row_selection(&self) -> Option<RowSelection> { |
| 97 | + if !self |
| 98 | + .row_groups |
| 99 | + .iter() |
| 100 | + .any(|rg| matches!(rg, RowGroupAccess::Selection(_))) |
| 101 | + { |
| 102 | + return None; |
| 103 | + } |
| 104 | + |
| 105 | + let total_selection: RowSelection = self |
| 106 | + .row_groups |
| 107 | + .iter() |
| 108 | + .flat_map(|rg| { |
| 109 | + match rg { |
| 110 | + RowGroupAccess::Skip => vec![], |
| 111 | + RowGroupAccess::Scan => { |
| 112 | + // need a row group access to scan the entire row group (need row group counts) |
| 113 | + // This is clearly not tested TODO |
| 114 | + todo!(); |
| 115 | + } |
| 116 | + RowGroupAccess::Selection(selection) => { |
| 117 | + // todo avoid these clones |
| 118 | + let selection: Vec<RowSelector> = selection.clone().into(); |
| 119 | + selection |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | + .collect(); |
| 124 | + |
| 125 | + Some(total_selection) |
| 126 | + } |
| 127 | + |
| 128 | + /// Return an iterator over the row group indexes that should be scanned |
| 129 | + pub fn row_group_index_iter(&self) -> impl Iterator<Item = usize> + '_ { |
| 130 | + self.row_groups.iter().enumerate().filter_map(|(idx, b)| { |
| 131 | + if b.should_scan() { |
| 132 | + Some(idx) |
| 133 | + } else { |
| 134 | + None |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + /// Return a vec of all row group indexes to scan |
| 140 | + pub fn row_group_indexes(&self) -> Vec<usize> { |
| 141 | + self.row_group_index_iter().collect() |
| 142 | + } |
| 143 | + |
| 144 | + /// Return the total number of row groups (not the total number to be scanned) |
| 145 | + pub fn len(&self) -> usize { |
| 146 | + self.row_groups.len() |
| 147 | + } |
| 148 | + |
| 149 | + /// Return true if there are no row groups |
| 150 | + pub fn is_empty(&self) -> bool { |
| 151 | + self.row_groups.is_empty() |
| 152 | + } |
| 153 | +} |
0 commit comments