
# 集合支持的函数
#### 集合操作符
- = 参数：nesttable类型
  返回值：true或false，boolean类型。
  功能描述：判断两个集合是否相等。
  示例：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   a nest := nest(1,2);
   b nest := nest(1,2);
   flag bool;
   BEGIN
      flag := a = b;
      raise info '%', flag;
  END;
   /
  INFO:  t
  ANONYMOUS BLOCK EXECUTE
  ```
  
- \<\> 参数：nesttable类型
  返回值：true或false，boolean类型。
  功能描述：判断两个集合是否不相等。
  示例：
  ```
  gaussdb=# DECLARE
    type nest is table of int;
    a nest := nest(1,2);
    b nest := nest(1,2);
    flag bool; 
   BEGIN
     flag := a <> b;
     raise info '%', flag;
  END;
  /
  INFO:  f
  ANONYMOUS BLOCK EXECUTE
  ```
  
 
#### 集合MULTISET函数
- MULTISET UNION \[ALL \| DISTINCT\] 参数：nesttable类型。
  返回值：nesttable类型。
  功能描述：两个集合变量的并集，ALL表示不去除重复元素，DISTINCT表示去除重复元素，ALL为默认值。
  示例1，求两个集合变量的并集，且不去除重复元素，即MULTISET UNION ALL：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2);
     b nest := nest(2,3);
   BEGIN
     a := a MULTISET UNION ALL b;
     raise info '%', a;
   END;
   /
  INFO:  {1,2,2,3}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，求两个集合变量的并集，且去除重复元素，即MULTISET UNION DISTINCT：
  ```
  gaussdb=# DECLARE
      type nest is table of int;
      a nest := nest(1,2);
      b nest := nest(2,3);
   BEGIN
      a := a MULTISET UNION DISTINCT b;
      raise info '%', a;
   END;
   /
  INFO:  {1,2,3}
  ANONYMOUS BLOCK EXECUTE
  ```
  
- MULTISET EXCEPT \[ALL \| DISTINCT\] 参数：nesttable类型。
  返回值：nesttable类型。
  功能描述：两个集合变量的差集。如A MULTISET EXCEPT B：ALL表示去除A中A具有的元素且B也具有的元素并返回，其中去除动作计算元素个数。例如，对于特定元素，如果A中出现了m次，B中出现了n次，当 m \> n时，则去除A中m - n个该元素，当m \<= n时，则去除A中m个该元素；DISTINCT表示去除A中A具有的元素且B也具有的元素并返回，其中去除动作不计算元素个数。对于特定元素，如果在A中出现且又在B中出现了，则去除A中所有该元素。ALL为默认值。
  示例1，求两个集合变量的差集，去除A中A具有的元素且B也具有的元素并返回，其中去除动作计算元素个数。即MULTISET EXCEPT ALL：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,2);
     b nest := nest(2,3);
   BEGIN
     a := a MULTISET EXCEPT ALL b;
     raise info '%', a;
   END;
   /
  INFO:  {1,2}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，求两个集合变量的差集，去除A中A具有的元素且B也具有的元素并返回，其中去除动作不计算元素个数。即MULTISET EXCEPT DISTINCT：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,2);
     b nest := nest(2,3);
   BEGIN
     a := a MULTISET EXCEPT DISTINCT b;
     raise info '%', a;
   END;
   /
  INFO:  {1}
  ANONYMOUS BLOCK EXECUTE
  ```
  
