1 /*-------------------------------------------------------------------------
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
10 * src/backend/utils/adt/int8.c
12 *-------------------------------------------------------------------------
20 #include "common/int.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"
34 } generate_series_fctx
;
37 /***********************************************************************
39 ** Routines for 64-bit integers.
41 ***********************************************************************/
43 /*----------------------------------------------------------
44 * Formatting and conversion routines.
45 *---------------------------------------------------------*/
50 int8in(PG_FUNCTION_ARGS
)
52 char *num
= PG_GETARG_CSTRING(0);
54 PG_RETURN_INT64(pg_strtoint64_safe(num
, fcinfo
->context
));
61 int8out(PG_FUNCTION_ARGS
)
63 int64 val
= PG_GETARG_INT64(0);
64 char buf
[MAXINT8LEN
+ 1];
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().
75 memcpy(result
, buf
, len
);
76 PG_RETURN_CSTRING(result
);
80 * int8recv - converts external binary format to int8
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
94 int8send(PG_FUNCTION_ARGS
)
96 int64 arg1
= PG_GETARG_INT64(0);
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 *---------------------------------------------------------*/
110 * Is val1 relop val2?
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
);
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
);
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
);
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
);
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
);
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
);
167 * Is 64-bit val1 relop 32-bit val2?
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
);
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
);
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
);
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
);
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
);
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
);
224 * Is 32-bit val1 relop 64-bit val2?
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
);
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
);
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
);
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
);
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
);
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
);
281 * Is 64-bit val1 relop 16-bit val2?
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
);
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
);
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
);
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
);
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
);
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
);
338 * Is 16-bit val1 relop 64-bit val2?
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
);
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
);
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
);
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
);
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
);
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.
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);
412 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE
),
413 errmsg("invalid preceding or following size in window function")));
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
);
429 PG_RETURN_BOOL(val
<= sum
);
431 PG_RETURN_BOOL(val
>= sum
);
435 /*----------------------------------------------------------
436 * Arithmetic operators on 64-bit integers.
437 *---------------------------------------------------------*/
440 int8um(PG_FUNCTION_ARGS
)
442 int64 arg
= PG_GETARG_INT64(0);
445 if (unlikely(arg
== PG_INT64_MIN
))
447 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
448 errmsg("bigint out of range")));
450 PG_RETURN_INT64(result
);
454 int8up(PG_FUNCTION_ARGS
)
456 int64 arg
= PG_GETARG_INT64(0);
458 PG_RETURN_INT64(arg
);
462 int8pl(PG_FUNCTION_ARGS
)
464 int64 arg1
= PG_GETARG_INT64(0);
465 int64 arg2
= PG_GETARG_INT64(1);
468 if (unlikely(pg_add_s64_overflow(arg1
, arg2
, &result
)))
470 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
471 errmsg("bigint out of range")));
472 PG_RETURN_INT64(result
);
476 int8mi(PG_FUNCTION_ARGS
)
478 int64 arg1
= PG_GETARG_INT64(0);
479 int64 arg2
= PG_GETARG_INT64(1);
482 if (unlikely(pg_sub_s64_overflow(arg1
, arg2
, &result
)))
484 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
485 errmsg("bigint out of range")));
486 PG_RETURN_INT64(result
);
490 int8mul(PG_FUNCTION_ARGS
)
492 int64 arg1
= PG_GETARG_INT64(0);
493 int64 arg2
= PG_GETARG_INT64(1);
496 if (unlikely(pg_mul_s64_overflow(arg1
, arg2
, &result
)))
498 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
499 errmsg("bigint out of range")));
500 PG_RETURN_INT64(result
);
504 int8div(PG_FUNCTION_ARGS
)
506 int64 arg1
= PG_GETARG_INT64(0);
507 int64 arg2
= PG_GETARG_INT64(1);
513 (errcode(ERRCODE_DIVISION_BY_ZERO
),
514 errmsg("division by zero")));
515 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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.
527 if (unlikely(arg1
== PG_INT64_MIN
))
529 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
530 errmsg("bigint out of range")));
532 PG_RETURN_INT64(result
);
535 /* No overflow is possible */
537 result
= arg1
/ arg2
;
539 PG_RETURN_INT64(result
);
546 int8abs(PG_FUNCTION_ARGS
)
548 int64 arg1
= PG_GETARG_INT64(0);
551 if (unlikely(arg1
== PG_INT64_MIN
))
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
);
563 int8mod(PG_FUNCTION_ARGS
)
565 int64 arg1
= PG_GETARG_INT64(0);
566 int64 arg2
= PG_GETARG_INT64(1);
568 if (unlikely(arg2
== 0))
571 (errcode(ERRCODE_DIVISION_BY_ZERO
),
572 errmsg("division by zero")));
573 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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.
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.
595 * - gcd(x, 0) = gcd(0, x) = abs(x)
596 * because 0 is divisible by anything
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
606 int8gcd_internal(int64 arg1
, int64 arg2
)
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
;
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
)
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.
648 /* Use the Euclidean algorithm to find the GCD */
657 * Make sure the result is positive. (We know we don't have INT64_MIN
667 int8gcd(PG_FUNCTION_ARGS
)
669 int64 arg1
= PG_GETARG_INT64(0);
670 int64 arg2
= PG_GETARG_INT64(1);
673 result
= int8gcd_internal(arg1
, arg2
);
675 PG_RETURN_INT64(result
);
679 * Least Common Multiple
682 int8lcm(PG_FUNCTION_ARGS
)
684 int64 arg1
= PG_GETARG_INT64(0);
685 int64 arg2
= PG_GETARG_INT64(1);
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)
697 /* lcm(x, y) = abs(x / gcd(x, y) * y) */
698 gcd
= int8gcd_internal(arg1
, arg2
);
701 if (unlikely(pg_mul_s64_overflow(arg1
, arg2
, &result
)))
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
))
709 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
710 errmsg("bigint out of range")));
715 PG_RETURN_INT64(result
);
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
)))
735 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
736 errmsg("bigint out of range")));
738 PG_RETURN_POINTER(arg
);
743 /* Not called as an aggregate, so just do it the dumb way */
744 int64 arg
= PG_GETARG_INT64(0);
747 if (unlikely(pg_add_s64_overflow(arg
, 1, &result
)))
749 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
750 errmsg("bigint out of range")));
752 PG_RETURN_INT64(result
);
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
)))
773 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
774 errmsg("bigint out of range")));
775 PG_RETURN_POINTER(arg
);
780 /* Not called as an aggregate, so just do it the dumb way */
781 int64 arg
= PG_GETARG_INT64(0);
784 if (unlikely(pg_sub_s64_overflow(arg
, 1, &result
)))
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.
804 int8inc_any(PG_FUNCTION_ARGS
)
806 return int8inc(fcinfo
);
810 int8inc_float8_float8(PG_FUNCTION_ARGS
)
812 return int8inc(fcinfo
);
816 int8dec_any(PG_FUNCTION_ARGS
)
818 return int8dec(fcinfo
);
823 * prosupport function for int8inc() and int8inc_any()
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
;
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
);
866 int8larger(PG_FUNCTION_ARGS
)
868 int64 arg1
= PG_GETARG_INT64(0);
869 int64 arg2
= PG_GETARG_INT64(1);
872 result
= ((arg1
> arg2
) ? arg1
: arg2
);
874 PG_RETURN_INT64(result
);
878 int8smaller(PG_FUNCTION_ARGS
)
880 int64 arg1
= PG_GETARG_INT64(0);
881 int64 arg2
= PG_GETARG_INT64(1);
884 result
= ((arg1
< arg2
) ? arg1
: arg2
);
886 PG_RETURN_INT64(result
);
890 int84pl(PG_FUNCTION_ARGS
)
892 int64 arg1
= PG_GETARG_INT64(0);
893 int32 arg2
= PG_GETARG_INT32(1);
896 if (unlikely(pg_add_s64_overflow(arg1
, (int64
) arg2
, &result
)))
898 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
899 errmsg("bigint out of range")));
900 PG_RETURN_INT64(result
);
904 int84mi(PG_FUNCTION_ARGS
)
906 int64 arg1
= PG_GETARG_INT64(0);
907 int32 arg2
= PG_GETARG_INT32(1);
910 if (unlikely(pg_sub_s64_overflow(arg1
, (int64
) arg2
, &result
)))
912 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
913 errmsg("bigint out of range")));
914 PG_RETURN_INT64(result
);
918 int84mul(PG_FUNCTION_ARGS
)
920 int64 arg1
= PG_GETARG_INT64(0);
921 int32 arg2
= PG_GETARG_INT32(1);
924 if (unlikely(pg_mul_s64_overflow(arg1
, (int64
) arg2
, &result
)))
926 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
927 errmsg("bigint out of range")));
928 PG_RETURN_INT64(result
);
932 int84div(PG_FUNCTION_ARGS
)
934 int64 arg1
= PG_GETARG_INT64(0);
935 int32 arg2
= PG_GETARG_INT32(1);
941 (errcode(ERRCODE_DIVISION_BY_ZERO
),
942 errmsg("division by zero")));
943 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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.
955 if (unlikely(arg1
== PG_INT64_MIN
))
957 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
958 errmsg("bigint out of range")));
960 PG_RETURN_INT64(result
);
963 /* No overflow is possible */
965 result
= arg1
/ arg2
;
967 PG_RETURN_INT64(result
);
971 int48pl(PG_FUNCTION_ARGS
)
973 int32 arg1
= PG_GETARG_INT32(0);
974 int64 arg2
= PG_GETARG_INT64(1);
977 if (unlikely(pg_add_s64_overflow((int64
) arg1
, arg2
, &result
)))
979 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
980 errmsg("bigint out of range")));
981 PG_RETURN_INT64(result
);
985 int48mi(PG_FUNCTION_ARGS
)
987 int32 arg1
= PG_GETARG_INT32(0);
988 int64 arg2
= PG_GETARG_INT64(1);
991 if (unlikely(pg_sub_s64_overflow((int64
) arg1
, arg2
, &result
)))
993 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
994 errmsg("bigint out of range")));
995 PG_RETURN_INT64(result
);
999 int48mul(PG_FUNCTION_ARGS
)
1001 int32 arg1
= PG_GETARG_INT32(0);
1002 int64 arg2
= PG_GETARG_INT64(1);
1005 if (unlikely(pg_mul_s64_overflow((int64
) arg1
, arg2
, &result
)))
1007 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1008 errmsg("bigint out of range")));
1009 PG_RETURN_INT64(result
);
1013 int48div(PG_FUNCTION_ARGS
)
1015 int32 arg1
= PG_GETARG_INT32(0);
1016 int64 arg2
= PG_GETARG_INT64(1);
1018 if (unlikely(arg2
== 0))
1021 (errcode(ERRCODE_DIVISION_BY_ZERO
),
1022 errmsg("division by zero")));
1023 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1027 /* No overflow is possible */
1028 PG_RETURN_INT64((int64
) arg1
/ arg2
);
1032 int82pl(PG_FUNCTION_ARGS
)
1034 int64 arg1
= PG_GETARG_INT64(0);
1035 int16 arg2
= PG_GETARG_INT16(1);
1038 if (unlikely(pg_add_s64_overflow(arg1
, (int64
) arg2
, &result
)))
1040 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1041 errmsg("bigint out of range")));
1042 PG_RETURN_INT64(result
);
1046 int82mi(PG_FUNCTION_ARGS
)
1048 int64 arg1
= PG_GETARG_INT64(0);
1049 int16 arg2
= PG_GETARG_INT16(1);
1052 if (unlikely(pg_sub_s64_overflow(arg1
, (int64
) arg2
, &result
)))
1054 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1055 errmsg("bigint out of range")));
1056 PG_RETURN_INT64(result
);
1060 int82mul(PG_FUNCTION_ARGS
)
1062 int64 arg1
= PG_GETARG_INT64(0);
1063 int16 arg2
= PG_GETARG_INT16(1);
1066 if (unlikely(pg_mul_s64_overflow(arg1
, (int64
) arg2
, &result
)))
1068 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1069 errmsg("bigint out of range")));
1070 PG_RETURN_INT64(result
);
1074 int82div(PG_FUNCTION_ARGS
)
1076 int64 arg1
= PG_GETARG_INT64(0);
1077 int16 arg2
= PG_GETARG_INT16(1);
1080 if (unlikely(arg2
== 0))
1083 (errcode(ERRCODE_DIVISION_BY_ZERO
),
1084 errmsg("division by zero")));
1085 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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.
1097 if (unlikely(arg1
== PG_INT64_MIN
))
1099 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1100 errmsg("bigint out of range")));
1102 PG_RETURN_INT64(result
);
1105 /* No overflow is possible */
1107 result
= arg1
/ arg2
;
1109 PG_RETURN_INT64(result
);
1113 int28pl(PG_FUNCTION_ARGS
)
1115 int16 arg1
= PG_GETARG_INT16(0);
1116 int64 arg2
= PG_GETARG_INT64(1);
1119 if (unlikely(pg_add_s64_overflow((int64
) arg1
, arg2
, &result
)))
1121 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1122 errmsg("bigint out of range")));
1123 PG_RETURN_INT64(result
);
1127 int28mi(PG_FUNCTION_ARGS
)
1129 int16 arg1
= PG_GETARG_INT16(0);
1130 int64 arg2
= PG_GETARG_INT64(1);
1133 if (unlikely(pg_sub_s64_overflow((int64
) arg1
, arg2
, &result
)))
1135 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1136 errmsg("bigint out of range")));
1137 PG_RETURN_INT64(result
);
1141 int28mul(PG_FUNCTION_ARGS
)
1143 int16 arg1
= PG_GETARG_INT16(0);
1144 int64 arg2
= PG_GETARG_INT64(1);
1147 if (unlikely(pg_mul_s64_overflow((int64
) arg1
, arg2
, &result
)))
1149 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1150 errmsg("bigint out of range")));
1151 PG_RETURN_INT64(result
);
1155 int28div(PG_FUNCTION_ARGS
)
1157 int16 arg1
= PG_GETARG_INT16(0);
1158 int64 arg2
= PG_GETARG_INT64(1);
1160 if (unlikely(arg2
== 0))
1163 (errcode(ERRCODE_DIVISION_BY_ZERO
),
1164 errmsg("division by zero")));
1165 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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
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
);
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
);
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
);
1211 int8not(PG_FUNCTION_ARGS
)
1213 int64 arg1
= PG_GETARG_INT64(0);
1215 PG_RETURN_INT64(~arg1
);
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
);
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 *---------------------------------------------------------*/
1241 int48(PG_FUNCTION_ARGS
)
1243 int32 arg
= PG_GETARG_INT32(0);
1245 PG_RETURN_INT64((int64
) arg
);
1249 int84(PG_FUNCTION_ARGS
)
1251 int64 arg
= PG_GETARG_INT64(0);
1253 if (unlikely(arg
< PG_INT32_MIN
) || unlikely(arg
> PG_INT32_MAX
))
1255 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1256 errmsg("integer out of range")));
1258 PG_RETURN_INT32((int32
) arg
);
1262 int28(PG_FUNCTION_ARGS
)
1264 int16 arg
= PG_GETARG_INT16(0);
1266 PG_RETURN_INT64((int64
) arg
);
1270 int82(PG_FUNCTION_ARGS
)
1272 int64 arg
= PG_GETARG_INT64(0);
1274 if (unlikely(arg
< PG_INT16_MIN
) || unlikely(arg
> PG_INT16_MAX
))
1276 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1277 errmsg("smallint out of range")));
1279 PG_RETURN_INT16((int16
) arg
);
1283 i8tod(PG_FUNCTION_ARGS
)
1285 int64 arg
= PG_GETARG_INT64(0);
1290 PG_RETURN_FLOAT8(result
);
1294 * Convert float8 to 8-byte integer.
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.
1309 if (unlikely(isnan(num
) || !FLOAT8_FITS_IN_INT64(num
)))
1311 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1312 errmsg("bigint out of range")));
1314 PG_RETURN_INT64((int64
) num
);
1318 i8tof(PG_FUNCTION_ARGS
)
1320 int64 arg
= PG_GETARG_INT64(0);
1325 PG_RETURN_FLOAT4(result
);
1329 * Convert float4 to 8-byte integer.
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.
1344 if (unlikely(isnan(num
) || !FLOAT4_FITS_IN_INT64(num
)))
1346 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1347 errmsg("bigint out of range")));
1349 PG_RETURN_INT64((int64
) num
);
1353 i8tooid(PG_FUNCTION_ARGS
)
1355 int64 arg
= PG_GETARG_INT64(0);
1357 if (unlikely(arg
< 0) || unlikely(arg
> PG_UINT32_MAX
))
1359 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1360 errmsg("OID out of range")));
1362 PG_RETURN_OID((Oid
) arg
);
1366 oidtoi8(PG_FUNCTION_ARGS
)
1368 Oid arg
= PG_GETARG_OID(0);
1370 PG_RETURN_INT64((int64
) arg
);
1374 * non-persistent numeric series generator
1377 generate_series_int8(PG_FUNCTION_ARGS
)
1379 return generate_series_step_int8(fcinfo
);
1383 generate_series_step_int8(PG_FUNCTION_ARGS
)
1385 FuncCallContext
*funcctx
;
1386 generate_series_fctx
*fctx
;
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);
1397 /* see if we were given an explicit step size */
1398 if (PG_NARGS() == 3)
1399 step
= PG_GETARG_INT64(2);
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
;
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
))
1447 /* do when there is more left to send */
1448 SRF_RETURN_NEXT(funcctx
, Int64GetDatum(result
));
1451 /* do when there is no more left */
1452 SRF_RETURN_DONE(funcctx
);
1456 * Planner support function for generate_series(int8, int8 [, int8])
1459 generate_series_int8_support(PG_FUNCTION_ARGS
)
1461 Node
*rawreq
= (Node
*) PG_GETARG_POINTER(0);
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
;
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
));
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
))
1500 else if (IsA(arg1
, Const
) &&
1502 (arg3
== NULL
|| IsA(arg3
, Const
)))
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 */
1515 req
->rows
= floor((finish
- start
+ step
) / step
);
1522 PG_RETURN_POINTER(ret
);