3

PostgreSQL8.2中, PostgreSQL 的文本到数组转换的逆是什么?

据我们所知:

select '{a,b,c}'::text[] as x, '{{0,1},{2,3}}'::int[][] as y;
/*
 *     x    |       y
 * ---------+---------------
 *  {a,b,c} | {{0,1},{2,3}}
 */

是否有预定义的函数f(anyarray)或运算符来执行相反的操作?通过“逆”,我的意思是应用f(x)::text[]f(y)::int[][]将其带回数组形式。

下面是一个假设的例子,只是为了说明这一点。

select 'x='||f(x) as x_str, 'y'=f(y) as y_str
from (
  select '{a,b,c}'::text[] as x, '{{0,1},{2,3}}'::int[][] as y;
) as a;
/*
 *      x     |        y
 * -----------+-----------------
 *  x={a,b,c} | y={{0,1},{2,3}}
 */

编辑:不幸的是(相信我,这是我在这里浪费任何人时间之前尝试过的第一件事),select x::text对我不起作用(因为我坚​​持使用 PostgreSQL8.2——我使用 Greenplum):

test=> SELECT ('{a,b,c}'::text[])::text;
ERROR:  cannot cast type text[] to text
LINE 1: SELECT ('{a,b,c}'::text[])::text;
                                    ^

编辑#2:与某些人所断言的不同,这与 Greenplum 无关。它与 PostgreSQL8.2 有关。我证实anyarray不能text在香草 PostgreSQL 8.2 上强制转换。

事实上,select ('{a,b,c}'::text[])::text;在 8.3.0 版中引入了有效的更改,根据以下更改日志(在与源一起分发的 HISTORY 文件中):

 * Create a general mechanism that supports casts to and from the
   standard string types (TEXT, VARCHAR, CHAR) for *every* datatype,
   by invoking the datatype's I/O functions (Tom)
   Previously, such casts were available only for types that had
   specialized function(s) for the purpose. These new casts are
   assignment-only in the to-string direction, explicit-only in the
   other direction, and therefore should create no surprising
   behavior.

为了让我 100% 确定这一点,我只是继续从 pg 8.2.23 和 8.3.0 的源代码编译。事实上,在 8.3.0 中它可以工作:

test=# select version();
                                                  version                                                   
------------------------------------------------------------------------------------------------------------
 PostgreSQL 8.3.0 on x86_64-unknown-linux-gnu, compiled by GCC gcc47 (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
(1 row)

test=# select ('{a,b,c}'::text[])::text;
  text   
---------
 {a,b,c}
(1 row)

但不在 8.2.23 上:

test=# select version();
                                                   version                                                   
-------------------------------------------------------------------------------------------------------------
 PostgreSQL 8.2.23 on x86_64-unknown-linux-gnu, compiled by GCC gcc47 (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
(1 row)

test=# select ('{a,b,c}'::text[])::text;
ERROR:  cannot cast type text[] to text
LINE 1: select ('{a,b,c}'::text[])::text;
4

1 回答 1

0

如果我不清楚你的问题:但以下是一种将文本转换为数组和数组到文本的方法

array_to_string(任意数组,文本)

Example:
postgres=# select * from array_to_string(array[1,2,3],'');
 array_to_string 
-----------------
 123
(1 row)

string_to_array(文本,文本):

postgres=# select string_to_array('xx~^~yy~^~zz', '~^~');
 string_to_array 
-----------------
 {xx,yy,zz}
(1 row)

如果您想了解有关数组函数的更多信息,请查看: http ://www.postgresql.org/docs/8.2/static/functions-array.html

更新#1:对于多维数组:

CREATE OR REPLACE FUNCTION aaa(anyarray,text)
RETURNS SETOF text
LANGUAGE plpgsql
AS $function$
DECLARE s $1%type;
BEGIN
FOREACH s SLICE 1 IN ARRAY $1 LOOP
RETURN NEXT array_to_string(s,$2);
END LOOP;
RETURN;
END;
$function$;

postgres=# select aaa('{{a,b,c},{x,y,z}}'::text[], ',');
  aaa  
-------
 a,b,c
 x,y,z
(2 rows)

postgres=# select aaa('{a,b,c}'::text[], ',');
  aaa  
-------
 a,b,c
(1 row)
于 2014-06-18T08:50:19.577 回答