先上官方的文档:sql引擎插件开发指导,插件库于05-16日更新,不知道文档更新了没有,目前插件的名字叫dolphin
这里就写一点自己是怎么跑通流程的和一些小Tips,
这里我试的是在插件库dolphin里新建函数makedate(),
因为makedate()要基于MySQL的一些时间类型,所以这里用的是达哥的Plugin仓库里的compatible_time_type
分支;
先把官方的Plugin仓库和openGauss源码源码clone下来,
要先保证openGauss能正常的编译运行。
我们先把插件跑起来先,这个官方的文档里写的比较详细建议去看看,
Makedate
我改了以下几个地方:
在 contrib/dolphin/plugin_utils/adt/date.cpp 中添加
PG_FUNCTION_INFO_V1_PUBLIC(makedate);
extern "C" DLL_PUBLIC Datum makedate(PG_FUNCTION_ARGS);
/*
* @Description: Convert yyyy.ddd into date type
* @return: Convert result, date type.
*/
Datum makedate(PG_FUNCTION_ARGS)
{
int32 yyyy = PG_GETARG_INT32(0);
int32 ddd = PG_GETARG_INT32(1);
struct pg_tm tt;
DateADT date;
j2date(date2j(yyyy, 1, 1) + ddd - 1, &tt.tm_year, &tt.tm_mon, &tt.tm_mday);
if (!IS_VALID_JULIAN(tt.tm_year, tt.tm_mon, tt.tm_mday))
ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("date out of range")));
date = date2j(tt.tm_year, tt.tm_mon, tt.tm_mday) - POSTGRES_EPOCH_JDATE;
PG_RETURN_DATEADT(date);
}
在 contrib/dolphin/sql_script/b_time_type.sql 中添加
CREATE OR REPLACE FUNCTION pg_catalog.makedate (int4, int4) RETURNS date LANGUAGE C STABLE STRICT as '$libdir/b_sql_plugin', 'makedate';
然后在放过去编译安装,重新建库就好了,
testb=# select makedate(2001,40);
makedate
------------
2001-02-09
(1 row)
添加新的函数需要重新导入插件,目前重新导入插件的唯一方法就是删库再建库
修改好像不用重新导入插件(大概)
Tips
自动化测试
关于一类自动化测试的方法,
假设我们已经写好了一些函数测试用例,也写好了插件的代码,现在我们想快速自动化测试所有的用例,
这里我使用了一些sql语句来自动化测试,
总体思想是:
- 将测试用例做成表写成sql文件,在mysql和openGauss侧均导入执行SQL文件;
- 将mysql侧表导入openGauss中;
- 使用表的连接,查看不同之处;
Step 1
将测试用例做成表写成sql文件,在mysql和openGauss侧均导入执行SQL文件;
这里给个小例子:
-- /home/howar/test/func_test.sql
drop table if exists func_test;
create table func_test(functionName varchar(256),result varchar(256));
-- makedate()
insert into func_test(functionName, result) values('makedate(2000, 60) ',makedate(2000, 60) );
insert into func_test(functionName, result) values('makedate(5963, 380) ',makedate(5963, 380) );
insert into func_test(functionName, result) values('makedate(NULL, 10) ',makedate(NULL, 10) );
insert into func_test(functionName, result) values('makedate(2000, NULL) ',makedate(2000, NULL) );
mysql侧执行sql文件:source /home/howar/test/func_test.sql
;
opengauss侧执行sql文件:\i /home/howar/test/func_test.sql
;
这样,我们在mysql侧执行即可得到期望输出,在openGauss侧执行即可得到实际输出;
Step 2
将mysql侧表导入openGauss中;
-- MYSQL侧导出csv表格
SELECT * FROM func_test INTO OUTFILE '/tmp/func_test_mysql.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
-- og侧初始化表
drop table if exists func_test_mysql;
create table func_test_mysql(functionName varchar(256),result varchar(256));
-- og侧导入csv表格
TRUNCATE TABLE func_test_mysql;
copy func_test_mysql (functionName, result) from '/tmp/func_test_mysql.csv' csv header;
Step 3
使用表的连接,查看不同之处;
我这里用了视图的方法,这样看起来就很一目了然了:
CREATE VIEW func_diff AS
SELECT func_test_mysql.functionName AS functionName, func_test_mysql.result AS expect, func_test.result AS og_output
FROM func_test_mysql
left join func_test on func_test_mysql.functionName=func_test.functionName
where (og_output IS NULL and expect!='\N') or expect!=og_output;
效果图:
其实最好的方法是在迁移到mysql侧,在mysql进行对比,因为og要频繁的删库建库,每次都要重新弄很麻烦。但是我并没有成功把og导出来的表导入到mysql里呜呜呜。
VSCode偷懒设置1.0
一些用来偷懒的东西;
我使用的是vscode里的task脚本是实现自动化复制替换编译的:
>>注意把里面的路径全部替换成你自己的路径<<
我为了偷懒把最后一个作为build了,所以f11就可以直接执行最后一个。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "openGauss - start",
"type": "shell",
"command": "gs_ctl -D 你的数据目录 start",
"presentation": {
"reveal": "never"
}
},
{
"label": "openGauss - stop",
"type": "shell",
"command": "gs_ctl -D 你的数据目录 stop"
},
{
"label": "openGaussPlugins - move&build",
"type": "shell",
// 下面这个是偷懒2.0的命令
// "command": "cd $CODE_BASE/contrib/dolphin && make -sj10 && make install -sj",
"command": "rm -R $CODE_BASE/contrib/dolphin && cp -r 你插件仓库Plugin目录/contrib/dolphin $CODE_BASE/contrib/dolphin && cd $CODE_BASE/contrib/dolphin && make -sj12 && make install -sj",
"problemMatcher": [
"$eslint-compact"
]
},
{
"label": "openGaussPlugins - stop, move&build, start",
"type": "shell",
"group": "build",
"dependsOrder": "sequence",
"dependsOn": [
"openGauss - stop",
"openGaussPlugins - move&build",
"openGauss - start"
]
}
]
}
顺便把我的vscode里的gdb调试的设置放上来吧,这样就可以用vsc内置的带UI的gdb来调试openGauss了,
调试的时候要从左边侧边栏的点进去才行,选择进程的时候要选gaussdb,等加载完以后就可以愉快的打断点调试了。
这个是.vscode/launch.json里的
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "opengauss安装地址/bin/gaussdb",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
VSCode偷懒设置2.0
之前我们用脚本一键删除复制编译安装一键挺不错的了,唯一的缺点就是每次编译都是全量编译,太慢了不优美,
在VSCode偷懒设置2.0里,我们考虑使用软连接的方法,连接所有.cpp和.sql文件,这样就不用删除复制了,之前已经编译好了的.o文件也得以保留。
不难发现,我们所有要修改的文件均为Plugin/contrib/dolphin/plugin_utils/adt/*.cpp
和Plugin/contrib/dolphin/sql_script/*.sql
,
那我们就在这两个文件夹里,对于所有的.cpp和.sql全量建立软连接,
先走标准流程,手动复制过去
cp -r 你的Plugin/contrib/dolphin $CODE_BASE/contrib/dolphin
然后删除原来的文件并创立软连接
rm $CODE_BASE/contrib/dolphin/plugin_utils/adt/*.cpp
ln -s 你的Plugin/contrib/dolphin/plugin_utils/adt/*.cpp $CODE_BASE/contrib/dolphin/plugin_utils/adt/
rm $CODE_BASE/contrib/dolphin/include/plugin_utils/*.h
ln -s 你的Plugin/contrib/dolphin/include/plugin_utils/*.h $CODE_BASE/contrib/dolphin/include/plugin_utils/
rm $CODE_BASE/contrib/dolphin/sql_script/*.sql
ln -s 你的Plugin/contrib/dolphin/sql_script/*.sql $CODE_BASE/contrib/dolphin/sql_script/
好了这样你每次就不用rm再cp了,直接cd然后make即可,剩下大量编译时间。