Code

CData: Added support for a type modifier.
authorSebastian Harl <sh@tokkee.org>
Sun, 29 Apr 2012 18:49:12 +0000 (20:49 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 29 Apr 2012 18:49:12 +0000 (20:49 +0200)
The type modifier may be any of 'MIN', 'AVG', 'MAX', specifying the
consolidation function to be used.

src/cdata.c
src/postrr.h.in
src/postrr.sql.in

index 076b037bbe903d4c73b43528018b21288b6c1486..432aba359a274df1b1489cc4a9cb3cd3a9557f5e 100644 (file)
 #include "postrr.h"
 
 #include <errno.h>
+#include <string.h>
 
 #include <postgres.h>
 #include <fmgr.h>
 
 /* Postgres utilities */
+#include <catalog/pg_type.h>
 #include <utils/array.h>
 
+enum {
+       CF_AVG = 0,
+       CF_MIN = 1,
+       CF_MAX = 2
+};
+
+#define CF_TO_STR(cf) \
+       (((cf) == CF_AVG) \
+               ? "AVG" \
+               : ((cf) == CF_MIN) \
+                       ? "MIN" \
+                       : ((cf) == CF_MAX) \
+                               ? "MAX" : "UNKNOWN")
+
 /*
  * data type
  */
@@ -176,5 +192,109 @@ cdata_out(PG_FUNCTION_ARGS)
        PG_RETURN_CSTRING(result);
 } /* cdata_out */
 
+Datum
+cdata_typmodin(PG_FUNCTION_ARGS)
+{
+       ArrayType *tm_array;
+
+       Datum *elem_values;
+       int    n;
+       char  *cf_str;
+       int32  typmod = CF_AVG;
+
+       if (PG_NARGS() != 1)
+               ereport(ERROR, (
+                                       errmsg("cdata_typmodin() expects one argument"),
+                                       errhint("Usage: cdata_typmodin(array)")
+                               ));
+
+       tm_array = PG_GETARG_ARRAYTYPE_P(0);
+
+       if (ARR_ELEMTYPE(tm_array) != CSTRINGOID)
+               ereport(ERROR, (
+                                       errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
+                                       errmsg("typmod array must be type cstring[]")
+                               ));
+
+       if (ARR_NDIM(tm_array) != 1)
+               ereport(ERROR, (
+                                       errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+                                       errmsg("typmod array must be one-dimensional")
+                               ));
+
+       deconstruct_array(tm_array, CSTRINGOID,
+                       /* elmlen = */ -2, /* elmbyval = */ false, /* elmalign = */ 'c',
+                       &elem_values, /* nullsp = */ NULL, &n);
+
+       if (n != 1)
+               ereport(ERROR, (
+                                       errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                       errmsg("cdata typmod array must have one element")
+                               ));
+
+       cf_str = DatumGetCString(elem_values[0]);
+       if (! strcasecmp(cf_str, "AVG"))
+               typmod = CF_AVG;
+       else if (! strcasecmp(cf_str, "MIN"))
+               typmod = CF_MIN;
+       else if (! strcasecmp(cf_str, "MAX"))
+               typmod = CF_MAX;
+       else
+               ereport(ERROR, (
+                                       errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                       errmsg("invalid cdata typmod: %s", cf_str)
+                               ));
+
+       PG_RETURN_INT32(typmod);
+} /* cdata_typmodin */
+
+Datum
+cdata_typmodout(PG_FUNCTION_ARGS)
+{
+       int32 typmod;
+       char  tm_str[32];
+       char *result;
+
+       if (PG_NARGS() != 1)
+               ereport(ERROR, (
+                                       errmsg("cdata_typmodout() expects one argument"),
+                                       errhint("Usage: cdata_typmodout(typmod)")
+                               ));
+
+       typmod = PG_GETARG_INT32(0);
+       snprintf(tm_str, sizeof(tm_str), "('%s')", CF_TO_STR(typmod));
+       tm_str[sizeof(tm_str) - 1] = '\0';
+       result = pstrdup(tm_str);
+       PG_RETURN_CSTRING(result);
+} /* cdata_typmodout */
+
+Datum
+cdata_to_cdata(PG_FUNCTION_ARGS)
+{
+       cdata_t *data;
+       int32 typmod;
+
+       if (PG_NARGS() != 3)
+               ereport(ERROR, (
+                                       errmsg("cdata_to_cdata() "
+                                               "expects three arguments"),
+                                       errhint("Usage: cdata_to_cdata"
+                                               "(cdata, typmod, is_explicit)")
+                               ));
+
+       data   = PG_GETARG_CDATA_P(0);
+       typmod = PG_GETARG_INT32(1);
+
+       if ((data->cf >= 0) && (data->cf != typmod) && (data->val_num > 1))
+               ereport(ERROR, (
+                                       errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                       errmsg("invalid cast: cannot cast cdata "
+                                               "with different typmod (yet)")
+                               ));
+
+       data->cf = typmod;
+       PG_RETURN_CDATA_P(data);
+} /* cdata_to_cdata */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index b2c9ad2483ab65a4a365c1aa86a05c7fed58665e..95582db6eaf3a7d22f2f311f0fb2fb0cef1cf0d6 100644 (file)
@@ -120,6 +120,14 @@ Datum
 cdata_in(PG_FUNCTION_ARGS);
 Datum
 cdata_out(PG_FUNCTION_ARGS);
+Datum
+cdata_typmodin(PG_FUNCTION_ARGS);
+Datum
+cdata_typmodout(PG_FUNCTION_ARGS);
+
+/* casts */
+Datum
+cdata_to_cdata(PG_FUNCTION_ARGS);
 
 #endif /* ! POSTRR_H */
 
index 22ce15ca3eaa2affab83f5188a30a25244fa63b2..cf24fced1003d0d4f57d92165c5a19e37ad664d9 100644 (file)
@@ -219,14 +219,35 @@ CREATE OR REPLACE FUNCTION CData_out(CData)
        AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_out'
        LANGUAGE 'C' IMMUTABLE STRICT;
 
+CREATE OR REPLACE FUNCTION CData_typmodin(cstring[])
+       RETURNS integer
+       AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_typmodin'
+       LANGUAGE 'C' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION CData_typmodout(integer)
+       RETURNS cstring
+       AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_typmodout'
+       LANGUAGE 'C' IMMUTABLE STRICT;
+
 CREATE TYPE CData (
        INTERNALLENGTH = 24,
        INPUT          = CData_in,
        OUTPUT         = CData_out,
+       TYPMOD_IN      = CData_typmodin,
+       TYPMOD_OUT     = CData_typmodout,
        ALIGNMENT      = double,
        STORAGE        = plain
 );
 
+CREATE OR REPLACE FUNCTION CData(cdata, integer, boolean)
+       RETURNS cdata
+       AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_to_cdata'
+       LANGUAGE 'C' IMMUTABLE STRICT;
+
+CREATE CAST (cdata AS cdata)
+       WITH FUNCTION CData(cdata, integer, boolean)
+       AS IMPLICIT;
+
 COMMIT;
 
 SET client_min_messages TO DEFAULT;