测试DuckDB 2.1的match_recognize模式匹配语句

  1. 下载DuckDB 2.1 alpha版本

    C:\d>wget https://artifacts.duckdb.org/latest/duckdb-cli-windows-amd64.tar.gz -O duckdb-cli-windows-amd64-260918a.tar.gz

    duckdb-cli-windows-amd64-2609 100%[=================================================>] 18.39M 33.3KB/s in 7m 57s

    2026-09-18 17:29:40 (39.5 KB/s) - 'duckdb-cli-windows-amd64-260918a.tar.gz' saved [19288695/19288695]

  2. 解压执行

    C:\d>duckdb0918a
    DuckDB v2.1.0-alpha42457 (Unknown Version)
    Enter ".help" for usage hints.

  3. https://www.modb.pro/db/192919 中的例子测试,为了使用oracle的语法,先创建一个dual表。
    memory D create table dual as select 1 a;

sql 复制代码
with d (type,dt,status)as
(         select 'X' ,date '2021-7-1','1'  from dual
union all select 'X' ,date '2021-7-2','1'  from dual
union all select 'X' ,date '2021-7-3','0'  from dual
union all select 'X' ,date '2021-7-4','0'  from dual
union all select 'X' ,date '2021-7-5','1'  from dual
union all select 'X' ,date '2021-7-6','0'  from dual
union all select 'X' ,date '2021-7-7','0'  from dual
union all select 'X' ,date '2021-7-8','0'  from dual
union all select 'X' ,date '2021-7-9','1'  from dual
union all select 'X' ,date '2021-7-10','1'  from dual
union all select 'X' ,date '2021-7-11','1'  from dual
)
SELECT *
FROM d
MATCH_RECOGNIZE (
     PARTITION BY type
     ORDER BY dt
      measures
             dt as dt,
             status as status
      one ROW PER MATCH
     PATTERN (  A  )   
     DEFINE
           A as status<>prev(status)  or prev(status) is null
);

输出

复制代码
memory D .read match-rec3.txt
┌─────────┬────────────┬─────────┐
│  type   │     dt     │ status  │
│ varchar │    date    │ varchar │
├─────────┼────────────┼─────────┤
│ X       │ 2021-07-01 │ 1       │
│ X       │ 2021-07-03 │ 0       │
│ X       │ 2021-07-05 │ 1       │
│ X       │ 2021-07-06 │ 0       │
│ X       │ 2021-07-09 │ 1       │
└─────────┴────────────┴─────────┘
sql 复制代码
with d (type,dt,status)as
(         select 'X' ,date '2021-7-1','1'  from dual
union all select 'X' ,date '2021-7-2','1'  from dual
union all select 'X' ,date '2021-7-3','0'  from dual
union all select 'X' ,date '2021-7-4','0'  from dual
union all select 'X' ,date '2021-7-5','1'  from dual
union all select 'X' ,date '2021-7-6','0'  from dual
union all select 'X' ,date '2021-7-7','0'  from dual
union all select 'X' ,date '2021-7-8','0'  from dual
union all select 'X' ,date '2021-7-9','1'  from dual
union all select 'X' ,date '2021-7-10','1'  from dual
union all select 'X' ,date '2021-7-11','1'  from dual
)
SELECT *
FROM d
MATCH_RECOGNIZE (
     PARTITION BY type
     ORDER BY dt
     ALL ROWS PER MATCH
     PATTERN (    (A|{-B-})+   )   
     DEFINE
        A as status<>last(status,1) or prev(status) is null
);

输出

