c language functions
Hello.
I'm trying to create a generic add function.
I have defined a type my_uint and it needs a '+' operator.
This operator should work like normal int + int operation.
The function is defined expecting arguments (my_uint, anyelement).
I'm confused in retrieving the anyelement type, value and than do the add
operation and return the correct value and type.
I tried to use PG_GETARG_DATUM, but I don't know how to extract the value
from it (should it be a uint32, uint64, float or double).
Any tips?
On Wed, Apr 3, 2013 at 11:26 AM, Rodrigo Barboza
<rodrigombufrj@gmail.com> wrote:
Hello.
I'm trying to create a generic add function.
I have defined a type my_uint and it needs a '+' operator.
This operator should work like normal int + int operation.
The function is defined expecting arguments (my_uint, anyelement).I'm confused in retrieving the anyelement type, value and than do the add
operation and return the correct value and type.
I tried to use PG_GETARG_DATUM, but I don't know how to extract the value
from it (should it be a uint32, uint64, float or double).Any tips?
Well, the information the function ends up receiving is going to
depend on how the function is declared at an SQL level. So if you are
defining the function to take an argument of anyelement (which seems
unlikely to be a useful thing to do, but let's suppose you do it
anyway) then look at the C code for some other function that takes an
anyelement argument and look at how that function unpacks it.
Similarly, if you declare the function to take int4 argument, then go
look at the C code function that takes an int4 argument and see what
it does to unpack it.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Why not useful?
If I don't make it receive anyelement, I will have to create an add
function for each type.
Correct me if I'm wrong.
On Wed, Apr 3, 2013 at 2:27 PM, Robert Haas <robertmhaas@gmail.com> wrote:
Show quoted text
On Wed, Apr 3, 2013 at 11:26 AM, Rodrigo Barboza
<rodrigombufrj@gmail.com> wrote:Hello.
I'm trying to create a generic add function.
I have defined a type my_uint and it needs a '+' operator.
This operator should work like normal int + int operation.
The function is defined expecting arguments (my_uint, anyelement).I'm confused in retrieving the anyelement type, value and than do the add
operation and return the correct value and type.
I tried to use PG_GETARG_DATUM, but I don't know how to extract the value
from it (should it be a uint32, uint64, float or double).Any tips?
Well, the information the function ends up receiving is going to
depend on how the function is declared at an SQL level. So if you are
defining the function to take an argument of anyelement (which seems
unlikely to be a useful thing to do, but let's suppose you do it
anyway) then look at the C code for some other function that takes an
anyelement argument and look at how that function unpacks it.Similarly, if you declare the function to take int4 argument, then go
look at the C code function that takes an int4 argument and see what
it does to unpack it.--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
Rodrigo Barboza <rodrigombufrj@gmail.com> writes:
Why not useful?
If I don't make it receive anyelement, I will have to create an add
function for each type.
If you make it anyelement, then you're contracting to be able to add
any datatype whatsoever to a my_uint. This is nonsensical.
You'd be better off declaring several specific addition functions,
one for each other type. This will be an order of magnitude easier
to write, and probably run an order of magnitude faster too, because
just checking to see what type you got would already be significantly
more expensive than adding a couple of integers ought to be.
Look at the built-in types and functions for precedent. There are
indeed separate functions for int2 + int2, int2 + int4, int4 + int2,
int4 + int4, etc etc. If we were starting from scratch, we might reduce
that to just int4 + int4 and rely on the implicit coercion from int2 to
int4 to handle the other cases; but there's no way we'd put in run-time
type determination.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Well, I was checking inside my function the type of the second argument and
switching between the allowed types.
This way kind of does the same thing of many functions, doesn't it?
On Wed, Apr 3, 2013 at 3:39 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Show quoted text
Rodrigo Barboza <rodrigombufrj@gmail.com> writes:
Why not useful?
If I don't make it receive anyelement, I will have to create an add
function for each type.If you make it anyelement, then you're contracting to be able to add
any datatype whatsoever to a my_uint. This is nonsensical.You'd be better off declaring several specific addition functions,
one for each other type. This will be an order of magnitude easier
to write, and probably run an order of magnitude faster too, because
just checking to see what type you got would already be significantly
more expensive than adding a couple of integers ought to be.Look at the built-in types and functions for precedent. There are
indeed separate functions for int2 + int2, int2 + int4, int4 + int2,
int4 + int4, etc etc. If we were starting from scratch, we might reduce
that to just int4 + int4 and rely on the implicit coercion from int2 to
int4 to handle the other cases; but there's no way we'd put in run-time
type determination.regards, tom lane
On Wed, Apr 3, 2013 at 3:25 PM, Rodrigo Barboza <rodrigombufrj@gmail.com> wrote:
Well, I was checking inside my function the type of the second argument and
switching between the allowed types.
This way kind of does the same thing of many functions, doesn't it?
Sure, except that it will also call your function when someone does
my_int + text or my_int + bytea or my_int + box, which are surely a
lot less sensible. It's probably a good idea to assume that if you
make your type work like the existing types, it's more likely to end
well than if you make it behave completely differently.
...Robert
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
I see, that's true.
I'm returning unknown type, there is a little more overhead. But it's
working now. =]
Thanks for the help guys!
On Wed, Apr 3, 2013 at 6:17 PM, Robert Haas <robertmhaas@gmail.com> wrote:
Show quoted text
On Wed, Apr 3, 2013 at 3:25 PM, Rodrigo Barboza <rodrigombufrj@gmail.com>
wrote:Well, I was checking inside my function the type of the second argument
and
switching between the allowed types.
This way kind of does the same thing of many functions, doesn't it?Sure, except that it will also call your function when someone does
my_int + text or my_int + bytea or my_int + box, which are surely a
lot less sensible. It's probably a good idea to assume that if you
make your type work like the existing types, it's more likely to end
well than if you make it behave completely differently....Robert