Code

CData: added a cast to double precision.
[postrr.git] / src / cdata.c
1 /*
2  * PostRR - src/cdata.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * A PostgreSQL data-type providing consolidated data points.
30  */
32 #include "postrr.h"
34 #include <errno.h>
35 #include <string.h>
37 #include <postgres.h>
38 #include <fmgr.h>
40 /* Postgres utilities */
41 #include <catalog/pg_type.h>
42 #include <utils/array.h>
44 enum {
45         CF_AVG = 0,
46         CF_MIN = 1,
47         CF_MAX = 2
48 };
50 #define CF_TO_STR(cf) \
51         (((cf) == CF_AVG) \
52                 ? "AVG" \
53                 : ((cf) == CF_MIN) \
54                         ? "MIN" \
55                         : ((cf) == CF_MAX) \
56                                 ? "MAX" : "UNKNOWN")
58 /*
59  * data type
60  */
62 struct cdata {
63         float8 value;
64         int32 undef_num;
65         int32 val_num;
66         int32 cf;
67 };
69 /*
70  * prototypes for PostgreSQL functions
71  */
73 PG_FUNCTION_INFO_V1(cdata_validate);
75 PG_FUNCTION_INFO_V1(cdata_in);
76 PG_FUNCTION_INFO_V1(cdata_out);
77 PG_FUNCTION_INFO_V1(cdata_typmodin);
78 PG_FUNCTION_INFO_V1(cdata_typmodout);
80 PG_FUNCTION_INFO_V1(cdata_to_cdata);
81 PG_FUNCTION_INFO_V1(int32_to_cdata);
82 PG_FUNCTION_INFO_V1(cdata_to_float8);
84 PG_FUNCTION_INFO_V1(cdata_update);
86 /*
87  * public API
88  */
90 Datum
91 cdata_validate(PG_FUNCTION_ARGS)
92 {
93         char   type_info[1024];
94         char  *result;
95         size_t req_len;
96         size_t len;
98         if (PG_NARGS() != 1)
99                 ereport(ERROR, (
100                                         errmsg("cdata_validate() expect one argument"),
101                                         errhint("Usage cdata_validate(expected_size)")
102                                 ));
104         req_len = (size_t)PG_GETARG_UINT32(0);
105         len = sizeof(cdata_t);
107         if (req_len != len)
108                 ereport(ERROR, (
109                                         errmsg("length of the cdata type "
110                                                 "does not match the expected length"),
111                                         errhint("Please report a bug against PostRR")
112                                 ));
114         snprintf(type_info, sizeof(type_info),
115                         "cdata validated successfully; type length = %zu", len);
116         type_info[sizeof(type_info) - 1] = '\0';
118         result = pstrdup(type_info);
119         PG_RETURN_CSTRING(result);
120 } /* cdata_validate */
122 Datum
123 cdata_in(PG_FUNCTION_ARGS)
125         cdata_t *data;
126         int32 typmod;
128         char *val_str, *orig;
129         char *endptr = NULL;
131         if (PG_NARGS() != 3)
132                 ereport(ERROR, (
133                                         errmsg("cdata_in() expects three arguments"),
134                                         errhint("Usage: cdata_in(col_name, oid, typmod)")
135                                 ));
137         data = (cdata_t *)palloc0(sizeof(*data));
139         val_str = PG_GETARG_CSTRING(0);
140         typmod  = PG_GETARG_INT32(2);
142         orig = val_str;
143         while ((*val_str != '\0') && isspace((int)*val_str))
144                 ++val_str;
146         if (*val_str == '\0')
147                 ereport(ERROR, (
148                                         errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
149                                         errmsg("invalid input syntax for cdata: \"%s\"", orig)
150                                 ));
152         errno = 0;
153         data->value   = strtod(val_str, &endptr);
154         data->val_num = 1;
156         if ((endptr == val_str) || errno)
157                 ereport(ERROR, (
158                                         errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
159                                         errmsg("invalid input syntax for cdata: \"%s\"", orig)
160                                 ));
162         while ((*endptr != '\0') && isspace((int)*endptr))
163                 ereport(ERROR, (
164                                         errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
165                                         errmsg("invalid input syntax for cdata: \"%s\"", orig)
166                                 ));
168         if (typmod > 0)
169                 data->cf = typmod;
170         else
171                 data->cf = 0;
173         PG_RETURN_CDATA_P(data);
174 } /* cdata_in */
176 Datum
177 cdata_out(PG_FUNCTION_ARGS)
179         cdata_t *data;
181         char  cd_str[1024];
182         char *result;
184         if (PG_NARGS() != 1)
185                 ereport(ERROR, (
186                                         errmsg("cdata_out() expects one argument"),
187                                         errhint("Usage: cdata_out(cdata)")
188                                 ));
190         data = PG_GETARG_CDATA_P(0);
192         snprintf(cd_str, sizeof(cd_str), "%g (U:%i/%i)",
193                         data->value, data->undef_num, data->val_num);
195         result = pstrdup(cd_str);
196         PG_RETURN_CSTRING(result);
197 } /* cdata_out */
199 Datum
200 cdata_typmodin(PG_FUNCTION_ARGS)
202         ArrayType *tm_array;
204         Datum *elem_values;
205         int    n;
206         char  *cf_str;
207         int32  typmod = CF_AVG;
209         if (PG_NARGS() != 1)
210                 ereport(ERROR, (
211                                         errmsg("cdata_typmodin() expects one argument"),
212                                         errhint("Usage: cdata_typmodin(array)")
213                                 ));
215         tm_array = PG_GETARG_ARRAYTYPE_P(0);
217         if (ARR_ELEMTYPE(tm_array) != CSTRINGOID)
218                 ereport(ERROR, (
219                                         errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
220                                         errmsg("typmod array must be type cstring[]")
221                                 ));
223         if (ARR_NDIM(tm_array) != 1)
224                 ereport(ERROR, (
225                                         errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
226                                         errmsg("typmod array must be one-dimensional")
227                                 ));
229         deconstruct_array(tm_array, CSTRINGOID,
230                         /* elmlen = */ -2, /* elmbyval = */ false, /* elmalign = */ 'c',
231                         &elem_values, /* nullsp = */ NULL, &n);
233         if (n != 1)
234                 ereport(ERROR, (
235                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
236                                         errmsg("cdata typmod array must have one element")
237                                 ));
239         cf_str = DatumGetCString(elem_values[0]);
240         if (! strcasecmp(cf_str, "AVG"))
241                 typmod = CF_AVG;
242         else if (! strcasecmp(cf_str, "MIN"))
243                 typmod = CF_MIN;
244         else if (! strcasecmp(cf_str, "MAX"))
245                 typmod = CF_MAX;
246         else
247                 ereport(ERROR, (
248                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
249                                         errmsg("invalid cdata typmod: %s", cf_str)
250                                 ));
252         PG_RETURN_INT32(typmod);
253 } /* cdata_typmodin */
255 Datum
256 cdata_typmodout(PG_FUNCTION_ARGS)
258         int32 typmod;
259         char  tm_str[32];
260         char *result;
262         if (PG_NARGS() != 1)
263                 ereport(ERROR, (
264                                         errmsg("cdata_typmodout() expects one argument"),
265                                         errhint("Usage: cdata_typmodout(typmod)")
266                                 ));
268         typmod = PG_GETARG_INT32(0);
269         snprintf(tm_str, sizeof(tm_str), "('%s')", CF_TO_STR(typmod));
270         tm_str[sizeof(tm_str) - 1] = '\0';
271         result = pstrdup(tm_str);
272         PG_RETURN_CSTRING(result);
273 } /* cdata_typmodout */
275 Datum
276 cdata_to_cdata(PG_FUNCTION_ARGS)
278         cdata_t *data;
279         int32 typmod;
281         if (PG_NARGS() != 3)
282                 ereport(ERROR, (
283                                         errmsg("cdata_to_cdata() "
284                                                 "expects three arguments"),
285                                         errhint("Usage: cdata_to_cdata"
286                                                 "(cdata, typmod, is_explicit)")
287                                 ));
289         data   = PG_GETARG_CDATA_P(0);
290         typmod = PG_GETARG_INT32(1);
292         if ((data->cf >= 0) && (data->cf != typmod) && (data->val_num > 1))
293                 ereport(ERROR, (
294                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
295                                         errmsg("invalid cast: cannot cast cdata "
296                                                 "with different typmod (yet)")
297                                 ));
299         data->cf = typmod;
300         PG_RETURN_CDATA_P(data);
301 } /* cdata_to_cdata */
303 Datum
304 int32_to_cdata(PG_FUNCTION_ARGS)
306         int32 i_val;
307         int32 typmod;
309         cdata_t *data;
311         if (PG_NARGS() != 3)
312                 ereport(ERROR, (
313                                         errmsg("int32_to_cdata() expects three arguments"),
314                                         errhint("Usage: int32_to_cdata"
315                                                 "(integer, typmod, is_explicit)")
316                                 ));
318         i_val  = PG_GETARG_INT32(0);
319         typmod = PG_GETARG_INT32(1);
321         data = (cdata_t *)palloc0(sizeof(*data));
323         data->value     = (float8)i_val;
324         data->undef_num = 0;
325         data->val_num   = 1;
327         if (typmod >= 0)
328                 data->cf = typmod;
329         else
330                 data->cf = CF_AVG;
332         PG_RETURN_CDATA_P(data);
333 } /* int32_to_cdata */
335 Datum
336 cdata_to_float8(PG_FUNCTION_ARGS)
338         cdata_t *data;
340         if (PG_NARGS() != 1)
341                 ereport(ERROR, (
342                                         errmsg("cdata_to_float8() expects one argument"),
343                                         errhint("Usage: cdata_to_float8(cdata)")
344                                 ));
346         data = PG_GETARG_CDATA_P(0);
347         PG_RETURN_FLOAT8(data->value);
348 } /* cdata_to_float8 */
350 Datum
351 cdata_update(PG_FUNCTION_ARGS)
353         cdata_t *data;
354         cdata_t *update;
356         if (PG_NARGS() != 2)
357                 ereport(ERROR, (
358                                         errmsg("cdata_update() expects two arguments"),
359                                         errhint("Usage: cdata_update(cdata, cdata)")
360                                 ));
362         data   = PG_GETARG_CDATA_P(0);
363         update = PG_GETARG_CDATA_P(1);
365         if ((data->cf != update->cf) && (update->val_num > 1))
366                 ereport(ERROR, (
367                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
368                                         errmsg("invalid update value: incompatible "
369                                                 "consolidation function")
370                                 ));
372         switch (data->cf) {
373                 case CF_AVG:
374                         data->value = (data->value * (data->val_num - data->undef_num))
375                                 + (update->value * (update->val_num - update->undef_num));
376                         data->value /= (data->val_num - data->undef_num)
377                                         +  (update->val_num - update->undef_num);
378                         break;
379                 case CF_MIN:
380                         data->value = (data->value <= update->value)
381                                 ? data->value : update->value;
382                         break;
383                 case CF_MAX:
384                         data->value = (data->value >= update->value)
385                                 ? data->value : update->value;
386                         break;
387                 default:
388                         ereport(ERROR, (
389                                                 errcode(ERRCODE_DATA_CORRUPTED),
390                                                 errmsg("unknown consolidation function %d",
391                                                         data->cf)
392                                         ));
393                         break;
394         }
396         data->undef_num += update->undef_num;
397         data->val_num   += update->val_num;
398         PG_RETURN_CDATA_P(data);
399 } /* cdata_update */
401 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */