LCOV - code coverage report
Current view: top level - src/backend/utils/adt - array_userfuncs.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 86.6 % 954 826
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 29 29
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 49.6 % 494 245

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * array_userfuncs.c
       4                 :             :  *        Misc user-visible array support functions
       5                 :             :  *
       6                 :             :  * Copyright (c) 2003-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  * IDENTIFICATION
       9                 :             :  *        src/backend/utils/adt/array_userfuncs.c
      10                 :             :  *
      11                 :             :  *-------------------------------------------------------------------------
      12                 :             :  */
      13                 :             : #include "postgres.h"
      14                 :             : 
      15                 :             : #include "catalog/pg_operator_d.h"
      16                 :             : #include "catalog/pg_type.h"
      17                 :             : #include "common/int.h"
      18                 :             : #include "common/pg_prng.h"
      19                 :             : #include "libpq/pqformat.h"
      20                 :             : #include "miscadmin.h"
      21                 :             : #include "nodes/supportnodes.h"
      22                 :             : #include "port/pg_bitutils.h"
      23                 :             : #include "utils/array.h"
      24                 :             : #include "utils/builtins.h"
      25                 :             : #include "utils/datum.h"
      26                 :             : #include "utils/lsyscache.h"
      27                 :             : #include "utils/tuplesort.h"
      28                 :             : #include "utils/typcache.h"
      29                 :             : 
      30                 :             : /*
      31                 :             :  * SerialIOData
      32                 :             :  *              Used for caching element-type data in array_agg_serialize
      33                 :             :  */
      34                 :             : typedef struct SerialIOData
      35                 :             : {
      36                 :             :         FmgrInfo        typsend;
      37                 :             : } SerialIOData;
      38                 :             : 
      39                 :             : /*
      40                 :             :  * DeserialIOData
      41                 :             :  *              Used for caching element-type data in array_agg_deserialize
      42                 :             :  */
      43                 :             : typedef struct DeserialIOData
      44                 :             : {
      45                 :             :         FmgrInfo        typreceive;
      46                 :             :         Oid                     typioparam;
      47                 :             : } DeserialIOData;
      48                 :             : 
      49                 :             : /*
      50                 :             :  * ArraySortCachedInfo
      51                 :             :  *              Used for caching catalog data in array_sort
      52                 :             :  */
      53                 :             : typedef struct ArraySortCachedInfo
      54                 :             : {
      55                 :             :         ArrayMetaState array_meta;      /* metadata for array_create_iterator */
      56                 :             :         Oid                     elem_lt_opr;    /* "<" operator for element type */
      57                 :             :         Oid                     elem_gt_opr;    /* ">" operator for element type */
      58                 :             :         Oid                     array_type;             /* pg_type OID of array type */
      59                 :             : } ArraySortCachedInfo;
      60                 :             : 
      61                 :             : static Datum array_position_common(FunctionCallInfo fcinfo);
      62                 :             : 
      63                 :             : 
      64                 :             : /*
      65                 :             :  * fetch_array_arg_replace_nulls
      66                 :             :  *
      67                 :             :  * Fetch an array-valued argument in expanded form; if it's null, construct an
      68                 :             :  * empty array value of the proper data type.  Also cache basic element type
      69                 :             :  * information in fn_extra.
      70                 :             :  *
      71                 :             :  * Caution: if the input is a read/write pointer, this returns the input
      72                 :             :  * argument; so callers must be sure that their changes are "safe", that is
      73                 :             :  * they cannot leave the array in a corrupt state.
      74                 :             :  *
      75                 :             :  * If we're being called as an aggregate function, make sure any newly-made
      76                 :             :  * expanded array is allocated in the aggregate state context, so as to save
      77                 :             :  * copying operations.
      78                 :             :  */
      79                 :             : static ExpandedArrayHeader *
      80                 :         300 : fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
      81                 :             : {
      82                 :         300 :         ExpandedArrayHeader *eah;
      83                 :         300 :         Oid                     element_type;
      84                 :         300 :         ArrayMetaState *my_extra;
      85                 :         300 :         MemoryContext resultcxt;
      86                 :             : 
      87                 :             :         /* If first time through, create datatype cache struct */
      88                 :         300 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
      89         [ +  + ]:         300 :         if (my_extra == NULL)
      90                 :             :         {
      91                 :         184 :                 my_extra = (ArrayMetaState *)
      92                 :         184 :                         MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
      93                 :             :                                                            sizeof(ArrayMetaState));
      94                 :         184 :                 my_extra->element_type = InvalidOid;
      95                 :         184 :                 fcinfo->flinfo->fn_extra = my_extra;
      96                 :         184 :         }
      97                 :             : 
      98                 :             :         /* Figure out which context we want the result in */
      99         [ +  + ]:         300 :         if (!AggCheckCallContext(fcinfo, &resultcxt))
     100                 :         280 :                 resultcxt = CurrentMemoryContext;
     101                 :             : 
     102                 :             :         /* Now collect the array value */
     103         [ +  + ]:         300 :         if (!PG_ARGISNULL(argno))
     104                 :             :         {
     105                 :         296 :                 MemoryContext oldcxt = MemoryContextSwitchTo(resultcxt);
     106                 :             : 
     107                 :         296 :                 eah = PG_GETARG_EXPANDED_ARRAYX(argno, my_extra);
     108                 :         296 :                 MemoryContextSwitchTo(oldcxt);
     109                 :         296 :         }
     110                 :             :         else
     111                 :             :         {
     112                 :             :                 /* We have to look up the array type and element type */
     113                 :           4 :                 Oid                     arr_typeid = get_fn_expr_argtype(fcinfo->flinfo, argno);
     114                 :             : 
     115         [ +  - ]:           4 :                 if (!OidIsValid(arr_typeid))
     116   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     117                 :             :                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     118                 :             :                                          errmsg("could not determine input data type")));
     119                 :           4 :                 element_type = get_element_type(arr_typeid);
     120         [ +  - ]:           4 :                 if (!OidIsValid(element_type))
     121   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     122                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     123                 :             :                                          errmsg("input data type is not an array")));
     124                 :             : 
     125                 :           8 :                 eah = construct_empty_expanded_array(element_type,
     126                 :           4 :                                                                                          resultcxt,
     127                 :           4 :                                                                                          my_extra);
     128                 :           4 :         }
     129                 :             : 
     130                 :         600 :         return eah;
     131                 :         300 : }
     132                 :             : 
     133                 :             : /*-----------------------------------------------------------------------------
     134                 :             :  * array_append :
     135                 :             :  *              push an element onto the end of a one-dimensional array
     136                 :             :  *----------------------------------------------------------------------------
     137                 :             :  */
     138                 :             : Datum
     139                 :         287 : array_append(PG_FUNCTION_ARGS)
     140                 :             : {
     141                 :         287 :         ExpandedArrayHeader *eah;
     142                 :         287 :         Datum           newelem;
     143                 :         287 :         bool            isNull;
     144                 :         287 :         Datum           result;
     145                 :         287 :         int                *dimv,
     146                 :             :                            *lb;
     147                 :         287 :         int                     indx;
     148                 :         287 :         ArrayMetaState *my_extra;
     149                 :             : 
     150                 :         287 :         eah = fetch_array_arg_replace_nulls(fcinfo, 0);
     151                 :         287 :         isNull = PG_ARGISNULL(1);
     152         [ -  + ]:         287 :         if (isNull)
     153                 :           0 :                 newelem = (Datum) 0;
     154                 :             :         else
     155                 :         287 :                 newelem = PG_GETARG_DATUM(1);
     156                 :             : 
     157         [ +  + ]:         287 :         if (eah->ndims == 1)
     158                 :             :         {
     159                 :             :                 /* append newelem */
     160                 :         235 :                 lb = eah->lbound;
     161                 :         235 :                 dimv = eah->dims;
     162                 :             : 
     163                 :             :                 /* index of added elem is at lb[0] + (dimv[0] - 1) + 1 */
     164         [ +  - ]:         235 :                 if (pg_add_s32_overflow(lb[0], dimv[0], &indx))
     165   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     166                 :             :                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     167                 :             :                                          errmsg("integer out of range")));
     168                 :         235 :         }
     169         [ +  - ]:          52 :         else if (eah->ndims == 0)
     170                 :          52 :                 indx = 1;
     171                 :             :         else
     172   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     173                 :             :                                 (errcode(ERRCODE_DATA_EXCEPTION),
     174                 :             :                                  errmsg("argument must be empty or one-dimensional array")));
     175                 :             : 
     176                 :             :         /* Perform element insertion */
     177                 :         287 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
     178                 :             : 
     179                 :         574 :         result = array_set_element(EOHPGetRWDatum(&eah->hdr),
     180                 :         287 :                                                            1, &indx, newelem, isNull,
     181                 :         287 :                                                            -1, my_extra->typlen, my_extra->typbyval, my_extra->typalign);
     182                 :             : 
     183                 :         574 :         PG_RETURN_DATUM(result);
     184                 :         287 : }
     185                 :             : 
     186                 :             : /*
     187                 :             :  * array_append_support()
     188                 :             :  *
     189                 :             :  * Planner support function for array_append()
     190                 :             :  */
     191                 :             : Datum
     192                 :          35 : array_append_support(PG_FUNCTION_ARGS)
     193                 :             : {
     194                 :          35 :         Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
     195                 :          35 :         Node       *ret = NULL;
     196                 :             : 
     197         [ +  - ]:          35 :         if (IsA(rawreq, SupportRequestModifyInPlace))
     198                 :             :         {
     199                 :             :                 /*
     200                 :             :                  * We can optimize in-place appends if the function's array argument
     201                 :             :                  * is the array being assigned to.  We don't need to worry about array
     202                 :             :                  * references within the other argument.
     203                 :             :                  */
     204                 :           0 :                 SupportRequestModifyInPlace *req = (SupportRequestModifyInPlace *) rawreq;
     205                 :           0 :                 Param      *arg = (Param *) linitial(req->args);
     206                 :             : 
     207   [ #  #  #  # ]:           0 :                 if (arg && IsA(arg, Param) &&
     208   [ #  #  #  # ]:           0 :                         arg->paramkind == PARAM_EXTERN &&
     209                 :           0 :                         arg->paramid == req->paramid)
     210                 :           0 :                         ret = (Node *) arg;
     211                 :           0 :         }
     212                 :             : 
     213                 :          70 :         PG_RETURN_POINTER(ret);
     214                 :          35 : }
     215                 :             : 
     216                 :             : /*-----------------------------------------------------------------------------
     217                 :             :  * array_prepend :
     218                 :             :  *              push an element onto the front of a one-dimensional array
     219                 :             :  *----------------------------------------------------------------------------
     220                 :             :  */
     221                 :             : Datum
     222                 :          13 : array_prepend(PG_FUNCTION_ARGS)
     223                 :             : {
     224                 :          13 :         ExpandedArrayHeader *eah;
     225                 :          13 :         Datum           newelem;
     226                 :          13 :         bool            isNull;
     227                 :          13 :         Datum           result;
     228                 :          13 :         int                *lb;
     229                 :          13 :         int                     indx;
     230                 :          13 :         int                     lb0;
     231                 :          13 :         ArrayMetaState *my_extra;
     232                 :             : 
     233                 :          13 :         isNull = PG_ARGISNULL(0);
     234         [ -  + ]:          13 :         if (isNull)
     235                 :           0 :                 newelem = (Datum) 0;
     236                 :             :         else
     237                 :          13 :                 newelem = PG_GETARG_DATUM(0);
     238                 :          13 :         eah = fetch_array_arg_replace_nulls(fcinfo, 1);
     239                 :             : 
     240         [ +  - ]:          13 :         if (eah->ndims == 1)
     241                 :             :         {
     242                 :             :                 /* prepend newelem */
     243                 :          13 :                 lb = eah->lbound;
     244                 :          13 :                 lb0 = lb[0];
     245                 :             : 
     246         [ +  - ]:          13 :                 if (pg_sub_s32_overflow(lb0, 1, &indx))
     247   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     248                 :             :                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     249                 :             :                                          errmsg("integer out of range")));
     250                 :          13 :         }
     251         [ #  # ]:           0 :         else if (eah->ndims == 0)
     252                 :             :         {
     253                 :           0 :                 indx = 1;
     254                 :           0 :                 lb0 = 1;
     255                 :           0 :         }
     256                 :             :         else
     257   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     258                 :             :                                 (errcode(ERRCODE_DATA_EXCEPTION),
     259                 :             :                                  errmsg("argument must be empty or one-dimensional array")));
     260                 :             : 
     261                 :             :         /* Perform element insertion */
     262                 :          13 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
     263                 :             : 
     264                 :          26 :         result = array_set_element(EOHPGetRWDatum(&eah->hdr),
     265                 :          13 :                                                            1, &indx, newelem, isNull,
     266                 :          13 :                                                            -1, my_extra->typlen, my_extra->typbyval, my_extra->typalign);
     267                 :             : 
     268                 :             :         /* Readjust result's LB to match the input's, as expected for prepend */
     269         [ +  - ]:          13 :         Assert(result == EOHPGetRWDatum(&eah->hdr));
     270         [ -  + ]:          13 :         if (eah->ndims == 1)
     271                 :             :         {
     272                 :             :                 /* This is ok whether we've deconstructed or not */
     273                 :          13 :                 eah->lbound[0] = lb0;
     274                 :          13 :         }
     275                 :             : 
     276                 :          26 :         PG_RETURN_DATUM(result);
     277                 :          13 : }
     278                 :             : 
     279                 :             : /*
     280                 :             :  * array_prepend_support()
     281                 :             :  *
     282                 :             :  * Planner support function for array_prepend()
     283                 :             :  */
     284                 :             : Datum
     285                 :           8 : array_prepend_support(PG_FUNCTION_ARGS)
     286                 :             : {
     287                 :           8 :         Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
     288                 :           8 :         Node       *ret = NULL;
     289                 :             : 
     290         [ +  - ]:           8 :         if (IsA(rawreq, SupportRequestModifyInPlace))
     291                 :             :         {
     292                 :             :                 /*
     293                 :             :                  * We can optimize in-place prepends if the function's array argument
     294                 :             :                  * is the array being assigned to.  We don't need to worry about array
     295                 :             :                  * references within the other argument.
     296                 :             :                  */
     297                 :           0 :                 SupportRequestModifyInPlace *req = (SupportRequestModifyInPlace *) rawreq;
     298                 :           0 :                 Param      *arg = (Param *) lsecond(req->args);
     299                 :             : 
     300   [ #  #  #  # ]:           0 :                 if (arg && IsA(arg, Param) &&
     301   [ #  #  #  # ]:           0 :                         arg->paramkind == PARAM_EXTERN &&
     302                 :           0 :                         arg->paramid == req->paramid)
     303                 :           0 :                         ret = (Node *) arg;
     304                 :           0 :         }
     305                 :             : 
     306                 :          16 :         PG_RETURN_POINTER(ret);
     307                 :           8 : }
     308                 :             : 
     309                 :             : /*-----------------------------------------------------------------------------
     310                 :             :  * array_cat :
     311                 :             :  *              concatenate two nD arrays to form an nD array, or
     312                 :             :  *              push an (n-1)D array onto the end of an nD array
     313                 :             :  *----------------------------------------------------------------------------
     314                 :             :  */
     315                 :             : Datum
     316                 :         500 : array_cat(PG_FUNCTION_ARGS)
     317                 :             : {
     318                 :         500 :         ArrayType  *v1,
     319                 :             :                            *v2;
     320                 :         500 :         ArrayType  *result;
     321                 :         500 :         int                *dims,
     322                 :             :                            *lbs,
     323                 :             :                                 ndims,
     324                 :             :                                 nitems,
     325                 :             :                                 ndatabytes,
     326                 :             :                                 nbytes;
     327                 :         500 :         int                *dims1,
     328                 :             :                            *lbs1,
     329                 :             :                                 ndims1,
     330                 :             :                                 nitems1,
     331                 :             :                                 ndatabytes1;
     332                 :         500 :         int                *dims2,
     333                 :             :                            *lbs2,
     334                 :             :                                 ndims2,
     335                 :             :                                 nitems2,
     336                 :             :                                 ndatabytes2;
     337                 :         500 :         int                     i;
     338                 :         500 :         char       *dat1,
     339                 :             :                            *dat2;
     340                 :         500 :         bits8      *bitmap1,
     341                 :             :                            *bitmap2;
     342                 :         500 :         Oid                     element_type;
     343                 :         500 :         Oid                     element_type1;
     344                 :         500 :         Oid                     element_type2;
     345                 :         500 :         int32           dataoffset;
     346                 :             : 
     347                 :             :         /* Concatenating a null array is a no-op, just return the other input */
     348         [ +  + ]:         500 :         if (PG_ARGISNULL(0))
     349                 :             :         {
     350         [ +  - ]:         296 :                 if (PG_ARGISNULL(1))
     351                 :           0 :                         PG_RETURN_NULL();
     352                 :         296 :                 result = PG_GETARG_ARRAYTYPE_P(1);
     353                 :         296 :                 PG_RETURN_ARRAYTYPE_P(result);
     354                 :             :         }
     355         [ -  + ]:         204 :         if (PG_ARGISNULL(1))
     356                 :             :         {
     357                 :           0 :                 result = PG_GETARG_ARRAYTYPE_P(0);
     358                 :           0 :                 PG_RETURN_ARRAYTYPE_P(result);
     359                 :             :         }
     360                 :             : 
     361                 :         204 :         v1 = PG_GETARG_ARRAYTYPE_P(0);
     362                 :         204 :         v2 = PG_GETARG_ARRAYTYPE_P(1);
     363                 :             : 
     364                 :         204 :         element_type1 = ARR_ELEMTYPE(v1);
     365                 :         204 :         element_type2 = ARR_ELEMTYPE(v2);
     366                 :             : 
     367                 :             :         /* Check we have matching element types */
     368         [ +  - ]:         204 :         if (element_type1 != element_type2)
     369   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     370                 :             :                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
     371                 :             :                                  errmsg("cannot concatenate incompatible arrays"),
     372                 :             :                                  errdetail("Arrays with element types %s and %s are not "
     373                 :             :                                                    "compatible for concatenation.",
     374                 :             :                                                    format_type_be(element_type1),
     375                 :             :                                                    format_type_be(element_type2))));
     376                 :             : 
     377                 :             :         /* OK, use it */
     378                 :         204 :         element_type = element_type1;
     379                 :             : 
     380                 :             :         /*----------
     381                 :             :          * We must have one of the following combinations of inputs:
     382                 :             :          * 1) one empty array, and one non-empty array
     383                 :             :          * 2) both arrays empty
     384                 :             :          * 3) two arrays with ndims1 == ndims2
     385                 :             :          * 4) ndims1 == ndims2 - 1
     386                 :             :          * 5) ndims1 == ndims2 + 1
     387                 :             :          *----------
     388                 :             :          */
     389                 :         204 :         ndims1 = ARR_NDIM(v1);
     390                 :         204 :         ndims2 = ARR_NDIM(v2);
     391                 :             : 
     392                 :             :         /*
     393                 :             :          * short circuit - if one input array is empty, and the other is not, we
     394                 :             :          * return the non-empty one as the result
     395                 :             :          *
     396                 :             :          * if both are empty, return the first one
     397                 :             :          */
     398   [ -  +  #  # ]:         204 :         if (ndims1 == 0 && ndims2 > 0)
     399                 :           0 :                 PG_RETURN_ARRAYTYPE_P(v2);
     400                 :             : 
     401         [ +  + ]:         204 :         if (ndims2 == 0)
     402                 :           4 :                 PG_RETURN_ARRAYTYPE_P(v1);
     403                 :             : 
     404                 :             :         /* the rest fall under rule 3, 4, or 5 */
     405         [ +  + ]:         200 :         if (ndims1 != ndims2 &&
     406   [ +  +  +  - ]:           3 :                 ndims1 != ndims2 - 1 &&
     407                 :           2 :                 ndims1 != ndims2 + 1)
     408   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     409                 :             :                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
     410                 :             :                                  errmsg("cannot concatenate incompatible arrays"),
     411                 :             :                                  errdetail("Arrays of %d and %d dimensions are not "
     412                 :             :                                                    "compatible for concatenation.",
     413                 :             :                                                    ndims1, ndims2)));
     414                 :             : 
     415                 :             :         /* get argument array details */
     416                 :         200 :         lbs1 = ARR_LBOUND(v1);
     417                 :         200 :         lbs2 = ARR_LBOUND(v2);
     418                 :         200 :         dims1 = ARR_DIMS(v1);
     419                 :         200 :         dims2 = ARR_DIMS(v2);
     420         [ -  + ]:         200 :         dat1 = ARR_DATA_PTR(v1);
     421         [ -  + ]:         200 :         dat2 = ARR_DATA_PTR(v2);
     422         [ -  + ]:         200 :         bitmap1 = ARR_NULLBITMAP(v1);
     423         [ -  + ]:         200 :         bitmap2 = ARR_NULLBITMAP(v2);
     424                 :         200 :         nitems1 = ArrayGetNItems(ndims1, dims1);
     425                 :         200 :         nitems2 = ArrayGetNItems(ndims2, dims2);
     426         [ -  + ]:         200 :         ndatabytes1 = ARR_SIZE(v1) - ARR_DATA_OFFSET(v1);
     427         [ -  + ]:         200 :         ndatabytes2 = ARR_SIZE(v2) - ARR_DATA_OFFSET(v2);
     428                 :             : 
     429         [ +  + ]:         200 :         if (ndims1 == ndims2)
     430                 :             :         {
     431                 :             :                 /*
     432                 :             :                  * resulting array is made up of the elements (possibly arrays
     433                 :             :                  * themselves) of the input argument arrays
     434                 :             :                  */
     435                 :         197 :                 ndims = ndims1;
     436                 :         197 :                 dims = palloc_array(int, ndims);
     437                 :         197 :                 lbs = palloc_array(int, ndims);
     438                 :             : 
     439                 :         197 :                 dims[0] = dims1[0] + dims2[0];
     440                 :         197 :                 lbs[0] = lbs1[0];
     441                 :             : 
     442         [ +  + ]:         199 :                 for (i = 1; i < ndims; i++)
     443                 :             :                 {
     444         [ +  - ]:           2 :                         if (dims1[i] != dims2[i] || lbs1[i] != lbs2[i])
     445   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     446                 :             :                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
     447                 :             :                                                  errmsg("cannot concatenate incompatible arrays"),
     448                 :             :                                                  errdetail("Arrays with differing element dimensions are "
     449                 :             :                                                                    "not compatible for concatenation.")));
     450                 :             : 
     451                 :           2 :                         dims[i] = dims1[i];
     452                 :           2 :                         lbs[i] = lbs1[i];
     453                 :           2 :                 }
     454                 :         197 :         }
     455         [ +  + ]:           3 :         else if (ndims1 == ndims2 - 1)
     456                 :             :         {
     457                 :             :                 /*
     458                 :             :                  * resulting array has the second argument as the outer array, with
     459                 :             :                  * the first argument inserted at the front of the outer dimension
     460                 :             :                  */
     461                 :           1 :                 ndims = ndims2;
     462                 :           1 :                 dims = palloc_array(int, ndims);
     463                 :           1 :                 lbs = palloc_array(int, ndims);
     464                 :           1 :                 memcpy(dims, dims2, ndims * sizeof(int));
     465                 :           1 :                 memcpy(lbs, lbs2, ndims * sizeof(int));
     466                 :             : 
     467                 :             :                 /* increment number of elements in outer array */
     468                 :           1 :                 dims[0] += 1;
     469                 :             : 
     470                 :             :                 /* make sure the added element matches our existing elements */
     471         [ +  + ]:           2 :                 for (i = 0; i < ndims1; i++)
     472                 :             :                 {
     473         [ +  - ]:           1 :                         if (dims1[i] != dims[i + 1] || lbs1[i] != lbs[i + 1])
     474   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     475                 :             :                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
     476                 :             :                                                  errmsg("cannot concatenate incompatible arrays"),
     477                 :             :                                                  errdetail("Arrays with differing dimensions are not "
     478                 :             :                                                                    "compatible for concatenation.")));
     479                 :           1 :                 }
     480                 :           1 :         }
     481                 :             :         else
     482                 :             :         {
     483                 :             :                 /*
     484                 :             :                  * (ndims1 == ndims2 + 1)
     485                 :             :                  *
     486                 :             :                  * resulting array has the first argument as the outer array, with the
     487                 :             :                  * second argument appended to the end of the outer dimension
     488                 :             :                  */
     489                 :           2 :                 ndims = ndims1;
     490                 :           2 :                 dims = palloc_array(int, ndims);
     491                 :           2 :                 lbs = palloc_array(int, ndims);
     492                 :           2 :                 memcpy(dims, dims1, ndims * sizeof(int));
     493                 :           2 :                 memcpy(lbs, lbs1, ndims * sizeof(int));
     494                 :             : 
     495                 :             :                 /* increment number of elements in outer array */
     496                 :           2 :                 dims[0] += 1;
     497                 :             : 
     498                 :             :                 /* make sure the added element matches our existing elements */
     499         [ +  + ]:           4 :                 for (i = 0; i < ndims2; i++)
     500                 :             :                 {
     501         [ +  - ]:           2 :                         if (dims2[i] != dims[i + 1] || lbs2[i] != lbs[i + 1])
     502   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     503                 :             :                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
     504                 :             :                                                  errmsg("cannot concatenate incompatible arrays"),
     505                 :             :                                                  errdetail("Arrays with differing dimensions are not "
     506                 :             :                                                                    "compatible for concatenation.")));
     507                 :           2 :                 }
     508                 :             :         }
     509                 :             : 
     510                 :             :         /* Do this mainly for overflow checking */
     511                 :         200 :         nitems = ArrayGetNItems(ndims, dims);
     512                 :         200 :         ArrayCheckBounds(ndims, dims, lbs);
     513                 :             : 
     514                 :             :         /* build the result array */
     515                 :         200 :         ndatabytes = ndatabytes1 + ndatabytes2;
     516   [ +  -  +  - ]:         200 :         if (ARR_HASNULL(v1) || ARR_HASNULL(v2))
     517                 :             :         {
     518                 :           0 :                 dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
     519                 :           0 :                 nbytes = ndatabytes + dataoffset;
     520                 :           0 :         }
     521                 :             :         else
     522                 :             :         {
     523                 :         200 :                 dataoffset = 0;                 /* marker for no null bitmap */
     524                 :         200 :                 nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
     525                 :             :         }
     526                 :         200 :         result = (ArrayType *) palloc0(nbytes);
     527                 :         200 :         SET_VARSIZE(result, nbytes);
     528                 :         200 :         result->ndim = ndims;
     529                 :         200 :         result->dataoffset = dataoffset;
     530                 :         200 :         result->elemtype = element_type;
     531                 :         200 :         memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
     532                 :         200 :         memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
     533                 :             :         /* data area is arg1 then arg2 */
     534   [ -  +  -  + ]:         200 :         memcpy(ARR_DATA_PTR(result), dat1, ndatabytes1);
     535   [ -  +  -  + ]:         200 :         memcpy(ARR_DATA_PTR(result) + ndatabytes1, dat2, ndatabytes2);
     536                 :             :         /* handle the null bitmap if needed */
     537         [ +  - ]:         200 :         if (ARR_HASNULL(result))
     538                 :             :         {
     539         [ #  # ]:           0 :                 array_bitmap_copy(ARR_NULLBITMAP(result), 0,
     540                 :           0 :                                                   bitmap1, 0,
     541                 :           0 :                                                   nitems1);
     542         [ #  # ]:           0 :                 array_bitmap_copy(ARR_NULLBITMAP(result), nitems1,
     543                 :           0 :                                                   bitmap2, 0,
     544                 :           0 :                                                   nitems2);
     545                 :           0 :         }
     546                 :             : 
     547                 :         200 :         PG_RETURN_ARRAYTYPE_P(result);
     548                 :         500 : }
     549                 :             : 
     550                 :             : 
     551                 :             : /*
     552                 :             :  * ARRAY_AGG(anynonarray) aggregate function
     553                 :             :  */
     554                 :             : Datum
     555                 :      216563 : array_agg_transfn(PG_FUNCTION_ARGS)
     556                 :             : {
     557                 :      216563 :         Oid                     arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
     558                 :      216563 :         MemoryContext aggcontext;
     559                 :      216563 :         ArrayBuildState *state;
     560                 :      216563 :         Datum           elem;
     561                 :             : 
     562         [ +  - ]:      216563 :         if (arg1_typeid == InvalidOid)
     563   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     564                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     565                 :             :                                  errmsg("could not determine input data type")));
     566                 :             : 
     567                 :             :         /*
     568                 :             :          * Note: we do not need a run-time check about whether arg1_typeid is a
     569                 :             :          * valid array element type, because the parser would have verified that
     570                 :             :          * while resolving the input/result types of this polymorphic aggregate.
     571                 :             :          */
     572                 :             : 
     573         [ +  - ]:      216563 :         if (!AggCheckCallContext(fcinfo, &aggcontext))
     574                 :             :         {
     575                 :             :                 /* cannot be called directly because of internal-type argument */
     576   [ #  #  #  # ]:           0 :                 elog(ERROR, "array_agg_transfn called in non-aggregate context");
     577                 :           0 :         }
     578                 :             : 
     579         [ +  + ]:      216563 :         if (PG_ARGISNULL(0))
     580                 :        4171 :                 state = initArrayResult(arg1_typeid, aggcontext, false);
     581                 :             :         else
     582                 :      212392 :                 state = (ArrayBuildState *) PG_GETARG_POINTER(0);
     583                 :             : 
     584         [ +  + ]:      216563 :         elem = PG_ARGISNULL(1) ? (Datum) 0 : PG_GETARG_DATUM(1);
     585                 :             : 
     586                 :      433126 :         state = accumArrayResult(state,
     587                 :      216563 :                                                          elem,
     588                 :      216563 :                                                          PG_ARGISNULL(1),
     589                 :      216563 :                                                          arg1_typeid,
     590                 :      216563 :                                                          aggcontext);
     591                 :             : 
     592                 :             :         /*
     593                 :             :          * The transition type for array_agg() is declared to be "internal", which
     594                 :             :          * is a pass-by-value type the same size as a pointer.  So we can safely
     595                 :             :          * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
     596                 :             :          */
     597                 :      433126 :         PG_RETURN_POINTER(state);
     598                 :      216563 : }
     599                 :             : 
     600                 :             : Datum
     601                 :          20 : array_agg_combine(PG_FUNCTION_ARGS)
     602                 :             : {
     603                 :          20 :         ArrayBuildState *state1;
     604                 :          20 :         ArrayBuildState *state2;
     605                 :          20 :         MemoryContext agg_context;
     606                 :          20 :         MemoryContext old_context;
     607                 :             : 
     608         [ +  - ]:          20 :         if (!AggCheckCallContext(fcinfo, &agg_context))
     609   [ #  #  #  # ]:           0 :                 elog(ERROR, "aggregate function called in non-aggregate context");
     610                 :             : 
     611         [ +  + ]:          20 :         state1 = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0);
     612         [ -  + ]:          20 :         state2 = PG_ARGISNULL(1) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(1);
     613                 :             : 
     614         [ +  - ]:          20 :         if (state2 == NULL)
     615                 :             :         {
     616                 :             :                 /*
     617                 :             :                  * NULL state2 is easy, just return state1, which we know is already
     618                 :             :                  * in the agg_context
     619                 :             :                  */
     620         [ #  # ]:           0 :                 if (state1 == NULL)
     621                 :           0 :                         PG_RETURN_NULL();
     622                 :           0 :                 PG_RETURN_POINTER(state1);
     623                 :             :         }
     624                 :             : 
     625         [ +  + ]:          20 :         if (state1 == NULL)
     626                 :             :         {
     627                 :             :                 /* We must copy state2's data into the agg_context */
     628                 :          20 :                 state1 = initArrayResultWithSize(state2->element_type, agg_context,
     629                 :          10 :                                                                                  false, state2->alen);
     630                 :             : 
     631                 :          10 :                 old_context = MemoryContextSwitchTo(agg_context);
     632                 :             : 
     633         [ +  + ]:        1818 :                 for (int i = 0; i < state2->nelems; i++)
     634                 :             :                 {
     635         [ +  + ]:        1808 :                         if (!state2->dnulls[i])
     636                 :        2718 :                                 state1->dvalues[i] = datumCopy(state2->dvalues[i],
     637                 :        1359 :                                                                                            state1->typbyval,
     638                 :        1359 :                                                                                            state1->typlen);
     639                 :             :                         else
     640                 :         449 :                                 state1->dvalues[i] = (Datum) 0;
     641                 :        1808 :                 }
     642                 :             : 
     643                 :          10 :                 MemoryContextSwitchTo(old_context);
     644                 :             : 
     645                 :          10 :                 memcpy(state1->dnulls, state2->dnulls, sizeof(bool) * state2->nelems);
     646                 :             : 
     647                 :          10 :                 state1->nelems = state2->nelems;
     648                 :             : 
     649                 :          10 :                 PG_RETURN_POINTER(state1);
     650                 :             :         }
     651         [ -  + ]:          10 :         else if (state2->nelems > 0)
     652                 :             :         {
     653                 :             :                 /* We only need to combine the two states if state2 has any elements */
     654                 :          10 :                 int                     reqsize = state1->nelems + state2->nelems;
     655                 :          10 :                 MemoryContext oldContext = MemoryContextSwitchTo(state1->mcontext);
     656                 :             : 
     657         [ +  - ]:          10 :                 Assert(state1->element_type == state2->element_type);
     658                 :             : 
     659                 :             :                 /* Enlarge state1 arrays if needed */
     660         [ -  + ]:          10 :                 if (state1->alen < reqsize)
     661                 :             :                 {
     662                 :             :                         /* Use a power of 2 size rather than allocating just reqsize */
     663                 :          10 :                         state1->alen = pg_nextpower2_32(reqsize);
     664                 :          20 :                         state1->dvalues = (Datum *) repalloc(state1->dvalues,
     665                 :          10 :                                                                                                  state1->alen * sizeof(Datum));
     666                 :          20 :                         state1->dnulls = (bool *) repalloc(state1->dnulls,
     667                 :          10 :                                                                                            state1->alen * sizeof(bool));
     668                 :          10 :                 }
     669                 :             : 
     670                 :             :                 /* Copy in the state2 elements to the end of the state1 arrays */
     671         [ +  + ]:        3202 :                 for (int i = 0; i < state2->nelems; i++)
     672                 :             :                 {
     673         [ +  + ]:        3192 :                         if (!state2->dnulls[i])
     674                 :        2391 :                                 state1->dvalues[i + state1->nelems] =
     675                 :        4782 :                                         datumCopy(state2->dvalues[i],
     676                 :        2391 :                                                           state1->typbyval,
     677                 :        2391 :                                                           state1->typlen);
     678                 :             :                         else
     679                 :         801 :                                 state1->dvalues[i + state1->nelems] = (Datum) 0;
     680                 :        3192 :                 }
     681                 :             : 
     682                 :          10 :                 memcpy(&state1->dnulls[state1->nelems], state2->dnulls,
     683                 :             :                            sizeof(bool) * state2->nelems);
     684                 :             : 
     685                 :          10 :                 state1->nelems = reqsize;
     686                 :             : 
     687                 :          10 :                 MemoryContextSwitchTo(oldContext);
     688                 :          10 :         }
     689                 :             : 
     690                 :          10 :         PG_RETURN_POINTER(state1);
     691                 :          20 : }
     692                 :             : 
     693                 :             : /*
     694                 :             :  * array_agg_serialize
     695                 :             :  *              Serialize ArrayBuildState into bytea.
     696                 :             :  */
     697                 :             : Datum
     698                 :          20 : array_agg_serialize(PG_FUNCTION_ARGS)
     699                 :             : {
     700                 :          20 :         ArrayBuildState *state;
     701                 :          20 :         StringInfoData buf;
     702                 :          20 :         bytea      *result;
     703                 :             : 
     704                 :             :         /* cannot be called directly because of internal-type argument */
     705         [ +  - ]:          20 :         Assert(AggCheckCallContext(fcinfo, NULL));
     706                 :             : 
     707                 :          20 :         state = (ArrayBuildState *) PG_GETARG_POINTER(0);
     708                 :             : 
     709                 :          20 :         pq_begintypsend(&buf);
     710                 :             : 
     711                 :             :         /*
     712                 :             :          * element_type. Putting this first is more convenient in deserialization
     713                 :             :          */
     714                 :          20 :         pq_sendint32(&buf, state->element_type);
     715                 :             : 
     716                 :             :         /*
     717                 :             :          * nelems -- send first so we know how large to make the dvalues and
     718                 :             :          * dnulls array during deserialization.
     719                 :             :          */
     720                 :          20 :         pq_sendint64(&buf, state->nelems);
     721                 :             : 
     722                 :             :         /* alen can be decided during deserialization */
     723                 :             : 
     724                 :             :         /* typlen */
     725                 :          20 :         pq_sendint16(&buf, state->typlen);
     726                 :             : 
     727                 :             :         /* typbyval */
     728                 :          20 :         pq_sendbyte(&buf, state->typbyval);
     729                 :             : 
     730                 :             :         /* typalign */
     731                 :          20 :         pq_sendbyte(&buf, state->typalign);
     732                 :             : 
     733                 :             :         /* dnulls */
     734                 :          20 :         pq_sendbytes(&buf, state->dnulls, sizeof(bool) * state->nelems);
     735                 :             : 
     736                 :             :         /*
     737                 :             :          * dvalues.  By agreement with array_agg_deserialize, when the element
     738                 :             :          * type is byval, we just transmit the Datum array as-is, including any
     739                 :             :          * null elements.  For by-ref types, we must invoke the element type's
     740                 :             :          * send function, and we skip null elements (which is why the nulls flags
     741                 :             :          * must be sent first).
     742                 :             :          */
     743         [ +  - ]:          20 :         if (state->typbyval)
     744                 :          20 :                 pq_sendbytes(&buf, state->dvalues, sizeof(Datum) * state->nelems);
     745                 :             :         else
     746                 :             :         {
     747                 :           0 :                 SerialIOData *iodata;
     748                 :           0 :                 int                     i;
     749                 :             : 
     750                 :             :                 /* Avoid repeat catalog lookups for typsend function */
     751                 :           0 :                 iodata = (SerialIOData *) fcinfo->flinfo->fn_extra;
     752         [ #  # ]:           0 :                 if (iodata == NULL)
     753                 :             :                 {
     754                 :           0 :                         Oid                     typsend;
     755                 :           0 :                         bool            typisvarlena;
     756                 :             : 
     757                 :           0 :                         iodata = (SerialIOData *)
     758                 :           0 :                                 MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
     759                 :             :                                                                    sizeof(SerialIOData));
     760                 :           0 :                         getTypeBinaryOutputInfo(state->element_type, &typsend,
     761                 :             :                                                                         &typisvarlena);
     762                 :           0 :                         fmgr_info_cxt(typsend, &iodata->typsend,
     763                 :           0 :                                                   fcinfo->flinfo->fn_mcxt);
     764                 :           0 :                         fcinfo->flinfo->fn_extra = iodata;
     765                 :           0 :                 }
     766                 :             : 
     767         [ #  # ]:           0 :                 for (i = 0; i < state->nelems; i++)
     768                 :             :                 {
     769                 :           0 :                         bytea      *outputbytes;
     770                 :             : 
     771         [ #  # ]:           0 :                         if (state->dnulls[i])
     772                 :           0 :                                 continue;
     773                 :           0 :                         outputbytes = SendFunctionCall(&iodata->typsend,
     774                 :           0 :                                                                                    state->dvalues[i]);
     775                 :           0 :                         pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
     776                 :           0 :                         pq_sendbytes(&buf, VARDATA(outputbytes),
     777                 :           0 :                                                  VARSIZE(outputbytes) - VARHDRSZ);
     778      [ #  #  # ]:           0 :                 }
     779                 :           0 :         }
     780                 :             : 
     781                 :          20 :         result = pq_endtypsend(&buf);
     782                 :             : 
     783                 :          40 :         PG_RETURN_BYTEA_P(result);
     784                 :          20 : }
     785                 :             : 
     786                 :             : Datum
     787                 :          20 : array_agg_deserialize(PG_FUNCTION_ARGS)
     788                 :             : {
     789                 :          20 :         bytea      *sstate;
     790                 :          20 :         ArrayBuildState *result;
     791                 :          20 :         StringInfoData buf;
     792                 :          20 :         Oid                     element_type;
     793                 :          20 :         int64           nelems;
     794                 :          20 :         const char *temp;
     795                 :             : 
     796         [ +  - ]:          20 :         if (!AggCheckCallContext(fcinfo, NULL))
     797   [ #  #  #  # ]:           0 :                 elog(ERROR, "aggregate function called in non-aggregate context");
     798                 :             : 
     799                 :          20 :         sstate = PG_GETARG_BYTEA_PP(0);
     800                 :             : 
     801                 :             :         /*
     802                 :             :          * Initialize a StringInfo so that we can "receive" it using the standard
     803                 :             :          * recv-function infrastructure.
     804                 :             :          */
     805                 :          40 :         initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
     806                 :          20 :                                                    VARSIZE_ANY_EXHDR(sstate));
     807                 :             : 
     808                 :             :         /* element_type */
     809                 :          20 :         element_type = pq_getmsgint(&buf, 4);
     810                 :             : 
     811                 :             :         /* nelems */
     812                 :          20 :         nelems = pq_getmsgint64(&buf);
     813                 :             : 
     814                 :             :         /* Create output ArrayBuildState with the needed number of elements */
     815                 :          40 :         result = initArrayResultWithSize(element_type, CurrentMemoryContext,
     816                 :          20 :                                                                          false, nelems);
     817                 :          20 :         result->nelems = nelems;
     818                 :             : 
     819                 :             :         /* typlen */
     820                 :          20 :         result->typlen = pq_getmsgint(&buf, 2);
     821                 :             : 
     822                 :             :         /* typbyval */
     823                 :          20 :         result->typbyval = pq_getmsgbyte(&buf);
     824                 :             : 
     825                 :             :         /* typalign */
     826                 :          20 :         result->typalign = pq_getmsgbyte(&buf);
     827                 :             : 
     828                 :             :         /* dnulls */
     829                 :          20 :         temp = pq_getmsgbytes(&buf, sizeof(bool) * nelems);
     830                 :          20 :         memcpy(result->dnulls, temp, sizeof(bool) * nelems);
     831                 :             : 
     832                 :             :         /* dvalues --- see comment in array_agg_serialize */
     833         [ +  - ]:          20 :         if (result->typbyval)
     834                 :             :         {
     835                 :          20 :                 temp = pq_getmsgbytes(&buf, sizeof(Datum) * nelems);
     836                 :          20 :                 memcpy(result->dvalues, temp, sizeof(Datum) * nelems);
     837                 :          20 :         }
     838                 :             :         else
     839                 :             :         {
     840                 :           0 :                 DeserialIOData *iodata;
     841                 :             : 
     842                 :             :                 /* Avoid repeat catalog lookups for typreceive function */
     843                 :           0 :                 iodata = (DeserialIOData *) fcinfo->flinfo->fn_extra;
     844         [ #  # ]:           0 :                 if (iodata == NULL)
     845                 :             :                 {
     846                 :           0 :                         Oid                     typreceive;
     847                 :             : 
     848                 :           0 :                         iodata = (DeserialIOData *)
     849                 :           0 :                                 MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
     850                 :             :                                                                    sizeof(DeserialIOData));
     851                 :           0 :                         getTypeBinaryInputInfo(element_type, &typreceive,
     852                 :           0 :                                                                    &iodata->typioparam);
     853                 :           0 :                         fmgr_info_cxt(typreceive, &iodata->typreceive,
     854                 :           0 :                                                   fcinfo->flinfo->fn_mcxt);
     855                 :           0 :                         fcinfo->flinfo->fn_extra = iodata;
     856                 :           0 :                 }
     857                 :             : 
     858         [ #  # ]:           0 :                 for (int i = 0; i < nelems; i++)
     859                 :             :                 {
     860                 :           0 :                         int                     itemlen;
     861                 :           0 :                         StringInfoData elem_buf;
     862                 :             : 
     863         [ #  # ]:           0 :                         if (result->dnulls[i])
     864                 :             :                         {
     865                 :           0 :                                 result->dvalues[i] = (Datum) 0;
     866                 :           0 :                                 continue;
     867                 :             :                         }
     868                 :             : 
     869                 :           0 :                         itemlen = pq_getmsgint(&buf, 4);
     870         [ #  # ]:           0 :                         if (itemlen < 0 || itemlen > (buf.len - buf.cursor))
     871   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     872                 :             :                                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
     873                 :             :                                                  errmsg("insufficient data left in message")));
     874                 :             : 
     875                 :             :                         /*
     876                 :             :                          * Rather than copying data around, we just initialize a
     877                 :             :                          * StringInfo pointing to the correct portion of the message
     878                 :             :                          * buffer.
     879                 :             :                          */
     880                 :           0 :                         initReadOnlyStringInfo(&elem_buf, &buf.data[buf.cursor], itemlen);
     881                 :             : 
     882                 :           0 :                         buf.cursor += itemlen;
     883                 :             : 
     884                 :             :                         /* Now call the element's receiveproc */
     885                 :           0 :                         result->dvalues[i] = ReceiveFunctionCall(&iodata->typreceive,
     886                 :             :                                                                                                          &elem_buf,
     887                 :           0 :                                                                                                          iodata->typioparam,
     888                 :             :                                                                                                          -1);
     889      [ #  #  # ]:           0 :                 }
     890                 :           0 :         }
     891                 :             : 
     892                 :          20 :         pq_getmsgend(&buf);
     893                 :             : 
     894                 :          40 :         PG_RETURN_POINTER(result);
     895                 :          20 : }
     896                 :             : 
     897                 :             : Datum
     898                 :        4167 : array_agg_finalfn(PG_FUNCTION_ARGS)
     899                 :             : {
     900                 :        4167 :         Datum           result;
     901                 :        4167 :         ArrayBuildState *state;
     902                 :        4167 :         int                     dims[1];
     903                 :        4167 :         int                     lbs[1];
     904                 :             : 
     905                 :             :         /* cannot be called directly because of internal-type argument */
     906         [ +  - ]:        4167 :         Assert(AggCheckCallContext(fcinfo, NULL));
     907                 :             : 
     908         [ +  + ]:        4167 :         state = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0);
     909                 :             : 
     910         [ +  + ]:        4167 :         if (state == NULL)
     911                 :           5 :                 PG_RETURN_NULL();               /* returns null iff no input values */
     912                 :             : 
     913                 :        4162 :         dims[0] = state->nelems;
     914                 :        4162 :         lbs[0] = 1;
     915                 :             : 
     916                 :             :         /*
     917                 :             :          * Make the result.  We cannot release the ArrayBuildState because
     918                 :             :          * sometimes aggregate final functions are re-executed.  Rather, it is
     919                 :             :          * nodeAgg.c's responsibility to reset the aggcontext when it's safe to do
     920                 :             :          * so.
     921                 :             :          */
     922                 :        8324 :         result = makeMdArrayResult(state, 1, dims, lbs,
     923                 :        4162 :                                                            CurrentMemoryContext,
     924                 :             :                                                            false);
     925                 :             : 
     926                 :        4162 :         PG_RETURN_DATUM(result);
     927                 :        4167 : }
     928                 :             : 
     929                 :             : /*
     930                 :             :  * ARRAY_AGG(anyarray) aggregate function
     931                 :             :  */
     932                 :             : Datum
     933                 :       10037 : array_agg_array_transfn(PG_FUNCTION_ARGS)
     934                 :             : {
     935                 :       10037 :         Oid                     arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
     936                 :       10037 :         MemoryContext aggcontext;
     937                 :       10037 :         ArrayBuildStateArr *state;
     938                 :             : 
     939         [ +  - ]:       10037 :         if (arg1_typeid == InvalidOid)
     940   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     941                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     942                 :             :                                  errmsg("could not determine input data type")));
     943                 :             : 
     944                 :             :         /*
     945                 :             :          * Note: we do not need a run-time check about whether arg1_typeid is a
     946                 :             :          * valid array type, because the parser would have verified that while
     947                 :             :          * resolving the input/result types of this polymorphic aggregate.
     948                 :             :          */
     949                 :             : 
     950         [ +  - ]:       10037 :         if (!AggCheckCallContext(fcinfo, &aggcontext))
     951                 :             :         {
     952                 :             :                 /* cannot be called directly because of internal-type argument */
     953   [ #  #  #  # ]:           0 :                 elog(ERROR, "array_agg_array_transfn called in non-aggregate context");
     954                 :           0 :         }
     955                 :             : 
     956                 :             : 
     957         [ +  + ]:       10037 :         if (PG_ARGISNULL(0))
     958                 :          44 :                 state = initArrayResultArr(arg1_typeid, InvalidOid, aggcontext, false);
     959                 :             :         else
     960                 :        9993 :                 state = (ArrayBuildStateArr *) PG_GETARG_POINTER(0);
     961                 :             : 
     962                 :       20074 :         state = accumArrayResultArr(state,
     963                 :       10037 :                                                                 PG_GETARG_DATUM(1),
     964                 :       10037 :                                                                 PG_ARGISNULL(1),
     965                 :       10037 :                                                                 arg1_typeid,
     966                 :       10037 :                                                                 aggcontext);
     967                 :             : 
     968                 :             :         /*
     969                 :             :          * The transition type for array_agg() is declared to be "internal", which
     970                 :             :          * is a pass-by-value type the same size as a pointer.  So we can safely
     971                 :             :          * pass the ArrayBuildStateArr pointer through nodeAgg.c's machinations.
     972                 :             :          */
     973                 :       20074 :         PG_RETURN_POINTER(state);
     974                 :       10037 : }
     975                 :             : 
     976                 :             : Datum
     977                 :          20 : array_agg_array_combine(PG_FUNCTION_ARGS)
     978                 :             : {
     979                 :          20 :         ArrayBuildStateArr *state1;
     980                 :          20 :         ArrayBuildStateArr *state2;
     981                 :          20 :         MemoryContext agg_context;
     982                 :          20 :         MemoryContext old_context;
     983                 :             : 
     984         [ +  - ]:          20 :         if (!AggCheckCallContext(fcinfo, &agg_context))
     985   [ #  #  #  # ]:           0 :                 elog(ERROR, "aggregate function called in non-aggregate context");
     986                 :             : 
     987         [ +  + ]:          20 :         state1 = PG_ARGISNULL(0) ? NULL : (ArrayBuildStateArr *) PG_GETARG_POINTER(0);
     988         [ -  + ]:          20 :         state2 = PG_ARGISNULL(1) ? NULL : (ArrayBuildStateArr *) PG_GETARG_POINTER(1);
     989                 :             : 
     990         [ +  - ]:          20 :         if (state2 == NULL)
     991                 :             :         {
     992                 :             :                 /*
     993                 :             :                  * NULL state2 is easy, just return state1, which we know is already
     994                 :             :                  * in the agg_context
     995                 :             :                  */
     996         [ #  # ]:           0 :                 if (state1 == NULL)
     997                 :           0 :                         PG_RETURN_NULL();
     998                 :           0 :                 PG_RETURN_POINTER(state1);
     999                 :             :         }
    1000                 :             : 
    1001         [ +  + ]:          20 :         if (state1 == NULL)
    1002                 :             :         {
    1003                 :             :                 /* We must copy state2's data into the agg_context */
    1004                 :          10 :                 old_context = MemoryContextSwitchTo(agg_context);
    1005                 :             : 
    1006                 :          20 :                 state1 = initArrayResultArr(state2->array_type, InvalidOid,
    1007                 :          10 :                                                                         agg_context, false);
    1008                 :             : 
    1009                 :          10 :                 state1->abytes = state2->abytes;
    1010                 :          10 :                 state1->data = (char *) palloc(state1->abytes);
    1011                 :             : 
    1012         [ +  + ]:          10 :                 if (state2->nullbitmap)
    1013                 :             :                 {
    1014                 :           5 :                         int                     size = (state2->aitems + 7) / 8;
    1015                 :             : 
    1016                 :           5 :                         state1->nullbitmap = (bits8 *) palloc(size);
    1017                 :           5 :                         memcpy(state1->nullbitmap, state2->nullbitmap, size);
    1018                 :           5 :                 }
    1019                 :             : 
    1020                 :          10 :                 memcpy(state1->data, state2->data, state2->nbytes);
    1021                 :          10 :                 state1->nbytes = state2->nbytes;
    1022                 :          10 :                 state1->aitems = state2->aitems;
    1023                 :          10 :                 state1->nitems = state2->nitems;
    1024                 :          10 :                 state1->ndims = state2->ndims;
    1025                 :          10 :                 memcpy(state1->dims, state2->dims, sizeof(state2->dims));
    1026                 :          10 :                 memcpy(state1->lbs, state2->lbs, sizeof(state2->lbs));
    1027                 :          10 :                 state1->array_type = state2->array_type;
    1028                 :          10 :                 state1->element_type = state2->element_type;
    1029                 :             : 
    1030                 :          10 :                 MemoryContextSwitchTo(old_context);
    1031                 :             : 
    1032                 :          10 :                 PG_RETURN_POINTER(state1);
    1033                 :             :         }
    1034                 :             : 
    1035                 :             :         /* We only need to combine the two states if state2 has any items */
    1036         [ -  + ]:          10 :         else if (state2->nitems > 0)
    1037                 :             :         {
    1038                 :          10 :                 MemoryContext oldContext;
    1039                 :          10 :                 int                     reqsize = state1->nbytes + state2->nbytes;
    1040                 :          10 :                 int                     i;
    1041                 :             : 
    1042                 :             :                 /*
    1043                 :             :                  * Check the states are compatible with each other.  Ensure we use the
    1044                 :             :                  * same error messages that are listed in accumArrayResultArr so that
    1045                 :             :                  * the same error is shown as would have been if we'd not used the
    1046                 :             :                  * combine function for the aggregation.
    1047                 :             :                  */
    1048         [ +  - ]:          10 :                 if (state1->ndims != state2->ndims)
    1049   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    1050                 :             :                                         (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
    1051                 :             :                                          errmsg("cannot accumulate arrays of different dimensionality")));
    1052                 :             : 
    1053                 :             :                 /* Check dimensions match ignoring the first dimension. */
    1054         [ +  + ]:          20 :                 for (i = 1; i < state1->ndims; i++)
    1055                 :             :                 {
    1056         [ +  - ]:          10 :                         if (state1->dims[i] != state2->dims[i] || state1->lbs[i] != state2->lbs[i])
    1057   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    1058                 :             :                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
    1059                 :             :                                                  errmsg("cannot accumulate arrays of different dimensionality")));
    1060                 :          10 :                 }
    1061                 :             : 
    1062                 :             : 
    1063                 :          10 :                 oldContext = MemoryContextSwitchTo(state1->mcontext);
    1064                 :             : 
    1065                 :             :                 /*
    1066                 :             :                  * If there's not enough space in state1 then we'll need to reallocate
    1067                 :             :                  * more.
    1068                 :             :                  */
    1069         [ +  + ]:          10 :                 if (state1->abytes < reqsize)
    1070                 :             :                 {
    1071                 :             :                         /* use a power of 2 size rather than allocating just reqsize */
    1072                 :           5 :                         state1->abytes = pg_nextpower2_32(reqsize);
    1073                 :           5 :                         state1->data = (char *) repalloc(state1->data, state1->abytes);
    1074                 :           5 :                 }
    1075                 :             : 
    1076         [ +  + ]:          10 :                 if (state2->nullbitmap)
    1077                 :             :                 {
    1078                 :           5 :                         int                     newnitems = state1->nitems + state2->nitems;
    1079                 :             : 
    1080         [ +  - ]:           5 :                         if (state1->nullbitmap == NULL)
    1081                 :             :                         {
    1082                 :             :                                 /*
    1083                 :             :                                  * First input with nulls; we must retrospectively handle any
    1084                 :             :                                  * previous inputs by marking all their items non-null.
    1085                 :             :                                  */
    1086         [ #  # ]:           0 :                                 state1->aitems = pg_nextpower2_32(Max(256, newnitems + 1));
    1087                 :           0 :                                 state1->nullbitmap = (bits8 *) palloc((state1->aitems + 7) / 8);
    1088                 :           0 :                                 array_bitmap_copy(state1->nullbitmap, 0,
    1089                 :             :                                                                   NULL, 0,
    1090                 :           0 :                                                                   state1->nitems);
    1091                 :           0 :                         }
    1092         [ -  + ]:           5 :                         else if (newnitems > state1->aitems)
    1093                 :             :                         {
    1094                 :           5 :                                 int                     newaitems = state1->aitems + state2->aitems;
    1095                 :             : 
    1096                 :           5 :                                 state1->aitems = pg_nextpower2_32(newaitems);
    1097                 :           5 :                                 state1->nullbitmap = (bits8 *)
    1098                 :           5 :                                         repalloc(state1->nullbitmap, (state1->aitems + 7) / 8);
    1099                 :           5 :                         }
    1100                 :          10 :                         array_bitmap_copy(state1->nullbitmap, state1->nitems,
    1101                 :           5 :                                                           state2->nullbitmap, 0,
    1102                 :           5 :                                                           state2->nitems);
    1103                 :           5 :                 }
    1104                 :             : 
    1105                 :          10 :                 memcpy(state1->data + state1->nbytes, state2->data, state2->nbytes);
    1106                 :          10 :                 state1->nbytes += state2->nbytes;
    1107                 :          10 :                 state1->nitems += state2->nitems;
    1108                 :             : 
    1109                 :          10 :                 state1->dims[0] += state2->dims[0];
    1110                 :             :                 /* remaining dims already match, per test above */
    1111                 :             : 
    1112         [ +  - ]:          10 :                 Assert(state1->array_type == state2->array_type);
    1113         [ +  - ]:          10 :                 Assert(state1->element_type == state2->element_type);
    1114                 :             : 
    1115                 :          10 :                 MemoryContextSwitchTo(oldContext);
    1116                 :          10 :         }
    1117                 :             : 
    1118                 :          10 :         PG_RETURN_POINTER(state1);
    1119                 :          20 : }
    1120                 :             : 
    1121                 :             : /*
    1122                 :             :  * array_agg_array_serialize
    1123                 :             :  *              Serialize ArrayBuildStateArr into bytea.
    1124                 :             :  */
    1125                 :             : Datum
    1126                 :          20 : array_agg_array_serialize(PG_FUNCTION_ARGS)
    1127                 :             : {
    1128                 :          20 :         ArrayBuildStateArr *state;
    1129                 :          20 :         StringInfoData buf;
    1130                 :          20 :         bytea      *result;
    1131                 :             : 
    1132                 :             :         /* cannot be called directly because of internal-type argument */
    1133         [ +  - ]:          20 :         Assert(AggCheckCallContext(fcinfo, NULL));
    1134                 :             : 
    1135                 :          20 :         state = (ArrayBuildStateArr *) PG_GETARG_POINTER(0);
    1136                 :             : 
    1137                 :          20 :         pq_begintypsend(&buf);
    1138                 :             : 
    1139                 :             :         /*
    1140                 :             :          * element_type. Putting this first is more convenient in deserialization
    1141                 :             :          * so that we can init the new state sooner.
    1142                 :             :          */
    1143                 :          20 :         pq_sendint32(&buf, state->element_type);
    1144                 :             : 
    1145                 :             :         /* array_type */
    1146                 :          20 :         pq_sendint32(&buf, state->array_type);
    1147                 :             : 
    1148                 :             :         /* nbytes */
    1149                 :          20 :         pq_sendint32(&buf, state->nbytes);
    1150                 :             : 
    1151                 :             :         /* data */
    1152                 :          20 :         pq_sendbytes(&buf, state->data, state->nbytes);
    1153                 :             : 
    1154                 :             :         /* abytes */
    1155                 :          20 :         pq_sendint32(&buf, state->abytes);
    1156                 :             : 
    1157                 :             :         /* aitems */
    1158                 :          20 :         pq_sendint32(&buf, state->aitems);
    1159                 :             : 
    1160                 :             :         /* nullbitmap */
    1161         [ +  + ]:          20 :         if (state->nullbitmap)
    1162                 :             :         {
    1163         [ +  - ]:          10 :                 Assert(state->aitems > 0);
    1164                 :          10 :                 pq_sendbytes(&buf, state->nullbitmap, (state->aitems + 7) / 8);
    1165                 :          10 :         }
    1166                 :             : 
    1167                 :             :         /* nitems */
    1168                 :          20 :         pq_sendint32(&buf, state->nitems);
    1169                 :             : 
    1170                 :             :         /* ndims */
    1171                 :          20 :         pq_sendint32(&buf, state->ndims);
    1172                 :             : 
    1173                 :             :         /* dims: XXX should we just send ndims elements? */
    1174                 :          20 :         pq_sendbytes(&buf, state->dims, sizeof(state->dims));
    1175                 :             : 
    1176                 :             :         /* lbs */
    1177                 :          20 :         pq_sendbytes(&buf, state->lbs, sizeof(state->lbs));
    1178                 :             : 
    1179                 :          20 :         result = pq_endtypsend(&buf);
    1180                 :             : 
    1181                 :          40 :         PG_RETURN_BYTEA_P(result);
    1182                 :          20 : }
    1183                 :             : 
    1184                 :             : Datum
    1185                 :          20 : array_agg_array_deserialize(PG_FUNCTION_ARGS)
    1186                 :             : {
    1187                 :          20 :         bytea      *sstate;
    1188                 :          20 :         ArrayBuildStateArr *result;
    1189                 :          20 :         StringInfoData buf;
    1190                 :          20 :         Oid                     element_type;
    1191                 :          20 :         Oid                     array_type;
    1192                 :          20 :         int                     nbytes;
    1193                 :          20 :         const char *temp;
    1194                 :             : 
    1195                 :             :         /* cannot be called directly because of internal-type argument */
    1196         [ +  - ]:          20 :         Assert(AggCheckCallContext(fcinfo, NULL));
    1197                 :             : 
    1198                 :          20 :         sstate = PG_GETARG_BYTEA_PP(0);
    1199                 :             : 
    1200                 :             :         /*
    1201                 :             :          * Initialize a StringInfo so that we can "receive" it using the standard
    1202                 :             :          * recv-function infrastructure.
    1203                 :             :          */
    1204                 :          40 :         initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
    1205                 :          20 :                                                    VARSIZE_ANY_EXHDR(sstate));
    1206                 :             : 
    1207                 :             :         /* element_type */
    1208                 :          20 :         element_type = pq_getmsgint(&buf, 4);
    1209                 :             : 
    1210                 :             :         /* array_type */
    1211                 :          20 :         array_type = pq_getmsgint(&buf, 4);
    1212                 :             : 
    1213                 :             :         /* nbytes */
    1214                 :          20 :         nbytes = pq_getmsgint(&buf, 4);
    1215                 :             : 
    1216                 :          40 :         result = initArrayResultArr(array_type, element_type,
    1217                 :          20 :                                                                 CurrentMemoryContext, false);
    1218                 :             : 
    1219                 :          20 :         result->abytes = 1024;
    1220         [ +  + ]:          25 :         while (result->abytes < nbytes)
    1221                 :           5 :                 result->abytes *= 2;
    1222                 :             : 
    1223                 :          20 :         result->data = (char *) palloc(result->abytes);
    1224                 :             : 
    1225                 :             :         /* data */
    1226                 :          20 :         temp = pq_getmsgbytes(&buf, nbytes);
    1227                 :          20 :         memcpy(result->data, temp, nbytes);
    1228                 :          20 :         result->nbytes = nbytes;
    1229                 :             : 
    1230                 :             :         /* abytes */
    1231                 :          20 :         result->abytes = pq_getmsgint(&buf, 4);
    1232                 :             : 
    1233                 :             :         /* aitems: might be 0 */
    1234                 :          20 :         result->aitems = pq_getmsgint(&buf, 4);
    1235                 :             : 
    1236                 :             :         /* nullbitmap */
    1237         [ +  + ]:          20 :         if (result->aitems > 0)
    1238                 :             :         {
    1239                 :          10 :                 int                     size = (result->aitems + 7) / 8;
    1240                 :             : 
    1241                 :          10 :                 result->nullbitmap = (bits8 *) palloc(size);
    1242                 :          10 :                 temp = pq_getmsgbytes(&buf, size);
    1243                 :          10 :                 memcpy(result->nullbitmap, temp, size);
    1244                 :          10 :         }
    1245                 :             :         else
    1246                 :          10 :                 result->nullbitmap = NULL;
    1247                 :             : 
    1248                 :             :         /* nitems */
    1249                 :          20 :         result->nitems = pq_getmsgint(&buf, 4);
    1250                 :             : 
    1251                 :             :         /* ndims */
    1252                 :          20 :         result->ndims = pq_getmsgint(&buf, 4);
    1253                 :             : 
    1254                 :             :         /* dims */
    1255                 :          20 :         temp = pq_getmsgbytes(&buf, sizeof(result->dims));
    1256                 :          20 :         memcpy(result->dims, temp, sizeof(result->dims));
    1257                 :             : 
    1258                 :             :         /* lbs */
    1259                 :          20 :         temp = pq_getmsgbytes(&buf, sizeof(result->lbs));
    1260                 :          20 :         memcpy(result->lbs, temp, sizeof(result->lbs));
    1261                 :             : 
    1262                 :          20 :         pq_getmsgend(&buf);
    1263                 :             : 
    1264                 :          40 :         PG_RETURN_POINTER(result);
    1265                 :          20 : }
    1266                 :             : 
    1267                 :             : Datum
    1268                 :          31 : array_agg_array_finalfn(PG_FUNCTION_ARGS)
    1269                 :             : {
    1270                 :          31 :         Datum           result;
    1271                 :          31 :         ArrayBuildStateArr *state;
    1272                 :             : 
    1273                 :             :         /* cannot be called directly because of internal-type argument */
    1274         [ +  - ]:          31 :         Assert(AggCheckCallContext(fcinfo, NULL));
    1275                 :             : 
    1276         [ -  + ]:          31 :         state = PG_ARGISNULL(0) ? NULL : (ArrayBuildStateArr *) PG_GETARG_POINTER(0);
    1277                 :             : 
    1278         [ +  - ]:          31 :         if (state == NULL)
    1279                 :           0 :                 PG_RETURN_NULL();               /* returns null iff no input values */
    1280                 :             : 
    1281                 :             :         /*
    1282                 :             :          * Make the result.  We cannot release the ArrayBuildStateArr because
    1283                 :             :          * sometimes aggregate final functions are re-executed.  Rather, it is
    1284                 :             :          * nodeAgg.c's responsibility to reset the aggcontext when it's safe to do
    1285                 :             :          * so.
    1286                 :             :          */
    1287                 :          31 :         result = makeArrayResultArr(state, CurrentMemoryContext, false);
    1288                 :             : 
    1289                 :          31 :         PG_RETURN_DATUM(result);
    1290                 :          31 : }
    1291                 :             : 
    1292                 :             : /*-----------------------------------------------------------------------------
    1293                 :             :  * array_position, array_position_start :
    1294                 :             :  *                      return the offset of a value in an array.
    1295                 :             :  *
    1296                 :             :  * IS NOT DISTINCT FROM semantics are used for comparisons.  Return NULL when
    1297                 :             :  * the value is not found.
    1298                 :             :  *-----------------------------------------------------------------------------
    1299                 :             :  */
    1300                 :             : Datum
    1301                 :          12 : array_position(PG_FUNCTION_ARGS)
    1302                 :             : {
    1303                 :          12 :         return array_position_common(fcinfo);
    1304                 :             : }
    1305                 :             : 
    1306                 :             : Datum
    1307                 :           3 : array_position_start(PG_FUNCTION_ARGS)
    1308                 :             : {
    1309                 :           3 :         return array_position_common(fcinfo);
    1310                 :             : }
    1311                 :             : 
    1312                 :             : /*
    1313                 :             :  * array_position_common
    1314                 :             :  *              Common code for array_position and array_position_start
    1315                 :             :  *
    1316                 :             :  * These are separate wrappers for the sake of opr_sanity regression test.
    1317                 :             :  * They are not strict so we have to test for null inputs explicitly.
    1318                 :             :  */
    1319                 :             : static Datum
    1320                 :          15 : array_position_common(FunctionCallInfo fcinfo)
    1321                 :             : {
    1322                 :          15 :         ArrayType  *array;
    1323                 :          15 :         Oid                     collation = PG_GET_COLLATION();
    1324                 :          15 :         Oid                     element_type;
    1325                 :          15 :         Datum           searched_element,
    1326                 :             :                                 value;
    1327                 :          15 :         bool            isnull;
    1328                 :          15 :         int                     position,
    1329                 :             :                                 position_min;
    1330                 :          15 :         bool            found = false;
    1331                 :          15 :         TypeCacheEntry *typentry;
    1332                 :          15 :         ArrayMetaState *my_extra;
    1333                 :          15 :         bool            null_search;
    1334                 :          15 :         ArrayIterator array_iterator;
    1335                 :             : 
    1336         [ +  - ]:          15 :         if (PG_ARGISNULL(0))
    1337                 :           0 :                 PG_RETURN_NULL();
    1338                 :             : 
    1339                 :          15 :         array = PG_GETARG_ARRAYTYPE_P(0);
    1340                 :             : 
    1341                 :             :         /*
    1342                 :             :          * We refuse to search for elements in multi-dimensional arrays, since we
    1343                 :             :          * have no good way to report the element's location in the array.
    1344                 :             :          */
    1345         [ +  + ]:          15 :         if (ARR_NDIM(array) > 1)
    1346   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1347                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1348                 :             :                                  errmsg("searching for elements in multidimensional arrays is not supported")));
    1349                 :             : 
    1350                 :             :         /* Searching in an empty array is well-defined, though: it always fails */
    1351         [ +  - ]:          14 :         if (ARR_NDIM(array) < 1)
    1352                 :           0 :                 PG_RETURN_NULL();
    1353                 :             : 
    1354         [ +  + ]:          14 :         if (PG_ARGISNULL(1))
    1355                 :             :         {
    1356                 :             :                 /* fast return when the array doesn't have nulls */
    1357         [ +  + ]:           2 :                 if (!array_contains_nulls(array))
    1358                 :           1 :                         PG_RETURN_NULL();
    1359                 :           1 :                 searched_element = (Datum) 0;
    1360                 :           1 :                 null_search = true;
    1361                 :           1 :         }
    1362                 :             :         else
    1363                 :             :         {
    1364                 :          12 :                 searched_element = PG_GETARG_DATUM(1);
    1365                 :          12 :                 null_search = false;
    1366                 :             :         }
    1367                 :             : 
    1368                 :          13 :         element_type = ARR_ELEMTYPE(array);
    1369                 :          13 :         position = (ARR_LBOUND(array))[0] - 1;
    1370                 :             : 
    1371                 :             :         /* figure out where to start */
    1372         [ +  + ]:          13 :         if (PG_NARGS() == 3)
    1373                 :             :         {
    1374         [ +  - ]:           3 :                 if (PG_ARGISNULL(2))
    1375   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    1376                 :             :                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
    1377                 :             :                                          errmsg("initial position must not be null")));
    1378                 :             : 
    1379                 :           3 :                 position_min = PG_GETARG_INT32(2);
    1380                 :           3 :         }
    1381                 :             :         else
    1382                 :          10 :                 position_min = (ARR_LBOUND(array))[0];
    1383                 :             : 
    1384                 :             :         /*
    1385                 :             :          * We arrange to look up type info for array_create_iterator only once per
    1386                 :             :          * series of calls, assuming the element type doesn't change underneath
    1387                 :             :          * us.
    1388                 :             :          */
    1389                 :          13 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    1390         [ +  + ]:          13 :         if (my_extra == NULL)
    1391                 :             :         {
    1392                 :          10 :                 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    1393                 :             :                                                                                                           sizeof(ArrayMetaState));
    1394                 :          10 :                 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    1395                 :          10 :                 my_extra->element_type = ~element_type;
    1396                 :          10 :         }
    1397                 :             : 
    1398         [ +  + ]:          13 :         if (my_extra->element_type != element_type)
    1399                 :             :         {
    1400                 :          20 :                 get_typlenbyvalalign(element_type,
    1401                 :          10 :                                                          &my_extra->typlen,
    1402                 :          10 :                                                          &my_extra->typbyval,
    1403                 :          10 :                                                          &my_extra->typalign);
    1404                 :             : 
    1405                 :          10 :                 typentry = lookup_type_cache(element_type, TYPECACHE_EQ_OPR_FINFO);
    1406                 :             : 
    1407         [ +  - ]:          10 :                 if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
    1408   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    1409                 :             :                                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
    1410                 :             :                                          errmsg("could not identify an equality operator for type %s",
    1411                 :             :                                                         format_type_be(element_type))));
    1412                 :             : 
    1413                 :          10 :                 my_extra->element_type = element_type;
    1414                 :          20 :                 fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
    1415                 :          10 :                                           fcinfo->flinfo->fn_mcxt);
    1416                 :          10 :         }
    1417                 :             : 
    1418                 :             :         /* Examine each array element until we find a match. */
    1419                 :          13 :         array_iterator = array_create_iterator(array, 0, my_extra);
    1420         [ +  + ]:          55 :         while (array_iterate(array_iterator, &value, &isnull))
    1421                 :             :         {
    1422                 :          54 :                 position++;
    1423                 :             : 
    1424                 :             :                 /* skip initial elements if caller requested so */
    1425         [ +  + ]:          54 :                 if (position < position_min)
    1426                 :          13 :                         continue;
    1427                 :             : 
    1428                 :             :                 /*
    1429                 :             :                  * Can't look at the array element's value if it's null; but if we
    1430                 :             :                  * search for null, we have a hit and are done.
    1431                 :             :                  */
    1432   [ +  +  +  + ]:          41 :                 if (isnull || null_search)
    1433                 :             :                 {
    1434   [ +  +  +  + ]:           7 :                         if (isnull && null_search)
    1435                 :             :                         {
    1436                 :           1 :                                 found = true;
    1437                 :           1 :                                 break;
    1438                 :             :                         }
    1439                 :             :                         else
    1440                 :           6 :                                 continue;
    1441                 :             :                 }
    1442                 :             : 
    1443                 :             :                 /* not nulls, so run the operator */
    1444   [ +  +  +  + ]:          68 :                 if (DatumGetBool(FunctionCall2Coll(&my_extra->proc, collation,
    1445                 :          34 :                                                                                    searched_element, value)))
    1446                 :             :                 {
    1447                 :          11 :                         found = true;
    1448                 :          11 :                         break;
    1449                 :             :                 }
    1450                 :             :         }
    1451                 :             : 
    1452                 :          13 :         array_free_iterator(array_iterator);
    1453                 :             : 
    1454                 :             :         /* Avoid leaking memory when handed toasted input */
    1455         [ +  + ]:          13 :         PG_FREE_IF_COPY(array, 0);
    1456                 :             : 
    1457         [ +  + ]:          13 :         if (!found)
    1458                 :           1 :                 PG_RETURN_NULL();
    1459                 :             : 
    1460                 :          12 :         PG_RETURN_INT32(position);
    1461                 :          14 : }
    1462                 :             : 
    1463                 :             : /*-----------------------------------------------------------------------------
    1464                 :             :  * array_positions :
    1465                 :             :  *                      return an array of positions of a value in an array.
    1466                 :             :  *
    1467                 :             :  * IS NOT DISTINCT FROM semantics are used for comparisons.  Returns NULL when
    1468                 :             :  * the input array is NULL.  When the value is not found in the array, returns
    1469                 :             :  * an empty array.
    1470                 :             :  *
    1471                 :             :  * This is not strict so we have to test for null inputs explicitly.
    1472                 :             :  *-----------------------------------------------------------------------------
    1473                 :             :  */
    1474                 :             : Datum
    1475                 :          10 : array_positions(PG_FUNCTION_ARGS)
    1476                 :             : {
    1477                 :          10 :         ArrayType  *array;
    1478                 :          10 :         Oid                     collation = PG_GET_COLLATION();
    1479                 :          10 :         Oid                     element_type;
    1480                 :          10 :         Datum           searched_element,
    1481                 :             :                                 value;
    1482                 :          10 :         bool            isnull;
    1483                 :          10 :         int                     position;
    1484                 :          10 :         TypeCacheEntry *typentry;
    1485                 :          10 :         ArrayMetaState *my_extra;
    1486                 :          10 :         bool            null_search;
    1487                 :          10 :         ArrayIterator array_iterator;
    1488                 :          10 :         ArrayBuildState *astate = NULL;
    1489                 :             : 
    1490         [ +  + ]:          10 :         if (PG_ARGISNULL(0))
    1491                 :           2 :                 PG_RETURN_NULL();
    1492                 :             : 
    1493                 :           8 :         array = PG_GETARG_ARRAYTYPE_P(0);
    1494                 :             : 
    1495                 :             :         /*
    1496                 :             :          * We refuse to search for elements in multi-dimensional arrays, since we
    1497                 :             :          * have no good way to report the element's location in the array.
    1498                 :             :          */
    1499         [ +  + ]:           8 :         if (ARR_NDIM(array) > 1)
    1500   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1501                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1502                 :             :                                  errmsg("searching for elements in multidimensional arrays is not supported")));
    1503                 :             : 
    1504                 :           7 :         astate = initArrayResult(INT4OID, CurrentMemoryContext, false);
    1505                 :             : 
    1506                 :             :         /* Searching in an empty array is well-defined, though: it always fails */
    1507         [ +  - ]:           7 :         if (ARR_NDIM(array) < 1)
    1508                 :           0 :                 PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
    1509                 :             : 
    1510         [ +  + ]:           7 :         if (PG_ARGISNULL(1))
    1511                 :             :         {
    1512                 :             :                 /* fast return when the array doesn't have nulls */
    1513         [ +  + ]:           2 :                 if (!array_contains_nulls(array))
    1514                 :           1 :                         PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
    1515                 :           1 :                 searched_element = (Datum) 0;
    1516                 :           1 :                 null_search = true;
    1517                 :           1 :         }
    1518                 :             :         else
    1519                 :             :         {
    1520                 :           5 :                 searched_element = PG_GETARG_DATUM(1);
    1521                 :           5 :                 null_search = false;
    1522                 :             :         }
    1523                 :             : 
    1524                 :           6 :         element_type = ARR_ELEMTYPE(array);
    1525                 :           6 :         position = (ARR_LBOUND(array))[0] - 1;
    1526                 :             : 
    1527                 :             :         /*
    1528                 :             :          * We arrange to look up type info for array_create_iterator only once per
    1529                 :             :          * series of calls, assuming the element type doesn't change underneath
    1530                 :             :          * us.
    1531                 :             :          */
    1532                 :           6 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    1533         [ +  + ]:           6 :         if (my_extra == NULL)
    1534                 :             :         {
    1535                 :           5 :                 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    1536                 :             :                                                                                                           sizeof(ArrayMetaState));
    1537                 :           5 :                 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    1538                 :           5 :                 my_extra->element_type = ~element_type;
    1539                 :           5 :         }
    1540                 :             : 
    1541         [ +  + ]:           6 :         if (my_extra->element_type != element_type)
    1542                 :             :         {
    1543                 :          10 :                 get_typlenbyvalalign(element_type,
    1544                 :           5 :                                                          &my_extra->typlen,
    1545                 :           5 :                                                          &my_extra->typbyval,
    1546                 :           5 :                                                          &my_extra->typalign);
    1547                 :             : 
    1548                 :           5 :                 typentry = lookup_type_cache(element_type, TYPECACHE_EQ_OPR_FINFO);
    1549                 :             : 
    1550         [ +  - ]:           5 :                 if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
    1551   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    1552                 :             :                                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
    1553                 :             :                                          errmsg("could not identify an equality operator for type %s",
    1554                 :             :                                                         format_type_be(element_type))));
    1555                 :             : 
    1556                 :           5 :                 my_extra->element_type = element_type;
    1557                 :          10 :                 fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
    1558                 :           5 :                                           fcinfo->flinfo->fn_mcxt);
    1559                 :           5 :         }
    1560                 :             : 
    1561                 :             :         /*
    1562                 :             :          * Accumulate each array position iff the element matches the given
    1563                 :             :          * element.
    1564                 :             :          */
    1565                 :           6 :         array_iterator = array_create_iterator(array, 0, my_extra);
    1566         [ +  + ]:         136 :         while (array_iterate(array_iterator, &value, &isnull))
    1567                 :             :         {
    1568                 :         130 :                 position += 1;
    1569                 :             : 
    1570                 :             :                 /*
    1571                 :             :                  * Can't look at the array element's value if it's null; but if we
    1572                 :             :                  * search for null, we have a hit.
    1573                 :             :                  */
    1574   [ +  +  +  + ]:         130 :                 if (isnull || null_search)
    1575                 :             :                 {
    1576   [ +  +  -  + ]:          12 :                         if (isnull && null_search)
    1577                 :           2 :                                 astate =
    1578                 :           4 :                                         accumArrayResult(astate, Int32GetDatum(position), false,
    1579                 :           2 :                                                                          INT4OID, CurrentMemoryContext);
    1580                 :             : 
    1581                 :          12 :                         continue;
    1582                 :             :                 }
    1583                 :             : 
    1584                 :             :                 /* not nulls, so run the operator */
    1585   [ +  +  +  + ]:         236 :                 if (DatumGetBool(FunctionCall2Coll(&my_extra->proc, collation,
    1586                 :         118 :                                                                                    searched_element, value)))
    1587                 :          15 :                         astate =
    1588                 :          30 :                                 accumArrayResult(astate, Int32GetDatum(position), false,
    1589                 :          15 :                                                                  INT4OID, CurrentMemoryContext);
    1590                 :             :         }
    1591                 :             : 
    1592                 :           6 :         array_free_iterator(array_iterator);
    1593                 :             : 
    1594                 :             :         /* Avoid leaking memory when handed toasted input */
    1595         [ +  - ]:           6 :         PG_FREE_IF_COPY(array, 0);
    1596                 :             : 
    1597                 :           6 :         PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
    1598                 :           9 : }
    1599                 :             : 
    1600                 :             : /*
    1601                 :             :  * array_shuffle_n
    1602                 :             :  *              Return a copy of array with n randomly chosen items.
    1603                 :             :  *
    1604                 :             :  * The number of items must not exceed the size of the first dimension of the
    1605                 :             :  * array.  We preserve the first dimension's lower bound if keep_lb,
    1606                 :             :  * else it's set to 1.  Lower-order dimensions are preserved in any case.
    1607                 :             :  *
    1608                 :             :  * NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
    1609                 :             :  * from the system catalogs, given only the elmtyp. However, the caller is
    1610                 :             :  * in a better position to cache this info across multiple calls.
    1611                 :             :  */
    1612                 :             : static ArrayType *
    1613                 :           8 : array_shuffle_n(ArrayType *array, int n, bool keep_lb,
    1614                 :             :                                 Oid elmtyp, TypeCacheEntry *typentry)
    1615                 :             : {
    1616                 :           8 :         ArrayType  *result;
    1617                 :           8 :         int                     ndim,
    1618                 :             :                            *dims,
    1619                 :             :                            *lbs,
    1620                 :             :                                 nelm,
    1621                 :             :                                 nitem,
    1622                 :             :                                 rdims[MAXDIM],
    1623                 :             :                                 rlbs[MAXDIM];
    1624                 :           8 :         int16           elmlen;
    1625                 :           8 :         bool            elmbyval;
    1626                 :           8 :         char            elmalign;
    1627                 :           8 :         Datum      *elms,
    1628                 :             :                            *ielms;
    1629                 :           8 :         bool       *nuls,
    1630                 :             :                            *inuls;
    1631                 :             : 
    1632                 :           8 :         ndim = ARR_NDIM(array);
    1633                 :           8 :         dims = ARR_DIMS(array);
    1634                 :           8 :         lbs = ARR_LBOUND(array);
    1635                 :             : 
    1636                 :           8 :         elmlen = typentry->typlen;
    1637                 :           8 :         elmbyval = typentry->typbyval;
    1638                 :           8 :         elmalign = typentry->typalign;
    1639                 :             : 
    1640                 :             :         /* If the target array is empty, exit fast */
    1641   [ +  -  +  -  :           8 :         if (ndim < 1 || dims[0] < 1 || n < 1)
                   -  + ]
    1642                 :           0 :                 return construct_empty_array(elmtyp);
    1643                 :             : 
    1644                 :           8 :         deconstruct_array(array, elmtyp, elmlen, elmbyval, elmalign,
    1645                 :             :                                           &elms, &nuls, &nelm);
    1646                 :             : 
    1647                 :           8 :         nitem = dims[0];                        /* total number of items */
    1648                 :           8 :         nelm /= nitem;                          /* number of elements per item */
    1649                 :             : 
    1650         [ +  - ]:           8 :         Assert(n <= nitem);                  /* else it's caller error */
    1651                 :             : 
    1652                 :             :         /*
    1653                 :             :          * Shuffle array using Fisher-Yates algorithm.  Scan the array and swap
    1654                 :             :          * current item (nelm datums starting at ielms) with a randomly chosen
    1655                 :             :          * later item (nelm datums starting at jelms) in each iteration.  We can
    1656                 :             :          * stop once we've done n iterations; then first n items are the result.
    1657                 :             :          */
    1658                 :           8 :         ielms = elms;
    1659                 :           8 :         inuls = nuls;
    1660         [ +  + ]:          38 :         for (int i = 0; i < n; i++)
    1661                 :             :         {
    1662                 :          30 :                 int                     j = (int) pg_prng_uint64_range(&pg_global_prng_state, i, nitem - 1) * nelm;
    1663                 :          30 :                 Datum      *jelms = elms + j;
    1664                 :          30 :                 bool       *jnuls = nuls + j;
    1665                 :             : 
    1666                 :             :                 /* Swap i'th and j'th items; advance ielms/inuls to next item */
    1667         [ +  + ]:          82 :                 for (int k = 0; k < nelm; k++)
    1668                 :             :                 {
    1669                 :          52 :                         Datum           elm = *ielms;
    1670                 :          52 :                         bool            nul = *inuls;
    1671                 :             : 
    1672                 :          52 :                         *ielms++ = *jelms;
    1673                 :          52 :                         *inuls++ = *jnuls;
    1674                 :          52 :                         *jelms++ = elm;
    1675                 :          52 :                         *jnuls++ = nul;
    1676                 :          52 :                 }
    1677                 :          30 :         }
    1678                 :             : 
    1679                 :             :         /* Set up dimensions of the result */
    1680                 :           8 :         memcpy(rdims, dims, ndim * sizeof(int));
    1681                 :           8 :         memcpy(rlbs, lbs, ndim * sizeof(int));
    1682                 :           8 :         rdims[0] = n;
    1683         [ +  + ]:           8 :         if (!keep_lb)
    1684                 :           4 :                 rlbs[0] = 1;
    1685                 :             : 
    1686                 :          16 :         result = construct_md_array(elms, nuls, ndim, rdims, rlbs,
    1687                 :           8 :                                                                 elmtyp, elmlen, elmbyval, elmalign);
    1688                 :             : 
    1689                 :           8 :         pfree(elms);
    1690                 :           8 :         pfree(nuls);
    1691                 :             : 
    1692                 :           8 :         return result;
    1693                 :           8 : }
    1694                 :             : 
    1695                 :             : /*
    1696                 :             :  * array_shuffle
    1697                 :             :  *
    1698                 :             :  * Returns an array with the same dimensions as the input array, with its
    1699                 :             :  * first-dimension elements in random order.
    1700                 :             :  */
    1701                 :             : Datum
    1702                 :           4 : array_shuffle(PG_FUNCTION_ARGS)
    1703                 :             : {
    1704                 :           4 :         ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
    1705                 :           4 :         ArrayType  *result;
    1706                 :           4 :         Oid                     elmtyp;
    1707                 :           4 :         TypeCacheEntry *typentry;
    1708                 :             : 
    1709                 :             :         /*
    1710                 :             :          * There is no point in shuffling empty arrays or arrays with less than
    1711                 :             :          * two items.
    1712                 :             :          */
    1713   [ +  -  -  + ]:           4 :         if (ARR_NDIM(array) < 1 || ARR_DIMS(array)[0] < 2)
    1714                 :           0 :                 PG_RETURN_ARRAYTYPE_P(array);
    1715                 :             : 
    1716                 :           4 :         elmtyp = ARR_ELEMTYPE(array);
    1717                 :           4 :         typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
    1718   [ -  +  #  # ]:           4 :         if (typentry == NULL || typentry->type_id != elmtyp)
    1719                 :             :         {
    1720                 :           4 :                 typentry = lookup_type_cache(elmtyp, 0);
    1721                 :           4 :                 fcinfo->flinfo->fn_extra = typentry;
    1722                 :           4 :         }
    1723                 :             : 
    1724                 :           4 :         result = array_shuffle_n(array, ARR_DIMS(array)[0], true, elmtyp, typentry);
    1725                 :             : 
    1726                 :           4 :         PG_RETURN_ARRAYTYPE_P(result);
    1727                 :           4 : }
    1728                 :             : 
    1729                 :             : /*
    1730                 :             :  * array_sample
    1731                 :             :  *
    1732                 :             :  * Returns an array of n randomly chosen first-dimension elements
    1733                 :             :  * from the input array.
    1734                 :             :  */
    1735                 :             : Datum
    1736                 :           6 : array_sample(PG_FUNCTION_ARGS)
    1737                 :             : {
    1738                 :           6 :         ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
    1739                 :           6 :         int                     n = PG_GETARG_INT32(1);
    1740                 :           6 :         ArrayType  *result;
    1741                 :           6 :         Oid                     elmtyp;
    1742                 :           6 :         TypeCacheEntry *typentry;
    1743                 :           6 :         int                     nitem;
    1744                 :             : 
    1745         [ +  - ]:           6 :         nitem = (ARR_NDIM(array) < 1) ? 0 : ARR_DIMS(array)[0];
    1746                 :             : 
    1747         [ +  + ]:           6 :         if (n < 0 || n > nitem)
    1748   [ +  -  +  - ]:           2 :                 ereport(ERROR,
    1749                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1750                 :             :                                  errmsg("sample size must be between 0 and %d", nitem)));
    1751                 :             : 
    1752                 :           4 :         elmtyp = ARR_ELEMTYPE(array);
    1753                 :           4 :         typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
    1754   [ -  +  #  # ]:           4 :         if (typentry == NULL || typentry->type_id != elmtyp)
    1755                 :             :         {
    1756                 :           4 :                 typentry = lookup_type_cache(elmtyp, 0);
    1757                 :           4 :                 fcinfo->flinfo->fn_extra = typentry;
    1758                 :           4 :         }
    1759                 :             : 
    1760                 :           4 :         result = array_shuffle_n(array, n, false, elmtyp, typentry);
    1761                 :             : 
    1762                 :           8 :         PG_RETURN_ARRAYTYPE_P(result);
    1763                 :           4 : }
    1764                 :             : 
    1765                 :             : 
    1766                 :             : /*
    1767                 :             :  * array_reverse_n
    1768                 :             :  *              Return a copy of array with reversed items.
    1769                 :             :  *
    1770                 :             :  * NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
    1771                 :             :  * from the system catalogs, given only the elmtyp. However, the caller is
    1772                 :             :  * in a better position to cache this info across multiple calls.
    1773                 :             :  */
    1774                 :             : static ArrayType *
    1775                 :           3 : array_reverse_n(ArrayType *array, Oid elmtyp, TypeCacheEntry *typentry)
    1776                 :             : {
    1777                 :           3 :         ArrayType  *result;
    1778                 :           3 :         int                     ndim,
    1779                 :             :                            *dims,
    1780                 :             :                            *lbs,
    1781                 :             :                                 nelm,
    1782                 :             :                                 nitem,
    1783                 :             :                                 rdims[MAXDIM],
    1784                 :             :                                 rlbs[MAXDIM];
    1785                 :           3 :         int16           elmlen;
    1786                 :           3 :         bool            elmbyval;
    1787                 :           3 :         char            elmalign;
    1788                 :           3 :         Datum      *elms,
    1789                 :             :                            *ielms;
    1790                 :           3 :         bool       *nuls,
    1791                 :             :                            *inuls;
    1792                 :             : 
    1793                 :           3 :         ndim = ARR_NDIM(array);
    1794                 :           3 :         dims = ARR_DIMS(array);
    1795                 :           3 :         lbs = ARR_LBOUND(array);
    1796                 :             : 
    1797                 :           3 :         elmlen = typentry->typlen;
    1798                 :           3 :         elmbyval = typentry->typbyval;
    1799                 :           3 :         elmalign = typentry->typalign;
    1800                 :             : 
    1801                 :           3 :         deconstruct_array(array, elmtyp, elmlen, elmbyval, elmalign,
    1802                 :             :                                           &elms, &nuls, &nelm);
    1803                 :             : 
    1804                 :           3 :         nitem = dims[0];                        /* total number of items */
    1805                 :           3 :         nelm /= nitem;                          /* number of elements per item */
    1806                 :             : 
    1807                 :             :         /* Reverse the array */
    1808                 :           3 :         ielms = elms;
    1809                 :           3 :         inuls = nuls;
    1810         [ +  + ]:           9 :         for (int i = 0; i < nitem / 2; i++)
    1811                 :             :         {
    1812                 :           6 :                 int                     j = (nitem - i - 1) * nelm;
    1813                 :           6 :                 Datum      *jelms = elms + j;
    1814                 :           6 :                 bool       *jnuls = nuls + j;
    1815                 :             : 
    1816                 :             :                 /* Swap i'th and j'th items; advance ielms/inuls to next item */
    1817         [ +  + ]:          14 :                 for (int k = 0; k < nelm; k++)
    1818                 :             :                 {
    1819                 :           8 :                         Datum           elm = *ielms;
    1820                 :           8 :                         bool            nul = *inuls;
    1821                 :             : 
    1822                 :           8 :                         *ielms++ = *jelms;
    1823                 :           8 :                         *inuls++ = *jnuls;
    1824                 :           8 :                         *jelms++ = elm;
    1825                 :           8 :                         *jnuls++ = nul;
    1826                 :           8 :                 }
    1827                 :           6 :         }
    1828                 :             : 
    1829                 :             :         /* Set up dimensions of the result */
    1830                 :           3 :         memcpy(rdims, dims, ndim * sizeof(int));
    1831                 :           3 :         memcpy(rlbs, lbs, ndim * sizeof(int));
    1832                 :           3 :         rdims[0] = nitem;
    1833                 :             : 
    1834                 :           6 :         result = construct_md_array(elms, nuls, ndim, rdims, rlbs,
    1835                 :           3 :                                                                 elmtyp, elmlen, elmbyval, elmalign);
    1836                 :             : 
    1837                 :           3 :         pfree(elms);
    1838                 :           3 :         pfree(nuls);
    1839                 :             : 
    1840                 :           6 :         return result;
    1841                 :           3 : }
    1842                 :             : 
    1843                 :             : /*
    1844                 :             :  * array_reverse
    1845                 :             :  *
    1846                 :             :  * Returns an array with the same dimensions as the input array, with its
    1847                 :             :  * first-dimension elements in reverse order.
    1848                 :             :  */
    1849                 :             : Datum
    1850                 :           5 : array_reverse(PG_FUNCTION_ARGS)
    1851                 :             : {
    1852                 :           5 :         ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
    1853                 :           5 :         ArrayType  *result;
    1854                 :           5 :         Oid                     elmtyp;
    1855                 :           5 :         TypeCacheEntry *typentry;
    1856                 :             : 
    1857                 :             :         /*
    1858                 :             :          * There is no point in reversing empty arrays or arrays with less than
    1859                 :             :          * two items.
    1860                 :             :          */
    1861   [ +  +  +  + ]:           5 :         if (ARR_NDIM(array) < 1 || ARR_DIMS(array)[0] < 2)
    1862                 :           2 :                 PG_RETURN_ARRAYTYPE_P(array);
    1863                 :             : 
    1864                 :           3 :         elmtyp = ARR_ELEMTYPE(array);
    1865                 :           3 :         typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
    1866   [ -  +  #  # ]:           3 :         if (typentry == NULL || typentry->type_id != elmtyp)
    1867                 :             :         {
    1868                 :           3 :                 typentry = lookup_type_cache(elmtyp, 0);
    1869                 :           3 :                 fcinfo->flinfo->fn_extra = (void *) typentry;
    1870                 :           3 :         }
    1871                 :             : 
    1872                 :           3 :         result = array_reverse_n(array, elmtyp, typentry);
    1873                 :             : 
    1874                 :           3 :         PG_RETURN_ARRAYTYPE_P(result);
    1875                 :           5 : }
    1876                 :             : 
    1877                 :             : /*
    1878                 :             :  * array_sort
    1879                 :             :  *
    1880                 :             :  * Sorts the first dimension of the array.
    1881                 :             :  */
    1882                 :             : static ArrayType *
    1883                 :          26 : array_sort_internal(ArrayType *array, bool descending, bool nulls_first,
    1884                 :             :                                         FunctionCallInfo fcinfo)
    1885                 :             : {
    1886                 :          26 :         ArrayType  *newarray;
    1887                 :          26 :         Oid                     collation = PG_GET_COLLATION();
    1888                 :          26 :         int                     ndim,
    1889                 :             :                            *dims,
    1890                 :             :                            *lbs;
    1891                 :          26 :         ArraySortCachedInfo *cache_info;
    1892                 :          26 :         Oid                     elmtyp;
    1893                 :          26 :         Oid                     sort_typ;
    1894                 :          26 :         Oid                     sort_opr;
    1895                 :          26 :         Tuplesortstate *tuplesortstate;
    1896                 :          26 :         ArrayIterator array_iterator;
    1897                 :          26 :         Datum           value;
    1898                 :          26 :         bool            isnull;
    1899                 :          26 :         ArrayBuildStateAny *astate = NULL;
    1900                 :             : 
    1901                 :          26 :         ndim = ARR_NDIM(array);
    1902                 :          26 :         dims = ARR_DIMS(array);
    1903                 :          26 :         lbs = ARR_LBOUND(array);
    1904                 :             : 
    1905                 :             :         /* Quick exit if we don't need to sort */
    1906   [ +  +  +  + ]:          26 :         if (ndim < 1 || dims[0] < 2)
    1907                 :           4 :                 return array;
    1908                 :             : 
    1909                 :             :         /* Set up cache area if we didn't already */
    1910                 :          22 :         cache_info = (ArraySortCachedInfo *) fcinfo->flinfo->fn_extra;
    1911         [ -  + ]:          22 :         if (cache_info == NULL)
    1912                 :             :         {
    1913                 :          22 :                 cache_info = (ArraySortCachedInfo *)
    1914                 :          22 :                         MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
    1915                 :             :                                                                    sizeof(ArraySortCachedInfo));
    1916                 :          22 :                 fcinfo->flinfo->fn_extra = cache_info;
    1917                 :          22 :         }
    1918                 :             : 
    1919                 :             :         /* Fetch and cache required data if we don't have it */
    1920                 :          22 :         elmtyp = ARR_ELEMTYPE(array);
    1921         [ -  + ]:          22 :         if (elmtyp != cache_info->array_meta.element_type)
    1922                 :             :         {
    1923                 :          22 :                 TypeCacheEntry *typentry;
    1924                 :             : 
    1925                 :          22 :                 typentry = lookup_type_cache(elmtyp,
    1926                 :             :                                                                          TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
    1927                 :          22 :                 cache_info->array_meta.element_type = elmtyp;
    1928                 :          22 :                 cache_info->array_meta.typlen = typentry->typlen;
    1929                 :          22 :                 cache_info->array_meta.typbyval = typentry->typbyval;
    1930                 :          22 :                 cache_info->array_meta.typalign = typentry->typalign;
    1931                 :          22 :                 cache_info->elem_lt_opr = typentry->lt_opr;
    1932                 :          22 :                 cache_info->elem_gt_opr = typentry->gt_opr;
    1933                 :          22 :                 cache_info->array_type = typentry->typarray;
    1934                 :          22 :         }
    1935                 :             : 
    1936                 :             :         /* Identify the sort operator to use */
    1937         [ +  + ]:          22 :         if (ndim == 1)
    1938                 :             :         {
    1939                 :             :                 /* Need to sort the element type */
    1940                 :          15 :                 sort_typ = elmtyp;
    1941         [ +  + ]:          15 :                 sort_opr = (descending ? cache_info->elem_gt_opr : cache_info->elem_lt_opr);
    1942                 :          15 :         }
    1943                 :             :         else
    1944                 :             :         {
    1945                 :             :                 /* Otherwise we're sorting arrays */
    1946                 :           7 :                 sort_typ = cache_info->array_type;
    1947         [ +  - ]:           7 :                 if (!OidIsValid(sort_typ))
    1948   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    1949                 :             :                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    1950                 :             :                                          errmsg("could not find array type for data type %s",
    1951                 :             :                                                         format_type_be(elmtyp))));
    1952                 :             :                 /* We know what operators to use for arrays */
    1953                 :           7 :                 sort_opr = (descending ? ARRAY_GT_OP : ARRAY_LT_OP);
    1954                 :             :         }
    1955                 :             : 
    1956                 :             :         /*
    1957                 :             :          * Fail if we don't know how to sort.  The error message is chosen to
    1958                 :             :          * match what array_lt()/array_gt() will say in the multidimensional case.
    1959                 :             :          */
    1960         [ +  + ]:          22 :         if (!OidIsValid(sort_opr))
    1961   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1962                 :             :                                 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1963                 :             :                                 errmsg("could not identify a comparison function for type %s",
    1964                 :             :                                            format_type_be(elmtyp)));
    1965                 :             : 
    1966                 :             :         /* Put the things to be sorted (elements or sub-arrays) into a tuplesort */
    1967                 :          42 :         tuplesortstate = tuplesort_begin_datum(sort_typ,
    1968                 :          21 :                                                                                    sort_opr,
    1969                 :          21 :                                                                                    collation,
    1970                 :          21 :                                                                                    nulls_first,
    1971                 :          21 :                                                                                    work_mem,
    1972                 :             :                                                                                    NULL,
    1973                 :             :                                                                                    TUPLESORT_NONE);
    1974                 :             : 
    1975                 :          42 :         array_iterator = array_create_iterator(array, ndim - 1,
    1976                 :          21 :                                                                                    &cache_info->array_meta);
    1977         [ +  + ]:         111 :         while (array_iterate(array_iterator, &value, &isnull))
    1978                 :             :         {
    1979                 :          90 :                 tuplesort_putdatum(tuplesortstate, value, isnull);
    1980                 :             :         }
    1981                 :          21 :         array_free_iterator(array_iterator);
    1982                 :             : 
    1983                 :             :         /* Do the sort */
    1984                 :          21 :         tuplesort_performsort(tuplesortstate);
    1985                 :             : 
    1986                 :             :         /* Extract results into a new array */
    1987         [ +  + ]:         109 :         while (tuplesort_getdatum(tuplesortstate, true, false, &value, &isnull, NULL))
    1988                 :             :         {
    1989                 :         176 :                 astate = accumArrayResultAny(astate, value, isnull,
    1990                 :          88 :                                                                          sort_typ, CurrentMemoryContext);
    1991                 :             :         }
    1992                 :          21 :         tuplesort_end(tuplesortstate);
    1993                 :             : 
    1994                 :          21 :         newarray = DatumGetArrayTypeP(makeArrayResultAny(astate,
    1995                 :             :                                                                                                          CurrentMemoryContext,
    1996                 :             :                                                                                                          true));
    1997                 :             : 
    1998                 :             :         /* Adjust lower bound to match the input */
    1999                 :          21 :         ARR_LBOUND(newarray)[0] = lbs[0];
    2000                 :             : 
    2001                 :          21 :         return newarray;
    2002                 :          25 : }
    2003                 :             : 
    2004                 :             : Datum
    2005                 :          20 : array_sort(PG_FUNCTION_ARGS)
    2006                 :             : {
    2007                 :          20 :         ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
    2008                 :             : 
    2009                 :          40 :         PG_RETURN_ARRAYTYPE_P(array_sort_internal(array,
    2010                 :             :                                                                                           false,
    2011                 :             :                                                                                           false,
    2012                 :             :                                                                                           fcinfo));
    2013                 :          20 : }
    2014                 :             : 
    2015                 :             : Datum
    2016                 :           2 : array_sort_order(PG_FUNCTION_ARGS)
    2017                 :             : {
    2018                 :           2 :         ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
    2019                 :           2 :         bool            descending = PG_GETARG_BOOL(1);
    2020                 :             : 
    2021                 :           4 :         PG_RETURN_ARRAYTYPE_P(array_sort_internal(array,
    2022                 :             :                                                                                           descending,
    2023                 :             :                                                                                           descending,
    2024                 :             :                                                                                           fcinfo));
    2025                 :           2 : }
    2026                 :             : 
    2027                 :             : Datum
    2028                 :           4 : array_sort_order_nulls_first(PG_FUNCTION_ARGS)
    2029                 :             : {
    2030                 :           4 :         ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
    2031                 :           4 :         bool            descending = PG_GETARG_BOOL(1);
    2032                 :           4 :         bool            nulls_first = PG_GETARG_BOOL(2);
    2033                 :             : 
    2034                 :           8 :         PG_RETURN_ARRAYTYPE_P(array_sort_internal(array,
    2035                 :             :                                                                                           descending,
    2036                 :             :                                                                                           nulls_first,
    2037                 :             :                                                                                           fcinfo));
    2038                 :           4 : }
        

Generated by: LCOV version 2.3.2-1