]> granicus.if.org Git - postgresql/blob - src/test/regress/regress.c
Adjust comments previously moved to column 1 by pgident.
[postgresql] / src / test / regress / regress.c
1 /*
2  * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.61 2004/10/07 15:21:58 momjian Exp $
3  */
4
5 #include "postgres.h"
6
7 #include <float.h>                              /* faked on sunos */
8
9 #include "utils/geo_decls.h"    /* includes <math.h> */
10 #include "executor/executor.h"  /* For GetAttributeByName */
11 #include "commands/sequence.h"  /* for nextval() */
12
13 #define P_MAXDIG 12
14 #define LDELIM                  '('
15 #define RDELIM                  ')'
16 #define DELIM                   ','
17
18 extern Datum regress_dist_ptpath(PG_FUNCTION_ARGS);
19 extern Datum regress_path_dist(PG_FUNCTION_ARGS);
20 extern PATH *poly2path(POLYGON *poly);
21 extern Datum interpt_pp(PG_FUNCTION_ARGS);
22 extern void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
23 extern Datum overpaid(PG_FUNCTION_ARGS);
24 extern Datum boxarea(PG_FUNCTION_ARGS);
25 extern char *reverse_name(char *string);
26 extern int      oldstyle_length(int n, text *t);
27 extern Datum int44in(PG_FUNCTION_ARGS);
28 extern Datum int44out(PG_FUNCTION_ARGS);
29
30 /*
31  * Distance from a point to a path
32  */
33 PG_FUNCTION_INFO_V1(regress_dist_ptpath);
34
35 Datum
36 regress_dist_ptpath(PG_FUNCTION_ARGS)
37 {
38         Point      *pt = PG_GETARG_POINT_P(0);
39         PATH       *path = PG_GETARG_PATH_P(1);
40         float8          result = 0.0;   /* keep compiler quiet */
41         float8          tmp;
42         int                     i;
43         LSEG            lseg;
44
45         switch (path->npts)
46         {
47                 case 0:
48                         PG_RETURN_NULL();
49                 case 1:
50                         result = point_dt(pt, &path->p[0]);
51                         break;
52                 default:
53
54                         /*
55                          * the distance from a point to a path is the smallest
56                          * distance from the point to any of its constituent segments.
57                          */
58                         Assert(path->npts > 1);
59                         for (i = 0; i < path->npts - 1; ++i)
60                         {
61                                 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
62                                 tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps,
63                                                                                                           PointPGetDatum(pt),
64                                                                                                   LsegPGetDatum(&lseg)));
65                                 if (i == 0 || tmp < result)
66                                         result = tmp;
67                         }
68                         break;
69         }
70         PG_RETURN_FLOAT8(result);
71 }
72
73 /*
74  * this essentially does a cartesian product of the lsegs in the
75  * two paths, and finds the min distance between any two lsegs
76  */
77 PG_FUNCTION_INFO_V1(regress_path_dist);
78
79 Datum
80 regress_path_dist(PG_FUNCTION_ARGS)
81 {
82         PATH       *p1 = PG_GETARG_PATH_P(0);
83         PATH       *p2 = PG_GETARG_PATH_P(1);
84         bool            have_min = false;
85         float8          min = 0.0;              /* initialize to keep compiler quiet */
86         float8          tmp;
87         int                     i,
88                                 j;
89         LSEG            seg1,
90                                 seg2;
91
92         for (i = 0; i < p1->npts - 1; i++)
93         {
94                 for (j = 0; j < p2->npts - 1; j++)
95                 {
96                         regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
97                         regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
98
99                         tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
100                                                                                                          LsegPGetDatum(&seg1),
101                                                                                                   LsegPGetDatum(&seg2)));
102                         if (!have_min || tmp < min)
103                         {
104                                 min = tmp;
105                                 have_min = true;
106                         }
107                 }
108         }
109
110         if (!have_min)
111                 PG_RETURN_NULL();
112
113         PG_RETURN_FLOAT8(min);
114 }
115
116 PATH *
117 poly2path(POLYGON *poly)
118 {
119         int                     i;
120         char       *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
121         char            buf[2 * (P_MAXDIG) + 20];
122
123         sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
124
125         for (i = 0; i < poly->npts; i++)
126         {
127                 snprintf(buf, sizeof(buf), ",%*g,%*g",
128                                  P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
129                 strcat(output, buf);
130         }
131
132         snprintf(buf, sizeof(buf), "%c", RDELIM);
133         strcat(output, buf);
134         return DatumGetPathP(DirectFunctionCall1(path_in,
135                                                                                          CStringGetDatum(output)));
136 }
137
138 /* return the point where two paths intersect, or NULL if no intersection. */
139 PG_FUNCTION_INFO_V1(interpt_pp);
140
141 Datum
142 interpt_pp(PG_FUNCTION_ARGS)
143 {
144         PATH       *p1 = PG_GETARG_PATH_P(0);
145         PATH       *p2 = PG_GETARG_PATH_P(1);
146         int                     i,
147                                 j;
148         LSEG            seg1,
149                                 seg2;
150         bool            found;                  /* We've found the intersection */
151
152         found = false;                          /* Haven't found it yet */
153
154         for (i = 0; i < p1->npts - 1 && !found; i++)
155         {
156                 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
157                 for (j = 0; j < p2->npts - 1 && !found; j++)
158                 {
159                         regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
160                         if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
161                                                                                                  LsegPGetDatum(&seg1),
162                                                                                                  LsegPGetDatum(&seg2))))
163                                 found = true;
164                 }
165         }
166
167         if (!found)
168                 PG_RETURN_NULL();
169
170         /*
171          * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
172          * returns NULL, but that should be impossible since we know the two
173          * segments intersect.
174          */
175         PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
176                                                                                 LsegPGetDatum(&seg1),
177                                                                                 LsegPGetDatum(&seg2)));
178 }
179
180
181 /* like lseg_construct, but assume space already allocated */
182 void
183 regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
184 {
185         lseg->p[0].x = pt1->x;
186         lseg->p[0].y = pt1->y;
187         lseg->p[1].x = pt2->x;
188         lseg->p[1].y = pt2->y;
189         lseg->m = point_sl(pt1, pt2);
190 }
191
192 PG_FUNCTION_INFO_V1(overpaid);
193
194 Datum
195 overpaid(PG_FUNCTION_ARGS)
196 {
197         HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
198         bool            isnull;
199         int32           salary;
200
201         salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
202         if (isnull)
203                 PG_RETURN_NULL();
204         PG_RETURN_BOOL(salary > 699);
205 }
206
207 /* New type "widget"
208  * This used to be "circle", but I added circle to builtins,
209  *      so needed to make sure the names do not collide. - tgl 97/04/21
210  */
211
212 typedef struct
213 {
214         Point           center;
215         double          radius;
216 }       WIDGET;
217
218 WIDGET     *widget_in(char *str);
219 char       *widget_out(WIDGET * widget);
220 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
221
222 #define NARGS   3
223
224 WIDGET *
225 widget_in(char *str)
226 {
227         char       *p,
228                            *coord[NARGS],
229                                 buf2[1000];
230         int                     i;
231         WIDGET     *result;
232
233         if (str == NULL)
234                 return NULL;
235         for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
236                 if (*p == ',' || (*p == LDELIM && !i))
237                         coord[i++] = p + 1;
238         if (i < NARGS - 1)
239                 return NULL;
240         result = (WIDGET *) palloc(sizeof(WIDGET));
241         result->center.x = atof(coord[0]);
242         result->center.y = atof(coord[1]);
243         result->radius = atof(coord[2]);
244
245         snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
246                          result->center.x, result->center.y, result->radius);
247         return result;
248 }
249
250 char *
251 widget_out(WIDGET * widget)
252 {
253         char       *result;
254
255         if (widget == NULL)
256                 return NULL;
257
258         result = (char *) palloc(60);
259         sprintf(result, "(%g,%g,%g)",
260                         widget->center.x, widget->center.y, widget->radius);
261         return result;
262 }
263
264 PG_FUNCTION_INFO_V1(pt_in_widget);
265
266 Datum
267 pt_in_widget(PG_FUNCTION_ARGS)
268 {
269         Point      *point = PG_GETARG_POINT_P(0);
270         WIDGET     *widget = (WIDGET *) PG_GETARG_POINTER(1);
271
272         PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
273 }
274
275 #define ABS(X) ((X) >= 0 ? (X) : -(X))
276
277 PG_FUNCTION_INFO_V1(boxarea);
278
279 Datum
280 boxarea(PG_FUNCTION_ARGS)
281 {
282         BOX                *box = PG_GETARG_BOX_P(0);
283         double          width,
284                                 height;
285
286         width = ABS(box->high.x - box->low.x);
287         height = ABS(box->high.y - box->low.y);
288         PG_RETURN_FLOAT8(width * height);
289 }
290
291 char *
292 reverse_name(char *string)
293 {
294         int                     i;
295         int                     len;
296         char       *new_string;
297
298         new_string = palloc0(NAMEDATALEN);
299         for (i = 0; i < NAMEDATALEN && string[i]; ++i)
300                 ;
301         if (i == NAMEDATALEN || !string[i])
302                 --i;
303         len = i;
304         for (; i >= 0; --i)
305                 new_string[len - i] = string[i];
306         return new_string;
307 }
308
309 /*
310  * This rather silly function is just to test that oldstyle functions
311  * work correctly on toast-able inputs.
312  */
313 int
314 oldstyle_length(int n, text *t)
315 {
316         int                     len = 0;
317
318         if (t)
319                 len = VARSIZE(t) - VARHDRSZ;
320
321         return n + len;
322 }
323
324 #include "executor/spi.h"               /* this is what you need to work with SPI */
325 #include "commands/trigger.h"   /* -"- and triggers */
326
327 static TransactionId fd17b_xid = InvalidTransactionId;
328 static TransactionId fd17a_xid = InvalidTransactionId;
329 static int      fd17b_level = 0;
330 static int      fd17a_level = 0;
331 static bool fd17b_recursion = true;
332 static bool fd17a_recursion = true;
333 extern Datum funny_dup17(PG_FUNCTION_ARGS);
334
335 PG_FUNCTION_INFO_V1(funny_dup17);
336
337 Datum
338 funny_dup17(PG_FUNCTION_ARGS)
339 {
340         TriggerData *trigdata = (TriggerData *) fcinfo->context;
341         TransactionId *xid;
342         int                *level;
343         bool       *recursion;
344         Relation        rel;
345         TupleDesc       tupdesc;
346         HeapTuple       tuple;
347         char       *query,
348                            *fieldval,
349                            *fieldtype;
350         char       *when;
351         int                     inserted;
352         int                     selected = 0;
353         int                     ret;
354
355         if (!CALLED_AS_TRIGGER(fcinfo))
356                 elog(ERROR, "funny_dup17: not fired by trigger manager");
357
358         tuple = trigdata->tg_trigtuple;
359         rel = trigdata->tg_relation;
360         tupdesc = rel->rd_att;
361         if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
362         {
363                 xid = &fd17b_xid;
364                 level = &fd17b_level;
365                 recursion = &fd17b_recursion;
366                 when = "BEFORE";
367         }
368         else
369         {
370                 xid = &fd17a_xid;
371                 level = &fd17a_level;
372                 recursion = &fd17a_recursion;
373                 when = "AFTER ";
374         }
375
376         if (!TransactionIdIsCurrentTransactionId(*xid))
377         {
378                 *xid = GetCurrentTransactionId();
379                 *level = 0;
380                 *recursion = true;
381         }
382
383         if (*level == 17)
384         {
385                 *recursion = false;
386                 return PointerGetDatum(tuple);
387         }
388
389         if (!(*recursion))
390                 return PointerGetDatum(tuple);
391
392         (*level)++;
393
394         SPI_connect();
395
396         fieldval = SPI_getvalue(tuple, tupdesc, 1);
397         fieldtype = SPI_gettype(tupdesc, 1);
398
399         query = (char *) palloc(100 + NAMEDATALEN * 3 +
400                                                         strlen(fieldval) + strlen(fieldtype));
401
402         sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
403                         SPI_getrelname(rel), SPI_getrelname(rel),
404                         SPI_fname(tupdesc, 1),
405                         fieldval, fieldtype);
406
407         if ((ret = SPI_exec(query, 0)) < 0)
408                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
409                          when, *level, ret);
410
411         inserted = SPI_processed;
412
413         sprintf(query, "select count (*) from %s where %s = '%s'::%s",
414                         SPI_getrelname(rel),
415                         SPI_fname(tupdesc, 1),
416                         fieldval, fieldtype);
417
418         if ((ret = SPI_exec(query, 0)) < 0)
419                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
420                          when, *level, ret);
421
422         if (SPI_processed > 0)
423         {
424                 selected = DatumGetInt32(DirectFunctionCall1(int4in,
425                                                                                         CStringGetDatum(SPI_getvalue(
426                                                                                                    SPI_tuptable->vals[0],
427                                                                                                    SPI_tuptable->tupdesc,
428                                                                                                                                                  1
429                                                                                                                                         ))));
430         }
431
432         elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
433                  when, *level, inserted, selected);
434
435         SPI_finish();
436
437         (*level)--;
438
439         if (*level == 0)
440                 *xid = InvalidTransactionId;
441
442         return PointerGetDatum(tuple);
443 }
444
445 extern Datum ttdummy(PG_FUNCTION_ARGS);
446 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
447
448 #define TTDUMMY_INFINITY        999999
449
450 static void *splan = NULL;
451 static bool ttoff = false;
452
453 PG_FUNCTION_INFO_V1(ttdummy);
454
455 Datum
456 ttdummy(PG_FUNCTION_ARGS)
457 {
458         TriggerData *trigdata = (TriggerData *) fcinfo->context;
459         Trigger    *trigger;            /* to get trigger name */
460         char      **args;                       /* arguments */
461         int                     attnum[2];              /* fnumbers of start/stop columns */
462         Datum           oldon,
463                                 oldoff;
464         Datum           newon,
465                                 newoff;
466         Datum      *cvals;                      /* column values */
467         char       *cnulls;                     /* column nulls */
468         char       *relname;            /* triggered relation name */
469         Relation        rel;                    /* triggered relation */
470         HeapTuple       trigtuple;
471         HeapTuple       newtuple = NULL;
472         HeapTuple       rettuple;
473         TupleDesc       tupdesc;                /* tuple description */
474         int                     natts;                  /* # of attributes */
475         bool            isnull;                 /* to know is some column NULL or not */
476         int                     ret;
477         int                     i;
478
479         if (!CALLED_AS_TRIGGER(fcinfo))
480                 elog(ERROR, "ttdummy: not fired by trigger manager");
481         if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
482                 elog(ERROR, "ttdummy: can't process STATEMENT events");
483         if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
484                 elog(ERROR, "ttdummy: must be fired before event");
485         if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
486                 elog(ERROR, "ttdummy: can't process INSERT event");
487         if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
488                 newtuple = trigdata->tg_newtuple;
489
490         trigtuple = trigdata->tg_trigtuple;
491
492         rel = trigdata->tg_relation;
493         relname = SPI_getrelname(rel);
494
495         /* check if TT is OFF for this relation */
496         if (ttoff)                                      /* OFF - nothing to do */
497         {
498                 pfree(relname);
499                 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
500         }
501
502         trigger = trigdata->tg_trigger;
503
504         if (trigger->tgnargs != 2)
505                 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
506                          relname, trigger->tgnargs);
507
508         args = trigger->tgargs;
509         tupdesc = rel->rd_att;
510         natts = tupdesc->natts;
511
512         for (i = 0; i < 2; i++)
513         {
514                 attnum[i] = SPI_fnumber(tupdesc, args[i]);
515                 if (attnum[i] < 0)
516                         elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
517                 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
518                         elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
519                                  relname, args[0], args[1]);
520         }
521
522         oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
523         if (isnull)
524                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
525
526         oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
527         if (isnull)
528                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
529
530         if (newtuple != NULL)           /* UPDATE */
531         {
532                 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
533                 if (isnull)
534                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
535                 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
536                 if (isnull)
537                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
538
539                 if (oldon != newon || oldoff != newoff)
540                         elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
541                                  relname, args[0], args[1]);
542
543                 if (newoff != TTDUMMY_INFINITY)
544                 {
545                         pfree(relname);         /* allocated in upper executor context */
546                         return PointerGetDatum(NULL);
547                 }
548         }
549         else if (oldoff != TTDUMMY_INFINITY)            /* DELETE */
550         {
551                 pfree(relname);
552                 return PointerGetDatum(NULL);
553         }
554
555         {
556                 text       *seqname = DatumGetTextP(DirectFunctionCall1(textin,
557                                                                                 CStringGetDatum("ttdummy_seq")));
558
559                 newoff = DirectFunctionCall1(nextval,
560                                                                          PointerGetDatum(seqname));
561                 /* nextval now returns int64; coerce down to int32 */
562                 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
563                 pfree(seqname);
564         }
565
566         /* Connect to SPI manager */
567         if ((ret = SPI_connect()) < 0)
568                 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
569
570         /* Fetch tuple values and nulls */
571         cvals = (Datum *) palloc(natts * sizeof(Datum));
572         cnulls = (char *) palloc(natts * sizeof(char));
573         for (i = 0; i < natts; i++)
574         {
575                 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
576                                                                  tupdesc, i + 1, &isnull);
577                 cnulls[i] = (isnull) ? 'n' : ' ';
578         }
579
580         /* change date column(s) */
581         if (newtuple)                           /* UPDATE */
582         {
583                 cvals[attnum[0] - 1] = newoff;  /* start_date eq current date */
584                 cnulls[attnum[0] - 1] = ' ';
585                 cvals[attnum[1] - 1] = TTDUMMY_INFINITY;                /* stop_date eq INFINITY */
586                 cnulls[attnum[1] - 1] = ' ';
587         }
588         else
589                 /* DELETE */
590         {
591                 cvals[attnum[1] - 1] = newoff;  /* stop_date eq current date */
592                 cnulls[attnum[1] - 1] = ' ';
593         }
594
595         /* if there is no plan ... */
596         if (splan == NULL)
597         {
598                 void       *pplan;
599                 Oid                *ctypes;
600                 char       *query;
601
602                 /* allocate space in preparation */
603                 ctypes = (Oid *) palloc(natts * sizeof(Oid));
604                 query = (char *) palloc(100 + 16 * natts);
605
606                 /*
607                  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
608                  */
609                 sprintf(query, "INSERT INTO %s VALUES (", relname);
610                 for (i = 1; i <= natts; i++)
611                 {
612                         sprintf(query + strlen(query), "$%d%s",
613                                         i, (i < natts) ? ", " : ")");
614                         ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
615                 }
616
617                 /* Prepare plan for query */
618                 pplan = SPI_prepare(query, natts, ctypes);
619                 if (pplan == NULL)
620                         elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
621
622                 pplan = SPI_saveplan(pplan);
623                 if (pplan == NULL)
624                         elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
625
626                 splan = pplan;
627         }
628
629         ret = SPI_execp(splan, cvals, cnulls, 0);
630
631         if (ret < 0)
632                 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
633
634         /* Tuple to return to upper Executor ... */
635         if (newtuple)                           /* UPDATE */
636         {
637                 HeapTuple       tmptuple;
638
639                 tmptuple = SPI_copytuple(trigtuple);
640                 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
641                 SPI_freetuple(tmptuple);
642         }
643         else
644                 /* DELETE */
645                 rettuple = trigtuple;
646
647         SPI_finish();                           /* don't forget say Bye to SPI mgr */
648
649         pfree(relname);
650
651         return PointerGetDatum(rettuple);
652 }
653
654 PG_FUNCTION_INFO_V1(set_ttdummy);
655
656 Datum
657 set_ttdummy(PG_FUNCTION_ARGS)
658 {
659         int32           on = PG_GETARG_INT32(0);
660
661         if (ttoff)                                      /* OFF currently */
662         {
663                 if (on == 0)
664                         PG_RETURN_INT32(0);
665
666                 /* turn ON */
667                 ttoff = false;
668                 PG_RETURN_INT32(0);
669         }
670
671         /* ON currently */
672         if (on != 0)
673                 PG_RETURN_INT32(1);
674
675         /* turn OFF */
676         ttoff = true;
677
678         PG_RETURN_INT32(1);
679 }
680
681
682 /*
683  * Type int44 has no real-world use, but the regression tests use it.
684  * It's a four-element vector of int4's.
685  */
686
687 /*
688  *              int44in                 - converts "num num ..." to internal form
689  *
690  *              Note: Fills any missing positions with zeroes.
691  */
692 PG_FUNCTION_INFO_V1(int44in);
693
694 Datum
695 int44in(PG_FUNCTION_ARGS)
696 {
697         char       *input_string = PG_GETARG_CSTRING(0);
698         int32      *result = (int32 *) palloc(4 * sizeof(int32));
699         int                     i;
700
701         i = sscanf(input_string,
702                            "%d, %d, %d, %d",
703                            &result[0],
704                            &result[1],
705                            &result[2],
706                            &result[3]);
707         while (i < 4)
708                 result[i++] = 0;
709
710         PG_RETURN_POINTER(result);
711 }
712
713 /*
714  *              int44out                - converts internal form to "num num ..."
715  */
716 PG_FUNCTION_INFO_V1(int44out);
717
718 Datum
719 int44out(PG_FUNCTION_ARGS)
720 {
721         int32      *an_array = (int32 *) PG_GETARG_POINTER(0);
722         char       *result = (char *) palloc(16 * 4);           /* Allow 14 digits +
723                                                                                                                  * sign */
724         int                     i;
725         char       *walk;
726
727         walk = result;
728         for (i = 0; i < 4; i++)
729         {
730                 pg_ltoa(an_array[i], walk);
731                 while (*++walk != '\0')
732                         ;
733                 *walk++ = ' ';
734         }
735         *--walk = '\0';
736         PG_RETURN_CSTRING(result);
737 }