Re: Wierdness using SUM to add results of custom C function.

Поиск
Список
Период
Сортировка
От Konrad Scherer
Тема Re: Wierdness using SUM to add results of custom C function.
Дата
Msg-id 3C7A846C.CBF95D38@magma.ca
обсуждение исходный текст
Ответ на Wierdness using SUM to add results of custom C function.  (Konrad Scherer <kscherer@magma.ca>)
Ответы Re: Wierdness using SUM to add results of custom C function.
Список pgsql-general
Tom Lane wrote:
>
> Konrad Scherer <kscherer@magma.ca> writes:
> > The C function returns the correct result all the time but the sum
> > returns the wrong result all the time. It mostly returns 23, but it
> > returned 24 once!
>
> Very strange.  May we see the full text of the C function, and the
> SQL declaration you gave for it?
I was hoping someone would just say "Yeah I had that bug. Upgrade to .."
;-)
Here is everything you need to reproduce this.
C function
#include
"postgres.h"
#include
"executor/spi.h"

int countwords(text
*txt);

//this function counts the number of commas in a comma seperated
list
//and then deduces the number of words in the list
int countwords(text *txt)
{
    int i,
count;
    char *
str;
    //retrieve string from text
field
    str = DatumGetCString(DirectFunctionCall1(textout,
PointerGetDatum(txt)));

    //if no text, then no
words
    if (txt->vl_len < 1)
{
        return
0;

}
    //if there is text, but no commas there must be one
word
    count =
1;
    //ignore commas in first and last postion in
list
    for (i=1;i<(txt->vl_len)-1;i++)
{
        if (str[i]==',')
{

count++;

}

}
    return
count;
}
An aside: should I be freeing str after I am done with it? It looks like
it is only returning a pointer into the field.

Compiled on Solaris using
gcc  -Wall -Wmissing-prototypes -Wmissing-declarations -fPIC
-I/export/home/kscherer/postgresql-7.1.3/src/interfaces/libpq
-I/export/home/kscherer/postgresql-7.1.3/src/include   -c -o
countWords.o countWords.c
/usr/ccs/bin/ld -G -Bdynamic -o countWords.so countWords.o

A quick script to run
drop function countWords(text);
CREATE FUNCTION countwords(text)
RETURNS int
AS '/export/home/kscherer/src/countWords.so'
LANGUAGE 'C';

DROP table "sumtest";
CREATE table "sumtest" (
    headwords text );
INSERT into sumtest values (
        'abattage/nm , abat/nm , abats/npl , abattis/npl , abattoir/nm ,
abatteuse/nf
       , abattant/nm , abat-son/nm , abat-foin/nm , abat-feuille/nm ,
        abattable/adj , abat-voix/nm , abat-jour/nm , abatteur/nm ,
        abat-vent/nm , abattu/adj , abattement/nm , s\'abattre/vp');
INSERT into sumtest values (
        'abate.vt, abate.vi, abatement.n');
INSERT into sumtest values ('abattoir/n');
select countwords(headwords) from sumtest;
select sum(countwords(headwords)) from sumtest;

Gives output (repeatedly) on my machine as follows
 countwords
------------
         18
          3
          1
(3 rows)

 sum
-----
  23
(1
row)
You can quickly verify that 18,3,1 are indeed the correct answers and
that 23 is the worng one.
It may have something to do with the parameter passing of the int in the
C function, but then why does it work without using sum??
Thank You in advance

Konrad Scherer


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

Предыдущее
От: Eric Webber
Дата:
Сообщение: LXP, APACHE auth_module with html form, & mod_auth_pg questions
Следующее
От: Bruce Momjian
Дата:
Сообщение: Re: [SQL] Sort problem