在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;