Cache NO ACTION foreign keys separately from RESTRICT foreign keys
[pgsql.git] / src / backend / utils / adt / int8.c
blob9dd5889f34c6280085f9888782d707500b308a66
1 /*-------------------------------------------------------------------------
3 * int8.c
4 * Internal 64-bit integer operations
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * IDENTIFICATION
10 * src/backend/utils/adt/int8.c
12 *-------------------------------------------------------------------------
14 #include "postgres.h"
16 #include <ctype.h>
17 #include <limits.h>
18 #include <math.h>
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"
29 typedef struct
31 int64 current;
32 int64 finish;
33 int64 step;
34 } generate_series_fctx;
37 /***********************************************************************
39 ** Routines for 64-bit integers.
41 ***********************************************************************/
43 /*----------------------------------------------------------
44 * Formatting and conversion routines.
45 *---------------------------------------------------------*/
47 /* int8in()
49 Datum
50 int8in(PG_FUNCTION_ARGS)
52 char *num = PG_GETARG_CSTRING(0);
54 PG_RETURN_INT64(pg_strtoint64_safe(num, fcinfo->context));
58 /* int8out()
60 Datum
61 int8out(PG_FUNCTION_ARGS)
63 int64 val = PG_GETARG_INT64(0);
64 char buf[MAXINT8LEN + 1];
65 char *result;
66 int len;
68 len = pg_lltoa(val, buf) + 1;
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().
74 result = palloc(len);
75 memcpy(result, buf, len);
76 PG_RETURN_CSTRING(result);
80 * int8recv - converts external binary format to int8
82 Datum
83 int8recv(PG_FUNCTION_ARGS)
85 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
87 PG_RETURN_INT64(pq_getmsgint64(buf));
91 * int8send - converts int8 to binary format
93 Datum
94 int8send(PG_FUNCTION_ARGS)
96 int64 arg1 = PG_GETARG_INT64(0);
97 StringInfoData buf;
99 pq_begintypsend(&buf);
100 pq_sendint64(&buf, arg1);
101 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
105 /*----------------------------------------------------------
106 * Relational operators for int8s, including cross-data-type comparisons.
107 *---------------------------------------------------------*/
109 /* int8relop()
110 * Is val1 relop val2?
112 Datum
113 int8eq(PG_FUNCTION_ARGS)
115 int64 val1 = PG_GETARG_INT64(0);
116 int64 val2 = PG_GETARG_INT64(1);
118 PG_RETURN_BOOL(val1 == val2);
121 Datum
122 int8ne(PG_FUNCTION_ARGS)
124 int64 val1 = PG_GETARG_INT64(0);
125 int64 val2 = PG_GETARG_INT64(1);
127 PG_RETURN_BOOL(val1 != val2);
130 Datum
131 int8lt(PG_FUNCTION_ARGS)
133 int64 val1 = PG_GETARG_INT64(0);
134 int64 val2 = PG_GETARG_INT64(1);
136 PG_RETURN_BOOL(val1 < val2);
139 Datum
140 int8gt(PG_FUNCTION_ARGS)
142 int64 val1 = PG_GETARG_INT64(0);
143 int64 val2 = PG_GETARG_INT64(1);
145 PG_RETURN_BOOL(val1 > val2);
148 Datum
149 int8le(PG_FUNCTION_ARGS)
151 int64 val1 = PG_GETARG_INT64(0);
152 int64 val2 = PG_GETARG_INT64(1);
154 PG_RETURN_BOOL(val1 <= val2);
157 Datum
158 int8ge(PG_FUNCTION_ARGS)
160 int64 val1 = PG_GETARG_INT64(0);
161 int64 val2 = PG_GETARG_INT64(1);
163 PG_RETURN_BOOL(val1 >= val2);
166 /* int84relop()
167 * Is 64-bit val1 relop 32-bit val2?
169 Datum
170 int84eq(PG_FUNCTION_ARGS)
172 int64 val1 = PG_GETARG_INT64(0);
173 int32 val2 = PG_GETARG_INT32(1);
175 PG_RETURN_BOOL(val1 == val2);
178 Datum
179 int84ne(PG_FUNCTION_ARGS)
181 int64 val1 = PG_GETARG_INT64(0);
182 int32 val2 = PG_GETARG_INT32(1);
184 PG_RETURN_BOOL(val1 != val2);
187 Datum
188 int84lt(PG_FUNCTION_ARGS)
190 int64 val1 = PG_GETARG_INT64(0);
191 int32 val2 = PG_GETARG_INT32(1);
193 PG_RETURN_BOOL(val1 < val2);
196 Datum
197 int84gt(PG_FUNCTION_ARGS)
199 int64 val1 = PG_GETARG_INT64(0);
200 int32 val2 = PG_GETARG_INT32(1);
202 PG_RETURN_BOOL(val1 > val2);
205 Datum
206 int84le(PG_FUNCTION_ARGS)
208 int64 val1 = PG_GETARG_INT64(0);
209 int32 val2 = PG_GETARG_INT32(1);
211 PG_RETURN_BOOL(val1 <= val2);
214 Datum
215 int84ge(PG_FUNCTION_ARGS)
217 int64 val1 = PG_GETARG_INT64(0);
218 int32 val2 = PG_GETARG_INT32(1);
220 PG_RETURN_BOOL(val1 >= val2);
223 /* int48relop()
224 * Is 32-bit val1 relop 64-bit val2?
226 Datum
227 int48eq(PG_FUNCTION_ARGS)
229 int32 val1 = PG_GETARG_INT32(0);
230 int64 val2 = PG_GETARG_INT64(1);
232 PG_RETURN_BOOL(val1 == val2);
235 Datum
236 int48ne(PG_FUNCTION_ARGS)
238 int32 val1 = PG_GETARG_INT32(0);
239 int64 val2 = PG_GETARG_INT64(1);
241 PG_RETURN_BOOL(val1 != val2);
244 Datum
245 int48lt(PG_FUNCTION_ARGS)
247 int32 val1 = PG_GETARG_INT32(0);
248 int64 val2 = PG_GETARG_INT64(1);
250 PG_RETURN_BOOL(val1 < val2);
253 Datum
254 int48gt(PG_FUNCTION_ARGS)
256 int32 val1 = PG_GETARG_INT32(0);
257 int64 val2 = PG_GETARG_INT64(1);
259 PG_RETURN_BOOL(val1 > val2);
262 Datum
263 int48le(PG_FUNCTION_ARGS)
265 int32 val1 = PG_GETARG_INT32(0);
266 int64 val2 = PG_GETARG_INT64(1);
268 PG_RETURN_BOOL(val1 <= val2);
271 Datum
272 int48ge(PG_FUNCTION_ARGS)
274 int32 val1 = PG_GETARG_INT32(0);
275 int64 val2 = PG_GETARG_INT64(1);
277 PG_RETURN_BOOL(val1 >= val2);
280 /* int82relop()
281 * Is 64-bit val1 relop 16-bit val2?
283 Datum
284 int82eq(PG_FUNCTION_ARGS)
286 int64 val1 = PG_GETARG_INT64(0);
287 int16 val2 = PG_GETARG_INT16(1);
289 PG_RETURN_BOOL(val1 == val2);
292 Datum
293 int82ne(PG_FUNCTION_ARGS)
295 int64 val1 = PG_GETARG_INT64(0);
296 int16 val2 = PG_GETARG_INT16(1);
298 PG_RETURN_BOOL(val1 != val2);
301 Datum
302 int82lt(PG_FUNCTION_ARGS)
304 int64 val1 = PG_GETARG_INT64(0);
305 int16 val2 = PG_GETARG_INT16(1);
307 PG_RETURN_BOOL(val1 < val2);
310 Datum
311 int82gt(PG_FUNCTION_ARGS)
313 int64 val1 = PG_GETARG_INT64(0);
314 int16 val2 = PG_GETARG_INT16(1);
316 PG_RETURN_BOOL(val1 > val2);
319 Datum
320 int82le(PG_FUNCTION_ARGS)
322 int64 val1 = PG_GETARG_INT64(0);
323 int16 val2 = PG_GETARG_INT16(1);
325 PG_RETURN_BOOL(val1 <= val2);
328 Datum
329 int82ge(PG_FUNCTION_ARGS)
331 int64 val1 = PG_GETARG_INT64(0);
332 int16 val2 = PG_GETARG_INT16(1);
334 PG_RETURN_BOOL(val1 >= val2);
337 /* int28relop()
338 * Is 16-bit val1 relop 64-bit val2?
340 Datum
341 int28eq(PG_FUNCTION_ARGS)
343 int16 val1 = PG_GETARG_INT16(0);
344 int64 val2 = PG_GETARG_INT64(1);
346 PG_RETURN_BOOL(val1 == val2);
349 Datum
350 int28ne(PG_FUNCTION_ARGS)
352 int16 val1 = PG_GETARG_INT16(0);
353 int64 val2 = PG_GETARG_INT64(1);
355 PG_RETURN_BOOL(val1 != val2);
358 Datum
359 int28lt(PG_FUNCTION_ARGS)
361 int16 val1 = PG_GETARG_INT16(0);
362 int64 val2 = PG_GETARG_INT64(1);
364 PG_RETURN_BOOL(val1 < val2);
367 Datum
368 int28gt(PG_FUNCTION_ARGS)
370 int16 val1 = PG_GETARG_INT16(0);
371 int64 val2 = PG_GETARG_INT64(1);
373 PG_RETURN_BOOL(val1 > val2);
376 Datum
377 int28le(PG_FUNCTION_ARGS)
379 int16 val1 = PG_GETARG_INT16(0);
380 int64 val2 = PG_GETARG_INT64(1);
382 PG_RETURN_BOOL(val1 <= val2);
385 Datum
386 int28ge(PG_FUNCTION_ARGS)
388 int16 val1 = PG_GETARG_INT16(0);
389 int64 val2 = PG_GETARG_INT64(1);
391 PG_RETURN_BOOL(val1 >= val2);
395 * in_range support function for int8.
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.
400 Datum
401 in_range_int8_int8(PG_FUNCTION_ARGS)
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;
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")));
415 if (sub)
416 offset = -offset; /* cannot overflow */
418 if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
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".
425 PG_RETURN_BOOL(sub ? !less : less);
428 if (less)
429 PG_RETURN_BOOL(val <= sum);
430 else
431 PG_RETURN_BOOL(val >= sum);
435 /*----------------------------------------------------------
436 * Arithmetic operators on 64-bit integers.
437 *---------------------------------------------------------*/
439 Datum
440 int8um(PG_FUNCTION_ARGS)
442 int64 arg = PG_GETARG_INT64(0);
443 int64 result;
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);
453 Datum
454 int8up(PG_FUNCTION_ARGS)
456 int64 arg = PG_GETARG_INT64(0);
458 PG_RETURN_INT64(arg);
461 Datum
462 int8pl(PG_FUNCTION_ARGS)
464 int64 arg1 = PG_GETARG_INT64(0);
465 int64 arg2 = PG_GETARG_INT64(1);
466 int64 result;
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);
475 Datum
476 int8mi(PG_FUNCTION_ARGS)
478 int64 arg1 = PG_GETARG_INT64(0);
479 int64 arg2 = PG_GETARG_INT64(1);
480 int64 result;
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);
489 Datum
490 int8mul(PG_FUNCTION_ARGS)
492 int64 arg1 = PG_GETARG_INT64(0);
493 int64 arg2 = PG_GETARG_INT64(1);
494 int64 result;
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);
503 Datum
504 int8div(PG_FUNCTION_ARGS)
506 int64 arg1 = PG_GETARG_INT64(0);
507 int64 arg2 = PG_GETARG_INT64(1);
508 int64 result;
510 if (arg2 == 0)
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();
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.
525 if (arg2 == -1)
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);
535 /* No overflow is possible */
537 result = arg1 / arg2;
539 PG_RETURN_INT64(result);
542 /* int8abs()
543 * Absolute value
545 Datum
546 int8abs(PG_FUNCTION_ARGS)
548 int64 arg1 = PG_GETARG_INT64(0);
549 int64 result;
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);
559 /* int8mod()
560 * Modulo operation.
562 Datum
563 int8mod(PG_FUNCTION_ARGS)
565 int64 arg1 = PG_GETARG_INT64(0);
566 int64 arg2 = PG_GETARG_INT64(1);
568 if (unlikely(arg2 == 0))
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();
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.
582 if (arg2 == -1)
583 PG_RETURN_INT64(0);
585 /* No overflow is possible */
587 PG_RETURN_INT64(arg1 % arg2);
591 * Greatest Common Divisor
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
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.
605 static int64
606 int8gcd_internal(int64 arg1, int64 arg2)
608 int64 swap;
609 int64 a1,
613 * Put the greater absolute value in arg1.
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.
619 * We do this in negative space in order to handle INT64_MIN.
621 a1 = (arg1 < 0) ? arg1 : -arg1;
622 a2 = (arg2 < 0) ? arg2 : -arg2;
623 if (a1 > a2)
625 swap = arg1;
626 arg1 = arg2;
627 arg2 = swap;
630 /* Special care needs to be taken with INT64_MIN. See comments above. */
631 if (arg1 == PG_INT64_MIN)
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")));
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.
644 if (arg2 == -1)
645 return 1;
648 /* Use the Euclidean algorithm to find the GCD */
649 while (arg2 != 0)
651 swap = arg2;
652 arg2 = arg1 % arg2;
653 arg1 = swap;
657 * Make sure the result is positive. (We know we don't have INT64_MIN
658 * anymore).
660 if (arg1 < 0)
661 arg1 = -arg1;
663 return arg1;
666 Datum
667 int8gcd(PG_FUNCTION_ARGS)
669 int64 arg1 = PG_GETARG_INT64(0);
670 int64 arg2 = PG_GETARG_INT64(1);
671 int64 result;
673 result = int8gcd_internal(arg1, arg2);
675 PG_RETURN_INT64(result);
679 * Least Common Multiple
681 Datum
682 int8lcm(PG_FUNCTION_ARGS)
684 int64 arg1 = PG_GETARG_INT64(0);
685 int64 arg2 = PG_GETARG_INT64(1);
686 int64 gcd;
687 int64 result;
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.
694 if (arg1 == 0 || arg2 == 0)
695 PG_RETURN_INT64(0);
697 /* lcm(x, y) = abs(x / gcd(x, y) * y) */
698 gcd = int8gcd_internal(arg1, arg2);
699 arg1 = arg1 / gcd;
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")));
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")));
712 if (result < 0)
713 result = -result;
715 PG_RETURN_INT64(result);
718 Datum
719 int8inc(PG_FUNCTION_ARGS)
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.)
728 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
729 if (AggCheckCallContext(fcinfo, NULL))
731 int64 *arg = (int64 *) PG_GETARG_POINTER(0);
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")));
738 PG_RETURN_POINTER(arg);
740 else
741 #endif
743 /* Not called as an aggregate, so just do it the dumb way */
744 int64 arg = PG_GETARG_INT64(0);
745 int64 result;
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")));
752 PG_RETURN_INT64(result);
756 Datum
757 int8dec(PG_FUNCTION_ARGS)
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.)
766 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
767 if (AggCheckCallContext(fcinfo, NULL))
769 int64 *arg = (int64 *) PG_GETARG_POINTER(0);
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);
777 else
778 #endif
780 /* Not called as an aggregate, so just do it the dumb way */
781 int64 arg = PG_GETARG_INT64(0);
782 int64 result;
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")));
789 PG_RETURN_INT64(result);
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.
803 Datum
804 int8inc_any(PG_FUNCTION_ARGS)
806 return int8inc(fcinfo);
809 Datum
810 int8inc_float8_float8(PG_FUNCTION_ARGS)
812 return int8inc(fcinfo);
815 Datum
816 int8dec_any(PG_FUNCTION_ARGS)
818 return int8dec(fcinfo);
822 * int8inc_support
823 * prosupport function for int8inc() and int8inc_any()
825 Datum
826 int8inc_support(PG_FUNCTION_ARGS)
828 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
830 if (IsA(rawreq, SupportRequestWFuncMonotonic))
832 SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
833 MonotonicFunction monotonic = MONOTONICFUNC_NONE;
834 int frameOptions = req->window_clause->frameOptions;
836 /* No ORDER BY clause then all rows are peers */
837 if (req->window_clause->orderClause == NIL)
838 monotonic = MONOTONICFUNC_BOTH;
839 else
842 * Otherwise take into account the frame options. When the frame
843 * bound is the start of the window then the resulting value can
844 * never decrease, therefore is monotonically increasing
846 if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
847 monotonic |= MONOTONICFUNC_INCREASING;
850 * Likewise, if the frame bound is the end of the window then the
851 * resulting value can never decrease.
853 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
854 monotonic |= MONOTONICFUNC_DECREASING;
857 req->monotonic = monotonic;
858 PG_RETURN_POINTER(req);
861 PG_RETURN_POINTER(NULL);
865 Datum
866 int8larger(PG_FUNCTION_ARGS)
868 int64 arg1 = PG_GETARG_INT64(0);
869 int64 arg2 = PG_GETARG_INT64(1);
870 int64 result;
872 result = ((arg1 > arg2) ? arg1 : arg2);
874 PG_RETURN_INT64(result);
877 Datum
878 int8smaller(PG_FUNCTION_ARGS)
880 int64 arg1 = PG_GETARG_INT64(0);
881 int64 arg2 = PG_GETARG_INT64(1);
882 int64 result;
884 result = ((arg1 < arg2) ? arg1 : arg2);
886 PG_RETURN_INT64(result);
889 Datum
890 int84pl(PG_FUNCTION_ARGS)
892 int64 arg1 = PG_GETARG_INT64(0);
893 int32 arg2 = PG_GETARG_INT32(1);
894 int64 result;
896 if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
897 ereport(ERROR,
898 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
899 errmsg("bigint out of range")));
900 PG_RETURN_INT64(result);
903 Datum
904 int84mi(PG_FUNCTION_ARGS)
906 int64 arg1 = PG_GETARG_INT64(0);
907 int32 arg2 = PG_GETARG_INT32(1);
908 int64 result;
910 if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
911 ereport(ERROR,
912 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
913 errmsg("bigint out of range")));
914 PG_RETURN_INT64(result);
917 Datum
918 int84mul(PG_FUNCTION_ARGS)
920 int64 arg1 = PG_GETARG_INT64(0);
921 int32 arg2 = PG_GETARG_INT32(1);
922 int64 result;
924 if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
925 ereport(ERROR,
926 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
927 errmsg("bigint out of range")));
928 PG_RETURN_INT64(result);
931 Datum
932 int84div(PG_FUNCTION_ARGS)
934 int64 arg1 = PG_GETARG_INT64(0);
935 int32 arg2 = PG_GETARG_INT32(1);
936 int64 result;
938 if (arg2 == 0)
940 ereport(ERROR,
941 (errcode(ERRCODE_DIVISION_BY_ZERO),
942 errmsg("division by zero")));
943 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
944 PG_RETURN_NULL();
948 * INT64_MIN / -1 is problematic, since the result can't be represented on
949 * a two's-complement machine. Some machines produce INT64_MIN, some
950 * produce zero, some throw an exception. We can dodge the problem by
951 * recognizing that division by -1 is the same as negation.
953 if (arg2 == -1)
955 if (unlikely(arg1 == PG_INT64_MIN))
956 ereport(ERROR,
957 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
958 errmsg("bigint out of range")));
959 result = -arg1;
960 PG_RETURN_INT64(result);
963 /* No overflow is possible */
965 result = arg1 / arg2;
967 PG_RETURN_INT64(result);
970 Datum
971 int48pl(PG_FUNCTION_ARGS)
973 int32 arg1 = PG_GETARG_INT32(0);
974 int64 arg2 = PG_GETARG_INT64(1);
975 int64 result;
977 if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
978 ereport(ERROR,
979 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
980 errmsg("bigint out of range")));
981 PG_RETURN_INT64(result);
984 Datum
985 int48mi(PG_FUNCTION_ARGS)
987 int32 arg1 = PG_GETARG_INT32(0);
988 int64 arg2 = PG_GETARG_INT64(1);
989 int64 result;
991 if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
992 ereport(ERROR,
993 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
994 errmsg("bigint out of range")));
995 PG_RETURN_INT64(result);
998 Datum
999 int48mul(PG_FUNCTION_ARGS)
1001 int32 arg1 = PG_GETARG_INT32(0);
1002 int64 arg2 = PG_GETARG_INT64(1);
1003 int64 result;
1005 if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
1006 ereport(ERROR,
1007 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1008 errmsg("bigint out of range")));
1009 PG_RETURN_INT64(result);
1012 Datum
1013 int48div(PG_FUNCTION_ARGS)
1015 int32 arg1 = PG_GETARG_INT32(0);
1016 int64 arg2 = PG_GETARG_INT64(1);
1018 if (unlikely(arg2 == 0))
1020 ereport(ERROR,
1021 (errcode(ERRCODE_DIVISION_BY_ZERO),
1022 errmsg("division by zero")));
1023 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1024 PG_RETURN_NULL();
1027 /* No overflow is possible */
1028 PG_RETURN_INT64((int64) arg1 / arg2);
1031 Datum
1032 int82pl(PG_FUNCTION_ARGS)
1034 int64 arg1 = PG_GETARG_INT64(0);
1035 int16 arg2 = PG_GETARG_INT16(1);
1036 int64 result;
1038 if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
1039 ereport(ERROR,
1040 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1041 errmsg("bigint out of range")));
1042 PG_RETURN_INT64(result);
1045 Datum
1046 int82mi(PG_FUNCTION_ARGS)
1048 int64 arg1 = PG_GETARG_INT64(0);
1049 int16 arg2 = PG_GETARG_INT16(1);
1050 int64 result;
1052 if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
1053 ereport(ERROR,
1054 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1055 errmsg("bigint out of range")));
1056 PG_RETURN_INT64(result);
1059 Datum
1060 int82mul(PG_FUNCTION_ARGS)
1062 int64 arg1 = PG_GETARG_INT64(0);
1063 int16 arg2 = PG_GETARG_INT16(1);
1064 int64 result;
1066 if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
1067 ereport(ERROR,
1068 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1069 errmsg("bigint out of range")));
1070 PG_RETURN_INT64(result);
1073 Datum
1074 int82div(PG_FUNCTION_ARGS)
1076 int64 arg1 = PG_GETARG_INT64(0);
1077 int16 arg2 = PG_GETARG_INT16(1);
1078 int64 result;
1080 if (unlikely(arg2 == 0))
1082 ereport(ERROR,
1083 (errcode(ERRCODE_DIVISION_BY_ZERO),
1084 errmsg("division by zero")));
1085 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1086 PG_RETURN_NULL();
1090 * INT64_MIN / -1 is problematic, since the result can't be represented on
1091 * a two's-complement machine. Some machines produce INT64_MIN, some
1092 * produce zero, some throw an exception. We can dodge the problem by
1093 * recognizing that division by -1 is the same as negation.
1095 if (arg2 == -1)
1097 if (unlikely(arg1 == PG_INT64_MIN))
1098 ereport(ERROR,
1099 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1100 errmsg("bigint out of range")));
1101 result = -arg1;
1102 PG_RETURN_INT64(result);
1105 /* No overflow is possible */
1107 result = arg1 / arg2;
1109 PG_RETURN_INT64(result);
1112 Datum
1113 int28pl(PG_FUNCTION_ARGS)
1115 int16 arg1 = PG_GETARG_INT16(0);
1116 int64 arg2 = PG_GETARG_INT64(1);
1117 int64 result;
1119 if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
1120 ereport(ERROR,
1121 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1122 errmsg("bigint out of range")));
1123 PG_RETURN_INT64(result);
1126 Datum
1127 int28mi(PG_FUNCTION_ARGS)
1129 int16 arg1 = PG_GETARG_INT16(0);
1130 int64 arg2 = PG_GETARG_INT64(1);
1131 int64 result;
1133 if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
1134 ereport(ERROR,
1135 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1136 errmsg("bigint out of range")));
1137 PG_RETURN_INT64(result);
1140 Datum
1141 int28mul(PG_FUNCTION_ARGS)
1143 int16 arg1 = PG_GETARG_INT16(0);
1144 int64 arg2 = PG_GETARG_INT64(1);
1145 int64 result;
1147 if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
1148 ereport(ERROR,
1149 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1150 errmsg("bigint out of range")));
1151 PG_RETURN_INT64(result);
1154 Datum
1155 int28div(PG_FUNCTION_ARGS)
1157 int16 arg1 = PG_GETARG_INT16(0);
1158 int64 arg2 = PG_GETARG_INT64(1);
1160 if (unlikely(arg2 == 0))
1162 ereport(ERROR,
1163 (errcode(ERRCODE_DIVISION_BY_ZERO),
1164 errmsg("division by zero")));
1165 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1166 PG_RETURN_NULL();
1169 /* No overflow is possible */
1170 PG_RETURN_INT64((int64) arg1 / arg2);
1173 /* Binary arithmetics
1175 * int8and - returns arg1 & arg2
1176 * int8or - returns arg1 | arg2
1177 * int8xor - returns arg1 # arg2
1178 * int8not - returns ~arg1
1179 * int8shl - returns arg1 << arg2
1180 * int8shr - returns arg1 >> arg2
1183 Datum
1184 int8and(PG_FUNCTION_ARGS)
1186 int64 arg1 = PG_GETARG_INT64(0);
1187 int64 arg2 = PG_GETARG_INT64(1);
1189 PG_RETURN_INT64(arg1 & arg2);
1192 Datum
1193 int8or(PG_FUNCTION_ARGS)
1195 int64 arg1 = PG_GETARG_INT64(0);
1196 int64 arg2 = PG_GETARG_INT64(1);
1198 PG_RETURN_INT64(arg1 | arg2);
1201 Datum
1202 int8xor(PG_FUNCTION_ARGS)
1204 int64 arg1 = PG_GETARG_INT64(0);
1205 int64 arg2 = PG_GETARG_INT64(1);
1207 PG_RETURN_INT64(arg1 ^ arg2);
1210 Datum
1211 int8not(PG_FUNCTION_ARGS)
1213 int64 arg1 = PG_GETARG_INT64(0);
1215 PG_RETURN_INT64(~arg1);
1218 Datum
1219 int8shl(PG_FUNCTION_ARGS)
1221 int64 arg1 = PG_GETARG_INT64(0);
1222 int32 arg2 = PG_GETARG_INT32(1);
1224 PG_RETURN_INT64(arg1 << arg2);
1227 Datum
1228 int8shr(PG_FUNCTION_ARGS)
1230 int64 arg1 = PG_GETARG_INT64(0);
1231 int32 arg2 = PG_GETARG_INT32(1);
1233 PG_RETURN_INT64(arg1 >> arg2);
1236 /*----------------------------------------------------------
1237 * Conversion operators.
1238 *---------------------------------------------------------*/
1240 Datum
1241 int48(PG_FUNCTION_ARGS)
1243 int32 arg = PG_GETARG_INT32(0);
1245 PG_RETURN_INT64((int64) arg);
1248 Datum
1249 int84(PG_FUNCTION_ARGS)
1251 int64 arg = PG_GETARG_INT64(0);
1253 if (unlikely(arg < PG_INT32_MIN) || unlikely(arg > PG_INT32_MAX))
1254 ereport(ERROR,
1255 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1256 errmsg("integer out of range")));
1258 PG_RETURN_INT32((int32) arg);
1261 Datum
1262 int28(PG_FUNCTION_ARGS)
1264 int16 arg = PG_GETARG_INT16(0);
1266 PG_RETURN_INT64((int64) arg);
1269 Datum
1270 int82(PG_FUNCTION_ARGS)
1272 int64 arg = PG_GETARG_INT64(0);
1274 if (unlikely(arg < PG_INT16_MIN) || unlikely(arg > PG_INT16_MAX))
1275 ereport(ERROR,
1276 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1277 errmsg("smallint out of range")));
1279 PG_RETURN_INT16((int16) arg);
1282 Datum
1283 i8tod(PG_FUNCTION_ARGS)
1285 int64 arg = PG_GETARG_INT64(0);
1286 float8 result;
1288 result = arg;
1290 PG_RETURN_FLOAT8(result);
1293 /* dtoi8()
1294 * Convert float8 to 8-byte integer.
1296 Datum
1297 dtoi8(PG_FUNCTION_ARGS)
1299 float8 num = PG_GETARG_FLOAT8(0);
1302 * Get rid of any fractional part in the input. This is so we don't fail
1303 * on just-out-of-range values that would round into range. Note
1304 * assumption that rint() will pass through a NaN or Inf unchanged.
1306 num = rint(num);
1308 /* Range check */
1309 if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT64(num)))
1310 ereport(ERROR,
1311 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1312 errmsg("bigint out of range")));
1314 PG_RETURN_INT64((int64) num);
1317 Datum
1318 i8tof(PG_FUNCTION_ARGS)
1320 int64 arg = PG_GETARG_INT64(0);
1321 float4 result;
1323 result = arg;
1325 PG_RETURN_FLOAT4(result);
1328 /* ftoi8()
1329 * Convert float4 to 8-byte integer.
1331 Datum
1332 ftoi8(PG_FUNCTION_ARGS)
1334 float4 num = PG_GETARG_FLOAT4(0);
1337 * Get rid of any fractional part in the input. This is so we don't fail
1338 * on just-out-of-range values that would round into range. Note
1339 * assumption that rint() will pass through a NaN or Inf unchanged.
1341 num = rint(num);
1343 /* Range check */
1344 if (unlikely(isnan(num) || !FLOAT4_FITS_IN_INT64(num)))
1345 ereport(ERROR,
1346 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1347 errmsg("bigint out of range")));
1349 PG_RETURN_INT64((int64) num);
1352 Datum
1353 i8tooid(PG_FUNCTION_ARGS)
1355 int64 arg = PG_GETARG_INT64(0);
1357 if (unlikely(arg < 0) || unlikely(arg > PG_UINT32_MAX))
1358 ereport(ERROR,
1359 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1360 errmsg("OID out of range")));
1362 PG_RETURN_OID((Oid) arg);
1365 Datum
1366 oidtoi8(PG_FUNCTION_ARGS)
1368 Oid arg = PG_GETARG_OID(0);
1370 PG_RETURN_INT64((int64) arg);
1374 * non-persistent numeric series generator
1376 Datum
1377 generate_series_int8(PG_FUNCTION_ARGS)
1379 return generate_series_step_int8(fcinfo);
1382 Datum
1383 generate_series_step_int8(PG_FUNCTION_ARGS)
1385 FuncCallContext *funcctx;
1386 generate_series_fctx *fctx;
1387 int64 result;
1388 MemoryContext oldcontext;
1390 /* stuff done only on the first call of the function */
1391 if (SRF_IS_FIRSTCALL())
1393 int64 start = PG_GETARG_INT64(0);
1394 int64 finish = PG_GETARG_INT64(1);
1395 int64 step = 1;
1397 /* see if we were given an explicit step size */
1398 if (PG_NARGS() == 3)
1399 step = PG_GETARG_INT64(2);
1400 if (step == 0)
1401 ereport(ERROR,
1402 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1403 errmsg("step size cannot equal zero")));
1405 /* create a function context for cross-call persistence */
1406 funcctx = SRF_FIRSTCALL_INIT();
1409 * switch to memory context appropriate for multiple function calls
1411 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1413 /* allocate memory for user context */
1414 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1417 * Use fctx to keep state from call to call. Seed current with the
1418 * original start value
1420 fctx->current = start;
1421 fctx->finish = finish;
1422 fctx->step = step;
1424 funcctx->user_fctx = fctx;
1425 MemoryContextSwitchTo(oldcontext);
1428 /* stuff done on every call of the function */
1429 funcctx = SRF_PERCALL_SETUP();
1432 * get the saved state and use current as the result for this iteration
1434 fctx = funcctx->user_fctx;
1435 result = fctx->current;
1437 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1438 (fctx->step < 0 && fctx->current >= fctx->finish))
1441 * Increment current in preparation for next iteration. If next-value
1442 * computation overflows, this is the final result.
1444 if (pg_add_s64_overflow(fctx->current, fctx->step, &fctx->current))
1445 fctx->step = 0;
1447 /* do when there is more left to send */
1448 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1450 else
1451 /* do when there is no more left */
1452 SRF_RETURN_DONE(funcctx);
1456 * Planner support function for generate_series(int8, int8 [, int8])
1458 Datum
1459 generate_series_int8_support(PG_FUNCTION_ARGS)
1461 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1462 Node *ret = NULL;
1464 if (IsA(rawreq, SupportRequestRows))
1466 /* Try to estimate the number of rows returned */
1467 SupportRequestRows *req = (SupportRequestRows *) rawreq;
1469 if (is_funcclause(req->node)) /* be paranoid */
1471 List *args = ((FuncExpr *) req->node)->args;
1472 Node *arg1,
1473 *arg2,
1474 *arg3;
1476 /* We can use estimated argument values here */
1477 arg1 = estimate_expression_value(req->root, linitial(args));
1478 arg2 = estimate_expression_value(req->root, lsecond(args));
1479 if (list_length(args) >= 3)
1480 arg3 = estimate_expression_value(req->root, lthird(args));
1481 else
1482 arg3 = NULL;
1485 * If any argument is constant NULL, we can safely assume that
1486 * zero rows are returned. Otherwise, if they're all non-NULL
1487 * constants, we can calculate the number of rows that will be
1488 * returned. Use double arithmetic to avoid overflow hazards.
1490 if ((IsA(arg1, Const) &&
1491 ((Const *) arg1)->constisnull) ||
1492 (IsA(arg2, Const) &&
1493 ((Const *) arg2)->constisnull) ||
1494 (arg3 != NULL && IsA(arg3, Const) &&
1495 ((Const *) arg3)->constisnull))
1497 req->rows = 0;
1498 ret = (Node *) req;
1500 else if (IsA(arg1, Const) &&
1501 IsA(arg2, Const) &&
1502 (arg3 == NULL || IsA(arg3, Const)))
1504 double start,
1505 finish,
1506 step;
1508 start = DatumGetInt64(((Const *) arg1)->constvalue);
1509 finish = DatumGetInt64(((Const *) arg2)->constvalue);
1510 step = arg3 ? DatumGetInt64(((Const *) arg3)->constvalue) : 1;
1512 /* This equation works for either sign of step */
1513 if (step != 0)
1515 req->rows = floor((finish - start + step) / step);
1516 ret = (Node *) req;
1522 PG_RETURN_POINTER(ret);