2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.30 1999/02/21 03:49:55 scrappy Exp $
5 #include <float.h> /* faked on sunos */
7 #include <string.h> /* for MemSet() */
11 #include "utils/geo_decls.h" /* includes <math.h> */
12 #include "executor/executor.h" /* For GetAttributeByName */
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_name(char *string);
31 ** Distance from a point to a path
34 regress_dist_ptpath(pt, path)
46 result = palloc(sizeof(double));
47 *result = Abs((double) DBL_MAX); /* +infinity */
50 result = point_distance(pt, &path->p[0]);
55 * the distance from a point to a path is the smallest
56 * distance from the point to any of its constituent segments.
58 Assert(path->npts > 1);
59 result = palloc(sizeof(double));
60 for (i = 0; i < path->npts - 1; ++i)
62 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
63 tmp = dist_ps(pt, &lseg);
64 if (i == 0 || *tmp < *result)
74 /* this essentially does a cartesian product of the lsegs in the
75 two paths, and finds the min distance between any two lsegs */
77 regress_path_dist(p1, p2)
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);
92 for (i = 0; i < p1->npts - 1; i++)
93 for (j = 0; j < p2->npts - 1; j++)
95 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
96 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
98 if (*min < *(tmp = lseg_distance(&seg1, &seg2)))
111 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
112 char buf[2 * (P_MAXDIG) + 20];
114 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
116 for (i = 0; i < poly->npts; i++)
118 sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
122 sprintf(buf, "%c", RDELIM);
124 return path_in(output);
127 /* return the point where two paths intersect. Assumes that they do. */
144 bool found; /* We've found the intersection */
146 found = false; /* Haven't found it yet */
148 for (i = 0; i < p1->npts - 1 && !found; i++)
149 for (j = 0; j < p2->npts - 1 && !found; j++)
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))
158 ln = line_construct_pp(&seg2.p[0], &seg2.p[1]);
159 retval = interpt_sl(&seg1, ln);
161 retval = lseg_interpt(&seg1, &seg2);
167 /* like lseg_construct, but assume space already allocated */
169 regress_lseg_construct(lseg, pt1, pt2)
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);
189 salary = (long) GetAttributeByName(tuple, "salary", &isnull);
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
204 WIDGET *widget_in(char *str);
205 char *widget_out(WIDGET * widget);
206 int pt_in_widget(Point *point, WIDGET * widget);
222 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
223 if (*p == ',' || (*p == LDELIM && !i))
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]);
232 sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
233 result->center.y, result->radius);
246 result = (char *) palloc(60);
247 sprintf(result, "(%g,%g,%g)",
248 widget->center.x, widget->center.y, widget->radius);
253 pt_in_widget(point, widget)
257 extern double point_dt();
259 return point_dt(point, &widget->center) < widget->radius;
262 #define ABS(X) ((X) > 0 ? (X) : -(X))
273 width = ABS(box->high.x - box->low.x);
274 height = ABS(box->high.y - box->low.y);
275 return width * height;
286 if (!(new_string = palloc(NAMEDATALEN)))
288 fprintf(stderr, "reverse_name: palloc failed\n");
291 MemSet(new_string, 0, NAMEDATALEN);
292 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
294 if (i == NAMEDATALEN || !string[i])
298 new_string[len - i] = string[i];
302 #include "executor/spi.h" /* this is what you need to work with SPI */
303 #include "commands/trigger.h" /* -"- and triggers */
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);
313 HeapTuple /* have to return HeapTuple to Executor */
328 tuple = CurrentTriggerData->tg_trigtuple;
329 rel = CurrentTriggerData->tg_relation;
330 tupdesc = rel->rd_att;
331 if (TRIGGER_FIRED_BEFORE(CurrentTriggerData->tg_event))
334 level = &fd17b_level;
335 recursion = &fd17b_recursion;
341 level = &fd17a_level;
342 recursion = &fd17a_recursion;
346 CurrentTriggerData = NULL;
348 if (!TransactionIdIsCurrentTransactionId(*xid))
350 *xid = GetCurrentTransactionId();
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));
374 if ((ret = SPI_exec(sql, 0)) < 0)
375 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
378 inserted = SPI_processed;
380 sprintf(sql, "select count (*) from %s where %s = '%s'::%s",
382 SPI_fname(tupdesc, 1),
383 SPI_getvalue(tuple, tupdesc, 1),
384 SPI_gettype(tupdesc, 1));
386 if ((ret = SPI_exec(sql, 0)) < 0)
387 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
390 if (SPI_processed > 0)
394 SPI_tuptable->vals[0],
395 SPI_tuptable->tupdesc,
401 elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
402 when, *level, inserted, selected);
409 *xid = InvalidTransactionId;
414 HeapTuple ttdummy(void);
415 int32 set_ttdummy(int32 on);
417 extern int4 nextval(struct varlena * seqin);
419 #define TTDUMMY_INFINITY 999999
421 static void *splan = NULL;
422 static bool ttoff = false;
427 Trigger *trigger; /* to get trigger name */
428 char **args; /* arguments */
429 int attnum[2]; /* fnumbers of start/stop columns */
434 Datum *cvals; /* column values */
435 char *cnulls; /* column nulls */
436 char *relname; /* triggered relation name */
437 Relation rel; /* triggered relation */
439 HeapTuple newtuple = NULL;
441 TupleDesc tupdesc; /* tuple description */
442 int natts; /* # of attributes */
443 bool isnull; /* to know is some column NULL or not */
447 if (!CurrentTriggerData)
448 elog(ERROR, "ttdummy: triggers are not initialized");
449 if (TRIGGER_FIRED_FOR_STATEMENT(CurrentTriggerData->tg_event))
450 elog(ERROR, "ttdummy: can't process STATEMENT events");
451 if (TRIGGER_FIRED_AFTER(CurrentTriggerData->tg_event))
452 elog(ERROR, "ttdummy: must be fired before event");
453 if (TRIGGER_FIRED_BY_INSERT(CurrentTriggerData->tg_event))
454 elog(ERROR, "ttdummy: can't process INSERT event");
455 if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event))
456 newtuple = CurrentTriggerData->tg_newtuple;
458 trigtuple = CurrentTriggerData->tg_trigtuple;
460 rel = CurrentTriggerData->tg_relation;
461 relname = SPI_getrelname(rel);
463 /* check if TT is OFF for this relation */
464 if (ttoff) /* OFF - nothing to do */
467 return (newtuple != NULL) ? newtuple : trigtuple;
470 trigger = CurrentTriggerData->tg_trigger;
472 if (trigger->tgnargs != 2)
473 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
474 relname, trigger->tgnargs);
476 args = trigger->tgargs;
477 tupdesc = rel->rd_att;
478 natts = tupdesc->natts;
480 CurrentTriggerData = NULL;
482 for (i = 0; i < 2; i++)
484 attnum[i] = SPI_fnumber(tupdesc, args[i]);
486 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
487 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
488 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
489 relname, args[0], args[1]);
492 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
494 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
496 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
498 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
500 if (newtuple != NULL) /* UPDATE */
502 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
504 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
505 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
507 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
509 if (oldon != newon || oldoff != newoff)
510 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
511 relname, args[0], args[1]);
513 if (newoff != TTDUMMY_INFINITY)
515 pfree(relname); /* allocated in upper executor context */
519 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
526 struct varlena *seqname = textin("ttdummy_seq");
528 newoff = nextval(seqname);
532 /* Connect to SPI manager */
533 if ((ret = SPI_connect()) < 0)
534 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
536 /* Fetch tuple values and nulls */
537 cvals = (Datum *) palloc(natts * sizeof(Datum));
538 cnulls = (char *) palloc(natts * sizeof(char));
539 for (i = 0; i < natts; i++)
541 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
542 tupdesc, i + 1, &isnull);
543 cnulls[i] = (isnull) ? 'n' : ' ';
546 /* change date column(s) */
547 if (newtuple) /* UPDATE */
549 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
550 cnulls[attnum[0] - 1] = ' ';
551 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
552 cnulls[attnum[1] - 1] = ' ';
557 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
558 cnulls[attnum[1] - 1] = ' ';
561 /* if there is no plan ... */
568 /* allocate ctypes for preparation */
569 ctypes = (Oid *) palloc(natts * sizeof(Oid));
572 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
574 sprintf(sql, "INSERT INTO %s VALUES (", relname);
575 for (i = 1; i <= natts; i++)
577 sprintf(sql + strlen(sql), "$%d%s",
578 i, (i < natts) ? ", " : ")");
579 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
582 /* Prepare plan for query */
583 pplan = SPI_prepare(sql, natts, ctypes);
585 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
587 pplan = SPI_saveplan(pplan);
589 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
594 ret = SPI_execp(splan, cvals, cnulls, 0);
597 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
599 /* Tuple to return to upper Executor ... */
600 if (newtuple) /* UPDATE */
604 tmptuple = SPI_copytuple(trigtuple);
605 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
610 rettuple = trigtuple;
612 SPI_finish(); /* don't forget say Bye to SPI mgr */
620 set_ttdummy(int32 on)
623 if (ttoff) /* OFF currently */