datafusion-match-recognize 0.55.0

SQL MATCH_RECOGNIZE row-pattern matching for Apache DataFusion
Documentation
// Licensed under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2025-Present Datadog, Inc.

//! Chooses accept, reject, or Deferred work for one NFA transition.
//!
//! Executable construction has already selected one evaluator, cache policy,
//! aggregate-state program, and precheck program for every symbol. This module
//! applies cached and static-check results, then invokes that selected evaluator.

use crate::core::{AbsoluteRow, SymbolId};
use datafusion::common::Result;

use super::aggregate_engine::AggregateEngine;
use crate::execution::matching::DefineCachePolicy;

/// Outcome of transition predicate evaluation before transition side effects.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TransitionDecision {
    Reject,
    AcceptFinal,
    CheckHistory,
}

/// Result of the one define-cache lookup allowed for a transition attempt.
///
/// `Miss` records that lookup was reusable when the transition began, allowing
/// the caller to store the evaluated result for the same row generation.
#[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> {
    /// Finish the representation-independent half of one predicate decision.
    /// Updates have already run for cache hits that accept; cached rejects are
    /// returned before callers construct or mutate successor 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)
    }
}