gpt4 book ai didi

python - 从 plpython 函数之间传递一个 real[] 作为参数

转载 作者:行者123 更新时间:2023-11-29 12:22:14 24 4
gpt4 key购买 nike

我是 python 的新手,我正在尝试在 pl/python 中进行一些服务器编程(以实现更快的处理速度),经过多次尝试后,我决定在此列表中寻找资源以寻求帮助。

我想通过传递 vessel_speed 创建的类型从另一个调用 pl/python 函数

CREATE TYPE vessel_speed AS (
mmsi integer,
sog real[]
);

CREATE OR REPLACE FUNCTION dummy(c_vessel vessel_speed)
RETURNS real[]
AS $$
return c_vessel["sog"]

$$ LANGUAGE plpython3u;

我试图以这种方式从另一个 pl/python 调用虚拟函数:

st = "SELECT dummy(($1,ARRAY[$2])::vessel_speed)"
pst = plpy.prepare(st, ["integer", "real[]"])
rv = plpy.execute(pst, [112, (1.2,3.1)])

返回错误:

ERROR: spiexceptions.FeatureNotSupported: cannot convert multidimensional array to Python list
Estado SQL:0A000
Detalle:PL/Python only supports one-dimensional arrays.

任何人都知道如何解决这个问题?...或任何替代解决方案?多谢,胡安

最佳答案

如果我理解正确的话,你的问题是你试图将数组作为元组的成员传递给 python,而 PL/Python 不支持这个。这个问题存在于你的 dummy() 函数中。您可以做的是使用 sql 包装函数仅将数组传递给虚拟函数。像这样的东西:

CREATE OR REPLACE FUNCTION dummy(c_vessel vessel_speed)
RETURNS real[]
AS $$
SELECT dummy(c_vessal.sog);
$$ language sql;

CREATE OR REPLACE FUNCTION dummy(sog real[]) returns real[] LANGUAGE plpython3u AS
$$
return sog;
$$;

您的另一个选择是您可以将 sog 存储在字符分隔值中。像这样的东西:

CREATE TABLE c_vessal (
id serial primary key,
sog text not null,
check(array_upper(string_to_array(sog, ':')::real[], 1) > 0)
);

关于python - 从 plpython 函数之间传递一个 real[] 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15231607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com