plpython return setof and yield

Поиск
Список
Период
Сортировка
От Nuno Mota
Тема plpython return setof and yield
Дата
Msg-id d79bf9b30908161713i3de39b43t88c0f3a42d211b35@mail.gmail.com
обсуждение исходный текст
Ответы Re: plpython return setof and yield
Список pgsql-general
Hi,

I am kind of new into python, and I have been trying to port some plperl functions into plpython, but I've run up into a problem.

Imagine the following plpython code.

CREATE OR REPLACE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  rv = plpy.execute("SELECT 1")

  for article in range(10) :
    for other in range(10) :
        if (other == 1) : continue
        yield([article,other])
$$LANGUAGE plpythonu;

When executing the function on the psql console I always the this error.

netbo-dev=# select * from greet('Nuno');
ERROR:  error fetching next item from iterator

If I comment the first line:

rv = plpy.execute("select 1")

Turning the code into this:

CREATE OR REPLACE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  #rv = plpy.execute("SELECT 1")

  for article in range(10) :
    for other in range(10) :
        if (other == 1) : continue
        yield([article,other])
$$LANGUAGE plpythonu;

The code works:

netbo-dev=# select * from greet('Nuno');
 how | who
-----+-----
 0   | 0
 0   | 2
 0   | 3
 0   | 4
 0   | 5
 0   | 6

I know the example code is not the best, but What I was tryng to do is execute some SQL then process it and return it back.

I also know I could just generate the rows and place them in a list and then return it, but I would like to know why the yield function does'nt work in this case.

This was tried on with:

PostgreSQL 8.3.7 and Python 2.5.1

Thanks,
Nuno Mota

В списке pgsql-general по дате отправления:

Предыдущее
От: Alvaro Herrera
Дата:
Сообщение: Re: Generating random unique alphanumeric IDs
Следующее
От: Tom Lane
Дата:
Сообщение: Re: plpgsql function to validate e-mail