use rtlola_reporting::RtLolaError;
use super::{builder::Builder, ChangeSet, ExprOrigin, SynSugar};
use crate::ast::{Expression, ExpressionKind, RtLolaAst, WindowOperation};
#[derive(Debug, Clone)]
pub(crate) struct AggrMethodToWindow {}
impl AggrMethodToWindow {
fn apply(&self, expr: &Expression, ast: &RtLolaAst) -> ChangeSet {
match &expr.kind {
ExpressionKind::Method(base, name, _types, arguments) => {
let aggregation = match name.name.name.as_ref() {
"count" => WindowOperation::Count,
"min" => WindowOperation::Min,
"max" => WindowOperation::Max,
"sum" => WindowOperation::Sum,
"avg" => WindowOperation::Average,
"integral" => WindowOperation::Integral,
"var" => WindowOperation::Variance,
"cov" => WindowOperation::Covariance,
"sd" => WindowOperation::StandardDeviation,
"med" => WindowOperation::NthPercentile(50),
_ => return ChangeSet::empty(),
};
let builder = Builder::new(expr.span, ast);
let target_stream = base.clone();
let wait = false;
let duration = arguments[0].clone();
let new_expr = builder.sliding_window(*target_stream, duration, wait, aggregation);
ChangeSet::replace_current_expression(new_expr)
}
_ => ChangeSet::empty(),
}
}
}
impl SynSugar for AggrMethodToWindow {
fn desugarize_expr<'a>(
&self,
exp: &'a Expression,
ast: &'a RtLolaAst,
_stream: usize,
_origin: ExprOrigin,
) -> Result<ChangeSet, RtLolaError> {
Ok(self.apply(exp, ast))
}
}