2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.58 2003/08/04 00:43:34 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);
76 * this essentially does a cartesian product of the lsegs in the
77 * two paths, and finds the min distance between any two lsegs
79 PG_FUNCTION_INFO_V1(regress_path_dist);
82 regress_path_dist(PG_FUNCTION_ARGS)
84 PATH *p1 = PG_GETARG_PATH_P(0);
85 PATH *p2 = PG_GETARG_PATH_P(1);
86 bool have_min = false;
87 float8 min = 0.0; /* initialize to keep compiler quiet */
94 for (i = 0; i < p1->npts - 1; i++)
96 for (j = 0; j < p2->npts - 1; j++)
98 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
99 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
101 tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
102 LsegPGetDatum(&seg1),
103 LsegPGetDatum(&seg2)));
104 if (!have_min || tmp < min)
115 PG_RETURN_FLOAT8(min);
119 poly2path(POLYGON *poly)
122 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
123 char buf[2 * (P_MAXDIG) + 20];
125 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
127 for (i = 0; i < poly->npts; i++)
129 snprintf(buf, sizeof(buf), ",%*g,%*g",
130 P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
134 snprintf(buf, sizeof(buf), "%c", RDELIM);
136 return DatumGetPathP(DirectFunctionCall1(path_in,
137 CStringGetDatum(output)));
140 /* return the point where two paths intersect, or NULL if no intersection. */
141 PG_FUNCTION_INFO_V1(interpt_pp);
144 interpt_pp(PG_FUNCTION_ARGS)
146 PATH *p1 = PG_GETARG_PATH_P(0);
147 PATH *p2 = PG_GETARG_PATH_P(1);
152 bool found; /* We've found the intersection */
154 found = false; /* Haven't found it yet */
156 for (i = 0; i < p1->npts - 1 && !found; i++)
158 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
159 for (j = 0; j < p2->npts - 1 && !found; j++)
161 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
162 if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
163 LsegPGetDatum(&seg1),
164 LsegPGetDatum(&seg2))))
173 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
174 * returns NULL, but that should be impossible since we know the two
175 * segments intersect.
177 PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
178 LsegPGetDatum(&seg1),
179 LsegPGetDatum(&seg2)));
183 /* like lseg_construct, but assume space already allocated */
185 regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
187 lseg->p[0].x = pt1->x;
188 lseg->p[0].y = pt1->y;
189 lseg->p[1].x = pt2->x;
190 lseg->p[1].y = pt2->y;
191 lseg->m = point_sl(pt1, pt2);
194 PG_FUNCTION_INFO_V1(overpaid);
197 overpaid(PG_FUNCTION_ARGS)
199 TUPLE tuple = (TUPLE) PG_GETARG_POINTER(0);
203 salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
206 PG_RETURN_BOOL(salary > 699);
210 * This used to be "circle", but I added circle to builtins,
211 * so needed to make sure the names do not collide. - tgl 97/04/21
220 WIDGET *widget_in(char *str);
221 char *widget_out(WIDGET * widget);
222 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 snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
248 result->center.x, result->center.y, result->radius);
253 widget_out(WIDGET * widget)
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 new_string = palloc0(NAMEDATALEN);
301 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
303 if (i == NAMEDATALEN || !string[i])
307 new_string[len - i] = string[i];
312 * This rather silly function is just to test that oldstyle functions
313 * work correctly on toast-able inputs.
316 oldstyle_length(int n, text *t)
321 len = VARSIZE(t) - VARHDRSZ;
326 #include "executor/spi.h" /* this is what you need to work with SPI */
327 #include "commands/trigger.h" /* -"- and triggers */
329 static TransactionId fd17b_xid = InvalidTransactionId;
330 static TransactionId fd17a_xid = InvalidTransactionId;
331 static int fd17b_level = 0;
332 static int fd17a_level = 0;
333 static bool fd17b_recursion = true;
334 static bool fd17a_recursion = true;
335 extern Datum funny_dup17(PG_FUNCTION_ARGS);
337 PG_FUNCTION_INFO_V1(funny_dup17);
340 funny_dup17(PG_FUNCTION_ARGS)
342 TriggerData *trigdata = (TriggerData *) fcinfo->context;
357 if (!CALLED_AS_TRIGGER(fcinfo))
358 elog(ERROR, "funny_dup17: not fired by trigger manager");
360 tuple = trigdata->tg_trigtuple;
361 rel = trigdata->tg_relation;
362 tupdesc = rel->rd_att;
363 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
366 level = &fd17b_level;
367 recursion = &fd17b_recursion;
373 level = &fd17a_level;
374 recursion = &fd17a_recursion;
378 if (!TransactionIdIsCurrentTransactionId(*xid))
380 *xid = GetCurrentTransactionId();
388 return PointerGetDatum(tuple);
392 return PointerGetDatum(tuple);
398 fieldval = SPI_getvalue(tuple, tupdesc, 1);
399 fieldtype = SPI_gettype(tupdesc, 1);
401 query = (char *) palloc(100 + NAMEDATALEN * 3 +
402 strlen(fieldval) + strlen(fieldtype));
404 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
405 SPI_getrelname(rel), SPI_getrelname(rel),
406 SPI_fname(tupdesc, 1),
407 fieldval, fieldtype);
409 if ((ret = SPI_exec(query, 0)) < 0)
410 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
413 inserted = SPI_processed;
415 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
417 SPI_fname(tupdesc, 1),
418 fieldval, fieldtype);
420 if ((ret = SPI_exec(query, 0)) < 0)
421 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
424 if (SPI_processed > 0)
426 selected = DatumGetInt32(DirectFunctionCall1(int4in,
427 CStringGetDatum(SPI_getvalue(
428 SPI_tuptable->vals[0],
429 SPI_tuptable->tupdesc,
434 elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
435 when, *level, inserted, selected);
442 *xid = InvalidTransactionId;
444 return PointerGetDatum(tuple);
447 extern Datum ttdummy(PG_FUNCTION_ARGS);
448 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
450 #define TTDUMMY_INFINITY 999999
452 static void *splan = NULL;
453 static bool ttoff = false;
455 PG_FUNCTION_INFO_V1(ttdummy);
458 ttdummy(PG_FUNCTION_ARGS)
460 TriggerData *trigdata = (TriggerData *) fcinfo->context;
461 Trigger *trigger; /* to get trigger name */
462 char **args; /* arguments */
463 int attnum[2]; /* fnumbers of start/stop columns */
468 Datum *cvals; /* column values */
469 char *cnulls; /* column nulls */
470 char *relname; /* triggered relation name */
471 Relation rel; /* triggered relation */
473 HeapTuple newtuple = NULL;
475 TupleDesc tupdesc; /* tuple description */
476 int natts; /* # of attributes */
477 bool isnull; /* to know is some column NULL or not */
481 if (!CALLED_AS_TRIGGER(fcinfo))
482 elog(ERROR, "ttdummy: not fired by trigger manager");
483 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
484 elog(ERROR, "ttdummy: can't process STATEMENT events");
485 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
486 elog(ERROR, "ttdummy: must be fired before event");
487 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
488 elog(ERROR, "ttdummy: can't process INSERT event");
489 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
490 newtuple = trigdata->tg_newtuple;
492 trigtuple = trigdata->tg_trigtuple;
494 rel = trigdata->tg_relation;
495 relname = SPI_getrelname(rel);
497 /* check if TT is OFF for this relation */
498 if (ttoff) /* OFF - nothing to do */
501 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
504 trigger = trigdata->tg_trigger;
506 if (trigger->tgnargs != 2)
507 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
508 relname, trigger->tgnargs);
510 args = trigger->tgargs;
511 tupdesc = rel->rd_att;
512 natts = tupdesc->natts;
514 for (i = 0; i < 2; i++)
516 attnum[i] = SPI_fnumber(tupdesc, args[i]);
518 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
519 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
520 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
521 relname, args[0], args[1]);
524 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
526 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
528 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
530 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
532 if (newtuple != NULL) /* UPDATE */
534 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
536 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
537 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
539 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
541 if (oldon != newon || oldoff != newoff)
542 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
543 relname, args[0], args[1]);
545 if (newoff != TTDUMMY_INFINITY)
547 pfree(relname); /* allocated in upper executor context */
548 return PointerGetDatum(NULL);
551 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
554 return PointerGetDatum(NULL);
558 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
559 CStringGetDatum("ttdummy_seq")));
561 newoff = DirectFunctionCall1(nextval,
562 PointerGetDatum(seqname));
563 /* nextval now returns int64; coerce down to int32 */
564 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
568 /* Connect to SPI manager */
569 if ((ret = SPI_connect()) < 0)
570 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
572 /* Fetch tuple values and nulls */
573 cvals = (Datum *) palloc(natts * sizeof(Datum));
574 cnulls = (char *) palloc(natts * sizeof(char));
575 for (i = 0; i < natts; i++)
577 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
578 tupdesc, i + 1, &isnull);
579 cnulls[i] = (isnull) ? 'n' : ' ';
582 /* change date column(s) */
583 if (newtuple) /* UPDATE */
585 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
586 cnulls[attnum[0] - 1] = ' ';
587 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
588 cnulls[attnum[1] - 1] = ' ';
593 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
594 cnulls[attnum[1] - 1] = ' ';
597 /* if there is no plan ... */
604 /* allocate space in preparation */
605 ctypes = (Oid *) palloc(natts * sizeof(Oid));
606 query = (char *) palloc(100 + 16 * natts);
609 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
611 sprintf(query, "INSERT INTO %s VALUES (", relname);
612 for (i = 1; i <= natts; i++)
614 sprintf(query + strlen(query), "$%d%s",
615 i, (i < natts) ? ", " : ")");
616 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
619 /* Prepare plan for query */
620 pplan = SPI_prepare(query, natts, ctypes);
622 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
624 pplan = SPI_saveplan(pplan);
626 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
631 ret = SPI_execp(splan, cvals, cnulls, 0);
634 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
636 /* Tuple to return to upper Executor ... */
637 if (newtuple) /* UPDATE */
641 tmptuple = SPI_copytuple(trigtuple);
642 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
643 SPI_freetuple(tmptuple);
647 rettuple = trigtuple;
649 SPI_finish(); /* don't forget say Bye to SPI mgr */
653 return PointerGetDatum(rettuple);
656 PG_FUNCTION_INFO_V1(set_ttdummy);
659 set_ttdummy(PG_FUNCTION_ARGS)
661 int32 on = PG_GETARG_INT32(0);
663 if (ttoff) /* OFF currently */
685 * Type int44 has no real-world use, but the regression tests use it.
686 * It's a four-element vector of int4's.
690 * int44in - converts "num num ..." to internal form
692 * Note: Fills any missing positions with zeroes.
694 PG_FUNCTION_INFO_V1(int44in);
697 int44in(PG_FUNCTION_ARGS)
699 char *input_string = PG_GETARG_CSTRING(0);
700 int32 *result = (int32 *) palloc(4 * sizeof(int32));
703 i = sscanf(input_string,
712 PG_RETURN_POINTER(result);
716 * int44out - converts internal form to "num num ..."
718 PG_FUNCTION_INFO_V1(int44out);
721 int44out(PG_FUNCTION_ARGS)
723 int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
724 char *result = (char *) palloc(16 * 4); /* Allow 14 digits +
730 for (i = 0; i < 4; i++)
732 pg_ltoa(an_array[i], walk);
733 while (*++walk != '\0')
738 PG_RETURN_CSTRING(result);