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.

//! Round trips and wire-marker checks for candidate-dependent DEFINEs.

use super::*;

#[tokio::test]
async fn candidate_define_exports_marker_and_roundtrips() -> Result<()> {
    let ctx = context_with_input().await?;
    let payload = candidate_payload_with_marker();
    let source = from_substrait_plan_with_match_recognize(
        &ctx.state(),
        &plan_for_payload(payload),
    )
    .await?;
    let exported = to_substrait_plan_with_match_recognize(&source, &ctx.state())?;
    assert!(
        root_payload(&exported)?
            .required_features
            .iter()
            .any(|feature| feature == MATCH_RECOGNIZE_MATCH_AWARE_DEFINES_FEATURE)
    );

    roundtrip::assert_roundtrip_output(
        &ctx,
        source,
        &[
            ("company", DataType::Utf8, true),
            ("last_price", DataType::Int64, true),
        ],
        &[vec!["acme".into(), 14_i64.into()]],
    )
    .await?;
    Ok(())
}

#[tokio::test]
async fn optimized_candidate_program_retains_source_define_marker() -> Result<()> {
    let ctx = context_with_input().await?;
    let source = ctx
        .sql(
            r#"
WITH input AS (SELECT * FROM t ORDER BY ts LIMIT 1)
SELECT n FROM input
MATCH_RECOGNIZE (
  PARTITION BY company ORDER BY ts, price
  MEASURES COUNT(*) AS n
  ONE ROW PER MATCH
  PATTERN (^ PERMUTE(A,B,D) (B|B E))
  DEFINE
    A AS (SUM(CAST(E.price AS BIGINT) * CAST(0 AS BIGINT)) IS NULL
          OR SUM(CAST(E.price AS BIGINT) * CAST(0 AS BIGINT)) >= 0)
         AND COUNT(A.*) > 1,
    B AS COUNT(B.*) = 0,
    D AS (AVG(CAST(E.price AS DOUBLE) * CAST(0 AS DOUBLE)) IS NULL
          OR AVG(CAST(E.price AS DOUBLE) * CAST(0 AS DOUBLE)) >= 0),
    E AS COUNT(E.*) > 1
)
"#,
        )
        .await?
        .logical_plan()
        .clone();
    let optimized = ctx.state().optimize(&source)?;
    let node = roundtrip::match_recognize_node(&optimized).expect("matcher node");
    assert!(
        node.bound_source()
            .expect("retained source")
            .requirements()
            .requires_match_aware_defines()
    );
    let exported = to_substrait_plan_with_match_recognize(&optimized, &ctx.state())?;
    assert!(
        root_payload(&exported)?
            .required_features
            .iter()
            .any(|feature| feature == MATCH_RECOGNIZE_MATCH_AWARE_DEFINES_FEATURE)
    );
    roundtrip::assert_roundtrip_output(
        &ctx,
        optimized,
        &[("n", DataType::Int64, false)],
        &[],
    )
    .await?;
    Ok(())
}