复制代码
memory D .read match-rec4.txt
┌─────────┬────────────┬─────────┐
│  type   │     dt     │ status  │
│ varchar │    date    │ varchar │
├─────────┼────────────┼─────────┤
│ X       │ 2021-07-01 │ 1       │
│ X       │ 2021-07-03 │ 0       │
│ X       │ 2021-07-05 │ 1       │
│ X       │ 2021-07-06 │ 0       │
│ X       │ 2021-07-09 │ 1       │
└─────────┴────────────┴─────────┘
sql 复制代码
with tmp(id ,page) as
(select 1,3 from dual union all select 2,4 from dual union all
 select 4,8 from dual union all select 3,5 from dual union all
 select 5,9 from dual union all select 6,16 from dual union all
 select 7,15 from dual union all select 8,18 from dual
) 
SELECT *
FROM tmp
MATCH_RECOGNIZE 
(
ORDER BY page
MEASURES
  A.page as firstpage,
  LAST(page) as lastpage,
  COUNT(*) as cnt
 ONE ROW PER MATCH
 AFTER MATCH SKIP PAST LAST ROW
 PATTERN (A B*)
 DEFINE B AS page = PREV(page)+1
);

输出,注意别名前的as不可省略。

复制代码
memory D .read match-rec5.txt
Parser Error:
syntax error at or near "cnt"

LINE 29:   COUNT(*) cnt
                    ^^^
memory D .read match-rec5.txt
┌───────────┬──────────┬───────┐
│ firstpage │ lastpage │  cnt  │
│   int32   │  int32   │ int64 │
├───────────┼──────────┼───────┤
│        15 │       16 │     2 │
│        18 │       18 │     1 │
│         8 │        9 │     2 │
│         3 │        5 │     3 │
└───────────┴──────────┴───────┘
sql 复制代码
with gen (id, val) as 
(select 1, 3 from dual union all select 2, 2 from dual union all select 3,5 from dual union all
 select 5, 3 from dual union all select 8, 2 from dual union all select 9,5 from dual union all
 select 10, 3 from dual union all select 12, 2 from dual union all select 13,5 from dual union all
 select 14, 3 from dual union all select 15, 2 from dual union all select 16,5 from dual union all
 select 20, 3 from dual union all select 21, 2 from dual union all select 23,5 from dual 
)
select bid,bid+2 as eid,sum3 from gen
match_recognize(
order by id
measures 
   first (a.id) as bid,
   sum(val) as sum3
one row per match
after match skip to next row
pattern (A B*)
define
      B as b.id<=a.id+2
);

输出

复制代码
memory D .read match-rec6.txt
┌───────┬───────┬────────┐
│  bid  │  eid  │  sum3  │
│ int32 │ int32 │ int128 │
├───────┼───────┼────────┤
│     1 │     3 │     10 │
│    10 │    12 │      5 │
│    15 │    17 │      7 │
│     5 │     7 │      3 │
│    14 │    16 │     10 │
│    12 │    14 │     10 │
│    20 │    22 │      5 │
│    16 │    18 │      5 │
│    23 │    25 │      5 │
│     2 │     4 │      7 │
│     3 │     5 │      8 │
│     9 │    11 │      8 │
│     8 │    10 │     10 │
│    13 │    15 │     10 │
│    21 │    23 │      7 │
└───────┴───────┴────────┘
  15 rows      3 columns
memory D
相关推荐
IpdataCloud2 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
曹牧2 小时前
Java:SQL 注入漏洞
数据库
程序员JerrySUN3 小时前
Jetson Edge AI 实战01:Nano、Xavier、Orin 怎么选?Jetson 硬件选型详解【视频讲解】
java·数据库·redis·安全·mybatis
oradh3 小时前
Oracle Undo问题总结(ORA-600 [4xxx] 系列错误)
数据库·oracle
达梦数据3 小时前
DMDRS辅助表生成规则与作用
数据库·oracle
‎ദ്ദിᵔ.˛.ᵔ₎4 小时前
MySQL 表约束
数据库·mysql
Doris__HE4 小时前
【元脑服务器NF5476G7-NF5476M7技术规格分享】
运维·服务器·网络·数据库·性能优化
笃行3506 小时前
别拿MySQL数据库管理工具硬导:MySQL 迁 KingbaseES 的完整工具链
数据库
风哥2号7 小时前
数据库教程FGMT51‑PostgreSQL实例管理与参数文件
数据库·postgresql