datafusion-match-recognize 0.56.1

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.

//! Compile-time choices for selecting `^` and `$` closure tables.
//!
//! Static dispatch gives anchored and anchor-free scans separate loop versions,
//! avoiding a flag branch on every row.

use crate::nfa::AnchorEnvironment;

/// Selects the compiled anchor closure table for the current row.
pub(crate) trait AnchorPolicy: Copy + Clone {
    /// Return the precomputed anchor environment for the current row, when active.
    fn anchor_environment(&self) -> Option<AnchorEnvironment>;

    /// Update the current virtual row index.
    fn update_row(&mut self, row: usize);
}

/// Policy that ignores all anchors (direct evaluator).
#[derive(Copy, Clone)]
pub(crate) struct IgnoreAnchors;

impl AnchorPolicy for IgnoreAnchors {
    #[inline(always)]
    fn anchor_environment(&self) -> Option<AnchorEnvironment> {
        None
    }

    #[inline(always)]
    fn update_row(&mut self, _row: usize) {}
}

/// Policy that checks anchors against a specific virtual row context.
#[derive(Copy, Clone)]
pub(crate) struct CheckAnchors {
    pub(crate) virt_row: usize,
    pub(crate) total_virt_rows: usize,
    pub(crate) absolute_partition_start: usize,
    pub(crate) has_virtual_end: bool,
}

impl AnchorPolicy for CheckAnchors {
    #[inline(always)]
    fn anchor_environment(&self) -> Option<AnchorEnvironment> {
        let at_start = self.virt_row == self.absolute_partition_start;
        if self.has_virtual_end
            && self.virt_row.checked_add(1) == Some(self.total_virt_rows)
        {
            return Some(if at_start {
                AnchorEnvironment::VirtualStartEndEmpty
            } else {
                AnchorEnvironment::VirtualEnd
            });
        }
        Some(if at_start {
            AnchorEnvironment::PhysicalStart
        } else {
            AnchorEnvironment::PhysicalInterior
        })
    }

    #[inline(always)]
    fn update_row(&mut self, row: usize) {
        self.virt_row = row;
    }
}

#[cfg(test)]
mod tests {
    use super::{AnchorPolicy, CheckAnchors};
    use crate::nfa::AnchorEnvironment;

    #[test]
    fn end_anchor_comparison_is_exact_at_usize_boundary() {
        let mut policy = CheckAnchors {
            virt_row: usize::MAX - 1,
            total_virt_rows: usize::MAX,
            absolute_partition_start: 0,
            has_virtual_end: true,
        };

        assert_eq!(
            policy.anchor_environment(),
            Some(AnchorEnvironment::VirtualEnd)
        );

        policy.update_row(usize::MAX);
        assert_eq!(
            policy.anchor_environment(),
            Some(AnchorEnvironment::PhysicalInterior)
        );
    }
}