datafusion-match-recognize 0.56.1

SQL MATCH_RECOGNIZE row-pattern matching for Apache DataFusion
Documentation
# `datafusion-match-recognize`

[![CI][ci-badge]][ci-workflow]
[![Crates.io][crates-io-badge]][crates-io]
[![docs.rs][docs-rs-badge]][docs-rs]
[![License: Apache-2.0][license-badge]][license]

SQL `MATCH_RECOGNIZE` row-pattern matching for [Apache DataFusion](https://datafusion.apache.org/).

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:

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

Install the extension with [`new_match_recognize_context`][installer-docs] and
run a `MATCH_RECOGNIZE` query:

```rust
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`):

```text
+--------+----------+-----------+--------+
| 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`][planning-docs], or tune session limits with
  [`MatchRecognizeConfig`][config-docs]. Hosts with a custom `QueryPlanner` can
  compose hooks via [`advanced`][advanced-docs].

## Cargo Features

| Feature | Default | Effect |
| --- | --- | --- |
| `sql` | Yes | Enables the SQL adapter and relation planner for `MATCH_RECOGNIZE` |
| `substrait` | No | Exposes [`substrait`][substrait-docs] plan import and export helpers |

Use `default-features = false` to build plans programmatically via
[`planning`][planning-docs] 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][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

- [SQL reference][sql-reference]: supported clauses, functions, defaults, and limitations.
- [Application integration guide][integration-guide]: preserving an existing `SessionState`, `MatchRecognizeConfig`, and custom planners.
- [Production evaluation][production-evaluation]: memory limits, spilling, and cancellation.
- [Examples and repository][repo]: runnable examples, `EXPLAIN` guides, and benchmarks.

## License

Licensed under the Apache License, Version 2.0. See [LICENSE][license] and
[NOTICE][notice].

[ci-badge]: https://github.com/datafusion-contrib/datafusion-match-recognize/actions/workflows/ci.yml/badge.svg
[ci-workflow]: https://github.com/datafusion-contrib/datafusion-match-recognize/actions/workflows/ci.yml
[crates-io-badge]: https://img.shields.io/crates/v/datafusion-match-recognize.svg
[crates-io]: https://crates.io/crates/datafusion-match-recognize
[docs-rs-badge]: https://img.shields.io/docsrs/datafusion-match-recognize
[docs-rs]: https://docs.rs/datafusion-match-recognize
[license-badge]: https://img.shields.io/crates/l/datafusion-match-recognize.svg
[installer-docs]: https://docs.rs/datafusion-match-recognize/latest/datafusion_match_recognize/fn.new_match_recognize_context.html
[config-docs]: https://docs.rs/datafusion-match-recognize/latest/datafusion_match_recognize/config/struct.MatchRecognizeConfig.html
[planning-docs]: https://docs.rs/datafusion-match-recognize/latest/datafusion_match_recognize/planning/index.html
[advanced-docs]: https://docs.rs/datafusion-match-recognize/latest/datafusion_match_recognize/advanced/index.html
[substrait-docs]: https://docs.rs/datafusion-match-recognize/latest/datafusion_match_recognize/substrait/index.html
[repo]: https://github.com/datafusion-contrib/datafusion-match-recognize
[license]: https://github.com/datafusion-contrib/datafusion-match-recognize/blob/main/match-recognize/LICENSE
[notice]: https://github.com/datafusion-contrib/datafusion-match-recognize/blob/main/match-recognize/NOTICE
[integration-guide]: https://github.com/datafusion-contrib/datafusion-match-recognize/blob/main/docs/integration.md
[production-evaluation]: https://github.com/datafusion-contrib/datafusion-match-recognize/blob/main/docs/production_evaluation.md
[sql-reference]: https://github.com/datafusion-contrib/datafusion-match-recognize/blob/main/docs/sql/reference.md
[substrait-reference]: https://github.com/datafusion-contrib/datafusion-match-recognize/blob/main/docs/substrait/README.md