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