2013-04-17 27 views
4

我正在使用postgresql 9.2寫一個plpython函數。假設代碼已經執行了一個返回hstore字符串的查詢。我想,然後發出一個查詢:postgres與hstore字符串文字plpy.execute故障

SELECT hstore_to_matrix('hstorestring') 

比方說,它是一個包含hstore字符串的字符串:A=>B

create or replace function testfreq() 
returns text 
as $$ 
hstorestring = '"GT"=>"thing","HS"=>"[-0.1,-0.2]"' 
rv2 = plpy.execute("SELECT hstore_to_matrix(%s)" % (plpy.quote_literal(hstorestring))) 
plpy.log("Hstore:",rv2[0]) 
return("done") 
$$ LANGUAGE plpythonu; 

運行方式

select testfreq(); 

返回

testdb=# select testfreq(); 
ERROR: plpy.Error: unrecognized error in PLy_spi_execute_fetch_result 
CONTEXT: Traceback (most recent call last): 
PL/Python function "testfreq", line 3, in <module> 
rv2 = plpy.execute("SELECT hstore_to_matrix(%s)" % (plpy.quote_literal(hstorestring))) 
PL/Python function "testfreq": 

如果你r E放置在上面的代碼hstore_to_array,輸出爲:

testdb=# select testfreq(); 
LOG: ('Hstore:', {'hstore_to_array': ['GT', 'thing', 'HS', '[-0.1,-0.2]']}) 
CONTEXT: PL/Python function "testfreq" 
testfreq 
---------- 
done 
(1 row) 

我也曾嘗試使用hstore運營商,而不是功能,我已經嘗試了在pgSQL的終端這些功能,以確保在不嵌入在他們的工作蟒蛇。任何指針將不勝感激。

+0

的字符串文字可以是紅色鯡魚。我得到與任何「SELECT yourdbfunc()」查詢字符串相同的錯誤。 – user2288586

+0

另一個更新。我可以執行plpy.execute(「SELECT hstore_to_array(%s)」%(ploy.quote_literal(s)))。但不是hstore_to_matrix。耶因爲失敗。 – user2288586

+0

請從「CREATE FUNCTION」開始編輯您的問題,以包含一個演示此問題的函數的*完整*示例。如果可能的話,我也想看看你的Python版本。請在此處註釋,以便我得到通知。 –

回答

2

它看起來像PL/Python不正確處理多維數組:

create or replace function testarray() 
returns text 
as $$ 
rv2 = plpy.execute("SELECT ARRAY[ ARRAY['1','2','3'], ARRAY['a','b','c'] ];") 
$$ LANGUAGE plpythonu; 

結果:

craig=# select testarray(); 
ERROR: plpy.Error: unrecognized error in PLy_spi_execute_fetch_result 
CONTEXT: Traceback (most recent call last): 
    PL/Python function "testarray", line 2, in <module> 
    rv2 = plpy.execute("SELECT ARRAY[ ARRAY['1','2','3'], ARRAY['a','b','c'] ];") 
PL/Python function "testarray" 

(測試PG 9.2.4,2.7.3的Python)。

的hstore文本是有效的:

craig=# SELECT '"GT"=>"thing","HS"=>"[-0.1,-0.2]"'::hstore; 
       hstore    
------------------------------------ 
"GT"=>"thing", "HS"=>"[-0.1,-0.2]" 
(1 row) 

和查詢工作PL/Python的以外:

craig=# select hstore_to_matrix('"GT"=>"thing","HS"=>"[-0.1,-0.2]"'); 
     hstore_to_matrix   
--------------------------------- 
{{GT,thing},{HS,"[-0.1,-0.2]"}} 
(1 row) 

進一步表明,這是一個PL/Python的問題。

你或許可以解決這個由鑄造text然後返回結果後鑄造回text[],雖然它的效率不高:

create or replace function testfreq() 
returns text 
as $$ 
hstorestring = '"GT"=>"thing","HS"=>"[-0.1,-0.2]"' 
rv2 = plpy.execute("SELECT hstore_to_matrix(%s)::text" % (plpy.quote_literal(hstorestring))) 
plpy.log("Hstore:",rv2[0])                                      
return("done")                                         
$$ LANGUAGE plpythonu; 

結果:

craig=# SELECT testfreq(); 
testfreq 
---------- 
done 
(1 row)