首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有hstore字符串文字的postgres plpy.execute失败

带有hstore字符串文字的postgres plpy.execute失败
EN

Stack Overflow用户
提问于 2013-04-17 08:09:55
回答 1查看 915关注 0票数 4

我正在使用PostgreSQL9.2编写一个plpython函数。假设代码已经执行了一个返回hstore字符串的查询。然后,我希望发出一个查询:

代码语言:javascript
复制
SELECT hstore_to_matrix('hstorestring')

假设它是一个包含hstore字符串的字符串:A=>B

代码语言:javascript
复制
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;

运行方式

代码语言:javascript
复制
select testfreq();

返回

代码语言:javascript
复制
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":  

如果在上面的代码中替换为hstore_to_array,则输出为:

代码语言:javascript
复制
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终端中尝试了这些函数,以确保它们不嵌入到python中时也能正常工作。如果有任何建议,我们将非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2013-04-18 08:36:27

看起来PL/Python不能正确处理多维数组:

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

结果:

代码语言:javascript
复制
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,Python 2.7.3上测试)。

hstore文本有效:

代码语言:javascript
复制
craig=# SELECT '"GT"=>"thing","HS"=>"[-0.1,-0.2]"'::hstore;
               hstore               
------------------------------------
 "GT"=>"thing", "HS"=>"[-0.1,-0.2]"
(1 row)

并且查询在PL/Python之外工作:

代码语言:javascript
复制
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[]来解决此问题,尽管这样效率很低:

代码语言:javascript
复制
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; 

结果:

代码语言:javascript
复制
craig=# SELECT testfreq();
 testfreq 
----------
 done
(1 row)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16049313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档