UDF 编写自定函数

Posted zengkefu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UDF 编写自定函数相关的知识,希望对你有一定的参考价值。

mysql的官方文档中有说明如何给Mysql添加新的函数。

添加的函数可分为3种:

1.自行编写函数的源码,将源码编译成动态库,然后使用Mysql的CREATE FUNCTION语句来将函数添加进Mysql。

2.在源码等级上加入自行编写的函数,这样编译之后,新加入的函数就和Native函数一样,永久使用。

3.通过SQL语句脚本来完成函数的功能。

就这3中方式,第三种明显不足以完成我们所需要的业务工作,因为SQL语句不存在连接缓存的功能。第二种方法看似美好,

但是对于需要更换缓存方法(从redis更换成memcached),重新编译数据库源码,这种不符合工程化商务化。

所以采用第一种方法自行编写函数代码,动态导入函数到mysql之中。
[[email protected] ~]# vi test_add.cpp

#include <mysql.h>

extern "C" long long testadd(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
        int a = *((long long *)args->args[0]);
        int b = *((long long *)args->args[1]);
        return a + b;
}

extern "C" my_bool testadd_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
        return 0;
}
[[email protected] ~]# g++ -shared -fPIC -I /usr/local/mysql56/include -otest_add.so test_add.cpp 
[[email protected]-mysql ~]# cp test_add.so /usr/local/mysql56/lib/plugin
my.cnf
plugin-load=test_add.so


mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> CREATE FUNCTION testadd RETURNS INTEGER SONAME test_add.so; Query OK, 0 rows affected (0.16 sec)
mysql
> select testadd(1,2); +--------------+ | testadd(1,2) | +--------------+ | 3 | +--------------+ 1 row in set (0.00 sec)
mysql> select * from func;
+---------+-----+-------------+----------+
| name    | ret | dl          | type     |
+---------+-----+-------------+----------+
| testadd |   2 | test_add.so | function |
+---------+-----+-------------+----------+
1 row in set (0.07 sec)
func表格记录了所有UDF函数的信息,表格中ret代表的是返回值类型,之所以这里记录的是2,是因为我们返回的是整型数据,可以在mysql的源码中sql目录下的mysql_com.h找到类型的枚举定义。

enum Item_result {STRING_RESULT=0, REAL_RESULT, INT_RESULT, ROW_RESULT, DECIMAL_RESULT}; 

 

udf安全性:

#include <mysql.h>  
#include <malloc.h>  
  
extern "C" long long testadd(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)  
{  
    int a = *((long long *)args->args[0]);  
    int b = *((long long *)args->args[1]);  
  
    int *p = (int *)malloc(sizeof(int));  
    free(p);  
    free(p);  
    return a + b;  
}  
  
extern "C" my_bool testadd_init(UDF_INIT *initid, UDF_ARGS *args, char *message)  
{  
    return 0;  
}  

 

