Re: Setof RangeType returns - Mailing list pgsql-hackers
From | Patrick Handja |
---|---|
Subject | Re: Setof RangeType returns |
Date | |
Msg-id | CAOKRWVAssZXC9z0PxdxqFQvgGNjGG8M3euiUF1H2cyyTKVwY7Q@mail.gmail.com Whole thread Raw |
In response to | Re: Setof RangeType returns (Patrick Handja <patrick.bungama@gmail.com>) |
Responses |
Re: Setof RangeType returns
|
List | pgsql-hackers |
Just figured out. I think I can use RANGE_EMPTY and it will be like:
> typcache =range_get_typcache(fcinfo, RangeTypeGetOid(RANGE_EMPTY));
Regards,
Andjasubu Bungama, Patrick
Le mar. 1 déc. 2020 à 16:42, Patrick Handja <patrick.bungama@gmail.com> a écrit :
Thanks for the feedback Tom!> TypeCacheEntry *typcache;
> PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper, false));The use of typcache really confuses me. range_get_typcache() is used in order to initialize typcache> typcache =range_get_typcache(fcinfo, RangeTypeGetOid(r1));In my case, I do not have a range as an argument, I am receiving 2 int, which I am using to create a range. How can I initialize typcache in this case?That's the part where I am really stuck.Datum
make_range_griis(PG_FUNCTION_ARGS){
RangeBound lower;
RangeBound upper;
int32 start = PG_GETARG_INT32(0);
int32 finish = PG_GETARG_INT32(1);
lower.val = (Datum) (start);
lower.infinite = false;
lower.lower = true;
upper.val = (Datum) (finish);
upper.infinite = false;
upper.lower = false;
if (!lower.infinite && !lower.inclusive){
lower.val = DirectFunctionCall2(int4pl, lower.val, Int32GetDatum(1));
lower.inclusive = true;
}
if (!upper.infinite && upper.inclusive){
upper.val = DirectFunctionCall2(int4pl, upper.val, Int32GetDatum(1));
upper.inclusive = false;
}
TypeCacheEntry *typcache;
//> typcache = ??????;
PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper, false));
}regards,Andjasubu Bungama, PatrickLe ven. 27 nov. 2020 à 14:24, Tom Lane <tgl@sss.pgh.pa.us> a écrit :Patrick Handja <patrick.bungama@gmail.com> writes:
> This is what I am doing:
> static int
> get_range_lower(FunctionCallInfo fcinfo, RangeType *r1)
> {
> /* Return NULL if there's no finite lower bound */
> if (empty || lower.infinite)
> PG_RETURN_NULL();
You can't really use PG_RETURN_NULL here, mainly because there is
no good value for it to return from get_range_lower().
> return (lower.val);
Here and elsewhere, you're cavalierly casting between Datum and int.
While you can get away with that as long as the SQL type you're
working with is int4, it's bad style; mainly because it's confusing,
but also because you'll have a hard time adapting the code if you
ever want to work with some other type. Use DatumGetInt32 or
Int32GetDatum as appropriate.
> TypeCacheEntry *typcache;
> PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper, false));
This sure appears to be passing an uninitialized typcache pointer
to range_serialize(). If your compiler isn't whining about that,
you don't have adequately paranoid warning options enabled.
That's an excellent way to waste a lot of time, as you just have.
C is an unforgiving language, so you need all the help you can get.
BTW, use of PG_RETURN_RANGE_P here isn't very appropriate either,
since the function is not declared as returning Datum.
regards, tom lane
pgsql-hackers by date: