2

I am new to python and I am trying to do some server programming in pl/python (in order to achive speedier processing) and after many attemps I decided to resource on this list to look for help.

I want to call a pl/python function from another by passing a vessel_speed created type

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;

I am trying to call the dummy function from another pl/python in this manner:

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

Returning an error:

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

Anyone knows how to solve this?...or any alternative solution? Thanks a lot, Juan

1 Answers1

1

your problem if I understand it correctly is that you are trying to pass an array into python as a member of a tuple and PL/Python doesn't support this. This problem exists in your dummy() function. What you could do is use an sql wrapper function to pass only the array in to the dummy function. Something like:

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

Your other option is you can store sog in a character separated value. Something like:

CREATE TABLE c_vessal (
   id serial primary key,
   sog text not null,
   check(array_upper(string_to_array(sog, ':')::real[], 1) > 0)
);
Chris Travers
  • 25,424
  • 6
  • 65
  • 182