2 * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.67 2006/05/30 21:21:30 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);
30 #ifdef PG_MODULE_MAGIC
36 * Distance from a point to a path
38 PG_FUNCTION_INFO_V1(regress_dist_ptpath);
41 regress_dist_ptpath(PG_FUNCTION_ARGS)
43 Point *pt = PG_GETARG_POINT_P(0);
44 PATH *path = PG_GETARG_PATH_P(1);
45 float8 result = 0.0; /* keep compiler quiet */
55 result = point_dt(pt, &path->p[0]);
60 * the distance from a point to a path is the smallest distance
61 * from the point to any of its constituent segments.
63 Assert(path->npts > 1);
64 for (i = 0; i < path->npts - 1; ++i)
66 regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
67 tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps,
69 LsegPGetDatum(&lseg)));
70 if (i == 0 || tmp < result)
75 PG_RETURN_FLOAT8(result);
79 * this essentially does a cartesian product of the lsegs in the
80 * two paths, and finds the min distance between any two lsegs
82 PG_FUNCTION_INFO_V1(regress_path_dist);
85 regress_path_dist(PG_FUNCTION_ARGS)
87 PATH *p1 = PG_GETARG_PATH_P(0);
88 PATH *p2 = PG_GETARG_PATH_P(1);
89 bool have_min = false;
90 float8 min = 0.0; /* initialize to keep compiler quiet */
97 for (i = 0; i < p1->npts - 1; i++)
99 for (j = 0; j < p2->npts - 1; j++)
101 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
102 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
104 tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
105 LsegPGetDatum(&seg1),
106 LsegPGetDatum(&seg2)));
107 if (!have_min || tmp < min)
118 PG_RETURN_FLOAT8(min);
122 poly2path(POLYGON *poly)
125 char *output = (char *) palloc(2 * (P_MAXDIG + 1) * poly->npts + 64);
126 char buf[2 * (P_MAXDIG) + 20];
128 sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
130 for (i = 0; i < poly->npts; i++)
132 snprintf(buf, sizeof(buf), ",%*g,%*g",
133 P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
137 snprintf(buf, sizeof(buf), "%c", RDELIM);
139 return DatumGetPathP(DirectFunctionCall1(path_in,
140 CStringGetDatum(output)));
143 /* return the point where two paths intersect, or NULL if no intersection. */
144 PG_FUNCTION_INFO_V1(interpt_pp);
147 interpt_pp(PG_FUNCTION_ARGS)
149 PATH *p1 = PG_GETARG_PATH_P(0);
150 PATH *p2 = PG_GETARG_PATH_P(1);
155 bool found; /* We've found the intersection */
157 found = false; /* Haven't found it yet */
159 for (i = 0; i < p1->npts - 1 && !found; i++)
161 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
162 for (j = 0; j < p2->npts - 1 && !found; j++)
164 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
165 if (DatumGetBool(DirectFunctionCall2(lseg_intersect,
166 LsegPGetDatum(&seg1),
167 LsegPGetDatum(&seg2))))
176 * Note: DirectFunctionCall2 will kick out an error if lseg_interpt()
177 * returns NULL, but that should be impossible since we know the two
178 * segments intersect.
180 PG_RETURN_DATUM(DirectFunctionCall2(lseg_interpt,
181 LsegPGetDatum(&seg1),
182 LsegPGetDatum(&seg2)));
186 /* like lseg_construct, but assume space already allocated */
188 regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
190 lseg->p[0].x = pt1->x;
191 lseg->p[0].y = pt1->y;
192 lseg->p[1].x = pt2->x;
193 lseg->p[1].y = pt2->y;
194 lseg->m = point_sl(pt1, pt2);
197 PG_FUNCTION_INFO_V1(overpaid);
200 overpaid(PG_FUNCTION_ARGS)
202 HeapTupleHeader tuple = PG_GETARG_HEAPTUPLEHEADER(0);
206 salary = DatumGetInt32(GetAttributeByName(tuple, "salary", &isnull));
209 PG_RETURN_BOOL(salary > 699);
213 * This used to be "circle", but I added circle to builtins,
214 * so needed to make sure the names do not collide. - tgl 97/04/21
223 WIDGET *widget_in(char *str);
224 char *widget_out(WIDGET * widget);
225 extern Datum pt_in_widget(PG_FUNCTION_ARGS);
240 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
241 if (*p == ',' || (*p == LDELIM && !i))
245 result = (WIDGET *) palloc(sizeof(WIDGET));
246 result->center.x = atof(coord[0]);
247 result->center.y = atof(coord[1]);
248 result->radius = atof(coord[2]);
250 snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
251 result->center.x, result->center.y, result->radius);
256 widget_out(WIDGET * widget)
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 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 new_string = palloc0(NAMEDATALEN);
302 for (i = 0; i < NAMEDATALEN && string[i]; ++i)
304 if (i == NAMEDATALEN || !string[i])
308 new_string[len - i] = string[i];
313 * This rather silly function is just to test that oldstyle functions
314 * work correctly on toast-able inputs.
317 oldstyle_length(int n, text *t)
322 len = VARSIZE(t) - VARHDRSZ;
327 #include "executor/spi.h" /* this is what you need to work with SPI */
328 #include "commands/trigger.h" /* -"- and triggers */
330 static TransactionId fd17b_xid = InvalidTransactionId;
331 static TransactionId fd17a_xid = InvalidTransactionId;
332 static int fd17b_level = 0;
333 static int fd17a_level = 0;
334 static bool fd17b_recursion = true;
335 static bool fd17a_recursion = true;
336 extern Datum funny_dup17(PG_FUNCTION_ARGS);
338 PG_FUNCTION_INFO_V1(funny_dup17);
341 funny_dup17(PG_FUNCTION_ARGS)
343 TriggerData *trigdata = (TriggerData *) fcinfo->context;
358 if (!CALLED_AS_TRIGGER(fcinfo))
359 elog(ERROR, "funny_dup17: not fired by trigger manager");
361 tuple = trigdata->tg_trigtuple;
362 rel = trigdata->tg_relation;
363 tupdesc = rel->rd_att;
364 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
367 level = &fd17b_level;
368 recursion = &fd17b_recursion;
374 level = &fd17a_level;
375 recursion = &fd17a_recursion;
379 if (!TransactionIdIsCurrentTransactionId(*xid))
381 *xid = GetCurrentTransactionId();
389 return PointerGetDatum(tuple);
393 return PointerGetDatum(tuple);
399 fieldval = SPI_getvalue(tuple, tupdesc, 1);
400 fieldtype = SPI_gettype(tupdesc, 1);
402 query = (char *) palloc(100 + NAMEDATALEN * 3 +
403 strlen(fieldval) + strlen(fieldtype));
405 sprintf(query, "insert into %s select * from %s where %s = '%s'::%s",
406 SPI_getrelname(rel), SPI_getrelname(rel),
407 SPI_fname(tupdesc, 1),
408 fieldval, fieldtype);
410 if ((ret = SPI_exec(query, 0)) < 0)
411 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (insert ...) returned %d",
414 inserted = SPI_processed;
416 sprintf(query, "select count (*) from %s where %s = '%s'::%s",
418 SPI_fname(tupdesc, 1),
419 fieldval, fieldtype);
421 if ((ret = SPI_exec(query, 0)) < 0)
422 elog(ERROR, "funny_dup17 (fired %s) on level %3d: SPI_exec (select ...) returned %d",
425 if (SPI_processed > 0)
427 selected = DatumGetInt32(DirectFunctionCall1(int4in,
428 CStringGetDatum(SPI_getvalue(
429 SPI_tuptable->vals[0],
430 SPI_tuptable->tupdesc,
435 elog(DEBUG4, "funny_dup17 (fired %s) on level %3d: %d/%d tuples inserted/selected",
436 when, *level, inserted, selected);
443 *xid = InvalidTransactionId;
445 return PointerGetDatum(tuple);
448 extern Datum ttdummy(PG_FUNCTION_ARGS);
449 extern Datum set_ttdummy(PG_FUNCTION_ARGS);
451 #define TTDUMMY_INFINITY 999999
453 static void *splan = NULL;
454 static bool ttoff = false;
456 PG_FUNCTION_INFO_V1(ttdummy);
459 ttdummy(PG_FUNCTION_ARGS)
461 TriggerData *trigdata = (TriggerData *) fcinfo->context;
462 Trigger *trigger; /* to get trigger name */
463 char **args; /* arguments */
464 int attnum[2]; /* fnumbers of start/stop columns */
469 Datum *cvals; /* column values */
470 char *cnulls; /* column nulls */
471 char *relname; /* triggered relation name */
472 Relation rel; /* triggered relation */
474 HeapTuple newtuple = NULL;
476 TupleDesc tupdesc; /* tuple description */
477 int natts; /* # of attributes */
478 bool isnull; /* to know is some column NULL or not */
482 if (!CALLED_AS_TRIGGER(fcinfo))
483 elog(ERROR, "ttdummy: not fired by trigger manager");
484 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
485 elog(ERROR, "ttdummy: can't process STATEMENT events");
486 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
487 elog(ERROR, "ttdummy: must be fired before event");
488 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
489 elog(ERROR, "ttdummy: can't process INSERT event");
490 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
491 newtuple = trigdata->tg_newtuple;
493 trigtuple = trigdata->tg_trigtuple;
495 rel = trigdata->tg_relation;
496 relname = SPI_getrelname(rel);
498 /* check if TT is OFF for this relation */
499 if (ttoff) /* OFF - nothing to do */
502 return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
505 trigger = trigdata->tg_trigger;
507 if (trigger->tgnargs != 2)
508 elog(ERROR, "ttdummy (%s): invalid (!= 2) number of arguments %d",
509 relname, trigger->tgnargs);
511 args = trigger->tgargs;
512 tupdesc = rel->rd_att;
513 natts = tupdesc->natts;
515 for (i = 0; i < 2; i++)
517 attnum[i] = SPI_fnumber(tupdesc, args[i]);
519 elog(ERROR, "ttdummy (%s): there is no attribute %s", relname, args[i]);
520 if (SPI_gettypeid(tupdesc, attnum[i]) != INT4OID)
521 elog(ERROR, "ttdummy (%s): attributes %s and %s must be of abstime type",
522 relname, args[0], args[1]);
525 oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
527 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
529 oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
531 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
533 if (newtuple != NULL) /* UPDATE */
535 newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
537 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[0]);
538 newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
540 elog(ERROR, "ttdummy (%s): %s must be NOT NULL", relname, args[1]);
542 if (oldon != newon || oldoff != newoff)
543 elog(ERROR, "ttdummy (%s): you can't change %s and/or %s columns (use set_ttdummy)",
544 relname, args[0], args[1]);
546 if (newoff != TTDUMMY_INFINITY)
548 pfree(relname); /* allocated in upper executor context */
549 return PointerGetDatum(NULL);
552 else if (oldoff != TTDUMMY_INFINITY) /* DELETE */
555 return PointerGetDatum(NULL);
559 text *seqname = DatumGetTextP(DirectFunctionCall1(textin,
560 CStringGetDatum("ttdummy_seq")));
562 newoff = DirectFunctionCall1(nextval,
563 PointerGetDatum(seqname));
564 /* nextval now returns int64; coerce down to int32 */
565 newoff = Int32GetDatum((int32) DatumGetInt64(newoff));
569 /* Connect to SPI manager */
570 if ((ret = SPI_connect()) < 0)
571 elog(ERROR, "ttdummy (%s): SPI_connect returned %d", relname, ret);
573 /* Fetch tuple values and nulls */
574 cvals = (Datum *) palloc(natts * sizeof(Datum));
575 cnulls = (char *) palloc(natts * sizeof(char));
576 for (i = 0; i < natts; i++)
578 cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
579 tupdesc, i + 1, &isnull);
580 cnulls[i] = (isnull) ? 'n' : ' ';
583 /* change date column(s) */
584 if (newtuple) /* UPDATE */
586 cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
587 cnulls[attnum[0] - 1] = ' ';
588 cvals[attnum[1] - 1] = TTDUMMY_INFINITY; /* stop_date eq INFINITY */
589 cnulls[attnum[1] - 1] = ' ';
594 cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
595 cnulls[attnum[1] - 1] = ' ';
598 /* if there is no plan ... */
605 /* allocate space in preparation */
606 ctypes = (Oid *) palloc(natts * sizeof(Oid));
607 query = (char *) palloc(100 + 16 * natts);
610 * Construct query: INSERT INTO _relation_ VALUES ($1, ...)
612 sprintf(query, "INSERT INTO %s VALUES (", relname);
613 for (i = 1; i <= natts; i++)
615 sprintf(query + strlen(query), "$%d%s",
616 i, (i < natts) ? ", " : ")");
617 ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
620 /* Prepare plan for query */
621 pplan = SPI_prepare(query, natts, ctypes);
623 elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
625 pplan = SPI_saveplan(pplan);
627 elog(ERROR, "ttdummy (%s): SPI_saveplan returned %d", relname, SPI_result);
632 ret = SPI_execp(splan, cvals, cnulls, 0);
635 elog(ERROR, "ttdummy (%s): SPI_execp returned %d", relname, ret);
637 /* Tuple to return to upper Executor ... */
638 if (newtuple) /* UPDATE */
642 tmptuple = SPI_copytuple(trigtuple);
643 rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
644 SPI_freetuple(tmptuple);
648 rettuple = trigtuple;
650 SPI_finish(); /* don't forget say Bye to SPI mgr */
654 return PointerGetDatum(rettuple);
657 PG_FUNCTION_INFO_V1(set_ttdummy);
660 set_ttdummy(PG_FUNCTION_ARGS)
662 int32 on = PG_GETARG_INT32(0);
664 if (ttoff) /* OFF currently */
686 * Type int44 has no real-world use, but the regression tests use it.
687 * It's a four-element vector of int4's.
691 * int44in - converts "num num ..." to internal form
693 * Note: Fills any missing positions with zeroes.
695 PG_FUNCTION_INFO_V1(int44in);
698 int44in(PG_FUNCTION_ARGS)
700 char *input_string = PG_GETARG_CSTRING(0);
701 int32 *result = (int32 *) palloc(4 * sizeof(int32));
704 i = sscanf(input_string,
713 PG_RETURN_POINTER(result);
717 * int44out - converts internal form to "num num ..."
719 PG_FUNCTION_INFO_V1(int44out);
722 int44out(PG_FUNCTION_ARGS)
724 int32 *an_array = (int32 *) PG_GETARG_POINTER(0);
725 char *result = (char *) palloc(16 * 4); /* Allow 14 digits +
731 for (i = 0; i < 4; i++)
733 pg_ltoa(an_array[i], walk);
734 while (*++walk != '\0')
739 PG_RETURN_CSTRING(result);