PL / Python& postgreSQL:返回多列表的最佳方法是什么?

时间:2016-07-11 18:25:49

标签: python postgresql greenplum

在Pl / Python中,“RETURNS setof”或“RETURNS table”子句用于返回类似结构化数据的表。在我看来,必须提供每列的名称才能获得返回的表。如果你有一个包含几列的表格,这很容易。但是,如果你有一个200列的表,那么最好的方法是什么?我是否必须键入所有列的名称(如下所示)或者有办法解决它?任何帮助将非常感激。

下面是一个使用“RETURNS table”子句的示例。代码片段在postgres中创建一个表(mysales),填充它然后使用Pl / Python来获取它并返回列值。为简单起见,我只从表中返回4列。

DROP TABLE IF EXISTS mysales;

CREATE TABLE mysales (id int, year int, qtr int, day int, region
text)  DISTRIBUTED BY (id);

INSERT INTO mysales VALUES 
(1, 2014, 1,1, 'north america'),
(2, 2002, 2,2, 'europe'),
(3, 2014, 3,3, 'asia'),
(4, 2010, 4,4, 'north-america'),
(5, 2014, 1,5, 'europe'),
(6, 2009, 2,6, 'asia'),
(7, 2002, 3,7, 'south america');
DROP FUNCTION IF EXISTS myFunc02();
CREATE OR REPLACE FUNCTION myFunc02() 
RETURNS TABLE (id integer, x integer, y integer, s text) AS 
$$
rv = plpy.execute("SELECT * FROM mysales ORDER BY id", 5)
d  = rv.nrows()
return ( (rv[i]['id'],rv[i]['year'], rv[i]['qtr'], rv[i]['region'])
for i in range(0,d) ) 
$$ LANGUAGE 'plpythonu';

SELECT * FROM myFunc02();

#Here is the output of the SELECT statement:
1; 2014; 1;"north america" 
2; 2002; 2;"europe" 
3; 2014; 3;"asia" 
4; 2010; 4;"north-america" 
5; 2014; 1;"europe" 
6; 2009; 2;"asia" 
7; 2002; 3;"south america"

1 个答案:

答案 0 :(得分:1)

Try this:

CREATE OR REPLACE FUNCTION myFunc02() 
RETURNS TABLE (like mysales) AS 
$$
rv = plpy.execute('SELECT * FROM mysales ORDER BY id;', 5)
d  = rv.nrows()
return rv[0:d]
$$ LANGUAGE 'plpythonu';

which returns:

gpadmin=# SELECT * FROM myFunc02();                             
 id | year | qtr | day |    region
----+------+-----+-----+---------------
  1 | 2014 |   1 |   1 | north america
  2 | 2002 |   2 |   2 | europe
  3 | 2014 |   3 |   3 | asia
  4 | 2010 |   4 |   4 | north-america
  5 | 2014 |   1 |   5 | europe
(5 rows)

Something to consider for MPP like Greenplum and HAWQ is to strive for functions that take data as an argument and return a result, rather than originating the data in the function itself. The same code executes on every segment so occasionally there can be unintended side effects.

Update for SETOF variant:

CREATE TYPE myType AS (id integer, x integer, y integer, s text);

CREATE OR REPLACE FUNCTION myFunc02a() 
RETURNS SETOF myType AS 
$$

# column names of myType ['id', 'x', 'y', 's']
rv = plpy.execute("SELECT id, year as x, qtr as y, region as s FROM mysales ORDER BY id", 5)
d  = rv.nrows()

return rv[0:d]
$$ LANGUAGE 'plpythonu';

Note, to use the same data from the original example, I had to alias each of the columns to corresponding names in myType. Also, you'll have to enumerate all of the columns of mysales if going this route - there isn't a straightforward way to CREATE TYPE foo LIKE tableBar although you might be able to use this to alleviate some of the manual work of enumerate all the names/types:

select string_agg(t.attname || ' ' || t.format_type || ', ') as columns  from 
(
SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod),
  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
   FROM pg_catalog.pg_attrdef d
   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
  a.attnotnull, a.attnum,
  a.attstorage ,
  pg_catalog.col_description(a.attrelid, a.attnum)
FROM pg_catalog.pg_attribute a
LEFT OUTER JOIN pg_catalog.pg_attribute_encoding e
ON   e.attrelid = a .attrelid AND e.attnum = a.attnum
WHERE a.attrelid = (SELECT oid FROM pg_class WHERE relname = 'mysales') AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
) t ;

which returns:

                              columns
-------------------------------------------------------------------
 id integer, year integer, qtr integer, day integer, region text,
(1 row)