datafusion-match-recognize
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
56line. Production readiness is not expected before DataFusion57.
Quick Start
Add datafusion-match-recognize alongside the matching datafusion major version:
[]
= "56"
= "0.56"
= { = "1", = ["macros", "rt-multi-thread"] }
Install the extension with new_match_recognize_context and
run a MATCH_RECOGNIZE query:
use Result;
use SessionStateBuilder;
use new_match_recognize_context;
async
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 ROWorALL ROWS PER MATCH,AFTER MATCH SKIP, and{- ... -}exclusions. - Stateful
DEFINEandMEASURES: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
MemoryPoolwith cooperative yielding and cancellation support. - Multiple entry points: run SQL directly, build logical plans without SQL
via
planning, or tune session limits withMatchRecognizeConfig. Hosts with a customQueryPlannercan compose hooks viaadvanced.
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
- SQL reference: supported clauses, functions, defaults, and limitations.
- Application integration guide: preserving an existing
SessionState,MatchRecognizeConfig, and custom planners. - Production evaluation: memory limits, spilling, and cancellation.
- Examples and repository: runnable examples,
EXPLAINguides, and benchmarks.
License
Licensed under the Apache License, Version 2.0. See LICENSE and NOTICE.