Re: Getting a primitive numeric value from "DatumGetNumeric"?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Getting a primitive numeric value from "DatumGetNumeric"?
Дата
Msg-id 21871.1519185281@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Getting a primitive numeric value from "DatumGetNumeric"?  (Demitri Muna <postgresql@demitri.com>)
Ответы Re: Getting a primitive numeric value from "DatumGetNumeric"?  (Demitri Muna <postgresql@demitri.com>)
Список pgsql-general
Demitri Muna <postgresql@demitri.com> writes:
> I’m writing a C extension for PostgreSQL. One possible input datatype for my function is a numeric array, e.g.
ARRAY[[1.5,2.5],[3.5,4.5]].I can use “DatumGetNumeric” to extract a “Numeric” data type from the data, but at some
pointI need to convert this to a number (e.g. double) so that I can do mathy things with it. How does one convert a
“Numeric”to, say, a double? 

If you want to work with doubles, why don't you declare the function as
taking doubles?

> I have a workaround in that I can pass this to my function:
> ARRAY[[1.5,2.5],[3.5,4.5]]::float8[]
> but I’d rather have the code do that instead of bothering the user to remember that.

Well, the implicit coercions work in your favor in this particular case.
You can just do, eg,

regression=# create function foo(float8[]) returns float8 as
regression-# 'select $1[1]' language sql;
CREATE FUNCTION
regression=# select foo(array[1.1,1.2]);
 foo
-----
 1.1
(1 row)

or to emphasize that it is doing a conversion:

regression=# select foo(array[1.1,1.2]::numeric[]);
 foo
-----
 1.1
(1 row)


            regards, tom lane


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

Предыдущее
От: Demitri Muna
Дата:
Сообщение: Getting a primitive numeric value from "DatumGetNumeric"?
Следующее
От: Alexander Farber
Дата:
Сообщение: Not sure if I should CREATE INDEX for text columns on which I plan tofilter later