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