- MULTISET INTERSECT \[ALL \| DISTINCT\] 参数：nesttable类型。
  返回值：nesttable类型。
  功能描述：两个集合变量的交集。如 A MULTISET INTERSECT B：ALL表示取A与B所有重复的元素；DISTINCT表示取A与B中重复元素，且去除其中重复元素。ALL为默认值。
  示例1，求两个集合变量的交集，不去除重复元素，即MULTISET INTERSECT ALL：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,2);
     b nest := nest(2,2,3);
   BEGIN
     a := a MULTISET INTERSECT ALL b;
     raise info '%', a;
   END;
   /
  INFO:  {2,2}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，求两个集合变量的交集，去除重复元素，即MULTISET INTERSECT DISTINCT：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,2);
     b nest := nest(2,2,3);
   BEGIN
     a := a MULTISET INTERSECT DISTINCT b;
     raise info '%', a;
   END;
   /
  INFO:  {2}
  ANONYMOUS BLOCK EXECUTE
  ```
  
 
#### 集合类型函数
![](https://support.huaweicloud.com/centralized-devg-v3-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
- 以下函数定义说明里\[\]中的内容代表可选项，如：count\[()\]表示可以写成count或count()。
- 在内层表达式中，不支持通过嵌套的方式调用集合类型函数。
 
- exists(idx) 参数：idx为int4类型或varchar类型。
  返回值：true或false，boolean类型。
  功能描述：查找指定位置是否存在有效元素。
  示例：
  ```
  gaussdb=# DECLARE
     type nest is table of varchar2;
     a nest := nest('happy','?');
     flag bool;
   BEGIN
     flag := a.exists(1);
     raise info '%', flag;
     flag := a.exists(10);
     raise info '%', flag;
   END;
   /
  INFO:  t
  INFO:  f
  ANONYMOUS BLOCK EXECUTE
   
  gaussdb=# DECLARE
     type nest is table of varchar2 index by varchar2;
     a nest;
     flag bool;
   BEGIN
     a('1') := 'Be';
     a('2') := 'happy';
     a('3') := '.';
     flag := a.exists('1');
     raise info '%', flag;
     flag := a.exists('ddd');
     raise info '%', flag;
   END;
   /
  INFO:  t
  INFO:  f
  ANONYMOUS BLOCK EXECUTE
  ```
  
- extend\[(count\[, idx\])\] 参数：idx和count为int4类型。
  返回值：无返回值。
  功能描述：extend函数仅支持nesttable类型变量使用。在nesttable变量末尾拓展1个或count个元素。存在idx下标元素时，复制count个idx下标元素到变量末尾。
  示例1，nesttable类型变量扩展1个元素且所扩展的元素值为NULL：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1);
   BEGIN
     raise info '%', a;
     a.extend;
     raise info '%', a;
     a.extend;
     raise info '%', a;
   END;
   /
  INFO:  {1}
  INFO:  {1,NULL}
  INFO:  {1,NULL,NULL}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，nesttable类型变量扩展n个元素且所扩展的元素值为NULL：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1);
   BEGIN
     raise info '%', a;
     a.extend(2);
     raise info '%', a;
   END;
   /
  INFO:  {1}
  INFO:  {1,NULL,NULL}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例3，nesttable类型变量扩展n个元素且所扩展的元素值为指定下标元素的值：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(9);
   BEGIN
     raise info '%', a;
     a.extend(2,1);
     raise info '%', a;
   END;
   /
  INFO:  {9}
  INFO:  {9,9,9}
  ANONYMOUS BLOCK EXECUTE
  ```
  
