2 * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.60 2004/04/01 21:28:47 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 #define ABS(X) ((X) >= 0 ? (X) : -(X))
277 PG_FUNCTION_INFO_V1(boxarea);
280 boxarea(PG_FUNCTION_ARGS)
282 BOX *box = PG_GETARG_BOX_P(0);
286 width = ABS(box->high.x - box->low.x);
287 height = ABS(box->high.y - box->low.y);
288 PG_RETURN_FLOAT8(width * height);
292 reverse_name(char *string)
298 new_string = palloc0(NAMEDATALEN);
299 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
301 if (i == NAMEDATALEN || !string[i])
305 new_string[len - i] = string[i];
310 * This rather silly function is just to test that oldstyle functions
311 * work correctly on toast-able inputs.
314 oldstyle_length(int n, text *t)
319 len = VARSIZE(t) - VARHDRSZ;
324 #include "executor/spi.h" /* this is what you need to work with SPI */
325 #include "commands/trigger.h" /* -"- and triggers */
327 static TransactionId fd17b_xid = InvalidTransactionId;
328 static TransactionId fd17a_xid = InvalidTransactionId;
329 static int fd17b_level = 0;
330 static int fd17a_level = 0;
331 static bool fd17b_recursion = true;
332 static bool fd17a_recursion = true;
333 extern Datum funny_dup17(PG_FUNCTION_ARGS);
335 PG_FUNCTION_INFO_V1(funny_dup17);
338 funny_dup17(PG_FUNCTION_ARGS)
340 TriggerData *trigdata = (TriggerData *) fcinfo->context;
355 if (!CALLED_AS_TRIGGER(fcinfo))
356 elog(ERROR, "funny_dup17: not fired by trigger manager");
358 tuple = trigdata->tg_trigtuple;
359 rel = trigdata->tg_relation;
360 tupdesc = rel->rd_att;
361 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
364 level = &fd17b_level;
365 recursion = &fd17b_recursion;
371 level = &fd17a_level;
372 recursion = &fd17a_recursion;
376 if (!TransactionIdIsCurrentTransactionId(*xid))
378 *xid = GetCurrentTransactionId();
386 return PointerGetDatum(tuple);
390 return PointerGetDatum(tuple);
396 fieldval = SPI_getvalue(tuple, tupdesc, 1);
397 fieldtype = SPI_gettype(tupdesc, 1);
399 query = (char *) palloc(100 + NAMEDATALEN * 3 +
400 strlen(fieldval) + strlen(fieldtype));
402 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
403 SPI_getrelname(rel), SPI_getrelname(rel),
404 SPI_fname(tupdesc, 1),
405 fieldval, fieldtype);
407 if ((ret = SPI_exec(query, 0)) < 0)
408 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
411 inserted = SPI_processed;
413 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
415 SPI_fname(tupdesc, 1),
416 fieldval, fieldtype);
418 if ((ret = SPI_exec(query, 0)) < 0)
419 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
422 if (SPI_processed > 0)
424 selected = DatumGetInt32(DirectFunctionCall1(int4in,
425 CStringGetDatum(SPI_getvalue(
426 SPI_tuptable->vals[0],
427 SPI_tuptable->tupdesc,
432 elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
433 when, *level, inserted, selected);
440 *xid = InvalidTransactionId;
442 return PointerGetDatum(tuple);
445 extern Datum ttdummy(PG_FUNCTION_ARGS);
446 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
448 #define TTDUMMY_INFINITY 999999
450 static void *splan = NULL;
451 static bool ttoff = false;
453 PG_FUNCTION_INFO_V1(ttdummy);
456 ttdummy(PG_FUNCTION_ARGS)
458 TriggerData *trigdata = (TriggerData *) fcinfo->context;
459 Trigger *trigger; /* to get trigger name */
460 char **args; /* arguments */
461 int attnum[2]; /* fnumbers of start/stop columns */
466 Datum *cvals; /* column values */
467 char *cnulls; /* column nulls */
468 char *relname; /* triggered relation name */
469 Relation rel; /* triggered relation */
471 HeapTuple newtuple = NULL;
473 TupleDesc tupdesc; /* tuple description */
474 int natts; /* # of attributes */
475 bool isnull; /* to know is some column NULL or not */
479 if (!CALLED_AS_TRIGGER(fcinfo))
480 elog(ERROR, "ttdummy: not fired by trigger manager");
481 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
482 elog(ERROR, "ttdummy: can't process STATEMENT events");
483 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
484 elog(ERROR, "ttdummy: must be fired before event");
485 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
486 elog(ERROR, "ttdummy: can't process INSERT event");
487 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
488 newtuple = trigdata->tg_newtuple;
490 trigtuple = trigdata->tg_trigtuple;
492 rel = trigdata->tg_relation;
493 relname = SPI_getrelname(rel);
495 /* check if TT is OFF for this relation */
496 if (ttoff) /* OFF - nothing to do */
499 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
502 trigger = trigdata->tg_trigger;
504 if (trigger->tgnargs != 2)
505 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
506 relname, trigger->tgnargs);
508 args = trigger->tgargs;
509 tupdesc = rel->rd_att;
510 natts = tupdesc->natts;
512 for (i = 0; i < 2; i++)
514 attnum[i] = SPI_fnumber(tupdesc, args[i]);
516 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
517 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
518 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
519 relname, args[0], args[1]);
522 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
524 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
526 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
528 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
530 if (newtuple != NULL) /* UPDATE */
532 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
534 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
535 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
537 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
539 if (oldon != newon || oldoff != newoff)
540 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
541 relname, args[0], args[1]);
543 if (newoff != TTDUMMY_INFINITY)
545 pfree(relname); /* allocated in upper executor context */
546 return PointerGetDatum(NULL);
549 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
552 return PointerGetDatum(NULL);
556 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
557 CStringGetDatum("ttdummy_seq")));
559 newoff = DirectFunctionCall1(nextval,
560 PointerGetDatum(seqname));
561 /* nextval now returns int64; coerce down to int32 */
562 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
566 /* Connect to SPI manager */
567 if ((ret = SPI_connect()) < 0)
568 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
570 /* Fetch tuple values and nulls */
571 cvals = (Datum *) palloc(natts * sizeof(Datum));
572 cnulls = (char *) palloc(natts * sizeof(char));
573 for (i = 0; i < natts; i++)
575 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
576 tupdesc, i + 1, &isnull);
577 cnulls[i] = (isnull) ? 'n' : ' ';
580 /* change date column(s) */
581 if (newtuple) /* UPDATE */
583 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
584 cnulls[attnum[0] - 1] = ' ';
585 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
586 cnulls[attnum[1] - 1] = ' ';
591 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
592 cnulls[attnum[1] - 1] = ' ';
595 /* if there is no plan ... */
602 /* allocate space in preparation */
603 ctypes = (Oid *) palloc(natts * sizeof(Oid));
604 query = (char *) palloc(100 + 16 * natts);
607 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
609 sprintf(query, "INSERT INTO %s VALUES (", relname);
610 for (i = 1; i <= natts; i++)
612 sprintf(query + strlen(query), "$%d%s",
613 i, (i < natts) ? ", " : ")");
614 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
617 /* Prepare plan for query */
618 pplan = SPI_prepare(query, natts, ctypes);
620 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
622 pplan = SPI_saveplan(pplan);
624 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
629 ret = SPI_execp(splan, cvals, cnulls, 0);
632 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
634 /* Tuple to return to upper Executor ... */
635 if (newtuple) /* UPDATE */
639 tmptuple = SPI_copytuple(trigtuple);
640 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
641 SPI_freetuple(tmptuple);
645 rettuple = trigtuple;
647 SPI_finish(); /* don't forget say Bye to SPI mgr */
651 return PointerGetDatum(rettuple);
654 PG_FUNCTION_INFO_V1(set_ttdummy);
657 set_ttdummy(PG_FUNCTION_ARGS)
659 int32 on = PG_GETARG_INT32(0);
661 if (ttoff) /* OFF currently */
683 * Type int44 has no real-world use, but the regression tests use it.
684 * It's a four-element vector of int4's.
688 * int44in - converts "num num ..." to internal form
690 * Note: Fills any missing positions with zeroes.
692 PG_FUNCTION_INFO_V1(int44in);
695 int44in(PG_FUNCTION_ARGS)
697 char *input_string = PG_GETARG_CSTRING(0);
698 int32 *result = (int32 *) palloc(4 * sizeof(int32));
701 i = sscanf(input_string,
710 PG_RETURN_POINTER(result);
714 * int44out - converts internal form to "num num ..."
716 PG_FUNCTION_INFO_V1(int44out);
719 int44out(PG_FUNCTION_ARGS)
721 int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
722 char *result = (char *) palloc(16 * 4); /* Allow 14 digits +
728 for (i = 0; i < 4; i++)
730 pg_ltoa(an_array[i], walk);
731 while (*++walk != '\0')
736 PG_RETURN_CSTRING(result);