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