2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.33 1999/07/17 20:18:52 momjian Exp $
5 #include <float.h> /* faked on sunos */
9 #include "utils/geo_decls.h" /* includes <math.h> */
10 #include "executor/executor.h" /* For GetAttributeByName */
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);
29 ** Distance from a point to a path
32 regress_dist_ptpath(pt, path)
44 result = palloc(sizeof(double));
45 *result = Abs((double) DBL_MAX); /* +infinity */
48 result = point_distance(pt, &path->p[0]);
53 * the distance from a point to a path is the smallest
54 * distance from the point to any of its constituent segments.
56 Assert(path->npts > 1);
57 result = palloc(sizeof(double));
58 for (i = 0; i < path->npts - 1; ++i)
60 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
61 tmp = dist_ps(pt, &lseg);
62 if (i == 0 || *tmp < *result)
72 /* this essentially does a cartesian product of the lsegs in the
73 two paths, and finds the min distance between any two lsegs */
75 regress_path_dist(p1, p2)
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);
90 for (i = 0; i < p1->npts - 1; i++)
91 for (j = 0; j < p2->npts - 1; j++)
93 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
94 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
96 if (*min < *(tmp = lseg_distance(&seg1, &seg2)))
109 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
110 char buf[2 * (P_MAXDIG) + 20];
112 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
114 for (i = 0; i < poly->npts; i++)
116 sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
120 sprintf(buf, "%c", RDELIM);
122 return path_in(output);
125 /* return the point where two paths intersect. Assumes that they do. */
142 bool found; /* We've found the intersection */
144 found = false; /* Haven't found it yet */
146 for (i = 0; i < p1->npts - 1 && !found; i++)
147 for (j = 0; j < p2->npts - 1 && !found; j++)
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))
156 ln = line_construct_pp(&seg2.p[0], &seg2.p[1]);
157 retval = interpt_sl(&seg1, ln);
159 retval = lseg_interpt(&seg1, &seg2);
165 /* like lseg_construct, but assume space already allocated */
167 regress_lseg_construct(lseg, pt1, pt2)
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);
187 salary = (long) GetAttributeByName(tuple, "salary", &isnull);
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
202 WIDGET *widget_in(char *str);
203 char *widget_out(WIDGET * widget);
204 int pt_in_widget(Point *point, WIDGET * widget);
220 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
221 if (*p == ',' || (*p == LDELIM && !i))
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]);
230 sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
231 result->center.y, result->radius);
244 result = (char *) palloc(60);
245 sprintf(result, "(%g,%g,%g)",
246 widget->center.x, widget->center.y, widget->radius);
251 pt_in_widget(point, widget)
255 extern double point_dt();
257 return point_dt(point, &widget->center) < widget->radius;
260 #define ABS(X) ((X) > 0 ? (X) : -(X))
271 width = ABS(box->high.x - box->low.x);
272 height = ABS(box->high.y - box->low.y);
273 return width * height;
284 if (!(new_string = palloc(NAMEDATALEN)))
286 fprintf(stderr, "reverse_name: palloc failed\n");
289 MemSet(new_string, 0, NAMEDATALEN);
290 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
292 if (i == NAMEDATALEN || !string[i])
296 new_string[len - i] = string[i];
300 #include "executor/spi.h" /* this is what you need to work with SPI */
301 #include "commands/trigger.h" /* -"- and triggers */
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);
311 HeapTuple /* have to return HeapTuple to Executor */
320 char sql[MAX_QUERY_SIZE];
326 tuple = CurrentTriggerData->tg_trigtuple;
327 rel = CurrentTriggerData->tg_relation;
328 tupdesc = rel->rd_att;
329 if (TRIGGER_FIRED_BEFORE(CurrentTriggerData->tg_event))
332 level = &fd17b_level;
333 recursion = &fd17b_recursion;
339 level = &fd17a_level;
340 recursion = &fd17a_recursion;
344 CurrentTriggerData = NULL;
346 if (!TransactionIdIsCurrentTransactionId(*xid))
348 *xid = GetCurrentTransactionId();
366 sprintf(sql, "insert into %s select * from %s where %s = '%s'::%s",
367 SPI_getrelname(rel), SPI_getrelname(rel),
368 SPI_fname(tupdesc, 1),
369 SPI_getvalue(tuple, tupdesc, 1),
370 SPI_gettype(tupdesc, 1));
372 if ((ret = SPI_exec(sql, 0)) < 0)
373 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
376 inserted = SPI_processed;
378 sprintf(sql, "select count (*) from %s where %s = '%s'::%s",
380 SPI_fname(tupdesc, 1),
381 SPI_getvalue(tuple, tupdesc, 1),
382 SPI_gettype(tupdesc, 1));
384 if ((ret = SPI_exec(sql, 0)) < 0)
385 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
388 if (SPI_processed > 0)
392 SPI_tuptable->vals[0],
393 SPI_tuptable->tupdesc,
399 elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
400 when, *level, inserted, selected);
407 *xid = InvalidTransactionId;
412 HeapTuple ttdummy(void);
413 int32 set_ttdummy(int32 on);
415 extern int4 nextval(struct varlena * seqin);
417 #define TTDUMMY_INFINITY 999999
419 static void *splan = NULL;
420 static bool ttoff = false;
425 Trigger *trigger; /* to get trigger name */
426 char **args; /* arguments */
427 int attnum[2]; /* fnumbers of start/stop columns */
432 Datum *cvals; /* column values */
433 char *cnulls; /* column nulls */
434 char *relname; /* triggered relation name */
435 Relation rel; /* triggered relation */
437 HeapTuple newtuple = NULL;
439 TupleDesc tupdesc; /* tuple description */
440 int natts; /* # of attributes */
441 bool isnull; /* to know is some column NULL or not */
445 if (!CurrentTriggerData)
446 elog(ERROR, "ttdummy: triggers are not initialized");
447 if (TRIGGER_FIRED_FOR_STATEMENT(CurrentTriggerData->tg_event))
448 elog(ERROR, "ttdummy: can't process STATEMENT events");
449 if (TRIGGER_FIRED_AFTER(CurrentTriggerData->tg_event))
450 elog(ERROR, "ttdummy: must be fired before event");
451 if (TRIGGER_FIRED_BY_INSERT(CurrentTriggerData->tg_event))
452 elog(ERROR, "ttdummy: can't process INSERT event");
453 if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event))
454 newtuple = CurrentTriggerData->tg_newtuple;
456 trigtuple = CurrentTriggerData->tg_trigtuple;
458 rel = CurrentTriggerData->tg_relation;
459 relname = SPI_getrelname(rel);
461 /* check if TT is OFF for this relation */
462 if (ttoff) /* OFF - nothing to do */
465 return (newtuple != NULL) ? newtuple : trigtuple;
468 trigger = CurrentTriggerData->tg_trigger;
470 if (trigger->tgnargs != 2)
471 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
472 relname, trigger->tgnargs);
474 args = trigger->tgargs;
475 tupdesc = rel->rd_att;
476 natts = tupdesc->natts;
478 CurrentTriggerData = NULL;
480 for (i = 0; i < 2; i++)
482 attnum[i] = SPI_fnumber(tupdesc, args[i]);
484 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
485 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
486 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
487 relname, args[0], args[1]);
490 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
492 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
494 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
496 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
498 if (newtuple != NULL) /* UPDATE */
500 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
502 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
503 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
505 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
507 if (oldon != newon || oldoff != newoff)
508 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
509 relname, args[0], args[1]);
511 if (newoff != TTDUMMY_INFINITY)
513 pfree(relname); /* allocated in upper executor context */
517 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
524 struct varlena *seqname = textin("ttdummy_seq");
526 newoff = nextval(seqname);
530 /* Connect to SPI manager */
531 if ((ret = SPI_connect()) < 0)
532 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
534 /* Fetch tuple values and nulls */
535 cvals = (Datum *) palloc(natts * sizeof(Datum));
536 cnulls = (char *) palloc(natts * sizeof(char));
537 for (i = 0; i < natts; i++)
539 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
540 tupdesc, i + 1, &isnull);
541 cnulls[i] = (isnull) ? 'n' : ' ';
544 /* change date column(s) */
545 if (newtuple) /* UPDATE */
547 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
548 cnulls[attnum[0] - 1] = ' ';
549 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
550 cnulls[attnum[1] - 1] = ' ';
555 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
556 cnulls[attnum[1] - 1] = ' ';
559 /* if there is no plan ... */
564 char sql[MAX_QUERY_SIZE];
566 /* allocate ctypes for preparation */
567 ctypes = (Oid *) palloc(natts * sizeof(Oid));
570 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
572 sprintf(sql, "INSERT INTO %s VALUES (", relname);
573 for (i = 1; i <= natts; i++)
575 sprintf(sql + strlen(sql), "$%d%s",
576 i, (i < natts) ? ", " : ")");
577 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
580 /* Prepare plan for query */
581 pplan = SPI_prepare(sql, natts, ctypes);
583 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
585 pplan = SPI_saveplan(pplan);
587 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
592 ret = SPI_execp(splan, cvals, cnulls, 0);
595 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
597 /* Tuple to return to upper Executor ... */
598 if (newtuple) /* UPDATE */
602 tmptuple = SPI_copytuple(trigtuple);
603 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
608 rettuple = trigtuple;
610 SPI_finish(); /* don't forget say Bye to SPI mgr */
618 set_ttdummy(int32 on)
621 if (ttoff) /* OFF currently */