2 * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.62 2004/10/21 19:28:36 tgl 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 extern Datum regress_dist_ptpath(PG_FUNCTION_ARGS);
19 extern Datum regress_path_dist(PG_FUNCTION_ARGS);
20 extern PATH *poly2path(POLYGON *poly);
21 extern Datum interpt_pp(PG_FUNCTION_ARGS);
22 extern void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
23 extern Datum overpaid(PG_FUNCTION_ARGS);
24 extern Datum boxarea(PG_FUNCTION_ARGS);
25 extern char *reverse_name(char *string);
26 extern int oldstyle_length(int n, text *t);
27 extern Datum int44in(PG_FUNCTION_ARGS);
28 extern Datum int44out(PG_FUNCTION_ARGS);
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);
74 * this essentially does a cartesian product of the lsegs in the
75 * 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);
117 poly2path(POLYGON *poly)
120 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
121 char buf[2 * (P_MAXDIG) + 20];
123 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
125 for (i = 0; i < poly->npts; i++)
127 snprintf(buf, sizeof(buf), ",%*g,%*g",
128 P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
132 snprintf(buf, sizeof(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 *lseg, Point *pt1, Point *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 HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(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);
235 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
236 if (*p == ',' || (*p == LDELIM && !i))
240 result = (WIDGET *) palloc(sizeof(WIDGET));
241 result->center.x = atof(coord[0]);
242 result->center.y = atof(coord[1]);
243 result->radius = atof(coord[2]);
245 snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
246 result->center.x, result->center.y, result->radius);
251 widget_out(WIDGET * widget)
258 result = (char *) palloc(60);
259 sprintf(result, "(%g,%g,%g)",
260 widget->center.x, widget->center.y, widget->radius);
264 PG_FUNCTION_INFO_V1(pt_in_widget);
267 pt_in_widget(PG_FUNCTION_ARGS)
269 Point *point = PG_GETARG_POINT_P(0);
270 WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1);
272 PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
275 PG_FUNCTION_INFO_V1(boxarea);
278 boxarea(PG_FUNCTION_ARGS)
280 BOX *box = PG_GETARG_BOX_P(0);
284 width = Abs(box->high.x - box->low.x);
285 height = Abs(box->high.y - box->low.y);
286 PG_RETURN_FLOAT8(width * height);
290 reverse_name(char *string)
296 new_string = palloc0(NAMEDATALEN);
297 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
299 if (i == NAMEDATALEN || !string[i])
303 new_string[len - i] = string[i];
308 * This rather silly function is just to test that oldstyle functions
309 * work correctly on toast-able inputs.
312 oldstyle_length(int n, text *t)
317 len = VARSIZE(t) - VARHDRSZ;
322 #include "executor/spi.h" /* this is what you need to work with SPI */
323 #include "commands/trigger.h" /* -"- and triggers */
325 static TransactionId fd17b_xid = InvalidTransactionId;
326 static TransactionId fd17a_xid = InvalidTransactionId;
327 static int fd17b_level = 0;
328 static int fd17a_level = 0;
329 static bool fd17b_recursion = true;
330 static bool fd17a_recursion = true;
331 extern Datum funny_dup17(PG_FUNCTION_ARGS);
333 PG_FUNCTION_INFO_V1(funny_dup17);
336 funny_dup17(PG_FUNCTION_ARGS)
338 TriggerData *trigdata = (TriggerData *) fcinfo->context;
353 if (!CALLED_AS_TRIGGER(fcinfo))
354 elog(ERROR, "funny_dup17: not fired by trigger manager");
356 tuple = trigdata->tg_trigtuple;
357 rel = trigdata->tg_relation;
358 tupdesc = rel->rd_att;
359 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
362 level = &fd17b_level;
363 recursion = &fd17b_recursion;
369 level = &fd17a_level;
370 recursion = &fd17a_recursion;
374 if (!TransactionIdIsCurrentTransactionId(*xid))
376 *xid = GetCurrentTransactionId();
384 return PointerGetDatum(tuple);
388 return PointerGetDatum(tuple);
394 fieldval = SPI_getvalue(tuple, tupdesc, 1);
395 fieldtype = SPI_gettype(tupdesc, 1);
397 query = (char *) palloc(100 + NAMEDATALEN * 3 +
398 strlen(fieldval) + strlen(fieldtype));
400 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
401 SPI_getrelname(rel), SPI_getrelname(rel),
402 SPI_fname(tupdesc, 1),
403 fieldval, fieldtype);
405 if ((ret = SPI_exec(query, 0)) < 0)
406 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
409 inserted = SPI_processed;
411 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
413 SPI_fname(tupdesc, 1),
414 fieldval, fieldtype);
416 if ((ret = SPI_exec(query, 0)) < 0)
417 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
420 if (SPI_processed > 0)
422 selected = DatumGetInt32(DirectFunctionCall1(int4in,
423 CStringGetDatum(SPI_getvalue(
424 SPI_tuptable->vals[0],
425 SPI_tuptable->tupdesc,
430 elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
431 when, *level, inserted, selected);
438 *xid = InvalidTransactionId;
440 return PointerGetDatum(tuple);
443 extern Datum ttdummy(PG_FUNCTION_ARGS);
444 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
446 #define TTDUMMY_INFINITY 999999
448 static void *splan = NULL;
449 static bool ttoff = false;
451 PG_FUNCTION_INFO_V1(ttdummy);
454 ttdummy(PG_FUNCTION_ARGS)
456 TriggerData *trigdata = (TriggerData *) fcinfo->context;
457 Trigger *trigger; /* to get trigger name */
458 char **args; /* arguments */
459 int attnum[2]; /* fnumbers of start/stop columns */
464 Datum *cvals; /* column values */
465 char *cnulls; /* column nulls */
466 char *relname; /* triggered relation name */
467 Relation rel; /* triggered relation */
469 HeapTuple newtuple = NULL;
471 TupleDesc tupdesc; /* tuple description */
472 int natts; /* # of attributes */
473 bool isnull; /* to know is some column NULL or not */
477 if (!CALLED_AS_TRIGGER(fcinfo))
478 elog(ERROR, "ttdummy: not fired by trigger manager");
479 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
480 elog(ERROR, "ttdummy: can't process STATEMENT events");
481 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
482 elog(ERROR, "ttdummy: must be fired before event");
483 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
484 elog(ERROR, "ttdummy: can't process INSERT event");
485 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
486 newtuple = trigdata->tg_newtuple;
488 trigtuple = trigdata->tg_trigtuple;
490 rel = trigdata->tg_relation;
491 relname = SPI_getrelname(rel);
493 /* check if TT is OFF for this relation */
494 if (ttoff) /* OFF - nothing to do */
497 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
500 trigger = trigdata->tg_trigger;
502 if (trigger->tgnargs != 2)
503 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
504 relname, trigger->tgnargs);
506 args = trigger->tgargs;
507 tupdesc = rel->rd_att;
508 natts = tupdesc->natts;
510 for (i = 0; i < 2; i++)
512 attnum[i] = SPI_fnumber(tupdesc, args[i]);
514 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
515 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
516 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
517 relname, args[0], args[1]);
520 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
522 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
524 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
526 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
528 if (newtuple != NULL) /* UPDATE */
530 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
532 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
533 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
535 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
537 if (oldon != newon || oldoff != newoff)
538 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
539 relname, args[0], args[1]);
541 if (newoff != TTDUMMY_INFINITY)
543 pfree(relname); /* allocated in upper executor context */
544 return PointerGetDatum(NULL);
547 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
550 return PointerGetDatum(NULL);
554 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
555 CStringGetDatum("ttdummy_seq")));
557 newoff = DirectFunctionCall1(nextval,
558 PointerGetDatum(seqname));
559 /* nextval now returns int64; coerce down to int32 */
560 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
564 /* Connect to SPI manager */
565 if ((ret = SPI_connect()) < 0)
566 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
568 /* Fetch tuple values and nulls */
569 cvals = (Datum *) palloc(natts * sizeof(Datum));
570 cnulls = (char *) palloc(natts * sizeof(char));
571 for (i = 0; i < natts; i++)
573 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
574 tupdesc, i + 1, &isnull);
575 cnulls[i] = (isnull) ? 'n' : ' ';
578 /* change date column(s) */
579 if (newtuple) /* UPDATE */
581 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
582 cnulls[attnum[0] - 1] = ' ';
583 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
584 cnulls[attnum[1] - 1] = ' ';
589 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
590 cnulls[attnum[1] - 1] = ' ';
593 /* if there is no plan ... */
600 /* allocate space in preparation */
601 ctypes = (Oid *) palloc(natts * sizeof(Oid));
602 query = (char *) palloc(100 + 16 * natts);
605 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
607 sprintf(query, "INSERT INTO %s VALUES (", relname);
608 for (i = 1; i <= natts; i++)
610 sprintf(query + strlen(query), "$%d%s",
611 i, (i < natts) ? ", " : ")");
612 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
615 /* Prepare plan for query */
616 pplan = SPI_prepare(query, natts, ctypes);
618 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
620 pplan = SPI_saveplan(pplan);
622 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
627 ret = SPI_execp(splan, cvals, cnulls, 0);
630 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
632 /* Tuple to return to upper Executor ... */
633 if (newtuple) /* UPDATE */
637 tmptuple = SPI_copytuple(trigtuple);
638 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
639 SPI_freetuple(tmptuple);
643 rettuple = trigtuple;
645 SPI_finish(); /* don't forget say Bye to SPI mgr */
649 return PointerGetDatum(rettuple);
652 PG_FUNCTION_INFO_V1(set_ttdummy);
655 set_ttdummy(PG_FUNCTION_ARGS)
657 int32 on = PG_GETARG_INT32(0);
659 if (ttoff) /* OFF currently */
681 * Type int44 has no real-world use, but the regression tests use it.
682 * It's a four-element vector of int4's.
686 * int44in - converts "num num ..." to internal form
688 * Note: Fills any missing positions with zeroes.
690 PG_FUNCTION_INFO_V1(int44in);
693 int44in(PG_FUNCTION_ARGS)
695 char *input_string = PG_GETARG_CSTRING(0);
696 int32 *result = (int32 *) palloc(4 * sizeof(int32));
699 i = sscanf(input_string,
708 PG_RETURN_POINTER(result);
712 * int44out - converts internal form to "num num ..."
714 PG_FUNCTION_INFO_V1(int44out);
717 int44out(PG_FUNCTION_ARGS)
719 int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
720 char *result = (char *) palloc(16 * 4); /* Allow 14 digits +
726 for (i = 0; i < 4; i++)
728 pg_ltoa(an_array[i], walk);
729 while (*++walk != '\0')
734 PG_RETURN_CSTRING(result);