//! DataFusion translations for logical operators.

use datafusion::logical_expr::{BinaryExpr, Expr as DFExpr, Operator as DFOperator};

use hamelin_lib::func::defs::{And, Not, Or};

use super::DataFusionTranslationRegistry;

pub fn register(registry: &mut DataFusionTranslationRegistry) {
    // NOT x -> NOT(x)
    registry.register::<Not>(|mut params| {
        let x = params.take()?.expr;
        Ok(DFExpr::Not(Box::new(x)))
    });

    // x AND y -> x AND y
    registry.register::<And>(|mut params| {
        let left = params.take()?.expr;
        let right = params.take()?.expr;
        Ok(DFExpr::BinaryExpr(BinaryExpr::new(
            Box::new(left),
            DFOperator::And,
            Box::new(right),
        )))
    });

    // x OR y -> x OR y
    registry.register::<Or>(|mut params| {
        let left = params.take()?.expr;
        let right = params.take()?.expr;
        Ok(DFExpr::BinaryExpr(BinaryExpr::new(
            Box::new(left),
            DFOperator::Or,
            Box::new(right),
        )))
    });
}
