datafusion-match-recognize 0.56.0

MATCH_RECOGNIZE extension for 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.

use std::sync::Arc;

use datafusion::logical_expr::{AggregateUDF, WindowUDF};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum NavigationFunctionFamily {
    First,
    Last,
    Prev,
    Next,
}

impl NavigationFunctionFamily {
    pub(crate) fn from_name(name: &str) -> Option<Self> {
        match name.to_ascii_lowercase().as_str() {
            "first" | "first_value" | "mr_first" | "__mr_first_agg" => Some(Self::First),
            "last" | "last_value" | "mr_last" | "__mr_last_agg" => Some(Self::Last),
            "prev" | "mr_prev" | "__mr_prev_agg" => Some(Self::Prev),
            "next" | "mr_next" | "__mr_next_agg" => Some(Self::Next),
            _ => None,
        }
    }

    pub(crate) fn internal_udaf(self) -> Arc<AggregateUDF> {
        match self {
            Self::First => datafusion_functions_aggregate::first_last::first_value_udaf(),
            Self::Last => datafusion_functions_aggregate::first_last::last_value_udaf(),
            Self::Prev => crate::functions::udaf::mr_prev_udaf(),
            Self::Next => crate::functions::udaf::mr_next_udaf(),
        }
    }

    pub(crate) fn offset_udaf(self) -> Arc<AggregateUDF> {
        match self {
            Self::First => crate::functions::udaf::mr_first_offset_udaf(),
            Self::Last => crate::functions::udaf::mr_last_offset_udaf(),
            Self::Prev | Self::Next => {
                unreachable!("offset_udaf is only defined for FIRST/LAST")
            }
        }
    }

    /// Resolve a matcher navigation aggregate by concrete implementation.
    pub(crate) fn from_aggregate_udf(source: &AggregateUDF) -> Option<Self> {
        use crate::functions::udaf;

        let same = |known: Arc<AggregateUDF>| source == known.as_ref();
        if same(datafusion_functions_aggregate::first_last::first_value_udaf())
            || same(udaf::mr_first_offset_udaf())
        {
            Some(Self::First)
        } else if same(datafusion_functions_aggregate::first_last::last_value_udaf())
            || same(udaf::mr_last_offset_udaf())
        {
            Some(Self::Last)
        } else if same(udaf::mr_prev_udaf()) {
            Some(Self::Prev)
        } else if same(udaf::mr_next_udaf()) {
            Some(Self::Next)
        } else {
            None
        }
    }

    /// Resolve a supported Deferred window implementation by semantic identity.
    pub(crate) fn from_window_udf(source: &WindowUDF) -> Option<Self> {
        use crate::functions::udwf;
        use datafusion::functions_window::nth_value::{
            first_value_udwf, last_value_udwf,
        };

        let same = |known: Arc<WindowUDF>| source == known.as_ref();
        if same(udwf::mr_first_udwf()) || same(first_value_udwf()) {
            Some(Self::First)
        } else if same(udwf::mr_last_udwf()) || same(last_value_udwf()) {
            Some(Self::Last)
        } else if same(udwf::mr_prev_udwf()) {
            Some(Self::Prev)
        } else if same(udwf::mr_next_udwf()) {
            Some(Self::Next)
        } else {
            None
        }
    }
}