LCOV - code coverage report
Current view: top level - src/bin/psql - crosstabview.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 95.5 % 247 236
Test Date: 2026-07-23 06:15:50 Functions: 100.0 % 16 16
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 85.5 % 138 118

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * psql - the PostgreSQL interactive terminal
       3                 :             :  *
       4                 :             :  * Copyright (c) 2000-2026, PostgreSQL Global Development Group
       5                 :             :  *
       6                 :             :  * src/bin/psql/crosstabview.c
       7                 :             :  */
       8                 :             : #include "postgres_fe.h"
       9                 :             : 
      10                 :             : #include "catalog/pg_type_d.h"
      11                 :             : #include "common.h"
      12                 :             : #include "common/int.h"
      13                 :             : #include "common/logging.h"
      14                 :             : #include "crosstabview.h"
      15                 :             : #include "pqexpbuffer.h"
      16                 :             : #include "psqlscanslash.h"
      17                 :             : #include "settings.h"
      18                 :             : 
      19                 :             : /*
      20                 :             :  * Value/position from the resultset that goes into the horizontal or vertical
      21                 :             :  * crosstabview header.
      22                 :             :  */
      23                 :             : typedef struct _pivot_field
      24                 :             : {
      25                 :             :     /*
      26                 :             :      * Pointer obtained from PQgetvalue() for colV or colH. Each distinct
      27                 :             :      * value becomes an entry in the vertical header (colV), or horizontal
      28                 :             :      * header (colH). A Null value is represented by a NULL pointer.
      29                 :             :      */
      30                 :             :     char       *name;
      31                 :             : 
      32                 :             :     /*
      33                 :             :      * When a sort is requested on an alternative column, this holds
      34                 :             :      * PQgetvalue() for the sort column corresponding to <name>. If <name>
      35                 :             :      * appear multiple times, it's the first value in the order of the results
      36                 :             :      * that is kept. A Null value is represented by a NULL pointer.
      37                 :             :      */
      38                 :             :     char       *sort_value;
      39                 :             : 
      40                 :             :     /*
      41                 :             :      * Rank of this value, starting at 0. Initially, it's the relative
      42                 :             :      * position of the first appearance of <name> in the resultset. For
      43                 :             :      * example, if successive rows contain B,A,C,A,D then it's B:0,A:1,C:2,D:3
      44                 :             :      * When a sort column is specified, ranks get updated in a final pass to
      45                 :             :      * reflect the desired order.
      46                 :             :      */
      47                 :             :     int         rank;
      48                 :             : } pivot_field;
      49                 :             : 
      50                 :             : /* Node in avl_tree */
      51                 :             : typedef struct _avl_node
      52                 :             : {
      53                 :             :     /* Node contents */
      54                 :             :     pivot_field field;
      55                 :             : 
      56                 :             :     /*
      57                 :             :      * Height of this node in the tree (number of nodes on the longest path to
      58                 :             :      * a leaf).
      59                 :             :      */
      60                 :             :     int         height;
      61                 :             : 
      62                 :             :     /*
      63                 :             :      * Child nodes. [0] points to left subtree, [1] to right subtree. Never
      64                 :             :      * NULL, points to the empty node avl_tree.end when no left or right
      65                 :             :      * value.
      66                 :             :      */
      67                 :             :     struct _avl_node *children[2];
      68                 :             : } avl_node;
      69                 :             : 
      70                 :             : /*
      71                 :             :  * Control structure for the AVL tree (binary search tree kept
      72                 :             :  * balanced with the AVL algorithm)
      73                 :             :  */
      74                 :             : typedef struct _avl_tree
      75                 :             : {
      76                 :             :     int         count;          /* Total number of nodes */
      77                 :             :     avl_node   *root;           /* root of the tree */
      78                 :             :     avl_node   *end;            /* Immutable dereferenceable empty tree */
      79                 :             : } avl_tree;
      80                 :             : 
      81                 :             : 
      82                 :             : static bool printCrosstab(const PGresult *result,
      83                 :             :                           int num_columns, pivot_field *piv_columns, int field_for_columns,
      84                 :             :                           int num_rows, pivot_field *piv_rows, int field_for_rows,
      85                 :             :                           int field_for_data);
      86                 :             : static char *displayValue(char *value, Oid ftype, char *default_null);
      87                 :             : 
      88                 :             : static void avlInit(avl_tree *tree);
      89                 :             : static void avlMergeValue(avl_tree *tree, char *name, char *sort_value);
      90                 :             : static int  avlCollectFields(avl_tree *tree, avl_node *node,
      91                 :             :                              pivot_field *fields, int idx);
      92                 :             : static void avlFree(avl_tree *tree, avl_node *node);
      93                 :             : static void rankSort(int num_columns, pivot_field *piv_columns);
      94                 :             : static int  indexOfColumn(char *arg, const PGresult *res);
      95                 :             : static int  pivotFieldCompare(const void *a, const void *b);
      96                 :             : static int  rankCompare(const void *a, const void *b);
      97                 :             : 
      98                 :             : 
      99                 :             : /*
     100                 :             :  * Main entry point to this module.
     101                 :             :  *
     102                 :             :  * Process the data from *res according to the options in pset (global),
     103                 :             :  * to generate the horizontal and vertical headers contents,
     104                 :             :  * then call printCrosstab() for the actual output.
     105                 :             :  */
     106                 :             : bool
     107                 :          96 : PrintResultInCrosstab(const PGresult *res)
     108                 :             : {
     109                 :          96 :     bool        retval = false;
     110                 :             :     avl_tree    piv_columns;
     111                 :             :     avl_tree    piv_rows;
     112                 :          96 :     pivot_field *array_columns = NULL;
     113                 :          96 :     pivot_field *array_rows = NULL;
     114                 :          96 :     int         num_columns = 0;
     115                 :          96 :     int         num_rows = 0;
     116                 :             :     int         field_for_rows;
     117                 :             :     int         field_for_columns;
     118                 :             :     int         field_for_data;
     119                 :             :     int         sort_field_for_columns;
     120                 :             :     int         rn;
     121                 :             : 
     122                 :          96 :     avlInit(&piv_rows);
     123                 :          96 :     avlInit(&piv_columns);
     124                 :             : 
     125         [ -  + ]:          96 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     126                 :             :     {
     127                 :           0 :         pg_log_error("\\crosstabview: statement did not return a result set");
     128                 :           0 :         goto error_return;
     129                 :             :     }
     130                 :             : 
     131         [ +  + ]:          96 :     if (PQnfields(res) < 3)
     132                 :             :     {
     133                 :           8 :         pg_log_error("\\crosstabview: query must return at least three columns");
     134                 :           8 :         goto error_return;
     135                 :             :     }
     136                 :             : 
     137                 :             :     /* Process first optional arg (vertical header column) */
     138         [ +  + ]:          88 :     if (pset.ctv_args[0] == NULL)
     139                 :          20 :         field_for_rows = 0;
     140                 :             :     else
     141                 :             :     {
     142                 :          68 :         field_for_rows = indexOfColumn(pset.ctv_args[0], res);
     143         [ -  + ]:          68 :         if (field_for_rows < 0)
     144                 :           0 :             goto error_return;
     145                 :             :     }
     146                 :             : 
     147                 :             :     /* Process second optional arg (horizontal header column) */
     148         [ +  + ]:          88 :     if (pset.ctv_args[1] == NULL)
     149                 :          20 :         field_for_columns = 1;
     150                 :             :     else
     151                 :             :     {
     152                 :          68 :         field_for_columns = indexOfColumn(pset.ctv_args[1], res);
     153         [ +  + ]:          68 :         if (field_for_columns < 0)
     154                 :           4 :             goto error_return;
     155                 :             :     }
     156                 :             : 
     157                 :             :     /* Insist that header columns be distinct */
     158         [ +  + ]:          84 :     if (field_for_columns == field_for_rows)
     159                 :             :     {
     160                 :           4 :         pg_log_error("\\crosstabview: vertical and horizontal headers must be different columns");
     161                 :           4 :         goto error_return;
     162                 :             :     }
     163                 :             : 
     164                 :             :     /* Process third optional arg (data column) */
     165         [ +  + ]:          80 :     if (pset.ctv_args[2] == NULL)
     166                 :             :     {
     167                 :             :         int         i;
     168                 :             : 
     169                 :             :         /*
     170                 :             :          * If the data column was not specified, we search for the one not
     171                 :             :          * used as either vertical or horizontal headers.  Must be exactly
     172                 :             :          * three columns, or this won't be unique.
     173                 :             :          */
     174         [ -  + ]:          24 :         if (PQnfields(res) != 3)
     175                 :             :         {
     176                 :           0 :             pg_log_error("\\crosstabview: data column must be specified when query returns more than three columns");
     177                 :           0 :             goto error_return;
     178                 :             :         }
     179                 :             : 
     180                 :          24 :         field_for_data = -1;
     181         [ +  - ]:          72 :         for (i = 0; i < PQnfields(res); i++)
     182                 :             :         {
     183   [ +  +  +  + ]:          72 :             if (i != field_for_rows && i != field_for_columns)
     184                 :             :             {
     185                 :          24 :                 field_for_data = i;
     186                 :          24 :                 break;
     187                 :             :             }
     188                 :             :         }
     189                 :             :         Assert(field_for_data >= 0);
     190                 :             :     }
     191                 :             :     else
     192                 :             :     {
     193                 :          56 :         field_for_data = indexOfColumn(pset.ctv_args[2], res);
     194         [ +  + ]:          56 :         if (field_for_data < 0)
     195                 :          12 :             goto error_return;
     196                 :             :     }
     197                 :             : 
     198                 :             :     /* Process fourth optional arg (horizontal header sort column) */
     199         [ +  + ]:          68 :     if (pset.ctv_args[3] == NULL)
     200                 :          48 :         sort_field_for_columns = -1;    /* no sort column */
     201                 :             :     else
     202                 :             :     {
     203                 :          20 :         sort_field_for_columns = indexOfColumn(pset.ctv_args[3], res);
     204         [ -  + ]:          20 :         if (sort_field_for_columns < 0)
     205                 :           0 :             goto error_return;
     206                 :             :     }
     207                 :             : 
     208                 :             :     /*
     209                 :             :      * First part: accumulate the names that go into the vertical and
     210                 :             :      * horizontal headers, each into an AVL binary tree to build the set of
     211                 :             :      * DISTINCT values.
     212                 :             :      */
     213                 :             : 
     214         [ +  + ]:        6792 :     for (rn = 0; rn < PQntuples(res); rn++)
     215                 :             :     {
     216                 :             :         char       *val;
     217                 :             :         char       *val1;
     218                 :             : 
     219                 :             :         /* horizontal */
     220         [ +  + ]:        6728 :         val = PQgetisnull(res, rn, field_for_columns) ? NULL :
     221                 :        6680 :             PQgetvalue(res, rn, field_for_columns);
     222                 :        6728 :         val1 = NULL;
     223                 :             : 
     224   [ +  +  +  - ]:        6828 :         if (sort_field_for_columns >= 0 &&
     225                 :         100 :             !PQgetisnull(res, rn, sort_field_for_columns))
     226                 :         100 :             val1 = PQgetvalue(res, rn, sort_field_for_columns);
     227                 :             : 
     228                 :        6728 :         avlMergeValue(&piv_columns, val, val1);
     229                 :             : 
     230         [ +  + ]:        6728 :         if (piv_columns.count > CROSSTABVIEW_MAX_COLUMNS)
     231                 :             :         {
     232                 :           4 :             pg_log_error("\\crosstabview: maximum number of columns (%d) exceeded",
     233                 :             :                          CROSSTABVIEW_MAX_COLUMNS);
     234                 :           4 :             goto error_return;
     235                 :             :         }
     236                 :             : 
     237                 :             :         /* vertical */
     238         [ +  + ]:        6724 :         val = PQgetisnull(res, rn, field_for_rows) ? NULL :
     239                 :        6704 :             PQgetvalue(res, rn, field_for_rows);
     240                 :             : 
     241                 :        6724 :         avlMergeValue(&piv_rows, val, NULL);
     242                 :             :     }
     243                 :             : 
     244                 :             :     /*
     245                 :             :      * Second part: Generate sorted arrays from the AVL trees.
     246                 :             :      */
     247                 :             : 
     248                 :          64 :     num_columns = piv_columns.count;
     249                 :          64 :     num_rows = piv_rows.count;
     250                 :             : 
     251                 :          64 :     array_columns = pg_malloc_array(pivot_field, num_columns);
     252                 :             : 
     253                 :          64 :     array_rows = pg_malloc_array(pivot_field, num_rows);
     254                 :             : 
     255                 :          64 :     avlCollectFields(&piv_columns, piv_columns.root, array_columns, 0);
     256                 :          64 :     avlCollectFields(&piv_rows, piv_rows.root, array_rows, 0);
     257                 :             : 
     258                 :             :     /*
     259                 :             :      * Third part: optionally, process the ranking data for the horizontal
     260                 :             :      * header
     261                 :             :      */
     262         [ +  + ]:          64 :     if (sort_field_for_columns >= 0)
     263                 :          20 :         rankSort(num_columns, array_columns);
     264                 :             : 
     265                 :             :     /*
     266                 :             :      * Fourth part: print the crosstab'ed result.
     267                 :             :      */
     268                 :          64 :     retval = printCrosstab(res,
     269                 :             :                            num_columns, array_columns, field_for_columns,
     270                 :             :                            num_rows, array_rows, field_for_rows,
     271                 :             :                            field_for_data);
     272                 :             : 
     273                 :          96 : error_return:
     274                 :          96 :     avlFree(&piv_columns, piv_columns.root);
     275                 :          96 :     avlFree(&piv_rows, piv_rows.root);
     276                 :          96 :     pg_free(array_columns);
     277                 :          96 :     pg_free(array_rows);
     278                 :             : 
     279                 :          96 :     return retval;
     280                 :             : }
     281                 :             : 
     282                 :             : /*
     283                 :             :  * Output the pivoted resultset with the printTable* functions.  Return true
     284                 :             :  * if successful, false otherwise.
     285                 :             :  */
     286                 :             : static bool
     287                 :          64 : printCrosstab(const PGresult *result,
     288                 :             :               int num_columns, pivot_field *piv_columns, int field_for_columns,
     289                 :             :               int num_rows, pivot_field *piv_rows, int field_for_rows,
     290                 :             :               int field_for_data)
     291                 :             : {
     292                 :          64 :     printQueryOpt popt = pset.popt;
     293                 :             :     printTableContent cont;
     294                 :             :     int         rn;
     295                 :             :     char        col_align;
     296                 :             :     int        *horiz_map;
     297                 :          64 :     Oid         col_ftype = PQftype(result, field_for_columns);
     298                 :          64 :     Oid         row_ftype = PQftype(result, field_for_rows);
     299                 :          64 :     Oid         data_ftype = PQftype(result, field_for_data);
     300                 :          64 :     bool        retval = false;
     301                 :             : 
     302                 :          64 :     printTableInit(&cont, &popt.topt, popt.title, num_columns + 1, num_rows);
     303                 :             : 
     304                 :             :     /* Step 1: set target column names (horizontal header) */
     305                 :             : 
     306                 :             :     /* The name of the first column is kept unchanged by the pivoting */
     307                 :          64 :     printTableAddHeader(&cont,
     308                 :             :                         PQfname(result, field_for_rows),
     309                 :             :                         false,
     310                 :          64 :                         column_type_alignment(row_ftype));
     311                 :             : 
     312                 :             :     /*
     313                 :             :      * To iterate over piv_columns[] by piv_columns[].rank, create a reverse
     314                 :             :      * map associating each piv_columns[].rank to its index in piv_columns.
     315                 :             :      * This avoids an O(N^2) loop later.
     316                 :             :      */
     317                 :          64 :     horiz_map = pg_malloc_array(int, num_columns);
     318         [ +  + ]:         332 :     for (int i = 0; i < num_columns; i++)
     319                 :         268 :         horiz_map[piv_columns[i].rank] = i;
     320                 :             : 
     321                 :             :     /*
     322                 :             :      * The display alignment depends on its PQftype().
     323                 :             :      */
     324                 :          64 :     col_align = column_type_alignment(data_ftype);
     325                 :             : 
     326         [ +  + ]:         332 :     for (int i = 0; i < num_columns; i++)
     327                 :             :     {
     328                 :             :         char       *colname;
     329                 :             : 
     330                 :         268 :         colname = displayValue(piv_columns[horiz_map[i]].name, col_ftype, "");
     331                 :             : 
     332                 :         268 :         printTableAddHeader(&cont, colname, false, col_align);
     333                 :             :     }
     334                 :          64 :     pg_free(horiz_map);
     335                 :             : 
     336                 :             :     /* Step 2: set row names in the first output column (vertical header) */
     337         [ +  + ]:         228 :     for (int i = 0; i < num_rows; i++)
     338                 :             :     {
     339                 :         164 :         int         k = piv_rows[i].rank;
     340                 :         164 :         int         idx = k * (num_columns + 1);
     341                 :             : 
     342                 :         164 :         cont.cells[idx] = displayValue(piv_rows[i].name, row_ftype, "");
     343                 :             :     }
     344                 :          64 :     cont.cellsadded = num_rows * (num_columns + 1);
     345                 :             : 
     346                 :             :     /*
     347                 :             :      * Step 3: fill in the content cells.
     348                 :             :      */
     349         [ +  + ]:         380 :     for (rn = 0; rn < PQntuples(result); rn++)
     350                 :             :     {
     351                 :             :         int         row_number;
     352                 :             :         int         col_number;
     353                 :             :         pivot_field *rp,
     354                 :             :                    *cp;
     355                 :             :         pivot_field elt;
     356                 :             : 
     357                 :             :         /* Find target row */
     358         [ +  + ]:         324 :         if (!PQgetisnull(result, rn, field_for_rows))
     359                 :         304 :             elt.name = PQgetvalue(result, rn, field_for_rows);
     360                 :             :         else
     361                 :          20 :             elt.name = NULL;
     362                 :         324 :         rp = (pivot_field *) bsearch(&elt,
     363                 :             :                                      piv_rows,
     364                 :             :                                      num_rows,
     365                 :             :                                      sizeof(pivot_field),
     366                 :             :                                      pivotFieldCompare);
     367                 :             :         Assert(rp != NULL);
     368                 :         324 :         row_number = rp->rank;
     369                 :             : 
     370                 :             :         /* Find target column */
     371         [ +  + ]:         324 :         if (!PQgetisnull(result, rn, field_for_columns))
     372                 :         276 :             elt.name = PQgetvalue(result, rn, field_for_columns);
     373                 :             :         else
     374                 :          48 :             elt.name = NULL;
     375                 :             : 
     376                 :         324 :         cp = (pivot_field *) bsearch(&elt,
     377                 :             :                                      piv_columns,
     378                 :             :                                      num_columns,
     379                 :             :                                      sizeof(pivot_field),
     380                 :             :                                      pivotFieldCompare);
     381                 :             :         Assert(cp != NULL);
     382                 :         324 :         col_number = cp->rank;
     383                 :             : 
     384                 :             :         /* Place value into cell */
     385   [ +  -  +  - ]:         324 :         if (col_number >= 0 && row_number >= 0)
     386                 :             :         {
     387                 :             :             int         idx;
     388                 :             :             char       *value;
     389                 :             : 
     390                 :             :             /* index into the cont.cells array */
     391                 :         324 :             idx = 1 + col_number + row_number * (num_columns + 1);
     392                 :             : 
     393                 :             :             /*
     394                 :             :              * If the cell already contains a value, raise an error.
     395                 :             :              */
     396         [ +  + ]:         324 :             if (cont.cells[idx] != NULL)
     397                 :             :             {
     398                 :           8 :                 pg_log_error("\\crosstabview: query result contains multiple data values for row \"%s\", column \"%s\"",
     399                 :             :                              displayValue(rp->name, row_ftype, "(null)"),
     400                 :             :                              displayValue(cp->name, col_ftype, "(null)"));
     401                 :           8 :                 goto error;
     402                 :             :             }
     403                 :             : 
     404         [ +  + ]:         316 :             if (PQgetisnull(result, rn, field_for_data))
     405                 :          12 :                 value = NULL;
     406                 :             :             else
     407                 :         304 :                 value = PQgetvalue(result, rn, field_for_data);
     408                 :         316 :             cont.cells[idx] = displayValue(value, data_ftype, "");
     409                 :             :         }
     410                 :             :     }
     411                 :             : 
     412                 :             :     /*
     413                 :             :      * The non-initialized cells must be set to an empty string for the print
     414                 :             :      * functions
     415                 :             :      */
     416         [ +  + ]:         820 :     for (uint64 i = 0; i < cont.cellsadded; i++)
     417                 :             :     {
     418         [ +  + ]:         764 :         if (cont.cells[i] == NULL)
     419                 :         336 :             cont.cells[i] = "";
     420                 :             :     }
     421                 :             : 
     422                 :          56 :     printTable(&cont, pset.queryFout, false, pset.logfile);
     423                 :          56 :     retval = true;
     424                 :             : 
     425                 :          64 : error:
     426                 :          64 :     printTableCleanup(&cont);
     427                 :             : 
     428                 :          64 :     return retval;
     429                 :             : }
     430                 :             : 
     431                 :             : /*
     432                 :             :  * Return the display representation of one cell value in \crosstabview,
     433                 :             :  * following pset substitutions.
     434                 :             :  *
     435                 :             :  * The returned pointer is not to be freed.
     436                 :             :  */
     437                 :             : static char *
     438                 :         764 : displayValue(char *value, Oid ftype, char *default_null)
     439                 :             : {
     440                 :         764 :     printQueryOpt popt = pset.popt;
     441                 :             : 
     442         [ +  + ]:         764 :     if (value == NULL)
     443         [ +  + ]:          64 :         value = popt.nullPrint ? popt.nullPrint : default_null;
     444         [ +  + ]:         700 :     else if (ftype == BOOLOID)
     445                 :             :     {
     446   [ +  +  +  - ]:          48 :         if (value[0] == 't' && popt.truePrint)
     447                 :          28 :             value = popt.truePrint;
     448   [ +  -  +  - ]:          20 :         else if (value[0] == 'f' && popt.falsePrint)
     449                 :          20 :             value = popt.falsePrint;
     450                 :             :     }
     451                 :             : 
     452                 :         764 :     return value;
     453                 :             : }
     454                 :             : 
     455                 :             : /*
     456                 :             :  * The avl* functions below provide a minimalistic implementation of AVL binary
     457                 :             :  * trees, to efficiently collect the distinct values that will form the horizontal
     458                 :             :  * and vertical headers. It only supports adding new values, no removal or even
     459                 :             :  * search.
     460                 :             :  */
     461                 :             : static void
     462                 :         192 : avlInit(avl_tree *tree)
     463                 :             : {
     464                 :         192 :     tree->end = pg_malloc0_object(avl_node);
     465                 :         192 :     tree->end->children[0] = tree->end->children[1] = tree->end;
     466                 :         192 :     tree->count = 0;
     467                 :         192 :     tree->root = tree->end;
     468                 :         192 : }
     469                 :             : 
     470                 :             : /* Deallocate recursively an AVL tree, starting from node */
     471                 :             : static void
     472                 :       13292 : avlFree(avl_tree *tree, avl_node *node)
     473                 :             : {
     474         [ +  + ]:       13292 :     if (node->children[0] != tree->end)
     475                 :             :     {
     476                 :        6712 :         avlFree(tree, node->children[0]);
     477                 :        6712 :         pg_free(node->children[0]);
     478                 :             :     }
     479         [ +  + ]:       13292 :     if (node->children[1] != tree->end)
     480                 :             :     {
     481                 :        6388 :         avlFree(tree, node->children[1]);
     482                 :        6388 :         pg_free(node->children[1]);
     483                 :             :     }
     484         [ +  + ]:       13292 :     if (node == tree->root)
     485                 :             :     {
     486                 :             :         /* free the root separately as it's not child of anything */
     487         [ +  + ]:         192 :         if (node != tree->end)
     488                 :         136 :             pg_free(node);
     489                 :             :         /* free the tree->end struct only once and when all else is freed */
     490                 :         192 :         pg_free(tree->end);
     491                 :             :     }
     492                 :       13292 : }
     493                 :             : 
     494                 :             : /* Set the height to 1 plus the greatest of left and right heights */
     495                 :             : static void
     496                 :      150380 : avlUpdateHeight(avl_node *n)
     497                 :             : {
     498                 :      150380 :     n->height = 1 + (n->children[0]->height > n->children[1]->height ?
     499                 :      150380 :                      n->children[0]->height :
     500                 :             :                      n->children[1]->height);
     501                 :      150380 : }
     502                 :             : 
     503                 :             : /* Rotate a subtree left (dir=0) or right (dir=1). Not recursive */
     504                 :             : static avl_node *
     505                 :       16148 : avlRotate(avl_node **current, int dir)
     506                 :             : {
     507                 :       16148 :     avl_node   *before = *current;
     508                 :       16148 :     avl_node   *after = (*current)->children[dir];
     509                 :             : 
     510                 :       16148 :     *current = after;
     511                 :       16148 :     before->children[dir] = after->children[!dir];
     512                 :       16148 :     avlUpdateHeight(before);
     513                 :       16148 :     after->children[!dir] = before;
     514                 :             : 
     515                 :       16148 :     return after;
     516                 :             : }
     517                 :             : 
     518                 :             : static int
     519                 :      146124 : avlBalance(avl_node *n)
     520                 :             : {
     521                 :      146124 :     return n->children[0]->height - n->children[1]->height;
     522                 :             : }
     523                 :             : 
     524                 :             : /*
     525                 :             :  * After an insertion, possibly rebalance the tree so that the left and right
     526                 :             :  * node heights don't differ by more than 1.
     527                 :             :  * May update *node.
     528                 :             :  */
     529                 :             : static void
     530                 :      134232 : avlAdjustBalance(avl_tree *tree, avl_node **node)
     531                 :             : {
     532                 :      134232 :     avl_node   *current = *node;
     533                 :      134232 :     int         b = avlBalance(current) / 2;
     534                 :             : 
     535         [ +  + ]:      134232 :     if (b != 0)
     536                 :             :     {
     537                 :       11892 :         int         dir = (1 - b) / 2;
     538                 :             : 
     539         [ +  + ]:       11892 :         if (avlBalance(current->children[dir]) == -b)
     540                 :        4256 :             avlRotate(&current->children[dir], !dir);
     541                 :       11892 :         current = avlRotate(node, dir);
     542                 :             :     }
     543         [ +  - ]:      134232 :     if (current != tree->end)
     544                 :      134232 :         avlUpdateHeight(current);
     545                 :      134232 : }
     546                 :             : 
     547                 :             : /*
     548                 :             :  * Insert a new value/field, starting from *node, reaching the correct position
     549                 :             :  * in the tree by recursion.  Possibly rebalance the tree and possibly update
     550                 :             :  * *node.  Do nothing if the value is already present in the tree.
     551                 :             :  */
     552                 :             : static void
     553                 :      147684 : avlInsertNode(avl_tree *tree, avl_node **node, pivot_field field)
     554                 :             : {
     555                 :      147684 :     avl_node   *current = *node;
     556                 :             : 
     557         [ +  + ]:      147684 :     if (current == tree->end)
     558                 :             :     {
     559                 :       13236 :         avl_node   *new_node = pg_malloc_object(avl_node);
     560                 :             : 
     561                 :       13236 :         new_node->height = 1;
     562                 :       13236 :         new_node->field = field;
     563                 :       13236 :         new_node->children[0] = new_node->children[1] = tree->end;
     564                 :       13236 :         tree->count++;
     565                 :       13236 :         *node = new_node;
     566                 :             :     }
     567                 :             :     else
     568                 :             :     {
     569                 :      134448 :         int         cmp = pivotFieldCompare(&field, &current->field);
     570                 :             : 
     571         [ +  + ]:      134448 :         if (cmp != 0)
     572                 :             :         {
     573         [ +  + ]:      134232 :             avlInsertNode(tree,
     574                 :             :                           cmp > 0 ? &current->children[1] : &current->children[0],
     575                 :             :                           field);
     576                 :      134232 :             avlAdjustBalance(tree, node);
     577                 :             :         }
     578                 :             :     }
     579                 :      147684 : }
     580                 :             : 
     581                 :             : /* Insert the value into the AVL tree, if it does not preexist */
     582                 :             : static void
     583                 :       13452 : avlMergeValue(avl_tree *tree, char *name, char *sort_value)
     584                 :             : {
     585                 :             :     pivot_field field;
     586                 :             : 
     587                 :       13452 :     field.name = name;
     588                 :       13452 :     field.rank = tree->count;
     589                 :       13452 :     field.sort_value = sort_value;
     590                 :       13452 :     avlInsertNode(tree, &tree->root, field);
     591                 :       13452 : }
     592                 :             : 
     593                 :             : /*
     594                 :             :  * Recursively extract node values into the names array, in sorted order with a
     595                 :             :  * left-to-right tree traversal.
     596                 :             :  * Return the next candidate offset to write into the names array.
     597                 :             :  * fields[] must be preallocated to hold tree->count entries
     598                 :             :  */
     599                 :             : static int
     600                 :         992 : avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
     601                 :             : {
     602         [ +  + ]:         992 :     if (node == tree->end)
     603                 :         560 :         return idx;
     604                 :             : 
     605                 :         432 :     idx = avlCollectFields(tree, node->children[0], fields, idx);
     606                 :         432 :     fields[idx] = node->field;
     607                 :         432 :     return avlCollectFields(tree, node->children[1], fields, idx + 1);
     608                 :             : }
     609                 :             : 
     610                 :             : static void
     611                 :          20 : rankSort(int num_columns, pivot_field *piv_columns)
     612                 :             : {
     613                 :             :     int        *hmap;           /* [[offset in piv_columns, rank], ...for
     614                 :             :                                  * every header entry] */
     615                 :             :     int         i;
     616                 :             : 
     617                 :          20 :     hmap = pg_malloc_array(int, num_columns * 2);
     618         [ +  + ]:         104 :     for (i = 0; i < num_columns; i++)
     619                 :             :     {
     620                 :          84 :         char       *val = piv_columns[i].sort_value;
     621                 :             : 
     622                 :             :         /* ranking information is valid if non null and matches /^-?\d+$/ */
     623         [ +  - ]:          84 :         if (val &&
     624         [ -  + ]:          84 :             ((*val == '-' &&
     625         [ #  # ]:           0 :               strspn(val + 1, "0123456789") == strlen(val + 1)) ||
     626         [ +  - ]:          84 :              strspn(val, "0123456789") == strlen(val)))
     627                 :             :         {
     628                 :          84 :             hmap[i * 2] = atoi(val);
     629                 :          84 :             hmap[i * 2 + 1] = i;
     630                 :             :         }
     631                 :             :         else
     632                 :             :         {
     633                 :             :             /* invalid rank information ignored (equivalent to rank 0) */
     634                 :           0 :             hmap[i * 2] = 0;
     635                 :           0 :             hmap[i * 2 + 1] = i;
     636                 :             :         }
     637                 :             :     }
     638                 :             : 
     639                 :          20 :     qsort(hmap, num_columns, sizeof(int) * 2, rankCompare);
     640                 :             : 
     641         [ +  + ]:         104 :     for (i = 0; i < num_columns; i++)
     642                 :             :     {
     643                 :          84 :         piv_columns[hmap[i * 2 + 1]].rank = i;
     644                 :             :     }
     645                 :             : 
     646                 :          20 :     pg_free(hmap);
     647                 :          20 : }
     648                 :             : 
     649                 :             : /*
     650                 :             :  * Look up a column reference, which can be either:
     651                 :             :  * - a number from 1 to PQnfields(res)
     652                 :             :  * - a column name matching one of PQfname(res,...)
     653                 :             :  *
     654                 :             :  * Returns zero-based column number, or -1 if not found or ambiguous.
     655                 :             :  *
     656                 :             :  * Note: may modify contents of "arg" string.
     657                 :             :  */
     658                 :             : static int
     659                 :         212 : indexOfColumn(char *arg, const PGresult *res)
     660                 :             : {
     661                 :             :     int         idx;
     662                 :             : 
     663   [ +  -  +  + ]:         212 :     if (arg[0] && strspn(arg, "0123456789") == strlen(arg))
     664                 :             :     {
     665                 :             :         /* if arg contains only digits, it's a column number */
     666                 :          64 :         idx = atoi(arg) - 1;
     667   [ +  -  +  + ]:          64 :         if (idx < 0 || idx >= PQnfields(res))
     668                 :             :         {
     669                 :           4 :             pg_log_error("\\crosstabview: column number %d is out of range 1..%d",
     670                 :             :                          idx + 1, PQnfields(res));
     671                 :           4 :             return -1;
     672                 :             :         }
     673                 :             :     }
     674                 :             :     else
     675                 :             :     {
     676                 :             :         int         i;
     677                 :             : 
     678                 :             :         /*
     679                 :             :          * Dequote and downcase the column name.  By checking for all-digits
     680                 :             :          * before doing this, we can ensure that a quoted name is treated as a
     681                 :             :          * name even if it's all digits.
     682                 :             :          */
     683                 :         148 :         dequote_downcase_identifier(arg, true, pset.encoding);
     684                 :             : 
     685                 :             :         /* Now look for match(es) among res' column names */
     686                 :         148 :         idx = -1;
     687         [ +  + ]:         688 :         for (i = 0; i < PQnfields(res); i++)
     688                 :             :         {
     689         [ +  + ]:         540 :             if (strcmp(arg, PQfname(res, i)) == 0)
     690                 :             :             {
     691         [ -  + ]:         136 :                 if (idx >= 0)
     692                 :             :                 {
     693                 :             :                     /* another idx was already found for the same name */
     694                 :           0 :                     pg_log_error("\\crosstabview: ambiguous column name: \"%s\"", arg);
     695                 :           0 :                     return -1;
     696                 :             :                 }
     697                 :         136 :                 idx = i;
     698                 :             :             }
     699                 :             :         }
     700         [ +  + ]:         148 :         if (idx == -1)
     701                 :             :         {
     702                 :          12 :             pg_log_error("\\crosstabview: column name not found: \"%s\"", arg);
     703                 :          12 :             return -1;
     704                 :             :         }
     705                 :             :     }
     706                 :             : 
     707                 :         196 :     return idx;
     708                 :             : }
     709                 :             : 
     710                 :             : /*
     711                 :             :  * Value comparator for vertical and horizontal headers
     712                 :             :  * used for deduplication only.
     713                 :             :  * - null values are considered equal
     714                 :             :  * - non-null < null
     715                 :             :  * - non-null values are compared with strcmp()
     716                 :             :  */
     717                 :             : static int
     718                 :      135624 : pivotFieldCompare(const void *a, const void *b)
     719                 :             : {
     720                 :      135624 :     const pivot_field *pa = (const pivot_field *) a;
     721                 :      135624 :     const pivot_field *pb = (const pivot_field *) b;
     722                 :             : 
     723                 :             :     /* test null values */
     724         [ +  + ]:      135624 :     if (!pb->name)
     725         [ +  + ]:         140 :         return pa->name ? -1 : 0;
     726         [ +  + ]:      135484 :     else if (!pa->name)
     727                 :         124 :         return 1;
     728                 :             : 
     729                 :             :     /* non-null values */
     730                 :      135360 :     return strcmp(pa->name, pb->name);
     731                 :             : }
     732                 :             : 
     733                 :             : static int
     734                 :          96 : rankCompare(const void *a, const void *b)
     735                 :             : {
     736                 :          96 :     return pg_cmp_s32(*(const int *) a, *(const int *) b);
     737                 :             : }
        

Generated by: LCOV version 2.0-1