LCOV - code coverage report
Current view: top level - contrib/tablefunc - tablefunc.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 94.6 % 423 400
Test Date: 2026-07-25 22:15:46 Functions: 100.0 % 19 19
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 71.2 % 351 250

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

Generated by: LCOV version 2.0-1