int_array_enum function should be using fcinfo->flinfo->fn_extra for
authorTom Lane <[email protected]>
Mon, 15 Aug 2005 19:05:30 +0000 (19:05 +0000)
committerTom Lane <[email protected]>
Mon, 15 Aug 2005 19:05:30 +0000 (19:05 +0000)
working state, not fcinfo->context.  Silly oversight on my part in last
go-round of fixes.

contrib/intagg/int_aggregate.c

index 78b1a173873e4a0d708e0a093ebbf5aa30fbc878..638afddb5ae479412fc230c95f591dcf05a56f7f 100644 (file)
@@ -11,8 +11,6 @@
  * 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"
 
@@ -36,9 +34,6 @@
 #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
@@ -58,19 +53,17 @@ typedef struct callContext
 }      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);
 
@@ -81,11 +74,9 @@ 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);
@@ -94,9 +85,7 @@ GetPGArray(int4 state, int fAdd)
                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;
        }
@@ -118,7 +107,8 @@ GetPGArray(int4 state, int fAdd)
 }
 
 /* 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)
 {
@@ -139,10 +129,8 @@ 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);
        }
@@ -153,28 +141,39 @@ ShrinkPGArray(PGARRAY * 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);
@@ -201,23 +200,21 @@ int_enum(PG_FUNCTION_ARGS)
                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
                {
@@ -225,25 +222,29 @@ int_enum(PG_FUNCTION_ARGS)
                        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;