利用DuckDB luajit插件和openblas库对表中数据做矩阵运算

  1. 安装openblas库

    C:\d>wsl
    root@DESKTOP-59T6U68:/mnt/c/d# docker start gcc
    gcc
    root@DESKTOP-59T6U68:/mnt/c/d# docker exec -it gcc bash
    root@6ae32a5ffcde:/# cd /par
    root@6ae32a5ffcde:/par# whereis libopenblas.so
    libopenblas.so:
    root@6ae32a5ffcde:/par# apt install libopenblas0
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
    libgfortran5 libopenblas0-pthread
    The following NEW packages will be installed:
    libgfortran5 libopenblas0 libopenblas0-pthread
    0 upgraded, 3 newly installed, 0 to remove and 117 not upgraded.
    Need to get 7534 kB of archives.
    After this operation, 47.4 MB of additional disk space will be used.
    N: Ignoring file 'debian.sources_org' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension
    Do you want to continue? [Y/n]
    Get:1 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 libgfortran5 amd64 12.2.0-14+deb12u1 [793 kB]
    Get:2 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 libopenblas0-pthread amd64 0.3.21+ds-4 [6709 kB]
    Get:3 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 libopenblas0 amd64 0.3.21+ds-4 [32.6 kB]
    Fetched 7534 kB in 0s (15.4 MB/s)
    debconf: delaying package configuration, since apt-utils is not installed
    Selecting previously unselected package libgfortran5:amd64.
    (Reading database ... 65497 files and directories currently installed.)
    Preparing to unpack .../libgfortran5_12.2.0-14+deb12u1_amd64.deb ...
    Unpacking libgfortran5:amd64 (12.2.0-14+deb12u1) ...
    Selecting previously unselected package libopenblas0-pthread:amd64.
    Preparing to unpack .../libopenblas0-pthread_0.3.21+ds-4_amd64.deb ...
    Unpacking libopenblas0-pthread:amd64 (0.3.21+ds-4) ...
    Selecting previously unselected package libopenblas0:amd64.
    Preparing to unpack .../libopenblas0_0.3.21+ds-4_amd64.deb ...
    Unpacking libopenblas0:amd64 (0.3.21+ds-4) ...
    Setting up libgfortran5:amd64 (12.2.0-14+deb12u1) ...
    Setting up libopenblas0-pthread:amd64 (0.3.21+ds-4) ...
    update-alternatives: using /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 to provide /usr/lib/x86_64-linux-gnu/libblas.so.3 (libblas.so.3-x86_64-linux-gnu) in auto mode
    update-alternatives: using /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 to provide /usr/lib/x86_64-linux-gnu/liblapack.so.3 (liblapack.so.3-x86_64-linux-gnu) in auto mode
    update-alternatives: using /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblas.so.0 to provide /usr/lib/x86_64-linux-gnu/libopenblas.so.0 (libopenblas.so.0-x86_64-linux-gnu) in auto mode
    Setting up libopenblas0:amd64 (0.3.21+ds-4) ...
    Processing triggers for libc-bin (2.36-9+deb12u13) ...

2.安装duckdb luajit插件linalg库,如果网络不畅安装失败,可以再试

