]> granicus.if.org Git - postgresql/blob - src/test/regress/regress.c
Some changes to prepare for LONG attributes.
[postgresql] / src / test / regress / regress.c
1 /*
2  * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.35 1999/12/16 22:20:03 wieck 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 void *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 char overpaid(TUPLE tuple);
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
180 char
181 overpaid(tuple)
182 TUPLE           tuple;
183 {
184         bool            isnull;
185         long            salary;
186
187         salary = (long) GetAttributeByName(tuple, "salary", &isnull);
188         return salary > 699;
189 }
190
191 /* New type "widget"
192  * This used to be "circle", but I added circle to builtins,
193  *      so needed to make sure the names do not collide. - tgl 97/04/21
194  */
195
196 typedef struct
197 {
198         Point           center;
199         double          radius;
200 }                       WIDGET;
201
202 WIDGET     *widget_in(char *str);
203 char       *widget_out(WIDGET * widget);
204 int                     pt_in_widget(Point *point, WIDGET * widget);
205
206 #define NARGS   3
207
208 WIDGET *
209 widget_in(str)
210 char       *str;
211 {
212         char       *p,
213                            *coord[NARGS],
214                                 buf2[1000];
215         int                     i;
216         WIDGET     *result;
217
218         if (str == NULL)
219                 return NULL;
220         for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
221                 if (*p == ',' || (*p == LDELIM && !i))
222                         coord[i++] = p + 1;
223         if (i < NARGS - 1)
224                 return NULL;
225         result = (WIDGET *) palloc(sizeof(WIDGET));
226         result->center.x = atof(coord[0]);
227         result->center.y = atof(coord[1]);
228         result->radius = atof(coord[2]);
229
230         sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
231                         result->center.y, result->radius);
232         return result;
233 }
234
235 char *
236 widget_out(widget)
237 WIDGET     *widget;
238 {
239         char       *result;
240
241         if (widget == NULL)
242                 return NULL;
243
244         result = (char *) palloc(60);
245         sprintf(result, "(%g,%g,%g)",
246                         widget->center.x, widget->center.y, widget->radius);
247         return result;
248 }
249
250 int
251 pt_in_widget(point, widget)
252 Point      *point;
253 WIDGET     *widget;
254 {
255         extern double point_dt();
256
257         return point_dt(point, &widget->center) < widget->radius;
258 }
259
260 #define ABS(X) ((X) > 0 ? (X) : -(X))
261
262 int
263 boxarea(box)
264
265 BOX                *box;
266
267 {
268         int                     width,
269                                 height;
270
271         width = ABS(box->high.x - box->low.x);
272         height = ABS(box->high.y - box->low.y);
273         return width * height;
274 }
275
276 char *
277 reverse_name(string)
278 char       *string;
279 {
280         int                     i;
281         int                     len;
282         char       *new_string;
283
284         if (!(new_string = palloc(NAMEDATALEN)))
285         {
286                 fprintf(stderr, "reverse_name: palloc failed\n");
287                 return NULL;
288         }
289         MemSet(new_string, 0, NAMEDATALEN);
290         for (i = 0; i < NAMEDATALEN && string[i]; ++i)
291                 ;
292         if (i == NAMEDATALEN || !string[i])
293                 --i;
294         len = i;
295         for (; i >= 0; --i)
296                 new_string[len - i] = string[i];
297         return new_string;
298 }
299
300 #include "executor/spi.h"               /* this is what you need to work with SPI */
301 #include "commands/trigger.h"   /* -"- and triggers */
302
303 static TransactionId fd17b_xid = InvalidTransactionId;
304 static TransactionId fd17a_xid = InvalidTransactionId;
305 static int      fd17b_level = 0;
306 static int      fd17a_level = 0;
307 static bool fd17b_recursion = true;
308 static bool fd17a_recursion = true;
309 HeapTuple       funny_dup17(void);
310
311 HeapTuple                                               /* have to return HeapTuple to Executor */
312 funny_dup17()
313 {
314         TransactionId *xid;
315         int                *level;
316         bool       *recursion;
317         Relation        rel;
318         TupleDesc       tupdesc;
319         HeapTuple       tuple;
320         char       *query,
321                            *fieldval,
322                            *fieldtype;
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         fieldval = SPI_getvalue(tuple, tupdesc, 1);
369         fieldtype = SPI_gettype(tupdesc, 1);
370
371         query = (char *) palloc(100 + NAMEDATALEN*3 +
372                                                         strlen(fieldval) + strlen(fieldtype));
373
374         sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
375                         SPI_getrelname(rel), SPI_getrelname(rel),
376                         SPI_fname(tupdesc, 1),
377                         fieldval, fieldtype);
378
379         if ((ret = SPI_exec(query, 0)) < 0)
380                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
381                          when, *level, ret);
382
383         inserted = SPI_processed;
384
385         sprintf(query, "select count (*) from %s where %s = '%s'::%s",
386                         SPI_getrelname(rel),
387                         SPI_fname(tupdesc, 1),
388                         fieldval, fieldtype);
389
390         if ((ret = SPI_exec(query, 0)) < 0)
391                 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
392                          when, *level, ret);
393
394         if (SPI_processed > 0)
395         {
396                 selected = int4in(
397                                                   SPI_getvalue(
398                                                                            SPI_tuptable->vals[0],
399                                                                            SPI_tuptable->tupdesc,
400                                                                            1
401                                                                            )
402                         );
403         }
404
405         elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
406                  when, *level, inserted, selected);
407
408         SPI_finish();
409
410         (*level)--;
411
412         if (*level == 0)
413                 *xid = InvalidTransactionId;
414
415         return tuple;
416 }
417
418 HeapTuple       ttdummy(void);
419 int32           set_ttdummy(int32 on);
420
421 extern int4 nextval(struct varlena * seqin);
422
423 #define TTDUMMY_INFINITY        999999
424
425 static void *splan = NULL;
426 static bool ttoff = false;
427
428 HeapTuple
429 ttdummy()
430 {
431         Trigger    *trigger;            /* to get trigger name */
432         char      **args;                       /* arguments */
433         int                     attnum[2];              /* fnumbers of start/stop columns */
434         Datum           oldon,
435                                 oldoff;
436         Datum           newon,
437                                 newoff;
438         Datum      *cvals;                      /* column values */
439         char       *cnulls;                     /* column nulls */
440         char       *relname;            /* triggered relation name */
441         Relation        rel;                    /* triggered relation */
442         HeapTuple       trigtuple;
443         HeapTuple       newtuple = NULL;
444         HeapTuple       rettuple;
445         TupleDesc       tupdesc;                /* tuple description */
446         int                     natts;                  /* # of attributes */
447         bool            isnull;                 /* to know is some column NULL or not */
448         int                     ret;
449         int                     i;
450
451         if (!CurrentTriggerData)
452                 elog(ERROR, "ttdummy: triggers are not initialized");
453         if (TRIGGER_FIRED_FOR_STATEMENT(CurrentTriggerData->tg_event))
454                 elog(ERROR, "ttdummy: can't process STATEMENT events");
455         if (TRIGGER_FIRED_AFTER(CurrentTriggerData->tg_event))
456                 elog(ERROR, "ttdummy: must be fired before event");
457         if (TRIGGER_FIRED_BY_INSERT(CurrentTriggerData->tg_event))
458                 elog(ERROR, "ttdummy: can't process INSERT event");
459         if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event))
460                 newtuple = CurrentTriggerData->tg_newtuple;
461
462         trigtuple = CurrentTriggerData->tg_trigtuple;
463
464         rel = CurrentTriggerData->tg_relation;
465         relname = SPI_getrelname(rel);
466
467         /* check if TT is OFF for this relation */
468         if (ttoff)                                      /* OFF - nothing to do */
469         {
470                 pfree(relname);
471                 return (newtuple != NULL) ? newtuple : trigtuple;
472         }
473
474         trigger = CurrentTriggerData->tg_trigger;
475
476         if (trigger->tgnargs != 2)
477                 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
478                          relname, trigger->tgnargs);
479
480         args = trigger->tgargs;
481         tupdesc = rel->rd_att;
482         natts = tupdesc->natts;
483
484         CurrentTriggerData = NULL;
485
486         for (i = 0; i < 2; i++)
487         {
488                 attnum[i] = SPI_fnumber(tupdesc, args[i]);
489                 if (attnum[i] < 0)
490                         elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
491                 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
492                         elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
493                                  relname, args[0], args[1]);
494         }
495
496         oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
497         if (isnull)
498                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
499
500         oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
501         if (isnull)
502                 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
503
504         if (newtuple != NULL)           /* UPDATE */
505         {
506                 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
507                 if (isnull)
508                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
509                 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
510                 if (isnull)
511                         elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
512
513                 if (oldon != newon || oldoff != newoff)
514                         elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
515                                  relname, args[0], args[1]);
516
517                 if (newoff != TTDUMMY_INFINITY)
518                 {
519                         pfree(relname);         /* allocated in upper executor context */
520                         return NULL;
521                 }
522         }
523         else if (oldoff != TTDUMMY_INFINITY)            /* DELETE */
524         {
525                 pfree(relname);
526                 return NULL;
527         }
528
529         {
530                 struct varlena *seqname = textin("ttdummy_seq");
531
532                 newoff = nextval(seqname);
533                 pfree(seqname);
534         }
535
536         /* Connect to SPI manager */
537         if ((ret = SPI_connect()) < 0)
538                 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
539
540         /* Fetch tuple values and nulls */
541         cvals = (Datum *) palloc(natts * sizeof(Datum));
542         cnulls = (char *) palloc(natts * sizeof(char));
543         for (i = 0; i < natts; i++)
544         {
545                 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
546                                                                  tupdesc, i + 1, &isnull);
547                 cnulls[i] = (isnull) ? 'n' : ' ';
548         }
549
550         /* change date column(s) */
551         if (newtuple)                           /* UPDATE */
552         {
553                 cvals[attnum[0] - 1] = newoff;  /* start_date eq current date */
554                 cnulls[attnum[0] - 1] = ' ';
555                 cvals[attnum[1] - 1] = TTDUMMY_INFINITY;                /* stop_date eq INFINITY */
556                 cnulls[attnum[1] - 1] = ' ';
557         }
558         else
559 /* DELETE */
560         {
561                 cvals[attnum[1] - 1] = newoff;  /* stop_date eq current date */
562                 cnulls[attnum[1] - 1] = ' ';
563         }
564
565         /* if there is no plan ... */
566         if (splan == NULL)
567         {
568                 void       *pplan;
569                 Oid                *ctypes;
570                 char       *query;
571
572                 /* allocate space in preparation */
573                 ctypes = (Oid *) palloc(natts * sizeof(Oid));
574                 query = (char *) palloc(100 + 16 * natts);
575
576                 /*
577                  * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
578                  */
579                 sprintf(query, "INSERT INTO %s VALUES (", relname);
580                 for (i = 1; i <= natts; i++)
581                 {
582                         sprintf(query + strlen(query), "$%d%s",
583                                         i, (i < natts) ? ", " : ")");
584                         ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
585                 }
586
587                 /* Prepare plan for query */
588                 pplan = SPI_prepare(query, natts, ctypes);
589                 if (pplan == NULL)
590                         elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
591
592                 pplan = SPI_saveplan(pplan);
593                 if (pplan == NULL)
594                         elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
595
596                 splan = pplan;
597         }
598
599         ret = SPI_execp(splan, cvals, cnulls, 0);
600
601         if (ret < 0)
602                 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
603
604         /* Tuple to return to upper Executor ... */
605         if (newtuple)                           /* UPDATE */
606         {
607                 HeapTuple       tmptuple;
608
609                 tmptuple = SPI_copytuple(trigtuple);
610                 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
611                 SPI_freetuple(tmptuple);
612         }
613         else
614 /* DELETE */
615                 rettuple = trigtuple;
616
617         SPI_finish();                           /* don't forget say Bye to SPI mgr */
618
619         pfree(relname);
620
621         return rettuple;
622 }
623
624 int32
625 set_ttdummy(int32 on)
626 {
627
628         if (ttoff)                                      /* OFF currently */
629         {
630                 if (on == 0)
631                         return 0;
632
633                 /* turn ON */
634                 ttoff = false;
635                 return 0;
636         }
637
638         /* ON currently */
639         if (on != 0)
640                 return 1;
641
642         /* turn OFF */
643         ttoff = true;
644
645         return 1;
646
647 }