如何在 Postgres 中将一个类型拆分为多个列?

发布于 2024-10-15 11:38:02 字数 512 浏览 4 评论 0原文

我有以下代码从 pl/python 返回多个值:

CREATE TYPE named_value AS (
  name   text,
  value  integer
);
CREATE or replace FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  return [ name, value ]
$$ LANGUAGE plpythonu;

select make_pair('egg', 4) as column;

输出是:

column
(egg,4)

我想要做的是将输出分成两个单独的列。像这样:

column, column2
egg, 4

我该怎么做?谷歌搜索了 1 个小时,我一无所获。所以我希望最后添加一些搜索关键词: 多个返回值 多个结果 多列 取消嵌套列表 取消嵌套集合

I have the following code to return multiple values from pl/python:

CREATE TYPE named_value AS (
  name   text,
  value  integer
);
CREATE or replace FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $
  return [ name, value ]
$ LANGUAGE plpythonu;

select make_pair('egg', 4) as column;

The output is:

column
(egg,4)

What I want to do is to split the output into two separate columns. Like this:

column, column2
egg, 4

How do I do this? Googled for 1 hour got me nowhere. So I hope I will add some search keywords in the end:
multiple return values multiple results multiple columns unnest list unnest set

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好菇凉咱不稀罕他 2024-10-22 11:38:02

是的,这个语法有点古怪,需要额外的括号:

select (make_pair('egg', 4)).name

要从输出中获取多个组件,同时仅调用该函数一次,您可以使用子选择:

select (x.column).name, (x.column).value from (select make_pair('egg', 4) as column) x;

Yeah, the syntax for this is a bit wacky, requiring extra parentheses:

select (make_pair('egg', 4)).name

To get multiple components from the output while only invoking the function once, you can use a sub-select:

select (x.column).name, (x.column).value from (select make_pair('egg', 4) as column) x;
清醇 2024-10-22 11:38:02
SELECT * FROM make_pair('egg', 4);

和一些变体:

 SELECT name, value FROM make_pair('egg', 4) AS x;


 SELECT a, b FROM make_pair('egg', 4) AS x(a,b);
SELECT * FROM make_pair('egg', 4);

and some variants:

 SELECT name, value FROM make_pair('egg', 4) AS x;


 SELECT a, b FROM make_pair('egg', 4) AS x(a,b);
那支青花 2024-10-22 11:38:02

我发现的一个解决方案是使用 join:

create table tmp (a int, b int, c int);
insert into tmp (a,b,c) values (1,2,3), (3,4,5), (5,12,13);
create type ispyth3 as (is_it boolean, perimeter int);
create function check_it(int, int, int) returns ispyth3 as $
    begin
        return ($1*$1 + $2*$2 = $3*$3, $1+$2+$3);
    end
$ language plpgsql;
select * from tmp join check_it(a,b,c) on 1=1;

这返回:

 a | b  | c  | is_it | perimeter 
---+----+----+-------+-----------
 1 |  2 |  3 | f     |         6
 3 |  4 |  5 | t     |        12
 5 | 12 | 13 | t     |        30
(3 rows)

A solution I found was to use join:

create table tmp (a int, b int, c int);
insert into tmp (a,b,c) values (1,2,3), (3,4,5), (5,12,13);
create type ispyth3 as (is_it boolean, perimeter int);
create function check_it(int, int, int) returns ispyth3 as $
    begin
        return ($1*$1 + $2*$2 = $3*$3, $1+$2+$3);
    end
$ language plpgsql;
select * from tmp join check_it(a,b,c) on 1=1;

This returns:

 a | b  | c  | is_it | perimeter 
---+----+----+-------+-----------
 1 |  2 |  3 | f     |         6
 3 |  4 |  5 | t     |        12
 5 | 12 | 13 | t     |        30
(3 rows)
时光病人 2024-10-22 11:38:02

以下是避免运行该函数两次并同时避免子查询的工作代码。

CREATE TYPE named_value AS (
  name   text,
  value  integer
);

CREATE or replace FUNCTION setcustomvariable(variablename text, variablevalue named_value)
  RETURNS named_value
AS $
  GD[variablename] = variablevalue
  return variablevalue
$ LANGUAGE plpythonu;

CREATE or replace FUNCTION getcustomvariable(variablename text)
  RETURNS named_value
AS $
  return GD[variablename]
$ LANGUAGE plpythonu;

CREATE or replace FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $
  return [ name, value ]
$ LANGUAGE plpythonu;

select setcustomvariable('result', make_pair('egg', 4)), (getcustomvariable('result')).name, (getcustomvariable('result')).value

The following is working code to avoid having to run the function twice and at the same time avoid a subquery.

CREATE TYPE named_value AS (
  name   text,
  value  integer
);

CREATE or replace FUNCTION setcustomvariable(variablename text, variablevalue named_value)
  RETURNS named_value
AS $
  GD[variablename] = variablevalue
  return variablevalue
$ LANGUAGE plpythonu;

CREATE or replace FUNCTION getcustomvariable(variablename text)
  RETURNS named_value
AS $
  return GD[variablename]
$ LANGUAGE plpythonu;

CREATE or replace FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $
  return [ name, value ]
$ LANGUAGE plpythonu;

select setcustomvariable('result', make_pair('egg', 4)), (getcustomvariable('result')).name, (getcustomvariable('result')).value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文