LCOV - differential code coverage report
Current view: top level - contrib/tablefunc - tablefunc.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 94.6 % 424 401 23 401
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 19 19 19
Baseline: lcov-20250906-005545-baseline Branches: 71.1 % 353 251 102 251
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 1 1 1
(360..) days: 94.6 % 423 400 23 400
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 18 18 18
Branch coverage date bins:
(360..) days: 71.1 % 353 251 102 251

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

Generated by: LCOV version 2.4-beta