Oracle中集合类型覆盖了Postgresql数组的功能,在Oracle用户中时非常常用的。
尤其是包内定义的集合类型,在SPEC定义后即可直接使用,scope也只在包在生效,使用非常灵活。
开源PG因为有数组没有实现这部分语法,下面对openGauss的包内集合类型实现方法做一些分析。
底层用的还是PG的数组。核心逻辑都在evalSubsciptsNested函数附近。
drop schema if exists pkg_val_1 cascade;
drop schema if exists pkg_val_2 cascade;
create schema pkg_val_1;
create schema pkg_val_2;
set current_schema = pkg_val_2;
set behavior_compat_options='allow_procedure_compile_check';
--test package val assign
create or replace package pck1 is
type r3 is table of float;
vc r3;
type r4 is table of r3;
vd r4;
type r5 is table of r4;
ve r5;
end pck1;
/
create or replace package body pck1 is
end pck1;
/
create or replace package pck2 is
ve int;
procedure p1;
end pck2;
/
create or replace package body pck2 is
procedure p1 as
begin
pck1.vc(1) := 1;
pck1.vc(2) := 2 + pck1.vc(1);
pck1.vd(1) := pck1.vc;
pck1.vd(2) := pck1.vc;
pck1.ve(1) := pck1.vd;
raise info '% % % %', pck1.vc(1), pck1.vd(1)(1), pck1.vd(1)(1), pck1.ve(1)(1)(2);
end;
end pck2;
/
call pck2.p1();
create or replace package pck1 is
type r3 is table of float;
vc r3;
type r4 is table of r3;
vd r4;
type r5 is table of r4;
ve r5;
end pck1;
/
堆栈
CreatePackageCommand
PackageSpecCreate
plpgsql_package_validator
plpgsql_pkg_compile
do_pkg_compile
plpgsql_yyparse
变量列表和PG的差异
区别 | openGauss | PostgreSQL |
---|---|---|
变量数 | u_sess->plsql_cxt.curr_compile_context->plpgsql_nDatums | plpgsql_nDatums |
变量列表 | u_sess->plsql_cxt.curr_compile_context->plpgsql_Datums | plpgsql_Datums |
包变量数 | u_sess->plsql_cxt.curr_compile_context->plpgsql_pkg_nDatums | 无 |
包变量列表 | u_sess->plsql_cxt.curr_compile_context->plpgsql_Datums | 无 |
怎么区分编包函数编函数?
u_sess->plsql_cxt.curr_compile_context->plpgsql_curr_compile
plpgsql_curr_compile非空则是编函数,使用
u_sess->plsql_cxt.curr_compile_context->plpgsql_nDatums
u_sess->plsql_cxt.curr_compile_context->plpgsql_Datums
plpgsql_curr_compile空则是编包函数,使用
u_sess->plsql_cxt.curr_compile_context->plpgsql_pkg_nDatums
u_sess->plsql_cxt.curr_compile_context->plpgsql_Datums
注意变量列表是一套plpgsql_Datums
命名空间区别:
openGauss | PostgreSQL | |
---|---|---|
命名空间区别 | u_sess->plsql_cxt.curr_compile_context->ns_top | ns_top |
命名空间结构体区别 | int itemtype; | PLpgSQL_nsitem_type itemtype; |
int itemno; | int itemno; | |
struct PLpgSQL_nsitem* prev; | struct PLpgSQL_nsitem *prev; | |
char name[FLEXIBLE_ARRAY_MEMBER]; | char name[FLEXIBLE_ARRAY_MEMBER]; | |
char* pkgname; | ||
char* schemaName; |
从编译结果来看:
create or replace package pck2 is
ve int;
procedure p1;
end pck2;
/
create or replace package body pck2 is
procedure p1 as
begin
pck1.vc(1) := 1;
pck1.vc(2) := 2 + pck1.vc(1);
pck1.vd(1) := pck1.vc;
pck1.vd(2) := pck1.vc;
pck1.ve(1) := pck1.vd;
raise info '% % % %', pck1.vc(1), pck1.vd(1)(1), pck1.vd(1)(1), pck1.ve(1)(1)(2);
end;
end pck2;
/
call pck2.p1();
PLpgSQL_stmt_assign
PLpgSQL_stmt_assign = {
cmd_type = 1,
lineno = 3,
varno = 11,
expr = {dtype = 7, dno = 0, query = "SELECT 1"},
sqlString = "pck1.vc(1) := 1;"
}
varno = 11 PLpgSQL_tableelem
PLpgSQL_tableelem = {
dtype = 6,
dno = 11,
ispkg = false,
subscript = {query = "SELECT 1"},
tableparentno = 10,
parenttypoid = 0,
parenttypmod = 0,
tabletypoid = 0,
tabletypmod = 0,
tabletyplen = 0,
elemtypoid = 0,
elemtyplen = 0,
elemtypbyval = false,
elemtypalign = 0 '\000',
assignattrno = -1,
pkg_name = 0x0,
pkg = 0x0
}
运行时exec_assign_expr
PLpgSQL_stmt_assign
PLpgSQL_stmt_assign = {
cmd_type = 1,
lineno = 4,
varno = 12,
expr = {dtype = 7, dno = 0, query = "SELECT 2 + pck1.vc[1]"},
sqlString = "pck1.vc(2) := 2 + pck1.vc(1);"
}
varno = 12 PLpgSQL_tableelem
PLpgSQL_tableelem = {
dtype = 6,
dno = 12,
ispkg = false,
subscript = {query = "SELECT 2"},
tableparentno = 10,
parenttypoid = 0,
parenttypmod = 0,
tabletypoid = 0,
tabletypmod = 0,
tabletyplen = 0,
elemtypoid = 0,
elemtyplen = 0,
elemtypbyval = false,
elemtypalign = 0 '\000',
assignattrno = -1,
pkg_name = 0x0,
pkg = 0x0
}
运行时:和上述类似。
PLpgSQL_stmt_assign
PLpgSQL_stmt_assign = {
cmd_type = 1,
lineno = 5,
varno = 14,
expr = {dtype = 7, dno = 0, query = "SELECT pck1.vc"},
sqlString = "pck1.vd(1) := pck1.vc;"
}
varno = 14 PLpgSQL_tableelem
PLpgSQL_tableelem = {
dtype = 6,
dno = 14,
ispkg = false,
subscript = {query = "SELECT 1"},
tableparentno = 13,
parenttypoid = 0,
parenttypmod = 0,
tabletypoid = 0,
tabletypmod = 0,
tabletyplen = 0,
elemtypoid = 0,
elemtyplen = 0,
elemtypbyval = false,
elemtypalign = 0 '\000',
assignattrno = -1,
pkg_name = 0x0,
pkg = 0x0
}
运行时:
{dtype = 0, dno = 3, refname = 'vd'}
,然后找到nested_table指向的{dtype = 0, dno = 9, ispkg = true, refname = "vd_nest"}