* This file is the property of the Digital Music Network (DMN).
* It is being made available to users of the PostgreSQL system
* under the BSD license.
- *
- * NOTE: This module requires sizeof(void *) to be the same as sizeof(int)
*/
#include "postgres.h"
#include "utils/lsyscache.h"
-/* Uncomment this define if you are compiling for postgres 7.2.x */
-/* #define PG_7_2 */
-
/* This is actually a postgres version of a one dimensional array */
typedef struct
} CTX;
#define TOASTED 1
-#define START_NUM 8
-#define PGARRAY_SIZE(n) (sizeof(PGARRAY) + ((n-1)*sizeof(int4)))
+#define START_NUM 8 /* initial size of arrays */
+#define PGARRAY_SIZE(n) (sizeof(PGARRAY) + (((n)-1)*sizeof(int4)))
-static PGARRAY *GetPGArray(int4 state, int fAdd);
-static PGARRAY *ShrinkPGArray(PGARRAY * p);
+static PGARRAY *GetPGArray(PGARRAY *p, int fAdd);
+static PGARRAY *ShrinkPGArray(PGARRAY *p);
Datum int_agg_state(PG_FUNCTION_ARGS);
-Datum int_agg_final_count(PG_FUNCTION_ARGS);
Datum int_agg_final_array(PG_FUNCTION_ARGS);
Datum int_enum(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(int_agg_state);
-PG_FUNCTION_INFO_V1(int_agg_final_count);
PG_FUNCTION_INFO_V1(int_agg_final_array);
PG_FUNCTION_INFO_V1(int_enum);
* PortalContext isn't really right, but it's close enough.
*/
static PGARRAY *
-GetPGArray(int4 state, int fAdd)
+GetPGArray(PGARRAY *p, int fAdd)
{
- PGARRAY *p = (PGARRAY *) state;
-
- if (!state)
+ if (!p)
{
/* New array */
int cb = PGARRAY_SIZE(START_NUM);
p->a.size = cb;
p->a.ndim = 0;
p->a.flags = 0;
-#ifndef PG_7_2
p->a.elemtype = INT4OID;
-#endif
p->items = 0;
p->lower = START_NUM;
}
}
/* Shrinks the array to its actual size and moves it into the standard
- * memory allocation context, frees working memory */
+ * memory allocation context, frees working memory
+ */
static PGARRAY *
ShrinkPGArray(PGARRAY * p)
{
pnew->a.size = cb;
pnew->a.ndim = 1;
pnew->a.flags = 0;
-#ifndef PG_7_2
pnew->a.elemtype = INT4OID;
-#endif
- pnew->lower = 0;
+ pnew->lower = 1;
pfree(p);
}
Datum
int_agg_state(PG_FUNCTION_ARGS)
{
- int4 state = PG_GETARG_INT32(0);
- int4 value = PG_GETARG_INT32(1);
+ PGARRAY *state;
+ PGARRAY *p;
- PGARRAY *p = GetPGArray(state, 1);
+ /*
+ * We can keep a pointer in the datum even though nodeAgg thinks it's
+ * an int4. Note we assume the initial state of int4 zero will look
+ * like a null pointer.
+ */
+ state = (PGARRAY *) PG_GETARG_POINTER(0);
+ p = GetPGArray(state, 1);
- if (!p)
- /* internal error */
- elog(ERROR, "no aggregate storage");
- else if (p->items >= p->lower)
- /* internal error */
- elog(ERROR, "aggregate storage too small");
- else
- p->array[p->items++] = value;
- PG_RETURN_INT32(p);
+ if (!PG_ARGISNULL(1))
+ {
+ int4 value = PG_GETARG_INT32(1);
+
+ if (!p) /* internal error */
+ elog(ERROR, "no aggregate storage");
+ else if (p->items >= p->lower) /* internal error */
+ elog(ERROR, "aggregate storage too small");
+ else
+ p->array[p->items++] = value;
+ }
+ PG_RETURN_POINTER(p);
}
-/* This is the final function used for the integer aggregator. It returns all the integers
- * collected as a one dimensional integer array */
+/* This is the final function used for the integer aggregator. It returns all
+ * the integers collected as a one dimensional integer array
+ */
Datum
int_agg_final_array(PG_FUNCTION_ARGS)
{
- PGARRAY *pnew = ShrinkPGArray(GetPGArray(PG_GETARG_INT32(0), 0));
+ PGARRAY *state = (PGARRAY *) PG_GETARG_POINTER(0);
+ PGARRAY *pnew = ShrinkPGArray(GetPGArray(state, 0));
if (pnew)
PG_RETURN_POINTER(pnew);
PG_RETURN_NULL();
}
- if (!fcinfo->context)
+ if (!fcinfo->flinfo->fn_extra)
{
- /* Allocate a working context */
+ /* Allocate working state */
+ MemoryContext oldcontext;
+
+ oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
+
pc = (CTX *) palloc(sizeof(CTX));
- /* Don't copy attribute if you don't need too */
+ /* Don't copy attribute if you don't need to */
if (VARATT_IS_EXTENDED(p))
{
/* Toasted!!! */
pc->p = (PGARRAY *) PG_DETOAST_DATUM_COPY(p);
pc->flags = TOASTED;
- if (!pc->p)
- {
- /* internal error */
- elog(ERROR, "error in toaster; not detoasting");
- PG_RETURN_NULL();
- }
}
else
{
pc->p = p;
pc->flags = 0;
}
- fcinfo->context = (Node *) pc;
+ /* Now that we have a detoasted array, verify dimensions */
+ if (pc->p->a.ndim != 1)
+ elog(ERROR, "int_enum only accepts 1-D arrays");
pc->num = 0;
+ fcinfo->flinfo->fn_extra = (void *) pc;
+ MemoryContextSwitchTo(oldcontext);
}
- else
- /* use an existing one */
- pc = (CTX *) fcinfo->context;
+ else /* use existing working state */
+ pc = (CTX *) fcinfo->flinfo->fn_extra;
+
/* Are we done yet? */
if (pc->num >= pc->p->items)
{
/* We are done */
if (pc->flags & TOASTED)
pfree(pc->p);
- pfree(fcinfo->context);
- fcinfo->context = NULL;
+ pfree(pc);
+ fcinfo->flinfo->fn_extra = NULL;
rsi->isDone = ExprEndResult;
}
else
- /* nope, return the next value */
{
+ /* nope, return the next value */
int val = pc->p->array[pc->num++];
rsi->isDone = ExprMultipleResult;