mysql> select testadd(1,2);
ERROR 2013 (HY000): Lost connection to MySQL server during query
[[email protected] log]# *** glibc detected *** /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld: double free or corruption (fasttop): 0x00007ff00c05cf00 ***
======= Backtrace: =========
/lib64/libc.so.6[0x36bb075366]
/usr/local/mysql56/lib/plugin/test_add.so(testadd+0x64)[0x7ff02ab5a6e0]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN17Item_func_udf_int7val_intEv+0x66)[0x5f4606]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN4Item4sendEP8ProtocolP6String+0x1a1)[0x59bf41]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN8Protocol19send_result_set_rowEP4ListI4ItemE+0x92)[0x66edf2]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN11select_send9send_dataER4ListI4ItemE+0x6c)[0x6b7bbc]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN4JOIN4execEv+0x608)[0x6d1488]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld[0x718039]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z12mysql_selectP3THDP10TABLE_LISTjR4ListI4ItemEPS4_P10SQL_I_ListI8st_orderESB_S7_yP13select_resultP18st_select_lex_unitP13st_select_lex+0xbc)[0x718afc]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z13handle_selectP3THDP13select_resultm+0x175)[0x718d05]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld[0x6f34d9]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z21mysql_execute_commandP3THD+0x3575)[0x6f7c95]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z11mysql_parseP3THDPcjP12Parser_state+0x338)[0x6fb7c8]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z16dispatch_command19enum_server_commandP3THDPcj+0xc60)[0x6fcba0]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z10do_commandP3THD+0xd7)[0x6fe997]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z24do_handle_one_connectionP3THD+0x116)[0x6c5906]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(handle_one_connection+0x45)[0x6c59e5]
/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(pfs_spawn_thread+0x126)[0xae0596]
/lib64/libpthread.so.0[0x36bb807851]
/lib64/libc.so.6(clone+0x6d)[0x36bb0e767d]
======= Memory map: ========
00400000-00f9f000 r-xp 00000000 08:03 423182                             /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld
0119e000-0134c000 rw-p 00b9e000 08:03 423182                             /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld
0134c000-0139d000 rw-p 00000000 00:00 0 
01518000-0198e000 rw-p 00000000 00:00 0                                  [heap]
36ba800000-36ba820000 r-xp 00000000 08:03 824743                         /lib64/ld-2.12.so
36baa1f000-36baa20000 r--p 0001f000 08:03 824743                         /lib64/ld-2.12.so
36baa20000-36baa21000 rw-p 00020000 08:03 824743                         /lib64/ld-2.12.so
36baa21000-36baa22000 rw-p 00000000 00:00 0 
36bac00000-36bac01000 r-xp 00000000 08:03 806064                         /lib64/libaio.so.1.0.1
36bac01000-36bae00000 ---p 00001000 08:03 806064                         /lib64/libaio.so.1.0.1
36bae00000-36bae01000 rw-p 00000000 08:03 806064                         /lib64/libaio.so.1.0.1
36bb000000-36bb189000 r-xp 00000000 08:03 824744                         /lib64/libc-2.12.so
36bb189000-36bb388000 ---p 00189000 08:03 824744                         /lib64/libc-2.12.so
36bb388000-36bb38c000 r--p 00188000 08:03 824744                         /lib64/libc-2.12.so
36bb38c000-36bb38d000 rw-p 0018c000 08:03 824744                         /lib64/libc-2.12.so
36bb38d000-36bb392000 rw-p 00000000 00:00 0 
36bb400000-36bb402000 r-xp 00000000 08:03 824750                         /lib64/libdl-2.12.so
36bb402000-36bb602000 ---p 00002000 08:03 824750                         /lib64/libdl-2.12.so
36bb602000-36bb603000 r--p 00002000 08:03 824750                         /lib64/libdl-2.12.so
36bb603000-36bb604000 rw-p 00003000 08:03 824750                         /lib64/libdl-2.12.so
36bb800000-36bb817000 r-xp 00000000 08:03 824745                         /lib64/libpthread-2.12.so
36bb817000-36bba17000 ---p 00017000 08:03 824745                         /lib64/libpthread-2.12.so
36bba17000-36bba18000 r--p 00017000 08:03 824745                         /lib64/libpthread-2.12.so
36bba18000-36bba19000 rw-p 00018000 08:03 824745                         /lib64/libpthread-2.12.so
36bba19000-36bba1d000 rw-p 00000000 00:00 0 
36bc000000-36bc007000 r-xp 00000000 08:03 824746                         /lib64/librt-2.12.so
36bc007000-36bc206000 ---p 00007000 08:03 824746                         /lib64/librt-2.12.so
36bc206000-36bc207000 r--p 00006000 08:03 824746                         /lib64/librt-2.12.so
36bc207000-36bc208000 rw-p 00007000 08:03 824746                         /lib64/librt-2.12.so
36c2000000-36c2016000 r-xp 00000000 08:03 824759                         /lib64/libgcc_s-4.4.6-20120305.so.1
36c2016000-36c2215000 ---p 00016000 08:03 824759                         /lib64/libgcc_s-4.4.6-20120305.so.1
36c2215000-36c2216000 rw-p 00015000 08:03 824759                         /lib64/libgcc_s-4.4.6-20120305.so.1
36c5000000-36c50e8000 r-xp 00000000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.13
36c50e8000-36c52e8000 ---p 000e8000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.13
36c52e8000-36c52ef000 r--p 000e8000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.13
36c52ef000-36c52f1000 rw-p 000ef000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.13
36c52f1000-36c5306000 rw-p 00000000 00:00 0 
36c9400000-36c945d000 r-xp 00000000 08:03 824769                         /lib64/libfreebl3.so
36c945d000-36c965c000 ---p 0005d000 08:03 824769                         /lib64/libfreebl3.so
36c965c000-36c965d000 r--p 0005c000 08:03 824769                         /lib64/libfreebl3.so
36c965d000-36c965e000 rw-p 0005d000 08:03 824769                         /lib64/libfreebl3.so
36c965e000-36c9662000 rw-p 00000000 00:00 0 
36c9c00000-36c9c07000 r-xp 00000000 08:03 824770                         /lib64/libcrypt-2.12.so
36c9c07000-36c9e07000 ---p 00007000 08:03 824770                         /lib64/libcrypt-2.12.so
36c9e07000-36c9e08000 r--p 00007000 08:03 824770                         /lib64/libcrypt-2.12.so
36c9e08000-36c9e09000 rw-p 00008000 08:03 824770                         /lib64/libcrypt-2.12.so
36c9e09000-36c9e37000 rw-p 00000000 00:00 0 
7ff00c000000-7ff00c05f000 rw-p 00000000 00:00 0 
7ff00c05f000-7ff010000000 ---p 00000000 00:00 0 
7ff014000000-7ff014021000 rw-p 00000000 00:00 0 
7ff014021000-7ff018000000 ---p 00000000 00:00 0 
7ff018000000-7ff018021000 rw-p 00000000 00:00 0 
7ff018021000-7ff01c000000 ---p 00000000 00:00 0 
7ff01c000000-7ff01c021000 rw-p 00000000 00:00 0 
7ff01c021000-7ff020000000 ---p 00000000 00:00 0 
7ff020dfb000-7ff020dfc000 ---p 00000000 00:00 0 
7ff020dfc000-7ff0217fc000 rw-p 00000000 00:00 0 
7ff0217fc000-7ff0217fd000 ---p 00000000 00:00 0 
7ff0217fd000-7ff0221fd000 rw-p 00000000 00:00 0 
7ff0221fd000-7ff0221fe000 ---p 00000000 00:00 0 
7ff0221fe000-7ff022bfe000 rw-p 00000000 00:00 0 
7ff022bfe000-7ff022bff000 ---p 00000000 00:00 0 
7ff022bff000-7ff0235ff000 rw-p 00000000 00:00 0 
7ff0235ff000-7ff023600000 ---p 00000000 00:00 0 
7ff023600000-7ff024000000 rw-p 00000000 00:00 0 
7ff024000000-7ff024085000 rw-p 00000000 00:00 0 
7ff024085000-7ff028000000 ---p 00000000 00:00 0 
7ff028109000-7ff02810a000 ---p 00000000 00:00 0 
7ff02810a000-7ff028b0a000 rw-p 00000000 00:00 0 
7ff028b0a000-7ff028b0b000 ---p 00000000 00:00 0 
7ff028b0b000-7ff02950b000 rw-p 00000000 00:00 0 
7ff02950b000-7ff02950c000 ---p 00000000 00:00 0 
7ff02950c000-7ff029f0d000 rw-p 00000000 00:00 0 
7ff029f0d000-7ff029f0e000 ---p 00000000 00:00 0 
7ff029f0e000-7ff02ab13000 rw-p 00000000 00:00 0 
7ff02ab19000-7ff02ab1a000 ---p 00000000 00:00 0 
7ff02ab1a000-7ff02ab5a000 rw-p 00000000 00:00 0 
7ff02ab5a000-7ff02ab5b000 r-xp 00000000 08:03 394342                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/test_add.so
7ff02ab5b000-7ff02ad5a000 ---p 00001000 08:03 394342                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/test_add.so
7ff02ad5a000-7ff02ad5b000 rw-p 00000000 08:03 394342                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/test_add.so
7ff02ad5b000-7ff02ad5c000 ---p 00000000 00:00 0 
7ff02ad5c000-7ff02ad9c000 rw-p 00000000 00:00 0 
7ff02ad9c000-7ff02ad9d000 ---p 00000000 00:00 0 
7ff02ad9d000-7ff02b79d000 rw-p 00000000 00:00 0 
7ff02b79d000-7ff02b79e000 ---p 00000000 00:00 0 
7ff02b79e000-7ff02c19e000 rw-p 00000000 00:00 0 
7ff02c19e000-7ff02c19f000 ---p 00000000 00:00 0 
7ff02c19f000-7ff02cb9f000 rw-p 00000000 00:00 0 
7ff02cb9f000-7ff02cba0000 ---p 00000000 00:00 0 
7ff02cba0000-7ff02d5a0000 rw-p 00000000 00:00 0 
7ff02d5a0000-7ff02d5a1000 ---p 00000000 00:00 0 
7ff02d5a1000-7ff02dfa1000 rw-p 00000000 00:00 0 
7ff02dfa1000-7ff02dfa2000 ---p 00000000 00:00 0 
7ff02dfa2000-7ff02e9a2000 rw-p 00000000 00:00 0 
7ff02e9a2000-7ff02e9a3000 ---p 00000000 00:00 0 
7ff02e9a3000-7ff02f3a3000 rw-p 00000000 00:00 0 
7ff02f3a3000-7ff02f3a4000 ---p 00000000 00:00 0 
7ff02f3a4000-7ff02fda4000 rw-p 00000000 00:00 0 
7ff02fda4000-7ff02fda5000 ---p 00000000 00:00 0 
7ff02fda5000-7ff0307a5000 rw-p 00000000 00:00 0 
7ff0307a5000-7ff0307a6000 ---p 00000000 00:00 0 
7ff0307a6000-7ff033c5b000 rw-p 00000000 00:00 0 
7ff033c5b000-7ff033c60000 r-xp 00000000 08:03 423032                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/validate_password.so
7ff033c60000-7ff033e60000 ---p 00005000 08:03 423032                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/validate_password.so
7ff033e60000-7ff033e61000 rw-p 00005000 08:03 423032                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/validate_password.so
7ff033e61000-7ff03475e000 rw-p 00000000 00:00 0 
7ff03475e000-7ff03476a000 r-xp 00000000 08:03 786463                     /lib64/libnss_files-2.12.so
7ff03476a000-7ff03496a000 ---p 0000c000 08:03 786463                     /lib64/libnss_files-2.12.so
7ff03496a000-7ff03496b000 r--p 0000c000 08:03 786463                     /lib64/libnss_files-2.12.so
7ff03496b

以上是关于UDF 编写自定函数的主要内容,如果未能解决你的问题,请参考以下文章

[源码分析]从"UDF不应有状态" 切入来剖析Flink SQL代码生成 (修订版)

Scala中的Spark分组映射UDF

在 SQL 中编写函数以遍历 UDF 中的日期范围

hive自定义函数(UDF)

函数在代码中工作,但不能作为工作表中的 UDF

编写一个 Pyspark UDF,其功能类似于 Python any 函数