2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.52 2002/11/10 07:25:14 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);
29 extern Datum int44in(PG_FUNCTION_ARGS);
30 extern Datum int44out(PG_FUNCTION_ARGS);
33 ** Distance from a point to a path
35 PG_FUNCTION_INFO_V1(regress_dist_ptpath);
38 regress_dist_ptpath(PG_FUNCTION_ARGS)
40 Point *pt = PG_GETARG_POINT_P(0);
41 PATH *path = PG_GETARG_PATH_P(1);
42 float8 result = 0.0; /* keep compiler quiet */
52 result = point_dt(pt, &path->p[0]);
57 * the distance from a point to a path is the smallest
58 * distance from the point to any of its constituent segments.
60 Assert(path->npts > 1);
61 for (i = 0; i < path->npts - 1; ++i)
63 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
64 tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps,
66 LsegPGetDatum(&lseg)));
67 if (i == 0 || tmp < result)
72 PG_RETURN_FLOAT8(result);
75 /* this essentially does a cartesian product of the lsegs in the
76 two paths, and finds the min distance between any two lsegs */
77 PG_FUNCTION_INFO_V1(regress_path_dist);
80 regress_path_dist(PG_FUNCTION_ARGS)
82 PATH *p1 = PG_GETARG_PATH_P(0);
83 PATH *p2 = PG_GETARG_PATH_P(1);
84 bool have_min = false;
85 float8 min = 0.0; /* initialize to keep compiler quiet */
92 for (i = 0; i < p1->npts - 1; i++)
94 for (j = 0; j < p2->npts - 1; j++)
96 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
97 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
99 tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
100 LsegPGetDatum(&seg1),
101 LsegPGetDatum(&seg2)));
102 if (!have_min || tmp < min)
113 PG_RETURN_FLOAT8(min);
121 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
122 char buf[2 * (P_MAXDIG) + 20];
124 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
126 for (i = 0; i < poly->npts; i++)
128 sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
132 sprintf(buf, "%c", RDELIM);
134 return DatumGetPathP(DirectFunctionCall1(path_in,
135 CStringGetDatum(output)));
138 /* return the point where two paths intersect, or NULL if no intersection. */
139 PG_FUNCTION_INFO_V1(interpt_pp);
142 interpt_pp(PG_FUNCTION_ARGS)
144 PATH *p1 = PG_GETARG_PATH_P(0);
145 PATH *p2 = PG_GETARG_PATH_P(1);
150 bool found; /* We've found the intersection */
152 found = false; /* Haven't found it yet */
154 for (i = 0; i < p1->npts - 1 && !found; i++)
156 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
157 for (j = 0; j < p2->npts - 1 && !found; j++)
159 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
160 if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
161 LsegPGetDatum(&seg1),
162 LsegPGetDatum(&seg2))))
171 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
172 * returns NULL, but that should be impossible since we know the two
173 * segments intersect.
175 PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
176 LsegPGetDatum(&seg1),
177 LsegPGetDatum(&seg2)));
181 /* like lseg_construct, but assume space already allocated */
183 regress_lseg_construct(lseg, pt1, pt2)
188 lseg->p[0].x = pt1->x;
189 lseg->p[0].y = pt1->y;
190 lseg->p[1].x = pt2->x;
191 lseg->p[1].y = pt2->y;
192 lseg->m = point_sl(pt1, pt2);
195 PG_FUNCTION_INFO_V1(overpaid);
198 overpaid(PG_FUNCTION_ARGS)
200 TUPLE tuple = (TUPLE) PG_GETARG_POINTER(0);
204 salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
207 PG_RETURN_BOOL(salary > 699);
211 * This used to be "circle", but I added circle to builtins,
212 * so needed to make sure the names do not collide. - tgl 97/04/21
221 WIDGET *widget_in(char *str);
222 char *widget_out(WIDGET * widget);
223 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
239 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
240 if (*p == ',' || (*p == LDELIM && !i))
244 result = (WIDGET *) palloc(sizeof(WIDGET));
245 result->center.x = atof(coord[0]);
246 result->center.y = atof(coord[1]);
247 result->radius = atof(coord[2]);
249 sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
250 result->center.y, result->radius);
263 result = (char *) palloc(60);
264 sprintf(result, "(%g,%g,%g)",
265 widget->center.x, widget->center.y, widget->radius);
269 PG_FUNCTION_INFO_V1(pt_in_widget);
272 pt_in_widget(PG_FUNCTION_ARGS)
274 Point *point = PG_GETARG_POINT_P(0);
275 WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
277 PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
280 #define ABS(X) ((X) >= 0 ? (X) : -(X))
282 PG_FUNCTION_INFO_V1(boxarea);
285 boxarea(PG_FUNCTION_ARGS)
287 BOX *box = PG_GETARG_BOX_P(0);
291 width = ABS(box->high.x - box->low.x);
292 height = ABS(box->high.y - box->low.y);
293 PG_RETURN_FLOAT8(width * height);
297 reverse_name(char *string)
303 if (!(new_string = palloc0(NAMEDATALEN)))
305 fprintf(stderr, "reverse_name: palloc failed\n");
308 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
310 if (i == NAMEDATALEN || !string[i])
314 new_string[len - i] = string[i];
318 /* This rather silly function is just to test that oldstyle functions
319 * work correctly on toast-able inputs.
322 oldstyle_length(int n, text *t)
327 len = VARSIZE(t) - VARHDRSZ;
332 #include "executor/spi.h" /* this is what you need to work with SPI */
333 #include "commands/trigger.h" /* -"- and triggers */
335 static TransactionId fd17b_xid = InvalidTransactionId;
336 static TransactionId fd17a_xid = InvalidTransactionId;
337 static int fd17b_level = 0;
338 static int fd17a_level = 0;
339 static bool fd17b_recursion = true;
340 static bool fd17a_recursion = true;
341 extern Datum funny_dup17(PG_FUNCTION_ARGS);
343 PG_FUNCTION_INFO_V1(funny_dup17);
346 funny_dup17(PG_FUNCTION_ARGS)
348 TriggerData *trigdata = (TriggerData *) fcinfo->context;
363 if (!CALLED_AS_TRIGGER(fcinfo))
364 elog(ERROR, "funny_dup17: not fired by trigger manager");
366 tuple = trigdata->tg_trigtuple;
367 rel = trigdata->tg_relation;
368 tupdesc = rel->rd_att;
369 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
372 level = &fd17b_level;
373 recursion = &fd17b_recursion;
379 level = &fd17a_level;
380 recursion = &fd17a_recursion;
384 if (!TransactionIdIsCurrentTransactionId(*xid))
386 *xid = GetCurrentTransactionId();
394 return PointerGetDatum(tuple);
398 return PointerGetDatum(tuple);
404 fieldval = SPI_getvalue(tuple, tupdesc, 1);
405 fieldtype = SPI_gettype(tupdesc, 1);
407 query = (char *) palloc(100 + NAMEDATALEN * 3 +
408 strlen(fieldval) + strlen(fieldtype));
410 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
411 SPI_getrelname(rel), SPI_getrelname(rel),
412 SPI_fname(tupdesc, 1),
413 fieldval, fieldtype);
415 if ((ret = SPI_exec(query, 0)) < 0)
416 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
419 inserted = SPI_processed;
421 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
423 SPI_fname(tupdesc, 1),
424 fieldval, fieldtype);
426 if ((ret = SPI_exec(query, 0)) < 0)
427 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
430 if (SPI_processed > 0)
432 selected = DatumGetInt32(DirectFunctionCall1(int4in,
433 CStringGetDatum(SPI_getvalue(
434 SPI_tuptable->vals[0],
435 SPI_tuptable->tupdesc,
440 elog(DEBUG3, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
441 when, *level, inserted, selected);
448 *xid = InvalidTransactionId;
450 return PointerGetDatum(tuple);
453 extern Datum ttdummy(PG_FUNCTION_ARGS);
454 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
456 #define TTDUMMY_INFINITY 999999
458 static void *splan = NULL;
459 static bool ttoff = false;
461 PG_FUNCTION_INFO_V1(ttdummy);
464 ttdummy(PG_FUNCTION_ARGS)
466 TriggerData *trigdata = (TriggerData *) fcinfo->context;
467 Trigger *trigger; /* to get trigger name */
468 char **args; /* arguments */
469 int attnum[2]; /* fnumbers of start/stop columns */
474 Datum *cvals; /* column values */
475 char *cnulls; /* column nulls */
476 char *relname; /* triggered relation name */
477 Relation rel; /* triggered relation */
479 HeapTuple newtuple = NULL;
481 TupleDesc tupdesc; /* tuple description */
482 int natts; /* # of attributes */
483 bool isnull; /* to know is some column NULL or not */
487 if (!CALLED_AS_TRIGGER(fcinfo))
488 elog(ERROR, "ttdummy: not fired by trigger manager");
489 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
490 elog(ERROR, "ttdummy: can't process STATEMENT events");
491 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
492 elog(ERROR, "ttdummy: must be fired before event");
493 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
494 elog(ERROR, "ttdummy: can't process INSERT event");
495 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
496 newtuple = trigdata->tg_newtuple;
498 trigtuple = trigdata->tg_trigtuple;
500 rel = trigdata->tg_relation;
501 relname = SPI_getrelname(rel);
503 /* check if TT is OFF for this relation */
504 if (ttoff) /* OFF - nothing to do */
507 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
510 trigger = trigdata->tg_trigger;
512 if (trigger->tgnargs != 2)
513 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
514 relname, trigger->tgnargs);
516 args = trigger->tgargs;
517 tupdesc = rel->rd_att;
518 natts = tupdesc->natts;
520 for (i = 0; i < 2; i++)
522 attnum[i] = SPI_fnumber(tupdesc, args[i]);
524 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
525 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
526 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
527 relname, args[0], args[1]);
530 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
532 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
534 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
536 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
538 if (newtuple != NULL) /* UPDATE */
540 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
542 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
543 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
545 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
547 if (oldon != newon || oldoff != newoff)
548 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
549 relname, args[0], args[1]);
551 if (newoff != TTDUMMY_INFINITY)
553 pfree(relname); /* allocated in upper executor context */
554 return PointerGetDatum(NULL);
557 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
560 return PointerGetDatum(NULL);
564 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
565 CStringGetDatum("ttdummy_seq")));
567 newoff = DirectFunctionCall1(nextval,
568 PointerGetDatum(seqname));
569 /* nextval now returns int64; coerce down to int32 */
570 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
574 /* Connect to SPI manager */
575 if ((ret = SPI_connect()) < 0)
576 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
578 /* Fetch tuple values and nulls */
579 cvals = (Datum *) palloc(natts * sizeof(Datum));
580 cnulls = (char *) palloc(natts * sizeof(char));
581 for (i = 0; i < natts; i++)
583 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
584 tupdesc, i + 1, &isnull);
585 cnulls[i] = (isnull) ? 'n' : ' ';
588 /* change date column(s) */
589 if (newtuple) /* UPDATE */
591 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
592 cnulls[attnum[0] - 1] = ' ';
593 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
594 cnulls[attnum[1] - 1] = ' ';
599 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
600 cnulls[attnum[1] - 1] = ' ';
603 /* if there is no plan ... */
610 /* allocate space in preparation */
611 ctypes = (Oid *) palloc(natts * sizeof(Oid));
612 query = (char *) palloc(100 + 16 * natts);
615 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
617 sprintf(query, "INSERT INTO %s VALUES (", relname);
618 for (i = 1; i <= natts; i++)
620 sprintf(query + strlen(query), "$%d%s",
621 i, (i < natts) ? ", " : ")");
622 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
625 /* Prepare plan for query */
626 pplan = SPI_prepare(query, natts, ctypes);
628 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
630 pplan = SPI_saveplan(pplan);
632 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
637 ret = SPI_execp(splan, cvals, cnulls, 0);
640 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
642 /* Tuple to return to upper Executor ... */
643 if (newtuple) /* UPDATE */
647 tmptuple = SPI_copytuple(trigtuple);
648 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
649 SPI_freetuple(tmptuple);
653 rettuple = trigtuple;
655 SPI_finish(); /* don't forget say Bye to SPI mgr */
659 return PointerGetDatum(rettuple);
662 PG_FUNCTION_INFO_V1(set_ttdummy);
665 set_ttdummy(PG_FUNCTION_ARGS)
667 int32 on = PG_GETARG_INT32(0);
669 if (ttoff) /* OFF currently */
691 * Type int44 has no real-world use, but the regression tests use it.
692 * It's a four-element vector of int4's.
696 * int44in - converts "num num ..." to internal form
698 * Note: Fills any missing positions with zeroes.
700 PG_FUNCTION_INFO_V1(int44in);
703 int44in(PG_FUNCTION_ARGS)
705 char *input_string = PG_GETARG_CSTRING(0);
706 int32 *result = (int32 *) palloc(4 * sizeof(int32));
709 i = sscanf(input_string,
718 PG_RETURN_POINTER(result);
722 * int44out - converts internal form to "num num ..."
724 PG_FUNCTION_INFO_V1(int44out);
727 int44out(PG_FUNCTION_ARGS)
729 int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
730 char *result = (char *) palloc(16 * 4); /* Allow 14 digits +
736 for (i = 0; i < 4; i++)
738 pg_ltoa(an_array[i], walk);
739 while (*++walk != '\0')
744 PG_RETURN_CSTRING(result);