1
/*-------------------------------------------------------------------------
4
* Internal 64-bit integer operations
6
* Portions Copyright (c) 1996-2005, 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.57 2004-12-31 22:01:22 pgsql Exp $
12
*-------------------------------------------------------------------------
21
#include "libpq/pqformat.h"
22
#include "utils/int8.h"
27
#define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
34
} generate_series_fctx;
37
/***********************************************************************
39
** Routines for 64-bit integers.
41
***********************************************************************/
43
/*----------------------------------------------------------
44
* Formatting and conversion routines.
45
*---------------------------------------------------------*/
48
* scanint8 --- try to parse a string into an int8.
50
* If errorOK is false, ereport a useful error message if the string is bad.
51
* If errorOK is true, just return "false" for bad input.
54
scanint8(const char *str, bool errorOK, int64 *result)
56
const char *ptr = str;
61
* Do our own scan, rather than relying on sscanf which might be
62
* broken for long long.
65
/* skip leading spaces */
66
while (*ptr && isspace((unsigned char) *ptr))
75
* Do an explicit check for INT64_MIN. Ugly though this is, it's
76
* cleaner than trying to get the loop below to handle it
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;
527
* Overflow check. If the inputs are of different signs then their sum
528
* cannot overflow. If the inputs are of the same sign, their sum
529
* had better be that sign too.
531
if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
533
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
534
errmsg("bigint out of range")));
535
PG_RETURN_INT64(result);
539
int8mi(PG_FUNCTION_ARGS)
541
int64 arg1 = PG_GETARG_INT64(0);
542
int64 arg2 = PG_GETARG_INT64(1);
545
result = arg1 - arg2;
547
* Overflow check. If the inputs are of the same sign then their
548
* difference cannot overflow. If they are of different signs then
549
* the result should be of the same sign as the first input.
551
if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
553
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
554
errmsg("bigint out of range")));
555
PG_RETURN_INT64(result);
559
int8mul(PG_FUNCTION_ARGS)
561
int64 arg1 = PG_GETARG_INT64(0);
562
int64 arg2 = PG_GETARG_INT64(1);
565
result = arg1 * arg2;
567
* Overflow check. We basically check to see if result / arg2 gives
568
* arg1 again. There are two cases where this fails: arg2 = 0 (which
569
* cannot overflow) and arg1 = INT64_MIN, arg2 = -1 (where the division
570
* itself will overflow and thus incorrectly match).
572
* Since the division is likely much more expensive than the actual
573
* multiplication, we'd like to skip it where possible. The best
574
* bang for the buck seems to be to check whether both inputs are in
575
* the int32 range; if so, no overflow is possible.
577
if (!(arg1 == (int64) ((int32) arg1) &&
578
arg2 == (int64) ((int32) arg2)) &&
580
(result/arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
582
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
583
errmsg("bigint out of range")));
584
PG_RETURN_INT64(result);
588
int8div(PG_FUNCTION_ARGS)
590
int64 arg1 = PG_GETARG_INT64(0);
591
int64 arg2 = PG_GETARG_INT64(1);
596
(errcode(ERRCODE_DIVISION_BY_ZERO),
597
errmsg("division by zero")));
599
result = arg1 / arg2;
601
* Overflow check. The only possible overflow case is for
602
* arg1 = INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN,
603
* which can't be represented on a two's-complement machine.
605
if (arg2 == -1 && arg1 < 0 && result < 0)
607
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
608
errmsg("bigint out of range")));
609
PG_RETURN_INT64(result);
616
int8abs(PG_FUNCTION_ARGS)
618
int64 arg1 = PG_GETARG_INT64(0);
621
result = (arg1 < 0) ? -arg1 : arg1;
622
/* overflow check (needed for INT64_MIN) */
625
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
626
errmsg("bigint out of range")));
627
PG_RETURN_INT64(result);
634
int8mod(PG_FUNCTION_ARGS)
636
int64 arg1 = PG_GETARG_INT64(0);
637
int64 arg2 = PG_GETARG_INT64(1);
641
(errcode(ERRCODE_DIVISION_BY_ZERO),
642
errmsg("division by zero")));
643
/* No overflow is possible */
645
PG_RETURN_INT64(arg1 % arg2);
650
int8inc(PG_FUNCTION_ARGS)
652
int64 arg = PG_GETARG_INT64(0);
657
if (arg > 0 && result < 0)
659
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
660
errmsg("bigint out of range")));
662
PG_RETURN_INT64(result);
666
int8larger(PG_FUNCTION_ARGS)
668
int64 arg1 = PG_GETARG_INT64(0);
669
int64 arg2 = PG_GETARG_INT64(1);
672
result = ((arg1 > arg2) ? arg1 : arg2);
674
PG_RETURN_INT64(result);
678
int8smaller(PG_FUNCTION_ARGS)
680
int64 arg1 = PG_GETARG_INT64(0);
681
int64 arg2 = PG_GETARG_INT64(1);
684
result = ((arg1 < arg2) ? arg1 : arg2);
686
PG_RETURN_INT64(result);
690
int84pl(PG_FUNCTION_ARGS)
692
int64 arg1 = PG_GETARG_INT64(0);
693
int32 arg2 = PG_GETARG_INT32(1);
696
result = arg1 + arg2;
698
* Overflow check. If the inputs are of different signs then their sum
699
* cannot overflow. If the inputs are of the same sign, their sum
700
* had better be that sign too.
702
if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
704
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
705
errmsg("bigint out of range")));
706
PG_RETURN_INT64(result);
710
int84mi(PG_FUNCTION_ARGS)
712
int64 arg1 = PG_GETARG_INT64(0);
713
int32 arg2 = PG_GETARG_INT32(1);
716
result = arg1 - arg2;
718
* Overflow check. If the inputs are of the same sign then their
719
* difference cannot overflow. If they are of different signs then
720
* the result should be of the same sign as the first input.
722
if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
724
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
725
errmsg("bigint out of range")));
726
PG_RETURN_INT64(result);
730
int84mul(PG_FUNCTION_ARGS)
732
int64 arg1 = PG_GETARG_INT64(0);
733
int32 arg2 = PG_GETARG_INT32(1);
736
result = arg1 * arg2;
738
* Overflow check. We basically check to see if result / arg1 gives
739
* arg2 again. There is one case where this fails: arg1 = 0 (which
742
* Since the division is likely much more expensive than the actual
743
* multiplication, we'd like to skip it where possible. The best
744
* bang for the buck seems to be to check whether both inputs are in
745
* the int32 range; if so, no overflow is possible.
747
if (arg1 != (int64) ((int32) arg1) &&
750
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
751
errmsg("bigint out of range")));
752
PG_RETURN_INT64(result);
756
int84div(PG_FUNCTION_ARGS)
758
int64 arg1 = PG_GETARG_INT64(0);
759
int32 arg2 = PG_GETARG_INT32(1);
764
(errcode(ERRCODE_DIVISION_BY_ZERO),
765
errmsg("division by zero")));
767
result = arg1 / arg2;
769
* Overflow check. The only possible overflow case is for
770
* arg1 = INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN,
771
* which can't be represented on a two's-complement machine.
773
if (arg2 == -1 && arg1 < 0 && result < 0)
775
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
776
errmsg("bigint out of range")));
777
PG_RETURN_INT64(result);
781
int48pl(PG_FUNCTION_ARGS)
783
int32 arg1 = PG_GETARG_INT32(0);
784
int64 arg2 = PG_GETARG_INT64(1);
787
result = arg1 + arg2;
789
* Overflow check. If the inputs are of different signs then their sum
790
* cannot overflow. If the inputs are of the same sign, their sum
791
* had better be that sign too.
793
if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
795
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
796
errmsg("bigint out of range")));
797
PG_RETURN_INT64(result);
801
int48mi(PG_FUNCTION_ARGS)
803
int32 arg1 = PG_GETARG_INT32(0);
804
int64 arg2 = PG_GETARG_INT64(1);
807
result = arg1 - arg2;
809
* Overflow check. If the inputs are of the same sign then their
810
* difference cannot overflow. If they are of different signs then
811
* the result should be of the same sign as the first input.
813
if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
815
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
816
errmsg("bigint out of range")));
817
PG_RETURN_INT64(result);
821
int48mul(PG_FUNCTION_ARGS)
823
int32 arg1 = PG_GETARG_INT32(0);
824
int64 arg2 = PG_GETARG_INT64(1);
827
result = arg1 * arg2;
829
* Overflow check. We basically check to see if result / arg2 gives
830
* arg1 again. There is one case where this fails: arg2 = 0 (which
833
* Since the division is likely much more expensive than the actual
834
* multiplication, we'd like to skip it where possible. The best
835
* bang for the buck seems to be to check whether both inputs are in
836
* the int32 range; if so, no overflow is possible.
838
if (arg2 != (int64) ((int32) arg2) &&
841
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
842
errmsg("bigint out of range")));
843
PG_RETURN_INT64(result);
847
int48div(PG_FUNCTION_ARGS)
849
int32 arg1 = PG_GETARG_INT32(0);
850
int64 arg2 = PG_GETARG_INT64(1);
854
(errcode(ERRCODE_DIVISION_BY_ZERO),
855
errmsg("division by zero")));
856
/* No overflow is possible */
857
PG_RETURN_INT64((int64) arg1 / arg2);
860
/* Binary arithmetics
862
* int8and - returns arg1 & arg2
863
* int8or - returns arg1 | arg2
864
* int8xor - returns arg1 # arg2
865
* int8not - returns ~arg1
866
* int8shl - returns arg1 << arg2
867
* int8shr - returns arg1 >> arg2
871
int8and(PG_FUNCTION_ARGS)
873
int64 arg1 = PG_GETARG_INT64(0);
874
int64 arg2 = PG_GETARG_INT64(1);
876
PG_RETURN_INT64(arg1 & arg2);
880
int8or(PG_FUNCTION_ARGS)
882
int64 arg1 = PG_GETARG_INT64(0);
883
int64 arg2 = PG_GETARG_INT64(1);
885
PG_RETURN_INT64(arg1 | arg2);
889
int8xor(PG_FUNCTION_ARGS)
891
int64 arg1 = PG_GETARG_INT64(0);
892
int64 arg2 = PG_GETARG_INT64(1);
894
PG_RETURN_INT64(arg1 ^ arg2);
898
int8not(PG_FUNCTION_ARGS)
900
int64 arg1 = PG_GETARG_INT64(0);
902
PG_RETURN_INT64(~arg1);
906
int8shl(PG_FUNCTION_ARGS)
908
int64 arg1 = PG_GETARG_INT64(0);
909
int32 arg2 = PG_GETARG_INT32(1);
911
PG_RETURN_INT64(arg1 << arg2);
915
int8shr(PG_FUNCTION_ARGS)
917
int64 arg1 = PG_GETARG_INT64(0);
918
int32 arg2 = PG_GETARG_INT32(1);
920
PG_RETURN_INT64(arg1 >> arg2);
923
/*----------------------------------------------------------
924
* Conversion operators.
925
*---------------------------------------------------------*/
928
int48(PG_FUNCTION_ARGS)
930
int32 arg = PG_GETARG_INT32(0);
932
PG_RETURN_INT64((int64) arg);
936
int84(PG_FUNCTION_ARGS)
938
int64 arg = PG_GETARG_INT64(0);
941
result = (int32) arg;
943
/* Test for overflow by reverse-conversion. */
944
if ((int64) result != arg)
946
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
947
errmsg("integer out of range")));
949
PG_RETURN_INT32(result);
953
int28(PG_FUNCTION_ARGS)
955
int16 arg = PG_GETARG_INT16(0);
957
PG_RETURN_INT64((int64) arg);
961
int82(PG_FUNCTION_ARGS)
963
int64 arg = PG_GETARG_INT64(0);
966
result = (int16) arg;
968
/* Test for overflow by reverse-conversion. */
969
if ((int64) result != arg)
971
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
972
errmsg("smallint out of range")));
974
PG_RETURN_INT16(result);
978
i8tod(PG_FUNCTION_ARGS)
980
int64 arg = PG_GETARG_INT64(0);
985
PG_RETURN_FLOAT8(result);
989
* Convert float8 to 8-byte integer.
992
dtoi8(PG_FUNCTION_ARGS)
994
float8 arg = PG_GETARG_FLOAT8(0);
997
/* Round arg to nearest integer (but it's still in float form) */
1001
* Does it fit in an int64? Avoid assuming that we have handy
1002
* constants defined for the range boundaries, instead test for
1003
* overflow by reverse-conversion.
1005
result = (int64) arg;
1007
if ((float8) result != arg)
1009
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1010
errmsg("bigint out of range")));
1012
PG_RETURN_INT64(result);
1016
i8tof(PG_FUNCTION_ARGS)
1018
int64 arg = PG_GETARG_INT64(0);
1023
PG_RETURN_FLOAT4(result);
1027
* Convert float4 to 8-byte integer.
1030
ftoi8(PG_FUNCTION_ARGS)
1032
float4 arg = PG_GETARG_FLOAT4(0);
1036
/* Round arg to nearest integer (but it's still in float form) */
1040
* Does it fit in an int64? Avoid assuming that we have handy
1041
* constants defined for the range boundaries, instead test for
1042
* overflow by reverse-conversion.
1044
result = (int64) darg;
1046
if ((float8) result != darg)
1048
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1049
errmsg("bigint out of range")));
1051
PG_RETURN_INT64(result);
1055
i8tooid(PG_FUNCTION_ARGS)
1057
int64 arg = PG_GETARG_INT64(0);
1062
/* Test for overflow by reverse-conversion. */
1063
if ((int64) result != arg)
1065
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1066
errmsg("OID out of range")));
1068
PG_RETURN_OID(result);
1072
oidtoi8(PG_FUNCTION_ARGS)
1074
Oid arg = PG_GETARG_OID(0);
1076
PG_RETURN_INT64((int64) arg);
1080
text_int8(PG_FUNCTION_ARGS)
1082
text *str = PG_GETARG_TEXT_P(0);
1087
len = (VARSIZE(str) - VARHDRSZ);
1088
s = palloc(len + 1);
1089
memcpy(s, VARDATA(str), len);
1092
result = DirectFunctionCall1(int8in, CStringGetDatum(s));
1100
int8_text(PG_FUNCTION_ARGS)
1102
/* arg is int64, but easier to leave it as Datum */
1103
Datum arg = PG_GETARG_DATUM(0);
1108
s = DatumGetCString(DirectFunctionCall1(int8out, arg));
1111
result = (text *) palloc(VARHDRSZ + len);
1113
VARATT_SIZEP(result) = len + VARHDRSZ;
1114
memcpy(VARDATA(result), s, len);
1118
PG_RETURN_TEXT_P(result);
1122
* non-persistent numeric series generator
1125
generate_series_int8(PG_FUNCTION_ARGS)
1127
return generate_series_step_int8(fcinfo);
1131
generate_series_step_int8(PG_FUNCTION_ARGS)
1133
FuncCallContext *funcctx;
1134
generate_series_fctx *fctx;
1136
MemoryContext oldcontext;
1138
/* stuff done only on the first call of the function */
1139
if (SRF_IS_FIRSTCALL())
1141
int64 start = PG_GETARG_INT64(0);
1142
int64 finish = PG_GETARG_INT64(1);
1145
/* see if we were given an explicit step size */
1146
if (PG_NARGS() == 3)
1147
step = PG_GETARG_INT64(2);
1150
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1151
errmsg("step size may not equal zero")));
1153
/* create a function context for cross-call persistence */
1154
funcctx = SRF_FIRSTCALL_INIT();
1157
* switch to memory context appropriate for multiple function
1160
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1162
/* allocate memory for user context */
1163
fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1166
* Use fctx to keep state from call to call. Seed current with the
1167
* original start value
1169
fctx->current = start;
1170
fctx->finish = finish;
1173
funcctx->user_fctx = fctx;
1174
MemoryContextSwitchTo(oldcontext);
1177
/* stuff done on every call of the function */
1178
funcctx = SRF_PERCALL_SETUP();
1181
* get the saved state and use current as the result for this
1184
fctx = funcctx->user_fctx;
1185
result = fctx->current;
1187
if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1188
(fctx->step < 0 && fctx->current >= fctx->finish))
1190
/* increment current in preparation for next iteration */
1191
fctx->current += fctx->step;
1193
/* do when there is more left to send */
1194
SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1197
/* do when there is no more left */
1198
SRF_RETURN_DONE(funcctx);