use reifydb_core::{interface::catalog::flow::OperatorId, row::OperatorLateness};
use reifydb_transaction::transaction::Transaction;
use reifydb_value::Result;
use crate::{
expression::Expression,
flow::{
aggregate::AggregateContext,
compiler::{CompileOperator, FlowCompiler, operator::aggregate_validation::validate_flow_aggregations},
operator::OperatorDef::Aggregate,
},
nodes::AggregateNode,
query::QueryPlan,
};
pub(crate) struct AggregateCompiler {
pub input: Box<QueryPlan>,
pub by: Vec<Expression>,
pub map: Vec<Expression>,
pub ttl: Option<OperatorLateness>,
}
impl From<AggregateNode> for AggregateCompiler {
fn from(node: AggregateNode) -> Self {
Self {
input: node.input,
by: node.by,
map: node.map,
ttl: node.ttl,
}
}
}
impl CompileOperator for AggregateCompiler {
fn compile(self, compiler: &mut FlowCompiler, txn: &mut Transaction<'_>) -> Result<OperatorId> {
validate_flow_aggregations(&compiler.routines, &self.map, AggregateContext::Grouped)?;
let input_node = compiler.compile_plan(txn, *self.input)?;
let node_id = compiler.add_node(
txn,
Aggregate {
by: self.by,
map: self.map,
},
)?;
compiler.add_edge(txn, &input_node, &node_id)?;
compiler.write_operator_settings(txn, node_id, self.ttl)?;
Ok(node_id)
}
}