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