1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
|
/*-------------------------------------------------------------------------
*
* pg_dependencies.c
* pg_dependencies data type support.
*
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/utils/adt/pg_dependencies.c
*
*-------------------------------------------------------------------------
*/
#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"
typedef enum
{
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,
} DependenciesSemanticState;
typedef struct
{
const char *str;
DependenciesSemanticState state;
List *dependency_list;
Node *escontext;
bool found_attributes; /* Item has an attributes key */
bool found_dependency; /* Item has an dependency key */
bool found_degree; /* Item has degree key */
List *attnum_list; /* Accumulated attribute numbers */
AttrNumber dependency;
double degree;
} DependenciesParseState;
/*
* Invoked at the start of each MVDependency object.
*
* The entire JSON document should be one array of MVDependency objects.
*
* If we are anywhere else in the document, it's an error.
*/
static JsonParseErrorType
dependencies_object_start(void *state)
{
DependenciesParseState *parse = state;
switch (parse->state)
{
case DEPS_EXPECT_ITEM:
/* Now we expect to see attributes/dependency/degree keys */
parse->state = DEPS_EXPECT_KEY;
return JSON_SUCCESS;
case DEPS_EXPECT_START:
/* pg_dependencies must begin with a '[' */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Initial element must be an array."));
break;
case DEPS_EXPECT_KEY:
/* In an object, expecting key */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Expected an object key."));
break;
case DEPS_EXPECT_ATTNUM_LIST:
/* Just followed an "attributes": key */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Value of \"%s\" must be an array of attribute numbers.",
PG_DEPENDENCIES_KEY_ATTRIBUTES));
break;
case DEPS_EXPECT_ATTNUM:
/* In an attribute number list, expect only scalar integers */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Attribute lists can only contain attribute numbers."));
break;
case DEPS_EXPECT_DEPENDENCY:
/* Just followed a "dependency" key */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Value of \"%s\" must be an integer.",
PG_DEPENDENCIES_KEY_DEPENDENCY));
break;
case DEPS_EXPECT_DEGREE:
/* Just followed a "degree" key */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Value of \"%s\" must be an integer.",
PG_DEPENDENCIES_KEY_DEGREE));
break;
default:
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Unexpected parse state: %d", (int) parse->state));
break;
}
return JSON_SEM_ACTION_FAILED;
}
/*
* Invoked at the end of an object.
*
* Handle the end of an MVDependency object's JSON representation.
*/
static JsonParseErrorType
dependencies_object_end(void *state)
{
DependenciesParseState *parse = state;
MVDependency *dep;
int natts = 0;
if (parse->state != DEPS_EXPECT_KEY)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Unexpected parse state: %d", (int) parse->state));
return JSON_SEM_ACTION_FAILED;
}
if (!parse->found_attributes)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Item must contain \"%s\" key",
PG_DEPENDENCIES_KEY_ATTRIBUTES));
return JSON_SEM_ACTION_FAILED;
}
if (!parse->found_dependency)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Item must contain \"%s\" key.",
PG_DEPENDENCIES_KEY_DEPENDENCY));
return JSON_SEM_ACTION_FAILED;
}
if (!parse->found_degree)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Item must contain \"%s\" key.",
PG_DEPENDENCIES_KEY_DEGREE));
return JSON_SEM_ACTION_FAILED;
}
/*
* We need at least one attribute number in a dependencies item, anything
* less is malformed.
*/
natts = list_length(parse->attnum_list);
if ((natts < 1) || (natts > (STATS_MAX_DIMENSIONS - 1)))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("The \"%s\" key must contain an array of at least %d and no more than %d elements.",
PG_DEPENDENCIES_KEY_ATTRIBUTES, 1,
STATS_MAX_DIMENSIONS - 1));
return JSON_SEM_ACTION_FAILED;
}
/*
* Allocate enough space for the dependency, the attribute numbers in the
* list and the final attribute number for the dependency.
*/
dep = palloc0(offsetof(MVDependency, attributes) + ((natts + 1) * sizeof(AttrNumber)));
dep->nattributes = natts + 1;
dep->attributes[natts] = parse->dependency;
dep->degree = parse->degree;
/*
* Assign attribute numbers to the attributes array, comparing each one
* against the dependency attribute to ensure that there there are no
* matches.
*/
for (int i = 0; i < natts; i++)
{
dep->attributes[i] = (AttrNumber) list_nth_int(parse->attnum_list, i);
if (dep->attributes[i] == parse->dependency)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Item \"%s\" value %d found in the \"%s\" list.",
PG_DEPENDENCIES_KEY_DEPENDENCY, parse->dependency,
PG_DEPENDENCIES_KEY_ATTRIBUTES));
return JSON_SEM_ACTION_FAILED;
}
}
parse->dependency_list = lappend(parse->dependency_list, (void *) dep);
/*
* Reset dependency item state variables to look for the next
* MVDependency.
*/
list_free(parse->attnum_list);
parse->attnum_list = NIL;
parse->dependency = 0;
parse->degree = 0.0;
parse->found_attributes = false;
parse->found_dependency = false;
parse->found_degree = false;
parse->state = DEPS_EXPECT_ITEM;
return JSON_SUCCESS;
}
/*
* Invoked at the start of an array.
*
* Dependency input format does not have arrays, so any array elements
* encountered are an error.
*/
static JsonParseErrorType
dependencies_array_start(void *state)
{
DependenciesParseState *parse = state;
switch (parse->state)
{
case DEPS_EXPECT_ATTNUM_LIST:
parse->state = DEPS_EXPECT_ATTNUM;
break;
case DEPS_EXPECT_START:
parse->state = DEPS_EXPECT_ITEM;
break;
default:
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Array found in unexpected place."));
return JSON_SEM_ACTION_FAILED;
}
return JSON_SUCCESS;
}
/*
* Invoked at the end of an array.
*
* Either the end of an attribute number list or the whole object.
*/
static JsonParseErrorType
dependencies_array_end(void *state)
{
DependenciesParseState *parse = state;
switch (parse->state)
{
case DEPS_EXPECT_ATTNUM:
if (list_length(parse->attnum_list) > 0)
{
parse->state = DEPS_EXPECT_KEY;
return JSON_SUCCESS;
}
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("The \"%s\" key must be an non-empty array.",
PG_DEPENDENCIES_KEY_ATTRIBUTES));
break;
case DEPS_EXPECT_ITEM:
if (list_length(parse->dependency_list) > 0)
{
parse->state = DEPS_PARSE_COMPLETE;
return JSON_SUCCESS;
}
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Item array cannot be empty."));
break;
default:
/*
* This can only happen if a case was missed in
* dependencies_array_start().
*/
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Array found in unexpected place."));
break;
}
return JSON_SEM_ACTION_FAILED;
}
/*
* Invoked at the start of a key/value field.
*
* The valid keys for the MVDependency object are:
* - attributes
* - dependency
* - degree
*/
static JsonParseErrorType
dependencies_object_field_start(void *state, char *fname, bool isnull)
{
DependenciesParseState *parse = state;
if (strcmp(fname, PG_DEPENDENCIES_KEY_ATTRIBUTES) == 0)
{
if (parse->found_attributes)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Multiple \"%s\" keys are not allowed.",
PG_DEPENDENCIES_KEY_ATTRIBUTES));
return JSON_SEM_ACTION_FAILED;
}
parse->found_attributes = true;
parse->state = DEPS_EXPECT_ATTNUM_LIST;
return JSON_SUCCESS;
}
if (strcmp(fname, PG_DEPENDENCIES_KEY_DEPENDENCY) == 0)
{
if (parse->found_dependency)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Multiple \"%s\" keys are not allowed.",
PG_DEPENDENCIES_KEY_DEPENDENCY));
return JSON_SEM_ACTION_FAILED;
}
parse->found_dependency = true;
parse->state = DEPS_EXPECT_DEPENDENCY;
return JSON_SUCCESS;
}
if (strcmp(fname, PG_DEPENDENCIES_KEY_DEGREE) == 0)
{
if (parse->found_degree)
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Multiple \"%s\" keys are not allowed.",
PG_DEPENDENCIES_KEY_DEGREE));
return JSON_SEM_ACTION_FAILED;
}
parse->found_degree = true;
parse->state = DEPS_EXPECT_DEGREE;
return JSON_SUCCESS;
}
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Only allowed keys are \"%s\", \"%s\" and \"%s\".",
PG_DEPENDENCIES_KEY_ATTRIBUTES,
PG_DEPENDENCIES_KEY_DEPENDENCY,
PG_DEPENDENCIES_KEY_DEGREE));
return JSON_SEM_ACTION_FAILED;
}
/*
* Invoked at the start of an array element.
*
* pg_dependencies input format does not have arrays, so any array elements
* encountered are an error.
*/
static JsonParseErrorType
dependencies_array_element_start(void *state, bool isnull)
{
DependenciesParseState *parse = state;
switch (parse->state)
{
case DEPS_EXPECT_ATTNUM:
if (!isnull)
return JSON_SUCCESS;
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Attribute number array cannot be null."));
break;
case DEPS_EXPECT_ITEM:
if (!isnull)
return JSON_SUCCESS;
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Item list elements cannot be null."));
break;
default:
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Unexpected array element."));
break;
}
return JSON_SEM_ACTION_FAILED;
}
/*
* Test for valid subsequent attribute number.
*
* If the previous value is positive, then current value must either be
* greater than the previous value, or negative.
*
* If the previous value is negative, then the value must be less than
* the previous value.
*
* Duplicate values are not allowed; that is already covered by the rules
* described above.
*/
static bool
valid_subsequent_attnum(const AttrNumber prev, const AttrNumber cur)
{
Assert(prev != 0);
if (prev > 0)
return ((cur > prev) || (cur < 0));
return (cur < prev);
}
/*
* Handle scalar events from the dependencies input parser.
*
* There is only one case where we will encounter a scalar, and that is the
* dependency degree for the previous object key.
*/
static JsonParseErrorType
dependencies_scalar(void *state, char *token, JsonTokenType tokentype)
{
DependenciesParseState *parse = state;
AttrNumber attnum;
ErrorSaveContext escontext = {T_ErrorSaveContext};
switch (parse->state)
{
case DEPS_EXPECT_ATTNUM:
attnum = pg_strtoint16_safe(token, (Node *) &escontext);
if (SOFT_ERROR_OCCURRED(&escontext))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Invalid \"%s\" value.", PG_DEPENDENCIES_KEY_ATTRIBUTES));
return JSON_SEM_ACTION_FAILED;
}
/*
* An attribute number cannot be zero or a negative number beyond
* the number of the possible expressions.
*/
if (attnum == 0 || attnum < (0 - STATS_MAX_DIMENSIONS))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Invalid \"%s\" element: %d.",
PG_DEPENDENCIES_KEY_ATTRIBUTES, attnum));
return JSON_SEM_ACTION_FAILED;
}
if (parse->attnum_list != NIL)
{
const AttrNumber prev = llast_int(parse->attnum_list);
if (!valid_subsequent_attnum(prev, attnum))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Invalid \"%s\" element: %d cannot follow %d.",
PG_DEPENDENCIES_KEY_ATTRIBUTES, attnum, prev));
return JSON_SEM_ACTION_FAILED;
}
}
parse->attnum_list = lappend_int(parse->attnum_list, (int) attnum);
return JSON_SUCCESS;
case DEPS_EXPECT_DEPENDENCY:
parse->dependency = (AttrNumber)
pg_strtoint16_safe(token, (Node *) &escontext);
if (SOFT_ERROR_OCCURRED(&escontext))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Invalid \"%s\" value.", PG_DEPENDENCIES_KEY_DEPENDENCY));
return JSON_SEM_ACTION_FAILED;
}
/*
* The dependency attribute number cannot be zero or a negative
* number beyond the number of the possible expressions.
*/
if (parse->dependency == 0 || parse->dependency < (0 - STATS_MAX_DIMENSIONS))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Invalid \"%s\" value: %d.",
PG_DEPENDENCIES_KEY_DEPENDENCY, parse->dependency));
return JSON_SEM_ACTION_FAILED;
}
parse->state = DEPS_EXPECT_KEY;
return JSON_SUCCESS;
case DEPS_EXPECT_DEGREE:
parse->degree = float8in_internal(token, NULL, "double",
token, (Node *) &escontext);
if (SOFT_ERROR_OCCURRED(&escontext))
{
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Invalid \"%s\" value.", PG_DEPENDENCIES_KEY_DEGREE));
return JSON_SEM_ACTION_FAILED;
}
parse->state = DEPS_EXPECT_KEY;
return JSON_SUCCESS;
default:
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", parse->str),
errdetail("Unexpected scalar."));
break;
}
return JSON_SEM_ACTION_FAILED;
}
/*
* Compare the attribute arrays of two MVDependency values,
* looking for duplicated sets.
*/
static bool
dep_attributes_eq(const MVDependency *a, const MVDependency *b)
{
int i;
if (a->nattributes != b->nattributes)
return false;
for (i = 0; i < a->nattributes; i++)
{
if (a->attributes[i] != b->attributes[i])
return false;
}
return true;
}
/*
* Generate a string representing an array of attribute numbers.
* Internally, the dependency attribute is the last element, so we
* leave that off.
*
* Freeing the allocated string is the responsibility of the caller.
*/
static char *
dep_attnum_list(const MVDependency *item)
{
StringInfoData str;
initStringInfo(&str);
appendStringInfo(&str, "%d", item->attributes[0]);
for (int i = 1; i < item->nattributes - 1; i++)
appendStringInfo(&str, ", %d", item->attributes[i]);
return str.data;
}
/*
* Return the dependency, which is the last attribute element.
*/
static AttrNumber
dep_attnum_dependency(const MVDependency *item)
{
return item->attributes[item->nattributes - 1];
}
/*
* Attempt to build and serialize the MVDependencies object.
*
* This can only be executed after the completion of the JSON parsing.
*
* In the event of an error, set the error context and return NULL.
*/
static bytea *
build_mvdependencies(DependenciesParseState *parse, char *str)
{
int ndeps = list_length(parse->dependency_list);
MVDependencies *mvdeps;
bytea *bytes;
switch (parse->state)
{
case DEPS_PARSE_COMPLETE:
/*
* Parse ended in the expected place. We should have a list of
* items, but if we do not there is an issue with one of the
* earlier parse steps.
*/
if (ndeps == 0)
elog(ERROR,
"pg_dependencies parsing claims success with an empty item list.");
break;
case DEPS_EXPECT_START:
/* blank */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", str),
errdetail("Value cannot be empty."));
return NULL;
default:
/* Unexpected end-state. */
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", str),
errdetail("Unexpected end state %d.", parse->state));
return NULL;
}
mvdeps = palloc0(offsetof(MVDependencies, deps)
+ (ndeps * sizeof(MVDependency *)));
mvdeps->magic = STATS_DEPS_MAGIC;
mvdeps->type = STATS_DEPS_TYPE_BASIC;
mvdeps->ndeps = ndeps;
for (int i = 0; i < ndeps; i++)
{
/*
* Use the MVDependency objects in the dependency_list.
*
* Because we free the dependency_list after parsing is done, we
* cannot free it here.
*/
mvdeps->deps[i] = list_nth(parse->dependency_list, i);
/*
* Ensure that this item does not duplicate the attributes of any
* pre-existing item.
*/
for (int j = 0; j < i; j++)
{
if (dep_attributes_eq(mvdeps->deps[i], mvdeps->deps[j]))
{
MVDependency *dep = mvdeps->deps[i];
char *attnum_list = dep_attnum_list(dep);
AttrNumber attnum_dep = dep_attnum_dependency(dep);
errsave(parse->escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", str),
errdetail("Duplicate \"%s\" array: [%s] with \"%s\": %d.",
PG_DEPENDENCIES_KEY_ATTRIBUTES, attnum_list,
PG_DEPENDENCIES_KEY_DEPENDENCY, attnum_dep));
pfree(mvdeps);
return NULL;
}
}
}
bytes = statext_dependencies_serialize(mvdeps);
/*
* No need to free the individual MVDependency objects, because they are
* still in the dependency_list, and will be freed with that.
*/
pfree(mvdeps);
return bytes;
}
/*
* pg_dependencies_in - input routine for type pg_dependencies.
*
* This format is valid JSON, with the expected format:
* [{"attributes": [1,2], "dependency": -1, "degree": 1.0000},
* {"attributes": [1,-1], "dependency": 2, "degree": 0.0000},
* {"attributes": [2,-1], "dependency": 1, "degree": 1.0000}]
*
*/
Datum
pg_dependencies_in(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
bytea *bytes = NULL;
DependenciesParseState parse_state;
JsonParseErrorType result;
JsonLexContext *lex;
JsonSemAction sem_action;
/* initialize the semantic state */
parse_state.str = str;
parse_state.state = DEPS_EXPECT_START;
parse_state.dependency_list = NIL;
parse_state.attnum_list = NIL;
parse_state.dependency = 0;
parse_state.degree = 0.0;
parse_state.found_attributes = false;
parse_state.found_dependency = false;
parse_state.found_degree = false;
parse_state.escontext = fcinfo->context;
/* set callbacks */
sem_action.semstate = (void *) &parse_state;
sem_action.object_start = dependencies_object_start;
sem_action.object_end = dependencies_object_end;
sem_action.array_start = dependencies_array_start;
sem_action.array_end = dependencies_array_end;
sem_action.array_element_start = dependencies_array_element_start;
sem_action.array_element_end = NULL;
sem_action.object_field_start = dependencies_object_field_start;
sem_action.object_field_end = NULL;
sem_action.scalar = dependencies_scalar;
lex = makeJsonLexContextCstringLen(NULL, str, strlen(str), PG_UTF8, true);
result = pg_parse_json(lex, &sem_action);
freeJsonLexContext(lex);
if (result == JSON_SUCCESS)
bytes = build_mvdependencies(&parse_state, str);
list_free_deep(parse_state.dependency_list);
list_free(parse_state.attnum_list);
if (bytes)
PG_RETURN_BYTEA_P(bytes);
/*
* If escontext already set, just use that. Anything else is a generic
* JSON parse error.
*/
if (!SOFT_ERROR_OCCURRED(parse_state.escontext))
errsave(parse_state.escontext,
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed pg_dependencies: \"%s\"", str),
errdetail("Must be valid JSON."));
PG_RETURN_NULL();
}
/*
* pg_dependencies_out - output routine for type pg_dependencies.
*/
Datum
pg_dependencies_out(PG_FUNCTION_ARGS)
{
bytea *data = PG_GETARG_BYTEA_PP(0);
MVDependencies *dependencies = statext_dependencies_deserialize(data);
StringInfoData str;
initStringInfo(&str);
appendStringInfoChar(&str, '[');
for (int i = 0; i < dependencies->ndeps; i++)
{
MVDependency *dependency = dependencies->deps[i];
if (i > 0)
appendStringInfoString(&str, ", ");
if (dependency->nattributes <= 1)
elog(ERROR, "invalid zero-length nattributes array in MVDependencies");
appendStringInfo(&str, "{\"" PG_DEPENDENCIES_KEY_ATTRIBUTES "\": [%d",
dependency->attributes[0]);
for (int j = 1; j < dependency->nattributes - 1; j++)
appendStringInfo(&str, ", %d", dependency->attributes[j]);
appendStringInfo(&str, "], \"" PG_DEPENDENCIES_KEY_DEPENDENCY "\": %d, "
"\"" PG_DEPENDENCIES_KEY_DEGREE "\": %f}",
dependency->attributes[dependency->nattributes - 1],
dependency->degree);
}
appendStringInfoChar(&str, ']');
PG_RETURN_CSTRING(str.data);
}
/*
* pg_dependencies_recv - binary input routine for type pg_dependencies.
*/
Datum
pg_dependencies_recv(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot accept a value of type %s", "pg_dependencies")));
PG_RETURN_VOID(); /* keep compiler quiet */
}
/*
* pg_dependencies_send - binary output routine for type pg_dependencies.
*
* Functional dependencies are serialized in a bytea value (although the type
* is named differently), so let's just send that.
*/
Datum
pg_dependencies_send(PG_FUNCTION_ARGS)
{
return byteasend(fcinfo);
}
|