use oxisql_core::OxiSqlError;
use sqlparser::ast::{SetExpr, SetOperator, SetQuantifier};
use crate::{LogicalPlan, SetOpType};
pub(crate) fn map_set_operator(op: &SetOperator) -> Result<SetOpType, OxiSqlError> {
match op {
SetOperator::Union => Ok(SetOpType::Union),
SetOperator::Intersect => Ok(SetOpType::Intersect),
SetOperator::Except => Ok(SetOpType::Except),
SetOperator::Minus => Ok(SetOpType::Except), }
}
pub(crate) fn quantifier_is_all(q: &SetQuantifier) -> bool {
matches!(q, SetQuantifier::All | SetQuantifier::AllByName)
}
pub(crate) fn plan_set_operation(
op: &SetOperator,
set_quantifier: &SetQuantifier,
left: &SetExpr,
right: &SetExpr,
planner: &dyn Fn(&SetExpr) -> Result<LogicalPlan, OxiSqlError>,
) -> Result<LogicalPlan, OxiSqlError> {
let set_op = map_set_operator(op)?;
let all = quantifier_is_all(set_quantifier);
let left_plan = planner(left)?;
let right_plan = planner(right)?;
Ok(LogicalPlan::SetOp {
op: set_op,
all,
left: Box::new(left_plan),
right: Box::new(right_plan),
})
}