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: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] 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


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faq

Reply via email to