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.

//! Shared expression preparation for executable program construction.

use std::sync::Arc;

use crate::planning::logical::LogicalSymbolId;
use datafusion::arrow::datatypes::{DataType, Schema};
use datafusion::catalog::Session;
use datafusion::common::{DFSchema, DataFusionError, Result, plan_err};
use datafusion::logical_expr::physical_planning_context::PhysicalPlanningContext;
use datafusion::logical_expr::{Expr, simplify::SimplifyContext};
use datafusion::optimizer::simplify_expressions::ExprSimplifier;
use datafusion::physical_expr::PhysicalExpr;
use datafusion::physical_planner::PhysicalPlanner;

/// Classify caller-controlled DEFINE binding failures at their owning boundary.
fn predicate_plan_error(
    error: DataFusionError,
    symbol_id: LogicalSymbolId,
) -> DataFusionError {
    DataFusionError::Plan(format!("MATCH_RECOGNIZE DEFINE for {symbol_id:?}: {error}"))
}

/// Reject a physical DEFINE expression whose result cannot drive a predicate.
pub(crate) fn validate_predicate_type(
    expression: &dyn PhysicalExpr,
    schema: &Schema,
    symbol_id: LogicalSymbolId,
) -> Result<()> {
    let data_type = expression
        .data_type(schema)
        .map_err(|error| predicate_plan_error(error, symbol_id))?;
    match data_type {
        DataType::Boolean => Ok(()),
        data_type => plan_err!(
            "MATCH_RECOGNIZE DEFINE for {symbol_id:?} has type {data_type}, expected Boolean"
        ),
    }
}

/// Coerce and plan a DEFINE before validating it against its execution schema.
pub(crate) fn plan_predicate_expr(
    expr: Expr,
    schema: &DFSchema,
    symbol_id: LogicalSymbolId,
    planner: &dyn PhysicalPlanner,
    state: &dyn Session,
    planning_ctx: &PhysicalPlanningContext,
) -> Result<Arc<dyn PhysicalExpr>> {
    // Demanded static DEFINEs bypass the input projection's analyzer. Use the
    // same coercion as Session::create_physical_expr, including CASE/function
    // arguments and typed NULLs, without folding or evaluating the predicate.
    let context = SimplifyContext::builder()
        .with_schema(Arc::new(schema.clone()))
        .with_config_options(Arc::new(state.config_options().clone()))
        .build();
    ExprSimplifier::new(context)
        .coerce(expr, schema)
        .and_then(|expr| planner.create_physical_expr(&expr, schema, state, planning_ctx))
        .map_err(|error| predicate_plan_error(error, symbol_id))
}