vortex_array/expr/forms/
extract_conjuncts.rs

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