|
| 1 | +/* |
| 2 | + * Integer array aggregator / enumerator |
| 3 | + * |
| 4 | + * Mark L. Woodward |
| 5 | + * DMN Digital Music Network. |
| 6 | + * www.dmn.com |
| 7 | + * |
| 8 | + * Copyright (C) Digital Music Network |
| 9 | + * December 20, 2001 |
| 10 | + * |
| 11 | + * This file is the property of the Digital Music Network (DMN). |
| 12 | + * It is being made available to users of the PostgreSQL system |
| 13 | + * under the BSD license. |
| 14 | + * |
| 15 | + */ |
| 16 | +#include "postgres.h" |
| 17 | + |
| 18 | +#include <ctype.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <sys/types.h> |
| 21 | +#include <string.h> |
| 22 | +#include "postgres.h" |
| 23 | +#include "access/heapam.h" |
| 24 | +#include "catalog/catname.h" |
| 25 | +#include "catalog/indexing.h" |
| 26 | +#include "catalog/pg_proc.h" |
| 27 | +#include "executor/executor.h" |
| 28 | +#include "utils/fcache.h" |
| 29 | +#include "utils/sets.h" |
| 30 | +#include "utils/syscache.h" |
| 31 | +#include "access/tupmacs.h" |
| 32 | +#include "access/xact.h" |
| 33 | +#include "fmgr.h" |
| 34 | +#include "miscadmin.h" |
| 35 | +#include "utils/array.h" |
| 36 | +#include "utils/builtins.h" |
| 37 | +#include "utils/memutils.h" |
| 38 | +#include "utils/lsyscache.h" |
| 39 | + |
| 40 | + |
| 41 | +/* This is actually a postgres version of a one dimentional array */ |
| 42 | + |
| 43 | +typedef struct agg |
| 44 | +{ |
| 45 | + ArrayType a; |
| 46 | + int items; |
| 47 | + int lower; |
| 48 | + int4 array[1]; |
| 49 | +}PGARRAY; |
| 50 | + |
| 51 | +/* This is used to keep track of our position during enumeration */ |
| 52 | +typedef struct callContext |
| 53 | +{ |
| 54 | + PGARRAY *p; |
| 55 | + int num; |
| 56 | + int flags; |
| 57 | +}CTX; |
| 58 | + |
| 59 | +#define TOASTED 1 |
| 60 | +#define START_NUM 8 |
| 61 | +#define PGARRAY_SIZE(n) (sizeof(PGARRAY) + ((n-1)*sizeof(int4))) |
| 62 | + |
| 63 | +static PGARRAY * GetPGArray(int4 state, int fAdd); |
| 64 | +static PGARRAY *ShrinkPGArray(PGARRAY *p); |
| 65 | + |
| 66 | +Datum int_agg_state(PG_FUNCTION_ARGS); |
| 67 | +Datum int_agg_final_count(PG_FUNCTION_ARGS); |
| 68 | +Datum int_agg_final_array(PG_FUNCTION_ARGS); |
| 69 | +Datum int_enum(PG_FUNCTION_ARGS); |
| 70 | + |
| 71 | +PG_FUNCTION_INFO_V1(int_agg_state); |
| 72 | +PG_FUNCTION_INFO_V1(int_agg_final_count); |
| 73 | +PG_FUNCTION_INFO_V1(int_agg_final_array); |
| 74 | +PG_FUNCTION_INFO_V1(int_enum); |
| 75 | + |
| 76 | +/* |
| 77 | + * Manage the aggregation state of the array |
| 78 | + * You need to specify the correct memory context, or it will vanish! |
| 79 | + */ |
| 80 | +static PGARRAY * GetPGArray(int4 state, int fAdd) |
| 81 | +{ |
| 82 | + PGARRAY *p = (PGARRAY *) state; |
| 83 | + |
| 84 | + if(!state) |
| 85 | + { |
| 86 | + /* New array */ |
| 87 | + int cb = PGARRAY_SIZE(START_NUM); |
| 88 | + |
| 89 | + p = (PGARRAY *) MemoryContextAlloc(TopTransactionContext, cb); |
| 90 | + |
| 91 | + if(!p) |
| 92 | + { |
| 93 | + elog(ERROR,"Integer aggregator, cant allocate TopTransactionContext memory"); |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + p->a.size = cb; |
| 98 | + p->a.ndim= 0; |
| 99 | + p->a.flags = 0; |
| 100 | + p->items = 0; |
| 101 | + p->lower= START_NUM; |
| 102 | + } |
| 103 | + else if(fAdd) |
| 104 | + { /* Ensure array has space */ |
| 105 | + if(p->items >= p->lower) |
| 106 | + { |
| 107 | + PGARRAY *pn; |
| 108 | + int n = p->lower + p->lower; |
| 109 | + int cbNew = PGARRAY_SIZE(n); |
| 110 | + |
| 111 | + pn = (PGARRAY *) repalloc(p, cbNew); |
| 112 | + |
| 113 | + if(!pn) |
| 114 | + { /* Realloc failed! Reallocate new block. */ |
| 115 | + pn = (PGARRAY *) MemoryContextAlloc(TopTransactionContext, cbNew); |
| 116 | + if(!pn) |
| 117 | + { |
| 118 | + elog(ERROR, "Integer aggregator, REALLY REALLY can't alloc memory"); |
| 119 | + return (PGARRAY *) NULL; |
| 120 | + } |
| 121 | + memcpy(pn, p, p->a.size); |
| 122 | + pfree(p); |
| 123 | + } |
| 124 | + pn->a.size = cbNew; |
| 125 | + pn->lower = n; |
| 126 | + return pn; |
| 127 | + } |
| 128 | + } |
| 129 | + return p; |
| 130 | +} |
| 131 | + |
| 132 | +/* Shrinks the array to its actual size and moves it into the standard |
| 133 | + * memory allocation context, frees working memory */ |
| 134 | +static PGARRAY *ShrinkPGArray(PGARRAY *p) |
| 135 | +{ |
| 136 | + PGARRAY *pnew=NULL; |
| 137 | + if(p) |
| 138 | + { |
| 139 | + /* get target size */ |
| 140 | + int cb = PGARRAY_SIZE(p->items); |
| 141 | + |
| 142 | + /* use current transaction context */ |
| 143 | + pnew = palloc(cb); |
| 144 | + |
| 145 | + if(pnew) |
| 146 | + { |
| 147 | + /* Fix up the fields in the new structure, so Postgres understands */ |
| 148 | + memcpy(pnew, p, cb); |
| 149 | + pnew->a.size = cb; |
| 150 | + pnew->a.ndim=1; |
| 151 | + pnew->a.flags = 0; |
| 152 | + pnew->lower = 0; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + elog(ERROR, "Integer aggregator, can't allocate memory"); |
| 157 | + } |
| 158 | + pfree(p); |
| 159 | + } |
| 160 | + return pnew; |
| 161 | +} |
| 162 | + |
| 163 | +/* Called for each iteration during an aggregate function */ |
| 164 | +Datum int_agg_state(PG_FUNCTION_ARGS) |
| 165 | +{ |
| 166 | + int4 state = PG_GETARG_INT32(0); |
| 167 | + int4 value = PG_GETARG_INT32(1); |
| 168 | + |
| 169 | + PGARRAY *p = GetPGArray(state, 1); |
| 170 | + if(!p) |
| 171 | + { |
| 172 | + elog(ERROR,"No aggregate storage\n"); |
| 173 | + } |
| 174 | + else if(p->items >= p->lower) |
| 175 | + { |
| 176 | + elog(ERROR,"aggregate storage too small\n"); |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + p->array[p->items++]= value; |
| 181 | + } |
| 182 | + PG_RETURN_INT32(p); |
| 183 | +} |
| 184 | + |
| 185 | +/* This is the final function used for the integer aggregator. It returns all the integers |
| 186 | + * collected as a one dimentional integer array */ |
| 187 | +Datum int_agg_final_array(PG_FUNCTION_ARGS) |
| 188 | +{ |
| 189 | + PGARRAY *pnew = ShrinkPGArray(GetPGArray(PG_GETARG_INT32(0),0)); |
| 190 | + if(pnew) |
| 191 | + { |
| 192 | + PG_RETURN_POINTER(pnew); |
| 193 | + } |
| 194 | + else |
| 195 | + { |
| 196 | + PG_RETURN_NULL(); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +/* This function accepts an array, and returns one item for each entry in the array */ |
| 201 | +Datum int_enum(PG_FUNCTION_ARGS) |
| 202 | +{ |
| 203 | + CTX *pc; |
| 204 | + PGARRAY *p = (PGARRAY *) PG_GETARG_POINTER(0); |
| 205 | + ReturnSetInfo *rsi = (ReturnSetInfo *)fcinfo->resultinfo; |
| 206 | + |
| 207 | + if(!p) |
| 208 | + { |
| 209 | + elog(NOTICE, "No data sent\n"); |
| 210 | + return 0; |
| 211 | + } |
| 212 | + if(!rsi) |
| 213 | + { |
| 214 | + elog(ERROR, "No ReturnSetInfo sent! function must be declared returning a 'setof' integer"); |
| 215 | + PG_RETURN_NULL(); |
| 216 | + |
| 217 | + } |
| 218 | + if(!fcinfo->context) |
| 219 | + { |
| 220 | + /* Allocate a working context */ |
| 221 | + pc = (CTX *) palloc(sizeof(CTX)); |
| 222 | + |
| 223 | + if(!pc) |
| 224 | + { |
| 225 | + elog(ERROR, "CTX Alocation failed\n"); |
| 226 | + PG_RETURN_NULL(); |
| 227 | + } |
| 228 | + |
| 229 | + /* Don't copy atribute if you don't need too */ |
| 230 | + if(VARATT_IS_EXTENDED(p) ) |
| 231 | + { |
| 232 | + /* Toasted!!! */ |
| 233 | + pc->p = (PGARRAY *) PG_DETOAST_DATUM_COPY(p); |
| 234 | + pc->flags = TOASTED; |
| 235 | + if(!pc->p) |
| 236 | + { |
| 237 | + elog(ERROR, "Error in toaster!!! no detoasting\n"); |
| 238 | + PG_RETURN_NULL(); |
| 239 | + } |
| 240 | + } |
| 241 | + else |
| 242 | + { |
| 243 | + /* Untoasted */ |
| 244 | + pc->p = p; |
| 245 | + pc->flags = 0; |
| 246 | + } |
| 247 | + fcinfo->context = (Node *) pc; |
| 248 | + pc->num=0; |
| 249 | + } |
| 250 | + else /* use an existing one */ |
| 251 | + { |
| 252 | + pc = (CTX *) fcinfo->context; |
| 253 | + } |
| 254 | + /* Are we done yet? */ |
| 255 | + if(pc->num >= pc->p->items) |
| 256 | + { |
| 257 | + /* We are done */ |
| 258 | + if(pc->flags & TOASTED) |
| 259 | + pfree(pc->p); |
| 260 | + pfree(fcinfo->context); |
| 261 | + fcinfo->context = NULL; |
| 262 | + rsi->isDone = ExprEndResult ; |
| 263 | + } |
| 264 | + else /* nope, return the next value */ |
| 265 | + { |
| 266 | + int val = pc->p->array[pc->num++]; |
| 267 | + rsi->isDone = ExprMultipleResult; |
| 268 | + PG_RETURN_INT32(val); |
| 269 | + } |
| 270 | + PG_RETURN_NULL(); |
| 271 | +} |
0 commit comments