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.

//! SQL-to-source planning for MATCH_RECOGNIZE.

use datafusion::common::Result;
use datafusion::logical_expr::planner::{
    PlannedRelation, RelationPlanner, RelationPlannerContext, RelationPlanning,
};
use datafusion::sql::sqlparser::ast::TableFactor;

/// RelationPlanner implementation for MATCH_RECOGNIZE.
///
/// This planner handles `MATCH_RECOGNIZE` table expressions. It converts the SQL
/// syntax tree to a SQL-independent source specification, then passes that
/// specification to the shared planning module.
#[derive(Debug)]
pub struct MatchRecognizePlanner;

impl RelationPlanner for MatchRecognizePlanner {
    fn plan_relation(
        &self,
        table_factor: TableFactor,
        context: &mut dyn RelationPlannerContext,
    ) -> Result<RelationPlanning> {
        if let TableFactor::MatchRecognize {
            table,
            partition_by,
            order_by,
            measures,
            rows_per_match,
            after_match_skip,
            pattern,
            symbols,
            alias,
        } = table_factor
        {
            // Guard the outer pattern and clause expressions before moving the
            // child table into recursive planning so nested MATCH_RECOGNIZE
            // nodes dismantle their own trees on return or early error without
            // retaining a second copy here.
            let parts = crate::sql::adapter::SqlMatchRecognizeParts {
                partition_by,
                order_by,
                measures,
                rows_per_match,
                after_match_skip,
                pattern,
                symbols,
            };
            let input = context.plan(*table)?;
            let parsed_clause = crate::sql::adapter::build_parsed_match_clause_parts(
                context.context_provider(),
                parts,
            )?;
            let logical_plan = crate::sql::adapter::plan_sql_match_recognize(
                context,
                input,
                parsed_clause,
            )?;

            Ok(RelationPlanning::Planned(Box::new(PlannedRelation {
                plan: logical_plan,
                alias,
            })))
        } else {
            Ok(RelationPlanning::Original(Box::new(table_factor)))
        }
    }
}