help defining a basic type operator

Поиск
Список
Период
Сортировка
От Luca Ferrari
Тема help defining a basic type operator
Дата
Msg-id CAKoxK+6huo++-LL50d-5qvST94cFKpNR4_qZnEqDYZfbvbXBPQ@mail.gmail.com
обсуждение исходный текст
Ответы Re: help defining a basic type operator  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Hi all,
I'm trying to define a custom data type that would represent a number
of bytes in a lossy human way.
The type is defined as:

typedef struct HFSize
{
        double          size;
        int       scaling;
} HFSize;

and the operator function is defined as:

Datum
hfsize_add(PG_FUNCTION_ARGS)
{

  HFSize *first  = (HFSize *) PG_GETARG_POINTER(0);
  HFSize *second = (HFSize *) PG_GETARG_POINTER(1);
  HFSize *sum    = new_HFSize();

  to_bytes( first );
  to_bytes( second );

  elog( DEBUG1, "sum %s + %s ", to_string( first ), to_string( second ) );
  sum->size = first->size + second->size;

  elog( DEBUG1, "Final sum %s ", to_string( sum ) );
        PG_RETURN_POINTER( sum );
}



The problem is that, even if the last elog shows a correct result, the
final value returned via PG_RETURN_POINTER is something totally
different with the double value set to zero and an apparently random
'scaling':

# set client_min_messages to debug;
SET
testdb=# SELECT '1030'::hfsize + '2030'::hfsize;
DEBUG:  Converting to human text format 1030.00-bytes
LINE 1: SELECT '1030'::hfsize + '2030'::hfsize;
               ^
DEBUG:  Converting to human text format 2030.00-bytes
LINE 1: SELECT '1030'::hfsize + '2030'::hfsize;
                                ^
DEBUG:  sum 1030.00-bytes + 2030.00-bytes
DEBUG:  Final sum 3060.00-bytes
 ?column?
----------
 0.00-64


I've tried also to return one of the two operands from the add
function, so something like:
Datum
hfsize_add(PG_FUNCTION_ARGS)
{

  HFSize *first  = (HFSize *) PG_GETARG_POINTER(0);
  HFSize *second = (HFSize *) PG_GETARG_POINTER(1);
  PG_RETURN_POINTER( first );
}

but again the result has a zero value and a random scaling, and most
notably is not the first operand. Also memory addresses (used with %x)
are different from inside and outside the add function.
Is there something I'm missing?

Thanks,
Luca


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

Предыдущее
От: TalGloz
Дата:
Сообщение: Re: Linker errors while creating a PostgreSQL C extension function.
Следующее
От: Tom Lane
Дата:
Сообщение: Re: help defining a basic type operator