vortex_expr/forms/
extract_conjuncts.rs

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