复制代码
root@6ae32a5ffcde:/par# ./duckdb155
DuckDB v1.5.5 (Variegata)
Enter ".help" for usage hints.
memory D load luajit;
memory D SELECT * FROM luajit_module(mode := 'install', sql_name := 'linalg');
┌─────────┬─────────┬─────────┬───────────────────────────────────────────────────────────────────┬─────────┬──────────┐
│   ok    │  mode   │  phase  │                              message                              │ detail  │ sql_name │
│ boolean │ varchar │ varchar │                              varchar                              │ varchar │ varchar  │
├─────────┼─────────┼─────────┼───────────────────────────────────────────────────────────────────┼─────────┼──────────┤
│ false   │ install │         │ install 'linalg' failed: IO Error: Timeout was reached error for  │         │ linalg   │
│         │         │         │ HTTP HEAD to 'https://raw.githubusercontent.com/alitrack/duckdb-l │         │          │
│         │         │         │ uajit-libs/main/INDEX'                                            │         │          │
└─────────┴─────────┴─────────┴───────────────────────────────────────────────────────────────────┴─────────┴──────────┘
memory D SELECT * FROM luajit_module(mode := 'install', sql_name := 'linalg');
┌─────────┬─────────┬─────────┬──────────────────────────────────────┬──────────────────────────────────────┬──────────┐
│   ok    │  mode   │  phase  │               message                │                detail                │ sql_name │
│ boolean │ varchar │ varchar │               varchar                │               varchar                │ varchar  │
├─────────┼─────────┼─────────┼──────────────────────────────────────┼──────────────────────────────────────┼──────────┤
│ true    │ install │ install │ installed 'linalg' (UDF) --- cached at │ luajit_table('linalg') or luajit_s(' │ linalg   │
│         │         │         │  /root/.duckdb/luajit-libs/linalg.lu │ linalg', ...)                        │          │
│         │         │         │ a                                    │                                      │          │
└─────────┴─────────┴─────────┴──────────────────────────────────────┴──────────────────────────────────────┴──────────┘

3.计算常量和表中数据的运算结果

复制代码
memory D SELECT luajit_s('linalg', {'op':'matmul', 'a':[1,2,3,4], 'm':2, 'n':2,'b':[5,6,7,8], 'mb':2, 'nb':2});
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ luajit_s('linalg', main.struct_pack(op := 'matmul', a := main.list_value(1, 2, 3, 4), m := 2, n := 2, b := main.list │
│                                        _value(5, 6, 7, 8), mb := 2, nb := 2))                                        │
│                                                       varchar                                                        │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"c":[19,22,43,50]}                                                                                                  │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
memory D create table d (a float[],b float[]);
memory D insert into d select range(1,101)::float[],range(2,202,2)::float[];
memory D SELECT luajit_s('linalg', {'op':'matmul', 'a':a, 'm':2, 'n':50,'b':b, 'mb':50, 'nb':2})from d where length(a)=100;
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ luajit_s('linalg', main.struct_pack(op := 'matmul', a := a, m := 2, n := 50, b := b, mb := 50, nb := 2)) │
│                                                 varchar                                                  │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"c":[169150,171700,419150,426700]}                                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘

如果把内存数据库改成文件数据库,下次打开数据库时,已安装的luajia库会自动加载。

相关推荐
2601_966377135 小时前
运营商密评密改落地实践:透明加密解决密码应用合规与安全痛点
数据库·安全·oracle
伯恩bourne5 小时前
Qdrant 快速入门 :数据模型介绍
服务器·数据库·人工智能
AgentMaster5 小时前
元数据、血缘、质量、安全四大模块能力拆解,数据治理方案对比:4 种技术路线深度评测
大数据·数据库·数据仓库·人工智能·原型模式
tryxr6 小时前
Redis 的背景知识
数据库·redis·缓存
IvorySQL6 小时前
PostgreSQL 日报 |SUM-AVG 溢出检测不足(9 月 5 日)
数据库·postgresql·区块链
2601_962099086 小时前
python flask sqlalchemy连接数据库流程介绍
数据库·python·flask·教程·sqlalchemy
TDengine (老段)6 小时前
TDgpt 概览 — AI 增强的时序数据库
大数据·数据库·人工智能·物联网·时序数据库·iot·tdengine
2501_933670796 小时前
采购运营校招Excel能力清单:函数、透视表、ERP数据与SQL入门
数据库·sql·excel
YOLO数据集集合6 小时前
红外热成像人员检测数据集 |红外检测 白热模式 人员检测 夜间监控 目标检测 YOLO格式 深度学习数据集 计算机视觉9067期
数据库·深度学习·yolo·目标检测·计算机视觉·红外人员·红外数据
BUG指挥官6 小时前
Redis已接入AI
数据库·人工智能·spring boot·redis·spring cloud