PostgreSQL Source Code git master
Loading...
Searching...
No Matches
crosstabview.c File Reference
#include "postgres_fe.h"
#include "catalog/pg_type_d.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 chardisplayValue (char *value, Oid ftype, char *default_null)
 
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

◆ avl_tree

◆ pivot_field

Function Documentation

◆ avlAdjustBalance()

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

Definition at line 531 of file crosstabview.c.

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}
static void avlUpdateHeight(avl_node *n)
static int avlBalance(avl_node *n)
static avl_node * avlRotate(avl_node **current, int dir)
int b
Definition isn.c:74
tree
Definition radixtree.h:1828
struct _avl_node * children[2]

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

Referenced by avlInsertNode().

◆ avlBalance()

static int avlBalance ( avl_node n)
static

Definition at line 520 of file crosstabview.c.

521{
522 return n->children[0]->height - n->children[1]->height;
523}

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 601 of file crosstabview.c.

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}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
static int avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
pivot_field field

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

Referenced by avlCollectFields(), and PrintResultInCrosstab().

◆ avlFree()

static void avlFree ( avl_tree tree,
avl_node node 
)
static

Definition at line 473 of file crosstabview.c.

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}
static void avlFree(avl_tree *tree, avl_node *node)
void pg_free(void *ptr)

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

Referenced by avlFree(), and PrintResultInCrosstab().

◆ avlInit()

static void avlInit ( avl_tree tree)
static

Definition at line 463 of file crosstabview.c.

464{
466 tree->end->children[0] = tree->end->children[1] = tree->end;
467 tree->count = 0;
468 tree->root = tree->end;
469}
#define pg_malloc0_object(type)
Definition fe_memutils.h:61

References pg_malloc0_object, and tree.

Referenced by PrintResultInCrosstab().

◆ avlInsertNode()

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

Definition at line 554 of file crosstabview.c.

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}
static int pivotFieldCompare(const void *a, const void *b)
static void avlInsertNode(avl_tree *tree, avl_node **node, pivot_field field)
static void avlAdjustBalance(avl_tree *tree, avl_node **node)
#define pg_malloc_object(type)
Definition fe_memutils.h:60
static int fb(int x)
static int cmp(const chr *x, const chr *y, size_t len)

References avlAdjustBalance(), avlInsertNode(), _avl_node::children, cmp(), fb(), _avl_node::field, pg_malloc_object, pivotFieldCompare(), and tree.

Referenced by avlInsertNode(), and avlMergeValue().

◆ avlMergeValue()

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

Definition at line 584 of file crosstabview.c.

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}
char * sort_value
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 506 of file crosstabview.c.

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}
static int before(chr x, chr y)

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

Referenced by avlAdjustBalance().

◆ avlUpdateHeight()

static void avlUpdateHeight ( avl_node n)
static

Definition at line 497 of file crosstabview.c.

498{
499 n->height = 1 + (n->children[0]->height > n->children[1]->height ?
500 n->children[0]->height :
501 n->children[1]->height);
502}

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

Referenced by avlAdjustBalance(), and avlRotate().

◆ displayValue()

static char * displayValue ( char value,
Oid  ftype,
char default_null 
)
static

Definition at line 439 of file crosstabview.c.

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}
static struct @175 value
PsqlSettings pset
Definition startup.c:33
printQueryOpt popt
Definition settings.h:112
char * nullPrint
Definition print.h:186
char * falsePrint
Definition print.h:188
char * truePrint
Definition print.h:187

References printQueryOpt::falsePrint, fb(), printQueryOpt::nullPrint, _psqlSettings::popt, pset, printQueryOpt::truePrint, and value.

Referenced by printCrosstab().

◆ indexOfColumn()

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

Definition at line 660 of file crosstabview.c.

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}
Datum arg
Definition elog.c:1323
int i
Definition isn.c:77
#define PQnfields
#define PQfname
#define pg_log_error(...)
Definition logging.h:108
void dequote_downcase_identifier(char *str, bool downcase, int encoding)

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

Referenced by PrintResultInCrosstab().

◆ pivotFieldCompare()

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

Definition at line 719 of file crosstabview.c.

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}
int a
Definition isn.c:73

References a, b, fb(), 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 287 of file crosstabview.c.

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}
#define Assert(condition)
Definition c.h:999
uint32 result
static char * displayValue(char *value, Oid ftype, char *default_null)
Oid PQftype(const PGresult *res, int field_num)
Definition fe-exec.c:3750
#define pg_malloc_array(type, count)
Definition fe_memutils.h:66
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition print.c:3192
void printTableCleanup(printTableContent *const content)
Definition print.c:3373
char column_type_alignment(Oid ftype)
Definition print.c:3812
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition print.c:3637
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition print.c:3240
#define PQgetvalue
#define PQgetisnull
#define PQntuples
unsigned int Oid
static void error(void)
FILE * logfile
Definition settings.h:149
FILE * queryFout
Definition settings.h:105
printTableOpt topt
Definition print.h:185
char * title
Definition print.h:189

References Assert, column_type_alignment(), displayValue(), error(), fb(), i, idx(), _psqlSettings::logfile, name, _pivot_field::name, pg_free(), pg_log_error, pg_malloc_array, pivotFieldCompare(), _psqlSettings::popt, PQfname, PQftype(), PQgetisnull, PQgetvalue, PQntuples, printTable(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, _psqlSettings::queryFout, result, printQueryOpt::title, printQueryOpt::topt, and value.

Referenced by PrintResultInCrosstab().

◆ PrintResultInCrosstab()

bool PrintResultInCrosstab ( const PGresult res)

Definition at line 107 of file crosstabview.c.

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}
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)
static int indexOfColumn(char *arg, const PGresult *res)
static void avlInit(avl_tree *tree)
static void avlMergeValue(avl_tree *tree, char *name, char *sort_value)
#define CROSSTABVIEW_MAX_COLUMNS
long val
Definition informix.c:689
#define PQresultStatus
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
char * ctv_args[4]
Definition settings.h:133

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

Referenced by PrintQueryResult().

◆ rankCompare()

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

Definition at line 735 of file crosstabview.c.

736{
737 return pg_cmp_s32(*(const int *) a, *(const int *) b);
738}
static int pg_cmp_s32(int32 a, int32 b)
Definition int.h:713

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 612 of file crosstabview.c.

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}
static int rankCompare(const void *a, const void *b)
#define qsort(a, b, c, d)
Definition port.h:496

References fb(), i, pg_free(), pg_malloc_array, qsort, rankCompare(), and val.

Referenced by PrintResultInCrosstab().