Handling Numeric Datums in C

Поиск
Список
Период
Сортировка
От Alban Hertroys
Тема Handling Numeric Datums in C
Дата
Msg-id F4E6B1FF-8950-4B8E-85C9-85C6DD85D95D@solfertje.student.utwente.nl
обсуждение исходный текст
Список pgsql-general
Hey all,

I'm working on some server-side C code that involves a few calculations on a numeric value. The calculation is along
thelines of: 
    (numeric) result = (numeric) value * ((int) base ^ (int) power);

What's the usual approach to write functions like these?

This is how far I got:

#include <postgres.h>
#include <fmgr.h>
#include <executor/spi.h>
#include <executor/executor.h>
#include <utils/numeric.h>
#include <utils/builtins.h>

PG_FUNCTION_INFO_V1(unit_product__aggregate_state);

Datum
unit_product__aggregate_state(PG_FUNCTION_ARGS)
{
    Datum state        = PG_GETARG_DATUM(0);
    HeapTupleHeader t    = PG_GETARG_HEAPTUPLEHEADER(1);

    Datum base, scale, exponent;
    Datum result;
    bool isNull;

    base    = GetAttributeByName(t, "base", &isNull);
    if (isNull)
    PG_RETURN_NULL();

    scale    = GetAttributeByName(t, "scale", &isNull);
    if (isNull)
    PG_RETURN_NULL();

    exponent    = GetAttributeByName(t, "exponent", &isNull);
    if (isNull)
    PG_RETURN_NULL();


    /* state *= base ^ (scale * exponent) */
    result = DirectFunctionCall2(int4mul, scale, exponent);
    result = DirectFunctionCall2(numeric_power, base, result);
    result = DirectFunctionCall2(numeric_mul, state, result);

    PG_RETURN_NUMERIC(result);
}

But it crashes the server, probably because I'm passing ints where numerics are expected. Is this the right approach at
all?And if so, how am I supposed to cast those ints to numerics? 

Are there any examples around for dealing with numerics? The stuff in the documentation
(http://www.postgresql.org/docs/8.4/interactive/xfunc-c.html)conveniently omits numerics, so I had to dig through
doxygento get as far as I am now... 

Regards,

Alban Hertroys

--
Screwing up is the best way to attach something to the ceiling.


!DSPAM:737,4b74028910442077341801!



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

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: Inserting exported bytea value
Следующее
От: Alban Hertroys
Дата:
Сообщение: Fwd: Handling Numeric Datums in C