PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_dependencies.c File Reference
#include "postgres.h"
#include "common/int.h"
#include "common/jsonapi.h"
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "nodes/miscnodes.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics_format.h"
#include "utils/builtins.h"
#include "utils/float.h"
#include "utils/fmgrprotos.h"
Include dependency graph for pg_dependencies.c:

Go to the source code of this file.

Data Structures

struct  DependenciesParseState
 

Enumerations

enum  DependenciesSemanticState {
  DEPS_EXPECT_START = 0 , DEPS_EXPECT_ITEM , DEPS_EXPECT_KEY , DEPS_EXPECT_ATTNUM_LIST ,
  DEPS_EXPECT_ATTNUM , DEPS_EXPECT_DEPENDENCY , DEPS_EXPECT_DEGREE , DEPS_PARSE_COMPLETE
}
 

Functions

static JsonParseErrorType dependencies_object_start (void *state)
 
static JsonParseErrorType dependencies_object_end (void *state)
 
static JsonParseErrorType dependencies_array_start (void *state)
 
static JsonParseErrorType dependencies_array_end (void *state)
 
static JsonParseErrorType dependencies_object_field_start (void *state, char *fname, bool isnull)
 
static JsonParseErrorType dependencies_array_element_start (void *state, bool isnull)
 
static bool valid_subsequent_attnum (const AttrNumber prev, const AttrNumber cur)
 
static JsonParseErrorType dependencies_scalar (void *state, char *token, JsonTokenType tokentype)
 
static bool dep_attributes_eq (const MVDependency *a, const MVDependency *b)
 
static chardep_attnum_list (const MVDependency *item)
 
static AttrNumber dep_attnum_dependency (const MVDependency *item)
 
static byteabuild_mvdependencies (DependenciesParseState *parse, char *str)
 
Datum pg_dependencies_in (PG_FUNCTION_ARGS)
 
Datum pg_dependencies_out (PG_FUNCTION_ARGS)
 
Datum pg_dependencies_recv (PG_FUNCTION_ARGS)
 
Datum pg_dependencies_send (PG_FUNCTION_ARGS)
 

Enumeration Type Documentation

◆ DependenciesSemanticState

Enumerator
DEPS_EXPECT_START 
DEPS_EXPECT_ITEM 
DEPS_EXPECT_KEY 
DEPS_EXPECT_ATTNUM_LIST 
DEPS_EXPECT_ATTNUM 
DEPS_EXPECT_DEPENDENCY 
DEPS_EXPECT_DEGREE 
DEPS_PARSE_COMPLETE 

Definition at line 28 of file pg_dependencies.c.

