datafusion-match-recognize 0.55.0

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.

//! Supported Arrow types at the shared semantic planning boundary.

use datafusion::arrow::datatypes::DataType;
use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion};
use datafusion::common::{DFSchema, DataFusionError, Result, SchemaError, plan_err};
use datafusion::logical_expr::{Expr, ExprSchemable};

/// Supported types that can contain logical NULLs in a required Arrow field.
pub fn can_have_nulls_outside_bitmap(data_type: &DataType) -> bool {
    matches!(
        data_type,
        DataType::Null | DataType::Dictionary(..) | DataType::RunEndEncoded(..)
    )
}

pub(crate) fn validate_data_type(data_type: &DataType) -> Result<()> {
    match data_type {
        DataType::Union(_, _) => plan_err!(
            "MATCH_RECOGNIZE does not support Arrow Union types (including nested Union types); see https://github.com/apache/arrow-rs/issues/11598"
        ),
        DataType::List(field)
        | DataType::ListView(field)
        | DataType::LargeList(field)
        | DataType::LargeListView(field)
        | DataType::FixedSizeList(field, _)
        | DataType::Map(field, _) => validate_data_type(field.data_type()),
        DataType::Struct(fields) => fields
            .iter()
            .try_for_each(|field| validate_data_type(field.data_type())),
        DataType::Dictionary(key, value) => {
            validate_data_type(key)?;
            validate_data_type(value)
        }
        DataType::RunEndEncoded(run_ends, values) => {
            validate_data_type(run_ends.data_type())?;
            validate_data_type(values.data_type())
        }
        _ => Ok(()),
    }
}

pub(crate) fn validate_schema(schema: &DFSchema) -> Result<()> {
    schema
        .fields()
        .iter()
        .try_for_each(|field| validate_data_type(field.data_type()))
}

pub(crate) fn validate_expr(expr: &Expr, schema: &DFSchema) -> Result<()> {
    validate_expr_inner(expr, schema, false)
}

/// Validate source expressions retained after their input columns were pruned.
///
/// Source lowering already checks their inferred types before preparation. At
/// the checked node boundary the prepared field carries the resulting type,
/// while the preserved source expression can no longer resolve all its inputs.
pub(crate) fn validate_retained_expr(expr: &Expr, schema: &DFSchema) -> Result<()> {
    validate_expr_inner(expr, schema, true)
}

fn validate_expr_inner(
    expr: &Expr,
    schema: &DFSchema,
    allow_pruned_columns: bool,
) -> Result<()> {
    expr.apply(|node| {
        // Inspect the typed leaves and function results, including those hidden
        // by a Boolean expression such as union_udf() IS NULL. Operators only
        // propagate operand types or produce Boolean; inferring every operator
        // again here would make deep arithmetic chains quadratic to validate.
        if matches!(
            node,
            Expr::Literal(_, _)
                | Expr::Cast(_)
                | Expr::TryCast(_)
                | Expr::ScalarFunction(_)
                | Expr::AggregateFunction(_)
                | Expr::WindowFunction(_)
                | Expr::HigherOrderFunction(_)
                | Expr::ScalarVariable(_, _)
                | Expr::OuterReferenceColumn(_, _)
                | Expr::ScalarSubquery(_)
                | Expr::Placeholder(_)
                | Expr::LambdaVariable(_)
        ) {
            match node.get_type(schema) {
                Ok(data_type) => validate_data_type(&data_type)?,
                Err(DataFusionError::SchemaError(error, _))
                    if allow_pruned_columns
                        && matches!(
                            error.as_ref(),
                            SchemaError::FieldNotFound { .. }
                        ) => {}
                Err(error) => return Err(error),
            }
        }
        Ok(TreeNodeRecursion::Continue)
    })?;
    Ok(())
}

#[cfg(test)]
mod tests;