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