LCOV - code coverage report
Current view: top level - src/backend/utils/adt - int8.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 96.1 % 694 667
Test Date: 2026-01-26 10:56:24 Functions: 98.9 % 90 89
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 70.8 % 360 255

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * int8.c
       4                 :             :  *        Internal 64-bit integer operations
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/backend/utils/adt/int8.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include <ctype.h>
      17                 :             : #include <limits.h>
      18                 :             : #include <math.h>
      19                 :             : 
      20                 :             : #include "common/int.h"
      21                 :             : #include "funcapi.h"
      22                 :             : #include "libpq/pqformat.h"
      23                 :             : #include "nodes/nodeFuncs.h"
      24                 :             : #include "nodes/supportnodes.h"
      25                 :             : #include "optimizer/optimizer.h"
      26                 :             : #include "utils/builtins.h"
      27                 :             : #include "utils/fmgroids.h"
      28                 :             : 
      29                 :             : typedef struct
      30                 :             : {
      31                 :             :         int64           current;
      32                 :             :         int64           finish;
      33                 :             :         int64           step;
      34                 :             : } generate_series_fctx;
      35                 :             : 
      36                 :             : 
      37                 :             : /***********************************************************************
      38                 :             :  **
      39                 :             :  **             Routines for 64-bit integers.
      40                 :             :  **
      41                 :             :  ***********************************************************************/
      42                 :             : 
      43                 :             : /*----------------------------------------------------------
      44                 :             :  * Formatting and conversion routines.
      45                 :             :  *---------------------------------------------------------*/
      46                 :             : 
      47                 :             : /* int8in()
      48                 :             :  */
      49                 :             : Datum
      50                 :        5604 : int8in(PG_FUNCTION_ARGS)
      51                 :             : {
      52                 :        5604 :         char       *num = PG_GETARG_CSTRING(0);
      53                 :             : 
      54                 :       11208 :         PG_RETURN_INT64(pg_strtoint64_safe(num, fcinfo->context));
      55                 :        5604 : }
      56                 :             : 
      57                 :             : 
      58                 :             : /* int8out()
      59                 :             :  */
      60                 :             : Datum
      61                 :        9608 : int8out(PG_FUNCTION_ARGS)
      62                 :             : {
      63                 :        9608 :         int64           val = PG_GETARG_INT64(0);
      64                 :        9608 :         char            buf[MAXINT8LEN + 1];
      65                 :        9608 :         char       *result;
      66                 :        9608 :         int                     len;
      67                 :             : 
      68                 :        9608 :         len = pg_lltoa(val, buf) + 1;
      69                 :             : 
      70                 :             :         /*
      71                 :             :          * Since the length is already known, we do a manual palloc() and memcpy()
      72                 :             :          * to avoid the strlen() call that would otherwise be done in pstrdup().
      73                 :             :          */
      74                 :        9608 :         result = palloc(len);
      75                 :        9608 :         memcpy(result, buf, len);
      76                 :       19216 :         PG_RETURN_CSTRING(result);
      77                 :        9608 : }
      78                 :             : 
      79                 :             : /*
      80                 :             :  *              int8recv                        - converts external binary format to int8
      81                 :             :  */
      82                 :             : Datum
      83                 :           0 : int8recv(PG_FUNCTION_ARGS)
      84                 :             : {
      85                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
      86                 :             : 
      87                 :           0 :         PG_RETURN_INT64(pq_getmsgint64(buf));
      88                 :           0 : }
      89                 :             : 
      90                 :             : /*
      91                 :             :  *              int8send                        - converts int8 to binary format
      92                 :             :  */
      93                 :             : Datum
      94                 :          12 : int8send(PG_FUNCTION_ARGS)
      95                 :             : {
      96                 :          12 :         int64           arg1 = PG_GETARG_INT64(0);
      97                 :          12 :         StringInfoData buf;
      98                 :             : 
      99                 :          12 :         pq_begintypsend(&buf);
     100                 :          12 :         pq_sendint64(&buf, arg1);
     101                 :          24 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     102                 :          12 : }
     103                 :             : 
     104                 :             : 
     105                 :             : /*----------------------------------------------------------
     106                 :             :  *      Relational operators for int8s, including cross-data-type comparisons.
     107                 :             :  *---------------------------------------------------------*/
     108                 :             : 
     109                 :             : /* int8relop()
     110                 :             :  * Is val1 relop val2?
     111                 :             :  */
     112                 :             : Datum
     113                 :       85364 : int8eq(PG_FUNCTION_ARGS)
     114                 :             : {
     115                 :       85364 :         int64           val1 = PG_GETARG_INT64(0);
     116                 :       85364 :         int64           val2 = PG_GETARG_INT64(1);
     117                 :             : 
     118                 :      170728 :         PG_RETURN_BOOL(val1 == val2);
     119                 :       85364 : }
     120                 :             : 
     121                 :             : Datum
     122                 :       10008 : int8ne(PG_FUNCTION_ARGS)
     123                 :             : {
     124                 :       10008 :         int64           val1 = PG_GETARG_INT64(0);
     125                 :       10008 :         int64           val2 = PG_GETARG_INT64(1);
     126                 :             : 
     127                 :       20016 :         PG_RETURN_BOOL(val1 != val2);
     128                 :       10008 : }
     129                 :             : 
     130                 :             : Datum
     131                 :      192109 : int8lt(PG_FUNCTION_ARGS)
     132                 :             : {
     133                 :      192109 :         int64           val1 = PG_GETARG_INT64(0);
     134                 :      192109 :         int64           val2 = PG_GETARG_INT64(1);
     135                 :             : 
     136                 :      384218 :         PG_RETURN_BOOL(val1 < val2);
     137                 :      192109 : }
     138                 :             : 
     139                 :             : Datum
     140                 :        7016 : int8gt(PG_FUNCTION_ARGS)
     141                 :             : {
     142                 :        7016 :         int64           val1 = PG_GETARG_INT64(0);
     143                 :        7016 :         int64           val2 = PG_GETARG_INT64(1);
     144                 :             : 
     145                 :       14032 :         PG_RETURN_BOOL(val1 > val2);
     146                 :        7016 : }
     147                 :             : 
     148                 :             : Datum
     149                 :         645 : int8le(PG_FUNCTION_ARGS)
     150                 :             : {
     151                 :         645 :         int64           val1 = PG_GETARG_INT64(0);
     152                 :         645 :         int64           val2 = PG_GETARG_INT64(1);
     153                 :             : 
     154                 :        1290 :         PG_RETURN_BOOL(val1 <= val2);
     155                 :         645 : }
     156                 :             : 
     157                 :             : Datum
     158                 :         733 : int8ge(PG_FUNCTION_ARGS)
     159                 :             : {
     160                 :         733 :         int64           val1 = PG_GETARG_INT64(0);
     161                 :         733 :         int64           val2 = PG_GETARG_INT64(1);
     162                 :             : 
     163                 :        1466 :         PG_RETURN_BOOL(val1 >= val2);
     164                 :         733 : }
     165                 :             : 
     166                 :             : /* int84relop()
     167                 :             :  * Is 64-bit val1 relop 32-bit val2?
     168                 :             :  */
     169                 :             : Datum
     170                 :       19147 : int84eq(PG_FUNCTION_ARGS)
     171                 :             : {
     172                 :       19147 :         int64           val1 = PG_GETARG_INT64(0);
     173                 :       19147 :         int32           val2 = PG_GETARG_INT32(1);
     174                 :             : 
     175                 :       38294 :         PG_RETURN_BOOL(val1 == val2);
     176                 :       19147 : }
     177                 :             : 
     178                 :             : Datum
     179                 :          12 : int84ne(PG_FUNCTION_ARGS)
     180                 :             : {
     181                 :          12 :         int64           val1 = PG_GETARG_INT64(0);
     182                 :          12 :         int32           val2 = PG_GETARG_INT32(1);
     183                 :             : 
     184                 :          24 :         PG_RETURN_BOOL(val1 != val2);
     185                 :          12 : }
     186                 :             : 
     187                 :             : Datum
     188                 :       89089 : int84lt(PG_FUNCTION_ARGS)
     189                 :             : {
     190                 :       89089 :         int64           val1 = PG_GETARG_INT64(0);
     191                 :       89089 :         int32           val2 = PG_GETARG_INT32(1);
     192                 :             : 
     193                 :      178178 :         PG_RETURN_BOOL(val1 < val2);
     194                 :       89089 : }
     195                 :             : 
     196                 :             : Datum
     197                 :       23330 : int84gt(PG_FUNCTION_ARGS)
     198                 :             : {
     199                 :       23330 :         int64           val1 = PG_GETARG_INT64(0);
     200                 :       23330 :         int32           val2 = PG_GETARG_INT32(1);
     201                 :             : 
     202                 :       46660 :         PG_RETURN_BOOL(val1 > val2);
     203                 :       23330 : }
     204                 :             : 
     205                 :             : Datum
     206                 :        3696 : int84le(PG_FUNCTION_ARGS)
     207                 :             : {
     208                 :        3696 :         int64           val1 = PG_GETARG_INT64(0);
     209                 :        3696 :         int32           val2 = PG_GETARG_INT32(1);
     210                 :             : 
     211                 :        7392 :         PG_RETURN_BOOL(val1 <= val2);
     212                 :        3696 : }
     213                 :             : 
     214                 :             : Datum
     215                 :        1664 : int84ge(PG_FUNCTION_ARGS)
     216                 :             : {
     217                 :        1664 :         int64           val1 = PG_GETARG_INT64(0);
     218                 :        1664 :         int32           val2 = PG_GETARG_INT32(1);
     219                 :             : 
     220                 :        3328 :         PG_RETURN_BOOL(val1 >= val2);
     221                 :        1664 : }
     222                 :             : 
     223                 :             : /* int48relop()
     224                 :             :  * Is 32-bit val1 relop 64-bit val2?
     225                 :             :  */
     226                 :             : Datum
     227                 :        5667 : int48eq(PG_FUNCTION_ARGS)
     228                 :             : {
     229                 :        5667 :         int32           val1 = PG_GETARG_INT32(0);
     230                 :        5667 :         int64           val2 = PG_GETARG_INT64(1);
     231                 :             : 
     232                 :       11334 :         PG_RETURN_BOOL(val1 == val2);
     233                 :        5667 : }
     234                 :             : 
     235                 :             : Datum
     236                 :           6 : int48ne(PG_FUNCTION_ARGS)
     237                 :             : {
     238                 :           6 :         int32           val1 = PG_GETARG_INT32(0);
     239                 :           6 :         int64           val2 = PG_GETARG_INT64(1);
     240                 :             : 
     241                 :          12 :         PG_RETURN_BOOL(val1 != val2);
     242                 :           6 : }
     243                 :             : 
     244                 :             : Datum
     245                 :        1103 : int48lt(PG_FUNCTION_ARGS)
     246                 :             : {
     247                 :        1103 :         int32           val1 = PG_GETARG_INT32(0);
     248                 :        1103 :         int64           val2 = PG_GETARG_INT64(1);
     249                 :             : 
     250                 :        2206 :         PG_RETURN_BOOL(val1 < val2);
     251                 :        1103 : }
     252                 :             : 
     253                 :             : Datum
     254                 :         545 : int48gt(PG_FUNCTION_ARGS)
     255                 :             : {
     256                 :         545 :         int32           val1 = PG_GETARG_INT32(0);
     257                 :         545 :         int64           val2 = PG_GETARG_INT64(1);
     258                 :             : 
     259                 :        1090 :         PG_RETURN_BOOL(val1 > val2);
     260                 :         545 : }
     261                 :             : 
     262                 :             : Datum
     263                 :         638 : int48le(PG_FUNCTION_ARGS)
     264                 :             : {
     265                 :         638 :         int32           val1 = PG_GETARG_INT32(0);
     266                 :         638 :         int64           val2 = PG_GETARG_INT64(1);
     267                 :             : 
     268                 :        1276 :         PG_RETURN_BOOL(val1 <= val2);
     269                 :         638 : }
     270                 :             : 
     271                 :             : Datum
     272                 :         579 : int48ge(PG_FUNCTION_ARGS)
     273                 :             : {
     274                 :         579 :         int32           val1 = PG_GETARG_INT32(0);
     275                 :         579 :         int64           val2 = PG_GETARG_INT64(1);
     276                 :             : 
     277                 :        1158 :         PG_RETURN_BOOL(val1 >= val2);
     278                 :         579 : }
     279                 :             : 
     280                 :             : /* int82relop()
     281                 :             :  * Is 64-bit val1 relop 16-bit val2?
     282                 :             :  */
     283                 :             : Datum
     284                 :           5 : int82eq(PG_FUNCTION_ARGS)
     285                 :             : {
     286                 :           5 :         int64           val1 = PG_GETARG_INT64(0);
     287                 :           5 :         int16           val2 = PG_GETARG_INT16(1);
     288                 :             : 
     289                 :          10 :         PG_RETURN_BOOL(val1 == val2);
     290                 :           5 : }
     291                 :             : 
     292                 :             : Datum
     293                 :           5 : int82ne(PG_FUNCTION_ARGS)
     294                 :             : {
     295                 :           5 :         int64           val1 = PG_GETARG_INT64(0);
     296                 :           5 :         int16           val2 = PG_GETARG_INT16(1);
     297                 :             : 
     298                 :          10 :         PG_RETURN_BOOL(val1 != val2);
     299                 :           5 : }
     300                 :             : 
     301                 :             : Datum
     302                 :           5 : int82lt(PG_FUNCTION_ARGS)
     303                 :             : {
     304                 :           5 :         int64           val1 = PG_GETARG_INT64(0);
     305                 :           5 :         int16           val2 = PG_GETARG_INT16(1);
     306                 :             : 
     307                 :          10 :         PG_RETURN_BOOL(val1 < val2);
     308                 :           5 : }
     309                 :             : 
     310                 :             : Datum
     311                 :         538 : int82gt(PG_FUNCTION_ARGS)
     312                 :             : {
     313                 :         538 :         int64           val1 = PG_GETARG_INT64(0);
     314                 :         538 :         int16           val2 = PG_GETARG_INT16(1);
     315                 :             : 
     316                 :        1076 :         PG_RETURN_BOOL(val1 > val2);
     317                 :         538 : }
     318                 :             : 
     319                 :             : Datum
     320                 :           5 : int82le(PG_FUNCTION_ARGS)
     321                 :             : {
     322                 :           5 :         int64           val1 = PG_GETARG_INT64(0);
     323                 :           5 :         int16           val2 = PG_GETARG_INT16(1);
     324                 :             : 
     325                 :          10 :         PG_RETURN_BOOL(val1 <= val2);
     326                 :           5 : }
     327                 :             : 
     328                 :             : Datum
     329                 :         538 : int82ge(PG_FUNCTION_ARGS)
     330                 :             : {
     331                 :         538 :         int64           val1 = PG_GETARG_INT64(0);
     332                 :         538 :         int16           val2 = PG_GETARG_INT16(1);
     333                 :             : 
     334                 :        1076 :         PG_RETURN_BOOL(val1 >= val2);
     335                 :         538 : }
     336                 :             : 
     337                 :             : /* int28relop()
     338                 :             :  * Is 16-bit val1 relop 64-bit val2?
     339                 :             :  */
     340                 :             : Datum
     341                 :         309 : int28eq(PG_FUNCTION_ARGS)
     342                 :             : {
     343                 :         309 :         int16           val1 = PG_GETARG_INT16(0);
     344                 :         309 :         int64           val2 = PG_GETARG_INT64(1);
     345                 :             : 
     346                 :         618 :         PG_RETURN_BOOL(val1 == val2);
     347                 :         309 : }
     348                 :             : 
     349                 :             : Datum
     350                 :         554 : int28ne(PG_FUNCTION_ARGS)
     351                 :             : {
     352                 :         554 :         int16           val1 = PG_GETARG_INT16(0);
     353                 :         554 :         int64           val2 = PG_GETARG_INT64(1);
     354                 :             : 
     355                 :        1108 :         PG_RETURN_BOOL(val1 != val2);
     356                 :         554 : }
     357                 :             : 
     358                 :             : Datum
     359                 :         538 : int28lt(PG_FUNCTION_ARGS)
     360                 :             : {
     361                 :         538 :         int16           val1 = PG_GETARG_INT16(0);
     362                 :         538 :         int64           val2 = PG_GETARG_INT64(1);
     363                 :             : 
     364                 :        1076 :         PG_RETURN_BOOL(val1 < val2);
     365                 :         538 : }
     366                 :             : 
     367                 :             : Datum
     368                 :         538 : int28gt(PG_FUNCTION_ARGS)
     369                 :             : {
     370                 :         538 :         int16           val1 = PG_GETARG_INT16(0);
     371                 :         538 :         int64           val2 = PG_GETARG_INT64(1);
     372                 :             : 
     373                 :        1076 :         PG_RETURN_BOOL(val1 > val2);
     374                 :         538 : }
     375                 :             : 
     376                 :             : Datum
     377                 :         638 : int28le(PG_FUNCTION_ARGS)
     378                 :             : {
     379                 :         638 :         int16           val1 = PG_GETARG_INT16(0);
     380                 :         638 :         int64           val2 = PG_GETARG_INT64(1);
     381                 :             : 
     382                 :        1276 :         PG_RETURN_BOOL(val1 <= val2);
     383                 :         638 : }
     384                 :             : 
     385                 :             : Datum
     386                 :         619 : int28ge(PG_FUNCTION_ARGS)
     387                 :             : {
     388                 :         619 :         int16           val1 = PG_GETARG_INT16(0);
     389                 :         619 :         int64           val2 = PG_GETARG_INT64(1);
     390                 :             : 
     391                 :        1238 :         PG_RETURN_BOOL(val1 >= val2);
     392                 :         619 : }
     393                 :             : 
     394                 :             : /*
     395                 :             :  * in_range support function for int8.
     396                 :             :  *
     397                 :             :  * Note: we needn't supply int8_int4 or int8_int2 variants, as implicit
     398                 :             :  * coercion of the offset value takes care of those scenarios just as well.
     399                 :             :  */
     400                 :             : Datum
     401                 :          18 : in_range_int8_int8(PG_FUNCTION_ARGS)
     402                 :             : {
     403                 :          18 :         int64           val = PG_GETARG_INT64(0);
     404                 :          18 :         int64           base = PG_GETARG_INT64(1);
     405                 :          18 :         int64           offset = PG_GETARG_INT64(2);
     406                 :          18 :         bool            sub = PG_GETARG_BOOL(3);
     407                 :          18 :         bool            less = PG_GETARG_BOOL(4);
     408                 :          18 :         int64           sum;
     409                 :             : 
     410         [ +  - ]:          18 :         if (offset < 0)
     411   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     412                 :             :                                 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
     413                 :             :                                  errmsg("invalid preceding or following size in window function")));
     414                 :             : 
     415         [ +  + ]:          18 :         if (sub)
     416                 :           9 :                 offset = -offset;               /* cannot overflow */
     417                 :             : 
     418         [ +  + ]:          18 :         if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
     419                 :             :         {
     420                 :             :                 /*
     421                 :             :                  * If sub is false, the true sum is surely more than val, so correct
     422                 :             :                  * answer is the same as "less".  If sub is true, the true sum is
     423                 :             :                  * surely less than val, so the answer is "!less".
     424                 :             :                  */
     425         [ +  + ]:           6 :                 PG_RETURN_BOOL(sub ? !less : less);
     426                 :             :         }
     427                 :             : 
     428         [ +  + ]:          12 :         if (less)
     429                 :           6 :                 PG_RETURN_BOOL(val <= sum);
     430                 :             :         else
     431                 :           6 :                 PG_RETURN_BOOL(val >= sum);
     432                 :          18 : }
     433                 :             : 
     434                 :             : 
     435                 :             : /*----------------------------------------------------------
     436                 :             :  *      Arithmetic operators on 64-bit integers.
     437                 :             :  *---------------------------------------------------------*/
     438                 :             : 
     439                 :             : Datum
     440                 :         156 : int8um(PG_FUNCTION_ARGS)
     441                 :             : {
     442                 :         156 :         int64           arg = PG_GETARG_INT64(0);
     443                 :         156 :         int64           result;
     444                 :             : 
     445         [ +  + ]:         156 :         if (unlikely(arg == PG_INT64_MIN))
     446   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     447                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     448                 :             :                                  errmsg("bigint out of range")));
     449                 :         155 :         result = -arg;
     450                 :         310 :         PG_RETURN_INT64(result);
     451                 :         155 : }
     452                 :             : 
     453                 :             : Datum
     454                 :           1 : int8up(PG_FUNCTION_ARGS)
     455                 :             : {
     456                 :           1 :         int64           arg = PG_GETARG_INT64(0);
     457                 :             : 
     458                 :           2 :         PG_RETURN_INT64(arg);
     459                 :           1 : }
     460                 :             : 
     461                 :             : Datum
     462                 :       41807 : int8pl(PG_FUNCTION_ARGS)
     463                 :             : {
     464                 :       41807 :         int64           arg1 = PG_GETARG_INT64(0);
     465                 :       41807 :         int64           arg2 = PG_GETARG_INT64(1);
     466                 :       41807 :         int64           result;
     467                 :             : 
     468         [ +  + ]:       41807 :         if (unlikely(pg_add_s64_overflow(arg1, arg2, &result)))
     469   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     470                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     471                 :             :                                  errmsg("bigint out of range")));
     472                 :       83610 :         PG_RETURN_INT64(result);
     473                 :       41805 : }
     474                 :             : 
     475                 :             : Datum
     476                 :          26 : int8mi(PG_FUNCTION_ARGS)
     477                 :             : {
     478                 :          26 :         int64           arg1 = PG_GETARG_INT64(0);
     479                 :          26 :         int64           arg2 = PG_GETARG_INT64(1);
     480                 :          26 :         int64           result;
     481                 :             : 
     482         [ +  + ]:          26 :         if (unlikely(pg_sub_s64_overflow(arg1, arg2, &result)))
     483   [ +  -  +  - ]:           3 :                 ereport(ERROR,
     484                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     485                 :             :                                  errmsg("bigint out of range")));
     486                 :          46 :         PG_RETURN_INT64(result);
     487                 :          23 : }
     488                 :             : 
     489                 :             : Datum
     490                 :          30 : int8mul(PG_FUNCTION_ARGS)
     491                 :             : {
     492                 :          30 :         int64           arg1 = PG_GETARG_INT64(0);
     493                 :          30 :         int64           arg2 = PG_GETARG_INT64(1);
     494                 :          30 :         int64           result;
     495                 :             : 
     496         [ +  + ]:          30 :         if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
     497   [ +  -  +  - ]:           3 :                 ereport(ERROR,
     498                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     499                 :             :                                  errmsg("bigint out of range")));
     500                 :          54 :         PG_RETURN_INT64(result);
     501                 :          27 : }
     502                 :             : 
     503                 :             : Datum
     504                 :          17 : int8div(PG_FUNCTION_ARGS)
     505                 :             : {
     506                 :          17 :         int64           arg1 = PG_GETARG_INT64(0);
     507                 :          17 :         int64           arg2 = PG_GETARG_INT64(1);
     508                 :          17 :         int64           result;
     509                 :             : 
     510         [ +  + ]:          17 :         if (arg2 == 0)
     511                 :             :         {
     512   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     513                 :             :                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
     514                 :             :                                  errmsg("division by zero")));
     515                 :             :                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
     516                 :           0 :                 PG_RETURN_NULL();
     517                 :           0 :         }
     518                 :             : 
     519                 :             :         /*
     520                 :             :          * INT64_MIN / -1 is problematic, since the result can't be represented on
     521                 :             :          * a two's-complement machine.  Some machines produce INT64_MIN, some
     522                 :             :          * produce zero, some throw an exception.  We can dodge the problem by
     523                 :             :          * recognizing that division by -1 is the same as negation.
     524                 :             :          */
     525         [ +  + ]:          16 :         if (arg2 == -1)
     526                 :             :         {
     527         [ -  + ]:           1 :                 if (unlikely(arg1 == PG_INT64_MIN))
     528   [ +  -  +  - ]:           1 :                         ereport(ERROR,
     529                 :             :                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     530                 :             :                                          errmsg("bigint out of range")));
     531                 :           0 :                 result = -arg1;
     532                 :           0 :                 PG_RETURN_INT64(result);
     533                 :             :         }
     534                 :             : 
     535                 :             :         /* No overflow is possible */
     536                 :             : 
     537                 :          15 :         result = arg1 / arg2;
     538                 :             : 
     539                 :          15 :         PG_RETURN_INT64(result);
     540                 :          15 : }
     541                 :             : 
     542                 :             : /* int8abs()
     543                 :             :  * Absolute value
     544                 :             :  */
     545                 :             : Datum
     546                 :           6 : int8abs(PG_FUNCTION_ARGS)
     547                 :             : {
     548                 :           6 :         int64           arg1 = PG_GETARG_INT64(0);
     549                 :           6 :         int64           result;
     550                 :             : 
     551         [ +  + ]:           6 :         if (unlikely(arg1 == PG_INT64_MIN))
     552   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     553                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     554                 :             :                                  errmsg("bigint out of range")));
     555         [ +  + ]:           5 :         result = (arg1 < 0) ? -arg1 : arg1;
     556                 :          10 :         PG_RETURN_INT64(result);
     557                 :           5 : }
     558                 :             : 
     559                 :             : /* int8mod()
     560                 :             :  * Modulo operation.
     561                 :             :  */
     562                 :             : Datum
     563                 :           9 : int8mod(PG_FUNCTION_ARGS)
     564                 :             : {
     565                 :           9 :         int64           arg1 = PG_GETARG_INT64(0);
     566                 :           9 :         int64           arg2 = PG_GETARG_INT64(1);
     567                 :             : 
     568         [ +  + ]:           9 :         if (unlikely(arg2 == 0))
     569                 :             :         {
     570   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     571                 :             :                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
     572                 :             :                                  errmsg("division by zero")));
     573                 :             :                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
     574                 :           0 :                 PG_RETURN_NULL();
     575                 :           0 :         }
     576                 :             : 
     577                 :             :         /*
     578                 :             :          * Some machines throw a floating-point exception for INT64_MIN % -1,
     579                 :             :          * which is a bit silly since the correct answer is perfectly
     580                 :             :          * well-defined, namely zero.
     581                 :             :          */
     582         [ +  + ]:           8 :         if (arg2 == -1)
     583                 :           3 :                 PG_RETURN_INT64(0);
     584                 :             : 
     585                 :             :         /* No overflow is possible */
     586                 :             : 
     587                 :           5 :         PG_RETURN_INT64(arg1 % arg2);
     588                 :           8 : }
     589                 :             : 
     590                 :             : /*
     591                 :             :  * Greatest Common Divisor
     592                 :             :  *
     593                 :             :  * Returns the largest positive integer that exactly divides both inputs.
     594                 :             :  * Special cases:
     595                 :             :  *   - gcd(x, 0) = gcd(0, x) = abs(x)
     596                 :             :  *              because 0 is divisible by anything
     597                 :             :  *   - gcd(0, 0) = 0
     598                 :             :  *              complies with the previous definition and is a common convention
     599                 :             :  *
     600                 :             :  * Special care must be taken if either input is INT64_MIN ---
     601                 :             :  * gcd(0, INT64_MIN), gcd(INT64_MIN, 0) and gcd(INT64_MIN, INT64_MIN) are
     602                 :             :  * all equal to abs(INT64_MIN), which cannot be represented as a 64-bit signed
     603                 :             :  * integer.
     604                 :             :  */
     605                 :             : static int64
     606                 :          44 : int8gcd_internal(int64 arg1, int64 arg2)
     607                 :             : {
     608                 :          44 :         int64           swap;
     609                 :          44 :         int64           a1,
     610                 :             :                                 a2;
     611                 :             : 
     612                 :             :         /*
     613                 :             :          * Put the greater absolute value in arg1.
     614                 :             :          *
     615                 :             :          * This would happen automatically in the loop below, but avoids an
     616                 :             :          * expensive modulo operation, and simplifies the special-case handling
     617                 :             :          * for INT64_MIN below.
     618                 :             :          *
     619                 :             :          * We do this in negative space in order to handle INT64_MIN.
     620                 :             :          */
     621         [ +  + ]:          44 :         a1 = (arg1 < 0) ? arg1 : -arg1;
     622         [ +  + ]:          44 :         a2 = (arg2 < 0) ? arg2 : -arg2;
     623         [ +  + ]:          44 :         if (a1 > a2)
     624                 :             :         {
     625                 :          16 :                 swap = arg1;
     626                 :          16 :                 arg1 = arg2;
     627                 :          16 :                 arg2 = swap;
     628                 :          16 :         }
     629                 :             : 
     630                 :             :         /* Special care needs to be taken with INT64_MIN.  See comments above. */
     631         [ +  + ]:          44 :         if (arg1 == PG_INT64_MIN)
     632                 :             :         {
     633         [ +  + ]:          15 :                 if (arg2 == 0 || arg2 == PG_INT64_MIN)
     634   [ +  -  +  - ]:           2 :                         ereport(ERROR,
     635                 :             :                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     636                 :             :                                          errmsg("bigint out of range")));
     637                 :             : 
     638                 :             :                 /*
     639                 :             :                  * Some machines throw a floating-point exception for INT64_MIN % -1,
     640                 :             :                  * which is a bit silly since the correct answer is perfectly
     641                 :             :                  * well-defined, namely zero.  Guard against this and just return the
     642                 :             :                  * result, gcd(INT64_MIN, -1) = 1.
     643                 :             :                  */
     644         [ +  + ]:          13 :                 if (arg2 == -1)
     645                 :           2 :                         return 1;
     646                 :          11 :         }
     647                 :             : 
     648                 :             :         /* Use the Euclidean algorithm to find the GCD */
     649         [ +  + ]:         205 :         while (arg2 != 0)
     650                 :             :         {
     651                 :         165 :                 swap = arg2;
     652                 :         165 :                 arg2 = arg1 % arg2;
     653                 :         165 :                 arg1 = swap;
     654                 :             :         }
     655                 :             : 
     656                 :             :         /*
     657                 :             :          * Make sure the result is positive. (We know we don't have INT64_MIN
     658                 :             :          * anymore).
     659                 :             :          */
     660         [ +  + ]:          40 :         if (arg1 < 0)
     661                 :          17 :                 arg1 = -arg1;
     662                 :             : 
     663                 :          40 :         return arg1;
     664                 :          42 : }
     665                 :             : 
     666                 :             : Datum
     667                 :          30 : int8gcd(PG_FUNCTION_ARGS)
     668                 :             : {
     669                 :          30 :         int64           arg1 = PG_GETARG_INT64(0);
     670                 :          30 :         int64           arg2 = PG_GETARG_INT64(1);
     671                 :          30 :         int64           result;
     672                 :             : 
     673                 :          30 :         result = int8gcd_internal(arg1, arg2);
     674                 :             : 
     675                 :          60 :         PG_RETURN_INT64(result);
     676                 :          30 : }
     677                 :             : 
     678                 :             : /*
     679                 :             :  * Least Common Multiple
     680                 :             :  */
     681                 :             : Datum
     682                 :          26 : int8lcm(PG_FUNCTION_ARGS)
     683                 :             : {
     684                 :          26 :         int64           arg1 = PG_GETARG_INT64(0);
     685                 :          26 :         int64           arg2 = PG_GETARG_INT64(1);
     686                 :          26 :         int64           gcd;
     687                 :          26 :         int64           result;
     688                 :             : 
     689                 :             :         /*
     690                 :             :          * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case.  This prevents a
     691                 :             :          * division-by-zero error below when x is zero, and an overflow error from
     692                 :             :          * the GCD computation when x = INT64_MIN.
     693                 :             :          */
     694   [ +  +  +  + ]:          26 :         if (arg1 == 0 || arg2 == 0)
     695                 :          12 :                 PG_RETURN_INT64(0);
     696                 :             : 
     697                 :             :         /* lcm(x, y) = abs(x / gcd(x, y) * y) */
     698                 :          14 :         gcd = int8gcd_internal(arg1, arg2);
     699                 :          14 :         arg1 = arg1 / gcd;
     700                 :             : 
     701         [ +  + ]:          14 :         if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
     702   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     703                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     704                 :             :                                  errmsg("bigint out of range")));
     705                 :             : 
     706                 :             :         /* If the result is INT64_MIN, it cannot be represented. */
     707         [ +  + ]:          13 :         if (unlikely(result == PG_INT64_MIN))
     708   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     709                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     710                 :             :                                  errmsg("bigint out of range")));
     711                 :             : 
     712         [ +  + ]:          12 :         if (result < 0)
     713                 :           6 :                 result = -result;
     714                 :             : 
     715                 :          12 :         PG_RETURN_INT64(result);
     716                 :          24 : }
     717                 :             : 
     718                 :             : Datum
     719                 :     3196913 : int8inc(PG_FUNCTION_ARGS)
     720                 :             : {
     721                 :     3196913 :         int64           arg = PG_GETARG_INT64(0);
     722                 :     3196913 :         int64           result;
     723                 :             : 
     724         [ +  - ]:     3196913 :         if (unlikely(pg_add_s64_overflow(arg, 1, &result)))
     725   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     726                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     727                 :             :                                  errmsg("bigint out of range")));
     728                 :             : 
     729                 :     6393826 :         PG_RETURN_INT64(result);
     730                 :     3196913 : }
     731                 :             : 
     732                 :             : Datum
     733                 :           4 : int8dec(PG_FUNCTION_ARGS)
     734                 :             : {
     735                 :           4 :         int64           arg = PG_GETARG_INT64(0);
     736                 :           4 :         int64           result;
     737                 :             : 
     738         [ +  - ]:           4 :         if (unlikely(pg_sub_s64_overflow(arg, 1, &result)))
     739   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     740                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     741                 :             :                                  errmsg("bigint out of range")));
     742                 :             : 
     743                 :           8 :         PG_RETURN_INT64(result);
     744                 :           4 : }
     745                 :             : 
     746                 :             : 
     747                 :             : /*
     748                 :             :  * These functions are exactly like int8inc/int8dec but are used for
     749                 :             :  * aggregates that count only non-null values.  Since the functions are
     750                 :             :  * declared strict, the null checks happen before we ever get here, and all we
     751                 :             :  * need do is increment the state value.  We could actually make these pg_proc
     752                 :             :  * entries point right at int8inc/int8dec, but then the opr_sanity regression
     753                 :             :  * test would complain about mismatched entries for a built-in function.
     754                 :             :  */
     755                 :             : 
     756                 :             : Datum
     757                 :      155501 : int8inc_any(PG_FUNCTION_ARGS)
     758                 :             : {
     759                 :      155501 :         return int8inc(fcinfo);
     760                 :             : }
     761                 :             : 
     762                 :             : Datum
     763                 :       40004 : int8inc_float8_float8(PG_FUNCTION_ARGS)
     764                 :             : {
     765                 :       40004 :         return int8inc(fcinfo);
     766                 :             : }
     767                 :             : 
     768                 :             : Datum
     769                 :           1 : int8dec_any(PG_FUNCTION_ARGS)
     770                 :             : {
     771                 :           1 :         return int8dec(fcinfo);
     772                 :             : }
     773                 :             : 
     774                 :             : /*
     775                 :             :  * int8inc_support
     776                 :             :  *              prosupport function for int8inc() and int8inc_any()
     777                 :             :  */
     778                 :             : Datum
     779                 :        1851 : int8inc_support(PG_FUNCTION_ARGS)
     780                 :             : {
     781                 :        1851 :         Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
     782                 :             : 
     783         [ +  + ]:        1851 :         if (IsA(rawreq, SupportRequestWFuncMonotonic))
     784                 :             :         {
     785                 :          13 :                 SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
     786                 :          13 :                 MonotonicFunction monotonic = MONOTONICFUNC_NONE;
     787                 :          13 :                 int                     frameOptions = req->window_clause->frameOptions;
     788                 :             : 
     789                 :             :                 /* No ORDER BY clause then all rows are peers */
     790         [ +  + ]:          13 :                 if (req->window_clause->orderClause == NIL)
     791                 :           4 :                         monotonic = MONOTONICFUNC_BOTH;
     792                 :             :                 else
     793                 :             :                 {
     794                 :             :                         /*
     795                 :             :                          * Otherwise take into account the frame options.  When the frame
     796                 :             :                          * bound is the start of the window then the resulting value can
     797                 :             :                          * never decrease, therefore is monotonically increasing
     798                 :             :                          */
     799         [ +  + ]:           9 :                         if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
     800                 :           7 :                                 monotonic |= MONOTONICFUNC_INCREASING;
     801                 :             : 
     802                 :             :                         /*
     803                 :             :                          * Likewise, if the frame bound is the end of the window then the
     804                 :             :                          * resulting value can never decrease.
     805                 :             :                          */
     806         [ +  + ]:           9 :                         if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
     807                 :           2 :                                 monotonic |= MONOTONICFUNC_DECREASING;
     808                 :             :                 }
     809                 :             : 
     810                 :          13 :                 req->monotonic = monotonic;
     811                 :          13 :                 PG_RETURN_POINTER(req);
     812                 :          13 :         }
     813                 :             : 
     814         [ +  + ]:        1838 :         if (IsA(rawreq, SupportRequestSimplifyAggref))
     815                 :             :         {
     816                 :        1751 :                 SupportRequestSimplifyAggref *req = (SupportRequestSimplifyAggref *) rawreq;
     817                 :        1751 :                 Aggref     *agg = req->aggref;
     818                 :             : 
     819                 :             :                 /*
     820                 :             :                  * Check for COUNT(ANY) and try to convert to COUNT(*). The input
     821                 :             :                  * argument cannot be NULL, we can't have an ORDER BY / DISTINCT in
     822                 :             :                  * the aggregate, and agglevelsup must be 0.
     823                 :             :                  *
     824                 :             :                  * Technically COUNT(ANY) must have 1 arg, but be paranoid and check.
     825                 :             :                  */
     826   [ +  +  -  + ]:        1751 :                 if (agg->aggfnoid == F_COUNT_ANY && list_length(agg->args) == 1)
     827                 :             :                 {
     828                 :         148 :                         TargetEntry *tle = (TargetEntry *) linitial(agg->args);
     829                 :         148 :                         Expr       *arg = tle->expr;
     830                 :             : 
     831                 :             :                         /* Check for unsupported cases */
     832   [ +  +  +  +  :         148 :                         if (agg->aggdistinct != NIL || agg->aggorder != NIL ||
                   +  + ]
     833                 :         101 :                                 agg->agglevelsup != 0)
     834                 :          49 :                                 PG_RETURN_POINTER(NULL);
     835                 :             : 
     836                 :             :                         /* If the arg isn't NULLable, do the conversion */
     837         [ +  + ]:          99 :                         if (expr_is_nonnullable(req->root, arg, false))
     838                 :             :                         {
     839                 :           6 :                                 Aggref     *newagg;
     840                 :             : 
     841                 :             :                                 /* We don't expect these to have been set yet */
     842         [ +  - ]:           6 :                                 Assert(agg->aggtransno == -1);
     843         [ +  - ]:           6 :                                 Assert(agg->aggtranstype == InvalidOid);
     844                 :             : 
     845                 :             :                                 /* Convert COUNT(ANY) to COUNT(*) by making a new Aggref */
     846                 :           6 :                                 newagg = makeNode(Aggref);
     847                 :           6 :                                 memcpy(newagg, agg, sizeof(Aggref));
     848                 :           6 :                                 newagg->aggfnoid = F_COUNT_;
     849                 :             : 
     850                 :             :                                 /* count(*) has no args */
     851                 :           6 :                                 newagg->aggargtypes = NULL;
     852                 :           6 :                                 newagg->args = NULL;
     853                 :           6 :                                 newagg->aggstar = true;
     854                 :           6 :                                 newagg->location = -1;
     855                 :             : 
     856                 :           6 :                                 PG_RETURN_POINTER(newagg);
     857                 :           6 :                         }
     858         [ +  + ]:         148 :                 }
     859         [ +  + ]:        1751 :         }
     860                 :             : 
     861                 :        1783 :         PG_RETURN_POINTER(NULL);
     862                 :        1851 : }
     863                 :             : 
     864                 :             : 
     865                 :             : Datum
     866                 :         143 : int8larger(PG_FUNCTION_ARGS)
     867                 :             : {
     868                 :         143 :         int64           arg1 = PG_GETARG_INT64(0);
     869                 :         143 :         int64           arg2 = PG_GETARG_INT64(1);
     870                 :         143 :         int64           result;
     871                 :             : 
     872         [ +  + ]:         143 :         result = ((arg1 > arg2) ? arg1 : arg2);
     873                 :             : 
     874                 :         286 :         PG_RETURN_INT64(result);
     875                 :         143 : }
     876                 :             : 
     877                 :             : Datum
     878                 :         122 : int8smaller(PG_FUNCTION_ARGS)
     879                 :             : {
     880                 :         122 :         int64           arg1 = PG_GETARG_INT64(0);
     881                 :         122 :         int64           arg2 = PG_GETARG_INT64(1);
     882                 :         122 :         int64           result;
     883                 :             : 
     884         [ +  + ]:         122 :         result = ((arg1 < arg2) ? arg1 : arg2);
     885                 :             : 
     886                 :         244 :         PG_RETURN_INT64(result);
     887                 :         122 : }
     888                 :             : 
     889                 :             : Datum
     890                 :         134 : int84pl(PG_FUNCTION_ARGS)
     891                 :             : {
     892                 :         134 :         int64           arg1 = PG_GETARG_INT64(0);
     893                 :         134 :         int32           arg2 = PG_GETARG_INT32(1);
     894                 :         134 :         int64           result;
     895                 :             : 
     896         [ +  + ]:         134 :         if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
     897   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     898                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     899                 :             :                                  errmsg("bigint out of range")));
     900                 :         266 :         PG_RETURN_INT64(result);
     901                 :         133 : }
     902                 :             : 
     903                 :             : Datum
     904                 :          14 : int84mi(PG_FUNCTION_ARGS)
     905                 :             : {
     906                 :          14 :         int64           arg1 = PG_GETARG_INT64(0);
     907                 :          14 :         int32           arg2 = PG_GETARG_INT32(1);
     908                 :          14 :         int64           result;
     909                 :             : 
     910         [ +  + ]:          14 :         if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
     911   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     912                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     913                 :             :                                  errmsg("bigint out of range")));
     914                 :          26 :         PG_RETURN_INT64(result);
     915                 :          13 : }
     916                 :             : 
     917                 :             : Datum
     918                 :         391 : int84mul(PG_FUNCTION_ARGS)
     919                 :             : {
     920                 :         391 :         int64           arg1 = PG_GETARG_INT64(0);
     921                 :         391 :         int32           arg2 = PG_GETARG_INT32(1);
     922                 :         391 :         int64           result;
     923                 :             : 
     924         [ +  + ]:         391 :         if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
     925   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     926                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     927                 :             :                                  errmsg("bigint out of range")));
     928                 :         778 :         PG_RETURN_INT64(result);
     929                 :         389 : }
     930                 :             : 
     931                 :             : Datum
     932                 :          13 : int84div(PG_FUNCTION_ARGS)
     933                 :             : {
     934                 :          13 :         int64           arg1 = PG_GETARG_INT64(0);
     935                 :          13 :         int32           arg2 = PG_GETARG_INT32(1);
     936                 :          13 :         int64           result;
     937                 :             : 
     938         [ +  + ]:          13 :         if (arg2 == 0)
     939                 :             :         {
     940   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     941                 :             :                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
     942                 :             :                                  errmsg("division by zero")));
     943                 :             :                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
     944                 :           0 :                 PG_RETURN_NULL();
     945                 :           0 :         }
     946                 :             : 
     947                 :             :         /*
     948                 :             :          * INT64_MIN / -1 is problematic, since the result can't be represented on
     949                 :             :          * a two's-complement machine.  Some machines produce INT64_MIN, some
     950                 :             :          * produce zero, some throw an exception.  We can dodge the problem by
     951                 :             :          * recognizing that division by -1 is the same as negation.
     952                 :             :          */
     953         [ +  + ]:          12 :         if (arg2 == -1)
     954                 :             :         {
     955         [ -  + ]:           1 :                 if (unlikely(arg1 == PG_INT64_MIN))
     956   [ +  -  +  - ]:           1 :                         ereport(ERROR,
     957                 :             :                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     958                 :             :                                          errmsg("bigint out of range")));
     959                 :           0 :                 result = -arg1;
     960                 :           0 :                 PG_RETURN_INT64(result);
     961                 :             :         }
     962                 :             : 
     963                 :             :         /* No overflow is possible */
     964                 :             : 
     965                 :          11 :         result = arg1 / arg2;
     966                 :             : 
     967                 :          11 :         PG_RETURN_INT64(result);
     968                 :          11 : }
     969                 :             : 
     970                 :             : Datum
     971                 :         230 : int48pl(PG_FUNCTION_ARGS)
     972                 :             : {
     973                 :         230 :         int32           arg1 = PG_GETARG_INT32(0);
     974                 :         230 :         int64           arg2 = PG_GETARG_INT64(1);
     975                 :         230 :         int64           result;
     976                 :             : 
     977         [ +  + ]:         230 :         if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
     978   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     979                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     980                 :             :                                  errmsg("bigint out of range")));
     981                 :         458 :         PG_RETURN_INT64(result);
     982                 :         229 : }
     983                 :             : 
     984                 :             : Datum
     985                 :          11 : int48mi(PG_FUNCTION_ARGS)
     986                 :             : {
     987                 :          11 :         int32           arg1 = PG_GETARG_INT32(0);
     988                 :          11 :         int64           arg2 = PG_GETARG_INT64(1);
     989                 :          11 :         int64           result;
     990                 :             : 
     991         [ +  + ]:          11 :         if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
     992   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     993                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     994                 :             :                                  errmsg("bigint out of range")));
     995                 :          20 :         PG_RETURN_INT64(result);
     996                 :          10 : }
     997                 :             : 
     998                 :             : Datum
     999                 :          37 : int48mul(PG_FUNCTION_ARGS)
    1000                 :             : {
    1001                 :          37 :         int32           arg1 = PG_GETARG_INT32(0);
    1002                 :          37 :         int64           arg2 = PG_GETARG_INT64(1);
    1003                 :          37 :         int64           result;
    1004                 :             : 
    1005         [ +  + ]:          37 :         if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
    1006   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1007                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1008                 :             :                                  errmsg("bigint out of range")));
    1009                 :          72 :         PG_RETURN_INT64(result);
    1010                 :          36 : }
    1011                 :             : 
    1012                 :             : Datum
    1013                 :           6 : int48div(PG_FUNCTION_ARGS)
    1014                 :             : {
    1015                 :           6 :         int32           arg1 = PG_GETARG_INT32(0);
    1016                 :           6 :         int64           arg2 = PG_GETARG_INT64(1);
    1017                 :             : 
    1018         [ +  + ]:           6 :         if (unlikely(arg2 == 0))
    1019                 :             :         {
    1020   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1021                 :             :                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
    1022                 :             :                                  errmsg("division by zero")));
    1023                 :             :                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
    1024                 :           0 :                 PG_RETURN_NULL();
    1025                 :           0 :         }
    1026                 :             : 
    1027                 :             :         /* No overflow is possible */
    1028                 :           5 :         PG_RETURN_INT64((int64) arg1 / arg2);
    1029                 :           5 : }
    1030                 :             : 
    1031                 :             : Datum
    1032                 :           6 : int82pl(PG_FUNCTION_ARGS)
    1033                 :             : {
    1034                 :           6 :         int64           arg1 = PG_GETARG_INT64(0);
    1035                 :           6 :         int16           arg2 = PG_GETARG_INT16(1);
    1036                 :           6 :         int64           result;
    1037                 :             : 
    1038         [ +  + ]:           6 :         if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
    1039   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1040                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1041                 :             :                                  errmsg("bigint out of range")));
    1042                 :          10 :         PG_RETURN_INT64(result);
    1043                 :           5 : }
    1044                 :             : 
    1045                 :             : Datum
    1046                 :           6 : int82mi(PG_FUNCTION_ARGS)
    1047                 :             : {
    1048                 :           6 :         int64           arg1 = PG_GETARG_INT64(0);
    1049                 :           6 :         int16           arg2 = PG_GETARG_INT16(1);
    1050                 :           6 :         int64           result;
    1051                 :             : 
    1052         [ +  + ]:           6 :         if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
    1053   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1054                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1055                 :             :                                  errmsg("bigint out of range")));
    1056                 :          10 :         PG_RETURN_INT64(result);
    1057                 :           5 : }
    1058                 :             : 
    1059                 :             : Datum
    1060                 :           7 : int82mul(PG_FUNCTION_ARGS)
    1061                 :             : {
    1062                 :           7 :         int64           arg1 = PG_GETARG_INT64(0);
    1063                 :           7 :         int16           arg2 = PG_GETARG_INT16(1);
    1064                 :           7 :         int64           result;
    1065                 :             : 
    1066         [ +  + ]:           7 :         if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
    1067   [ +  -  +  - ]:           2 :                 ereport(ERROR,
    1068                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1069                 :             :                                  errmsg("bigint out of range")));
    1070                 :          10 :         PG_RETURN_INT64(result);
    1071                 :           5 : }
    1072                 :             : 
    1073                 :             : Datum
    1074                 :           7 : int82div(PG_FUNCTION_ARGS)
    1075                 :             : {
    1076                 :           7 :         int64           arg1 = PG_GETARG_INT64(0);
    1077                 :           7 :         int16           arg2 = PG_GETARG_INT16(1);
    1078                 :           7 :         int64           result;
    1079                 :             : 
    1080         [ +  + ]:           7 :         if (unlikely(arg2 == 0))
    1081                 :             :         {
    1082   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1083                 :             :                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
    1084                 :             :                                  errmsg("division by zero")));
    1085                 :             :                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
    1086                 :           0 :                 PG_RETURN_NULL();
    1087                 :           0 :         }
    1088                 :             : 
    1089                 :             :         /*
    1090                 :             :          * INT64_MIN / -1 is problematic, since the result can't be represented on
    1091                 :             :          * a two's-complement machine.  Some machines produce INT64_MIN, some
    1092                 :             :          * produce zero, some throw an exception.  We can dodge the problem by
    1093                 :             :          * recognizing that division by -1 is the same as negation.
    1094                 :             :          */
    1095         [ +  + ]:           6 :         if (arg2 == -1)
    1096                 :             :         {
    1097         [ -  + ]:           1 :                 if (unlikely(arg1 == PG_INT64_MIN))
    1098   [ +  -  +  - ]:           1 :                         ereport(ERROR,
    1099                 :             :                                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1100                 :             :                                          errmsg("bigint out of range")));
    1101                 :           0 :                 result = -arg1;
    1102                 :           0 :                 PG_RETURN_INT64(result);
    1103                 :             :         }
    1104                 :             : 
    1105                 :             :         /* No overflow is possible */
    1106                 :             : 
    1107                 :           5 :         result = arg1 / arg2;
    1108                 :             : 
    1109                 :           5 :         PG_RETURN_INT64(result);
    1110                 :           5 : }
    1111                 :             : 
    1112                 :             : Datum
    1113                 :           6 : int28pl(PG_FUNCTION_ARGS)
    1114                 :             : {
    1115                 :           6 :         int16           arg1 = PG_GETARG_INT16(0);
    1116                 :           6 :         int64           arg2 = PG_GETARG_INT64(1);
    1117                 :           6 :         int64           result;
    1118                 :             : 
    1119         [ +  + ]:           6 :         if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
    1120   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1121                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1122                 :             :                                  errmsg("bigint out of range")));
    1123                 :          10 :         PG_RETURN_INT64(result);
    1124                 :           5 : }
    1125                 :             : 
    1126                 :             : Datum
    1127                 :           6 : int28mi(PG_FUNCTION_ARGS)
    1128                 :             : {
    1129                 :           6 :         int16           arg1 = PG_GETARG_INT16(0);
    1130                 :           6 :         int64           arg2 = PG_GETARG_INT64(1);
    1131                 :           6 :         int64           result;
    1132                 :             : 
    1133         [ +  + ]:           6 :         if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
    1134   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1135                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1136                 :             :                                  errmsg("bigint out of range")));
    1137                 :          10 :         PG_RETURN_INT64(result);
    1138                 :           5 : }
    1139                 :             : 
    1140                 :             : Datum
    1141                 :           6 : int28mul(PG_FUNCTION_ARGS)
    1142                 :             : {
    1143                 :           6 :         int16           arg1 = PG_GETARG_INT16(0);
    1144                 :           6 :         int64           arg2 = PG_GETARG_INT64(1);
    1145                 :           6 :         int64           result;
    1146                 :             : 
    1147         [ +  + ]:           6 :         if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
    1148   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1149                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1150                 :             :                                  errmsg("bigint out of range")));
    1151                 :          10 :         PG_RETURN_INT64(result);
    1152                 :           5 : }
    1153                 :             : 
    1154                 :             : Datum
    1155                 :           6 : int28div(PG_FUNCTION_ARGS)
    1156                 :             : {
    1157                 :           6 :         int16           arg1 = PG_GETARG_INT16(0);
    1158                 :           6 :         int64           arg2 = PG_GETARG_INT64(1);
    1159                 :             : 
    1160         [ +  + ]:           6 :         if (unlikely(arg2 == 0))
    1161                 :             :         {
    1162   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1163                 :             :                                 (errcode(ERRCODE_DIVISION_BY_ZERO),
    1164                 :             :                                  errmsg("division by zero")));
    1165                 :             :                 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
    1166                 :           0 :                 PG_RETURN_NULL();
    1167                 :           0 :         }
    1168                 :             : 
    1169                 :             :         /* No overflow is possible */
    1170                 :           5 :         PG_RETURN_INT64((int64) arg1 / arg2);
    1171                 :           5 : }
    1172                 :             : 
    1173                 :             : /* Binary arithmetics
    1174                 :             :  *
    1175                 :             :  *              int8and         - returns arg1 & arg2
    1176                 :             :  *              int8or          - returns arg1 | arg2
    1177                 :             :  *              int8xor         - returns arg1 # arg2
    1178                 :             :  *              int8not         - returns ~arg1
    1179                 :             :  *              int8shl         - returns arg1 << arg2
    1180                 :             :  *              int8shr         - returns arg1 >> arg2
    1181                 :             :  */
    1182                 :             : 
    1183                 :             : Datum
    1184                 :           7 : int8and(PG_FUNCTION_ARGS)
    1185                 :             : {
    1186                 :           7 :         int64           arg1 = PG_GETARG_INT64(0);
    1187                 :           7 :         int64           arg2 = PG_GETARG_INT64(1);
    1188                 :             : 
    1189                 :          14 :         PG_RETURN_INT64(arg1 & arg2);
    1190                 :           7 : }
    1191                 :             : 
    1192                 :             : Datum
    1193                 :           7 : int8or(PG_FUNCTION_ARGS)
    1194                 :             : {
    1195                 :           7 :         int64           arg1 = PG_GETARG_INT64(0);
    1196                 :           7 :         int64           arg2 = PG_GETARG_INT64(1);
    1197                 :             : 
    1198                 :          14 :         PG_RETURN_INT64(arg1 | arg2);
    1199                 :           7 : }
    1200                 :             : 
    1201                 :             : Datum
    1202                 :           7 : int8xor(PG_FUNCTION_ARGS)
    1203                 :             : {
    1204                 :           7 :         int64           arg1 = PG_GETARG_INT64(0);
    1205                 :           7 :         int64           arg2 = PG_GETARG_INT64(1);
    1206                 :             : 
    1207                 :          14 :         PG_RETURN_INT64(arg1 ^ arg2);
    1208                 :           7 : }
    1209                 :             : 
    1210                 :             : Datum
    1211                 :           5 : int8not(PG_FUNCTION_ARGS)
    1212                 :             : {
    1213                 :           5 :         int64           arg1 = PG_GETARG_INT64(0);
    1214                 :             : 
    1215                 :          10 :         PG_RETURN_INT64(~arg1);
    1216                 :           5 : }
    1217                 :             : 
    1218                 :             : Datum
    1219                 :           7 : int8shl(PG_FUNCTION_ARGS)
    1220                 :             : {
    1221                 :           7 :         int64           arg1 = PG_GETARG_INT64(0);
    1222                 :           7 :         int32           arg2 = PG_GETARG_INT32(1);
    1223                 :             : 
    1224                 :          14 :         PG_RETURN_INT64(arg1 << arg2);
    1225                 :           7 : }
    1226                 :             : 
    1227                 :             : Datum
    1228                 :           5 : int8shr(PG_FUNCTION_ARGS)
    1229                 :             : {
    1230                 :           5 :         int64           arg1 = PG_GETARG_INT64(0);
    1231                 :           5 :         int32           arg2 = PG_GETARG_INT32(1);
    1232                 :             : 
    1233                 :          10 :         PG_RETURN_INT64(arg1 >> arg2);
    1234                 :           5 : }
    1235                 :             : 
    1236                 :             : /*----------------------------------------------------------
    1237                 :             :  *      Conversion operators.
    1238                 :             :  *---------------------------------------------------------*/
    1239                 :             : 
    1240                 :             : Datum
    1241                 :      177904 : int48(PG_FUNCTION_ARGS)
    1242                 :             : {
    1243                 :      177904 :         int32           arg = PG_GETARG_INT32(0);
    1244                 :             : 
    1245                 :      355808 :         PG_RETURN_INT64((int64) arg);
    1246                 :      177904 : }
    1247                 :             : 
    1248                 :             : Datum
    1249                 :       25718 : int84(PG_FUNCTION_ARGS)
    1250                 :             : {
    1251                 :       25718 :         int64           arg = PG_GETARG_INT64(0);
    1252                 :             : 
    1253         [ +  + ]:       25718 :         if (unlikely(arg < PG_INT32_MIN) || unlikely(arg > PG_INT32_MAX))
    1254   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1255                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1256                 :             :                                  errmsg("integer out of range")));
    1257                 :             : 
    1258                 :       51434 :         PG_RETURN_INT32((int32) arg);
    1259                 :       25717 : }
    1260                 :             : 
    1261                 :             : Datum
    1262                 :           4 : int28(PG_FUNCTION_ARGS)
    1263                 :             : {
    1264                 :           4 :         int16           arg = PG_GETARG_INT16(0);
    1265                 :             : 
    1266                 :           8 :         PG_RETURN_INT64((int64) arg);
    1267                 :           4 : }
    1268                 :             : 
    1269                 :             : Datum
    1270                 :           6 : int82(PG_FUNCTION_ARGS)
    1271                 :             : {
    1272                 :           6 :         int64           arg = PG_GETARG_INT64(0);
    1273                 :             : 
    1274         [ +  + ]:           6 :         if (unlikely(arg < PG_INT16_MIN) || unlikely(arg > PG_INT16_MAX))
    1275   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1276                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1277                 :             :                                  errmsg("smallint out of range")));
    1278                 :             : 
    1279                 :          10 :         PG_RETURN_INT16((int16) arg);
    1280                 :           5 : }
    1281                 :             : 
    1282                 :             : Datum
    1283                 :        2029 : i8tod(PG_FUNCTION_ARGS)
    1284                 :             : {
    1285                 :        2029 :         int64           arg = PG_GETARG_INT64(0);
    1286                 :        2029 :         float8          result;
    1287                 :             : 
    1288                 :        2029 :         result = arg;
    1289                 :             : 
    1290                 :        4058 :         PG_RETURN_FLOAT8(result);
    1291                 :        2029 : }
    1292                 :             : 
    1293                 :             : /* dtoi8()
    1294                 :             :  * Convert float8 to 8-byte integer.
    1295                 :             :  */
    1296                 :             : Datum
    1297                 :          24 : dtoi8(PG_FUNCTION_ARGS)
    1298                 :             : {
    1299                 :          24 :         float8          num = PG_GETARG_FLOAT8(0);
    1300                 :             : 
    1301                 :             :         /*
    1302                 :             :          * Get rid of any fractional part in the input.  This is so we don't fail
    1303                 :             :          * on just-out-of-range values that would round into range.  Note
    1304                 :             :          * assumption that rint() will pass through a NaN or Inf unchanged.
    1305                 :             :          */
    1306                 :          24 :         num = rint(num);
    1307                 :             : 
    1308                 :             :         /* Range check */
    1309   [ -  +  +  +  :          24 :         if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT64(num)))
          +  -  +  +  +  
                      + ]
    1310   [ +  -  +  - ]:           3 :                 ereport(ERROR,
    1311                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1312                 :             :                                  errmsg("bigint out of range")));
    1313                 :             : 
    1314                 :         138 :         PG_RETURN_INT64((int64) num);
    1315                 :          69 : }
    1316                 :             : 
    1317                 :             : Datum
    1318                 :          25 : i8tof(PG_FUNCTION_ARGS)
    1319                 :             : {
    1320                 :          25 :         int64           arg = PG_GETARG_INT64(0);
    1321                 :          25 :         float4          result;
    1322                 :             : 
    1323                 :          25 :         result = arg;
    1324                 :             : 
    1325                 :          50 :         PG_RETURN_FLOAT4(result);
    1326                 :          25 : }
    1327                 :             : 
    1328                 :             : /* ftoi8()
    1329                 :             :  * Convert float4 to 8-byte integer.
    1330                 :             :  */
    1331                 :             : Datum
    1332                 :           5 : ftoi8(PG_FUNCTION_ARGS)
    1333                 :             : {
    1334                 :           5 :         float4          num = PG_GETARG_FLOAT4(0);
    1335                 :             : 
    1336                 :             :         /*
    1337                 :             :          * Get rid of any fractional part in the input.  This is so we don't fail
    1338                 :             :          * on just-out-of-range values that would round into range.  Note
    1339                 :             :          * assumption that rint() will pass through a NaN or Inf unchanged.
    1340                 :             :          */
    1341                 :           5 :         num = rint(num);
    1342                 :             : 
    1343                 :             :         /* Range check */
    1344   [ +  -  -  +  :           5 :         if (unlikely(isnan(num) || !FLOAT4_FITS_IN_INT64(num)))
          #  #  +  +  +  
                      + ]
    1345   [ +  -  +  - ]:           2 :                 ereport(ERROR,
    1346                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1347                 :             :                                  errmsg("bigint out of range")));
    1348                 :             : 
    1349                 :           6 :         PG_RETURN_INT64((int64) num);
    1350                 :           3 : }
    1351                 :             : 
    1352                 :             : Datum
    1353                 :           3 : i8tooid(PG_FUNCTION_ARGS)
    1354                 :             : {
    1355                 :           3 :         int64           arg = PG_GETARG_INT64(0);
    1356                 :             : 
    1357         [ +  + ]:           3 :         if (unlikely(arg < 0) || unlikely(arg > PG_UINT32_MAX))
    1358   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1359                 :             :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    1360                 :             :                                  errmsg("OID out of range")));
    1361                 :             : 
    1362                 :           4 :         PG_RETURN_OID((Oid) arg);
    1363                 :           2 : }
    1364                 :             : 
    1365                 :             : Datum
    1366                 :           1 : oidtoi8(PG_FUNCTION_ARGS)
    1367                 :             : {
    1368                 :           1 :         Oid                     arg = PG_GETARG_OID(0);
    1369                 :             : 
    1370                 :           2 :         PG_RETURN_INT64((int64) arg);
    1371                 :           1 : }
    1372                 :             : 
    1373                 :             : Datum
    1374                 :           1 : oidtooid8(PG_FUNCTION_ARGS)
    1375                 :             : {
    1376                 :           1 :         Oid                     arg = PG_GETARG_OID(0);
    1377                 :             : 
    1378                 :           2 :         PG_RETURN_OID8((Oid8) arg);
    1379                 :           1 : }
    1380                 :             : 
    1381                 :             : /*
    1382                 :             :  * non-persistent numeric series generator
    1383                 :             :  */
    1384                 :             : Datum
    1385                 :          20 : generate_series_int8(PG_FUNCTION_ARGS)
    1386                 :             : {
    1387                 :          20 :         return generate_series_step_int8(fcinfo);
    1388                 :             : }
    1389                 :             : 
    1390                 :             : Datum
    1391                 :          28 : generate_series_step_int8(PG_FUNCTION_ARGS)
    1392                 :             : {
    1393                 :          28 :         FuncCallContext *funcctx;
    1394                 :          28 :         generate_series_fctx *fctx;
    1395                 :          28 :         int64           result;
    1396                 :          28 :         MemoryContext oldcontext;
    1397                 :             : 
    1398                 :             :         /* stuff done only on the first call of the function */
    1399         [ +  + ]:          28 :         if (SRF_IS_FIRSTCALL())
    1400                 :             :         {
    1401                 :           5 :                 int64           start = PG_GETARG_INT64(0);
    1402                 :           5 :                 int64           finish = PG_GETARG_INT64(1);
    1403                 :           5 :                 int64           step = 1;
    1404                 :             : 
    1405                 :             :                 /* see if we were given an explicit step size */
    1406         [ +  + ]:           5 :                 if (PG_NARGS() == 3)
    1407                 :           2 :                         step = PG_GETARG_INT64(2);
    1408         [ +  + ]:           5 :                 if (step == 0)
    1409   [ +  -  +  - ]:           1 :                         ereport(ERROR,
    1410                 :             :                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1411                 :             :                                          errmsg("step size cannot equal zero")));
    1412                 :             : 
    1413                 :             :                 /* create a function context for cross-call persistence */
    1414                 :           4 :                 funcctx = SRF_FIRSTCALL_INIT();
    1415                 :             : 
    1416                 :             :                 /*
    1417                 :             :                  * switch to memory context appropriate for multiple function calls
    1418                 :             :                  */
    1419                 :           4 :                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
    1420                 :             : 
    1421                 :             :                 /* allocate memory for user context */
    1422                 :           4 :                 fctx = palloc_object(generate_series_fctx);
    1423                 :             : 
    1424                 :             :                 /*
    1425                 :             :                  * Use fctx to keep state from call to call. Seed current with the
    1426                 :             :                  * original start value
    1427                 :             :                  */
    1428                 :           4 :                 fctx->current = start;
    1429                 :           4 :                 fctx->finish = finish;
    1430                 :           4 :                 fctx->step = step;
    1431                 :             : 
    1432                 :           4 :                 funcctx->user_fctx = fctx;
    1433                 :           4 :                 MemoryContextSwitchTo(oldcontext);
    1434                 :           4 :         }
    1435                 :             : 
    1436                 :             :         /* stuff done on every call of the function */
    1437                 :          27 :         funcctx = SRF_PERCALL_SETUP();
    1438                 :             : 
    1439                 :             :         /*
    1440                 :             :          * get the saved state and use current as the result for this iteration
    1441                 :             :          */
    1442                 :          27 :         fctx = funcctx->user_fctx;
    1443                 :          27 :         result = fctx->current;
    1444                 :             : 
    1445   [ +  -  +  + ]:          27 :         if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
    1446         [ -  + ]:           4 :                 (fctx->step < 0 && fctx->current >= fctx->finish))
    1447                 :             :         {
    1448                 :             :                 /*
    1449                 :             :                  * Increment current in preparation for next iteration. If next-value
    1450                 :             :                  * computation overflows, this is the final result.
    1451                 :             :                  */
    1452         [ +  - ]:          23 :                 if (pg_add_s64_overflow(fctx->current, fctx->step, &fctx->current))
    1453                 :           0 :                         fctx->step = 0;
    1454                 :             : 
    1455                 :             :                 /* do when there is more left to send */
    1456                 :          23 :                 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
    1457                 :           0 :         }
    1458                 :             :         else
    1459                 :             :                 /* do when there is no more left */
    1460         [ +  - ]:           4 :                 SRF_RETURN_DONE(funcctx);
    1461         [ -  + ]:          27 : }
    1462                 :             : 
    1463                 :             : /*
    1464                 :             :  * Planner support function for generate_series(int8, int8 [, int8])
    1465                 :             :  */
    1466                 :             : Datum
    1467                 :          25 : generate_series_int8_support(PG_FUNCTION_ARGS)
    1468                 :             : {
    1469                 :          25 :         Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
    1470                 :          25 :         Node       *ret = NULL;
    1471                 :             : 
    1472         [ +  + ]:          25 :         if (IsA(rawreq, SupportRequestRows))
    1473                 :             :         {
    1474                 :             :                 /* Try to estimate the number of rows returned */
    1475                 :          22 :                 SupportRequestRows *req = (SupportRequestRows *) rawreq;
    1476                 :             : 
    1477         [ +  + ]:          22 :                 if (is_funcclause(req->node))        /* be paranoid */
    1478                 :             :                 {
    1479                 :           6 :                         List       *args = ((FuncExpr *) req->node)->args;
    1480                 :           6 :                         Node       *arg1,
    1481                 :             :                                            *arg2,
    1482                 :             :                                            *arg3;
    1483                 :             : 
    1484                 :             :                         /* We can use estimated argument values here */
    1485                 :           6 :                         arg1 = estimate_expression_value(req->root, linitial(args));
    1486                 :           6 :                         arg2 = estimate_expression_value(req->root, lsecond(args));
    1487         [ +  + ]:           6 :                         if (list_length(args) >= 3)
    1488                 :           2 :                                 arg3 = estimate_expression_value(req->root, lthird(args));
    1489                 :             :                         else
    1490                 :           4 :                                 arg3 = NULL;
    1491                 :             : 
    1492                 :             :                         /*
    1493                 :             :                          * If any argument is constant NULL, we can safely assume that
    1494                 :             :                          * zero rows are returned.  Otherwise, if they're all non-NULL
    1495                 :             :                          * constants, we can calculate the number of rows that will be
    1496                 :             :                          * returned.  Use double arithmetic to avoid overflow hazards.
    1497                 :             :                          */
    1498         [ +  + ]:           6 :                         if ((IsA(arg1, Const) &&
    1499         [ +  + ]:           5 :                                  ((Const *) arg1)->constisnull) ||
    1500         [ +  + ]:          14 :                                 (IsA(arg2, Const) &&
    1501                 :           8 :                                  ((Const *) arg2)->constisnull) ||
    1502   [ +  +  +  - ]:           8 :                                 (arg3 != NULL && IsA(arg3, Const) &&
    1503                 :           2 :                                  ((Const *) arg3)->constisnull))
    1504                 :             :                         {
    1505                 :          16 :                                 req->rows = 0;
    1506                 :          16 :                                 ret = (Node *) req;
    1507                 :          16 :                         }
    1508         [ +  + ]:           6 :                         else if (IsA(arg1, Const) &&
    1509   [ +  -  +  - ]:           7 :                                          IsA(arg2, Const) &&
    1510         [ +  + ]:           5 :                                          (arg3 == NULL || IsA(arg3, Const)))
    1511                 :             :                         {
    1512                 :           5 :                                 double          start,
    1513                 :             :                                                         finish,
    1514                 :             :                                                         step;
    1515                 :             : 
    1516                 :           5 :                                 start = DatumGetInt64(((Const *) arg1)->constvalue);
    1517                 :           5 :                                 finish = DatumGetInt64(((Const *) arg2)->constvalue);
    1518         [ +  + ]:           5 :                                 step = arg3 ? DatumGetInt64(((Const *) arg3)->constvalue) : 1;
    1519                 :             : 
    1520                 :             :                                 /* This equation works for either sign of step */
    1521         [ +  + ]:           5 :                                 if (step != 0)
    1522                 :             :                                 {
    1523                 :           4 :                                         req->rows = floor((finish - start + step) / step);
    1524                 :           4 :                                         ret = (Node *) req;
    1525                 :           4 :                                 }
    1526                 :           5 :                         }
    1527                 :          22 :                 }
    1528                 :          38 :         }
    1529                 :             : 
    1530                 :          18 :         PG_RETURN_POINTER(ret);
    1531                 :           9 : }
        

Generated by: LCOV version 2.3.2-1