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.

//! DataFusion functions used to plan and execute `MATCH_RECOGNIZE` expressions.
//!
//! Applications normally install the complete set through the crate root. This
//! module exposes the individual DataFusion scalar, aggregate, and window UDFs for
//! integrations that manage their own [`FunctionRegistry`].
//!
//! # Organization
//!
//! - [`udf`] provides planning placeholders for metadata functions such as
//!   `MATCH_NUMBER`, `CLASSIFIER`, and `EXCLUDED`.
//! - [`udwf`] provides window UDFs for row-relative `FIRST`, `LAST`, `PREV`, and
//!   `NEXT` navigation.
//! - [`udaf`] exposes DataFusion's native `FIRST_VALUE`/`LAST_VALUE` factories
//!   and package-owned `PREV`/`NEXT` aggregates for collapsed-output and
//!   managed-aggregate paths.

use log::debug;

use datafusion::logical_expr::registry::FunctionRegistry;

pub(crate) mod navigation_common;

/// Aggregate navigation factories used when output is collapsed per match.
///
/// DEFINE aggregate lifting uses the same factories.
pub mod udaf;
/// Nullary metadata placeholders normalized to virtual columns during source planning.
pub mod udf;
/// Row-relative navigation window UDFs for `ALL ROWS PER MATCH` output.
pub mod udwf;

/// Registers every `MATCH_RECOGNIZE` function with a [`FunctionRegistry`].
///
/// Registration proceeds in this order: metadata scalar UDFs, navigation window
/// UDFs, then internal navigation aggregate UDFs. A function already registered
/// under the same name in the same family is replaced.
///
/// # Errors
///
/// Returns the first error reported by the registry. Registrations and
/// replacements completed before that error remain in the registry; this
/// function does not roll them back.
pub fn register_all(
    registry: &mut dyn FunctionRegistry,
) -> datafusion_common::Result<()> {
    for function in udf::functions() {
        let name = function.name().to_string();
        if registry.register_udf(function)?.is_some() {
            debug!("Overwrite existing UDF: {name}");
        }
    }

    for function in udwf::functions() {
        let name = function.name().to_string();
        if registry.register_udwf(function)?.is_some() {
            debug!("Overwrite existing window UDF: {name}");
        }
    }

    for function in udaf::functions() {
        let name = function.name().to_string();
        if registry.register_udaf(function)?.is_some() {
            debug!("Overwrite existing aggregate UDF: {name}");
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use datafusion::execution::context::SessionContext;

    #[test]
    fn navigation_factories_register_in_their_function_families() {
        let mut registry = SessionContext::new();
        register_all(&mut registry).unwrap();
        for name in ["first", "last", "prev", "next"] {
            assert_eq!(registry.udwf(name).unwrap().name(), name);
        }
        for name in [
            "__mr_first_agg",
            "__mr_last_agg",
            "__mr_prev_agg",
            "__mr_next_agg",
        ] {
            assert_eq!(registry.udaf(name).unwrap().name(), name);
        }
    }
}