语法格式
CREATE FUNCTION ([[,…] [,parameter_n]])
RETURNS
type
$$
$$ LANGUAGE plpythonu
参数说明
要创建的函数的名称。在同一数据库内,函数的名称必须唯一。
函数名称只允许a~z、A~Z、0~9、下划线,且不能只包含数字;
([[,...] [,parameter_n]])定义函数的参数,每一个参数的
定义格式是:<参数名称><参数数据类型>。
说明
这里新增了字符串包围符“$$”,和包围符“ ’ ”相比,这种包围符能避免在Python 函
数定义中使用转义;
<参数名称>在同一个函数中必须唯一,只允许a~z、A~Z、0~9、下划线,且不能
只包含数字;
<参数数据类型>指定参数的数据类型;
GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-09-15)
南大通用数据技术股份有限公司
1407
<函数定义>是一系列的Python 语句的组合,
其中包含一些数据操作以完成一定的功
能逻辑;
定义函数时,函数名后面的括号是必需的,即使没有任何参数,也不能省略;
type 是GBase 8a MPP Cluster 支持的数据类型。
示例
示例1
gbase> create function getUrlTitle(url varchar) returns varchar $$
import lxml.html,urllib
return lxml.html.parse(urllib.urlopen(url)).xpath("//title")[0].text
$$ LANGUAGE plpythonu;
gbase> select getUrlTitle('http://192.168.6.253/') as result;
+----------------------+
| result
|
+----------------------+
| Login to Phabricator |
+---------------------------------+
gbase> drop function getUrlTitle;
示例2
tinyint 类型支持,示例如下:
gbase> create function type_tinyint(i tinyint) returns tinyint $$ return i $$ LANGUAGE
plpythonu;
gbase> select type_tinyint(127);
+-------------------+
| type_tinyint(127) |
+-------------------+
|
127 |
+-------------------+
示例3
GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-09-15)
南大通用数据技术股份有限公司
1408
smallint 类型支持,示例如下:
gbase> create function type_smallint(i smallint) returns smallint $$ return i $$ LANGUAG
E plpythonu;
gbase> select type_smallint(32767);
+----------------------+
| type_smallint(32767) |
+----------------------+
|
32767 |
+----------------------+
示例4
int 类型支持,示例如下:
gbase> create function type_int(i int) returns int $$ return i $$ LANGUAGE plpythonu;
gbase> select type_int(2147483647);
+----------------------+
| type_int(2147483647) |
+----------------------+
|
2147483647 |
+----------------------+
示例5
bigint 类型支持,示例如下:
gbase> create function type_bigint(i bigint) returns bigint $$ return i $$ LANGUAGE plp
ythonu;
gbase> select type_bigint(9223372036854775806);
+----------------------------------+
| type_bigint(9223372036854775806) |
+----------------------------------+
|
9223372036854775806 |
GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-09-15)
南大通用数据技术股份有限公司
1409
+----------------------------------+
示例6
float 类型支持,示例如下:
gbase> create function type_float(i float) returns float $$ return i $$ LANGUAGE plpytho
nu;
gbase> select type_float(3.40E+38);
+----------------------+
| type_float(3.40E+38) |
+----------------------+
| 3.39999995214436e+38 |
+----------------------+
示例7
double 类型支持,示例如下:
gbase> create function type_double(i double) returns double $$ return i $$ LANGUAGE p
lpythonu;
gbase> select type_double(1.7976931348623157E+308);
+--------------------------------------+
| type_double(1.7976931348623157E+308) |
+--------------------------------------+
|