Code

CData: Added casts from integer and numeric to cdata.
[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);
83 /*
84  * public API
85  */
87 Datum
88 cdata_validate(PG_FUNCTION_ARGS)
89 {
90         char   type_info[1024];
91         char  *result;
92         size_t req_len;
93         size_t len;
95         if (PG_NARGS() != 1)
96                 ereport(ERROR, (
97                                         errmsg("cdata_validate() expect one argument"),
98                                         errhint("Usage cdata_validate(expected_size)")
99                                 ));
101         req_len = (size_t)PG_GETARG_UINT32(0);
102         len = sizeof(cdata_t);
104         if (req_len != len)
105                 ereport(ERROR, (
106                                         errmsg("length of the cdata type "
107                                                 "does not match the expected length"),
108                                         errhint("Please report a bug against PostRR")
109                                 ));
111         snprintf(type_info, sizeof(type_info),
112                         "cdata validated successfully; type length = %zu", len);
113         type_info[sizeof(type_info) - 1] = '\0';
115         result = pstrdup(type_info);
116         PG_RETURN_CSTRING(result);
117 } /* cdata_validate */
119 Datum
120 cdata_in(PG_FUNCTION_ARGS)
122         cdata_t *data;
123         int32 typmod;
125         char *val_str, *orig;
126         char *endptr = NULL;
128         if (PG_NARGS() != 3)
129                 ereport(ERROR, (
130                                         errmsg("cdata_in() expects three arguments"),
131                                         errhint("Usage: cdata_in(col_name, oid, typmod)")
132                                 ));
134         data = (cdata_t *)palloc0(sizeof(*data));
136         val_str = PG_GETARG_CSTRING(0);
137         typmod  = PG_GETARG_INT32(2);
139         orig = val_str;
140         while ((*val_str != '\0') && isspace((int)*val_str))
141                 ++val_str;
143         if (*val_str == '\0')
144                 ereport(ERROR, (
145                                         errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
146                                         errmsg("invalid input syntax for cdata: \"%s\"", orig)
147                                 ));
149         errno = 0;
150         data->value   = strtod(val_str, &endptr);
151         data->val_num = 1;
153         if ((endptr == val_str) || errno)
154                 ereport(ERROR, (
155                                         errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
156                                         errmsg("invalid input syntax for cdata: \"%s\"", orig)
157                                 ));
159         while ((*endptr != '\0') && isspace((int)*endptr))
160                 ereport(ERROR, (
161                                         errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
162                                         errmsg("invalid input syntax for cdata: \"%s\"", orig)
163                                 ));
165         if (typmod > 0)
166                 data->cf = typmod;
167         else
168                 data->cf = 0;
170         PG_RETURN_CDATA_P(data);
171 } /* cdata_in */
173 Datum
174 cdata_out(PG_FUNCTION_ARGS)
176         cdata_t *data;
178         char  cd_str[1024];
179         char *result;
181         if (PG_NARGS() != 1)
182                 ereport(ERROR, (
183                                         errmsg("cdata_out() expects one argument"),
184                                         errhint("Usage: cdata_out(cdata)")
185                                 ));
187         data = PG_GETARG_CDATA_P(0);
189         snprintf(cd_str, sizeof(cd_str), "%g (U:%i/%i)",
190                         data->value, data->undef_num, data->val_num);
192         result = pstrdup(cd_str);
193         PG_RETURN_CSTRING(result);
194 } /* cdata_out */
196 Datum
197 cdata_typmodin(PG_FUNCTION_ARGS)
199         ArrayType *tm_array;
201         Datum *elem_values;
202         int    n;
203         char  *cf_str;
204         int32  typmod = CF_AVG;
206         if (PG_NARGS() != 1)
207                 ereport(ERROR, (
208                                         errmsg("cdata_typmodin() expects one argument"),
209                                         errhint("Usage: cdata_typmodin(array)")
210                                 ));
212         tm_array = PG_GETARG_ARRAYTYPE_P(0);
214         if (ARR_ELEMTYPE(tm_array) != CSTRINGOID)
215                 ereport(ERROR, (
216                                         errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
217                                         errmsg("typmod array must be type cstring[]")
218                                 ));
220         if (ARR_NDIM(tm_array) != 1)
221                 ereport(ERROR, (
222                                         errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
223                                         errmsg("typmod array must be one-dimensional")
224                                 ));
226         deconstruct_array(tm_array, CSTRINGOID,
227                         /* elmlen = */ -2, /* elmbyval = */ false, /* elmalign = */ 'c',
228                         &elem_values, /* nullsp = */ NULL, &n);
230         if (n != 1)
231                 ereport(ERROR, (
232                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
233                                         errmsg("cdata typmod array must have one element")
234                                 ));
236         cf_str = DatumGetCString(elem_values[0]);
237         if (! strcasecmp(cf_str, "AVG"))
238                 typmod = CF_AVG;
239         else if (! strcasecmp(cf_str, "MIN"))
240                 typmod = CF_MIN;
241         else if (! strcasecmp(cf_str, "MAX"))
242                 typmod = CF_MAX;
243         else
244                 ereport(ERROR, (
245                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
246                                         errmsg("invalid cdata typmod: %s", cf_str)
247                                 ));
249         PG_RETURN_INT32(typmod);
250 } /* cdata_typmodin */
252 Datum
253 cdata_typmodout(PG_FUNCTION_ARGS)
255         int32 typmod;
256         char  tm_str[32];
257         char *result;
259         if (PG_NARGS() != 1)
260                 ereport(ERROR, (
261                                         errmsg("cdata_typmodout() expects one argument"),
262                                         errhint("Usage: cdata_typmodout(typmod)")
263                                 ));
265         typmod = PG_GETARG_INT32(0);
266         snprintf(tm_str, sizeof(tm_str), "('%s')", CF_TO_STR(typmod));
267         tm_str[sizeof(tm_str) - 1] = '\0';
268         result = pstrdup(tm_str);
269         PG_RETURN_CSTRING(result);
270 } /* cdata_typmodout */
272 Datum
273 cdata_to_cdata(PG_FUNCTION_ARGS)
275         cdata_t *data;
276         int32 typmod;
278         if (PG_NARGS() != 3)
279                 ereport(ERROR, (
280                                         errmsg("cdata_to_cdata() "
281                                                 "expects three arguments"),
282                                         errhint("Usage: cdata_to_cdata"
283                                                 "(cdata, typmod, is_explicit)")
284                                 ));
286         data   = PG_GETARG_CDATA_P(0);
287         typmod = PG_GETARG_INT32(1);
289         if ((data->cf >= 0) && (data->cf != typmod) && (data->val_num > 1))
290                 ereport(ERROR, (
291                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE),
292                                         errmsg("invalid cast: cannot cast cdata "
293                                                 "with different typmod (yet)")
294                                 ));
296         data->cf = typmod;
297         PG_RETURN_CDATA_P(data);
298 } /* cdata_to_cdata */
300 Datum
301 int32_to_cdata(PG_FUNCTION_ARGS)
303         int32 i_val;
304         int32 typmod;
306         cdata_t *data;
308         if (PG_NARGS() != 3)
309                 ereport(ERROR, (
310                                         errmsg("int32_to_cdata() expects three arguments"),
311                                         errhint("Usage: int32_to_cdata"
312                                                 "(integer, typmod, is_explicit)")
313                                 ));
315         i_val  = PG_GETARG_INT32(0);
316         typmod = PG_GETARG_INT32(1);
318         data = (cdata_t *)palloc0(sizeof(*data));
320         data->value     = (float8)i_val;
321         data->undef_num = 0;
322         data->val_num   = 1;
324         if (typmod >= 0)
325                 data->cf = typmod;
326         else
327                 data->cf = CF_AVG;
329         PG_RETURN_CDATA_P(data);
330 } /* int32_to_cdata */
332 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */