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-2017, 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 void 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 void 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 	uint64		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
normal_rand(PG_FUNCTION_ARGS)174 normal_rand(PG_FUNCTION_ARGS)
175 {
176 	FuncCallContext *funcctx;
177 	uint64		call_cntr;
178 	uint64		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
get_normal_pair(float8 * x1,float8 * x2)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
crosstab(PG_FUNCTION_ARGS)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 	uint64		call_cntr;
356 	uint64		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 	uint64		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 			ereport(ERROR,
436 					(errcode(ERRCODE_DATATYPE_MISMATCH),
437 					 errmsg("return type must be a row type")));
438 			break;
439 	}
440 
441 	/*
442 	 * Check that return tupdesc is compatible with the data we got from SPI,
443 	 * at least based on number and type of attributes
444 	 */
445 	if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
446 		ereport(ERROR,
447 				(errcode(ERRCODE_SYNTAX_ERROR),
448 				 errmsg("return and sql tuple descriptions are " \
449 						"incompatible")));
450 
451 	/*
452 	 * switch to long-lived memory context
453 	 */
454 	oldcontext = MemoryContextSwitchTo(per_query_ctx);
455 
456 	/* make sure we have a persistent copy of the result tupdesc */
457 	tupdesc = CreateTupleDescCopy(tupdesc);
458 
459 	/* initialize our tuplestore in long-lived context */
460 	tupstore =
461 		tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
462 							  false, work_mem);
463 
464 	MemoryContextSwitchTo(oldcontext);
465 
466 	/*
467 	 * Generate attribute metadata needed later to produce tuples from raw C
468 	 * strings
469 	 */
470 	attinmeta = TupleDescGetAttInMetadata(tupdesc);
471 
472 	/* total number of tuples to be examined */
473 	max_calls = proc;
474 
475 	/* the return tuple always must have 1 rowid + num_categories columns */
476 	num_categories = tupdesc->natts - 1;
477 
478 	firstpass = true;
479 	lastrowid = NULL;
480 
481 	for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
482 	{
483 		bool		skip_tuple = false;
484 		char	  **values;
485 
486 		/* allocate and zero space */
487 		values = (char **) palloc0((1 + num_categories) * sizeof(char *));
488 
489 		/*
490 		 * now loop through the sql results and assign each value in sequence
491 		 * to the next category
492 		 */
493 		for (i = 0; i < num_categories; i++)
494 		{
495 			HeapTuple	spi_tuple;
496 			char	   *rowid;
497 
498 			/* see if we've gone too far already */
499 			if (call_cntr >= max_calls)
500 				break;
501 
502 			/* get the next sql result tuple */
503 			spi_tuple = spi_tuptable->vals[call_cntr];
504 
505 			/* get the rowid from the current sql result tuple */
506 			rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
507 
508 			/*
509 			 * If this is the first pass through the values for this rowid,
510 			 * set the first column to rowid
511 			 */
512 			if (i == 0)
513 			{
514 				xpstrdup(values[0], rowid);
515 
516 				/*
517 				 * Check to see if the rowid is the same as that of the last
518 				 * tuple sent -- if so, skip this tuple entirely
519 				 */
520 				if (!firstpass && xstreq(lastrowid, rowid))
521 				{
522 					xpfree(rowid);
523 					skip_tuple = true;
524 					break;
525 				}
526 			}
527 
528 			/*
529 			 * If rowid hasn't changed on us, continue building the output
530 			 * tuple.
531 			 */
532 			if (xstreq(rowid, values[0]))
533 			{
534 				/*
535 				 * Get the next category item value, which is always attribute
536 				 * number three.
537 				 *
538 				 * Be careful to assign the value to the array index based on
539 				 * which category we are presently processing.
540 				 */
541 				values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
542 
543 				/*
544 				 * increment the counter since we consume a row for each
545 				 * category, but not for last pass because the outer loop will
546 				 * do that for us
547 				 */
548 				if (i < (num_categories - 1))
549 					call_cntr++;
550 				xpfree(rowid);
551 			}
552 			else
553 			{
554 				/*
555 				 * We'll fill in NULLs for the missing values, but we need to
556 				 * decrement the counter since this sql result row doesn't
557 				 * belong to the current output tuple.
558 				 */
559 				call_cntr--;
560 				xpfree(rowid);
561 				break;
562 			}
563 		}
564 
565 		if (!skip_tuple)
566 		{
567 			HeapTuple	tuple;
568 
569 			/* build the tuple and store it */
570 			tuple = BuildTupleFromCStrings(attinmeta, values);
571 			tuplestore_puttuple(tupstore, tuple);
572 			heap_freetuple(tuple);
573 		}
574 
575 		/* Remember current rowid */
576 		xpfree(lastrowid);
577 		xpstrdup(lastrowid, values[0]);
578 		firstpass = false;
579 
580 		/* Clean up */
581 		for (i = 0; i < num_categories + 1; i++)
582 			if (values[i] != NULL)
583 				pfree(values[i]);
584 		pfree(values);
585 	}
586 
587 	/* let the caller know we're sending back a tuplestore */
588 	rsinfo->returnMode = SFRM_Materialize;
589 	rsinfo->setResult = tupstore;
590 	rsinfo->setDesc = tupdesc;
591 
592 	/* release SPI related resources (and return to caller's context) */
593 	SPI_finish();
594 
595 	return (Datum) 0;
596 }
597 
598 /*
599  * crosstab_hash - reimplement crosstab as materialized function and
600  * properly deal with missing values (i.e. don't pack remaining
601  * values to the left)
602  *
603  * crosstab - create a crosstab of rowids and values columns from a
604  * SQL statement returning one rowid column, one category column,
605  * and one value column.
606  *
607  * e.g. given sql which produces:
608  *
609  *			rowid	cat		value
610  *			------+-------+-------
611  *			row1	cat1	val1
612  *			row1	cat2	val2
613  *			row1	cat4	val4
614  *			row2	cat1	val5
615  *			row2	cat2	val6
616  *			row2	cat3	val7
617  *			row2	cat4	val8
618  *
619  * crosstab returns:
620  *					<===== values columns =====>
621  *			rowid	cat1	cat2	cat3	cat4
622  *			------+-------+-------+-------+-------
623  *			row1	val1	val2	null	val4
624  *			row2	val5	val6	val7	val8
625  *
626  * NOTES:
627  * 1. SQL result must be ordered by 1.
628  * 2. The number of values columns depends on the tuple description
629  *	  of the function's declared return type.
630  * 3. Missing values (i.e. missing category) are filled in with nulls.
631  * 4. Extra values (i.e. not in category results) are skipped.
632  */
633 PG_FUNCTION_INFO_V1(crosstab_hash);
634 Datum
crosstab_hash(PG_FUNCTION_ARGS)635 crosstab_hash(PG_FUNCTION_ARGS)
636 {
637 	char	   *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
638 	char	   *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
639 	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
640 	TupleDesc	tupdesc;
641 	MemoryContext per_query_ctx;
642 	MemoryContext oldcontext;
643 	HTAB	   *crosstab_hash;
644 
645 	/* check to see if caller supports us returning a tuplestore */
646 	if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
647 		ereport(ERROR,
648 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
649 				 errmsg("set-valued function called in context that cannot accept a set")));
650 	if (!(rsinfo->allowedModes & SFRM_Materialize) ||
651 		rsinfo->expectedDesc == NULL)
652 		ereport(ERROR,
653 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
654 				 errmsg("materialize mode required, but it is not " \
655 						"allowed in this context")));
656 
657 	per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
658 	oldcontext = MemoryContextSwitchTo(per_query_ctx);
659 
660 	/* get the requested return tuple description */
661 	tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
662 
663 	/*
664 	 * Check to make sure we have a reasonable tuple descriptor
665 	 *
666 	 * Note we will attempt to coerce the values into whatever the return
667 	 * attribute type is and depend on the "in" function to complain if
668 	 * needed.
669 	 */
670 	if (tupdesc->natts < 2)
671 		ereport(ERROR,
672 				(errcode(ERRCODE_SYNTAX_ERROR),
673 				 errmsg("query-specified return tuple and " \
674 						"crosstab function are not compatible")));
675 
676 	/* load up the categories hash table */
677 	crosstab_hash = load_categories_hash(cats_sql, per_query_ctx);
678 
679 	/* let the caller know we're sending back a tuplestore */
680 	rsinfo->returnMode = SFRM_Materialize;
681 
682 	/* now go build it */
683 	rsinfo->setResult = get_crosstab_tuplestore(sql,
684 												crosstab_hash,
685 												tupdesc,
686 												per_query_ctx,
687 												rsinfo->allowedModes & SFRM_Materialize_Random);
688 
689 	/*
690 	 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
691 	 * tuples are in our tuplestore and passed back through rsinfo->setResult.
692 	 * rsinfo->setDesc is set to the tuple description that we actually used
693 	 * to build our tuples with, so the caller can verify we did what it was
694 	 * expecting.
695 	 */
696 	rsinfo->setDesc = tupdesc;
697 	MemoryContextSwitchTo(oldcontext);
698 
699 	return (Datum) 0;
700 }
701 
702 /*
703  * load up the categories hash table
704  */
705 static HTAB *
load_categories_hash(char * cats_sql,MemoryContext per_query_ctx)706 load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
707 {
708 	HTAB	   *crosstab_hash;
709 	HASHCTL		ctl;
710 	int			ret;
711 	uint64		proc;
712 	MemoryContext SPIcontext;
713 
714 	/* initialize the category hash table */
715 	MemSet(&ctl, 0, sizeof(ctl));
716 	ctl.keysize = MAX_CATNAME_LEN;
717 	ctl.entrysize = sizeof(crosstab_HashEnt);
718 	ctl.hcxt = per_query_ctx;
719 
720 	/*
721 	 * use INIT_CATS, defined above as a guess of how many hash table entries
722 	 * to create, initially
723 	 */
724 	crosstab_hash = hash_create("crosstab hash",
725 								INIT_CATS,
726 								&ctl,
727 								HASH_ELEM | HASH_CONTEXT);
728 
729 	/* Connect to SPI manager */
730 	if ((ret = SPI_connect()) < 0)
731 		/* internal error */
732 		elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
733 
734 	/* Retrieve the category name rows */
735 	ret = SPI_execute(cats_sql, true, 0);
736 	proc = SPI_processed;
737 
738 	/* Check for qualifying tuples */
739 	if ((ret == SPI_OK_SELECT) && (proc > 0))
740 	{
741 		SPITupleTable *spi_tuptable = SPI_tuptable;
742 		TupleDesc	spi_tupdesc = spi_tuptable->tupdesc;
743 		uint64		i;
744 
745 		/*
746 		 * The provided categories SQL query must always return one column:
747 		 * category - the label or identifier for each column
748 		 */
749 		if (spi_tupdesc->natts != 1)
750 			ereport(ERROR,
751 					(errcode(ERRCODE_SYNTAX_ERROR),
752 					 errmsg("provided \"categories\" SQL must " \
753 							"return 1 column of at least one row")));
754 
755 		for (i = 0; i < proc; i++)
756 		{
757 			crosstab_cat_desc *catdesc;
758 			char	   *catname;
759 			HeapTuple	spi_tuple;
760 
761 			/* get the next sql result tuple */
762 			spi_tuple = spi_tuptable->vals[i];
763 
764 			/* get the category from the current sql result tuple */
765 			catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
766 			if (catname == NULL)
767 				ereport(ERROR,
768 						(errcode(ERRCODE_SYNTAX_ERROR),
769 						 errmsg("provided \"categories\" SQL must " \
770 								"not return NULL values")));
771 
772 			SPIcontext = MemoryContextSwitchTo(per_query_ctx);
773 
774 			catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
775 			catdesc->catname = catname;
776 			catdesc->attidx = i;
777 
778 			/* Add the proc description block to the hashtable */
779 			crosstab_HashTableInsert(crosstab_hash, catdesc);
780 
781 			MemoryContextSwitchTo(SPIcontext);
782 		}
783 	}
784 
785 	if (SPI_finish() != SPI_OK_FINISH)
786 		/* internal error */
787 		elog(ERROR, "load_categories_hash: SPI_finish() failed");
788 
789 	return crosstab_hash;
790 }
791 
792 /*
793  * create and populate the crosstab tuplestore using the provided source query
794  */
795 static Tuplestorestate *
get_crosstab_tuplestore(char * sql,HTAB * crosstab_hash,TupleDesc tupdesc,MemoryContext per_query_ctx,bool randomAccess)796 get_crosstab_tuplestore(char *sql,
797 						HTAB *crosstab_hash,
798 						TupleDesc tupdesc,
799 						MemoryContext per_query_ctx,
800 						bool randomAccess)
801 {
802 	Tuplestorestate *tupstore;
803 	int			num_categories = hash_get_num_entries(crosstab_hash);
804 	AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
805 	char	  **values;
806 	HeapTuple	tuple;
807 	int			ret;
808 	uint64		proc;
809 
810 	/* initialize our tuplestore (while still in query context!) */
811 	tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
812 
813 	/* Connect to SPI manager */
814 	if ((ret = SPI_connect()) < 0)
815 		/* internal error */
816 		elog(ERROR, "get_crosstab_tuplestore: SPI_connect returned %d", ret);
817 
818 	/* Now retrieve the crosstab source rows */
819 	ret = SPI_execute(sql, true, 0);
820 	proc = SPI_processed;
821 
822 	/* Check for qualifying tuples */
823 	if ((ret == SPI_OK_SELECT) && (proc > 0))
824 	{
825 		SPITupleTable *spi_tuptable = SPI_tuptable;
826 		TupleDesc	spi_tupdesc = spi_tuptable->tupdesc;
827 		int			ncols = spi_tupdesc->natts;
828 		char	   *rowid;
829 		char	   *lastrowid = NULL;
830 		bool		firstpass = true;
831 		uint64		i;
832 		int			j;
833 		int			result_ncols;
834 
835 		if (num_categories == 0)
836 		{
837 			/* no qualifying category tuples */
838 			ereport(ERROR,
839 					(errcode(ERRCODE_SYNTAX_ERROR),
840 					 errmsg("provided \"categories\" SQL must " \
841 							"return 1 column of at least one row")));
842 		}
843 
844 		/*
845 		 * The provided SQL query must always return at least three columns:
846 		 *
847 		 * 1. rowname	the label for each row - column 1 in the final result
848 		 * 2. category	the label for each value-column in the final result 3.
849 		 * value	 the values used to populate the value-columns
850 		 *
851 		 * If there are more than three columns, the last two are taken as
852 		 * "category" and "values". The first column is taken as "rowname".
853 		 * Additional columns (2 thru N-2) are assumed the same for the same
854 		 * "rowname", and are copied into the result tuple from the first time
855 		 * we encounter a particular rowname.
856 		 */
857 		if (ncols < 3)
858 			ereport(ERROR,
859 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
860 					 errmsg("invalid source data SQL statement"),
861 					 errdetail("The provided SQL must return 3 " \
862 							   " columns; rowid, category, and values.")));
863 
864 		result_ncols = (ncols - 2) + num_categories;
865 
866 		/* Recheck to make sure we tuple descriptor still looks reasonable */
867 		if (tupdesc->natts != result_ncols)
868 			ereport(ERROR,
869 					(errcode(ERRCODE_SYNTAX_ERROR),
870 					 errmsg("invalid return type"),
871 					 errdetail("Query-specified return " \
872 							   "tuple has %d columns but crosstab " \
873 							   "returns %d.", tupdesc->natts, result_ncols)));
874 
875 		/* allocate space */
876 		values = (char **) palloc(result_ncols * sizeof(char *));
877 
878 		/* and make sure it's clear */
879 		memset(values, '\0', result_ncols * sizeof(char *));
880 
881 		for (i = 0; i < proc; i++)
882 		{
883 			HeapTuple	spi_tuple;
884 			crosstab_cat_desc *catdesc;
885 			char	   *catname;
886 
887 			/* get the next sql result tuple */
888 			spi_tuple = spi_tuptable->vals[i];
889 
890 			/* get the rowid from the current sql result tuple */
891 			rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
892 
893 			/*
894 			 * if we're on a new output row, grab the column values up to
895 			 * column N-2 now
896 			 */
897 			if (firstpass || !xstreq(lastrowid, rowid))
898 			{
899 				/*
900 				 * a new row means we need to flush the old one first, unless
901 				 * we're on the very first row
902 				 */
903 				if (!firstpass)
904 				{
905 					/* rowid changed, flush the previous output row */
906 					tuple = BuildTupleFromCStrings(attinmeta, values);
907 
908 					tuplestore_puttuple(tupstore, tuple);
909 
910 					for (j = 0; j < result_ncols; j++)
911 						xpfree(values[j]);
912 				}
913 
914 				values[0] = rowid;
915 				for (j = 1; j < ncols - 2; j++)
916 					values[j] = SPI_getvalue(spi_tuple, spi_tupdesc, j + 1);
917 
918 				/* we're no longer on the first pass */
919 				firstpass = false;
920 			}
921 
922 			/* look up the category and fill in the appropriate column */
923 			catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
924 
925 			if (catname != NULL)
926 			{
927 				crosstab_HashTableLookup(crosstab_hash, catname, catdesc);
928 
929 				if (catdesc)
930 					values[catdesc->attidx + ncols - 2] =
931 						SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
932 			}
933 
934 			xpfree(lastrowid);
935 			xpstrdup(lastrowid, rowid);
936 		}
937 
938 		/* flush the last output row */
939 		tuple = BuildTupleFromCStrings(attinmeta, values);
940 
941 		tuplestore_puttuple(tupstore, tuple);
942 	}
943 
944 	if (SPI_finish() != SPI_OK_FINISH)
945 		/* internal error */
946 		elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
947 
948 	tuplestore_donestoring(tupstore);
949 
950 	return tupstore;
951 }
952 
953 /*
954  * connectby_text - produce a result set from a hierarchical (parent/child)
955  * table.
956  *
957  * e.g. given table foo:
958  *
959  *			keyid	parent_keyid pos
960  *			------+------------+--
961  *			row1	NULL		 0
962  *			row2	row1		 0
963  *			row3	row1		 0
964  *			row4	row2		 1
965  *			row5	row2		 0
966  *			row6	row4		 0
967  *			row7	row3		 0
968  *			row8	row6		 0
969  *			row9	row5		 0
970  *
971  *
972  * connectby(text relname, text keyid_fld, text parent_keyid_fld
973  *			  [, text orderby_fld], text start_with, int max_depth
974  *			  [, text branch_delim])
975  * connectby('foo', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') returns:
976  *
977  *		keyid	parent_id	level	 branch				serial
978  *		------+-----------+--------+-----------------------
979  *		row2	NULL		  0		  row2				  1
980  *		row5	row2		  1		  row2~row5			  2
981  *		row9	row5		  2		  row2~row5~row9	  3
982  *		row4	row2		  1		  row2~row4			  4
983  *		row6	row4		  2		  row2~row4~row6	  5
984  *		row8	row6		  3		  row2~row4~row6~row8 6
985  *
986  */
987 PG_FUNCTION_INFO_V1(connectby_text);
988 
989 #define CONNECTBY_NCOLS					4
990 #define CONNECTBY_NCOLS_NOBRANCH		3
991 
992 Datum
connectby_text(PG_FUNCTION_ARGS)993 connectby_text(PG_FUNCTION_ARGS)
994 {
995 	char	   *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
996 	char	   *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
997 	char	   *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
998 	char	   *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3));
999 	int			max_depth = PG_GETARG_INT32(4);
1000 	char	   *branch_delim = NULL;
1001 	bool		show_branch = false;
1002 	bool		show_serial = false;
1003 	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1004 	TupleDesc	tupdesc;
1005 	AttInMetadata *attinmeta;
1006 	MemoryContext per_query_ctx;
1007 	MemoryContext oldcontext;
1008 
1009 	/* check to see if caller supports us returning a tuplestore */
1010 	if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1011 		ereport(ERROR,
1012 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1013 				 errmsg("set-valued function called in context that cannot accept a set")));
1014 	if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1015 		rsinfo->expectedDesc == NULL)
1016 		ereport(ERROR,
1017 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1018 				 errmsg("materialize mode required, but it is not " \
1019 						"allowed in this context")));
1020 
1021 	if (fcinfo->nargs == 6)
1022 	{
1023 		branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5));
1024 		show_branch = true;
1025 	}
1026 	else
1027 		/* default is no show, tilde for the delimiter */
1028 		branch_delim = pstrdup("~");
1029 
1030 	per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1031 	oldcontext = MemoryContextSwitchTo(per_query_ctx);
1032 
1033 	/* get the requested return tuple description */
1034 	tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1035 
1036 	/* does it meet our needs */
1037 	validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1038 
1039 	/* OK, use it then */
1040 	attinmeta = TupleDescGetAttInMetadata(tupdesc);
1041 
1042 	/* OK, go to work */
1043 	rsinfo->returnMode = SFRM_Materialize;
1044 	rsinfo->setResult = connectby(relname,
1045 								  key_fld,
1046 								  parent_key_fld,
1047 								  NULL,
1048 								  branch_delim,
1049 								  start_with,
1050 								  max_depth,
1051 								  show_branch,
1052 								  show_serial,
1053 								  per_query_ctx,
1054 								  rsinfo->allowedModes & SFRM_Materialize_Random,
1055 								  attinmeta);
1056 	rsinfo->setDesc = tupdesc;
1057 
1058 	MemoryContextSwitchTo(oldcontext);
1059 
1060 	/*
1061 	 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1062 	 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1063 	 * rsinfo->setDesc is set to the tuple description that we actually used
1064 	 * to build our tuples with, so the caller can verify we did what it was
1065 	 * expecting.
1066 	 */
1067 	return (Datum) 0;
1068 }
1069 
1070 PG_FUNCTION_INFO_V1(connectby_text_serial);
1071 Datum
connectby_text_serial(PG_FUNCTION_ARGS)1072 connectby_text_serial(PG_FUNCTION_ARGS)
1073 {
1074 	char	   *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
1075 	char	   *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
1076 	char	   *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
1077 	char	   *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3));
1078 	char	   *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4));
1079 	int			max_depth = PG_GETARG_INT32(5);
1080 	char	   *branch_delim = NULL;
1081 	bool		show_branch = false;
1082 	bool		show_serial = true;
1083 	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1084 	TupleDesc	tupdesc;
1085 	AttInMetadata *attinmeta;
1086 	MemoryContext per_query_ctx;
1087 	MemoryContext oldcontext;
1088 
1089 	/* check to see if caller supports us returning a tuplestore */
1090 	if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1091 		ereport(ERROR,
1092 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1093 				 errmsg("set-valued function called in context that cannot accept a set")));
1094 	if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1095 		rsinfo->expectedDesc == NULL)
1096 		ereport(ERROR,
1097 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1098 				 errmsg("materialize mode required, but it is not " \
1099 						"allowed in this context")));
1100 
1101 	if (fcinfo->nargs == 7)
1102 	{
1103 		branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6));
1104 		show_branch = true;
1105 	}
1106 	else
1107 		/* default is no show, tilde for the delimiter */
1108 		branch_delim = pstrdup("~");
1109 
1110 	per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1111 	oldcontext = MemoryContextSwitchTo(per_query_ctx);
1112 
1113 	/* get the requested return tuple description */
1114 	tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1115 
1116 	/* does it meet our needs */
1117 	validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1118 
1119 	/* OK, use it then */
1120 	attinmeta = TupleDescGetAttInMetadata(tupdesc);
1121 
1122 	/* OK, go to work */
1123 	rsinfo->returnMode = SFRM_Materialize;
1124 	rsinfo->setResult = connectby(relname,
1125 								  key_fld,
1126 								  parent_key_fld,
1127 								  orderby_fld,
1128 								  branch_delim,
1129 								  start_with,
1130 								  max_depth,
1131 								  show_branch,
1132 								  show_serial,
1133 								  per_query_ctx,
1134 								  rsinfo->allowedModes & SFRM_Materialize_Random,
1135 								  attinmeta);
1136 	rsinfo->setDesc = tupdesc;
1137 
1138 	MemoryContextSwitchTo(oldcontext);
1139 
1140 	/*
1141 	 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1142 	 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1143 	 * rsinfo->setDesc is set to the tuple description that we actually used
1144 	 * to build our tuples with, so the caller can verify we did what it was
1145 	 * expecting.
1146 	 */
1147 	return (Datum) 0;
1148 }
1149 
1150 
1151 /*
1152  * connectby - does the real work for connectby_text()
1153  */
1154 static Tuplestorestate *
connectby(char * relname,char * key_fld,char * parent_key_fld,char * orderby_fld,char * branch_delim,char * start_with,int max_depth,bool show_branch,bool show_serial,MemoryContext per_query_ctx,bool randomAccess,AttInMetadata * attinmeta)1155 connectby(char *relname,
1156 		  char *key_fld,
1157 		  char *parent_key_fld,
1158 		  char *orderby_fld,
1159 		  char *branch_delim,
1160 		  char *start_with,
1161 		  int max_depth,
1162 		  bool show_branch,
1163 		  bool show_serial,
1164 		  MemoryContext per_query_ctx,
1165 		  bool randomAccess,
1166 		  AttInMetadata *attinmeta)
1167 {
1168 	Tuplestorestate *tupstore = NULL;
1169 	int			ret;
1170 	MemoryContext oldcontext;
1171 
1172 	int			serial = 1;
1173 
1174 	/* Connect to SPI manager */
1175 	if ((ret = SPI_connect()) < 0)
1176 		/* internal error */
1177 		elog(ERROR, "connectby: SPI_connect returned %d", ret);
1178 
1179 	/* switch to longer term context to create the tuple store */
1180 	oldcontext = MemoryContextSwitchTo(per_query_ctx);
1181 
1182 	/* initialize our tuplestore */
1183 	tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1184 
1185 	MemoryContextSwitchTo(oldcontext);
1186 
1187 	/* now go get the whole tree */
1188 	build_tuplestore_recursively(key_fld,
1189 								 parent_key_fld,
1190 								 relname,
1191 								 orderby_fld,
1192 								 branch_delim,
1193 								 start_with,
1194 								 start_with,	/* current_branch */
1195 								 0, /* initial level is 0 */
1196 								 &serial,	/* initial serial is 1 */
1197 								 max_depth,
1198 								 show_branch,
1199 								 show_serial,
1200 								 per_query_ctx,
1201 								 attinmeta,
1202 								 tupstore);
1203 
1204 	SPI_finish();
1205 
1206 	return tupstore;
1207 }
1208 
1209 static void
build_tuplestore_recursively(char * key_fld,char * parent_key_fld,char * relname,char * orderby_fld,char * branch_delim,char * start_with,char * branch,int level,int * serial,int max_depth,bool show_branch,bool show_serial,MemoryContext per_query_ctx,AttInMetadata * attinmeta,Tuplestorestate * tupstore)1210 build_tuplestore_recursively(char *key_fld,
1211 							 char *parent_key_fld,
1212 							 char *relname,
1213 							 char *orderby_fld,
1214 							 char *branch_delim,
1215 							 char *start_with,
1216 							 char *branch,
1217 							 int level,
1218 							 int *serial,
1219 							 int max_depth,
1220 							 bool show_branch,
1221 							 bool show_serial,
1222 							 MemoryContext per_query_ctx,
1223 							 AttInMetadata *attinmeta,
1224 							 Tuplestorestate *tupstore)
1225 {
1226 	TupleDesc	tupdesc = attinmeta->tupdesc;
1227 	int			ret;
1228 	uint64		proc;
1229 	int			serial_column;
1230 	StringInfoData sql;
1231 	char	  **values;
1232 	char	   *current_key;
1233 	char	   *current_key_parent;
1234 	char		current_level[INT32_STRLEN];
1235 	char		serial_str[INT32_STRLEN];
1236 	char	   *current_branch;
1237 	HeapTuple	tuple;
1238 
1239 	if (max_depth > 0 && level > max_depth)
1240 		return;
1241 
1242 	initStringInfo(&sql);
1243 
1244 	/* Build initial sql statement */
1245 	if (!show_serial)
1246 	{
1247 		appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1248 						 key_fld,
1249 						 parent_key_fld,
1250 						 relname,
1251 						 parent_key_fld,
1252 						 quote_literal_cstr(start_with),
1253 						 key_fld, key_fld, parent_key_fld);
1254 		serial_column = 0;
1255 	}
1256 	else
1257 	{
1258 		appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1259 						 key_fld,
1260 						 parent_key_fld,
1261 						 relname,
1262 						 parent_key_fld,
1263 						 quote_literal_cstr(start_with),
1264 						 key_fld, key_fld, parent_key_fld,
1265 						 orderby_fld);
1266 		serial_column = 1;
1267 	}
1268 
1269 	if (show_branch)
1270 		values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1271 	else
1272 		values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1273 
1274 	/* First time through, do a little setup */
1275 	if (level == 0)
1276 	{
1277 		/* root value is the one we initially start with */
1278 		values[0] = start_with;
1279 
1280 		/* root value has no parent */
1281 		values[1] = NULL;
1282 
1283 		/* root level is 0 */
1284 		sprintf(current_level, "%d", level);
1285 		values[2] = current_level;
1286 
1287 		/* root branch is just starting root value */
1288 		if (show_branch)
1289 			values[3] = start_with;
1290 
1291 		/* root starts the serial with 1 */
1292 		if (show_serial)
1293 		{
1294 			sprintf(serial_str, "%d", (*serial)++);
1295 			if (show_branch)
1296 				values[4] = serial_str;
1297 			else
1298 				values[3] = serial_str;
1299 		}
1300 
1301 		/* construct the tuple */
1302 		tuple = BuildTupleFromCStrings(attinmeta, values);
1303 
1304 		/* now store it */
1305 		tuplestore_puttuple(tupstore, tuple);
1306 
1307 		/* increment level */
1308 		level++;
1309 	}
1310 
1311 	/* Retrieve the desired rows */
1312 	ret = SPI_execute(sql.data, true, 0);
1313 	proc = SPI_processed;
1314 
1315 	/* Check for qualifying tuples */
1316 	if ((ret == SPI_OK_SELECT) && (proc > 0))
1317 	{
1318 		HeapTuple	spi_tuple;
1319 		SPITupleTable *tuptable = SPI_tuptable;
1320 		TupleDesc	spi_tupdesc = tuptable->tupdesc;
1321 		uint64		i;
1322 		StringInfoData branchstr;
1323 		StringInfoData chk_branchstr;
1324 		StringInfoData chk_current_key;
1325 
1326 		/*
1327 		 * Check that return tupdesc is compatible with the one we got from
1328 		 * the query.
1329 		 */
1330 		compatConnectbyTupleDescs(tupdesc, spi_tupdesc);
1331 
1332 		initStringInfo(&branchstr);
1333 		initStringInfo(&chk_branchstr);
1334 		initStringInfo(&chk_current_key);
1335 
1336 		for (i = 0; i < proc; i++)
1337 		{
1338 			/* initialize branch for this pass */
1339 			appendStringInfoString(&branchstr, branch);
1340 			appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
1341 
1342 			/* get the next sql result tuple */
1343 			spi_tuple = tuptable->vals[i];
1344 
1345 			/* get the current key (might be NULL) */
1346 			current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
1347 
1348 			/* get the parent key (might be NULL) */
1349 			current_key_parent = SPI_getvalue(spi_tuple, spi_tupdesc, 2);
1350 
1351 			/* get the current level */
1352 			sprintf(current_level, "%d", level);
1353 
1354 			/* check to see if this key is also an ancestor */
1355 			if (current_key)
1356 			{
1357 				appendStringInfo(&chk_current_key, "%s%s%s",
1358 								 branch_delim, current_key, branch_delim);
1359 				if (strstr(chk_branchstr.data, chk_current_key.data))
1360 					ereport(ERROR,
1361 							(errcode(ERRCODE_INVALID_RECURSION),
1362 							 errmsg("infinite recursion detected")));
1363 			}
1364 
1365 			/* OK, extend the branch */
1366 			if (current_key)
1367 				appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
1368 			current_branch = branchstr.data;
1369 
1370 			/* build a tuple */
1371 			values[0] = current_key;
1372 			values[1] = current_key_parent;
1373 			values[2] = current_level;
1374 			if (show_branch)
1375 				values[3] = current_branch;
1376 			if (show_serial)
1377 			{
1378 				sprintf(serial_str, "%d", (*serial)++);
1379 				if (show_branch)
1380 					values[4] = serial_str;
1381 				else
1382 					values[3] = serial_str;
1383 			}
1384 
1385 			tuple = BuildTupleFromCStrings(attinmeta, values);
1386 
1387 			/* store the tuple for later use */
1388 			tuplestore_puttuple(tupstore, tuple);
1389 
1390 			heap_freetuple(tuple);
1391 
1392 			/* recurse using current_key as the new start_with */
1393 			if (current_key)
1394 				build_tuplestore_recursively(key_fld,
1395 											 parent_key_fld,
1396 											 relname,
1397 											 orderby_fld,
1398 											 branch_delim,
1399 											 current_key,
1400 											 current_branch,
1401 											 level + 1,
1402 											 serial,
1403 											 max_depth,
1404 											 show_branch,
1405 											 show_serial,
1406 											 per_query_ctx,
1407 											 attinmeta,
1408 											 tupstore);
1409 
1410 			xpfree(current_key);
1411 			xpfree(current_key_parent);
1412 
1413 			/* reset branch for next pass */
1414 			resetStringInfo(&branchstr);
1415 			resetStringInfo(&chk_branchstr);
1416 			resetStringInfo(&chk_current_key);
1417 		}
1418 
1419 		xpfree(branchstr.data);
1420 		xpfree(chk_branchstr.data);
1421 		xpfree(chk_current_key.data);
1422 	}
1423 }
1424 
1425 /*
1426  * Check expected (query runtime) tupdesc suitable for Connectby
1427  */
1428 static void
validateConnectbyTupleDesc(TupleDesc tupdesc,bool show_branch,bool show_serial)1429 validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
1430 {
1431 	int			serial_column = 0;
1432 
1433 	if (show_serial)
1434 		serial_column = 1;
1435 
1436 	/* are there the correct number of columns */
1437 	if (show_branch)
1438 	{
1439 		if (tupdesc->natts != (CONNECTBY_NCOLS + serial_column))
1440 			ereport(ERROR,
1441 					(errcode(ERRCODE_DATATYPE_MISMATCH),
1442 					 errmsg("invalid return type"),
1443 					 errdetail("Query-specified return tuple has " \
1444 							   "wrong number of columns.")));
1445 	}
1446 	else
1447 	{
1448 		if (tupdesc->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1449 			ereport(ERROR,
1450 					(errcode(ERRCODE_DATATYPE_MISMATCH),
1451 					 errmsg("invalid return type"),
1452 					 errdetail("Query-specified return tuple has " \
1453 							   "wrong number of columns.")));
1454 	}
1455 
1456 	/* check that the types of the first two columns match */
1457 	if (tupdesc->attrs[0]->atttypid != tupdesc->attrs[1]->atttypid)
1458 		ereport(ERROR,
1459 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1460 				 errmsg("invalid return type"),
1461 				 errdetail("First two columns must be the same type.")));
1462 
1463 	/* check that the type of the third column is INT4 */
1464 	if (tupdesc->attrs[2]->atttypid != INT4OID)
1465 		ereport(ERROR,
1466 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1467 				 errmsg("invalid return type"),
1468 				 errdetail("Third column must be type %s.",
1469 						   format_type_be(INT4OID))));
1470 
1471 	/* check that the type of the fourth column is TEXT if applicable */
1472 	if (show_branch && tupdesc->attrs[3]->atttypid != TEXTOID)
1473 		ereport(ERROR,
1474 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1475 				 errmsg("invalid return type"),
1476 				 errdetail("Fourth column must be type %s.",
1477 						   format_type_be(TEXTOID))));
1478 
1479 	/* check that the type of the fifth column is INT4 */
1480 	if (show_branch && show_serial && tupdesc->attrs[4]->atttypid != INT4OID)
1481 		ereport(ERROR,
1482 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1483 				 errmsg("query-specified return tuple not valid for Connectby: "
1484 						"fifth column must be type %s",
1485 						format_type_be(INT4OID))));
1486 
1487 	/* check that the type of the fifth column is INT4 */
1488 	if (!show_branch && show_serial && tupdesc->attrs[3]->atttypid != INT4OID)
1489 		ereport(ERROR,
1490 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1491 				 errmsg("query-specified return tuple not valid for Connectby: "
1492 						"fourth column must be type %s",
1493 						format_type_be(INT4OID))));
1494 
1495 	/* OK, the tupdesc is valid for our purposes */
1496 }
1497 
1498 /*
1499  * Check if spi sql tupdesc and return tupdesc are compatible
1500  */
1501 static void
compatConnectbyTupleDescs(TupleDesc ret_tupdesc,TupleDesc sql_tupdesc)1502 compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1503 {
1504 	Oid			ret_atttypid;
1505 	Oid			sql_atttypid;
1506 	int32		ret_atttypmod;
1507 	int32		sql_atttypmod;
1508 
1509 	/*
1510 	 * Result must have at least 2 columns.
1511 	 */
1512 	if (sql_tupdesc->natts < 2)
1513 		ereport(ERROR,
1514 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1515 				 errmsg("invalid return type"),
1516 				 errdetail("Query must return at least two columns.")));
1517 
1518 	/*
1519 	 * These columns must match the result type indicated by the calling
1520 	 * query.
1521 	 */
1522 	ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1523 	sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1524 	ret_atttypmod = ret_tupdesc->attrs[0]->atttypmod;
1525 	sql_atttypmod = sql_tupdesc->attrs[0]->atttypmod;
1526 	if (ret_atttypid != sql_atttypid ||
1527 		(ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1528 		ereport(ERROR,
1529 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1530 				 errmsg("invalid return type"),
1531 				 errdetail("SQL key field type %s does " \
1532 						   "not match return key field type %s.",
1533 						   format_type_with_typemod(ret_atttypid, ret_atttypmod),
1534 						   format_type_with_typemod(sql_atttypid, sql_atttypmod))));
1535 
1536 	ret_atttypid = ret_tupdesc->attrs[1]->atttypid;
1537 	sql_atttypid = sql_tupdesc->attrs[1]->atttypid;
1538 	ret_atttypmod = ret_tupdesc->attrs[1]->atttypmod;
1539 	sql_atttypmod = sql_tupdesc->attrs[1]->atttypmod;
1540 	if (ret_atttypid != sql_atttypid ||
1541 		(ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1542 		ereport(ERROR,
1543 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1544 				 errmsg("invalid return type"),
1545 				 errdetail("SQL parent key field type %s does " \
1546 						   "not match return parent key field type %s.",
1547 						   format_type_with_typemod(ret_atttypid, ret_atttypmod),
1548 						   format_type_with_typemod(sql_atttypid, sql_atttypmod))));
1549 
1550 	/* OK, the two tupdescs are compatible for our purposes */
1551 }
1552 
1553 /*
1554  * Check if two tupdescs match in type of attributes
1555  */
1556 static bool
compatCrosstabTupleDescs(TupleDesc ret_tupdesc,TupleDesc sql_tupdesc)1557 compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1558 {
1559 	int			i;
1560 	Form_pg_attribute ret_attr;
1561 	Oid			ret_atttypid;
1562 	Form_pg_attribute sql_attr;
1563 	Oid			sql_atttypid;
1564 
1565 	if (ret_tupdesc->natts < 2 ||
1566 		sql_tupdesc->natts < 3)
1567 		return false;
1568 
1569 	/* check the rowid types match */
1570 	ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1571 	sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1572 	if (ret_atttypid != sql_atttypid)
1573 		ereport(ERROR,
1574 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1575 				 errmsg("invalid return type"),
1576 				 errdetail("SQL rowid datatype does not match " \
1577 						   "return rowid datatype.")));
1578 
1579 	/*
1580 	 * - attribute [1] of the sql tuple is the category; no need to check it -
1581 	 * attribute [2] of the sql tuple should match attributes [1] to [natts]
1582 	 * of the return tuple
1583 	 */
1584 	sql_attr = sql_tupdesc->attrs[2];
1585 	for (i = 1; i < ret_tupdesc->natts; i++)
1586 	{
1587 		ret_attr = ret_tupdesc->attrs[i];
1588 
1589 		if (ret_attr->atttypid != sql_attr->atttypid)
1590 			return false;
1591 	}
1592 
1593 	/* OK, the two tupdescs are compatible for our purposes */
1594 	return true;
1595 }
1596