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.

//! Ordered transformations from a lowered source request to its final logical
//! plan.
//!
//! The phases prepare matcher input, finish `DEFINE`, assemble the semantic
//! node, and finalize output. Each phase passes a typed result to the next,
//! carrying both the source information and the output work that remains.

mod input_keys;
mod node_assembly;
mod output_finalization;

pub(crate) use crate::planning::define::preparation::{
    prepare_input_from_logical_spec, process_defines,
};
#[cfg(feature = "substrait")]
pub(crate) use input_keys::prepared_key_source;
pub(crate) use input_keys::{
    materialize_partition_keys, materialize_volatile_keys, normalize_partition_keys,
};
pub(crate) use node_assembly::{
    MatcherNodeSpec, MatcherOutputMode, NodeOutputSpec, assemble_node_from_logical_parts,
};
pub(crate) use output_finalization::finalize_output_with_navigation_offsets;

use crate::core::RowsPerMatch;
use crate::planning::NamedExpr;
use crate::planning::logical::MatchRecognize;
use datafusion::logical_expr::{Expr, LogicalPlan};

/// Predicate prepared for input projection, on-demand evaluation, or candidate evaluation.
pub enum SymbolPlanPredicate {
    Static(Expr),
    /// Row-local, evaluated by the matcher so only demanded errors are observable.
    Demanded(Expr),
    MatchAware(Box<crate::planning::define::preparation::CandidatePredicate>),
}
impl SymbolPlanPredicate {
    pub fn static_expr(&self) -> Option<&Expr> {
        match self {
            Self::Static(expr) | Self::Demanded(expr) => Some(expr),
            Self::MatchAware(_) => None,
        }
    }
}

/// Everything later planning steps need to know about one pattern variable.
pub struct SymbolPlan {
    pub name: String,
    pub predicate: SymbolPlanPredicate,
}

/// Input plan and symbol information produced by the first planning step.
pub struct PreparedInput {
    /// The input logical plan with placeholder projections.
    pub input: LogicalPlan,
    /// Default matcher output mode. `ONE ROW` is represented as `ALL ROWS` for
    /// measure programs that still need downstream DataFusion finalization.
    pub matcher_rows_per_match: RowsPerMatch,
    /// Preserve the source requirement even when preparation resolves candidate reads.
    pub requires_match_aware: bool,
    /// Canonical pattern symbols, including those without explicit DEFINEs.
    pub pattern_vars: std::collections::BTreeSet<String>,
    /// Managed state extracted from candidate predicates; empty for static queries.
    pub managed_aggregates: Vec<crate::planning::ManagedAggregate>,
    /// Prepared input expressions retained for source-export validation.
    pub prepared_exprs: Vec<NamedExpr>,
    /// Internal per-symbol planning records.
    pub symbol_plans: Vec<SymbolPlan>,
    /// Disjointness and implication facts from the original `DEFINE` conditions.
    pub symbol_relationships: crate::planning::symbol_relationships::SymbolRelationships,
}

/// Finished `DEFINE` expressions and the plan that calculates their row-level
/// inputs.
pub struct ProcessedDefines {
    /// The input plan with window functions and any enabled static extractions applied.
    pub input: LogicalPlan,
    /// Finalized per-symbol planning records carried directly into node assembly.
    pub symbol_plans: Vec<SymbolPlan>,
    /// Managed state extracted from candidate predicates; empty for static queries.
    pub managed_aggregates: Vec<crate::planning::ManagedAggregate>,
    /// Prepared candidate subexpressions; empty for static queries.
    pub static_extractions: Vec<crate::planning::static_extraction::StaticExtraction>,
    /// Initial lifted row expressions passed to the logical node.
    pub prepared_exprs: Vec<NamedExpr>,
    /// Disjointness and implication facts from the original conditions.
    pub symbol_relationships: crate::planning::symbol_relationships::SymbolRelationships,
}

/// A `MatchRecognize` node plus the expressions needed to build its final output.
pub struct AssembledNode {
    /// Checked matcher before conversion to a general logical plan.
    pub matcher: MatchRecognize,
    /// Lowered `MEASURES` expressions and their output names.
    pub measures_exprs: Vec<NamedExpr>,
    /// Prepared partition keys used by downstream ONE ROW grouping.
    pub logical_partition_by: Vec<Expr>,
    /// Whether the pattern has exclusions (for filtering).
    pub has_exclusions: bool,
}