- delete\[(idx1\[, idx2\])\] 参数：idx1和idx2为int4类型或varchar2类型。
  返回值：无返回值。
  功能描述：无参数时，无索引集合nesttable类型变量删除集合类型的所有元素和空间，后续使用需要重新扩展空间，带索引集合indexbytable类型变量删除所有元素内容；一个参数删除指定位置元素（不删除空间）；两个参数则删除下标区间内的所有元素（不删除空间）。
  示例1，无索引集合nesttable类型变量删除集合类型的所有元素和空间：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,3,4,5);
   BEGIN
     raise info '%', a;
     a.delete();
     raise info '%', a;
   END;
   /
  INFO:  {1,2,3,4,5}
  INFO:  {}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，无索引集合nesttable类型变量删除指定位置元素：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,3,4,5);
   BEGIN
     raise info '%', a;
     a.delete(3);
     raise info '%', a;
     a(3) := 3;
     raise info '%', a;
   END;
   /
  INFO:  {1,2,3,4,5}
  INFO:  {1,2,4,5}
  INFO:  {1,2,3,4,5}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例3，无索引集合nesttable类型变量删除指定区间元素：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     a nest := nest(1,2,3,4,5);
   BEGIN
     raise info '%', a;
     a.delete(2,4);
     raise info '%', a(1);
     raise info '%', a(5);
     raise info '%', a;
   END;
   /
  INFO:  {1,2,3,4,5}
  INFO:  1
  INFO:  5
  INFO:  {1,5}
  ANONYMOUS BLOCK EXECUTE
  ```
  示例4，带索引集合indexbytable类型变量删除集合类型的所有元素和空间：
  ```
  gaussdb=# DECLARE
     type t1 is table of int index by varchar;
     v t1 := t1('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
   BEGIN
     v.delete();
     raise info '%', v.count();
   END;
   /
  INFO:  0
  ANONYMOUS BLOCK EXECUTE
  ```
  示例5，带索引集合indexbytable类型变量删除指定位置元素：
  ```
  gaussdb=# DECLARE
     type t1 is table of int index by varchar;
     v t1 := t1('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
   BEGIN
     raise info '%', v('a');
     v.delete('a');
     raise info '%', v('a');
   END;
   /
  INFO:  1
  ERROR:  no data found
  CONTEXT:  PL/pgSQL function inline_code_block line 6 at RAISE
  ```
  示例6，带索引集合indexbytable类型变量删除指定区间元素：
  ```
  gaussdb=# DECLARE
     type t1 is table of int index by varchar;
     v t1 := t1('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
   BEGIN
     raise info '%', v('b');
     v.delete('a', 'c');
     raise info '%', v('b');
   END;
   /
  INFO:  2
  ERROR:  no data found
  CONTEXT:  PL/pgSQL function inline_code_block line 6 at RAISE
  ```
  
- trim\[(n)\] 参数：n为int4类型。
  返回值：无返回值。
  功能描述：trim函数仅支持nesttable类型变量使用。无参数时，删除末尾一个元素空间，输入参数合法时，删除末尾指定数量元素空间。
  示例：
  ```
  gaussdb=# DECLARE
     type nest is table of int;
     aa nest := nest(11,22,33,44,55);
   BEGIN
     raise info 'aa:%' ,aa;
     aa.trim;  -- 无参数
     raise info 'aa:%' ,aa;
     aa.trim();  -- 无参数
     raise info 'aa:%' ,aa;
     aa.trim(2);  -- 参数合法
     raise info 'aa:%' ,aa;
     aa.trim(2);  -- 集合元素空间不足2, 参数不合法报错
   END;
   /
  INFO:  aa:{11,22,33,44,55}
  INFO:  aa:{11,22,33,44}
  INFO:  aa:{11,22,33}
  INFO:  aa:{11}
  ERROR:  Subscript beyond count
  CONTEXT:  PL/pgSQL function inline_code_block line 11 at assignment
  ```
  
- count\[()\] 参数：无。
  返回值：int类型。
  功能描述：返回集合中存在有效元素的个数。
  示例1，无索引集合类型变量使用count函数：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   aa nest:=nest(11,22,33,44,55);
   BEGIN
   raise info 'count:%' ,aa.count;
   aa.delete(3);  -- 删除一个元素，下标为3的元素无效
   raise info 'count:%' ,aa.count();
   END;
   /
  INFO:  count:5
  INFO:  count:4
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，带索引集合类型变量使用count函数：
  ```
  gaussdb=# DECLARE
   type t1 is table of int index by int;
   aa t1;
   BEGIN
   aa(1) := 111;
   aa(2) := 222;
   aa(3) := 333;
   raise info 'count:%' ,aa.count();
   END;
   /
  INFO:  count:3
  ANONYMOUS BLOCK EXECUTE
  gaussdb=# DECLARE
   type t1 is table of int index by varchar;
   aa t1;
   BEGIN
   aa('aaa') := 111;
   aa('bbb') := 222;
   aa('ccc') := 333;
   raise info 'count:%' ,aa.count;
   END;
   /
  INFO:  count:3
  ANONYMOUS BLOCK EXECUTE
  ```
  
- first\[()\] 参数：无。
  返回值：int类型或varchar类型。
  功能描述：返回集合中第一个有效元素的下标。
  示例1，无索引集合类型变量使用first函数：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   aa nest:=nest(11,22,33,44,55);
   BEGIN
   raise info 'first:%' ,aa.first();
   aa.delete(1);
   raise info 'first:%' ,aa.first;  -- 下标1的元素无效，第一个有效元素下标为2
   END;
   /
  INFO:  first:1
  INFO:  first:2
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，带索引集合类型变量使用first函数：
  ```
  gaussdb=# DECLARE
   type t1 is table of int index by int;
   aa t1;
   BEGIN
   aa(3) := 111;
   aa(2) := 222;
   aa(1) := 333;
   raise info 'first:%' ,aa.first;
   END;
   /
  INFO:  first:1
  ANONYMOUS BLOCK EXECUTE
  gaussdb=# DECLARE
   type t1 is table of int index by varchar;
   aa t1;
   BEGIN
   aa('aaa') := 111;
   aa('bbb') := 222;
   aa('ccc') := 333;
   raise info 'first:%' ,aa.first;
   END;
   /
  INFO:  first:aaa
  ANONYMOUS BLOCK EXECUTE
  ```
  
- last\[()\] 参数：无。
  返回值：int类型或varchar类型。
  功能描述：返回集合中最后一个有效元素的下标。
  示例1，无索引集合类型变量使用last函数：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   aa nest:=nest(11,22,33,44,55);
   BEGIN
   raise info 'last:%' ,aa.last;
   aa.delete(5);
   raise info 'last:%' ,aa.last();  -- 下标5的元素无效，最后一个有效元素下标为4
   END;
   /
  INFO:  last:5
  INFO:  last:4
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，带索引集合类型变量使用last函数：
  ```
  gaussdb=# DECLARE
   type t1 is table of int index by varchar;
   aa t1;
   BEGIN
   aa(3) := 111;
   aa(2) := 222;
   aa(1) := 333;
   raise info 'last:%' ,aa.last();
   END;
   /
  INFO:  last:3
  ANONYMOUS BLOCK EXECUTE
  gaussdb=# DECLARE
   type t1 is table of int index by varchar;
   aa t1;
   BEGIN
   aa('aaa') := 111;
   aa('bbb') := 222;
   aa('ccc') := 333;
   raise info 'last:%' ,aa.last;
   END;
   /
  INFO:  last:ccc
  ANONYMOUS BLOCK EXECUTE
  ```
  
- prior(idx) 参数：idx为int类型或varchar类型。
  返回值：int类型或varchar类型。
  功能描述：返回集合中给定下标的前一个有效元素下标。
  示例1，无索引集合类型变量使用prior函数：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   aa nest:=nest(11,22,33,44,55);
   BEGIN
   raise info 'prior:%' ,aa.prior(3);
   END;
   /
  INFO:  prior:2
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，带索引集合类型变量使用prior函数：
  ```
  gaussdb=# DECLARE
   type t1 is table of int index by varchar;
   aa t1;
   BEGIN
   aa('ccc') := 111;
   aa('bbb') := 222;
   aa('aaa') := 333;
   raise info 'prior:%' ,aa.prior('bbb');
   raise info 'prior:%' ,aa.prior('ddd');
   END;
   /
  INFO:  prior:aaa
  INFO:  prior:ccc
  ANONYMOUS BLOCK EXECUTE
  ```
  
- next(idx) 参数：idx为int类型或varchar类型。
  返回值：int类型或varchar类型。
  功能描述：返回集合中给定下标的后一个有效元素下标。
  示例1，无索引集合类型变量使用next函数：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   aa nest:=nest(11,22,33,44,55);
   BEGIN
   raise info 'next:%' ,aa.next(3);
   END;
   /
  INFO:  next:4
  ANONYMOUS BLOCK EXECUTE
  ```
  示例2，带索引集合类型变量使用next函数：
  ```
  gaussdb=# DECLARE
   type t1 is table of int index by int;
   aa t1;
   BEGIN
   aa(3) := 111;
   aa(2) := 222;
   aa(1) := 333;
   raise info 'next:%' ,aa.next(2);
   raise info 'next:%' ,aa.next(-999);
   END;
   /
  INFO:  next:3
  INFO:  next:1
  ANONYMOUS BLOCK EXECUTE
  ```
  
- limit 参数：无。
  返回值：NULL。
  功能描述：仅用于nesttable类型变量，且返回null值。
  示例：
  ```
  gaussdb=# DECLARE
   type nest is table of int;
   aa nest:=nest(11,22,33,44,55);
   BEGIN
   raise info 'limit:%' ,aa.limit;
   END;
   /
  INFO:  limit:<NULL>
  ANONYMOUS BLOCK EXECUTE
  ```
  
 
#### 集合相关函数
- unnest_table(anynesttable)或unnest(anynesttable) 参数：任意无索引集合类型。
  描述：返回给定nesttable中所有有效元素的结果集，会返回多行数据。
  返回类型：setof anyelement。
  
  示例：
  ```
  gaussdb=# create or replace procedure f1()
  as
      type t1 is table of int;
      v2 t1 := t1(null, 2, 3, 4, null);
      tmp int;
      cursor c1 is select * from unnest_table(v2);
  begin
  open c1;
  for i in 1 .. v2.count loop
      fetch c1 into tmp;
      if tmp is null then
          dbe_output.print_line(i || ': is null');
      else
          dbe_output.print_line(i || ': ' || tmp);
      end if;
  end loop;
  close c1;
  end;
  /
  gaussdb=# call f1();
  1: is null
  2: 2
  3: 3
  4: 4
  5: is null
   f1 
  ----
  (1 row)
  -- nesttable嵌套record类型示例
  gaussdb=# create or replace procedure p1() is
    type rec is record(c1 int, c2 int);
    type t1 is table of rec;
    v t1 := t1(rec(1, 1), rec(2, null), rec(null, null), null);
    v2 t1 := t1();
    cursor cur is select * from unnest(v);
  begin
    v2.extend(v.count);
    open cur;
    for i in 1 .. v.count loop
      fetch cur into v2(i);
      raise info '%', v2(i);
    end loop;
    close cur;
  end;
  /
  gaussdb=# call p1();
  INFO:  (1,1)
  INFO:  (2,)
  INFO:  (,)
  INFO:  (,)
   p1 
  ----
  (1 row)
  gaussdb=# drop procedure if exists p1();
  DROP PROCEDURE
  ```
  ![](https://support.huaweicloud.com/centralized-devg-v3-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  当集合的元素类型为record类型且有元素为NULL时，该元素不会被返回，而是会返回一个非NULL的record值，该record的所有列为NULL，具体可参考示例。
  

- unnest_table(anyindexbytable)或unnest(anyindexbytable) 参数：任意带索引集合类型。
  描述：返回给定indexbytable中所有元素根据索引排序后的结果集，会返回多行数据。
  返回类型：setof anyelement。
  约束：只支持index by int类型，不支持index by varchar类型。
  示例：
  ```
  gaussdb=# create or replace procedure p1()
  as
      type t1 is table of int index by int;
      v2 t1 := t1(1=>1, -10=>(-10), 6=>6, 4=>null);
      tmp int;
      cursor c1 is select * from unnest_table(v2);
  begin
  open c1;
  for i in 1 .. v2.count loop
      fetch c1 into tmp;
      if tmp is null then
          dbe_output.print_line(i || ': is null');
      else
          dbe_output.print_line(i || ': ' || tmp);
      end if;
  end loop;
  close c1;
  end;
  /
  gaussdb=# call p1();
  1: -10
  2: 1
  3: is null
  4: 6
   p1 
  ----
  (1 row)
  -- index by table嵌套record类型示例
  gaussdb=# create or replace procedure p1() is
    type rec is record(c1 int, c2 int);
    type t1 is table of rec index by int;
    v t1 := t1(1 => rec(1, 1), 2 => rec(2, null), 3 => rec(null, null), 4 => null);
    v2 t1 := t1();
    cursor cur is select * from unnest(v);
  begin
    open cur;
    for i in 1 .. v.count loop
      fetch cur into v2(i);
      raise info '%', v2(i);
    end loop;
    close cur;
  END;
  /
  gaussdb=# call p1();
  INFO:  (1,1)
  INFO:  (2,)
  INFO:  (,)
  INFO:  (,)
   p1 
  ----
  (1 row)
  gaussdb=# drop procedure if exists p1();
  DROP PROCEDURE
  ```
  ![](https://support.huaweicloud.com/centralized-devg-v3-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  当集合的元素类型为record类型且有元素为NULL时，该元素不会被返回，而是会返回一个非NULL的record值，该record的所有列为NULL，具体可参考示例。
  
 
