2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.45 2000/11/20 20:36:53 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 Datum regress_dist_ptpath(PG_FUNCTION_ARGS);
21 extern Datum regress_path_dist(PG_FUNCTION_ARGS);
22 extern PATH *poly2path(POLYGON *poly);
23 extern Datum interpt_pp(PG_FUNCTION_ARGS);
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 extern int oldstyle_length(int n, text *t);
31 ** Distance from a point to a path
33 PG_FUNCTION_INFO_V1(regress_dist_ptpath);
36 regress_dist_ptpath(PG_FUNCTION_ARGS)
38 Point *pt = PG_GETARG_POINT_P(0);
39 PATH *path = PG_GETARG_PATH_P(1);
40 float8 result = 0.0; /* keep compiler quiet */
50 result = point_dt(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 for (i = 0; i < path->npts - 1; ++i)
61 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
62 tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps,
64 LsegPGetDatum(&lseg)));
65 if (i == 0 || tmp < result)
70 PG_RETURN_FLOAT8(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 */
75 PG_FUNCTION_INFO_V1(regress_path_dist);
78 regress_path_dist(PG_FUNCTION_ARGS)
80 PATH *p1 = PG_GETARG_PATH_P(0);
81 PATH *p2 = PG_GETARG_PATH_P(1);
82 bool have_min = false;
83 float8 min = 0.0; /* initialize to keep compiler quiet */
90 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 tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
99 LsegPGetDatum(&seg2)));
100 if (!have_min || tmp < min)
111 PG_RETURN_FLOAT8(min);
119 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
120 char buf[2 * (P_MAXDIG) + 20];
122 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
124 for (i = 0; i < poly->npts; i++)
126 sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
130 sprintf(buf, "%c", RDELIM);
132 return DatumGetPathP(DirectFunctionCall1(path_in,
133 CStringGetDatum(output)));
136 /* return the point where two paths intersect, or NULL if no intersection. */
137 PG_FUNCTION_INFO_V1(interpt_pp);
140 interpt_pp(PG_FUNCTION_ARGS)
142 PATH *p1 = PG_GETARG_PATH_P(0);
143 PATH *p2 = PG_GETARG_PATH_P(1);
148 bool found; /* We've found the intersection */
150 found = false; /* Haven't found it yet */
152 for (i = 0; i < p1->npts - 1 && !found; i++)
154 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
155 for (j = 0; j < p2->npts - 1 && !found; j++)
157 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
158 if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
159 LsegPGetDatum(&seg1),
160 LsegPGetDatum(&seg2))))
168 /* Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
169 * returns NULL, but that should be impossible since we know the two
170 * segments intersect.
172 PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
173 LsegPGetDatum(&seg1),
174 LsegPGetDatum(&seg2)));
178 /* like lseg_construct, but assume space already allocated */
180 regress_lseg_construct(lseg, pt1, pt2)
185 lseg->p[0].x = pt1->x;
186 lseg->p[0].y = pt1->y;
187 lseg->p[1].x = pt2->x;
188 lseg->p[1].y = pt2->y;
189 lseg->m = point_sl(pt1, pt2);
192 PG_FUNCTION_INFO_V1(overpaid);
195 overpaid(PG_FUNCTION_ARGS)
197 TUPLE tuple = (TUPLE) PG_GETARG_POINTER(0);
201 salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
204 PG_RETURN_BOOL(salary > 699);
208 * This used to be "circle", but I added circle to builtins,
209 * so needed to make sure the names do not collide. - tgl 97/04/21
218 WIDGET *widget_in(char *str);
219 char *widget_out(WIDGET * widget);
220 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
236 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
237 if (*p == ',' || (*p == LDELIM && !i))
241 result = (WIDGET *) palloc(sizeof(WIDGET));
242 result->center.x = atof(coord[0]);
243 result->center.y = atof(coord[1]);
244 result->radius = atof(coord[2]);
246 sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
247 result->center.y, result->radius);
260 result = (char *) palloc(60);
261 sprintf(result, "(%g,%g,%g)",
262 widget->center.x, widget->center.y, widget->radius);
266 PG_FUNCTION_INFO_V1(pt_in_widget);
269 pt_in_widget(PG_FUNCTION_ARGS)
271 Point *point = PG_GETARG_POINT_P(0);
272 WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
274 PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
277 #define ABS(X) ((X) >= 0 ? (X) : -(X))
279 PG_FUNCTION_INFO_V1(boxarea);
282 boxarea(PG_FUNCTION_ARGS)
284 BOX *box = PG_GETARG_BOX_P(0);
288 width = ABS(box->high.x - box->low.x);
289 height = ABS(box->high.y - box->low.y);
290 PG_RETURN_FLOAT8(width * height);
294 reverse_name(char *string)
300 if (!(new_string = palloc(NAMEDATALEN)))
302 fprintf(stderr, "reverse_name: palloc failed\n");
305 MemSet(new_string, 0, NAMEDATALEN);
306 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
308 if (i == NAMEDATALEN || !string[i])
312 new_string[len - i] = string[i];
316 /* This rather silly function is just to test that oldstyle functions
317 * work correctly on toast-able inputs.
320 oldstyle_length(int n, text *t)
325 len = VARSIZE(t) - VARHDRSZ;
330 #include "executor/spi.h" /* this is what you need to work with SPI */
331 #include "commands/trigger.h" /* -"- and triggers */
333 static TransactionId fd17b_xid = InvalidTransactionId;
334 static TransactionId fd17a_xid = InvalidTransactionId;
335 static int fd17b_level = 0;
336 static int fd17a_level = 0;
337 static bool fd17b_recursion = true;
338 static bool fd17a_recursion = true;
339 extern Datum funny_dup17(PG_FUNCTION_ARGS);
341 PG_FUNCTION_INFO_V1(funny_dup17);
344 funny_dup17(PG_FUNCTION_ARGS)
346 TriggerData *trigdata = (TriggerData *) fcinfo->context;
361 if (!CALLED_AS_TRIGGER(fcinfo))
362 elog(ERROR, "funny_dup17: not fired by trigger manager");
364 tuple = trigdata->tg_trigtuple;
365 rel = trigdata->tg_relation;
366 tupdesc = rel->rd_att;
367 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
370 level = &fd17b_level;
371 recursion = &fd17b_recursion;
377 level = &fd17a_level;
378 recursion = &fd17a_recursion;
382 if (!TransactionIdIsCurrentTransactionId(*xid))
384 *xid = GetCurrentTransactionId();
392 return PointerGetDatum(tuple);
396 return PointerGetDatum(tuple);
402 fieldval = SPI_getvalue(tuple, tupdesc, 1);
403 fieldtype = SPI_gettype(tupdesc, 1);
405 query = (char *) palloc(100 + NAMEDATALEN * 3 +
406 strlen(fieldval) + strlen(fieldtype));
408 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
409 SPI_getrelname(rel), SPI_getrelname(rel),
410 SPI_fname(tupdesc, 1),
411 fieldval, fieldtype);
413 if ((ret = SPI_exec(query, 0)) < 0)
414 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
417 inserted = SPI_processed;
419 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
421 SPI_fname(tupdesc, 1),
422 fieldval, fieldtype);
424 if ((ret = SPI_exec(query, 0)) < 0)
425 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
428 if (SPI_processed > 0)
430 selected = DatumGetInt32(DirectFunctionCall1(int4in,
431 CStringGetDatum(SPI_getvalue(
432 SPI_tuptable->vals[0],
433 SPI_tuptable->tupdesc,
438 elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
439 when, *level, inserted, selected);
446 *xid = InvalidTransactionId;
448 return PointerGetDatum(tuple);
451 extern Datum ttdummy(PG_FUNCTION_ARGS);
452 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
454 #define TTDUMMY_INFINITY 999999
456 static void *splan = NULL;
457 static bool ttoff = false;
459 PG_FUNCTION_INFO_V1(ttdummy);
462 ttdummy(PG_FUNCTION_ARGS)
464 TriggerData *trigdata = (TriggerData *) fcinfo->context;
465 Trigger *trigger; /* to get trigger name */
466 char **args; /* arguments */
467 int attnum[2]; /* fnumbers of start/stop columns */
472 Datum *cvals; /* column values */
473 char *cnulls; /* column nulls */
474 char *relname; /* triggered relation name */
475 Relation rel; /* triggered relation */
477 HeapTuple newtuple = NULL;
479 TupleDesc tupdesc; /* tuple description */
480 int natts; /* # of attributes */
481 bool isnull; /* to know is some column NULL or not */
485 if (!CALLED_AS_TRIGGER(fcinfo))
486 elog(ERROR, "ttdummy: not fired by trigger manager");
487 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
488 elog(ERROR, "ttdummy: can't process STATEMENT events");
489 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
490 elog(ERROR, "ttdummy: must be fired before event");
491 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
492 elog(ERROR, "ttdummy: can't process INSERT event");
493 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
494 newtuple = trigdata->tg_newtuple;
496 trigtuple = trigdata->tg_trigtuple;
498 rel = trigdata->tg_relation;
499 relname = SPI_getrelname(rel);
501 /* check if TT is OFF for this relation */
502 if (ttoff) /* OFF - nothing to do */
505 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
508 trigger = trigdata->tg_trigger;
510 if (trigger->tgnargs != 2)
511 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
512 relname, trigger->tgnargs);
514 args = trigger->tgargs;
515 tupdesc = rel->rd_att;
516 natts = tupdesc->natts;
518 for (i = 0; i < 2; i++)
520 attnum[i] = SPI_fnumber(tupdesc, args[i]);
522 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
523 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
524 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
525 relname, args[0], args[1]);
528 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
530 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
532 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
534 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
536 if (newtuple != NULL) /* UPDATE */
538 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
540 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
541 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
543 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
545 if (oldon != newon || oldoff != newoff)
546 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
547 relname, args[0], args[1]);
549 if (newoff != TTDUMMY_INFINITY)
551 pfree(relname); /* allocated in upper executor context */
552 return PointerGetDatum(NULL);
555 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
558 return PointerGetDatum(NULL);
562 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
563 CStringGetDatum("ttdummy_seq")));
565 newoff = DirectFunctionCall1(nextval,
566 PointerGetDatum(seqname));
570 /* Connect to SPI manager */
571 if ((ret = SPI_connect()) < 0)
572 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
574 /* Fetch tuple values and nulls */
575 cvals = (Datum *) palloc(natts * sizeof(Datum));
576 cnulls = (char *) palloc(natts * sizeof(char));
577 for (i = 0; i < natts; i++)
579 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
580 tupdesc, i + 1, &isnull);
581 cnulls[i] = (isnull) ? 'n' : ' ';
584 /* change date column(s) */
585 if (newtuple) /* UPDATE */
587 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
588 cnulls[attnum[0] - 1] = ' ';
589 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
590 cnulls[attnum[1] - 1] = ' ';
595 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
596 cnulls[attnum[1] - 1] = ' ';
599 /* if there is no plan ... */
606 /* allocate space in preparation */
607 ctypes = (Oid *) palloc(natts * sizeof(Oid));
608 query = (char *) palloc(100 + 16 * natts);
611 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
613 sprintf(query, "INSERT INTO %s VALUES (", relname);
614 for (i = 1; i <= natts; i++)
616 sprintf(query + strlen(query), "$%d%s",
617 i, (i < natts) ? ", " : ")");
618 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
621 /* Prepare plan for query */
622 pplan = SPI_prepare(query, natts, ctypes);
624 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
626 pplan = SPI_saveplan(pplan);
628 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
633 ret = SPI_execp(splan, cvals, cnulls, 0);
636 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
638 /* Tuple to return to upper Executor ... */
639 if (newtuple) /* UPDATE */
643 tmptuple = SPI_copytuple(trigtuple);
644 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
645 SPI_freetuple(tmptuple);
649 rettuple = trigtuple;
651 SPI_finish(); /* don't forget say Bye to SPI mgr */
655 return PointerGetDatum(rettuple);
658 PG_FUNCTION_INFO_V1(set_ttdummy);
661 set_ttdummy(PG_FUNCTION_ARGS)
663 int32 on = PG_GETARG_INT32(0);
665 if (ttoff) /* OFF currently */