PostgreSQL Source Code  git master
crosstabview.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
#include "psqlscanslash.h"
#include "settings.h"
Include dependency graph for crosstabview.c:

Go to the source code of this file.

Data Structures

struct  _pivot_field
 
struct  _avl_node
 
struct  _avl_tree
 

Typedefs

typedef struct _pivot_field pivot_field
 
typedef struct _avl_node avl_node
 
typedef struct _avl_tree avl_tree
 

Functions

static bool printCrosstab (const PGresult *result, int num_columns, pivot_field *piv_columns, int field_for_columns, int num_rows, pivot_field *piv_rows, int field_for_rows, int field_for_data)
 
static void avlInit (avl_tree *tree)
 
static void avlMergeValue (avl_tree *tree, char *name, char *sort_value)
 
static int avlCollectFields (avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
 
static void avlFree (avl_tree *tree, avl_node *node)
 
static void rankSort (int num_columns, pivot_field *piv_columns)
 
static int indexOfColumn (char *arg, const PGresult *res)
 
static int pivotFieldCompare (const void *a, const void *b)
 
static int rankCompare (const void *a, const void *b)
 
bool PrintResultInCrosstab (const PGresult *res)
 
static void avlUpdateHeight (avl_node *n)
 
static avl_nodeavlRotate (avl_node **current, int dir)
 
static int avlBalance (avl_node *n)
 
static void avlAdjustBalance (avl_tree *tree, avl_node **node)
 
static void avlInsertNode (avl_tree *tree, avl_node **node, pivot_field field)
 

Typedef Documentation

◆ avl_node

typedef struct _avl_node avl_node

◆ avl_tree

typedef struct _avl_tree avl_tree

◆ pivot_field

typedef struct _pivot_field pivot_field

Function Documentation

◆ avlAdjustBalance()

static void avlAdjustBalance ( avl_tree tree,
avl_node **  node 
)
static

Definition at line 506 of file crosstabview.c.

507 {
508  avl_node *current = *node;
509  int b = avlBalance(current) / 2;
510 
511  if (b != 0)
512  {
513  int dir = (1 - b) / 2;
514 
515  if (avlBalance(current->children[dir]) == -b)
516  avlRotate(&current->children[dir], !dir);
517  current = avlRotate(node, dir);
518  }
519  if (current != tree->end)
520  avlUpdateHeight(current);
521 }
static void avlUpdateHeight(avl_node *n)
Definition: crosstabview.c:472
static avl_node * avlRotate(avl_node **current, int dir)
Definition: crosstabview.c:481
static int avlBalance(avl_node *n)
Definition: crosstabview.c:495
int b
Definition: isn.c:70
tree
Definition: radixtree.h:1828
struct _avl_node * children[2]
Definition: crosstabview.c:66

References avlBalance(), avlRotate(), avlUpdateHeight(), b, _avl_node::children, and tree.

Referenced by avlInsertNode().

◆ avlBalance()

static int avlBalance ( avl_node n)
static

Definition at line 495 of file crosstabview.c.

496 {
497  return n->children[0]->height - n->children[1]->height;
498 }

References _avl_node::children, and _avl_node::height.

Referenced by avlAdjustBalance().

◆ avlCollectFields()

static int avlCollectFields ( avl_tree tree,
avl_node node,
pivot_field fields,
int  idx 
)
static

Definition at line 577 of file crosstabview.c.

578 {
579  if (node == tree->end)
580  return idx;
581 
582  idx = avlCollectFields(tree, node->children[0], fields, idx);
583  fields[idx] = node->field;
584  return avlCollectFields(tree, node->children[1], fields, idx + 1);
585 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static int avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
Definition: crosstabview.c:577
pivot_field field
Definition: crosstabview.c:53

References _avl_node::children, _avl_node::field, idx(), and tree.

Referenced by PrintResultInCrosstab().

◆ avlFree()

static void avlFree ( avl_tree tree,
avl_node node 
)
static

Definition at line 448 of file crosstabview.c.

449 {
450  if (node->children[0] != tree->end)
451  {
452  avlFree(tree, node->children[0]);
453  pg_free(node->children[0]);
454  }
455  if (node->children[1] != tree->end)
456  {
457  avlFree(tree, node->children[1]);
458  pg_free(node->children[1]);
459  }
460  if (node == tree->root)
461  {
462  /* free the root separately as it's not child of anything */
463  if (node != tree->end)
464  pg_free(node);
465  /* free the tree->end struct only once and when all else is freed */
466  pg_free(tree->end);
467  }
468 }
static void avlFree(avl_tree *tree, avl_node *node)
Definition: crosstabview.c:448
void pg_free(void *ptr)
Definition: fe_memutils.c:105

References _avl_node::children, pg_free(), and tree.

Referenced by PrintResultInCrosstab().

◆ avlInit()

static void avlInit ( avl_tree tree)
static

Definition at line 438 of file crosstabview.c.

439 {
440  tree->end = (avl_node *) pg_malloc0(sizeof(avl_node));
441  tree->end->children[0] = tree->end->children[1] = tree->end;
442  tree->count = 0;
443  tree->root = tree->end;
444 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53

References pg_malloc0(), and tree.

Referenced by PrintResultInCrosstab().

◆ avlInsertNode()

static void avlInsertNode ( avl_tree tree,
avl_node **  node,
pivot_field  field 
)
static

Definition at line 529 of file crosstabview.c.

530 {
531  avl_node *current = *node;
532 
533  if (current == tree->end)
534  {
535  avl_node *new_node = (avl_node *)
536  pg_malloc(sizeof(avl_node));
537 
538  new_node->height = 1;
539  new_node->field = field;
540  new_node->children[0] = new_node->children[1] = tree->end;
541  tree->count++;
542  *node = new_node;
543  }
544  else
545  {
546  int cmp = pivotFieldCompare(&field, &current->field);
547 
548  if (cmp != 0)
549  {
551  cmp > 0 ? &current->children[1] : &current->children[0],
552  field);
553  avlAdjustBalance(tree, node);
554  }
555  }
556 }
static int pivotFieldCompare(const void *a, const void *b)
Definition: crosstabview.c:695
static void avlInsertNode(avl_tree *tree, avl_node **node, pivot_field field)
Definition: crosstabview.c:529
static void avlAdjustBalance(avl_tree *tree, avl_node **node)
Definition: crosstabview.c:506
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743

References avlAdjustBalance(), _avl_node::children, cmp(), _avl_node::field, _avl_node::height, pg_malloc(), pivotFieldCompare(), and tree.

Referenced by avlMergeValue().

◆ avlMergeValue()

static void avlMergeValue ( avl_tree tree,
char *  name,
char *  sort_value 
)
static

Definition at line 560 of file crosstabview.c.

561 {
562  pivot_field field;
563 
564  field.name = name;
565  field.rank = tree->count;
566  field.sort_value = sort_value;
567  avlInsertNode(tree, &tree->root, field);
568 }
char * sort_value
Definition: crosstabview.c:37
const char * name

References avlInsertNode(), name, _pivot_field::name, _pivot_field::rank, _pivot_field::sort_value, and tree.

Referenced by PrintResultInCrosstab().

◆ avlRotate()

static avl_node* avlRotate ( avl_node **  current,
int  dir 
)
static

Definition at line 481 of file crosstabview.c.

482 {
483  avl_node *before = *current;
484  avl_node *after = (*current)->children[dir];
485 
486  *current = after;
487  before->children[dir] = after->children[!dir];
489  after->children[!dir] = before;
490 
491  return after;
492 }
static int before(chr x, chr y)
Definition: regc_locale.c:488

References avlUpdateHeight(), before(), and _avl_node::children.

Referenced by avlAdjustBalance().

◆ avlUpdateHeight()

static void avlUpdateHeight ( avl_node n)
static

Definition at line 472 of file crosstabview.c.

473 {
474  n->height = 1 + (n->children[0]->height > n->children[1]->height ?
475  n->children[0]->height :
476  n->children[1]->height);
477 }

References _avl_node::children, and _avl_node::height.

Referenced by avlAdjustBalance(), and avlRotate().

◆ indexOfColumn()

static int indexOfColumn ( char *  arg,
const PGresult res 
)
static

Definition at line 636 of file crosstabview.c.

637 {
638  int idx;
639 
640  if (arg[0] && strspn(arg, "0123456789") == strlen(arg))
641  {
642  /* if arg contains only digits, it's a column number */
643  idx = atoi(arg) - 1;
644  if (idx < 0 || idx >= PQnfields(res))
645  {
646  pg_log_error("\\crosstabview: column number %d is out of range 1..%d",
647  idx + 1, PQnfields(res));
648  return -1;
649  }
650  }
651  else
652  {
653  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  */
661 
662  /* Now look for match(es) among res' column names */
663  idx = -1;
664  for (i = 0; i < PQnfields(res); i++)
665  {
666  if (strcmp(arg, PQfname(res, i)) == 0)
667  {
668  if (idx >= 0)
669  {
670  /* another idx was already found for the same name */
671  pg_log_error("\\crosstabview: ambiguous column name: \"%s\"", arg);
672  return -1;
673  }
674  idx = i;
675  }
676  }
677  if (idx == -1)
678  {
679  pg_log_error("\\crosstabview: column name not found: \"%s\"", arg);
680  return -1;
681  }
682  }
683 
684  return idx;
685 }
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3567
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
int i
Definition: isn.c:73
#define pg_log_error(...)
Definition: logging.h:106
void * arg
void dequote_downcase_identifier(char *str, bool downcase, int encoding)
PsqlSettings pset
Definition: startup.c:32
int encoding
Definition: settings.h:83

References arg, dequote_downcase_identifier(), _psqlSettings::encoding, i, idx(), pg_log_error, PQfname(), PQnfields(), pset, and res.

Referenced by PrintResultInCrosstab().

◆ pivotFieldCompare()

static int pivotFieldCompare ( const void *  a,
const void *  b 
)
static

Definition at line 695 of file crosstabview.c.

696 {
697  const pivot_field *pa = (const pivot_field *) a;
698  const pivot_field *pb = (const pivot_field *) b;
699 
700  /* test null values */
701  if (!pb->name)
702  return pa->name ? -1 : 0;
703  else if (!pa->name)
704  return 1;
705 
706  /* non-null values */
707  return strcmp(pa->name, pb->name);
708 }
int a
Definition: isn.c:69

References a, b, and _pivot_field::name.

Referenced by avlInsertNode(), and printCrosstab().

◆ printCrosstab()

static bool printCrosstab ( const PGresult result,
int  num_columns,
pivot_field piv_columns,
int  field_for_columns,
int  num_rows,
pivot_field piv_rows,
int  field_for_rows,
int  field_for_data 
)
static

Definition at line 286 of file crosstabview.c.

290 {
291  printQueryOpt popt = pset.popt;
292  printTableContent cont;
293  int i,
294  rn;
295  char col_align;
296  int *horiz_map;
297  bool retval = false;
298 
299  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  printTableAddHeader(&cont,
305  PQfname(result, field_for_rows),
306  false,
308  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  horiz_map = (int *) pg_malloc(sizeof(int) * num_columns);
316  for (i = 0; i < num_columns; i++)
317  horiz_map[piv_columns[i].rank] = i;
318 
319  /*
320  * The display alignment depends on its PQftype().
321  */
322  col_align = column_type_alignment(PQftype(result, field_for_data));
323 
324  for (i = 0; i < num_columns; i++)
325  {
326  char *colname;
327 
328  colname = piv_columns[horiz_map[i]].name ?
329  piv_columns[horiz_map[i]].name :
330  (popt.nullPrint ? popt.nullPrint : "");
331 
332  printTableAddHeader(&cont, colname, false, col_align);
333  }
334  pg_free(horiz_map);
335 
336  /* Step 2: set row names in the first output column (vertical header) */
337  for (i = 0; i < num_rows; i++)
338  {
339  int k = piv_rows[i].rank;
340 
341  cont.cells[k * (num_columns + 1)] = piv_rows[i].name ?
342  piv_rows[i].name :
343  (popt.nullPrint ? popt.nullPrint : "");
344  }
345  cont.cellsadded = num_rows * (num_columns + 1);
346 
347  /*
348  * Step 3: fill in the content cells.
349  */
350  for (rn = 0; rn < PQntuples(result); rn++)
351  {
352  int row_number;
353  int col_number;
354  pivot_field *rp,
355  *cp;
356  pivot_field elt;
357 
358  /* Find target row */
359  if (!PQgetisnull(result, rn, field_for_rows))
360  elt.name = PQgetvalue(result, rn, field_for_rows);
361  else
362  elt.name = NULL;
363  rp = (pivot_field *) bsearch(&elt,
364  piv_rows,
365  num_rows,
366  sizeof(pivot_field),
368  Assert(rp != NULL);
369  row_number = rp->rank;
370 
371  /* Find target column */
372  if (!PQgetisnull(result, rn, field_for_columns))
373  elt.name = PQgetvalue(result, rn, field_for_columns);
374  else
375  elt.name = NULL;
376 
377  cp = (pivot_field *) bsearch(&elt,
378  piv_columns,
379  num_columns,
380  sizeof(pivot_field),
382  Assert(cp != NULL);
383  col_number = cp->rank;
384 
385  /* Place value into cell */
386  if (col_number >= 0 && row_number >= 0)
387  {
388  int idx;
389 
390  /* index into the cont.cells array */
391  idx = 1 + col_number + row_number * (num_columns + 1);
392 
393  /*
394  * If the cell already contains a value, raise an error.
395  */
396  if (cont.cells[idx] != NULL)
397  {
398  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  goto error;
404  }
405 
406  cont.cells[idx] = !PQgetisnull(result, rn, field_for_data) ?
407  PQgetvalue(result, rn, field_for_data) :
408  (popt.nullPrint ? popt.nullPrint : "");
409  }
410  }
411 
412  /*
413  * The non-initialized cells must be set to an empty string for the print
414  * functions
415  */
416  for (i = 0; i < cont.cellsadded; i++)
417  {
418  if (cont.cells[i] == NULL)
419  cont.cells[i] = "";
420  }
421 
422  printTable(&cont, pset.queryFout, false, pset.logfile);
423  retval = true;
424 
425 error:
426  printTableCleanup(&cont);
427 
428  return retval;
429 }
#define Assert(condition)
Definition: c.h:858
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3719
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3172
void printTableCleanup(printTableContent *const content)
Definition: print.c:3353
char column_type_alignment(Oid ftype)
Definition: print.c:3614
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3443
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3220
static void error(void)
Definition: sql-dyntest.c:147
printQueryOpt popt
Definition: settings.h:91
FILE * logfile
Definition: settings.h:120
FILE * queryFout
Definition: settings.h:84
printTableOpt topt
Definition: print.h:185
char * nullPrint
Definition: print.h:186
char * title
Definition: print.h:187
uint64 cellsadded
Definition: print.h:174
const char ** cells
Definition: print.h:171

References Assert, printTableContent::cells, printTableContent::cellsadded, column_type_alignment(), error(), i, idx(), _psqlSettings::logfile, name, _pivot_field::name, printQueryOpt::nullPrint, pg_free(), pg_log_error, pg_malloc(), pivotFieldCompare(), _psqlSettings::popt, PQfname(), PQftype(), PQgetisnull(), PQgetvalue(), PQntuples(), printTable(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, _psqlSettings::queryFout, _pivot_field::rank, printQueryOpt::title, and printQueryOpt::topt.

Referenced by PrintResultInCrosstab().

◆ PrintResultInCrosstab()

bool PrintResultInCrosstab ( const PGresult res)

Definition at line 104 of file crosstabview.c.

105 {
106  bool retval = false;
107  avl_tree piv_columns;
108  avl_tree piv_rows;
109  pivot_field *array_columns = NULL;
110  pivot_field *array_rows = NULL;
111  int num_columns = 0;
112  int num_rows = 0;
113  int field_for_rows;
114  int field_for_columns;
115  int field_for_data;
116  int sort_field_for_columns;
117  int rn;
118 
119  avlInit(&piv_rows);
120  avlInit(&piv_columns);
121 
123  {
124  pg_log_error("\\crosstabview: statement did not return a result set");
125  goto error_return;
126  }
127 
128  if (PQnfields(res) < 3)
129  {
130  pg_log_error("\\crosstabview: query must return at least three columns");
131  goto error_return;
132  }
133 
134  /* Process first optional arg (vertical header column) */
135  if (pset.ctv_args[0] == NULL)
136  field_for_rows = 0;
137  else
138  {
139  field_for_rows = indexOfColumn(pset.ctv_args[0], res);
140  if (field_for_rows < 0)
141  goto error_return;
142  }
143 
144  /* Process second optional arg (horizontal header column) */
145  if (pset.ctv_args[1] == NULL)
146  field_for_columns = 1;
147  else
148  {
149  field_for_columns = indexOfColumn(pset.ctv_args[1], res);
150  if (field_for_columns < 0)
151  goto error_return;
152  }
153 
154  /* Insist that header columns be distinct */
155  if (field_for_columns == field_for_rows)
156  {
157  pg_log_error("\\crosstabview: vertical and horizontal headers must be different columns");
158  goto error_return;
159  }
160 
161  /* Process third optional arg (data column) */
162  if (pset.ctv_args[2] == NULL)
163  {
164  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  if (PQnfields(res) != 3)
172  {
173  pg_log_error("\\crosstabview: data column must be specified when query returns more than three columns");
174  goto error_return;
175  }
176 
177  field_for_data = -1;
178  for (i = 0; i < PQnfields(res); i++)
179  {
180  if (i != field_for_rows && i != field_for_columns)
181  {
182  field_for_data = i;
183  break;
184  }
185  }
186  Assert(field_for_data >= 0);
187  }
188  else
189  {
190  field_for_data = indexOfColumn(pset.ctv_args[2], res);
191  if (field_for_data < 0)
192  goto error_return;
193  }
194 
195  /* Process fourth optional arg (horizontal header sort column) */
196  if (pset.ctv_args[3] == NULL)
197  sort_field_for_columns = -1; /* no sort column */
198  else
199  {
200  sort_field_for_columns = indexOfColumn(pset.ctv_args[3], res);
201  if (sort_field_for_columns < 0)
202  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  for (rn = 0; rn < PQntuples(res); rn++)
212  {
213  char *val;
214  char *val1;
215 
216  /* horizontal */
217  val = PQgetisnull(res, rn, field_for_columns) ? NULL :
218  PQgetvalue(res, rn, field_for_columns);
219  val1 = NULL;
220 
221  if (sort_field_for_columns >= 0 &&
222  !PQgetisnull(res, rn, sort_field_for_columns))
223  val1 = PQgetvalue(res, rn, sort_field_for_columns);
224 
225  avlMergeValue(&piv_columns, val, val1);
226 
227  if (piv_columns.count > CROSSTABVIEW_MAX_COLUMNS)
228  {
229  pg_log_error("\\crosstabview: maximum number of columns (%d) exceeded",
231  goto error_return;
232  }
233 
234  /* vertical */
235  val = PQgetisnull(res, rn, field_for_rows) ? NULL :
236  PQgetvalue(res, rn, field_for_rows);
237 
238  avlMergeValue(&piv_rows, val, NULL);
239  }
240 
241  /*
242  * Second part: Generate sorted arrays from the AVL trees.
243  */
244 
245  num_columns = piv_columns.count;
246  num_rows = piv_rows.count;
247 
248  array_columns = (pivot_field *)
249  pg_malloc(sizeof(pivot_field) * num_columns);
250 
251  array_rows = (pivot_field *)
252  pg_malloc(sizeof(pivot_field) * num_rows);
253 
254  avlCollectFields(&piv_columns, piv_columns.root, array_columns, 0);
255  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  if (sort_field_for_columns >= 0)
262  rankSort(num_columns, array_columns);
263 
264  /*
265  * Fourth part: print the crosstab'ed result.
266  */
267  retval = printCrosstab(res,
268  num_columns, array_columns, field_for_columns,
269  num_rows, array_rows, field_for_rows,
270  field_for_data);
271 
272 error_return:
273  avlFree(&piv_columns, piv_columns.root);
274  avlFree(&piv_rows, piv_rows.root);
275  pg_free(array_columns);
276  pg_free(array_rows);
277 
278  return retval;
279 }
static bool printCrosstab(const PGresult *result, int num_columns, pivot_field *piv_columns, int field_for_columns, int num_rows, pivot_field *piv_rows, int field_for_rows, int field_for_data)
Definition: crosstabview.c:286
static void rankSort(int num_columns, pivot_field *piv_columns)
Definition: crosstabview.c:588
static int indexOfColumn(char *arg, const PGresult *res)
Definition: crosstabview.c:636
static void avlInit(avl_tree *tree)
Definition: crosstabview.c:438
static void avlMergeValue(avl_tree *tree, char *name, char *sort_value)
Definition: crosstabview.c:560
#define CROSSTABVIEW_MAX_COLUMNS
Definition: crosstabview.h:24
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
long val
Definition: informix.c:670
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:103
avl_node * root
Definition: crosstabview.c:76
char * ctv_args[4]
Definition: settings.h:104

References Assert, avlCollectFields(), avlFree(), avlInit(), avlMergeValue(), _avl_tree::count, CROSSTABVIEW_MAX_COLUMNS, _psqlSettings::ctv_args, i, indexOfColumn(), pg_free(), pg_log_error, pg_malloc(), PGRES_TUPLES_OK, PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), printCrosstab(), pset, rankSort(), res, _avl_tree::root, and val.

Referenced by PrintQueryResult().

◆ rankCompare()

static int rankCompare ( const void *  a,
const void *  b 
)
static

Definition at line 711 of file crosstabview.c.

712 {
713  return pg_cmp_s32(*(const int *) a, *(const int *) b);
714 }
static int pg_cmp_s32(int32 a, int32 b)
Definition: int.h:483

References a, b, and pg_cmp_s32().

Referenced by rankSort().

◆ rankSort()

static void rankSort ( int  num_columns,
pivot_field piv_columns 
)
static

Definition at line 588 of file crosstabview.c.

589 {
590  int *hmap; /* [[offset in piv_columns, rank], ...for
591  * every header entry] */
592  int i;
593 
594  hmap = (int *) pg_malloc(sizeof(int) * num_columns * 2);
595  for (i = 0; i < num_columns; i++)
596  {
597  char *val = piv_columns[i].sort_value;
598 
599  /* ranking information is valid if non null and matches /^-?\d+$/ */
600  if (val &&
601  ((*val == '-' &&
602  strspn(val + 1, "0123456789") == strlen(val + 1)) ||
603  strspn(val, "0123456789") == strlen(val)))
604  {
605  hmap[i * 2] = atoi(val);
606  hmap[i * 2 + 1] = i;
607  }
608  else
609  {
610  /* invalid rank information ignored (equivalent to rank 0) */
611  hmap[i * 2] = 0;
612  hmap[i * 2 + 1] = i;
613  }
614  }
615 
616  qsort(hmap, num_columns, sizeof(int) * 2, rankCompare);
617 
618  for (i = 0; i < num_columns; i++)
619  {
620  piv_columns[hmap[i * 2 + 1]].rank = i;
621  }
622 
623  pg_free(hmap);
624 }
static int rankCompare(const void *a, const void *b)
Definition: crosstabview.c:711
#define qsort(a, b, c, d)
Definition: port.h:449

References i, pg_free(), pg_malloc(), qsort, _pivot_field::rank, rankCompare(), _pivot_field::sort_value, and val.

Referenced by PrintResultInCrosstab().