vortex_array/operator/
optimize.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use itertools::Itertools;
7use vortex_error::VortexResult;
8
9use crate::operator::{Operator, OperatorRef};
10
11impl dyn Operator + '_ {
12    /// Optimize the operator tree rooted at this operator by applying local
13    /// optimizations such as reducing redundant operators.
14    pub fn optimize(self: Arc<Self>) -> VortexResult<OperatorRef> {
15        let children = self
16            .children()
17            .iter()
18            .map(|child| child.clone().optimize())
19            .try_collect()?;
20
21        let mut operator = self.with_children(children)?;
22        operator = operator.reduce_children()?.unwrap_or(operator);
23
24        let parent = operator.clone();
25        for (idx, child) in operator.children().iter().enumerate() {
26            if let Some(new_operator) = child.reduce_parent(parent.clone(), idx)? {
27                return Ok(new_operator);
28            }
29        }
30
31        Ok(operator)
32    }
33}