测试DuckDB v2.1.0-alpha49002的bignum乘法

DuckDB 最近合并的提交 https://github.com/duckdb/duckdb/pull/26932 ,给bignum数据类型增加了乘法和整除功能。

  1. 下载二进制文件

    C:\d>wget https://artifacts.duckdb.org/staged/v2.0-cyanoptera/duckdb-cli-windows-amd64.tar.gz -O duckdb-cli-windows-amd64-261020a.tar.gz

    2026-10-20 17:46:48 (54.8 KB/s) - 'duckdb-cli-windows-amd64-261020a.tar.gz' saved [19907268/19907268]

  2. 用3种不同的方法求20000!的10进制位数

    方法1:从小到大顺序,将积和下一个数逐个相乘
    C:\d>duckdb1020a
    DuckDB v2.1.0-alpha49002 (Unknown Version)
    Enter ".help" for usage hints.
    memory D .timer on
    memory D with recursive t as (select 1 a,'1'::bignum b union all select a+1,b::bignum*(a+1)::bignum from t where a<20000)select length(b::varchar)from t where a=20000;
    ┌────────────────────────────┐
    │ length(CAST(b AS VARCHAR)) │
    │ int64 │
    ├────────────────────────────┤
    │ 77338 │
    └────────────────────────────┘
    Run Time (s): real 0.637 user 0.625000 sys 0.000000
    方法2:从小到大顺序,分别将奇数和偶数之积和下一个同类数逐个相乘,目的是减少中间值的位数
    memory D with t1 as(
    with recursive t as (select 1 a,'1'::bignum b union all select a+2,b::bignum*(a+2)::bignum from t where a<19999)from t where a=19999
    ),
    t2 as(
    with recursive t as (select 2 a,'2'::bignum b union all select a+2,b::bignum*(a+2)::bignum from t where a<20000)from t where a=20000
    )
    select length((t1.b*t2.b)::varchar) from t1,t2;
    ┌────────────────────────────────────────┐
    │ length(CAST((t1.b * t2.b) AS VARCHAR)) │
    │ int64 │
    ├────────────────────────────────────────┤
    │ 77338 │
    └────────────────────────────────────────┘
    Run Time (s): real 0.405 user 0.562500 sys 0.031250
    方法3:将相邻的数两两分别相乘,保存积,再对积同样处理,继续迭代直到只剩1个数,目的是尽量使相乘的两数大小类似
    memory D with recursive t(a) as(
    select list(i::bignum) from range(1,20001)t(i)
    union all
    select [a[i]*case when i+1<=length(a) then a[i+1] else 1 end for i in range(1,length(a)+1,2)]
     from t where length(a)>1)select length(a[1]::varchar)from t where length(a)=1;
    ┌───────────────────────────────┐
    │ length(CAST(a[1] AS VARCHAR)) │
    │ int64 │
    ├───────────────────────────────┤
    │ 77338 │
    └───────────────────────────────┘
    Run Time (s): real 0.050 user 0.046875 sys 0.000000

补记:DeepSeek建议的两种写法

复制代码
1.使用duckdb1.3以上的using key语法减少保存无用中间结果
memory D WITH RECURSIVE fact(k, n, val) USING KEY (k) AS (
             SELECT 1, 1, 1::BIGNUM
             UNION ALL
             SELECT k, n + 1, val * (n + 1)::BIGNUM
             FROM fact
             WHERE n < 20000
         )
         SELECT length(val::VARCHAR)
         FROM fact;
┌──────────────────────────────┐
│ length(CAST(val AS VARCHAR)) │
│            int64             │
├──────────────────────────────┤
│                        77338 │
└──────────────────────────────┘
Run Time (s): real 0.734 user 1.390625 sys 0.015625
2.使用lambda语法消除递归CTE
memory D select length(list_reduce(range(1,20001)::bignum[], lambda x,y: x*y)::varchar) a;
┌───────┐
│   a   │
│ int64 │
├───────┤
│ 77338 │
└───────┘
Run Time (s): real 0.542 user 0.531250 sys 0.000000

memory D select length((list_reduce(range(1,20001,2)::bignum[], lambda x,y: x*y)*list_reduce(range(2,20001,2)::bignum[], lambda x,y: x*y))::varchar) a;
┌───────┐
│   a   │
│ int64 │
├───────┤
│ 77338 │
└───────┘
Run Time (s): real 0.309 user 0.281250 sys 0.031250
相关推荐
Nil2081 小时前
LeetCode 283移动零
数据结构·算法·leetcode
CQU_JIAKE1 小时前
10.18【A】
算法
麦壳饼1 小时前
SonnetDB:向量召回率评估中的精确基线与参数记录
数据库·sonnetdb
ClouGence1 小时前
数据如何随时可用?广西水牛先生的 MySQL 实时同步实践
数据库·mysql·数据分析
LMT流氓兔1 小时前
第五单元——第4课:给工具参数加校验
java·前端·算法·ai·langchain
斯晨的AI笔记2 小时前
rsync 部署避坑:脚本执行位被 git 模式位抹掉,巡检备份双静默失败的排查与根治
数据库
这个DBA有点耶2 小时前
innodb_autoinc_lock_mode的三种模式:批量插入时自增锁的持有逻辑与调优
数据库·架构·代码规范
我命由我123452 小时前
5-1.Python 编码进阶 - 函数进阶(参数、返回值、递归、函数作为参数传递、lambda 匿名函数)
java·服务器·数据库·后端·python·java-ee·python3.11
小蒜学长2 小时前
基于Spring Boot+Vue3的在线游戏技术指导师匹配系统的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端·在线游戏技术指导