use crate::core::{AbsoluteRow, SymbolId};
use datafusion::common::Result;
use super::aggregate_engine::AggregateEngine;
use crate::execution::matching::DefineCachePolicy;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TransitionDecision {
Reject,
AcceptFinal,
CheckHistory,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PredicateCacheProbe {
Disabled,
Miss,
Hit(TransitionDecision),
}
impl PredicateCacheProbe {
#[inline]
pub(crate) fn probe<Lookup>(
policy: DefineCachePolicy,
registers_only: bool,
lookup: Lookup,
) -> PredicateCacheProbe
where
Lookup: FnOnce() -> Option<TransitionDecision>,
{
if registers_only || !policy.lookup_enabled() {
return PredicateCacheProbe::Disabled;
}
PredicateCacheProbe::from_enabled_lookup(lookup())
}
#[inline]
pub(crate) const fn from_enabled_lookup(cached: Option<TransitionDecision>) -> Self {
match cached {
Some(decision) => Self::Hit(decision),
None => Self::Miss,
}
}
#[inline]
pub(crate) const fn cached_decision(self) -> Option<TransitionDecision> {
match self {
Self::Hit(decision) => Some(decision),
Self::Disabled | Self::Miss => None,
}
}
#[inline]
pub(crate) const fn should_store(self) -> bool {
matches!(self, Self::Miss)
}
}
impl<'a, 'state> AggregateEngine<'a, 'state> {
#[inline]
pub(crate) fn finish_transition_decision<Evaluate>(
&mut self,
sym_id: SymbolId,
match_start: AbsoluteRow,
excluded: bool,
cache_probe: PredicateCacheProbe,
or_pass: bool,
evaluate: Evaluate,
) -> Result<TransitionDecision>
where
Evaluate: FnOnce(&mut Self) -> Result<TransitionDecision>,
{
self.state.evaluation.current_match_start_row = match_start;
let outcome = if let Some(decision) = cache_probe.cached_decision() {
decision
} else if or_pass {
TransitionDecision::AcceptFinal
} else {
evaluate(self)?
};
if cache_probe.should_store() {
self.cache_result(sym_id, match_start, excluded, outcome);
}
Ok(outcome)
}
}