Re: RFC: array_agg() per SQL:200n

From: "Hitoshi Harada" <hitoshi_harada(at)forcia(dot)com>
To: "'Joe Conway'" <mail(at)joeconway(dot)com>, "'Jeff Davis'" <pgsql(at)j-davis(dot)com>
Cc: "'Neil Conway'" <neilc(at)samurai(dot)com>, "'pgsql-hackers'" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: RFC: array_agg() per SQL:200n
Date: 2008-01-29 02:39:23
Message-ID: 006901c86220$28442810$78cc7830$@com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

yet another inverse function I wrote before, though it applies for only 1D
array.

typedef struct _enuminfo{
ArrayType *data;
char *ptr;
int16 typlen;
bool typbyval;
char typalign;
} EnumInfo;

Datum array_enum(PG_FUNCTION_ARGS){
FuncCallContext *funcctx;
MemoryContext oldcontext;
ArrayType *input;
EnumInfo *info;
Datum result;

if(SRF_IS_FIRSTCALL()){
funcctx = SRF_FIRSTCALL_INIT();
oldcontext =
MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

input = PG_GETARG_ARRAYTYPE_P(0);
if(ARR_NDIM(input) != 1){
elog(ERROR, "array_enum() accepts only one dimension
array.");
}
funcctx->max_calls = ArrayGetNItems(ARR_NDIM(input),
ARR_DIMS(input));

info = (EnumInfo*)palloc0(sizeof(EnumInfo));
info->data = (ArrayType*)PG_DETOAST_DATUM_COPY(input);
info->ptr = ARR_DATA_PTR(info->data);
get_typlenbyvalalign(
info->data->elemtype,
&(info->typlen),
&(info->typbyval),
&(info->typalign)
);

funcctx->user_fctx = info;

MemoryContextSwitchTo(oldcontext);
}

funcctx = SRF_PERCALL_SETUP();
info = funcctx->user_fctx;

if(funcctx->call_cntr < funcctx->max_calls){
/* Get source element */
result = fetch_att(info->ptr, info->typbyval, info->typlen);

info->ptr = att_addlength(info->ptr, info->typlen,
PointerGetDatum(info->ptr));
info->ptr = (char *) att_align(info->ptr, info->typalign);
SRF_RETURN_NEXT(funcctx, result);
}else{
SRF_RETURN_DONE(funcctx);
}
}

Hitoshi Harada

> -----Original Message-----
> From: pgsql-hackers-owner(at)postgresql(dot)org
> [mailto:pgsql-hackers-owner(at)postgresql(dot)org] On Behalf Of Joe Conway
> Sent: Tuesday, January 29, 2008 11:00 AM
> To: Jeff Davis
> Cc: Neil Conway; pgsql-hackers
> Subject: Re: [HACKERS] RFC: array_agg() per SQL:200n
>
> Jeff Davis wrote:
> > On Sun, 2008-01-27 at 22:11 -0800, Neil Conway wrote:
> >> p. 564 discusses the required behavior. The result of array_agg() is an
> >> array with one element per input value, sorted according to the optional
> >> ORDER BY clause. NULL input values are included in the array, and the
> >> result for an empty group is NULL, not an empty array. Note that per
> >> page 66, I'd expect array values in the input to array_agg() not to be
> >> flattened.
> >
> > Should there be an inverse operator (a SRF, in this case) that returns a
> > set from an array?
>
> Yes -- see UNNEST
>
> Joe
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2008-01-29 03:31:58 Re: Transition functions for SUM(::int2), SUM(::int4, SUM(::int8])
Previous Message Tom Lane 2008-01-29 02:11:31 Bogus cleanup code in GSSAPI/SSPI patch