LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pg_dependencies.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 93.3 % 299 279
Test Date: 2026-01-26 10:56:24 Functions: 87.5 % 16 14
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 76.1 % 218 166

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

Generated by: LCOV version 2.3.2-1