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.

//! Compile execution test_cases through the same source expression compiler as SQL.

use crate::planning::predicate::{
    DirectPredicatePlan, VmCompileContext, VmManagedPredicateColumn,
    VmMetadataComparisonColumn, VmPredicateInputColumn, VmPredicateInputRole,
    VmScalarType, VmType, compile_direct_predicate,
};
use datafusion::logical_expr::{Expr, col, lit};

pub(crate) fn compile(
    expr: Expr,
    managed: &[VmScalarType],
    mut inputs: Vec<VmPredicateInputColumn>,
) -> DirectPredicatePlan {
    use VmMetadataComparisonColumn::{MatchNumber, MatchSequenceNumber};
    for (name, source) in [
        ("__mr_match_number", MatchNumber),
        ("__mr_match_sequence_number", MatchSequenceNumber),
    ] {
        inputs.push(VmPredicateInputColumn::new(
            name.into(),
            Some(VmType::non_null(VmScalarType::Int64)),
            VmPredicateInputRole::Metadata(source),
        ));
    }
    let managed = managed
        .iter()
        .enumerate()
        .map(|(slot, ty)| {
            VmManagedPredicateColumn::new(
                slot,
                format!("__mr_managed_agg_{slot}"),
                Some(*ty),
            )
        })
        .collect();
    let context = VmCompileContext::try_new(managed, inputs, Default::default()).unwrap();
    compile_direct_predicate(&expr, &context).unwrap()
}

pub(crate) fn positive_match_number() -> DirectPredicatePlan {
    compile(col("__mr_match_number").gt_eq(lit(1_i64)), &[], vec![])
}

pub(crate) fn static_boolean(index: usize, scalar: bool) -> DirectPredicatePlan {
    let inputs = (0..=index)
        .map(|i| {
            let role = if scalar {
                VmPredicateInputRole::StaticScalar { scalar_idx: i }
            } else {
                VmPredicateInputRole::StaticPredicate { bool_idx: i }
            };
            VmPredicateInputColumn::new(
                format!("flag_{i}"),
                Some(VmType::non_null(VmScalarType::Bool)),
                role,
            )
        })
        .collect();
    compile(col(format!("flag_{index}")), &[], inputs)
}