簡體   English   中英

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

[英]PL/Python & postgreSQL: What is the best way to return a table of many columns?

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

嘗試這個:

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

返回:

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)

對於MPP(例如Greenplum和HAWQ),需要考慮的事情是爭取將數據作為參數並返回結果的函數,而不是在函數本身中發起數據。 相同的代碼在每個段上執行,因此偶爾會有意外的副作用。

SETOF變體的更新:

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

注意,要使用原始示例中的相同數據,我必須將每列的別名都命名為myType相應名稱。 此外,如果要使用此路線,則必須枚舉mysales所有列-盡管可以使用此方法來減輕一些枚舉全部的手動工作,但沒有一種直接CREATE TYPE foo LIKE tableBar方法。名稱/類型:

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 ;

返回:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM