datafusion-match-recognize 0.56.1

SQL MATCH_RECOGNIZE row-pattern matching for Apache DataFusion
Documentation

datafusion-match-recognize

CI Crates.io docs.rs License: Apache-2.0

SQL MATCH_RECOGNIZE row-pattern matching for Apache DataFusion.

Find ordered sequences of rows across partitions (such as falling-then-rising prices, user sessions, or alert escalations) using regex-style patterns, DEFINE predicates, and MEASURES.

Early preview — not ready for production. APIs, configuration options, and Substrait payloads may change on the DataFusion 56 line. Production readiness is not expected before DataFusion 57.

Quick Start

Add datafusion-match-recognize alongside the matching datafusion major version:

[dependencies]
datafusion = "56"
datafusion-match-recognize = "0.56"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

Install the extension with new_match_recognize_context and run a MATCH_RECOGNIZE query:

use datafusion::error::Result;
use datafusion::execution::SessionStateBuilder;
use datafusion_match_recognize::new_match_recognize_context;

#[tokio::main]
async fn main() -> Result<()> {
    let builder = SessionStateBuilder::new().with_default_features();
    let ctx = new_match_recognize_context(builder)?;

    let query = r#"
        WITH ticks(ticker, ts, price) AS (
            VALUES ('ACME', 1, 120),
                   ('ACME', 2, 95),
                   ('ACME', 3, 90),
                   ('ACME', 4, 98),
                   ('ACME', 5, 105)
        )
        SELECT ticker, start_ts, bottom_ts, end_ts
        FROM ticks
        MATCH_RECOGNIZE (
            PARTITION BY ticker
            ORDER BY ts
            MEASURES
                FIRST(DOWN.ts) AS start_ts,
                LAST(DOWN.ts) AS bottom_ts,
                LAST(UP.ts) AS end_ts
            ONE ROW PER MATCH
            AFTER MATCH SKIP PAST LAST ROW
            PATTERN (DOWN+ UP+)
            DEFINE
                DOWN AS price < PREV(price),
                UP AS price > PREV(price)
        )
        ORDER BY ticker, start_ts
    "#;

    ctx.sql(query).await?.show().await?;
    Ok(())
}

DOWN+ UP+ matches the drop (ts = 2..3) followed by the rebound (ts = 4..5):

+--------+----------+-----------+--------+
| ticker | start_ts | bottom_ts | end_ts |
+--------+----------+-----------+--------+
| ACME   | 2        | 3         | 5      |
+--------+----------+-----------+--------+

Highlights

  • Row patterns: greedy and reluctant quantifiers, alternation, ^/$ anchors, ONE ROW or ALL ROWS PER MATCH, AFTER MATCH SKIP, and {- ... -} exclusions.
  • Stateful DEFINE and MEASURES: PREV, NEXT, FIRST, LAST, CLASSIFIER(), MATCH_NUMBER(), MATCH_SEQUENCE_NUMBER(), and running aggregates (COUNT, SUM, AVG, MIN, MAX).
  • Streaming and bounded memory: processes Arrow batches incrementally under DataFusion's MemoryPool with cooperative yielding and cancellation support.
  • Multiple entry points: run SQL directly, build logical plans without SQL via planning, or tune session limits with MatchRecognizeConfig. Hosts with a custom QueryPlanner can compose hooks via advanced.

Cargo Features

Feature Default Effect
sql Yes Enables the SQL adapter and relation planner for MATCH_RECOGNIZE
substrait No Exposes substrait plan import and export helpers

Use default-features = false to build plans programmatically via planning without SQL parser dependencies. Enabling substrait pulls in datafusion/sql transitively, requires protoc on PATH, and uses a crate-specific Substrait extension rather than a standard portable relation (see the Substrait reference).

DataFusion Compatibility

Pre-1.0 releases track DataFusion's major version (0.<datafusion-major>.<patch>):

datafusion-match-recognize Apache DataFusion
0.56.x 56.x

Documentation

License

Licensed under the Apache License, Version 2.0. See LICENSE and NOTICE.