其他
macOS 下编译测试 duckdb_fdw
简介
duckdb_fdw 是三年多前发布的了,期间也做了些简单维护,最近应要求增加了 PG16, catalog
和schema
的支持, 同时 fixed 了 macOS 下报错的问题。
准备工作
安装 Xcode Command Line Tools
安装 PostgreSQL
为了简化工作,此处安装了Postgres.app[1]
编译
DUCKDB_VERSION=0.9.2
PG_VERSION=16
export PATH=/Applications/Postgres.app/Contents/Versions/${PG_VERSION}/bin:$PATH
# 下载duckdb_fdw
git clone https://github.com/alitrack/duckdb_fdw
cd duckdb_fdw
# 下载libduckdb
wget -c https://github.com/duckdb/duckdb/releases/download/v${DUCKDB_VERSION}/libduckdb-osx-universal.zip
!unzip -d . libduckdb-osx-universal.zip
cp libduckdb.* $(pg_config --libdir)
# 编译
make clean USE_PGXS=1
make USE_PGXS=1
# 如果不执行下面代码,可以能报错no LC_RPATH's found
install_name_tool -change @rpath/libduckdb.dylib $(pg_config --libdir) libduckdb.dylib
duckdb_fdw.dylib
sudo make install USE_PGXS=1
不执行上面install_name_tool
命令的时候报错信息,
主要就是$(pg_config --libdir)
不在系统路径中,duckdb_fdw.dylib 找不到libduckdb.dylib
,这个不是 bug。
RuntimeError: (psycopg2.errors.UndefinedFile) could not load library "/Applications/Postgres.app/Contents/Versions/16/lib/postgresql/duckdb_fdw.dylib": dlopen(/Applications/Postgres.app/Contents/Versions/16/lib/postgresql/duckdb_fdw.dylib, 0x000A): Library not loaded: @rpath/libduckdb.dylib
Referenced from: <1FA2BEC2-6323-3D36-9A7F-EB0741838CA7> /Applications/Postgres.app/Contents/Versions/16/lib/postgresql/duckdb_fdw.dylib
Reason: no LC_RPATH's found
[SQL: CREATE EXTENSION duckdb_fdw;]
简单测试
加载插件,并创建 DuckDB_server
DROP EXTENSION IF EXISTS duckdb_fdw CASCADE;
CREATE EXTENSION duckdb_fdw;
CREATE SERVER DuckDB_server FOREIGN DATA WRAPPER duckdb_fdw OPTIONS (database ':memory:');
在 duckdb 上创建个视图,方便查看表信息
SELECT duckdb_execute('duckdb_server','CREATE VIEW tables_duckdb AS SELECT * FROM information_schema.tables');
Create foreign table
最简单的方法就是,IMPORT FOREIGN SCHEMA
, 当然手工CREATE FOREIGN TABLE 更灵活
IMPORT FOREIGN SCHEMA public FROM SERVER DuckDB_server INTO public;
查询
SELECT * FROM tables_duckdb;
再试试 csv 和 parquet
顺便解锁IMPORT FOREIGN SCHEMA
另外两种用法,
SELECT duckdb_execute('duckdb_server','CREATE VIEW iris_csv AS SELECT * FROM ''/data/iris.csv''');
IMPORT FOREIGN SCHEMA public LIMIT TO(iris_csv) FROM SERVER DuckDB_server INTO public;
SELECT duckdb_execute('duckdb_server','CREATE VIEW iris_parquet AS SELECT * FROM ''/data/iris.parquet''');
IMPORT FOREIGN SCHEMA public EXCEPT(iris_csv,tables_duckdb) FROM SERVER DuckDB_server INTO public;
也可以使用CREATE FOREIGN TABLE
来创建,
CREATE FOREIGN TABLE public.iris_csv (
"SepalLength" float8 NULL,
"SepalWidth" float8 NULL,
"PetalLength" float8 NULL,
"PetalWidth" float8 NULL,
"Name" varchar NULL
)
OPTIONS (table 'memory.main.iris_csv');
memory.main.iris_csv
的含义,就是 memory catalog, main schema 下的 iris_csv 表。
SELECT * FROM iris_csv LIMIT 3;
返回结果
SepalLength | SepalWidth | PetalLength | PetalWidth | Name |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
SELECT * FROM iris_parquet LIMIT 3;
返回结果和 csv 的一样。
简单使用先介绍到这里,后续会介绍更多功能。
参考资料
Postgres.app: https://postgresapp.com/downloads.html