29{
DependenciesSemanticState
@ DEPS_EXPECT_START
@ DEPS_EXPECT_ATTNUM
@ DEPS_PARSE_COMPLETE
@ DEPS_EXPECT_ITEM
@ DEPS_EXPECT_DEPENDENCY
@ DEPS_EXPECT_ATTNUM_LIST
@ DEPS_EXPECT_KEY
@ DEPS_EXPECT_DEGREE

Function Documentation

◆ build_mvdependencies()

static bytea * build_mvdependencies ( DependenciesParseState parse,
char str 
)
static

Definition at line 647 of file pg_dependencies.c.

648{
649 int ndeps = list_length(parse->dependency_list);
650
652 bytea *bytes;
653
654 switch (parse->state)
655 {
657
658 /*
659 * Parse ended in the expected place. We should have a list of
660 * items, but if we do not there is an issue with one of the
661 * earlier parse steps.
662 */
663 if (ndeps == 0)
664 elog(ERROR,
665 "pg_dependencies parsing claims success with an empty item list.");
666 break;
667
669 /* blank */
670 errsave(parse->escontext,
672 errmsg("malformed pg_dependencies: \"%s\"", str),
673 errdetail("Value cannot be empty."));
674 return NULL;
675
676 default:
677 /* Unexpected end-state. */
678 errsave(parse->escontext,
680 errmsg("malformed pg_dependencies: \"%s\"", str),
681 errdetail("Unexpected end state has been found: %d.", parse->state));
682 return NULL;
683 }
684
686 + (ndeps * sizeof(MVDependency *)));
687 mvdeps->magic = STATS_DEPS_MAGIC;
689 mvdeps->ndeps = ndeps;
690
691 for (int i = 0; i < ndeps; i++)
692 {
693 /*
694 * Use the MVDependency objects in the dependency_list.
695 *
696 * Because we free the dependency_list after parsing is done, we
697 * cannot free it here.
698 */
699 mvdeps->deps[i] = list_nth(parse->dependency_list, i);
700
701 /*
702 * Ensure that this item does not duplicate the attributes of any
703 * pre-existing item.
704 */
705 for (int j = 0; j < i; j++)
706 {
707 if (dep_attributes_eq(mvdeps->deps[i], mvdeps->deps[j]))
708 {
709 MVDependency *dep = mvdeps->deps[i];
710 char *attnum_list = dep_attnum_list(dep);
712
713 errsave(parse->escontext,
715 errmsg("malformed pg_dependencies: \"%s\"", str),
716 errdetail("Duplicated \"%s\" array has been found: [%s] for key \"%s\" and value %d.",
719 pfree(mvdeps);
720 return NULL;
721 }
722 }
723 }
724
726
727 /*
728 * No need to free the individual MVDependency objects, because they are
729 * still in the dependency_list, and will be freed with that.
730 */
731 pfree(mvdeps);
732
733 return bytes;
734}
int16 AttrNumber
Definition attnum.h:21
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
int errcode(int sqlerrcode)
Definition elog.c:875
#define errsave(context,...)
Definition elog.h:264
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
const char * str
void parse(int)
Definition parse.c:49
int j
Definition isn.c:78
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1619
void * palloc0(Size size)
Definition mcxt.c:1420
static char * errmsg
static char * dep_attnum_list(const MVDependency *item)
static AttrNumber dep_attnum_dependency(const MVDependency *item)
static bool dep_attributes_eq(const MVDependency *a, const MVDependency *b)
static int list_length(const List *l)
Definition pg_list.h:152
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
static int fb(int x)
#define STATS_DEPS_MAGIC
Definition statistics.h:43
#define STATS_DEPS_TYPE_BASIC
Definition statistics.h:44
#define PG_DEPENDENCIES_KEY_ATTRIBUTES
#define PG_DEPENDENCIES_KEY_DEPENDENCY
Definition c.h:832

References dep_attnum_dependency(), dep_attnum_list(), dep_attributes_eq(), DEPS_EXPECT_START, DEPS_PARSE_COMPLETE, elog, errcode(), errdetail(), errmsg, ERROR, errsave, fb(), i, j, list_length(), list_nth(), palloc0(), parse(), pfree(), PG_DEPENDENCIES_KEY_ATTRIBUTES, PG_DEPENDENCIES_KEY_DEPENDENCY, statext_dependencies_serialize(), STATS_DEPS_MAGIC, STATS_DEPS_TYPE_BASIC, and str.

Referenced by pg_dependencies_in().

◆ dep_attnum_dependency()

static AttrNumber dep_attnum_dependency ( const MVDependency item)
static

Definition at line 634 of file pg_dependencies.c.

635{
636 return item->attributes[item->nattributes - 1];
637}
AttrNumber nattributes
Definition statistics.h:53
AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]
Definition statistics.h:54

References MVDependency::attributes, and MVDependency::nattributes.

Referenced by build_mvdependencies().

◆ dep_attnum_list()

static char * dep_attnum_list ( const MVDependency item)
static

Definition at line 616 of file pg_dependencies.c.

617{
619
621
622 appendStringInfo(&str, "%d", item->attributes[0]);
623
624 for (int i = 1; i < item->nattributes - 1; i++)
625 appendStringInfo(&str, ", %d", item->attributes[i]);
626
627 return str.data;
628}
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void initStringInfo(StringInfo str)
Definition stringinfo.c:97

References appendStringInfo(), MVDependency::attributes, i, initStringInfo(), MVDependency::nattributes, and str.

Referenced by build_mvdependencies().

◆ dep_attributes_eq()

static bool dep_attributes_eq ( const MVDependency a,
const MVDependency b 
)
static

Definition at line 592 of file pg_dependencies.c.

593{
594 int i;
595
596 if (a->nattributes != b->nattributes)
597 return false;
598
599 for (i = 0; i < a->nattributes; i++)
600 {
601 if (a->attributes[i] != b->attributes[i])
602 return false;
603 }
604
605 return true;
606}
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, b, and i.

Referenced by build_mvdependencies().

◆ dependencies_array_element_start()

static JsonParseErrorType dependencies_array_element_start ( void state,
bool  isnull 
)
static

Definition at line 412 of file pg_dependencies.c.

413{
415
416 switch (parse->state)
417 {
419 if (!isnull)
420 return JSON_SUCCESS;
421
422 errsave(parse->escontext,
424 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
425 errdetail("Attribute number array cannot be null."));
426 break;
427
428 case DEPS_EXPECT_ITEM:
429 if (!isnull)
430 return JSON_SUCCESS;
431
432 errsave(parse->escontext,
434 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
435 errdetail("Item list elements cannot be null."));
436 break;
437
438 default:
439 elog(ERROR,
440 "array element start of \"%s\" found in unexpected parse state: %d.",
441 "pg_dependencies", (int) parse->state);
442 break;
443 }
444
446}
@ JSON_SEM_ACTION_FAILED
Definition jsonapi.h:59
@ JSON_SUCCESS
Definition jsonapi.h:36

References DEPS_EXPECT_ATTNUM, DEPS_EXPECT_ITEM, elog, errcode(), errdetail(), errmsg, ERROR, errsave, fb(), JSON_SEM_ACTION_FAILED, JSON_SUCCESS, and parse().

Referenced by pg_dependencies_in().

◆ dependencies_array_end()

static JsonParseErrorType dependencies_array_end ( void state)
static

Definition at line 284 of file pg_dependencies.c.

285{
287
288 switch (parse->state)
289 {
291 if (list_length(parse->attnum_list) > 0)
292 {
293 parse->state = DEPS_EXPECT_KEY;
294 return JSON_SUCCESS;
295 }
296
297 errsave(parse->escontext,
299 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
300 errdetail("The \"%s\" key must be a non-empty array.",
302 break;
303
304 case DEPS_EXPECT_ITEM:
305 if (list_length(parse->dependency_list) > 0)
306 {
307 parse->state = DEPS_PARSE_COMPLETE;
308 return JSON_SUCCESS;
309 }
310
311 errsave(parse->escontext,
313 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
314 errdetail("Item array cannot be empty."));
315 break;
316
317 default:
318
319 /*
320 * This can only happen if a case was missed in
321 * dependencies_array_start().
322 */
323 elog(ERROR,
324 "array end of \"%s\" found in unexpected parse state: %d.",
325 "pg_dependencies", (int) parse->state);
326 break;
327 }
329}

References DEPS_EXPECT_ATTNUM, DEPS_EXPECT_ITEM, DEPS_EXPECT_KEY, DEPS_PARSE_COMPLETE, elog, errcode(), errdetail(), errmsg, ERROR, errsave, fb(), JSON_SEM_ACTION_FAILED, JSON_SUCCESS, list_length(), parse(), and PG_DEPENDENCIES_KEY_ATTRIBUTES.

Referenced by pg_dependencies_in().

◆ dependencies_array_start()

static JsonParseErrorType dependencies_array_start ( void state)
static

Definition at line 255 of file pg_dependencies.c.

256{
258
259 switch (parse->state)
260 {
262 parse->state = DEPS_EXPECT_ATTNUM;
263 break;
265 parse->state = DEPS_EXPECT_ITEM;
266 break;
267 default:
268 errsave(parse->escontext,
270 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
271 errdetail("Array has been found at an unexpected location."));
273 }
274
275 return JSON_SUCCESS;
276}

References DEPS_EXPECT_ATTNUM, DEPS_EXPECT_ATTNUM_LIST, DEPS_EXPECT_ITEM, DEPS_EXPECT_START, errcode(), errdetail(), errmsg, errsave, fb(), JSON_SEM_ACTION_FAILED, JSON_SUCCESS, and parse().

Referenced by pg_dependencies_in().

◆ dependencies_object_end()

static JsonParseErrorType dependencies_object_end ( void state)
static

Definition at line 142 of file pg_dependencies.c.

143{
145
147
148 int natts = 0;
149
150 if (parse->state != DEPS_EXPECT_KEY)
151 elog(ERROR,
152 "object end of \"%s\" found in unexpected parse state: %d.",
153 "pg_dependencies", (int) parse->state);
154
155 if (!parse->found_attributes)
156 {
157 errsave(parse->escontext,
159 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
160 errdetail("Item must contain \"%s\" key.",
163 }
164
165 if (!parse->found_dependency)
166 {
167 errsave(parse->escontext,
169 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
170 errdetail("Item must contain \"%s\" key.",
173 }
174
175 if (!parse->found_degree)
176 {
177 errsave(parse->escontext,
179 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
180 errdetail("Item must contain \"%s\" key.",
183 }
184
185 /*
186 * We need at least one attribute number in a dependencies item, anything
187 * less is malformed.
188 */
189 natts = list_length(parse->attnum_list);
190 if ((natts < 1) || (natts > (STATS_MAX_DIMENSIONS - 1)))
191 {
192 errsave(parse->escontext,
194 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
195 errdetail("The \"%s\" key must contain an array of at least %d and no more than %d elements.",
199 }
200
201 /*
202 * Allocate enough space for the dependency, the attribute numbers in the
203 * list and the final attribute number for the dependency.
204 */
205 dep = palloc0(offsetof(MVDependency, attributes) + ((natts + 1) * sizeof(AttrNumber)));
206 dep->nattributes = natts + 1;
207
208 dep->attributes[natts] = parse->dependency;
209 dep->degree = parse->degree;
210
211 /*
212 * Assign attribute numbers to the attributes array, comparing each one
213 * against the dependency attribute to ensure that there are no matches.
214 */
215 for (int i = 0; i < natts; i++)
216 {
217 dep->attributes[i] = (AttrNumber) list_nth_int(parse->attnum_list, i);
218 if (dep->attributes[i] == parse->dependency)
219 {
220 errsave(parse->escontext,
222 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
223 errdetail("Item \"%s\" with value %d has been found in the \"%s\" list.",
227 }
228 }
229
230 parse->dependency_list = lappend(parse->dependency_list, (void *) dep);
231
232 /*
233 * Reset dependency item state variables to look for the next
234 * MVDependency.
235 */
236 list_free(parse->attnum_list);
237 parse->attnum_list = NIL;
238 parse->dependency = 0;
239 parse->degree = 0.0;
240 parse->found_attributes = false;
241 parse->found_dependency = false;
242 parse->found_degree = false;
243 parse->state = DEPS_EXPECT_ITEM;
244
245 return JSON_SUCCESS;
246}
List * lappend(List *list, void *datum)
Definition list.c:339
void list_free(List *list)
Definition list.c:1546
#define NIL
Definition pg_list.h:68
static int list_nth_int(const List *list, int n)
Definition pg_list.h:342
#define STATS_MAX_DIMENSIONS
Definition statistics.h:19
#define PG_DEPENDENCIES_KEY_DEGREE

References DEPS_EXPECT_ITEM, DEPS_EXPECT_KEY, elog, errcode(), errdetail(), errmsg, ERROR, errsave, fb(), i, JSON_SEM_ACTION_FAILED, JSON_SUCCESS, lappend(), list_free(), list_length(), list_nth_int(), NIL, palloc0(), parse(), PG_DEPENDENCIES_KEY_ATTRIBUTES, PG_DEPENDENCIES_KEY_DEGREE, PG_DEPENDENCIES_KEY_DEPENDENCY, and STATS_MAX_DIMENSIONS.

Referenced by pg_dependencies_in().

◆ dependencies_object_field_start()

static JsonParseErrorType dependencies_object_field_start ( void state,
char fname,
bool  isnull 
)
static

Definition at line 340 of file pg_dependencies.c.

341{
343
344 if (strcmp(fname, PG_DEPENDENCIES_KEY_ATTRIBUTES) == 0)
345 {
346 if (parse->found_attributes)
347 {
348 errsave(parse->escontext,
350 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
351 errdetail("Multiple \"%s\" keys are not allowed.",
354 }
355
356 parse->found_attributes = true;
358 return JSON_SUCCESS;
359 }
360
361 if (strcmp(fname, PG_DEPENDENCIES_KEY_DEPENDENCY) == 0)
362 {
363 if (parse->found_dependency)
364 {
365 errsave(parse->escontext,
367 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
368 errdetail("Multiple \"%s\" keys are not allowed.",
371 }
372
373 parse->found_dependency = true;
375 return JSON_SUCCESS;
376 }
377
378 if (strcmp(fname, PG_DEPENDENCIES_KEY_DEGREE) == 0)
379 {
380 if (parse->found_degree)
381 {
382 errsave(parse->escontext,
384 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
385 errdetail("Multiple \"%s\" keys are not allowed.",
388 }
389
390 parse->found_degree = true;
391 parse->state = DEPS_EXPECT_DEGREE;
392 return JSON_SUCCESS;
393 }
394
395 errsave(parse->escontext,
397 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
398 errdetail("Only allowed keys are \"%s\", \"%s\", and \"%s\".",
403}

References DEPS_EXPECT_ATTNUM_LIST, DEPS_EXPECT_DEGREE, DEPS_EXPECT_DEPENDENCY, errcode(), errdetail(), errmsg, errsave, fb(), JSON_SEM_ACTION_FAILED, JSON_SUCCESS, parse(), PG_DEPENDENCIES_KEY_ATTRIBUTES, PG_DEPENDENCIES_KEY_DEGREE, and PG_DEPENDENCIES_KEY_DEPENDENCY.

Referenced by pg_dependencies_in().

◆ dependencies_object_start()

static JsonParseErrorType dependencies_object_start ( void state)
static

Definition at line 64 of file pg_dependencies.c.

65{
67
68 switch (parse->state)
69 {
71 /* Now we expect to see attributes/dependency/degree keys */
72 parse->state = DEPS_EXPECT_KEY;
73 return JSON_SUCCESS;
74
76 /* pg_dependencies must begin with a '[' */
77 errsave(parse->escontext,
79 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
80 errdetail("Initial element must be an array."));
81 break;
82
83 case DEPS_EXPECT_KEY:
84 /* In an object, expecting key */
85 errsave(parse->escontext,
87 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
88 errdetail("A key was expected."));
89 break;
90
92 /* Just followed an "attributes": key */
93 errsave(parse->escontext,
95 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
96 errdetail("Value of \"%s\" must be an array of attribute numbers.",
98 break;
99
101 /* In an attribute number list, expect only scalar integers */
102 errsave(parse->escontext,
104 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
105 errdetail("Attribute lists can only contain attribute numbers."));
106 break;
107
109 /* Just followed a "dependency" key */
110 errsave(parse->escontext,
112 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
113 errdetail("Value of \"%s\" must be an integer.",
115 break;
116
118 /* Just followed a "degree" key */
119 errsave(parse->escontext,
121 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
122 errdetail("Value of \"%s\" must be an integer.",
124 break;
125
126 default:
127 elog(ERROR,
128 "object start of \"%s\" found in unexpected parse state: %d.",
129 "pg_dependencies", (int) parse->state);
130 break;
131 }
132
134}

References DEPS_EXPECT_ATTNUM, DEPS_EXPECT_ATTNUM_LIST, DEPS_EXPECT_DEGREE, DEPS_EXPECT_DEPENDENCY, DEPS_EXPECT_ITEM, DEPS_EXPECT_KEY, DEPS_EXPECT_START, elog, errcode(), errdetail(), errmsg, ERROR, errsave, fb(), JSON_SEM_ACTION_FAILED, JSON_SUCCESS, parse(), PG_DEPENDENCIES_KEY_ATTRIBUTES, PG_DEPENDENCIES_KEY_DEGREE, and PG_DEPENDENCIES_KEY_DEPENDENCY.

Referenced by pg_dependencies_in().

◆ dependencies_scalar()

static JsonParseErrorType dependencies_scalar ( void state,
char token,
JsonTokenType  tokentype 
)
static

Definition at line 478 of file pg_dependencies.c.

479{
483
484 switch (parse->state)
485 {
487 attnum = pg_strtoint16_safe(token, (Node *) &escontext);
488
489 if (escontext.error_occurred)
490 {
491 errsave(parse->escontext,
493 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
494 errdetail("Key \"%s\" has an incorrect value.", PG_DEPENDENCIES_KEY_ATTRIBUTES));
496 }
497
498 /*
499 * An attribute number cannot be zero or a negative number beyond
500 * the number of the possible expressions.
501 */
502 if (attnum == 0 || attnum < (0 - STATS_MAX_DIMENSIONS))
503 {
504 errsave(parse->escontext,
506 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
507 errdetail("Invalid \"%s\" element has been found: %d.",
510 }
511
512 if (parse->attnum_list != NIL)
513 {
514 const AttrNumber prev = llast_int(parse->attnum_list);
515
517 {
518 errsave(parse->escontext,
520 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
521 errdetail("Invalid \"%s\" element has been found: %d cannot follow %d.",
524 }
525 }
526
527 parse->attnum_list = lappend_int(parse->attnum_list, (int) attnum);
528 return JSON_SUCCESS;
529
531 parse->dependency = (AttrNumber)
532 pg_strtoint16_safe(token, (Node *) &escontext);
533
534 if (escontext.error_occurred)
535 {
536 errsave(parse->escontext,
538 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
539 errdetail("Key \"%s\" has an incorrect value.", PG_DEPENDENCIES_KEY_DEPENDENCY));
541 }
542
543 /*
544 * The dependency attribute number cannot be zero or a negative
545 * number beyond the number of the possible expressions.
546 */
547 if (parse->dependency == 0 || parse->dependency < (0 - STATS_MAX_DIMENSIONS))
548 {
549 errsave(parse->escontext,
551 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
552 errdetail("Key \"%s\" has an incorrect value: %d.",
555 }
556
557 parse->state = DEPS_EXPECT_KEY;
558 return JSON_SUCCESS;
559
561 parse->degree = float8in_internal(token, NULL, "double",
562 token, (Node *) &escontext);
563
564 if (escontext.error_occurred)
565 {
566 errsave(parse->escontext,
568 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
569 errdetail("Key \"%s\" has an incorrect value.", PG_DEPENDENCIES_KEY_DEGREE));
571 }
572
573 parse->state = DEPS_EXPECT_KEY;
574 return JSON_SUCCESS;
575
576 default:
577 errsave(parse->escontext,
579 errmsg("malformed pg_dependencies: \"%s\"", parse->str),
580 errdetail("Unexpected scalar has been found."));
581 break;
582 }
583
585}
float8 float8in_internal(char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext)
Definition float.c:436
List * lappend_int(List *list, int datum)
Definition list.c:357
int16 pg_strtoint16_safe(const char *s, Node *escontext)
Definition numutils.c:127
int16 attnum
static bool valid_subsequent_attnum(const AttrNumber prev, const AttrNumber cur)
#define llast_int(l)
Definition pg_list.h:199
Definition nodes.h:133

References attnum, DEPS_EXPECT_ATTNUM, DEPS_EXPECT_DEGREE, DEPS_EXPECT_DEPENDENCY, DEPS_EXPECT_KEY, errcode(), errdetail(), errmsg, errsave, fb(), float8in_internal(), JSON_SEM_ACTION_FAILED, JSON_SUCCESS, lappend_int(), llast_int, NIL, parse(), PG_DEPENDENCIES_KEY_ATTRIBUTES, PG_DEPENDENCIES_KEY_DEGREE, PG_DEPENDENCIES_KEY_DEPENDENCY, pg_strtoint16_safe(), STATS_MAX_DIMENSIONS, and valid_subsequent_attnum().

Referenced by pg_dependencies_in().

◆ pg_dependencies_in()

Datum pg_dependencies_in ( PG_FUNCTION_ARGS  )

Definition at line 747 of file pg_dependencies.c.

748{
749 char *str = PG_GETARG_CSTRING(0);
750 bytea *bytes = NULL;
751
754 JsonLexContext *lex;
756
757 /* initialize the semantic state */
758 parse_state.str = str;
760 parse_state.dependency_list = NIL;
761 parse_state.attnum_list = NIL;
762 parse_state.dependency = 0;
763 parse_state.degree = 0.0;
764 parse_state.found_attributes = false;
765 parse_state.found_dependency = false;
766 parse_state.found_degree = false;
767 parse_state.escontext = fcinfo->context;
768
769 /* set callbacks */
770 sem_action.semstate = (void *) &parse_state;
775 sem_action.array_element_start = dependencies_array_element_start;
776 sem_action.array_element_end = NULL;
777 sem_action.object_field_start = dependencies_object_field_start;
778 sem_action.object_field_end = NULL;
780
782
785
786 if (result == JSON_SUCCESS)
788
789 list_free_deep(parse_state.dependency_list);
790 list_free(parse_state.attnum_list);
791
792 if (bytes)
793 PG_RETURN_BYTEA_P(bytes);
794
795 /*
796 * If escontext already set, just use that. Anything else is a generic
797 * JSON parse error.
798 */
799 if (!SOFT_ERROR_OCCURRED(parse_state.escontext))
800 errsave(parse_state.escontext,
802 errmsg("malformed pg_dependencies: \"%s\"", str),
803 errdetail("Input data must be valid JSON."));
804
806}
uint32 result
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
JsonParseErrorType pg_parse_json(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:744
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
Definition jsonapi.c:392
void freeJsonLexContext(JsonLexContext *lex)
Definition jsonapi.c:687
JsonParseErrorType
Definition jsonapi.h:35
void list_free_deep(List *list)
Definition list.c:1560
#define PG_UTF8
Definition mbprint.c:43
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
static JsonParseErrorType dependencies_array_end(void *state)
static JsonParseErrorType dependencies_array_start(void *state)
static JsonParseErrorType dependencies_array_element_start(void *state, bool isnull)
static JsonParseErrorType dependencies_object_end(void *state)
static bytea * build_mvdependencies(DependenciesParseState *parse, char *str)
static JsonParseErrorType dependencies_scalar(void *state, char *token, JsonTokenType tokentype)
static JsonParseErrorType dependencies_object_field_start(void *state, char *fname, bool isnull)
static JsonParseErrorType dependencies_object_start(void *state)
void * semstate
Definition jsonapi.h:153

References build_mvdependencies(), dependencies_array_element_start(), dependencies_array_end(), dependencies_array_start(), dependencies_object_end(), dependencies_object_field_start(), dependencies_object_start(), dependencies_scalar(), DEPS_EXPECT_START, errcode(), errdetail(), errmsg, errsave, fb(), freeJsonLexContext(), JSON_SUCCESS, list_free(), list_free_deep(), makeJsonLexContextCstringLen(), NIL, PG_GETARG_CSTRING, pg_parse_json(), PG_RETURN_BYTEA_P, PG_RETURN_NULL, PG_UTF8, result, JsonSemAction::semstate, SOFT_ERROR_OCCURRED, and str.

◆ pg_dependencies_out()

Datum pg_dependencies_out ( PG_FUNCTION_ARGS  )

Definition at line 813 of file pg_dependencies.c.

814{
818
821
822 for (int i = 0; i < dependencies->ndeps; i++)
823 {
824 MVDependency *dependency = dependencies->deps[i];
825
826 if (i > 0)
828
829 if (dependency->nattributes <= 1)
830 elog(ERROR, "invalid zero-length nattributes array in MVDependencies");
831
833 dependency->attributes[0]);
834
835 for (int j = 1; j < dependency->nattributes - 1; j++)
836 appendStringInfo(&str, ", %d", dependency->attributes[j]);
837
839 "\"" PG_DEPENDENCIES_KEY_DEGREE "\": %f}",
840 dependency->attributes[dependency->nattributes - 1],
841 dependency->degree);
842 }
843
845
847}
MVDependencies * statext_dependencies_deserialize(bytea *data)
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
const void * data
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
MVDependency * deps[FLEXIBLE_ARRAY_MEMBER]
Definition statistics.h:62
double degree
Definition statistics.h:52

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), MVDependency::attributes, data, MVDependency::degree, MVDependencies::deps, elog, ERROR, i, initStringInfo(), j, MVDependency::nattributes, MVDependencies::ndeps, PG_DEPENDENCIES_KEY_ATTRIBUTES, PG_DEPENDENCIES_KEY_DEGREE, PG_DEPENDENCIES_KEY_DEPENDENCY, PG_GETARG_BYTEA_PP, PG_RETURN_CSTRING, statext_dependencies_deserialize(), and str.

◆ pg_dependencies_recv()

Datum pg_dependencies_recv ( PG_FUNCTION_ARGS  )

Definition at line 853 of file pg_dependencies.c.

854{
857 errmsg("cannot accept a value of type %s", "pg_dependencies")));
858
859 PG_RETURN_VOID(); /* keep compiler quiet */
860}
#define ereport(elevel,...)
Definition elog.h:152
#define PG_RETURN_VOID()
Definition fmgr.h:350

References ereport, errcode(), errmsg, ERROR, fb(), and PG_RETURN_VOID.

◆ pg_dependencies_send()

Datum pg_dependencies_send ( PG_FUNCTION_ARGS  )

Definition at line 869 of file pg_dependencies.c.

870{
871 return byteasend(fcinfo);
872}
Datum byteasend(PG_FUNCTION_ARGS)
Definition bytea.c:377

References byteasend().

◆ valid_subsequent_attnum()

static bool valid_subsequent_attnum ( const AttrNumber  prev,
const AttrNumber  cur 
)
static

Definition at line 461 of file pg_dependencies.c.

462{
463 Assert(prev != 0);
464
465 if (prev > 0)
466 return ((cur > prev) || (cur < 0));
467
468 return (cur < prev);
469}
#define Assert(condition)
Definition c.h:999
struct cursor * cur
Definition ecpg.c:29

References Assert, and cur.

Referenced by dependencies_scalar().