|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::ops::Range; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +use bit_vec::BitVec; |
| 8 | +use vortex_array::MaskFuture; |
| 9 | +use vortex_array::VortexSessionExecute; |
| 10 | +use vortex_error::VortexResult; |
| 11 | +use vortex_layout::plan::PlanExecutionContext; |
| 12 | +use vortex_layout::plan::PlanRef; |
| 13 | +use vortex_layout::scan::FilterExpr; |
| 14 | +use vortex_mask::Mask; |
| 15 | + |
| 16 | +/// Controls how a scan executes top-level filter conjunctions. |
| 17 | +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 18 | +pub enum FilterMode { |
| 19 | + /// Optimizes the complete predicate as one plan, allowing independent branches to run in |
| 20 | + /// parallel. |
| 21 | + #[default] |
| 22 | + Parallel, |
| 23 | + /// Splits top-level conjunctions and executes them as an adaptively ordered mask chain. |
| 24 | + Adaptive, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Clone)] |
| 28 | +pub(crate) enum FilterPlan { |
| 29 | + Parallel(PlanRef), |
| 30 | + Adaptive { |
| 31 | + filter: Arc<FilterExpr>, |
| 32 | + plans: Arc<[PlanRef]>, |
| 33 | + }, |
| 34 | +} |
| 35 | + |
| 36 | +impl FilterPlan { |
| 37 | + pub(crate) fn parallel(plan: PlanRef) -> Self { |
| 38 | + Self::Parallel(plan) |
| 39 | + } |
| 40 | + |
| 41 | + pub(crate) fn adaptive(filter: FilterExpr, plans: Vec<PlanRef>) -> Self { |
| 42 | + Self::Adaptive { |
| 43 | + filter: Arc::new(filter), |
| 44 | + plans: plans.into(), |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) fn plans(&self) -> Vec<&PlanRef> { |
| 49 | + match self { |
| 50 | + Self::Parallel(plan) => vec![plan], |
| 51 | + Self::Adaptive { plans, .. } => plans.iter().collect(), |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + pub(crate) fn execute( |
| 56 | + &self, |
| 57 | + execution: &PlanExecutionContext, |
| 58 | + row_range: &Range<u64>, |
| 59 | + row_mask: Mask, |
| 60 | + ) -> VortexResult<MaskFuture> { |
| 61 | + match self { |
| 62 | + Self::Parallel(filter) => { |
| 63 | + let predicate = |
| 64 | + filter.execute(execution, row_range, MaskFuture::ready(row_mask.clone()))?; |
| 65 | + let session = execution.session().clone(); |
| 66 | + Ok(MaskFuture::new(row_mask.len(), async move { |
| 67 | + let predicate = predicate.await?; |
| 68 | + let mut execution = session.create_execution_ctx(); |
| 69 | + let predicate: Mask = predicate.null_as_false().execute(&mut execution)?; |
| 70 | + Ok(row_mask.intersect_by_rank(&predicate)) |
| 71 | + })) |
| 72 | + } |
| 73 | + Self::Adaptive { filter, plans } => { |
| 74 | + let execution = execution.clone(); |
| 75 | + let row_range = row_range.clone(); |
| 76 | + let filter = Arc::clone(filter); |
| 77 | + let plans = Arc::clone(plans); |
| 78 | + Ok(MaskFuture::new(row_mask.len(), async move { |
| 79 | + let mut row_mask = row_mask; |
| 80 | + let mut remaining = BitVec::from_elem(plans.len(), true); |
| 81 | + while let Some(index) = filter.next_conjunct(&remaining) { |
| 82 | + remaining.set(index, false); |
| 83 | + if row_mask.all_false() { |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + let input_rows = row_mask.true_count(); |
| 88 | + let predicate = plans[index].execute( |
| 89 | + &execution, |
| 90 | + &row_range, |
| 91 | + MaskFuture::ready(row_mask.clone()), |
| 92 | + )?; |
| 93 | + let predicate = predicate.await?; |
| 94 | + let mut ctx = execution.session().create_execution_ctx(); |
| 95 | + let predicate: Mask = predicate.null_as_false().execute(&mut ctx)?; |
| 96 | + row_mask = row_mask.intersect_by_rank(&predicate); |
| 97 | + filter.report_selectivity(index, row_mask.density()); |
| 98 | + tracing::trace!( |
| 99 | + target: "vortex_scan_v2::execution", |
| 100 | + conjunct = index, |
| 101 | + input_rows, |
| 102 | + output_rows = row_mask.true_count(), |
| 103 | + "applied an adaptive plan filter conjunct" |
| 104 | + ); |
| 105 | + } |
| 106 | + Ok(row_mask) |
| 107 | + })) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments