PostgreSQL 8.2——如何获得任何数组的字符串表示?

标签 postgresql postgresql-8.2

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 不能在 vanilla PostgreSQL 8.2 上转换为 text

事实上,根据以下更改日志(在随源分发的 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;

最佳答案

如果我没有清楚地理解您的问题:但以下是一种将文本转换为数组以及将数组转换为文本的方法

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)

关于PostgreSQL 8.2——如何获得任何数组的字符串表示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24274394/

相关文章:

sql - 如何计算 Postgres 整数数据类型的位数?

sql - 创建触发器以将行移动到存档表

postgresql - Docker:Springboot容器无法连接到PostgreSql容器连接错误

r - 将 RPostgres 或 RPostgreSQL dbGetQuery() 参数传递给 IN 运算符

postgresql - SQLSTATE 附近的语法错误

arrays - 将 NULL 数组填充到自定义聚合函数的最大长度 - 无 array_fill 函数

sql - 如何对多个 SELECT 结果进行数学计算?

postgresql - 按与 "now"的绝对距离对时间戳(包括 future )进行排序

sql - 没有窗口函数的行编号