1 /*-------------------------------------------------------------------------
4 * Internal 64-bit integer operations
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.69 2008/04/21 00:26:45 tgl Exp $
12 *-------------------------------------------------------------------------
21 #include "libpq/pqformat.h"
22 #include "nodes/nodes.h"
23 #include "utils/int8.h"
28 #define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
35 } generate_series_fctx;
38 /***********************************************************************
40 ** Routines for 64-bit integers.
42 ***********************************************************************/
44 /*----------------------------------------------------------
45 * Formatting and conversion routines.
46 *---------------------------------------------------------*/
49 * scanint8 --- try to parse a string into an int8.
51 * If errorOK is false, ereport a useful error message if the string is bad.
52 * If errorOK is true, just return "false" for bad input.
55 scanint8(const char *str, bool errorOK, int64 *result)
57 const char *ptr = str;
62 * Do our own scan, rather than relying on sscanf which might be broken
66 /* skip leading spaces */
67 while (*ptr && isspace((unsigned char) *ptr))
76 * Do an explicit check for INT64_MIN. Ugly though this is, it's
77 * cleaner than trying to get the loop below to handle it portably.
79 #ifndef INT64_IS_BUSTED
80 if (strncmp(ptr, "9223372036854775808", 19) == 0)
82 tmp = -INT64CONST(0x7fffffffffffffff) - 1;
93 /* require at least one digit */
94 if (!isdigit((unsigned char) *ptr))
100 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
101 errmsg("invalid input syntax for integer: \"%s\"",
106 while (*ptr && isdigit((unsigned char) *ptr))
108 int64 newtmp = tmp * 10 + (*ptr++ - '0');
110 if ((newtmp / 10) != tmp) /* overflow? */
116 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
117 errmsg("value \"%s\" is out of range for type bigint",
125 /* allow trailing whitespace, but not other trailing chars */
126 while (*ptr != '\0' && isspace((unsigned char) *ptr))
135 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
136 errmsg("invalid input syntax for integer: \"%s\"",
140 *result = (sign < 0) ? -tmp : tmp;
148 int8in(PG_FUNCTION_ARGS)
150 char *str = PG_GETARG_CSTRING(0);
153 (void) scanint8(str, false, &result);
154 PG_RETURN_INT64(result);
161 int8out(PG_FUNCTION_ARGS)
163 int64 val = PG_GETARG_INT64(0);
166 char buf[MAXINT8LEN + 1];
168 if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, val)) < 0)
169 elog(ERROR, "could not format int8");
171 result = pstrdup(buf);
172 PG_RETURN_CSTRING(result);
176 * int8recv - converts external binary format to int8
179 int8recv(PG_FUNCTION_ARGS)
181 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
183 PG_RETURN_INT64(pq_getmsgint64(buf));
187 * int8send - converts int8 to binary format
190 int8send(PG_FUNCTION_ARGS)
192 int64 arg1 = PG_GETARG_INT64(0);
195 pq_begintypsend(&buf);
196 pq_sendint64(&buf, arg1);
197 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
201 /*----------------------------------------------------------
202 * Relational operators for int8s, including cross-data-type comparisons.
203 *---------------------------------------------------------*/
206 * Is val1 relop val2?
209 int8eq(PG_FUNCTION_ARGS)
211 int64 val1 = PG_GETARG_INT64(0);
212 int64 val2 = PG_GETARG_INT64(1);
214 PG_RETURN_BOOL(val1 == val2);
218 int8ne(PG_FUNCTION_ARGS)
220 int64 val1 = PG_GETARG_INT64(0);
221 int64 val2 = PG_GETARG_INT64(1);
223 PG_RETURN_BOOL(val1 != val2);
227 int8lt(PG_FUNCTION_ARGS)
229 int64 val1 = PG_GETARG_INT64(0);
230 int64 val2 = PG_GETARG_INT64(1);
232 PG_RETURN_BOOL(val1 < val2);
236 int8gt(PG_FUNCTION_ARGS)
238 int64 val1 = PG_GETARG_INT64(0);
239 int64 val2 = PG_GETARG_INT64(1);
241 PG_RETURN_BOOL(val1 > val2);
245 int8le(PG_FUNCTION_ARGS)
247 int64 val1 = PG_GETARG_INT64(0);
248 int64 val2 = PG_GETARG_INT64(1);
250 PG_RETURN_BOOL(val1 <= val2);
254 int8ge(PG_FUNCTION_ARGS)
256 int64 val1 = PG_GETARG_INT64(0);
257 int64 val2 = PG_GETARG_INT64(1);
259 PG_RETURN_BOOL(val1 >= val2);
263 * Is 64-bit val1 relop 32-bit val2?
266 int84eq(PG_FUNCTION_ARGS)
268 int64 val1 = PG_GETARG_INT64(0);
269 int32 val2 = PG_GETARG_INT32(1);
271 PG_RETURN_BOOL(val1 == val2);
275 int84ne(PG_FUNCTION_ARGS)
277 int64 val1 = PG_GETARG_INT64(0);
278 int32 val2 = PG_GETARG_INT32(1);
280 PG_RETURN_BOOL(val1 != val2);
284 int84lt(PG_FUNCTION_ARGS)
286 int64 val1 = PG_GETARG_INT64(0);
287 int32 val2 = PG_GETARG_INT32(1);
289 PG_RETURN_BOOL(val1 < val2);
293 int84gt(PG_FUNCTION_ARGS)
295 int64 val1 = PG_GETARG_INT64(0);
296 int32 val2 = PG_GETARG_INT32(1);
298 PG_RETURN_BOOL(val1 > val2);
302 int84le(PG_FUNCTION_ARGS)
304 int64 val1 = PG_GETARG_INT64(0);
305 int32 val2 = PG_GETARG_INT32(1);
307 PG_RETURN_BOOL(val1 <= val2);
311 int84ge(PG_FUNCTION_ARGS)
313 int64 val1 = PG_GETARG_INT64(0);
314 int32 val2 = PG_GETARG_INT32(1);
316 PG_RETURN_BOOL(val1 >= val2);
320 * Is 32-bit val1 relop 64-bit val2?
323 int48eq(PG_FUNCTION_ARGS)
325 int32 val1 = PG_GETARG_INT32(0);
326 int64 val2 = PG_GETARG_INT64(1);
328 PG_RETURN_BOOL(val1 == val2);
332 int48ne(PG_FUNCTION_ARGS)
334 int32 val1 = PG_GETARG_INT32(0);
335 int64 val2 = PG_GETARG_INT64(1);
337 PG_RETURN_BOOL(val1 != val2);
341 int48lt(PG_FUNCTION_ARGS)
343 int32 val1 = PG_GETARG_INT32(0);
344 int64 val2 = PG_GETARG_INT64(1);
346 PG_RETURN_BOOL(val1 < val2);
350 int48gt(PG_FUNCTION_ARGS)
352 int32 val1 = PG_GETARG_INT32(0);
353 int64 val2 = PG_GETARG_INT64(1);
355 PG_RETURN_BOOL(val1 > val2);
359 int48le(PG_FUNCTION_ARGS)
361 int32 val1 = PG_GETARG_INT32(0);
362 int64 val2 = PG_GETARG_INT64(1);
364 PG_RETURN_BOOL(val1 <= val2);
368 int48ge(PG_FUNCTION_ARGS)
370 int32 val1 = PG_GETARG_INT32(0);
371 int64 val2 = PG_GETARG_INT64(1);
373 PG_RETURN_BOOL(val1 >= val2);
377 * Is 64-bit val1 relop 16-bit val2?
380 int82eq(PG_FUNCTION_ARGS)
382 int64 val1 = PG_GETARG_INT64(0);
383 int16 val2 = PG_GETARG_INT16(1);
385 PG_RETURN_BOOL(val1 == val2);
389 int82ne(PG_FUNCTION_ARGS)
391 int64 val1 = PG_GETARG_INT64(0);
392 int16 val2 = PG_GETARG_INT16(1);
394 PG_RETURN_BOOL(val1 != val2);
398 int82lt(PG_FUNCTION_ARGS)
400 int64 val1 = PG_GETARG_INT64(0);
401 int16 val2 = PG_GETARG_INT16(1);
403 PG_RETURN_BOOL(val1 < val2);
407 int82gt(PG_FUNCTION_ARGS)
409 int64 val1 = PG_GETARG_INT64(0);
410 int16 val2 = PG_GETARG_INT16(1);
412 PG_RETURN_BOOL(val1 > val2);
416 int82le(PG_FUNCTION_ARGS)
418 int64 val1 = PG_GETARG_INT64(0);
419 int16 val2 = PG_GETARG_INT16(1);
421 PG_RETURN_BOOL(val1 <= val2);
425 int82ge(PG_FUNCTION_ARGS)
427 int64 val1 = PG_GETARG_INT64(0);
428 int16 val2 = PG_GETARG_INT16(1);
430 PG_RETURN_BOOL(val1 >= val2);
434 * Is 16-bit val1 relop 64-bit val2?
437 int28eq(PG_FUNCTION_ARGS)
439 int16 val1 = PG_GETARG_INT16(0);
440 int64 val2 = PG_GETARG_INT64(1);
442 PG_RETURN_BOOL(val1 == val2);
446 int28ne(PG_FUNCTION_ARGS)
448 int16 val1 = PG_GETARG_INT16(0);
449 int64 val2 = PG_GETARG_INT64(1);
451 PG_RETURN_BOOL(val1 != val2);
455 int28lt(PG_FUNCTION_ARGS)
457 int16 val1 = PG_GETARG_INT16(0);
458 int64 val2 = PG_GETARG_INT64(1);
460 PG_RETURN_BOOL(val1 < val2);
464 int28gt(PG_FUNCTION_ARGS)
466 int16 val1 = PG_GETARG_INT16(0);
467 int64 val2 = PG_GETARG_INT64(1);
469 PG_RETURN_BOOL(val1 > val2);
473 int28le(PG_FUNCTION_ARGS)
475 int16 val1 = PG_GETARG_INT16(0);
476 int64 val2 = PG_GETARG_INT64(1);
478 PG_RETURN_BOOL(val1 <= val2);
482 int28ge(PG_FUNCTION_ARGS)
484 int16 val1 = PG_GETARG_INT16(0);
485 int64 val2 = PG_GETARG_INT64(1);
487 PG_RETURN_BOOL(val1 >= val2);
491 /*----------------------------------------------------------
492 * Arithmetic operators on 64-bit integers.
493 *---------------------------------------------------------*/
496 int8um(PG_FUNCTION_ARGS)
498 int64 arg = PG_GETARG_INT64(0);
502 /* overflow check (needed for INT64_MIN) */
503 if (arg != 0 && SAMESIGN(result, arg))
505 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
506 errmsg("bigint out of range")));
507 PG_RETURN_INT64(result);
511 int8up(PG_FUNCTION_ARGS)
513 int64 arg = PG_GETARG_INT64(0);
515 PG_RETURN_INT64(arg);
519 int8pl(PG_FUNCTION_ARGS)
521 int64 arg1 = PG_GETARG_INT64(0);
522 int64 arg2 = PG_GETARG_INT64(1);
525 result = arg1 + arg2;
528 * Overflow check. If the inputs are of different signs then their sum
529 * cannot overflow. If the inputs are of the same sign, their sum had
530 * better be that sign too.
532 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
534 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
535 errmsg("bigint out of range")));
536 PG_RETURN_INT64(result);
540 int8mi(PG_FUNCTION_ARGS)
542 int64 arg1 = PG_GETARG_INT64(0);
543 int64 arg2 = PG_GETARG_INT64(1);
546 result = arg1 - arg2;
549 * Overflow check. If the inputs are of the same sign then their
550 * difference cannot overflow. If they are of different signs then the
551 * result should be of the same sign as the first input.
553 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
555 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
556 errmsg("bigint out of range")));
557 PG_RETURN_INT64(result);
561 int8mul(PG_FUNCTION_ARGS)
563 int64 arg1 = PG_GETARG_INT64(0);
564 int64 arg2 = PG_GETARG_INT64(1);
567 result = arg1 * arg2;
570 * Overflow check. We basically check to see if result / arg2 gives arg1
571 * again. There are two cases where this fails: arg2 = 0 (which cannot
572 * overflow) and arg1 = INT64_MIN, arg2 = -1 (where the division itself
573 * will overflow and thus incorrectly match).
575 * Since the division is likely much more expensive than the actual
576 * multiplication, we'd like to skip it where possible. The best bang for
577 * the buck seems to be to check whether both inputs are in the int32
578 * range; if so, no overflow is possible. (But that only works if we
579 * really have a 64-bit int64 datatype...)
581 #ifndef INT64_IS_BUSTED
582 if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
586 (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
588 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
589 errmsg("bigint out of range")));
591 PG_RETURN_INT64(result);
595 int8div(PG_FUNCTION_ARGS)
597 int64 arg1 = PG_GETARG_INT64(0);
598 int64 arg2 = PG_GETARG_INT64(1);
603 (errcode(ERRCODE_DIVISION_BY_ZERO),
604 errmsg("division by zero")));
606 result = arg1 / arg2;
609 * Overflow check. The only possible overflow case is for arg1 =
610 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
611 * can't be represented on a two's-complement machine.
613 if (arg2 == -1 && arg1 < 0 && result < 0)
615 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
616 errmsg("bigint out of range")));
617 PG_RETURN_INT64(result);
624 int8abs(PG_FUNCTION_ARGS)
626 int64 arg1 = PG_GETARG_INT64(0);
629 result = (arg1 < 0) ? -arg1 : arg1;
630 /* overflow check (needed for INT64_MIN) */
633 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
634 errmsg("bigint out of range")));
635 PG_RETURN_INT64(result);
642 int8mod(PG_FUNCTION_ARGS)
644 int64 arg1 = PG_GETARG_INT64(0);
645 int64 arg2 = PG_GETARG_INT64(1);
649 (errcode(ERRCODE_DIVISION_BY_ZERO),
650 errmsg("division by zero")));
651 /* No overflow is possible */
653 PG_RETURN_INT64(arg1 % arg2);
658 int8inc(PG_FUNCTION_ARGS)
661 * When int8 is pass-by-reference, we provide this special case to avoid
662 * palloc overhead for COUNT(): when called from nodeAgg, we know that the
663 * argument is modifiable local storage, so just update it in-place.
664 * (If int8 is pass-by-value, then of course this is useless as well
665 * as incorrect, so just ifdef it out.)
667 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
668 if (fcinfo->context && IsA(fcinfo->context, AggState))
670 int64 *arg = (int64 *) PG_GETARG_POINTER(0);
675 if (result < 0 && *arg > 0)
677 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
678 errmsg("bigint out of range")));
681 PG_RETURN_POINTER(arg);
686 /* Not called by nodeAgg, so just do it the dumb way */
687 int64 arg = PG_GETARG_INT64(0);
692 if (result < 0 && arg > 0)
694 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
695 errmsg("bigint out of range")));
697 PG_RETURN_INT64(result);
702 * These functions are exactly like int8inc but are used for aggregates that
703 * count only non-null values. Since the functions are declared strict,
704 * the null checks happen before we ever get here, and all we need do is
705 * increment the state value. We could actually make these pg_proc entries
706 * point right at int8inc, but then the opr_sanity regression test would
707 * complain about mismatched entries for a built-in function.
711 int8inc_any(PG_FUNCTION_ARGS)
713 return int8inc(fcinfo);
717 int8inc_float8_float8(PG_FUNCTION_ARGS)
719 return int8inc(fcinfo);
724 int8larger(PG_FUNCTION_ARGS)
726 int64 arg1 = PG_GETARG_INT64(0);
727 int64 arg2 = PG_GETARG_INT64(1);
730 result = ((arg1 > arg2) ? arg1 : arg2);
732 PG_RETURN_INT64(result);
736 int8smaller(PG_FUNCTION_ARGS)
738 int64 arg1 = PG_GETARG_INT64(0);
739 int64 arg2 = PG_GETARG_INT64(1);
742 result = ((arg1 < arg2) ? arg1 : arg2);
744 PG_RETURN_INT64(result);
748 int84pl(PG_FUNCTION_ARGS)
750 int64 arg1 = PG_GETARG_INT64(0);
751 int32 arg2 = PG_GETARG_INT32(1);
754 result = arg1 + arg2;
757 * Overflow check. If the inputs are of different signs then their sum
758 * cannot overflow. If the inputs are of the same sign, their sum had
759 * better be that sign too.
761 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
763 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
764 errmsg("bigint out of range")));
765 PG_RETURN_INT64(result);
769 int84mi(PG_FUNCTION_ARGS)
771 int64 arg1 = PG_GETARG_INT64(0);
772 int32 arg2 = PG_GETARG_INT32(1);
775 result = arg1 - arg2;
778 * Overflow check. If the inputs are of the same sign then their
779 * difference cannot overflow. If they are of different signs then the
780 * result should be of the same sign as the first input.
782 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
784 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
785 errmsg("bigint out of range")));
786 PG_RETURN_INT64(result);
790 int84mul(PG_FUNCTION_ARGS)
792 int64 arg1 = PG_GETARG_INT64(0);
793 int32 arg2 = PG_GETARG_INT32(1);
796 result = arg1 * arg2;
799 * Overflow check. We basically check to see if result / arg1 gives arg2
800 * again. There is one case where this fails: arg1 = 0 (which cannot
803 * Since the division is likely much more expensive than the actual
804 * multiplication, we'd like to skip it where possible. The best bang for
805 * the buck seems to be to check whether both inputs are in the int32
806 * range; if so, no overflow is possible.
808 if (arg1 != (int64) ((int32) arg1) &&
809 result / arg1 != arg2)
811 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
812 errmsg("bigint out of range")));
813 PG_RETURN_INT64(result);
817 int84div(PG_FUNCTION_ARGS)
819 int64 arg1 = PG_GETARG_INT64(0);
820 int32 arg2 = PG_GETARG_INT32(1);
825 (errcode(ERRCODE_DIVISION_BY_ZERO),
826 errmsg("division by zero")));
828 result = arg1 / arg2;
831 * Overflow check. The only possible overflow case is for arg1 =
832 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
833 * can't be represented on a two's-complement machine.
835 if (arg2 == -1 && arg1 < 0 && result < 0)
837 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
838 errmsg("bigint out of range")));
839 PG_RETURN_INT64(result);
843 int48pl(PG_FUNCTION_ARGS)
845 int32 arg1 = PG_GETARG_INT32(0);
846 int64 arg2 = PG_GETARG_INT64(1);
849 result = arg1 + arg2;
852 * Overflow check. If the inputs are of different signs then their sum
853 * cannot overflow. If the inputs are of the same sign, their sum had
854 * better be that sign too.
856 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
858 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
859 errmsg("bigint out of range")));
860 PG_RETURN_INT64(result);
864 int48mi(PG_FUNCTION_ARGS)
866 int32 arg1 = PG_GETARG_INT32(0);
867 int64 arg2 = PG_GETARG_INT64(1);
870 result = arg1 - arg2;
873 * Overflow check. If the inputs are of the same sign then their
874 * difference cannot overflow. If they are of different signs then the
875 * result should be of the same sign as the first input.
877 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
879 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
880 errmsg("bigint out of range")));
881 PG_RETURN_INT64(result);
885 int48mul(PG_FUNCTION_ARGS)
887 int32 arg1 = PG_GETARG_INT32(0);
888 int64 arg2 = PG_GETARG_INT64(1);
891 result = arg1 * arg2;
894 * Overflow check. We basically check to see if result / arg2 gives arg1
895 * again. There is one case where this fails: arg2 = 0 (which cannot
898 * Since the division is likely much more expensive than the actual
899 * multiplication, we'd like to skip it where possible. The best bang for
900 * the buck seems to be to check whether both inputs are in the int32
901 * range; if so, no overflow is possible.
903 if (arg2 != (int64) ((int32) arg2) &&
904 result / arg2 != arg1)
906 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
907 errmsg("bigint out of range")));
908 PG_RETURN_INT64(result);
912 int48div(PG_FUNCTION_ARGS)
914 int32 arg1 = PG_GETARG_INT32(0);
915 int64 arg2 = PG_GETARG_INT64(1);
919 (errcode(ERRCODE_DIVISION_BY_ZERO),
920 errmsg("division by zero")));
921 /* No overflow is possible */
922 PG_RETURN_INT64((int64) arg1 / arg2);
925 /* Binary arithmetics
927 * int8and - returns arg1 & arg2
928 * int8or - returns arg1 | arg2
929 * int8xor - returns arg1 # arg2
930 * int8not - returns ~arg1
931 * int8shl - returns arg1 << arg2
932 * int8shr - returns arg1 >> arg2
936 int8and(PG_FUNCTION_ARGS)
938 int64 arg1 = PG_GETARG_INT64(0);
939 int64 arg2 = PG_GETARG_INT64(1);
941 PG_RETURN_INT64(arg1 & arg2);
945 int8or(PG_FUNCTION_ARGS)
947 int64 arg1 = PG_GETARG_INT64(0);
948 int64 arg2 = PG_GETARG_INT64(1);
950 PG_RETURN_INT64(arg1 | arg2);
954 int8xor(PG_FUNCTION_ARGS)
956 int64 arg1 = PG_GETARG_INT64(0);
957 int64 arg2 = PG_GETARG_INT64(1);
959 PG_RETURN_INT64(arg1 ^ arg2);
963 int8not(PG_FUNCTION_ARGS)
965 int64 arg1 = PG_GETARG_INT64(0);
967 PG_RETURN_INT64(~arg1);
971 int8shl(PG_FUNCTION_ARGS)
973 int64 arg1 = PG_GETARG_INT64(0);
974 int32 arg2 = PG_GETARG_INT32(1);
976 PG_RETURN_INT64(arg1 << arg2);
980 int8shr(PG_FUNCTION_ARGS)
982 int64 arg1 = PG_GETARG_INT64(0);
983 int32 arg2 = PG_GETARG_INT32(1);
985 PG_RETURN_INT64(arg1 >> arg2);
988 /*----------------------------------------------------------
989 * Conversion operators.
990 *---------------------------------------------------------*/
993 int48(PG_FUNCTION_ARGS)
995 int32 arg = PG_GETARG_INT32(0);
997 PG_RETURN_INT64((int64) arg);
1001 int84(PG_FUNCTION_ARGS)
1003 int64 arg = PG_GETARG_INT64(0);
1006 result = (int32) arg;
1008 /* Test for overflow by reverse-conversion. */
1009 if ((int64) result != arg)
1011 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1012 errmsg("integer out of range")));
1014 PG_RETURN_INT32(result);
1018 int28(PG_FUNCTION_ARGS)
1020 int16 arg = PG_GETARG_INT16(0);
1022 PG_RETURN_INT64((int64) arg);
1026 int82(PG_FUNCTION_ARGS)
1028 int64 arg = PG_GETARG_INT64(0);
1031 result = (int16) arg;
1033 /* Test for overflow by reverse-conversion. */
1034 if ((int64) result != arg)
1036 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1037 errmsg("smallint out of range")));
1039 PG_RETURN_INT16(result);
1043 i8tod(PG_FUNCTION_ARGS)
1045 int64 arg = PG_GETARG_INT64(0);
1050 PG_RETURN_FLOAT8(result);
1054 * Convert float8 to 8-byte integer.
1057 dtoi8(PG_FUNCTION_ARGS)
1059 float8 arg = PG_GETARG_FLOAT8(0);
1062 /* Round arg to nearest integer (but it's still in float form) */
1066 * Does it fit in an int64? Avoid assuming that we have handy constants
1067 * defined for the range boundaries, instead test for overflow by
1068 * reverse-conversion.
1070 result = (int64) arg;
1072 if ((float8) result != arg)
1074 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1075 errmsg("bigint out of range")));
1077 PG_RETURN_INT64(result);
1081 i8tof(PG_FUNCTION_ARGS)
1083 int64 arg = PG_GETARG_INT64(0);
1088 PG_RETURN_FLOAT4(result);
1092 * Convert float4 to 8-byte integer.
1095 ftoi8(PG_FUNCTION_ARGS)
1097 float4 arg = PG_GETARG_FLOAT4(0);
1101 /* Round arg to nearest integer (but it's still in float form) */
1105 * Does it fit in an int64? Avoid assuming that we have handy constants
1106 * defined for the range boundaries, instead test for overflow by
1107 * reverse-conversion.
1109 result = (int64) darg;
1111 if ((float8) result != darg)
1113 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1114 errmsg("bigint out of range")));
1116 PG_RETURN_INT64(result);
1120 i8tooid(PG_FUNCTION_ARGS)
1122 int64 arg = PG_GETARG_INT64(0);
1127 /* Test for overflow by reverse-conversion. */
1128 if ((int64) result != arg)
1130 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1131 errmsg("OID out of range")));
1133 PG_RETURN_OID(result);
1137 oidtoi8(PG_FUNCTION_ARGS)
1139 Oid arg = PG_GETARG_OID(0);
1141 PG_RETURN_INT64((int64) arg);
1145 * non-persistent numeric series generator
1148 generate_series_int8(PG_FUNCTION_ARGS)
1150 return generate_series_step_int8(fcinfo);
1154 generate_series_step_int8(PG_FUNCTION_ARGS)
1156 FuncCallContext *funcctx;
1157 generate_series_fctx *fctx;
1159 MemoryContext oldcontext;
1161 /* stuff done only on the first call of the function */
1162 if (SRF_IS_FIRSTCALL())
1164 int64 start = PG_GETARG_INT64(0);
1165 int64 finish = PG_GETARG_INT64(1);
1168 /* see if we were given an explicit step size */
1169 if (PG_NARGS() == 3)
1170 step = PG_GETARG_INT64(2);
1173 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1174 errmsg("step size cannot equal zero")));
1176 /* create a function context for cross-call persistence */
1177 funcctx = SRF_FIRSTCALL_INIT();
1180 * switch to memory context appropriate for multiple function calls
1182 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1184 /* allocate memory for user context */
1185 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1188 * Use fctx to keep state from call to call. Seed current with the
1189 * original start value
1191 fctx->current = start;
1192 fctx->finish = finish;
1195 funcctx->user_fctx = fctx;
1196 MemoryContextSwitchTo(oldcontext);
1199 /* stuff done on every call of the function */
1200 funcctx = SRF_PERCALL_SETUP();
1203 * get the saved state and use current as the result for this iteration
1205 fctx = funcctx->user_fctx;
1206 result = fctx->current;
1208 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1209 (fctx->step < 0 && fctx->current >= fctx->finish))
1211 /* increment current in preparation for next iteration */
1212 fctx->current += fctx->step;
1214 /* do when there is more left to send */
1215 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1218 /* do when there is no more left */
1219 SRF_RETURN_DONE(funcctx);