plprql
在PostgreSQL使用PRQL——管线式关系查询语言
仓库
kaspermarstal/plprql
https://github.com/kaspermarstal/plprql
源码
plprql-18.0.1.tar.gz
plprql-18.0.1.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
plprql | 18.0.1 | LANG | Apache-2.0 | Rust |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 3040 | plprql | 否 | 是 | 否 | 是 | 否 | 否 | - |
| 相关扩展 | pg_tle plpgsql plv8 plperl plpython3u pllua hstore_pllua plluau |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 18.0.1 | 1817161514 | plprql | - |
| RPM | PIGSTY | 18.0.1 | 1817161514 | plprql_$v | - |
| DEB | PIGSTY | 18.0.1 | 1817161514 | postgresql-$v-plprql | - |
构建
您可以使用 pig build 命令构建 plprql 扩展的 RPM / DEB 包:
pig build pkg plprql # 构建 RPM / DEB 包
安装
您可以直接安装 plprql 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install plprql; # 当前活跃 PG 版本安装
pig ext install -y plprql -v 18 # PG 18
pig ext install -y plprql -v 17 # PG 17
pig ext install -y plprql -v 16 # PG 16
pig ext install -y plprql -v 15 # PG 15
pig ext install -y plprql -v 14 # PG 14
dnf install -y plprql_18 # PG 18
dnf install -y plprql_17 # PG 17
dnf install -y plprql_16 # PG 16
dnf install -y plprql_15 # PG 15
dnf install -y plprql_14 # PG 14
apt install -y postgresql-18-plprql # PG 18
apt install -y postgresql-17-plprql # PG 17
apt install -y postgresql-16-plprql # PG 16
apt install -y postgresql-15-plprql # PG 15
apt install -y postgresql-14-plprql # PG 14
创建扩展:
CREATE EXTENSION plprql;
用法
plprql 允许使用 PRQL(管道式关系查询语言)编写 PostgreSQL 函数,这是一种编译为 SQL 的现代语言,采用管道语法。
CREATE EXTENSION plprql;
使用 PRQL 创建函数
CREATE FUNCTION match_stats(int)
RETURNS TABLE(player text, kd_ratio float) AS $$
from matches
filter match_id == $1
group player (
aggregate {
total_kills = sum kills,
total_deaths = sum deaths
}
)
filter total_deaths > 0
derive kd_ratio = total_kills / total_deaths
select { player, kd_ratio }
$$ LANGUAGE plprql;
SELECT * FROM match_stats(42);
直接执行 PRQL
SELECT prql('from matches | filter player == "Player1"')
AS (id int, match_id int, player text, kills int)
LIMIT 2;
使用游标处理大结果集
SELECT prql('from matches | filter player == "Player1"', 'cursor_name');
FETCH 2 FROM cursor_name;
查看编译后的 SQL
SELECT prql_to_sql('from matches | filter player == "Player1"');
PRQL 语法概览
PRQL 使用管道式转换:
from employees # 数据源
filter department == "Engineering" # 行过滤
derive monthly_salary = salary / 12 # 计算列
sort {-monthly_salary} # 排序(- 表示降序)
select {name, monthly_salary} # 列投影
take 10 # 限制行数
限制
PRQL 仅支持 SELECT 语句。对于 INSERT、UPDATE 和 DELETE,请使用 SQL 或 PL/pgSQL。