{"title":"MATCH_RECOGNIZE","description":"Find ordered row patterns such as funnels, journeys, and state transitions with SQL:2016-style MATCH_RECOGNIZE.","order":690,"askSuggestions":["How do I match a funnel with other events in between?","How do I require a match to finish within a time limit?","How do I return every matched row instead of a summary?"],"navigation":true,"body":{"code":"var Component=(()=>{var p=Object.create;var d=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,m=Object.prototype.hasOwnProperty;var R=(r,n)=>()=>(n||r((n={exports:{}}).exports,n),n.exports),S=(r,n)=>{for(var c in n)d(r,c,{get:n[c],enumerable:!0})},s=(r,n,c,i)=>{if(n&&typeof n==\"object\"||typeof n==\"function\")for(let t of E(n))!m.call(r,t)&&t!==c&&d(r,t,{get:()=>n[t],enumerable:!(i=A(n,t))||i.enumerable});return r};var I=(r,n,c)=>(c=r!=null?p(T(r)):{},s(n||!r||!r.__esModule?d(c,\"default\",{value:r,enumerable:!0}):c,r)),w=r=>s(d({},\"__esModule\",{value:!0}),r);var a=R((v,l)=>{l.exports=_jsx_runtime});var N={};S(N,{default:()=>u,frontmatter:()=>f});var e=I(a()),f={title:\"MATCH_RECOGNIZE\",description:\"Find ordered row patterns such as funnels, journeys, and state transitions with SQL:2016-style MATCH_RECOGNIZE.\",order:690,askSuggestions:[\"How do I match a funnel with other events in between?\",\"How do I require a match to finish within a time limit?\",\"How do I return every matched row instead of a summary?\"]};function o(r){let n={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",h3:\"h3\",li:\"li\",p:\"p\",pre:\"pre\",strong:\"strong\",ul:\"ul\",...r.components},{DocsPreviewDocument:c,DocsPreviewSection:i}=n;return c||h(\"DocsPreviewDocument\",!0),i||h(\"DocsPreviewSection\",!0),(0,e.jsxs)(c,{anchors:\"match_recognize first-example operator-syntax define-the-row-sequence partition-by order-by define write-patterns return-match-results measures rows-per-match after-match-skip subset bound-a-match-by-time practical-patterns funnel-with-filler-events preferred-path-with-a-fallback pattern-recognition-in-windows learn-more\",children:[(0,e.jsxs)(i,{sectionId:\"match_recognize\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,e.jsx)(n.h1,{id:\"match_recognize\",children:\"MATCH_RECOGNIZE\"}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"MATCH_RECOGNIZE\"}),\" finds sequences of rows that match a regular-expression-like pattern. Use it for funnels, customer journeys, state transitions, sessions, anomaly shapes, and other ordered event-stream problems that are difficult to express with self-joins and window functions.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"Run these statements through Altertable's \",(0,e.jsx)(n.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"}),\".\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"first-example\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,e.jsx)(n.h2,{id:\"first-example\",children:\"First example\"}),`\n`,(0,e.jsx)(n.p,{children:\"Suppose an event table contains one row for each user action:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`WITH events(user_id, ts, event) AS (\n    VALUES\n        (1, TIMESTAMP '2024-01-01 10:00:00', 'view'),\n        (1, TIMESTAMP '2024-01-01 10:01:00', 'click'),\n        (1, TIMESTAMP '2024-01-01 10:02:00', 'view'),\n        (2, TIMESTAMP '2024-01-01 10:00:00', 'view')\n)\nSELECT *\nFROM events\nMATCH_RECOGNIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    MEASURES\n        A.ts AS view_ts,\n        B.ts AS click_ts\n    PATTERN (A B)\n    DEFINE\n        A AS event = 'view',\n        B AS event = 'click'\n)\nORDER BY user_id, view_ts;\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"This query finds a \",(0,e.jsx)(n.code,{children:\"view\"}),\" immediately followed by a \",(0,e.jsx)(n.code,{children:\"click\"}),\" for each user:\"]}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"PARTITION BY user_id\"}),\" creates an independent sequence for each user.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"ORDER BY ts\"}),\" defines the order in which rows are consumed.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A\"}),\" and \",(0,e.jsx)(n.code,{children:\"B\"}),\" are pattern variables that describe row roles.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"DEFINE\"}),\" specifies which rows qualify for each role.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"PATTERN (A B)\"}),\" requires two consecutive rows: an \",(0,e.jsx)(n.code,{children:\"A\"}),\" row followed by a \",(0,e.jsx)(n.code,{children:\"B\"}),\" row.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"MEASURES\"}),\" defines the columns in each result row.\"]}),`\n`]}),`\n`,(0,e.jsx)(n.p,{children:\"Matches are non-overlapping. After a match is emitted, matching resumes after the last consumed row. Rows that cannot start a complete match are skipped.\"}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"operator-syntax\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,e.jsx)(n.h2,{id:\"operator-syntax\",children:\"Operator syntax\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"The supported \",(0,e.jsx)(n.code,{children:\"FROM\"}),\"-clause shape is:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`FROM input_relation [input_alias]\nMATCH_RECOGNIZE (\n    [PARTITION BY partition_column [, ...]]\n    [ORDER BY input_column [ASC | DESC] [NULLS FIRST | NULLS LAST] [, ...]]\n    [MEASURES measure_expression AS output_name [, ...]]\n    [ONE ROW PER MATCH | ALL ROWS PER MATCH\n        [SHOW EMPTY MATCHES | OMIT EMPTY MATCHES | WITH UNMATCHED ROWS]]\n    [AFTER MATCH SKIP\n        PAST LAST ROW | TO NEXT ROW | TO FIRST variable | TO LAST variable]\n    PATTERN (row_pattern) [WITHIN interval_expression]\n    [SUBSET subset_name = (variable [, ...]) [, ...]]\n    [DEFINE pattern_variable AS boolean_expression [, ...]]\n)\n[match_alias]\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"PATTERN\"}),\" is required. \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\", \",(0,e.jsx)(n.code,{children:\"MEASURES\"}),\", \",(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\", \",(0,e.jsx)(n.code,{children:\"SUBSET\"}),\", and \",(0,e.jsx)(n.code,{children:\"DEFINE\"}),\" are optional, but the query must produce at least one output column.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"The source relation must appear immediately before \",(0,e.jsx)(n.code,{children:\"MATCH_RECOGNIZE\"}),\". The result can be aliased after the clause:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT m.user_id, m.start_ts\nFROM events AS e\nMATCH_RECOGNIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    MEASURES A.ts AS start_ts\n    PATTERN (A)\n    DEFINE A AS event = 'view'\n) AS m;\n`})}),`\n`,(0,e.jsx)(n.p,{children:\"Common SQL constructs such as CTEs, subqueries, joins, outer filters, grouping, and aliases can surround the operator.\"}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"define-the-row-sequence\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,e.jsx)(n.h2,{id:\"define-the-row-sequence\",children:\"Define the row sequence\"}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"partition-by\",anchors:\"\",ancestors:\"define-the-row-sequence\",index:\"4\",children:[(0,e.jsx)(n.h3,{id:\"partition-by\",children:(0,e.jsx)(n.code,{children:\"PARTITION BY\"})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\" splits the input into independent streams. A pattern never crosses a partition boundary:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`PARTITION BY user_id, channel\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"The partition columns appear in the output before the measure columns. Without \",(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\", the entire input is treated as one partition.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"order-by\",anchors:\"\",ancestors:\"define-the-row-sequence\",index:\"5\",children:[(0,e.jsx)(n.h3,{id:\"order-by\",children:(0,e.jsx)(n.code,{children:\"ORDER BY\"})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" defines the sequence that the pattern consumes:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`ORDER BY ts, sequence_number\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"It supports input columns and \",(0,e.jsx)(n.code,{children:\"ASC\"}),\", \",(0,e.jsx)(n.code,{children:\"DESC\"}),\", \",(0,e.jsx)(n.code,{children:\"NULLS FIRST\"}),\", and \",(0,e.jsx)(n.code,{children:\"NULLS LAST\"}),\". If the primary ordering column can tie, add a stable secondary column so matching is deterministic.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"When \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" is omitted, rows keep their input order within each partition. Add an outer \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" when result order matters, especially when the query uses multiple partitions or multiple threads.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"define\",anchors:\"\",ancestors:\"define-the-row-sequence\",index:\"6\",children:[(0,e.jsx)(n.h3,{id:\"define\",children:(0,e.jsx)(n.code,{children:\"DEFINE\"})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"DEFINE\"}),\" maps pattern variables to Boolean predicates:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`DEFINE\n    A AS event = 'view',\n    B AS event = 'click',\n    C AS event NOT IN ('view', 'click')\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"The predicate is evaluated against the current input row. A \",(0,e.jsx)(n.code,{children:\"NULL\"}),\" predicate does not match; only \",(0,e.jsx)(n.code,{children:\"TRUE\"}),\" qualifies. An undefined pattern variable matches every row, but explicitly defining every variable makes the query intent clearer.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"write-patterns\",anchors:\"\",ancestors:\"\",index:\"7\",children:[(0,e.jsx)(n.h2,{id:\"write-patterns\",children:\"Write patterns\"}),`\n`,(0,e.jsx)(n.p,{children:\"Pattern variables represent row roles. The pattern language supports:\"}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A\"}),\" \\u2014 one row matching variable \",(0,e.jsx)(n.code,{children:\"A\"})]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A B\"}),\" \\u2014 concatenation\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A | B\"}),\" \\u2014 alternation\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"(A B)\"}),\" \\u2014 grouping\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A*\"}),\", \",(0,e.jsx)(n.code,{children:\"A+\"}),\", \",(0,e.jsx)(n.code,{children:\"A?\"}),\" \\u2014 greedy repetition\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A{n}\"}),\", \",(0,e.jsx)(n.code,{children:\"A{n,}\"}),\", \",(0,e.jsx)(n.code,{children:\"A{n,m}\"}),\" \\u2014 bounded repetition\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"A*?\"}),\", \",(0,e.jsx)(n.code,{children:\"A+?\"}),\", \",(0,e.jsx)(n.code,{children:\"A??\"}),\", and similar forms \\u2014 reluctant repetition\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"PERMUTE(A, B, \\u2026)\"}),\" \\u2014 any ordering of the listed variables\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"^\"}),\" and \",(0,e.jsx)(n.code,{children:\"$\"}),\" \\u2014 partition-start and partition-end anchors\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"{- A -}\"}),\" \\u2014 exclusion from \",(0,e.jsx)(n.code,{children:\"ALL ROWS PER MATCH\"}),\" output\"]}),`\n`]}),`\n`,(0,e.jsx)(n.p,{children:\"Patterns can be composed:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`PATTERN ((A B) | (A C* B) | A)\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Alternatives are evaluated from left to right. In the example above, a complete \",(0,e.jsx)(n.code,{children:\"A B\"}),\" match is preferred, then \",(0,e.jsx)(n.code,{children:\"A C* B\"}),\", then the single-row \",(0,e.jsx)(n.code,{children:\"A\"}),\" fallback. Repetition is greedy by default.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"return-match-results\",anchors:\"\",ancestors:\"\",index:\"8\",children:[(0,e.jsx)(n.h2,{id:\"return-match-results\",children:\"Return match results\"}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"measures\",anchors:\"\",ancestors:\"return-match-results\",index:\"9\",children:[(0,e.jsx)(n.h3,{id:\"measures\",children:(0,e.jsx)(n.code,{children:\"MEASURES\"})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"MEASURES\"}),\" defines the output columns. Every expression needs an alias:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`MEASURES\n    A.ts AS start_ts,\n    B.ts AS end_ts,\n    B.ts - A.ts AS elapsed\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"In a measure, \",(0,e.jsx)(n.code,{children:\"A.column\"}),\" returns the value from the last row assigned to \",(0,e.jsx)(n.code,{children:\"A\"}),\". An unqualified column such as \",(0,e.jsx)(n.code,{children:\"ts\"}),\" returns the value from the last row of the whole match.\"]}),`\n`,(0,e.jsx)(n.p,{children:\"Pattern expressions also support navigation and pattern functions, including:\"}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"PREV\"}),\", \",(0,e.jsx)(n.code,{children:\"NEXT\"}),\", \",(0,e.jsx)(n.code,{children:\"FIRST\"}),\", and \",(0,e.jsx)(n.code,{children:\"LAST\"})]}),`\n`,(0,e.jsx)(n.li,{children:(0,e.jsx)(n.code,{children:\"CLASSIFIER()\"})}),`\n`,(0,e.jsx)(n.li,{children:(0,e.jsx)(n.code,{children:\"MATCH_NUMBER()\"})}),`\n`,(0,e.jsxs)(n.li,{children:[\"Pattern aggregates such as \",(0,e.jsx)(n.code,{children:\"COUNT\"}),\", \",(0,e.jsx)(n.code,{children:\"SUM\"}),\", \",(0,e.jsx)(n.code,{children:\"AVG\"}),\", \",(0,e.jsx)(n.code,{children:\"MIN\"}),\", and \",(0,e.jsx)(n.code,{children:\"MAX\"})]}),`\n`,(0,e.jsxs)(n.li,{children:[\"Top-level \",(0,e.jsx)(n.code,{children:\"RUNNING\"}),\" and \",(0,e.jsx)(n.code,{children:\"FINAL\"}),\" prefixes, such as \",(0,e.jsx)(n.code,{children:\"RUNNING LAST(value)\"}),\" or \",(0,e.jsx)(n.code,{children:\"FINAL COUNT(*)\"})]}),`\n`]}),`\n`,(0,e.jsxs)(n.p,{children:[\"If a variable did not participate in the selected alternative, referencing it produces \",(0,e.jsx)(n.code,{children:\"NULL\"}),\".\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"rows-per-match\",anchors:\"\",ancestors:\"return-match-results\",index:\"10\",children:[(0,e.jsx)(n.h3,{id:\"rows-per-match\",children:\"Rows per match\"}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"ONE ROW PER MATCH\"}),\" is the default and emits one summary row for each successful match:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`ONE ROW PER MATCH\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"ALL ROWS PER MATCH\"}),\" emits one row for each matched input row. It supports modifiers for empty and unmatched rows:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`ALL ROWS PER MATCH SHOW EMPTY MATCHES\nALL ROWS PER MATCH OMIT EMPTY MATCHES\nALL ROWS PER MATCH WITH UNMATCHED ROWS\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"With \",(0,e.jsx)(n.code,{children:\"ONE ROW PER MATCH\"}),\", the output contains partition columns followed by measure columns. With \",(0,e.jsx)(n.code,{children:\"ALL ROWS PER MATCH\"}),\", the output also includes order columns and remaining input columns.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"after-match-skip\",anchors:\"\",ancestors:\"return-match-results\",index:\"11\",children:[(0,e.jsx)(n.h3,{id:\"after-match-skip\",children:(0,e.jsx)(n.code,{children:\"AFTER MATCH SKIP\"})}),`\n`,(0,e.jsx)(n.p,{children:\"Choose where matching resumes after a match:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`AFTER MATCH SKIP PAST LAST ROW\nAFTER MATCH SKIP TO NEXT ROW\nAFTER MATCH SKIP TO FIRST variable\nAFTER MATCH SKIP TO LAST variable\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"PAST LAST ROW\"}),\" is the default. A skip target that does not advance the search, or that refers to a missing variable, raises an error.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"subset\",anchors:\"\",ancestors:\"return-match-results\",index:\"12\",children:[(0,e.jsx)(n.h3,{id:\"subset\",children:(0,e.jsx)(n.code,{children:\"SUBSET\"})}),`\n`,(0,e.jsx)(n.p,{children:\"Create a union variable that refers to multiple pattern variables:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SUBSET milestone = (A, B)\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Union variables can be used in \",(0,e.jsx)(n.code,{children:\"MEASURES\"}),\", \",(0,e.jsx)(n.code,{children:\"DEFINE\"}),\", and \",(0,e.jsx)(n.code,{children:\"AFTER MATCH SKIP TO FIRST\"}),\" or \",(0,e.jsx)(n.code,{children:\"TO LAST\"}),\".\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"bound-a-match-by-time\",anchors:\"\",ancestors:\"\",index:\"13\",children:[(0,e.jsx)(n.h2,{id:\"bound-a-match-by-time\",children:\"Bound a match by time\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Add \",(0,e.jsx)(n.code,{children:\"WITHIN\"}),\" after \",(0,e.jsx)(n.code,{children:\"PATTERN\"}),\" to require that a match completes within an interval:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`PATTERN (A B C) WITHIN INTERVAL 30 SECOND\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"The interval is inclusive: the last timestamp can equal the first timestamp plus the interval. \",(0,e.jsx)(n.code,{children:\"WITHIN\"}),\" requires an ascending temporal first \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" key, such as \",(0,e.jsx)(n.code,{children:\"DATE\"}),\", \",(0,e.jsx)(n.code,{children:\"TIMESTAMP\"}),\", or \",(0,e.jsx)(n.code,{children:\"TIMESTAMPTZ\"}),\". The interval must be a non-null, strictly positive, foldable DuckDB \",(0,e.jsx)(n.code,{children:\"INTERVAL\"}),\".\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"practical-patterns\",anchors:\"\",ancestors:\"\",index:\"14\",children:[(0,e.jsx)(n.h2,{id:\"practical-patterns\",children:\"Practical patterns\"}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"funnel-with-filler-events\",anchors:\"\",ancestors:\"practical-patterns\",index:\"15\",children:[(0,e.jsx)(n.h3,{id:\"funnel-with-filler-events\",children:\"Funnel with filler events\"}),`\n`,(0,e.jsx)(n.p,{children:\"Use a repeated variable when unrelated events may occur between two milestones:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nMATCH_RECOGNIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    MEASURES\n        A.ts AS view_ts,\n        B.ts AS click_ts\n    PATTERN (A C* B)\n    DEFINE\n        A AS event = 'view',\n        B AS event = 'click',\n        C AS event NOT IN ('view', 'click')\n)\nORDER BY user_id, view_ts;\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"C*\"}),\" consumes zero or more filler rows. It cannot consume rows that do not match \",(0,e.jsx)(n.code,{children:\"C\"}),\", and the trailing \",(0,e.jsx)(n.code,{children:\"B\"}),\" still needs to complete the pattern.\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"preferred-path-with-a-fallback\",anchors:\"\",ancestors:\"practical-patterns\",index:\"16\",children:[(0,e.jsx)(n.h3,{id:\"preferred-path-with-a-fallback\",children:\"Preferred path with a fallback\"}),`\n`,(0,e.jsx)(n.p,{children:\"Use an alternation when a longer match should win, but a shorter match should still produce a result:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nMATCH_RECOGNIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    MEASURES\n        A.ts AS start_ts,\n        B.ts AS click_ts\n    PATTERN ((A B) | A)\n    DEFINE\n        A AS event = 'view',\n        B AS event = 'click'\n)\nORDER BY user_id, start_ts;\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"When a \",(0,e.jsx)(n.code,{children:\"view\"}),\" is followed by a \",(0,e.jsx)(n.code,{children:\"click\"}),\", \",(0,e.jsx)(n.code,{children:\"(A B)\"}),\" wins and consumes both rows. When no click follows, the query falls back to \",(0,e.jsx)(n.code,{children:\"A\"}),\", producing a row with \",(0,e.jsx)(n.code,{children:\"click_ts = NULL\"}),\".\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"pattern-recognition-in-windows\",anchors:\"\",ancestors:\"\",index:\"17\",children:[(0,e.jsx)(n.h2,{id:\"pattern-recognition-in-windows\",children:\"Pattern recognition in windows\"}),`\n`,(0,e.jsx)(n.p,{children:\"Pattern recognition is also supported in named or inline window structures. A pattern window produces one result row per input row:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT\n    user_id,\n    value OVER w,\n    label OVER w\nFROM events\nWINDOW w AS (\n    PARTITION BY user_id\n    ORDER BY ts\n    MEASURES\n        RUNNING LAST(value) AS value,\n        CLASSIFIER() AS label\n    ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING\n    AFTER MATCH SKIP PAST LAST ROW\n    SEEK\n    PATTERN (A B+)\n    DEFINE B AS value < PREV(B.value)\n);\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Window pattern frames must start at \",(0,e.jsx)(n.code,{children:\"CURRENT ROW\"}),\". The frame can end at \",(0,e.jsx)(n.code,{children:\"CURRENT ROW\"}),\", a constant \",(0,e.jsx)(n.code,{children:\"n FOLLOWING\"}),\", or \",(0,e.jsx)(n.code,{children:\"UNBOUNDED FOLLOWING\"}),\". \",(0,e.jsx)(n.code,{children:\"INITIAL\"}),\" and \",(0,e.jsx)(n.code,{children:\"SEEK\"}),\", \",(0,e.jsx)(n.code,{children:\"AFTER MATCH SKIP\"}),\", and \",(0,e.jsx)(n.code,{children:\"SUBSET\"}),\" are supported in pattern windows.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"Anchors and \",(0,e.jsx)(n.code,{children:\"MATCH_NUMBER()\"}),\" are not supported in windows. Ordinary window functions over the matched frame currently cover \",(0,e.jsx)(n.code,{children:\"SUM\"}),\", \",(0,e.jsx)(n.code,{children:\"COUNT\"}),\", \",(0,e.jsx)(n.code,{children:\"AVG\"}),\", \",(0,e.jsx)(n.code,{children:\"MIN\"}),\", and \",(0,e.jsx)(n.code,{children:\"MAX\"}),\".\"]}),`\n`]}),(0,e.jsxs)(i,{sectionId:\"learn-more\",anchors:\"\",ancestors:\"\",index:\"18\",children:[(0,e.jsx)(n.h2,{id:\"learn-more\",children:\"Learn more\"}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/features/journeys\",children:\"JOURNEYS\"})}),\": discover observed paths without writing a pattern.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/features/sessionize\",children:\"SESSIONIZE\"})}),\": assign events to gap-based sessions.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/query/sql-explorer\",children:\"SQL Explorer\"})}),\": run and refine pattern queries interactively.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"})}),\": learn about Altertable's DuckDB SQL dialect.\"]}),`\n`]})]})]})}function u(r={}){let{wrapper:n}=r.components||{};return n?(0,e.jsx)(n,{...r,children:(0,e.jsx)(o,{...r})}):o(r)}function h(r,n){throw new Error(\"Expected \"+(n?\"component\":\"object\")+\" `\"+r+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return w(N);})();\n;return Component;"},"_id":"docs/lakehouse/features/match-recognize.mdx","_raw":{"sourceFilePath":"docs/lakehouse/features/match-recognize.mdx","sourceFileName":"match-recognize.mdx","sourceFileDir":"docs/lakehouse/features","contentType":"mdx","flattenedPath":"docs/lakehouse/features/match-recognize"},"type":"Docs","path":"/lakehouse/features/match-recognize","level":3,"parent":"lakehouse/features"}