2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.47 2001/03/22 04:01:44 momjian Exp $
7 #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))))
169 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
170 * returns NULL, but that should be impossible since we know the two
171 * segments intersect.
173 PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
174 LsegPGetDatum(&seg1),
175 LsegPGetDatum(&seg2)));
179 /* like lseg_construct, but assume space already allocated */
181 regress_lseg_construct(lseg, pt1, pt2)
186 lseg->p[0].x = pt1->x;
187 lseg->p[0].y = pt1->y;
188 lseg->p[1].x = pt2->x;
189 lseg->p[1].y = pt2->y;
190 lseg->m = point_sl(pt1, pt2);
193 PG_FUNCTION_INFO_V1(overpaid);
196 overpaid(PG_FUNCTION_ARGS)
198 TUPLE tuple = (TUPLE) PG_GETARG_POINTER(0);
202 salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
205 PG_RETURN_BOOL(salary > 699);
209 * This used to be "circle", but I added circle to builtins,
210 * so needed to make sure the names do not collide. - tgl 97/04/21
219 WIDGET *widget_in(char *str);
220 char *widget_out(WIDGET * widget);
221 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
237 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
238 if (*p == ',' || (*p == LDELIM && !i))
242 result = (WIDGET *) palloc(sizeof(WIDGET));
243 result->center.x = atof(coord[0]);
244 result->center.y = atof(coord[1]);
245 result->radius = atof(coord[2]);
247 sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
248 result->center.y, result->radius);
261 result = (char *) palloc(60);
262 sprintf(result, "(%g,%g,%g)",
263 widget->center.x, widget->center.y, widget->radius);
267 PG_FUNCTION_INFO_V1(pt_in_widget);
270 pt_in_widget(PG_FUNCTION_ARGS)
272 Point *point = PG_GETARG_POINT_P(0);
273 WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
275 PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
278 #define ABS(X) ((X) >= 0 ? (X) : -(X))
280 PG_FUNCTION_INFO_V1(boxarea);
283 boxarea(PG_FUNCTION_ARGS)
285 BOX *box = PG_GETARG_BOX_P(0);
289 width = ABS(box->high.x - box->low.x);
290 height = ABS(box->high.y - box->low.y);
291 PG_RETURN_FLOAT8(width * height);
295 reverse_name(char *string)
301 if (!(new_string = palloc(NAMEDATALEN)))
303 fprintf(stderr, "reverse_name: palloc failed\n");
306 MemSet(new_string, 0, NAMEDATALEN);
307 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
309 if (i == NAMEDATALEN || !string[i])
313 new_string[len - i] = string[i];
317 /* This rather silly function is just to test that oldstyle functions
318 * work correctly on toast-able inputs.
321 oldstyle_length(int n, text *t)
326 len = VARSIZE(t) - VARHDRSZ;
331 #include "executor/spi.h" /* this is what you need to work with SPI */
332 #include "commands/trigger.h" /* -"- and triggers */
334 static TransactionId fd17b_xid = InvalidTransactionId;
335 static TransactionId fd17a_xid = InvalidTransactionId;
336 static int fd17b_level = 0;
337 static int fd17a_level = 0;
338 static bool fd17b_recursion = true;
339 static bool fd17a_recursion = true;
340 extern Datum funny_dup17(PG_FUNCTION_ARGS);
342 PG_FUNCTION_INFO_V1(funny_dup17);
345 funny_dup17(PG_FUNCTION_ARGS)
347 TriggerData *trigdata = (TriggerData *) fcinfo->context;
362 if (!CALLED_AS_TRIGGER(fcinfo))
363 elog(ERROR, "funny_dup17: not fired by trigger manager");
365 tuple = trigdata->tg_trigtuple;
366 rel = trigdata->tg_relation;
367 tupdesc = rel->rd_att;
368 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
371 level = &fd17b_level;
372 recursion = &fd17b_recursion;
378 level = &fd17a_level;
379 recursion = &fd17a_recursion;
383 if (!TransactionIdIsCurrentTransactionId(*xid))
385 *xid = GetCurrentTransactionId();
393 return PointerGetDatum(tuple);
397 return PointerGetDatum(tuple);
403 fieldval = SPI_getvalue(tuple, tupdesc, 1);
404 fieldtype = SPI_gettype(tupdesc, 1);
406 query = (char *) palloc(100 + NAMEDATALEN * 3 +
407 strlen(fieldval) + strlen(fieldtype));
409 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
410 SPI_getrelname(rel), SPI_getrelname(rel),
411 SPI_fname(tupdesc, 1),
412 fieldval, fieldtype);
414 if ((ret = SPI_exec(query, 0)) < 0)
415 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
418 inserted = SPI_processed;
420 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
422 SPI_fname(tupdesc, 1),
423 fieldval, fieldtype);
425 if ((ret = SPI_exec(query, 0)) < 0)
426 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
429 if (SPI_processed > 0)
431 selected = DatumGetInt32(DirectFunctionCall1(int4in,
432 CStringGetDatum(SPI_getvalue(
433 SPI_tuptable->vals[0],
434 SPI_tuptable->tupdesc,
439 elog(NOTICE, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
440 when, *level, inserted, selected);
447 *xid = InvalidTransactionId;
449 return PointerGetDatum(tuple);
452 extern Datum ttdummy(PG_FUNCTION_ARGS);
453 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
455 #define TTDUMMY_INFINITY 999999
457 static void *splan = NULL;
458 static bool ttoff = false;
460 PG_FUNCTION_INFO_V1(ttdummy);
463 ttdummy(PG_FUNCTION_ARGS)
465 TriggerData *trigdata = (TriggerData *) fcinfo->context;
466 Trigger *trigger; /* to get trigger name */
467 char **args; /* arguments */
468 int attnum[2]; /* fnumbers of start/stop columns */
473 Datum *cvals; /* column values */
474 char *cnulls; /* column nulls */
475 char *relname; /* triggered relation name */
476 Relation rel; /* triggered relation */
478 HeapTuple newtuple = NULL;
480 TupleDesc tupdesc; /* tuple description */
481 int natts; /* # of attributes */
482 bool isnull; /* to know is some column NULL or not */
486 if (!CALLED_AS_TRIGGER(fcinfo))
487 elog(ERROR, "ttdummy: not fired by trigger manager");
488 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
489 elog(ERROR, "ttdummy: can't process STATEMENT events");
490 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
491 elog(ERROR, "ttdummy: must be fired before event");
492 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
493 elog(ERROR, "ttdummy: can't process INSERT event");
494 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
495 newtuple = trigdata->tg_newtuple;
497 trigtuple = trigdata->tg_trigtuple;
499 rel = trigdata->tg_relation;
500 relname = SPI_getrelname(rel);
502 /* check if TT is OFF for this relation */
503 if (ttoff) /* OFF - nothing to do */
506 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
509 trigger = trigdata->tg_trigger;
511 if (trigger->tgnargs != 2)
512 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
513 relname, trigger->tgnargs);
515 args = trigger->tgargs;
516 tupdesc = rel->rd_att;
517 natts = tupdesc->natts;
519 for (i = 0; i < 2; i++)
521 attnum[i] = SPI_fnumber(tupdesc, args[i]);
523 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
524 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
525 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
526 relname, args[0], args[1]);
529 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
531 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
533 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
535 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
537 if (newtuple != NULL) /* UPDATE */
539 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
541 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
542 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
544 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
546 if (oldon != newon || oldoff != newoff)
547 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
548 relname, args[0], args[1]);
550 if (newoff != TTDUMMY_INFINITY)
552 pfree(relname); /* allocated in upper executor context */
553 return PointerGetDatum(NULL);
556 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
559 return PointerGetDatum(NULL);
563 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
564 CStringGetDatum("ttdummy_seq")));
566 newoff = DirectFunctionCall1(nextval,
567 PointerGetDatum(seqname));
571 /* Connect to SPI manager */
572 if ((ret = SPI_connect()) < 0)
573 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
575 /* Fetch tuple values and nulls */
576 cvals = (Datum *) palloc(natts * sizeof(Datum));
577 cnulls = (char *) palloc(natts * sizeof(char));
578 for (i = 0; i < natts; i++)
580 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
581 tupdesc, i + 1, &isnull);
582 cnulls[i] = (isnull) ? 'n' : ' ';
585 /* change date column(s) */
586 if (newtuple) /* UPDATE */
588 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
589 cnulls[attnum[0] - 1] = ' ';
590 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
591 cnulls[attnum[1] - 1] = ' ';
596 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
597 cnulls[attnum[1] - 1] = ' ';
600 /* if there is no plan ... */
607 /* allocate space in preparation */
608 ctypes = (Oid *) palloc(natts * sizeof(Oid));
609 query = (char *) palloc(100 + 16 * natts);
612 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
614 sprintf(query, "INSERT INTO %s VALUES (", relname);
615 for (i = 1; i <= natts; i++)
617 sprintf(query + strlen(query), "$%d%s",
618 i, (i < natts) ? ", " : ")");
619 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
622 /* Prepare plan for query */
623 pplan = SPI_prepare(query, natts, ctypes);
625 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
627 pplan = SPI_saveplan(pplan);
629 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
634 ret = SPI_execp(splan, cvals, cnulls, 0);
637 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
639 /* Tuple to return to upper Executor ... */
640 if (newtuple) /* UPDATE */
644 tmptuple = SPI_copytuple(trigtuple);
645 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
646 SPI_freetuple(tmptuple);
650 rettuple = trigtuple;
652 SPI_finish(); /* don't forget say Bye to SPI mgr */
656 return PointerGetDatum(rettuple);
659 PG_FUNCTION_INFO_V1(set_ttdummy);
662 set_ttdummy(PG_FUNCTION_ARGS)
664 int32 on = PG_GETARG_INT32(0);
666 if (ttoff) /* OFF currently */