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.

//! Pattern-aware window UDFs for `FIRST`, `LAST`, `PREV`, and `NEXT`.
//!
//! `FIRST`/`LAST` look up match edges. `PREV`/`NEXT` apply physical row offsets
//! from current or classifier-selected anchors; their targets can lie outside
//! the selected match. Planning uses these functions for row-level output and
//! supplies any internal classifier mask needed by a symbol-qualified call.
//!
//! Direct `PREV`/`NEXT` UDF calls take `(value, offset, default)` and an optional
//! classifier mask. SQL planning supplies omitted offset/default arguments;
//! the UDF signature itself requires all three.

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

use datafusion::logical_expr::WindowUDF;

use crate::functions::navigation_common::NavigationKind;
use wrappers::NavigationWindowUdf;

mod evaluators;
mod wrappers;

fn new_navigation_udwf(kind: NavigationKind) -> Arc<WindowUDF> {
    Arc::new(WindowUDF::from(NavigationWindowUdf::new(kind)))
}

/// Returns the window UDF registered as `first` for pattern-aware `FIRST`
/// navigation.
pub fn mr_first_udwf() -> Arc<WindowUDF> {
    static INSTANCE: LazyLock<Arc<WindowUDF>> =
        LazyLock::new(|| new_navigation_udwf(NavigationKind::First));
    Arc::clone(&INSTANCE)
}

/// Returns the window UDF registered as `last` for pattern-aware `LAST`
/// navigation.
pub fn mr_last_udwf() -> Arc<WindowUDF> {
    static INSTANCE: LazyLock<Arc<WindowUDF>> =
        LazyLock::new(|| new_navigation_udwf(NavigationKind::Last));
    Arc::clone(&INSTANCE)
}

/// Returns the window UDF registered as `prev` for pattern-aware `PREV`
/// navigation.
pub fn mr_prev_udwf() -> Arc<WindowUDF> {
    static INSTANCE: LazyLock<Arc<WindowUDF>> =
        LazyLock::new(|| new_navigation_udwf(NavigationKind::Prev));
    Arc::clone(&INSTANCE)
}

/// Returns the window UDF registered as `next` for pattern-aware `NEXT`
/// navigation.
pub fn mr_next_udwf() -> Arc<WindowUDF> {
    static INSTANCE: LazyLock<Arc<WindowUDF>> =
        LazyLock::new(|| new_navigation_udwf(NavigationKind::Next));
    Arc::clone(&INSTANCE)
}

/// Return the `FIRST`, `LAST`, `PREV`, and `NEXT` window registrations in
/// registration order.
pub fn functions() -> Vec<Arc<WindowUDF>> {
    vec![
        mr_first_udwf(),
        mr_last_udwf(),
        mr_prev_udwf(),
        mr_next_udwf(),
    ]
}