Postgres C 扩展数据类型定义
Postgres C extended data type definition
在处理下面的问题时,Postgres处理比较复杂的结构有点棘手。我想建立一个二维数组结构,但我不知道如何让 Postgres C 支持我这样做?有人有什么想法吗?
Table
id contents(text) num(double)
1 I love you. {1,3,4,5,6,7,8,10}
2 why do it? {3,4,2,11,12,33,44,15}
3 stopping. {22,33,11,15,14,22,11,55}
4 try it again. {15,12,11,22,55,21,31,11}
对数组每个位置的行进行排序,得到fo.lowing结构。下面第一行的结果是num字段列数组的第一个位置,所以on.thecount 4是指返回第n个排序。
select my_func(contents, num, 4) from table;
预期结果:
result
{('stopping.', 22), ('try it again.', 15), ('why do it?', 3), ('I love you.', 1)}
{('stopping.', 33), ('try it again.', 12), ('why do it?', 4), ('I love you.', 3)}
{('stopping.', 11), ('try it again.', 11), ('I love you.', 4), ('why do it?', 2)}
......
......
提前致谢。
我不确定你为什么需要 C 扩展数据类型,但下面会给你你想要的,并且可以作为 plpgsql 函数实现。
WITH t1 AS (
SELECT id, contents, unnest (num) AS n FROM table
),
t2 AS (
SELECT id, contents, n,
row_number () OVER (PARTITION BY id ORDER BY id) AS o
FROM t1 ORDER BY o ASC, n DESC, id ASC
),
t3 AS (
SELECT array_agg (ROW (contents, n)) AS a, o
FROM t2 GROUP BY o ORDER BY o
)
SELECT array_agg (a ORDER BY o) FROM t3;
更新:上述问题可能是'unnest'的未定义顺序。
下面给出了 index 和 num 的一致关系,但是需要明确写出 num 数组的大小。
WITH RECURSIVE t1 (i, id, contents, num) AS (
SELECT 1, id, contents, num[1] FROM table
UNION ALL
SELECT t1.i + 1, table.id, table.contents, table.num[t1.i + 1]
FROM t1, table
WHERE t1.id = table.id AND t1.i < 8 -- put here size of array
),
t2 (i, d) AS (
SELECT i, array_agg (ROW (contents, num) ORDER BY num DESC)
FROM t1 GROUP BY i
)
SELECT array_agg (d ORDER BY i) FROM t2;
在处理下面的问题时,Postgres处理比较复杂的结构有点棘手。我想建立一个二维数组结构,但我不知道如何让 Postgres C 支持我这样做?有人有什么想法吗?
Table
id contents(text) num(double)
1 I love you. {1,3,4,5,6,7,8,10}
2 why do it? {3,4,2,11,12,33,44,15}
3 stopping. {22,33,11,15,14,22,11,55}
4 try it again. {15,12,11,22,55,21,31,11}
对数组每个位置的行进行排序,得到fo.lowing结构。下面第一行的结果是num字段列数组的第一个位置,所以on.thecount 4是指返回第n个排序。
select my_func(contents, num, 4) from table;
预期结果:
result
{('stopping.', 22), ('try it again.', 15), ('why do it?', 3), ('I love you.', 1)}
{('stopping.', 33), ('try it again.', 12), ('why do it?', 4), ('I love you.', 3)}
{('stopping.', 11), ('try it again.', 11), ('I love you.', 4), ('why do it?', 2)}
......
......
提前致谢。
我不确定你为什么需要 C 扩展数据类型,但下面会给你你想要的,并且可以作为 plpgsql 函数实现。
WITH t1 AS (
SELECT id, contents, unnest (num) AS n FROM table
),
t2 AS (
SELECT id, contents, n,
row_number () OVER (PARTITION BY id ORDER BY id) AS o
FROM t1 ORDER BY o ASC, n DESC, id ASC
),
t3 AS (
SELECT array_agg (ROW (contents, n)) AS a, o
FROM t2 GROUP BY o ORDER BY o
)
SELECT array_agg (a ORDER BY o) FROM t3;
更新:上述问题可能是'unnest'的未定义顺序。 下面给出了 index 和 num 的一致关系,但是需要明确写出 num 数组的大小。
WITH RECURSIVE t1 (i, id, contents, num) AS (
SELECT 1, id, contents, num[1] FROM table
UNION ALL
SELECT t1.i + 1, table.id, table.contents, table.num[t1.i + 1]
FROM t1, table
WHERE t1.id = table.id AND t1.i < 8 -- put here size of array
),
t2 (i, d) AS (
SELECT i, array_agg (ROW (contents, num) ORDER BY num DESC)
FROM t1 GROUP BY i
)
SELECT array_agg (d ORDER BY i) FROM t2;