Repository navigation
MATCH_RECOGNIZE keyword for pattern matching #3994
Replies: 10 comments 5 replies
|
+1 for MATCH_RECOGNIZE (aka Row Pattern Matching) There is another open-source implementation in Trino (formerly known as PrestoSQL). It has very good documentation and you can find the code on github. |
|
+1 for MATCH_RECOGNIZE |
|
+1 |
|
Actual "regular" expressions (in the sense of detecting a regular language, via finite state machines) can be detected using the For example, to detect the pattern "price below 1 on day X, price above 1 between X and Y, price below 1 on day Y" you could use a three-state state machine as such SELECT
reduce(1, state -> state_machine(state,
[
[price >= 1, price < 1, false, false], # Move from state 1 to 2 if a <1 price is detected
[false, price < 1, price >= 1, false], # Stay if <1; move on if >=1
[false, false, price >= 1, price < 1], # Stay if >= 1; conclude if < 1
[false, price < 1, price >= 1, false]
], # Final state has same transitions as state 2 because final condition is simultaneously start of new match
))
OVER (PARTITION BY product ORDER BY date)
FROM dfwhere CREATE MACRO state_machine(state, transitions) AS
CASE
WHEN transitions[state][1] THEN 1
WHEN transitions[state][2] THEN 2
WHEN transitions[state][3] THEN 3
WHEN transitions[state][4] THEN 4
... --- can't think of a way right now to implement this non-templated, but that would be easy to offer in C++
ENDThat being said: I completely acknowledge that a regular expression would look better than this (even if it too were limited to FSM), but maybe the |
|
Just CC:ing @taniabogatsch as she actually wrote her bachelor thesis on how to efficiently implement match-recognize on modern hardware. Not sure we're planning to add this is anytime soon though. |
|
Hi, Clickhouse has a nice grammar : https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions#sequencematch |
|
Have not tried them myself yet but these are interesting https://github.com/Query-farm/vgi-matchrecognize/blob/main/README.md https://altertable.ai/blog/2026-08-07-match-recognize-that-scales |
|
We've indeed built one at https://altertable.ai/ and currently testing it with beta-testers, shoot us an email if you want to try it. And yes thanks for sharing @jimdickson007, we indeed blogged about it here: https://altertable.ai/blog/2026-08-07-match-recognize-that-scales |
|
This is now in Here is a fun example: -- old and busted
SELECT '..↑↑↓↓←→←→BA..' ~ '↑{2}↓{2}(←→){2}BA' konami_found;
-- new hotness
FROM (
PIVOT (
FROM UNNEST('..↑↑↓↓←→←→BA..'.split(''))
WITH ORDINALITY t(c,i))
ON c GROUP BY i)
MATCH_RECOGNIZE (
DEFINES AUTO
ORDER BY i
PATTERN (↑{2} ↓{2} (← →){2} B A)
MEASURES true AS konami_found); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The
MATCH_RECOGNIZEclause is an incredibly powerful way to do pattern matching in SQL. For example, here is a description of its implementation in Snowflake or Azure Stream Analytics.The only open source implementation I've come across is Apache Flink (Github here). It'd be quite a challenging undertaking but an awesome feature to have in DuckDB!
All reactions