2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.39 2000/06/11 20:07:44 tgl Exp $
5 #include <float.h> /* faked on sunos */
9 #include "utils/geo_decls.h" /* includes <math.h> */
10 #include "executor/executor.h" /* For GetAttributeByName */
11 #include "commands/sequence.h" /* for nextval() */
18 typedef TupleTableSlot *TUPLE;
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 int boxarea(BOX *box);
27 extern char *reverse_name(char *string);
30 ** Distance from a point to a path
33 regress_dist_ptpath(pt, path)
45 result = palloc(sizeof(double));
46 *result = Abs((double) DBL_MAX); /* +infinity */
49 result = point_distance(pt, &path->p[0]);
54 * the distance from a point to a path is the smallest
55 * distance from the point to any of its constituent segments.
57 Assert(path->npts > 1);
58 result = palloc(sizeof(double));
59 for (i = 0; i < path->npts - 1; ++i)
61 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
62 tmp = dist_ps(pt, &lseg);
63 if (i == 0 || *tmp < *result)
73 /* this essentially does a cartesian product of the lsegs in the
74 two paths, and finds the min distance between any two lsegs */
76 regress_path_dist(p1, p2)
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);
91 for (i = 0; i < p1->npts - 1; i++)
92 for (j = 0; j < p2->npts - 1; j++)
94 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
95 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
97 if (*min < *(tmp = lseg_distance(&seg1, &seg2)))
110 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
111 char buf[2 * (P_MAXDIG) + 20];
113 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
115 for (i = 0; i < poly->npts; i++)
117 sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
121 sprintf(buf, "%c", RDELIM);
123 return path_in(output);
126 /* return the point where two paths intersect. Assumes that they do. */
143 bool found; /* We've found the intersection */
145 found = false; /* Haven't found it yet */
147 for (i = 0; i < p1->npts - 1 && !found; i++)
148 for (j = 0; j < p2->npts - 1 && !found; j++)
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))
157 ln = line_construct_pp(&seg2.p[0], &seg2.p[1]);
158 retval = interpt_sl(&seg1, ln);
160 retval = lseg_interpt(&seg1, &seg2);
166 /* like lseg_construct, but assume space already allocated */
168 regress_lseg_construct(lseg, pt1, pt2)
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);
181 overpaid(PG_FUNCTION_ARGS)
183 TUPLE tuple = (TUPLE) PG_GETARG_POINTER(0);
187 salary = (long) GetAttributeByName(tuple, "salary", &isnull);
190 PG_RETURN_BOOL(salary > 699);
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 extern Datum funny_dup17(PG_FUNCTION_ARGS);
314 funny_dup17(PG_FUNCTION_ARGS)
316 TriggerData *trigdata = (TriggerData *) fcinfo->context;
331 if (!CALLED_AS_TRIGGER(fcinfo))
332 elog(ERROR, "funny_dup17: not fired by trigger manager");
334 tuple = trigdata->tg_trigtuple;
335 rel = trigdata->tg_relation;
336 tupdesc = rel->rd_att;
337 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
340 level = &fd17b_level;
341 recursion = &fd17b_recursion;
347 level = &fd17a_level;
348 recursion = &fd17a_recursion;
352 if (!TransactionIdIsCurrentTransactionId(*xid))
354 *xid = GetCurrentTransactionId();
362 return PointerGetDatum(tuple);
366 return PointerGetDatum(tuple);
372 fieldval = SPI_getvalue(tuple, tupdesc, 1);
373 fieldtype = SPI_gettype(tupdesc, 1);
375 query = (char *) palloc(100 + NAMEDATALEN * 3 +
376 strlen(fieldval) + strlen(fieldtype));
378 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
379 SPI_getrelname(rel), SPI_getrelname(rel),
380 SPI_fname(tupdesc, 1),
381 fieldval, fieldtype);
383 if ((ret = SPI_exec(query, 0)) < 0)
384 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
387 inserted = SPI_processed;
389 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
391 SPI_fname(tupdesc, 1),
392 fieldval, fieldtype);
394 if ((ret = SPI_exec(query, 0)) < 0)
395 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
398 if (SPI_processed > 0)
400 selected = DatumGetInt32(DirectFunctionCall1(int4in,
401 CStringGetDatum(SPI_getvalue(
402 SPI_tuptable->vals[0],
403 SPI_tuptable->tupdesc,
408 elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
409 when, *level, inserted, selected);
416 *xid = InvalidTransactionId;
418 return PointerGetDatum(tuple);
421 extern Datum ttdummy(PG_FUNCTION_ARGS);
422 int32 set_ttdummy(int32 on);
424 #define TTDUMMY_INFINITY 999999
426 static void *splan = NULL;
427 static bool ttoff = false;
430 ttdummy(PG_FUNCTION_ARGS)
432 TriggerData *trigdata = (TriggerData *) fcinfo->context;
433 Trigger *trigger; /* to get trigger name */
434 char **args; /* arguments */
435 int attnum[2]; /* fnumbers of start/stop columns */
440 Datum *cvals; /* column values */
441 char *cnulls; /* column nulls */
442 char *relname; /* triggered relation name */
443 Relation rel; /* triggered relation */
445 HeapTuple newtuple = NULL;
447 TupleDesc tupdesc; /* tuple description */
448 int natts; /* # of attributes */
449 bool isnull; /* to know is some column NULL or not */
453 if (!CALLED_AS_TRIGGER(fcinfo))
454 elog(ERROR, "ttdummy: not fired by trigger manager");
455 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
456 elog(ERROR, "ttdummy: can't process STATEMENT events");
457 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
458 elog(ERROR, "ttdummy: must be fired before event");
459 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
460 elog(ERROR, "ttdummy: can't process INSERT event");
461 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
462 newtuple = trigdata->tg_newtuple;
464 trigtuple = trigdata->tg_trigtuple;
466 rel = trigdata->tg_relation;
467 relname = SPI_getrelname(rel);
469 /* check if TT is OFF for this relation */
470 if (ttoff) /* OFF - nothing to do */
473 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
476 trigger = trigdata->tg_trigger;
478 if (trigger->tgnargs != 2)
479 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
480 relname, trigger->tgnargs);
482 args = trigger->tgargs;
483 tupdesc = rel->rd_att;
484 natts = tupdesc->natts;
486 for (i = 0; i < 2; i++)
488 attnum[i] = SPI_fnumber(tupdesc, args[i]);
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]);
496 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
498 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
500 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
502 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
504 if (newtuple != NULL) /* UPDATE */
506 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
508 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
509 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
511 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
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]);
517 if (newoff != TTDUMMY_INFINITY)
519 pfree(relname); /* allocated in upper executor context */
520 return PointerGetDatum(NULL);
523 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
526 return PointerGetDatum(NULL);
530 text *seqname = textin("ttdummy_seq");
532 newoff = DirectFunctionCall1(nextval,
533 PointerGetDatum(seqname));
537 /* Connect to SPI manager */
538 if ((ret = SPI_connect()) < 0)
539 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
541 /* Fetch tuple values and nulls */
542 cvals = (Datum *) palloc(natts * sizeof(Datum));
543 cnulls = (char *) palloc(natts * sizeof(char));
544 for (i = 0; i < natts; i++)
546 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
547 tupdesc, i + 1, &isnull);
548 cnulls[i] = (isnull) ? 'n' : ' ';
551 /* change date column(s) */
552 if (newtuple) /* UPDATE */
554 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
555 cnulls[attnum[0] - 1] = ' ';
556 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
557 cnulls[attnum[1] - 1] = ' ';
562 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
563 cnulls[attnum[1] - 1] = ' ';
566 /* if there is no plan ... */
573 /* allocate space in preparation */
574 ctypes = (Oid *) palloc(natts * sizeof(Oid));
575 query = (char *) palloc(100 + 16 * natts);
578 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
580 sprintf(query, "INSERT INTO %s VALUES (", relname);
581 for (i = 1; i <= natts; i++)
583 sprintf(query + strlen(query), "$%d%s",
584 i, (i < natts) ? ", " : ")");
585 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
588 /* Prepare plan for query */
589 pplan = SPI_prepare(query, natts, ctypes);
591 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
593 pplan = SPI_saveplan(pplan);
595 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
600 ret = SPI_execp(splan, cvals, cnulls, 0);
603 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
605 /* Tuple to return to upper Executor ... */
606 if (newtuple) /* UPDATE */
610 tmptuple = SPI_copytuple(trigtuple);
611 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
612 SPI_freetuple(tmptuple);
616 rettuple = trigtuple;
618 SPI_finish(); /* don't forget say Bye to SPI mgr */
622 return PointerGetDatum(rettuple);
626 set_ttdummy(int32 on)
629 if (ttoff) /* OFF currently */