Skip to main content

vortex_expr/forms/
extract_conjuncts.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::scalar_fn::builtin::boolean::Boolean;
5use vortex_array::scalar_fn::builtin::operators::BooleanOperator;
6
7use crate::Expression;
8
9/// Converting an expression to a conjunctive normal form can lead to a large number of expression
10/// nodes.
11/// For now, we will just extract the conjuncts from the expression, and return a vector of conjuncts.
12/// We could look at try cnf with a size cap and otherwise return the original conjuncts.
13pub fn conjuncts(expr: &Expression) -> Vec<Expression> {
14    let mut conjuncts = vec![];
15    conjuncts_impl(expr, &mut conjuncts);
16    if conjuncts.is_empty() {
17        conjuncts.push(expr.clone());
18    }
19    conjuncts
20}
21
22fn conjuncts_impl(expr: &Expression, conjuncts: &mut Vec<Expression>) {
23    if let Some(operator) = expr.as_opt::<Boolean>()
24        && *operator == BooleanOperator::And
25    {
26        conjuncts_impl(expr.child(0), conjuncts);
27        conjuncts_impl(expr.child(1), conjuncts);
28    } else {
29        conjuncts.push(expr.clone())
30    }
31}