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.

//! Aggregate function factories used by match-level navigation.
//!
//! Zero-offset `FIRST` and `LAST` use DataFusion's native aggregate
//! implementations. Positive-offset `FIRST`/`LAST` and `PREV`/`NEXT` (including
//! compound `PREV`/`NEXT` over `FIRST`/`LAST`) use package-owned aggregates
//! with row-pattern anchor semantics.

use std::sync::{Arc, LazyLock};

use datafusion::logical_expr::AggregateUDF;

use crate::functions::navigation_common::NavigationKind;
use navigation::NavigationAgg;

// Shared implementation for package-owned navigation aggregates.
pub(crate) mod navigation;

/// Return DataFusion's native `first_value` aggregate used for zero-offset match-level `FIRST`.
#[cfg(test)]
pub use datafusion_functions_aggregate::first_last::first_value_udaf as mr_first_udaf;
/// Return DataFusion's native `last_value` aggregate used for zero-offset match-level `LAST`.
#[cfg(test)]
pub use datafusion_functions_aggregate::first_last::last_value_udaf as mr_last_udaf;

fn new_navigation_udaf(kind: NavigationKind) -> Arc<AggregateUDF> {
    Arc::new(AggregateUDF::from(NavigationAgg::new(kind)))
}

/// Returns the internal aggregate UDF that evaluates `FIRST(expr, offset)`
/// with a logical offset over match-level aggregate state.
pub fn mr_first_offset_udaf() -> Arc<AggregateUDF> {
    static INSTANCE: LazyLock<Arc<AggregateUDF>> =
        LazyLock::new(|| new_navigation_udaf(NavigationKind::First));
    Arc::clone(&INSTANCE)
}

/// Returns the internal aggregate UDF that evaluates `LAST(expr, offset)`
/// with a logical offset over match-level aggregate state.
pub fn mr_last_offset_udaf() -> Arc<AggregateUDF> {
    static INSTANCE: LazyLock<Arc<AggregateUDF>> =
        LazyLock::new(|| new_navigation_udaf(NavigationKind::Last));
    Arc::clone(&INSTANCE)
}

/// Returns the internal aggregate UDF that evaluates pattern-aware `PREV` with
/// match-level aggregate state.
pub fn mr_prev_udaf() -> Arc<AggregateUDF> {
    static INSTANCE: LazyLock<Arc<AggregateUDF>> =
        LazyLock::new(|| new_navigation_udaf(NavigationKind::Prev));
    Arc::clone(&INSTANCE)
}

/// Returns the internal aggregate UDF that evaluates pattern-aware `NEXT` with
/// match-level aggregate state.
pub fn mr_next_udaf() -> Arc<AggregateUDF> {
    static INSTANCE: LazyLock<Arc<AggregateUDF>> =
        LazyLock::new(|| new_navigation_udaf(NavigationKind::Next));
    Arc::clone(&INSTANCE)
}

/// Return the package-owned navigation aggregate registrations.
pub fn functions() -> Vec<Arc<AggregateUDF>> {
    vec![
        mr_first_offset_udaf(),
        mr_last_offset_udaf(),
        mr_prev_udaf(),
        mr_next_udaf(),
    ]
}