PostgreSQL Source Code git master
Loading...
Searching...
No Matches
crosstabview.c
Go to the documentation of this file.
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 */
23typedef 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 */
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;
49
50/* Node in avl_tree */
51typedef struct _avl_node
52{
53 /* Node contents */
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];
69
70/*
71 * Control structure for the AVL tree (binary search tree kept
72 * balanced with the AVL algorithm)
73 */
74typedef 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 */
80
81
82static bool printCrosstab(const PGresult *result,
85 int field_for_data);
86static char *displayValue(char *value, Oid ftype, char *default_null);
87
88static void avlInit(avl_tree *tree);
89static void avlMergeValue(avl_tree *tree, char *name, char *sort_value);
90static int avlCollectFields(avl_tree *tree, avl_node *node,
91 pivot_field *fields, int idx);
92static void avlFree(avl_tree *tree, avl_node *node);
94static int indexOfColumn(char *arg, const PGresult *res);
95static int pivotFieldCompare(const void *a, const void *b);
96static 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 */
106bool
108{
109 bool retval = false;
114 int num_columns = 0;
115 int num_rows = 0;
116 int field_for_rows;
118 int field_for_data;
120 int rn;
121
124
126 {
127 pg_log_error("\\crosstabview: statement did not return a result set");
128 goto error_return;
129 }
130
131 if (PQnfields(res) < 3)
132 {
133 pg_log_error("\\crosstabview: query must return at least three columns");
134 goto error_return;
135 }
136
137 /* Process first optional arg (vertical header column) */
138 if (pset.ctv_args[0] == NULL)
139 field_for_rows = 0;
140 else
141 {
143 if (field_for_rows < 0)
144 goto error_return;
145 }
146
147 /* Process second optional arg (horizontal header column) */
148 if (pset.ctv_args[1] == NULL)
150 else
151 {
153 if (field_for_columns < 0)
154 goto error_return;
155 }
156
157 /* Insist that header columns be distinct */
159 {
160 pg_log_error("\\crosstabview: vertical and horizontal headers must be different columns");
161 goto error_return;
162 }
163
164 /* Process third optional arg (data column) */
165 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 if (PQnfields(res) != 3)
175 {
176 pg_log_error("\\crosstabview: data column must be specified when query returns more than three columns");
177 goto error_return;
178 }
179
180 field_for_data = -1;
181 for (i = 0; i < PQnfields(res); i++)
182 {
183 if (i != field_for_rows && i != field_for_columns)
184 {
186 break;
187 }
188 }
190 }
191 else
192 {
194 if (field_for_data < 0)
195 goto error_return;
196 }
197
198 /* Process fourth optional arg (horizontal header sort column) */
199 if (pset.ctv_args[3] == NULL)
200 sort_field_for_columns = -1; /* no sort column */
201 else
202 {
205 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 for (rn = 0; rn < PQntuples(res); rn++)
215 {
216 char *val;
217 char *val1;
218
219 /* horizontal */
222 val1 = NULL;
223
224 if (sort_field_for_columns >= 0 &&
227
229
231 {
232 pg_log_error("\\crosstabview: maximum number of columns (%d) exceeded",
234 goto error_return;
235 }
236
237 /* vertical */
240
242 }
243
244 /*
245 * Second part: Generate sorted arrays from the AVL trees.
246 */
247
248 num_columns = piv_columns.count;
249 num_rows = piv_rows.count;
250
252
254
257
258 /*
259 * Third part: optionally, process the ranking data for the horizontal
260 * header
261 */
262 if (sort_field_for_columns >= 0)
264
265 /*
266 * Fourth part: print the crosstab'ed result.
267 */
268 retval = printCrosstab(res,
272
275 avlFree(&piv_rows, piv_rows.root);
278
279 return retval;
280}
281
282/*
283 * Output the pivoted resultset with the printTable* functions. Return true
284 * if successful, false otherwise.
285 */
286static bool
290 int field_for_data)
291{
292 printQueryOpt popt = pset.popt;
294 int i,
295 rn;
296 char col_align;
297 int *horiz_map;
301 bool retval = false;
302
303 printTableInit(&cont, &popt.topt, popt.title, num_columns + 1, num_rows);
304
305 /* Step 1: set target column names (horizontal header) */
306
307 /* The name of the first column is kept unchanged by the pivoting */
310 false,
312
313 /*
314 * To iterate over piv_columns[] by piv_columns[].rank, create a reverse
315 * map associating each piv_columns[].rank to its index in piv_columns.
316 * This avoids an O(N^2) loop later.
317 */
319 for (i = 0; i < num_columns; i++)
320 horiz_map[piv_columns[i].rank] = i;
321
322 /*
323 * The display alignment depends on its PQftype().
324 */
326
327 for (i = 0; i < num_columns; i++)
328 {
329 char *colname;
330
332
333 printTableAddHeader(&cont, colname, false, col_align);
334 }
336
337 /* Step 2: set row names in the first output column (vertical header) */
338 for (i = 0; i < num_rows; i++)
339 {
340 int k = piv_rows[i].rank;
341 int idx = k * (num_columns + 1);
342
343 cont.cells[idx] = displayValue(piv_rows[i].name, row_ftype, "");
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;
355 *cp;
357
358 /* Find target row */
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 */
374 else
375 elt.name = NULL;
376
377 cp = (pivot_field *) bsearch(&elt,
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 char *value;
390
391 /* index into the cont.cells array */
392 idx = 1 + col_number + row_number * (num_columns + 1);
393
394 /*
395 * If the cell already contains a value, raise an error.
396 */
397 if (cont.cells[idx] != NULL)
398 {
399 pg_log_error("\\crosstabview: query result contains multiple data values for row \"%s\", column \"%s\"",
400 displayValue(rp->name, row_ftype, "(null)"),
401 displayValue(cp->name, col_ftype, "(null)"));
402 goto error;
403 }
404
406 value = NULL;
407 else
409 cont.cells[idx] = displayValue(value, data_ftype, "");
410 }
411 }
412
413 /*
414 * The non-initialized cells must be set to an empty string for the print
415 * functions
416 */
417 for (i = 0; i < cont.cellsadded; i++)
418 {
419 if (cont.cells[i] == NULL)
420 cont.cells[i] = "";
421 }
422
424 retval = true;
425
426error:
428
429 return retval;
430}
431
432/*
433 * Return the display representation of one cell value in \crosstabview,
434 * following pset substitutions.
435 *
436 * The returned pointer is not to be freed.
437 */
438static char *
439displayValue(char *value, Oid ftype, char *default_null)
440{
441 printQueryOpt popt = pset.popt;
442
443 if (value == NULL)
444 value = popt.nullPrint ? popt.nullPrint : default_null;
445 else if (ftype == BOOLOID)
446 {
447 if (value[0] == 't' && popt.truePrint)
448 value = popt.truePrint;
449 else if (value[0] == 'f' && popt.falsePrint)
450 value = popt.falsePrint;
451 }
452
453 return value;
454}
455
456/*
457 * The avl* functions below provide a minimalistic implementation of AVL binary
458 * trees, to efficiently collect the distinct values that will form the horizontal
459 * and vertical headers. It only supports adding new values, no removal or even
460 * search.
461 */
462static void
464{
466 tree->end->children[0] = tree->end->children[1] = tree->end;
467 tree->count = 0;
468 tree->root = tree->end;
469}
470
471/* Deallocate recursively an AVL tree, starting from node */
472static void
474{
475 if (node->children[0] != tree->end)
476 {
477 avlFree(tree, node->children[0]);
478 pg_free(node->children[0]);
479 }
480 if (node->children[1] != tree->end)
481 {
482 avlFree(tree, node->children[1]);
483 pg_free(node->children[1]);
484 }
485 if (node == tree->root)
486 {
487 /* free the root separately as it's not child of anything */
488 if (node != tree->end)
489 pg_free(node);
490 /* free the tree->end struct only once and when all else is freed */
491 pg_free(tree->end);
492 }
493}
494
495/* Set the height to 1 plus the greatest of left and right heights */
496static void
498{
499 n->height = 1 + (n->children[0]->height > n->children[1]->height ?
500 n->children[0]->height :
501 n->children[1]->height);
502}
503
504/* Rotate a subtree left (dir=0) or right (dir=1). Not recursive */
505static avl_node *
506avlRotate(avl_node **current, int dir)
507{
508 avl_node *before = *current;
509 avl_node *after = (*current)->children[dir];
510
511 *current = after;
512 before->children[dir] = after->children[!dir];
514 after->children[!dir] = before;
515
516 return after;
517}
518
519static int
521{
522 return n->children[0]->height - n->children[1]->height;
523}
524
525/*
526 * After an insertion, possibly rebalance the tree so that the left and right
527 * node heights don't differ by more than 1.
528 * May update *node.
529 */
530static void
532{
533 avl_node *current = *node;
534 int b = avlBalance(current) / 2;
535
536 if (b != 0)
537 {
538 int dir = (1 - b) / 2;
539
540 if (avlBalance(current->children[dir]) == -b)
541 avlRotate(&current->children[dir], !dir);
542 current = avlRotate(node, dir);
543 }
544 if (current != tree->end)
545 avlUpdateHeight(current);
546}
547
548/*
549 * Insert a new value/field, starting from *node, reaching the correct position
550 * in the tree by recursion. Possibly rebalance the tree and possibly update
551 * *node. Do nothing if the value is already present in the tree.
552 */
553static void
555{
556 avl_node *current = *node;
557
558 if (current == tree->end)
559 {
561
562 new_node->height = 1;
563 new_node->field = field;
564 new_node->children[0] = new_node->children[1] = tree->end;
565 tree->count++;
566 *node = new_node;
567 }
568 else
569 {
570 int cmp = pivotFieldCompare(&field, &current->field);
571
572 if (cmp != 0)
573 {
575 cmp > 0 ? &current->children[1] : &current->children[0],
576 field);
577 avlAdjustBalance(tree, node);
578 }
579 }
580}
581
582/* Insert the value into the AVL tree, if it does not preexist */
583static void
584avlMergeValue(avl_tree *tree, char *name, char *sort_value)
585{
586 pivot_field field;
587
588 field.name = name;
589 field.rank = tree->count;
590 field.sort_value = sort_value;
591 avlInsertNode(tree, &tree->root, field);
592}
593
594/*
595 * Recursively extract node values into the names array, in sorted order with a
596 * left-to-right tree traversal.
597 * Return the next candidate offset to write into the names array.
598 * fields[] must be preallocated to hold tree->count entries
599 */
600static int
602{
603 if (node == tree->end)
604 return idx;
605
606 idx = avlCollectFields(tree, node->children[0], fields, idx);
607 fields[idx] = node->field;
608 return avlCollectFields(tree, node->children[1], fields, idx + 1);
609}
610
611static void
613{
614 int *hmap; /* [[offset in piv_columns, rank], ...for
615 * every header entry] */
616 int i;
617
618 hmap = pg_malloc_array(int, num_columns * 2);
619 for (i = 0; i < num_columns; i++)
620 {
621 char *val = piv_columns[i].sort_value;
622
623 /* ranking information is valid if non null and matches /^-?\d+$/ */
624 if (val &&
625 ((*val == '-' &&
626 strspn(val + 1, "0123456789") == strlen(val + 1)) ||
627 strspn(val, "0123456789") == strlen(val)))
628 {
629 hmap[i * 2] = atoi(val);
630 hmap[i * 2 + 1] = i;
631 }
632 else
633 {
634 /* invalid rank information ignored (equivalent to rank 0) */
635 hmap[i * 2] = 0;
636 hmap[i * 2 + 1] = i;
637 }
638 }
639
640 qsort(hmap, num_columns, sizeof(int) * 2, rankCompare);
641
642 for (i = 0; i < num_columns; i++)
643 {
644 piv_columns[hmap[i * 2 + 1]].rank = i;
645 }
646
647 pg_free(hmap);
648}
649
650/*
651 * Look up a column reference, which can be either:
652 * - a number from 1 to PQnfields(res)
653 * - a column name matching one of PQfname(res,...)
654 *
655 * Returns zero-based column number, or -1 if not found or ambiguous.
656 *
657 * Note: may modify contents of "arg" string.
658 */
659static int
660indexOfColumn(char *arg, const PGresult *res)
661{
662 int idx;
663
664 if (arg[0] && strspn(arg, "0123456789") == strlen(arg))
665 {
666 /* if arg contains only digits, it's a column number */
667 idx = atoi(arg) - 1;
668 if (idx < 0 || idx >= PQnfields(res))
669 {
670 pg_log_error("\\crosstabview: column number %d is out of range 1..%d",
671 idx + 1, PQnfields(res));
672 return -1;
673 }
674 }
675 else
676 {
677 int i;
678
679 /*
680 * Dequote and downcase the column name. By checking for all-digits
681 * before doing this, we can ensure that a quoted name is treated as a
682 * name even if it's all digits.
683 */
685
686 /* Now look for match(es) among res' column names */
687 idx = -1;
688 for (i = 0; i < PQnfields(res); i++)
689 {
690 if (strcmp(arg, PQfname(res, i)) == 0)
691 {
692 if (idx >= 0)
693 {
694 /* another idx was already found for the same name */
695 pg_log_error("\\crosstabview: ambiguous column name: \"%s\"", arg);
696 return -1;
697 }
698 idx = i;
699 }
700 }
701 if (idx == -1)
702 {
703 pg_log_error("\\crosstabview: column name not found: \"%s\"", arg);
704 return -1;
705 }
706 }
707
708 return idx;
709}
710
711/*
712 * Value comparator for vertical and horizontal headers
713 * used for deduplication only.
714 * - null values are considered equal
715 * - non-null < null
716 * - non-null values are compared with strcmp()
717 */
718static int
719pivotFieldCompare(const void *a, const void *b)
720{
721 const pivot_field *pa = (const pivot_field *) a;
722 const pivot_field *pb = (const pivot_field *) b;
723
724 /* test null values */
725 if (!pb->name)
726 return pa->name ? -1 : 0;
727 else if (!pa->name)
728 return 1;
729
730 /* non-null values */
731 return strcmp(pa->name, pb->name);
732}
733
734static int
735rankCompare(const void *a, const void *b)
736{
737 return pg_cmp_s32(*(const int *) a, *(const int *) b);
738}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
#define Assert(condition)
Definition c.h:1002
uint32 result
static void avlUpdateHeight(avl_node *n)
static int avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
static int pivotFieldCompare(const void *a, const void *b)
static void avlInsertNode(avl_tree *tree, avl_node **node, pivot_field field)
static int rankCompare(const void *a, const void *b)
struct _avl_tree avl_tree
static void avlFree(avl_tree *tree, avl_node *node)
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 rankSort(int num_columns, pivot_field *piv_columns)
bool PrintResultInCrosstab(const PGresult *res)
static char * displayValue(char *value, Oid ftype, char *default_null)
static int avlBalance(avl_node *n)
static int indexOfColumn(char *arg, const PGresult *res)
static avl_node * avlRotate(avl_node **current, int dir)
static void avlInit(avl_tree *tree)
static void avlMergeValue(avl_tree *tree, char *name, char *sort_value)
struct _avl_node avl_node
struct _pivot_field pivot_field
static void avlAdjustBalance(avl_tree *tree, avl_node **node)
#define CROSSTABVIEW_MAX_COLUMNS
Datum arg
Definition elog.c:1323
Oid PQftype(const PGresult *res, int field_num)
Definition fe-exec.c:3750
void pg_free(void *ptr)
#define pg_malloc_array(type, count)
Definition fe_memutils.h:66
#define pg_malloc0_object(type)
Definition fe_memutils.h:61
#define pg_malloc_object(type)
Definition fe_memutils.h:60
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition print.c:3209
void printTableCleanup(printTableContent *const content)
Definition print.c:3390
char column_type_alignment(Oid ftype)
Definition print.c:3829
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition print.c:3654
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition print.c:3257
long val
Definition informix.c:689
static struct @175 value
static int pg_cmp_s32(int32 a, int32 b)
Definition int.h:713
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
#define PQgetvalue
#define PQnfields
#define PQresultStatus
#define PQgetisnull
#define PQfname
#define PQntuples
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
#define pg_log_error(...)
Definition logging.h:108
#define qsort(a, b, c, d)
Definition port.h:496
unsigned int Oid
static int fb(int x)
void dequote_downcase_identifier(char *str, bool downcase, int encoding)
tree
Definition radixtree.h:1828
static int cmp(const chr *x, const chr *y, size_t len)
static int before(chr x, chr y)
PsqlSettings pset
Definition startup.c:33
static void error(void)
struct _avl_node * children[2]
pivot_field field
avl_node * root
avl_node * end
char * sort_value
printQueryOpt popt
Definition settings.h:112
FILE * logfile
Definition settings.h:149
char * ctv_args[4]
Definition settings.h:133
FILE * queryFout
Definition settings.h:105
printTableOpt topt
Definition print.h:185
char * nullPrint
Definition print.h:186
char * falsePrint
Definition print.h:188
char * title
Definition print.h:189
char * truePrint
Definition print.h:187
const char * name