]> granicus.if.org Git - postgresql/blob - contrib/tablefunc/tablefunc.c
Split tuple struct defs from htup.h to htup_details.h
[postgresql] / contrib / tablefunc / tablefunc.c
1 /*
2  * contrib/tablefunc/tablefunc.c
3  *
4  *
5  * tablefunc
6  *
7  * Sample to demonstrate C functions which return setof scalar
8  * and setof composite.
9  * Joe Conway <mail@joeconway.com>
10  * And contributors:
11  * Nabil Sayegh <postgresql@e-trolley.de>
12  *
13  * Copyright (c) 2002-2012, PostgreSQL Global Development Group
14  *
15  * Permission to use, copy, modify, and distribute this software and its
16  * documentation for any purpose, without fee, and without a written agreement
17  * is hereby granted, provided that the above copyright notice and this
18  * paragraph and the following two paragraphs appear in all copies.
19  *
20  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
21  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
22  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
23  * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
27  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
29  * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
30  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
31  *
32  */
33 #include "postgres.h"
34
35 #include <math.h>
36
37 #include "access/htup_details.h"
38 #include "catalog/pg_type.h"
39 #include "executor/spi.h"
40 #include "funcapi.h"
41 #include "lib/stringinfo.h"
42 #include "miscadmin.h"
43 #include "utils/builtins.h"
44
45 #include "tablefunc.h"
46
47 PG_MODULE_MAGIC;
48
49 static HTAB *load_categories_hash(char *cats_sql, MemoryContext per_query_ctx);
50 static Tuplestorestate *get_crosstab_tuplestore(char *sql,
51                                                 HTAB *crosstab_hash,
52                                                 TupleDesc tupdesc,
53                                                 MemoryContext per_query_ctx,
54                                                 bool randomAccess);
55 static void validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial);
56 static bool compatCrosstabTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
57 static bool compatConnectbyTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
58 static void get_normal_pair(float8 *x1, float8 *x2);
59 static Tuplestorestate *connectby(char *relname,
60                   char *key_fld,
61                   char *parent_key_fld,
62                   char *orderby_fld,
63                   char *branch_delim,
64                   char *start_with,
65                   int max_depth,
66                   bool show_branch,
67                   bool show_serial,
68                   MemoryContext per_query_ctx,
69                   bool randomAccess,
70                   AttInMetadata *attinmeta);
71 static Tuplestorestate *build_tuplestore_recursively(char *key_fld,
72                                                          char *parent_key_fld,
73                                                          char *relname,
74                                                          char *orderby_fld,
75                                                          char *branch_delim,
76                                                          char *start_with,
77                                                          char *branch,
78                                                          int level,
79                                                          int *serial,
80                                                          int max_depth,
81                                                          bool show_branch,
82                                                          bool show_serial,
83                                                          MemoryContext per_query_ctx,
84                                                          AttInMetadata *attinmeta,
85                                                          Tuplestorestate *tupstore);
86
87 typedef struct
88 {
89         float8          mean;                   /* mean of the distribution */
90         float8          stddev;                 /* stddev of the distribution */
91         float8          carry_val;              /* hold second generated value */
92         bool            use_carry;              /* use second generated value */
93 } normal_rand_fctx;
94
95 #define xpfree(var_) \
96         do { \
97                 if (var_ != NULL) \
98                 { \
99                         pfree(var_); \
100                         var_ = NULL; \
101                 } \
102         } while (0)
103
104 #define xpstrdup(tgtvar_, srcvar_) \
105         do { \
106                 if (srcvar_) \
107                         tgtvar_ = pstrdup(srcvar_); \
108                 else \
109                         tgtvar_ = NULL; \
110         } while (0)
111
112 #define xstreq(tgtvar_, srcvar_) \
113         (((tgtvar_ == NULL) && (srcvar_ == NULL)) || \
114          ((tgtvar_ != NULL) && (srcvar_ != NULL) && (strcmp(tgtvar_, srcvar_) == 0)))
115
116 /* sign, 10 digits, '\0' */
117 #define INT32_STRLEN    12
118
119 /* stored info for a crosstab category */
120 typedef struct crosstab_cat_desc
121 {
122         char       *catname;            /* full category name */
123         int                     attidx;                 /* zero based */
124 } crosstab_cat_desc;
125
126 #define MAX_CATNAME_LEN                 NAMEDATALEN
127 #define INIT_CATS                               64
128
129 #define crosstab_HashTableLookup(HASHTAB, CATNAME, CATDESC) \
130 do { \
131         crosstab_HashEnt *hentry; char key[MAX_CATNAME_LEN]; \
132         \
133         MemSet(key, 0, MAX_CATNAME_LEN); \
134         snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATNAME); \
135         hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
136                                                                                  key, HASH_FIND, NULL); \
137         if (hentry) \
138                 CATDESC = hentry->catdesc; \
139         else \
140                 CATDESC = NULL; \
141 } while(0)
142
143 #define crosstab_HashTableInsert(HASHTAB, CATDESC) \
144 do { \
145         crosstab_HashEnt *hentry; bool found; char key[MAX_CATNAME_LEN]; \
146         \
147         MemSet(key, 0, MAX_CATNAME_LEN); \
148         snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATDESC->catname); \
149         hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
150                                                                                  key, HASH_ENTER, &found); \
151         if (found) \
152                 ereport(ERROR, \
153                                 (errcode(ERRCODE_DUPLICATE_OBJECT), \
154                                  errmsg("duplicate category name"))); \
155         hentry->catdesc = CATDESC; \
156 } while(0)
157
158 /* hash table */
159 typedef struct crosstab_hashent
160 {
161         char            internal_catname[MAX_CATNAME_LEN];
162         crosstab_cat_desc *catdesc;
163 } crosstab_HashEnt;
164
165 /*
166  * normal_rand - return requested number of random values
167  * with a Gaussian (Normal) distribution.
168  *
169  * inputs are int numvals, float8 mean, and float8 stddev
170  * returns setof float8
171  */
172 PG_FUNCTION_INFO_V1(normal_rand);
173 Datum
174 normal_rand(PG_FUNCTION_ARGS)
175 {
176         FuncCallContext *funcctx;
177         int                     call_cntr;
178         int                     max_calls;
179         normal_rand_fctx *fctx;
180         float8          mean;
181         float8          stddev;
182         float8          carry_val;
183         bool            use_carry;
184         MemoryContext oldcontext;
185
186         /* stuff done only on the first call of the function */
187         if (SRF_IS_FIRSTCALL())
188         {
189                 /* create a function context for cross-call persistence */
190                 funcctx = SRF_FIRSTCALL_INIT();
191
192                 /*
193                  * switch to memory context appropriate for multiple function calls
194                  */
195                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
196
197                 /* total number of tuples to be returned */
198                 funcctx->max_calls = PG_GETARG_UINT32(0);
199
200                 /* allocate memory for user context */
201                 fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
202
203                 /*
204                  * Use fctx to keep track of upper and lower bounds from call to call.
205                  * It will also be used to carry over the spare value we get from the
206                  * Box-Muller algorithm so that we only actually calculate a new value
207                  * every other call.
208                  */
209                 fctx->mean = PG_GETARG_FLOAT8(1);
210                 fctx->stddev = PG_GETARG_FLOAT8(2);
211                 fctx->carry_val = 0;
212                 fctx->use_carry = false;
213
214                 funcctx->user_fctx = fctx;
215
216                 MemoryContextSwitchTo(oldcontext);
217         }
218
219         /* stuff done on every call of the function */
220         funcctx = SRF_PERCALL_SETUP();
221
222         call_cntr = funcctx->call_cntr;
223         max_calls = funcctx->max_calls;
224         fctx = funcctx->user_fctx;
225         mean = fctx->mean;
226         stddev = fctx->stddev;
227         carry_val = fctx->carry_val;
228         use_carry = fctx->use_carry;
229
230         if (call_cntr < max_calls)      /* do when there is more left to send */
231         {
232                 float8          result;
233
234                 if (use_carry)
235                 {
236                         /*
237                          * reset use_carry and use second value obtained on last pass
238                          */
239                         fctx->use_carry = false;
240                         result = carry_val;
241                 }
242                 else
243                 {
244                         float8          normval_1;
245                         float8          normval_2;
246
247                         /* Get the next two normal values */
248                         get_normal_pair(&normval_1, &normval_2);
249
250                         /* use the first */
251                         result = mean + (stddev * normval_1);
252
253                         /* and save the second */
254                         fctx->carry_val = mean + (stddev * normval_2);
255                         fctx->use_carry = true;
256                 }
257
258                 /* send the result */
259                 SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
260         }
261         else
262                 /* do when there is no more left */
263                 SRF_RETURN_DONE(funcctx);
264 }
265
266 /*
267  * get_normal_pair()
268  * Assigns normally distributed (Gaussian) values to a pair of provided
269  * parameters, with mean 0, standard deviation 1.
270  *
271  * This routine implements Algorithm P (Polar method for normal deviates)
272  * from Knuth's _The_Art_of_Computer_Programming_, Volume 2, 3rd ed., pages
273  * 122-126. Knuth cites his source as "The polar method", G. E. P. Box, M. E.
274  * Muller, and G. Marsaglia, _Annals_Math,_Stat._ 29 (1958), 610-611.
275  *
276  */
277 static void
278 get_normal_pair(float8 *x1, float8 *x2)
279 {
280         float8          u1,
281                                 u2,
282                                 v1,
283                                 v2,
284                                 s;
285
286         do
287         {
288                 u1 = (float8) random() / (float8) MAX_RANDOM_VALUE;
289                 u2 = (float8) random() / (float8) MAX_RANDOM_VALUE;
290
291                 v1 = (2.0 * u1) - 1.0;
292                 v2 = (2.0 * u2) - 1.0;
293
294                 s = v1 * v1 + v2 * v2;
295         } while (s >= 1.0);
296
297         if (s == 0)
298         {
299                 *x1 = 0;
300                 *x2 = 0;
301         }
302         else
303         {
304                 s = sqrt((-2.0 * log(s)) / s);
305                 *x1 = v1 * s;
306                 *x2 = v2 * s;
307         }
308 }
309
310 /*
311  * crosstab - create a crosstab of rowids and values columns from a
312  * SQL statement returning one rowid column, one category column,
313  * and one value column.
314  *
315  * e.g. given sql which produces:
316  *
317  *                      rowid   cat             value
318  *                      ------+-------+-------
319  *                      row1    cat1    val1
320  *                      row1    cat2    val2
321  *                      row1    cat3    val3
322  *                      row1    cat4    val4
323  *                      row2    cat1    val5
324  *                      row2    cat2    val6
325  *                      row2    cat3    val7
326  *                      row2    cat4    val8
327  *
328  * crosstab returns:
329  *                                      <===== values columns =====>
330  *                      rowid   cat1    cat2    cat3    cat4
331  *                      ------+-------+-------+-------+-------
332  *                      row1    val1    val2    val3    val4
333  *                      row2    val5    val6    val7    val8
334  *
335  * NOTES:
336  * 1. SQL result must be ordered by 1,2.
337  * 2. The number of values columns depends on the tuple description
338  *        of the function's declared return type.  The return type's columns
339  *        must match the datatypes of the SQL query's result.  The datatype
340  *        of the category column can be anything, however.
341  * 3. Missing values (i.e. not enough adjacent rows of same rowid to
342  *        fill the number of result values columns) are filled in with nulls.
343  * 4. Extra values (i.e. too many adjacent rows of same rowid to fill
344  *        the number of result values columns) are skipped.
345  * 5. Rows with all nulls in the values columns are skipped.
346  */
347 PG_FUNCTION_INFO_V1(crosstab);
348 Datum
349 crosstab(PG_FUNCTION_ARGS)
350 {
351         char       *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
352         ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
353         Tuplestorestate *tupstore;
354         TupleDesc       tupdesc;
355         int                     call_cntr;
356         int                     max_calls;
357         AttInMetadata *attinmeta;
358         SPITupleTable *spi_tuptable;
359         TupleDesc       spi_tupdesc;
360         bool            firstpass;
361         char       *lastrowid;
362         int                     i;
363         int                     num_categories;
364         MemoryContext per_query_ctx;
365         MemoryContext oldcontext;
366         int                     ret;
367         int                     proc;
368
369         /* check to see if caller supports us returning a tuplestore */
370         if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
371                 ereport(ERROR,
372                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
373                                  errmsg("set-valued function called in context that cannot accept a set")));
374         if (!(rsinfo->allowedModes & SFRM_Materialize))
375                 ereport(ERROR,
376                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
377                                  errmsg("materialize mode required, but it is not " \
378                                                 "allowed in this context")));
379
380         per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
381
382         /* Connect to SPI manager */
383         if ((ret = SPI_connect()) < 0)
384                 /* internal error */
385                 elog(ERROR, "crosstab: SPI_connect returned %d", ret);
386
387         /* Retrieve the desired rows */
388         ret = SPI_execute(sql, true, 0);
389         proc = SPI_processed;
390
391         /* If no qualifying tuples, fall out early */
392         if (ret != SPI_OK_SELECT || proc <= 0)
393         {
394                 SPI_finish();
395                 rsinfo->isDone = ExprEndResult;
396                 PG_RETURN_NULL();
397         }
398
399         spi_tuptable = SPI_tuptable;
400         spi_tupdesc = spi_tuptable->tupdesc;
401
402         /*----------
403          * The provided SQL query must always return three columns.
404          *
405          * 1. rowname
406          *      the label or identifier for each row in the final result
407          * 2. category
408          *      the label or identifier for each column in the final result
409          * 3. values
410          *      the value for each column in the final result
411          *----------
412          */
413         if (spi_tupdesc->natts != 3)
414                 ereport(ERROR,
415                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
416                                  errmsg("invalid source data SQL statement"),
417                                  errdetail("The provided SQL must return 3 "
418                                                    "columns: rowid, category, and values.")));
419
420         /* get a tuple descriptor for our result type */
421         switch (get_call_result_type(fcinfo, NULL, &tupdesc))
422         {
423                 case TYPEFUNC_COMPOSITE:
424                         /* success */
425                         break;
426                 case TYPEFUNC_RECORD:
427                         /* failed to determine actual type of RECORD */
428                         ereport(ERROR,
429                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
430                                          errmsg("function returning record called in context "
431                                                         "that cannot accept type record")));
432                         break;
433                 default:
434                         /* result type isn't composite */
435                         elog(ERROR, "return type must be a row type");
436                         break;
437         }
438
439         /*
440          * Check that return tupdesc is compatible with the data we got from SPI,
441          * at least based on number and type of attributes
442          */
443         if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
444                 ereport(ERROR,
445                                 (errcode(ERRCODE_SYNTAX_ERROR),
446                                  errmsg("return and sql tuple descriptions are " \
447                                                 "incompatible")));
448
449         /*
450          * switch to long-lived memory context
451          */
452         oldcontext = MemoryContextSwitchTo(per_query_ctx);
453
454         /* make sure we have a persistent copy of the result tupdesc */
455         tupdesc = CreateTupleDescCopy(tupdesc);
456
457         /* initialize our tuplestore in long-lived context */
458         tupstore =
459                 tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
460                                                           false, work_mem);
461
462         MemoryContextSwitchTo(oldcontext);
463
464         /*
465          * Generate attribute metadata needed later to produce tuples from raw C
466          * strings
467          */
468         attinmeta = TupleDescGetAttInMetadata(tupdesc);
469
470         /* total number of tuples to be examined */
471         max_calls = proc;
472
473         /* the return tuple always must have 1 rowid + num_categories columns */
474         num_categories = tupdesc->natts - 1;
475
476         firstpass = true;
477         lastrowid = NULL;
478
479         for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
480         {
481                 bool            skip_tuple = false;
482                 char      **values;
483
484                 /* allocate and zero space */
485                 values = (char **) palloc0((1 + num_categories) * sizeof(char *));
486
487                 /*
488                  * now loop through the sql results and assign each value in sequence
489                  * to the next category
490                  */
491                 for (i = 0; i < num_categories; i++)
492                 {
493                         HeapTuple       spi_tuple;
494                         char       *rowid;
495
496                         /* see if we've gone too far already */
497                         if (call_cntr >= max_calls)
498                                 break;
499
500                         /* get the next sql result tuple */
501                         spi_tuple = spi_tuptable->vals[call_cntr];
502
503                         /* get the rowid from the current sql result tuple */
504                         rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
505
506                         /*
507                          * If this is the first pass through the values for this rowid,
508                          * set the first column to rowid
509                          */
510                         if (i == 0)
511                         {
512                                 xpstrdup(values[0], rowid);
513
514                                 /*
515                                  * Check to see if the rowid is the same as that of the last
516                                  * tuple sent -- if so, skip this tuple entirely
517                                  */
518                                 if (!firstpass && xstreq(lastrowid, rowid))
519                                 {
520                                         xpfree(rowid);
521                                         skip_tuple = true;
522                                         break;
523                                 }
524                         }
525
526                         /*
527                          * If rowid hasn't changed on us, continue building the output
528                          * tuple.
529                          */
530                         if (xstreq(rowid, values[0]))
531                         {
532                                 /*
533                                  * Get the next category item value, which is always attribute
534                                  * number three.
535                                  *
536                                  * Be careful to assign the value to the array index based on
537                                  * which category we are presently processing.
538                                  */
539                                 values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
540
541                                 /*
542                                  * increment the counter since we consume a row for each
543                                  * category, but not for last pass because the outer loop will
544                                  * do that for us
545                                  */
546                                 if (i < (num_categories - 1))
547                                         call_cntr++;
548                                 xpfree(rowid);
549                         }
550                         else
551                         {
552                                 /*
553                                  * We'll fill in NULLs for the missing values, but we need to
554                                  * decrement the counter since this sql result row doesn't
555                                  * belong to the current output tuple.
556                                  */
557                                 call_cntr--;
558                                 xpfree(rowid);
559                                 break;
560                         }
561                 }
562
563                 if (!skip_tuple)
564                 {
565                         HeapTuple       tuple;
566
567                         /* build the tuple and store it */
568                         tuple = BuildTupleFromCStrings(attinmeta, values);
569                         tuplestore_puttuple(tupstore, tuple);
570                         heap_freetuple(tuple);
571                 }
572
573                 /* Remember current rowid */
574                 xpfree(lastrowid);
575                 xpstrdup(lastrowid, values[0]);
576                 firstpass = false;
577
578                 /* Clean up */
579                 for (i = 0; i < num_categories + 1; i++)
580                         if (values[i] != NULL)
581                                 pfree(values[i]);
582                 pfree(values);
583         }
584
585         /* let the caller know we're sending back a tuplestore */
586         rsinfo->returnMode = SFRM_Materialize;
587         rsinfo->setResult = tupstore;
588         rsinfo->setDesc = tupdesc;
589
590         /* release SPI related resources (and return to caller's context) */
591         SPI_finish();
592
593         return (Datum) 0;
594 }
595
596 /*
597  * crosstab_hash - reimplement crosstab as materialized function and
598  * properly deal with missing values (i.e. don't pack remaining
599  * values to the left)
600  *
601  * crosstab - create a crosstab of rowids and values columns from a
602  * SQL statement returning one rowid column, one category column,
603  * and one value column.
604  *
605  * e.g. given sql which produces:
606  *
607  *                      rowid   cat             value
608  *                      ------+-------+-------
609  *                      row1    cat1    val1
610  *                      row1    cat2    val2
611  *                      row1    cat4    val4
612  *                      row2    cat1    val5
613  *                      row2    cat2    val6
614  *                      row2    cat3    val7
615  *                      row2    cat4    val8
616  *
617  * crosstab returns:
618  *                                      <===== values columns =====>
619  *                      rowid   cat1    cat2    cat3    cat4
620  *                      ------+-------+-------+-------+-------
621  *                      row1    val1    val2    null    val4
622  *                      row2    val5    val6    val7    val8
623  *
624  * NOTES:
625  * 1. SQL result must be ordered by 1.
626  * 2. The number of values columns depends on the tuple description
627  *        of the function's declared return type.
628  * 3. Missing values (i.e. missing category) are filled in with nulls.
629  * 4. Extra values (i.e. not in category results) are skipped.
630  */
631 PG_FUNCTION_INFO_V1(crosstab_hash);
632 Datum
633 crosstab_hash(PG_FUNCTION_ARGS)
634 {
635         char       *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
636         char       *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
637         ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
638         TupleDesc       tupdesc;
639         MemoryContext per_query_ctx;
640         MemoryContext oldcontext;
641         HTAB       *crosstab_hash;
642
643         /* check to see if caller supports us returning a tuplestore */
644         if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
645                 ereport(ERROR,
646                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
647                                  errmsg("set-valued function called in context that cannot accept a set")));
648         if (!(rsinfo->allowedModes & SFRM_Materialize) ||
649                 rsinfo->expectedDesc == NULL)
650                 ereport(ERROR,
651                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
652                                  errmsg("materialize mode required, but it is not " \
653                                                 "allowed in this context")));
654
655         per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
656         oldcontext = MemoryContextSwitchTo(per_query_ctx);
657
658         /* get the requested return tuple description */
659         tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
660
661         /*
662          * Check to make sure we have a reasonable tuple descriptor
663          *
664          * Note we will attempt to coerce the values into whatever the return
665          * attribute type is and depend on the "in" function to complain if
666          * needed.
667          */
668         if (tupdesc->natts < 2)
669                 ereport(ERROR,
670                                 (errcode(ERRCODE_SYNTAX_ERROR),
671                                  errmsg("query-specified return tuple and " \
672                                                 "crosstab function are not compatible")));
673
674         /* load up the categories hash table */
675         crosstab_hash = load_categories_hash(cats_sql, per_query_ctx);
676
677         /* let the caller know we're sending back a tuplestore */
678         rsinfo->returnMode = SFRM_Materialize;
679
680         /* now go build it */
681         rsinfo->setResult = get_crosstab_tuplestore(sql,
682                                                                                                 crosstab_hash,
683                                                                                                 tupdesc,
684                                                                                                 per_query_ctx,
685                                                          rsinfo->allowedModes & SFRM_Materialize_Random);
686
687         /*
688          * SFRM_Materialize mode expects us to return a NULL Datum. The actual
689          * tuples are in our tuplestore and passed back through rsinfo->setResult.
690          * rsinfo->setDesc is set to the tuple description that we actually used
691          * to build our tuples with, so the caller can verify we did what it was
692          * expecting.
693          */
694         rsinfo->setDesc = tupdesc;
695         MemoryContextSwitchTo(oldcontext);
696
697         return (Datum) 0;
698 }
699
700 /*
701  * load up the categories hash table
702  */
703 static HTAB *
704 load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
705 {
706         HTAB       *crosstab_hash;
707         HASHCTL         ctl;
708         int                     ret;
709         int                     proc;
710         MemoryContext SPIcontext;
711
712         /* initialize the category hash table */
713         MemSet(&ctl, 0, sizeof(ctl));
714         ctl.keysize = MAX_CATNAME_LEN;
715         ctl.entrysize = sizeof(crosstab_HashEnt);
716         ctl.hcxt = per_query_ctx;
717
718         /*
719          * use INIT_CATS, defined above as a guess of how many hash table entries
720          * to create, initially
721          */
722         crosstab_hash = hash_create("crosstab hash",
723                                                                 INIT_CATS,
724                                                                 &ctl,
725                                                                 HASH_ELEM | HASH_CONTEXT);
726
727         /* Connect to SPI manager */
728         if ((ret = SPI_connect()) < 0)
729                 /* internal error */
730                 elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
731
732         /* Retrieve the category name rows */
733         ret = SPI_execute(cats_sql, true, 0);
734         proc = SPI_processed;
735
736         /* Check for qualifying tuples */
737         if ((ret == SPI_OK_SELECT) && (proc > 0))
738         {
739                 SPITupleTable *spi_tuptable = SPI_tuptable;
740                 TupleDesc       spi_tupdesc = spi_tuptable->tupdesc;
741                 int                     i;
742
743                 /*
744                  * The provided categories SQL query must always return one column:
745                  * category - the label or identifier for each column
746                  */
747                 if (spi_tupdesc->natts != 1)
748                         ereport(ERROR,
749                                         (errcode(ERRCODE_SYNTAX_ERROR),
750                                          errmsg("provided \"categories\" SQL must " \
751                                                         "return 1 column of at least one row")));
752
753                 for (i = 0; i < proc; i++)
754                 {
755                         crosstab_cat_desc *catdesc;
756                         char       *catname;
757                         HeapTuple       spi_tuple;
758
759                         /* get the next sql result tuple */
760                         spi_tuple = spi_tuptable->vals[i];
761
762                         /* get the category from the current sql result tuple */
763                         catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
764
765                         SPIcontext = MemoryContextSwitchTo(per_query_ctx);
766
767                         catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
768                         catdesc->catname = catname;
769                         catdesc->attidx = i;
770
771                         /* Add the proc description block to the hashtable */
772                         crosstab_HashTableInsert(crosstab_hash, catdesc);
773
774                         MemoryContextSwitchTo(SPIcontext);
775                 }
776         }
777
778         if (SPI_finish() != SPI_OK_FINISH)
779                 /* internal error */
780                 elog(ERROR, "load_categories_hash: SPI_finish() failed");
781
782         return crosstab_hash;
783 }
784
785 /*
786  * create and populate the crosstab tuplestore using the provided source query
787  */
788 static Tuplestorestate *
789 get_crosstab_tuplestore(char *sql,
790                                                 HTAB *crosstab_hash,
791                                                 TupleDesc tupdesc,
792                                                 MemoryContext per_query_ctx,
793                                                 bool randomAccess)
794 {
795         Tuplestorestate *tupstore;
796         int                     num_categories = hash_get_num_entries(crosstab_hash);
797         AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
798         char      **values;
799         HeapTuple       tuple;
800         int                     ret;
801         int                     proc;
802
803         /* initialize our tuplestore (while still in query context!) */
804         tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
805
806         /* Connect to SPI manager */
807         if ((ret = SPI_connect()) < 0)
808                 /* internal error */
809                 elog(ERROR, "get_crosstab_tuplestore: SPI_connect returned %d", ret);
810
811         /* Now retrieve the crosstab source rows */
812         ret = SPI_execute(sql, true, 0);
813         proc = SPI_processed;
814
815         /* Check for qualifying tuples */
816         if ((ret == SPI_OK_SELECT) && (proc > 0))
817         {
818                 SPITupleTable *spi_tuptable = SPI_tuptable;
819                 TupleDesc       spi_tupdesc = spi_tuptable->tupdesc;
820                 int                     ncols = spi_tupdesc->natts;
821                 char       *rowid;
822                 char       *lastrowid = NULL;
823                 bool            firstpass = true;
824                 int                     i,
825                                         j;
826                 int                     result_ncols;
827
828                 if (num_categories == 0)
829                 {
830                         /* no qualifying category tuples */
831                         ereport(ERROR,
832                                         (errcode(ERRCODE_SYNTAX_ERROR),
833                                          errmsg("provided \"categories\" SQL must " \
834                                                         "return 1 column of at least one row")));
835                 }
836
837                 /*
838                  * The provided SQL query must always return at least three columns:
839                  *
840                  * 1. rowname   the label for each row - column 1 in the final result
841                  * 2. category  the label for each value-column in the final result 3.
842                  * value         the values used to populate the value-columns
843                  *
844                  * If there are more than three columns, the last two are taken as
845                  * "category" and "values". The first column is taken as "rowname".
846                  * Additional columns (2 thru N-2) are assumed the same for the same
847                  * "rowname", and are copied into the result tuple from the first time
848                  * we encounter a particular rowname.
849                  */
850                 if (ncols < 3)
851                         ereport(ERROR,
852                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
853                                          errmsg("invalid source data SQL statement"),
854                                          errdetail("The provided SQL must return 3 " \
855                                                            " columns; rowid, category, and values.")));
856
857                 result_ncols = (ncols - 2) + num_categories;
858
859                 /* Recheck to make sure we tuple descriptor still looks reasonable */
860                 if (tupdesc->natts != result_ncols)
861                         ereport(ERROR,
862                                         (errcode(ERRCODE_SYNTAX_ERROR),
863                                          errmsg("invalid return type"),
864                                          errdetail("Query-specified return " \
865                                                            "tuple has %d columns but crosstab " \
866                                                            "returns %d.", tupdesc->natts, result_ncols)));
867
868                 /* allocate space */
869                 values = (char **) palloc(result_ncols * sizeof(char *));
870
871                 /* and make sure it's clear */
872                 memset(values, '\0', result_ncols * sizeof(char *));
873
874                 for (i = 0; i < proc; i++)
875                 {
876                         HeapTuple       spi_tuple;
877                         crosstab_cat_desc *catdesc;
878                         char       *catname;
879
880                         /* get the next sql result tuple */
881                         spi_tuple = spi_tuptable->vals[i];
882
883                         /* get the rowid from the current sql result tuple */
884                         rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
885
886                         /*
887                          * if we're on a new output row, grab the column values up to
888                          * column N-2 now
889                          */
890                         if (firstpass || !xstreq(lastrowid, rowid))
891                         {
892                                 /*
893                                  * a new row means we need to flush the old one first, unless
894                                  * we're on the very first row
895                                  */
896                                 if (!firstpass)
897                                 {
898                                         /* rowid changed, flush the previous output row */
899                                         tuple = BuildTupleFromCStrings(attinmeta, values);
900
901                                         tuplestore_puttuple(tupstore, tuple);
902
903                                         for (j = 0; j < result_ncols; j++)
904                                                 xpfree(values[j]);
905                                 }
906
907                                 values[0] = rowid;
908                                 for (j = 1; j < ncols - 2; j++)
909                                         values[j] = SPI_getvalue(spi_tuple, spi_tupdesc, j + 1);
910
911                                 /* we're no longer on the first pass */
912                                 firstpass = false;
913                         }
914
915                         /* look up the category and fill in the appropriate column */
916                         catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
917
918                         if (catname != NULL)
919                         {
920                                 crosstab_HashTableLookup(crosstab_hash, catname, catdesc);
921
922                                 if (catdesc)
923                                         values[catdesc->attidx + ncols - 2] =
924                                                 SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
925                         }
926
927                         xpfree(lastrowid);
928                         xpstrdup(lastrowid, rowid);
929                 }
930
931                 /* flush the last output row */
932                 tuple = BuildTupleFromCStrings(attinmeta, values);
933
934                 tuplestore_puttuple(tupstore, tuple);
935         }
936
937         if (SPI_finish() != SPI_OK_FINISH)
938                 /* internal error */
939                 elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
940
941         tuplestore_donestoring(tupstore);
942
943         return tupstore;
944 }
945
946 /*
947  * connectby_text - produce a result set from a hierarchical (parent/child)
948  * table.
949  *
950  * e.g. given table foo:
951  *
952  *                      keyid   parent_keyid pos
953  *                      ------+------------+--
954  *                      row1    NULL             0
955  *                      row2    row1             0
956  *                      row3    row1             0
957  *                      row4    row2             1
958  *                      row5    row2             0
959  *                      row6    row4             0
960  *                      row7    row3             0
961  *                      row8    row6             0
962  *                      row9    row5             0
963  *
964  *
965  * connectby(text relname, text keyid_fld, text parent_keyid_fld
966  *                        [, text orderby_fld], text start_with, int max_depth
967  *                        [, text branch_delim])
968  * connectby('foo', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') returns:
969  *
970  *              keyid   parent_id       level    branch                         serial
971  *              ------+-----------+--------+-----------------------
972  *              row2    NULL              0               row2                            1
973  *              row5    row2              1               row2~row5                       2
974  *              row9    row5              2               row2~row5~row9          3
975  *              row4    row2              1               row2~row4                       4
976  *              row6    row4              2               row2~row4~row6          5
977  *              row8    row6              3               row2~row4~row6~row8 6
978  *
979  */
980 PG_FUNCTION_INFO_V1(connectby_text);
981
982 #define CONNECTBY_NCOLS                                 4
983 #define CONNECTBY_NCOLS_NOBRANCH                3
984
985 Datum
986 connectby_text(PG_FUNCTION_ARGS)
987 {
988         char       *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
989         char       *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
990         char       *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
991         char       *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3));
992         int                     max_depth = PG_GETARG_INT32(4);
993         char       *branch_delim = NULL;
994         bool            show_branch = false;
995         bool            show_serial = false;
996         ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
997         TupleDesc       tupdesc;
998         AttInMetadata *attinmeta;
999         MemoryContext per_query_ctx;
1000         MemoryContext oldcontext;
1001
1002         /* check to see if caller supports us returning a tuplestore */
1003         if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1004                 ereport(ERROR,
1005                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1006                                  errmsg("set-valued function called in context that cannot accept a set")));
1007         if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1008                 rsinfo->expectedDesc == NULL)
1009                 ereport(ERROR,
1010                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1011                                  errmsg("materialize mode required, but it is not " \
1012                                                 "allowed in this context")));
1013
1014         if (fcinfo->nargs == 6)
1015         {
1016                 branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5));
1017                 show_branch = true;
1018         }
1019         else
1020                 /* default is no show, tilde for the delimiter */
1021                 branch_delim = pstrdup("~");
1022
1023         per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1024         oldcontext = MemoryContextSwitchTo(per_query_ctx);
1025
1026         /* get the requested return tuple description */
1027         tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1028
1029         /* does it meet our needs */
1030         validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1031
1032         /* OK, use it then */
1033         attinmeta = TupleDescGetAttInMetadata(tupdesc);
1034
1035         /* OK, go to work */
1036         rsinfo->returnMode = SFRM_Materialize;
1037         rsinfo->setResult = connectby(relname,
1038                                                                   key_fld,
1039                                                                   parent_key_fld,
1040                                                                   NULL,
1041                                                                   branch_delim,
1042                                                                   start_with,
1043                                                                   max_depth,
1044                                                                   show_branch,
1045                                                                   show_serial,
1046                                                                   per_query_ctx,
1047                                                           rsinfo->allowedModes & SFRM_Materialize_Random,
1048                                                                   attinmeta);
1049         rsinfo->setDesc = tupdesc;
1050
1051         MemoryContextSwitchTo(oldcontext);
1052
1053         /*
1054          * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1055          * tuples are in our tuplestore and passed back through rsinfo->setResult.
1056          * rsinfo->setDesc is set to the tuple description that we actually used
1057          * to build our tuples with, so the caller can verify we did what it was
1058          * expecting.
1059          */
1060         return (Datum) 0;
1061 }
1062
1063 PG_FUNCTION_INFO_V1(connectby_text_serial);
1064 Datum
1065 connectby_text_serial(PG_FUNCTION_ARGS)
1066 {
1067         char       *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
1068         char       *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
1069         char       *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
1070         char       *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3));
1071         char       *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4));
1072         int                     max_depth = PG_GETARG_INT32(5);
1073         char       *branch_delim = NULL;
1074         bool            show_branch = false;
1075         bool            show_serial = true;
1076         ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1077         TupleDesc       tupdesc;
1078         AttInMetadata *attinmeta;
1079         MemoryContext per_query_ctx;
1080         MemoryContext oldcontext;
1081
1082         /* check to see if caller supports us returning a tuplestore */
1083         if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1084                 ereport(ERROR,
1085                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1086                                  errmsg("set-valued function called in context that cannot accept a set")));
1087         if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1088                 rsinfo->expectedDesc == NULL)
1089                 ereport(ERROR,
1090                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1091                                  errmsg("materialize mode required, but it is not " \
1092                                                 "allowed in this context")));
1093
1094         if (fcinfo->nargs == 7)
1095         {
1096                 branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6));
1097                 show_branch = true;
1098         }
1099         else
1100                 /* default is no show, tilde for the delimiter */
1101                 branch_delim = pstrdup("~");
1102
1103         per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1104         oldcontext = MemoryContextSwitchTo(per_query_ctx);
1105
1106         /* get the requested return tuple description */
1107         tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1108
1109         /* does it meet our needs */
1110         validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1111
1112         /* OK, use it then */
1113         attinmeta = TupleDescGetAttInMetadata(tupdesc);
1114
1115         /* OK, go to work */
1116         rsinfo->returnMode = SFRM_Materialize;
1117         rsinfo->setResult = connectby(relname,
1118                                                                   key_fld,
1119                                                                   parent_key_fld,
1120                                                                   orderby_fld,
1121                                                                   branch_delim,
1122                                                                   start_with,
1123                                                                   max_depth,
1124                                                                   show_branch,
1125                                                                   show_serial,
1126                                                                   per_query_ctx,
1127                                                           rsinfo->allowedModes & SFRM_Materialize_Random,
1128                                                                   attinmeta);
1129         rsinfo->setDesc = tupdesc;
1130
1131         MemoryContextSwitchTo(oldcontext);
1132
1133         /*
1134          * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1135          * tuples are in our tuplestore and passed back through rsinfo->setResult.
1136          * rsinfo->setDesc is set to the tuple description that we actually used
1137          * to build our tuples with, so the caller can verify we did what it was
1138          * expecting.
1139          */
1140         return (Datum) 0;
1141 }
1142
1143
1144 /*
1145  * connectby - does the real work for connectby_text()
1146  */
1147 static Tuplestorestate *
1148 connectby(char *relname,
1149                   char *key_fld,
1150                   char *parent_key_fld,
1151                   char *orderby_fld,
1152                   char *branch_delim,
1153                   char *start_with,
1154                   int max_depth,
1155                   bool show_branch,
1156                   bool show_serial,
1157                   MemoryContext per_query_ctx,
1158                   bool randomAccess,
1159                   AttInMetadata *attinmeta)
1160 {
1161         Tuplestorestate *tupstore = NULL;
1162         int                     ret;
1163         MemoryContext oldcontext;
1164
1165         int                     serial = 1;
1166
1167         /* Connect to SPI manager */
1168         if ((ret = SPI_connect()) < 0)
1169                 /* internal error */
1170                 elog(ERROR, "connectby: SPI_connect returned %d", ret);
1171
1172         /* switch to longer term context to create the tuple store */
1173         oldcontext = MemoryContextSwitchTo(per_query_ctx);
1174
1175         /* initialize our tuplestore */
1176         tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1177
1178         MemoryContextSwitchTo(oldcontext);
1179
1180         /* now go get the whole tree */
1181         tupstore = build_tuplestore_recursively(key_fld,
1182                                                                                         parent_key_fld,
1183                                                                                         relname,
1184                                                                                         orderby_fld,
1185                                                                                         branch_delim,
1186                                                                                         start_with,
1187                                                                                         start_with, /* current_branch */
1188                                                                                         0,      /* initial level is 0 */
1189                                                                                         &serial,        /* initial serial is 1 */
1190                                                                                         max_depth,
1191                                                                                         show_branch,
1192                                                                                         show_serial,
1193                                                                                         per_query_ctx,
1194                                                                                         attinmeta,
1195                                                                                         tupstore);
1196
1197         SPI_finish();
1198
1199         return tupstore;
1200 }
1201
1202 static Tuplestorestate *
1203 build_tuplestore_recursively(char *key_fld,
1204                                                          char *parent_key_fld,
1205                                                          char *relname,
1206                                                          char *orderby_fld,
1207                                                          char *branch_delim,
1208                                                          char *start_with,
1209                                                          char *branch,
1210                                                          int level,
1211                                                          int *serial,
1212                                                          int max_depth,
1213                                                          bool show_branch,
1214                                                          bool show_serial,
1215                                                          MemoryContext per_query_ctx,
1216                                                          AttInMetadata *attinmeta,
1217                                                          Tuplestorestate *tupstore)
1218 {
1219         TupleDesc       tupdesc = attinmeta->tupdesc;
1220         int                     ret;
1221         int                     proc;
1222         int                     serial_column;
1223         StringInfoData sql;
1224         char      **values;
1225         char       *current_key;
1226         char       *current_key_parent;
1227         char            current_level[INT32_STRLEN];
1228         char            serial_str[INT32_STRLEN];
1229         char       *current_branch;
1230         HeapTuple       tuple;
1231
1232         if (max_depth > 0 && level > max_depth)
1233                 return tupstore;
1234
1235         initStringInfo(&sql);
1236
1237         /* Build initial sql statement */
1238         if (!show_serial)
1239         {
1240                 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1241                                                  key_fld,
1242                                                  parent_key_fld,
1243                                                  relname,
1244                                                  parent_key_fld,
1245                                                  quote_literal_cstr(start_with),
1246                                                  key_fld, key_fld, parent_key_fld);
1247                 serial_column = 0;
1248         }
1249         else
1250         {
1251                 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1252                                                  key_fld,
1253                                                  parent_key_fld,
1254                                                  relname,
1255                                                  parent_key_fld,
1256                                                  quote_literal_cstr(start_with),
1257                                                  key_fld, key_fld, parent_key_fld,
1258                                                  orderby_fld);
1259                 serial_column = 1;
1260         }
1261
1262         if (show_branch)
1263                 values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1264         else
1265                 values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1266
1267         /* First time through, do a little setup */
1268         if (level == 0)
1269         {
1270                 /* root value is the one we initially start with */
1271                 values[0] = start_with;
1272
1273                 /* root value has no parent */
1274                 values[1] = NULL;
1275
1276                 /* root level is 0 */
1277                 sprintf(current_level, "%d", level);
1278                 values[2] = current_level;
1279
1280                 /* root branch is just starting root value */
1281                 if (show_branch)
1282                         values[3] = start_with;
1283
1284                 /* root starts the serial with 1 */
1285                 if (show_serial)
1286                 {
1287                         sprintf(serial_str, "%d", (*serial)++);
1288                         if (show_branch)
1289                                 values[4] = serial_str;
1290                         else
1291                                 values[3] = serial_str;
1292                 }
1293
1294                 /* construct the tuple */
1295                 tuple = BuildTupleFromCStrings(attinmeta, values);
1296
1297                 /* now store it */
1298                 tuplestore_puttuple(tupstore, tuple);
1299
1300                 /* increment level */
1301                 level++;
1302         }
1303
1304         /* Retrieve the desired rows */
1305         ret = SPI_execute(sql.data, true, 0);
1306         proc = SPI_processed;
1307
1308         /* Check for qualifying tuples */
1309         if ((ret == SPI_OK_SELECT) && (proc > 0))
1310         {
1311                 HeapTuple       spi_tuple;
1312                 SPITupleTable *tuptable = SPI_tuptable;
1313                 TupleDesc       spi_tupdesc = tuptable->tupdesc;
1314                 int                     i;
1315                 StringInfoData branchstr;
1316                 StringInfoData chk_branchstr;
1317                 StringInfoData chk_current_key;
1318
1319                 /* First time through, do a little more setup */
1320                 if (level == 0)
1321                 {
1322                         /*
1323                          * Check that return tupdesc is compatible with the one we got
1324                          * from the query, but only at level 0 -- no need to check more
1325                          * than once
1326                          */
1327
1328                         if (!compatConnectbyTupleDescs(tupdesc, spi_tupdesc))
1329                                 ereport(ERROR,
1330                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1331                                                  errmsg("invalid return type"),
1332                                                  errdetail("Return and SQL tuple descriptions are " \
1333                                                                    "incompatible.")));
1334                 }
1335
1336                 initStringInfo(&branchstr);
1337                 initStringInfo(&chk_branchstr);
1338                 initStringInfo(&chk_current_key);
1339
1340                 for (i = 0; i < proc; i++)
1341                 {
1342                         /* initialize branch for this pass */
1343                         appendStringInfo(&branchstr, "%s", branch);
1344                         appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
1345
1346                         /* get the next sql result tuple */
1347                         spi_tuple = tuptable->vals[i];
1348
1349                         /* get the current key and parent */
1350                         current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
1351                         appendStringInfo(&chk_current_key, "%s%s%s", branch_delim, current_key, branch_delim);
1352                         current_key_parent = pstrdup(SPI_getvalue(spi_tuple, spi_tupdesc, 2));
1353
1354                         /* get the current level */
1355                         sprintf(current_level, "%d", level);
1356
1357                         /* check to see if this key is also an ancestor */
1358                         if (strstr(chk_branchstr.data, chk_current_key.data))
1359                                 elog(ERROR, "infinite recursion detected");
1360
1361                         /* OK, extend the branch */
1362                         appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
1363                         current_branch = branchstr.data;
1364
1365                         /* build a tuple */
1366                         values[0] = pstrdup(current_key);
1367                         values[1] = current_key_parent;
1368                         values[2] = current_level;
1369                         if (show_branch)
1370                                 values[3] = current_branch;
1371                         if (show_serial)
1372                         {
1373                                 sprintf(serial_str, "%d", (*serial)++);
1374                                 if (show_branch)
1375                                         values[4] = serial_str;
1376                                 else
1377                                         values[3] = serial_str;
1378                         }
1379
1380                         tuple = BuildTupleFromCStrings(attinmeta, values);
1381
1382                         xpfree(current_key);
1383                         xpfree(current_key_parent);
1384
1385                         /* store the tuple for later use */
1386                         tuplestore_puttuple(tupstore, tuple);
1387
1388                         heap_freetuple(tuple);
1389
1390                         /* recurse using current_key_parent as the new start_with */
1391                         tupstore = build_tuplestore_recursively(key_fld,
1392                                                                                                         parent_key_fld,
1393                                                                                                         relname,
1394                                                                                                         orderby_fld,
1395                                                                                                         branch_delim,
1396                                                                                                         values[0],
1397                                                                                                         current_branch,
1398                                                                                                         level + 1,
1399                                                                                                         serial,
1400                                                                                                         max_depth,
1401                                                                                                         show_branch,
1402                                                                                                         show_serial,
1403                                                                                                         per_query_ctx,
1404                                                                                                         attinmeta,
1405                                                                                                         tupstore);
1406
1407                         /* reset branch for next pass */
1408                         resetStringInfo(&branchstr);
1409                         resetStringInfo(&chk_branchstr);
1410                         resetStringInfo(&chk_current_key);
1411                 }
1412
1413                 xpfree(branchstr.data);
1414                 xpfree(chk_branchstr.data);
1415                 xpfree(chk_current_key.data);
1416         }
1417
1418         return tupstore;
1419 }
1420
1421 /*
1422  * Check expected (query runtime) tupdesc suitable for Connectby
1423  */
1424 static void
1425 validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
1426 {
1427         int                     serial_column = 0;
1428
1429         if (show_serial)
1430                 serial_column = 1;
1431
1432         /* are there the correct number of columns */
1433         if (show_branch)
1434         {
1435                 if (tupdesc->natts != (CONNECTBY_NCOLS + serial_column))
1436                         ereport(ERROR,
1437                                         (errcode(ERRCODE_SYNTAX_ERROR),
1438                                          errmsg("invalid return type"),
1439                                          errdetail("Query-specified return tuple has " \
1440                                                            "wrong number of columns.")));
1441         }
1442         else
1443         {
1444                 if (tupdesc->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1445                         ereport(ERROR,
1446                                         (errcode(ERRCODE_SYNTAX_ERROR),
1447                                          errmsg("invalid return type"),
1448                                          errdetail("Query-specified return tuple has " \
1449                                                            "wrong number of columns.")));
1450         }
1451
1452         /* check that the types of the first two columns match */
1453         if (tupdesc->attrs[0]->atttypid != tupdesc->attrs[1]->atttypid)
1454                 ereport(ERROR,
1455                                 (errcode(ERRCODE_SYNTAX_ERROR),
1456                                  errmsg("invalid return type"),
1457                                  errdetail("First two columns must be the same type.")));
1458
1459         /* check that the type of the third column is INT4 */
1460         if (tupdesc->attrs[2]->atttypid != INT4OID)
1461                 ereport(ERROR,
1462                                 (errcode(ERRCODE_SYNTAX_ERROR),
1463                                  errmsg("invalid return type"),
1464                                  errdetail("Third column must be type %s.",
1465                                                    format_type_be(INT4OID))));
1466
1467         /* check that the type of the fourth column is TEXT if applicable */
1468         if (show_branch && tupdesc->attrs[3]->atttypid != TEXTOID)
1469                 ereport(ERROR,
1470                                 (errcode(ERRCODE_SYNTAX_ERROR),
1471                                  errmsg("invalid return type"),
1472                                  errdetail("Fourth column must be type %s.",
1473                                                    format_type_be(TEXTOID))));
1474
1475         /* check that the type of the fifth column is INT4 */
1476         if (show_branch && show_serial && tupdesc->attrs[4]->atttypid != INT4OID)
1477                 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1478                          "fifth column must be type %s", format_type_be(INT4OID));
1479
1480         /* check that the type of the fifth column is INT4 */
1481         if (!show_branch && show_serial && tupdesc->attrs[3]->atttypid != INT4OID)
1482                 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1483                          "fourth column must be type %s", format_type_be(INT4OID));
1484
1485         /* OK, the tupdesc is valid for our purposes */
1486 }
1487
1488 /*
1489  * Check if spi sql tupdesc and return tupdesc are compatible
1490  */
1491 static bool
1492 compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1493 {
1494         Oid                     ret_atttypid;
1495         Oid                     sql_atttypid;
1496
1497         /* check the key_fld types match */
1498         ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1499         sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1500         if (ret_atttypid != sql_atttypid)
1501                 ereport(ERROR,
1502                                 (errcode(ERRCODE_SYNTAX_ERROR),
1503                                  errmsg("invalid return type"),
1504                                  errdetail("SQL key field datatype does " \
1505                                                    "not match return key field datatype.")));
1506
1507         /* check the parent_key_fld types match */
1508         ret_atttypid = ret_tupdesc->attrs[1]->atttypid;
1509         sql_atttypid = sql_tupdesc->attrs[1]->atttypid;
1510         if (ret_atttypid != sql_atttypid)
1511                 ereport(ERROR,
1512                                 (errcode(ERRCODE_SYNTAX_ERROR),
1513                                  errmsg("invalid return type"),
1514                                  errdetail("SQL parent key field datatype does " \
1515                                                    "not match return parent key field datatype.")));
1516
1517         /* OK, the two tupdescs are compatible for our purposes */
1518         return true;
1519 }
1520
1521 /*
1522  * Check if two tupdescs match in type of attributes
1523  */
1524 static bool
1525 compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1526 {
1527         int                     i;
1528         Form_pg_attribute ret_attr;
1529         Oid                     ret_atttypid;
1530         Form_pg_attribute sql_attr;
1531         Oid                     sql_atttypid;
1532
1533         if (ret_tupdesc->natts < 2 ||
1534                 sql_tupdesc->natts < 3)
1535                 return false;
1536
1537         /* check the rowid types match */
1538         ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1539         sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1540         if (ret_atttypid != sql_atttypid)
1541                 ereport(ERROR,
1542                                 (errcode(ERRCODE_SYNTAX_ERROR),
1543                                  errmsg("invalid return type"),
1544                                  errdetail("SQL rowid datatype does not match " \
1545                                                    "return rowid datatype.")));
1546
1547         /*
1548          * - attribute [1] of the sql tuple is the category; no need to check it -
1549          * attribute [2] of the sql tuple should match attributes [1] to [natts]
1550          * of the return tuple
1551          */
1552         sql_attr = sql_tupdesc->attrs[2];
1553         for (i = 1; i < ret_tupdesc->natts; i++)
1554         {
1555                 ret_attr = ret_tupdesc->attrs[i];
1556
1557                 if (ret_attr->atttypid != sql_attr->atttypid)
1558                         return false;
1559         }
1560
1561         /* OK, the two tupdescs are compatible for our purposes */
1562         return true;
1563 }