LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - int8.c (source / functions) Coverage Total Hit UBC CBC
Current: 77aeca80249c9e640c811e80633a2e334a9320de vs 38afc3dcb25c45b744d4025029ce0a6c90b7059f Lines: 97.8 % 548 536 12 536
Current Date: 2026-07-25 19:08:27 +0900 Functions: 100.0 % 90 90 90
Baseline: lcov-20260725-baseline Branches: 77.2 % 290 224 66 224
Baseline Date: 2026-07-25 19:09:19 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 9 9 9
(30,360] days: 94.6 % 37 35 2 35
(360..) days: 98.0 % 502 492 10 492
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 89 89 89
Branch coverage date bins:
(7,30] days: 100.0 % 12 12 12
(30,360] days: 61.1 % 36 22 14 22
(360..) days: 78.5 % 242 190 52 190

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

Generated by: LCOV version 2.0-1