LCOV - code coverage report
Current view: top level - src/bin/psql - crosstabview.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 96.4 % 304 293
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 15 15
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 82.9 % 158 131

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

Generated by: LCOV version 2.3.2-1