|
| 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 arrow::array::{ArrayRef, BooleanArray}; |
| 19 | +use std::collections::HashSet; |
| 20 | + |
| 21 | +use crate::Column; |
| 22 | +use crate::ScalarValue; |
| 23 | + |
| 24 | +/// A source of runtime statistical information to [`PruningPredicate`]s. |
| 25 | +/// |
| 26 | +/// # Supported Information |
| 27 | +/// |
| 28 | +/// 1. Minimum and maximum values for columns |
| 29 | +/// |
| 30 | +/// 2. Null counts and row counts for columns |
| 31 | +/// |
| 32 | +/// 3. Whether the values in a column are contained in a set of literals |
| 33 | +/// |
| 34 | +/// # Vectorized Interface |
| 35 | +/// |
| 36 | +/// Information for containers / files are returned as Arrow [`ArrayRef`], so |
| 37 | +/// the evaluation happens once on a single `RecordBatch`, which amortizes the |
| 38 | +/// overhead of evaluating the predicate. This is important when pruning 1000s |
| 39 | +/// of containers which often happens in analytic systems that have 1000s of |
| 40 | +/// potential files to consider. |
| 41 | +/// |
| 42 | +/// For example, for the following three files with a single column `a`: |
| 43 | +/// ```text |
| 44 | +/// file1: column a: min=5, max=10 |
| 45 | +/// file2: column a: No stats |
| 46 | +/// file2: column a: min=20, max=30 |
| 47 | +/// ``` |
| 48 | +/// |
| 49 | +/// PruningStatistics would return: |
| 50 | +/// |
| 51 | +/// ```text |
| 52 | +/// min_values("a") -> Some([5, Null, 20]) |
| 53 | +/// max_values("a") -> Some([10, Null, 30]) |
| 54 | +/// min_values("X") -> None |
| 55 | +/// ``` |
| 56 | +/// |
| 57 | +/// [`PruningPredicate`]: https://docs.rs/datafusion/latest/datafusion/physical_optimizer/pruning/struct.PruningPredicate.html |
| 58 | +pub trait PruningStatistics { |
| 59 | + /// Return the minimum values for the named column, if known. |
| 60 | + /// |
| 61 | + /// If the minimum value for a particular container is not known, the |
| 62 | + /// returned array should have `null` in that row. If the minimum value is |
| 63 | + /// not known for any row, return `None`. |
| 64 | + /// |
| 65 | + /// Note: the returned array must contain [`Self::num_containers`] rows |
| 66 | + fn min_values(&self, column: &Column) -> Option<ArrayRef>; |
| 67 | + |
| 68 | + /// Return the maximum values for the named column, if known. |
| 69 | + /// |
| 70 | + /// See [`Self::min_values`] for when to return `None` and null values. |
| 71 | + /// |
| 72 | + /// Note: the returned array must contain [`Self::num_containers`] rows |
| 73 | + fn max_values(&self, column: &Column) -> Option<ArrayRef>; |
| 74 | + |
| 75 | + /// Return the number of containers (e.g. Row Groups) being pruned with |
| 76 | + /// these statistics. |
| 77 | + /// |
| 78 | + /// This value corresponds to the size of the [`ArrayRef`] returned by |
| 79 | + /// [`Self::min_values`], [`Self::max_values`], [`Self::null_counts`], |
| 80 | + /// and [`Self::row_counts`]. |
| 81 | + fn num_containers(&self) -> usize; |
| 82 | + |
| 83 | + /// Return the number of null values for the named column as an |
| 84 | + /// [`UInt64Array`] |
| 85 | + /// |
| 86 | + /// See [`Self::min_values`] for when to return `None` and null values. |
| 87 | + /// |
| 88 | + /// Note: the returned array must contain [`Self::num_containers`] rows |
| 89 | + /// |
| 90 | + /// [`UInt64Array`]: arrow::array::UInt64Array |
| 91 | + fn null_counts(&self, column: &Column) -> Option<ArrayRef>; |
| 92 | + |
| 93 | + /// Return the number of rows for the named column in each container |
| 94 | + /// as an [`UInt64Array`]. |
| 95 | + /// |
| 96 | + /// See [`Self::min_values`] for when to return `None` and null values. |
| 97 | + /// |
| 98 | + /// Note: the returned array must contain [`Self::num_containers`] rows |
| 99 | + /// |
| 100 | + /// [`UInt64Array`]: arrow::array::UInt64Array |
| 101 | + fn row_counts(&self, column: &Column) -> Option<ArrayRef>; |
| 102 | + |
| 103 | + /// Returns [`BooleanArray`] where each row represents information known |
| 104 | + /// about specific literal `values` in a column. |
| 105 | + /// |
| 106 | + /// For example, Parquet Bloom Filters implement this API to communicate |
| 107 | + /// that `values` are known not to be present in a Row Group. |
| 108 | + /// |
| 109 | + /// The returned array has one row for each container, with the following |
| 110 | + /// meanings: |
| 111 | + /// * `true` if the values in `column` ONLY contain values from `values` |
| 112 | + /// * `false` if the values in `column` are NOT ANY of `values` |
| 113 | + /// * `null` if the neither of the above holds or is unknown. |
| 114 | + /// |
| 115 | + /// If these statistics can not determine column membership for any |
| 116 | + /// container, return `None` (the default). |
| 117 | + /// |
| 118 | + /// Note: the returned array must contain [`Self::num_containers`] rows |
| 119 | + fn contained( |
| 120 | + &self, |
| 121 | + column: &Column, |
| 122 | + values: &HashSet<ScalarValue>, |
| 123 | + ) -> Option<BooleanArray>; |
| 124 | +} |
0 commit comments