PostgreSQL Source Code  git master
int.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * int.c
4  * Functions for the built-in integer types (except int8).
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/int.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * OLD COMMENTS
17  * I/O routines:
18  * int2in, int2out, int2recv, int2send
19  * int4in, int4out, int4recv, int4send
20  * int2vectorin, int2vectorout, int2vectorrecv, int2vectorsend
21  * Boolean operators:
22  * inteq, intne, intlt, intle, intgt, intge
23  * Arithmetic operators:
24  * intpl, intmi, int4mul, intdiv
25  *
26  * Arithmetic operators:
27  * intmod
28  */
29 #include "postgres.h"
30 
31 #include <ctype.h>
32 #include <limits.h>
33 #include <math.h>
34 
35 #include "catalog/pg_type.h"
36 #include "common/int.h"
37 #include "funcapi.h"
38 #include "libpq/pqformat.h"
39 #include "nodes/nodeFuncs.h"
40 #include "nodes/supportnodes.h"
41 #include "optimizer/optimizer.h"
42 #include "utils/array.h"
43 #include "utils/builtins.h"
44 
45 #define Int2VectorSize(n) (offsetof(int2vector, values) + (n) * sizeof(int16))
46 
47 typedef struct
48 {
53 
54 
55 /*****************************************************************************
56  * USER I/O ROUTINES *
57  *****************************************************************************/
58 
59 /*
60  * int2in - converts "num" to short
61  */
62 Datum
64 {
65  char *num = PG_GETARG_CSTRING(0);
66 
68 }
69 
70 /*
71  * int2out - converts short to "num"
72  */
73 Datum
75 {
76  int16 arg1 = PG_GETARG_INT16(0);
77  char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */
78 
79  pg_itoa(arg1, result);
80  PG_RETURN_CSTRING(result);
81 }
82 
83 /*
84  * int2recv - converts external binary format to int2
85  */
86 Datum
88 {
90 
92 }
93 
94 /*
95  * int2send - converts int2 to binary format
96  */
97 Datum
99 {
100  int16 arg1 = PG_GETARG_INT16(0);
102 
104  pq_sendint16(&buf, arg1);
106 }
107 
108 /*
109  * construct int2vector given a raw array of int2s
110  *
111  * If int2s is NULL then caller must fill values[] afterward
112  */
113 int2vector *
114 buildint2vector(const int16 *int2s, int n)
115 {
116  int2vector *result;
117 
118  result = (int2vector *) palloc0(Int2VectorSize(n));
119 
120  if (n > 0 && int2s)
121  memcpy(result->values, int2s, n * sizeof(int16));
122 
123  /*
124  * Attach standard array header. For historical reasons, we set the index
125  * lower bound to 0 not 1.
126  */
127  SET_VARSIZE(result, Int2VectorSize(n));
128  result->ndim = 1;
129  result->dataoffset = 0; /* never any nulls */
130  result->elemtype = INT2OID;
131  result->dim1 = n;
132  result->lbound1 = 0;
133 
134  return result;
135 }
136 
137 /*
138  * int2vectorin - converts "num num ..." to internal form
139  */
140 Datum
142 {
143  char *intString = PG_GETARG_CSTRING(0);
144  int2vector *result;
145  int n;
146 
148 
149  for (n = 0; n < FUNC_MAX_ARGS; n++)
150  {
151  long l;
152  char *endp;
153 
154  while (*intString && isspace((unsigned char) *intString))
155  intString++;
156  if (*intString == '\0')
157  break;
158 
159  errno = 0;
160  l = strtol(intString, &endp, 10);
161 
162  if (intString == endp)
163  ereport(ERROR,
164  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
165  errmsg("invalid input syntax for type %s: \"%s\"",
166  "smallint", intString)));
167 
168  if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
169  ereport(ERROR,
170  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
171  errmsg("value \"%s\" is out of range for type %s", intString,
172  "smallint")));
173 
174  if (*endp && *endp != ' ')
175  ereport(ERROR,
176  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
177  errmsg("invalid input syntax for type %s: \"%s\"",
178  "integer", intString)));
179 
180  result->values[n] = l;
181  intString = endp;
182  }
183  while (*intString && isspace((unsigned char) *intString))
184  intString++;
185  if (*intString)
186  ereport(ERROR,
187  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
188  errmsg("int2vector has too many elements")));
189 
190  SET_VARSIZE(result, Int2VectorSize(n));
191  result->ndim = 1;
192  result->dataoffset = 0; /* never any nulls */
193  result->elemtype = INT2OID;
194  result->dim1 = n;
195  result->lbound1 = 0;
196 
197  PG_RETURN_POINTER(result);
198 }
199 
200 /*
201  * int2vectorout - converts internal form to "num num ..."
202  */
203 Datum
205 {
206  int2vector *int2Array = (int2vector *) PG_GETARG_POINTER(0);
207  int num,
208  nnums = int2Array->dim1;
209  char *rp;
210  char *result;
211 
212  /* assumes sign, 5 digits, ' ' */
213  rp = result = (char *) palloc(nnums * 7 + 1);
214  for (num = 0; num < nnums; num++)
215  {
216  if (num != 0)
217  *rp++ = ' ';
218  rp += pg_itoa(int2Array->values[num], rp);
219  }
220  *rp = '\0';
221  PG_RETURN_CSTRING(result);
222 }
223 
224 /*
225  * int2vectorrecv - converts external binary format to int2vector
226  */
227 Datum
229 {
230  LOCAL_FCINFO(locfcinfo, 3);
232  int2vector *result;
233 
234  /*
235  * Normally one would call array_recv() using DirectFunctionCall3, but
236  * that does not work since array_recv wants to cache some data using
237  * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
238  * parameter.
239  */
240  InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
241  InvalidOid, NULL, NULL);
242 
243  locfcinfo->args[0].value = PointerGetDatum(buf);
244  locfcinfo->args[0].isnull = false;
245  locfcinfo->args[1].value = ObjectIdGetDatum(INT2OID);
246  locfcinfo->args[1].isnull = false;
247  locfcinfo->args[2].value = Int32GetDatum(-1);
248  locfcinfo->args[2].isnull = false;
249 
250  result = (int2vector *) DatumGetPointer(array_recv(locfcinfo));
251 
252  Assert(!locfcinfo->isnull);
253 
254  /* sanity checks: int2vector must be 1-D, 0-based, no nulls */
255  if (ARR_NDIM(result) != 1 ||
256  ARR_HASNULL(result) ||
257  ARR_ELEMTYPE(result) != INT2OID ||
258  ARR_LBOUND(result)[0] != 0)
259  ereport(ERROR,
260  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
261  errmsg("invalid int2vector data")));
262 
263  /* check length for consistency with int2vectorin() */
264  if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
265  ereport(ERROR,
266  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
267  errmsg("oidvector has too many elements")));
268 
269  PG_RETURN_POINTER(result);
270 }
271 
272 /*
273  * int2vectorsend - converts int2vector to binary format
274  */
275 Datum
277 {
278  return array_send(fcinfo);
279 }
280 
281 
282 /*****************************************************************************
283  * PUBLIC ROUTINES *
284  *****************************************************************************/
285 
286 /*
287  * int4in - converts "num" to int4
288  */
289 Datum
291 {
292  char *num = PG_GETARG_CSTRING(0);
293 
295 }
296 
297 /*
298  * int4out - converts int4 to "num"
299  */
300 Datum
302 {
303  int32 arg1 = PG_GETARG_INT32(0);
304  char *result = (char *) palloc(12); /* sign, 10 digits, '\0' */
305 
306  pg_ltoa(arg1, result);
307  PG_RETURN_CSTRING(result);
308 }
309 
310 /*
311  * int4recv - converts external binary format to int4
312  */
313 Datum
315 {
317 
319 }
320 
321 /*
322  * int4send - converts int4 to binary format
323  */
324 Datum
326 {
327  int32 arg1 = PG_GETARG_INT32(0);
329 
331  pq_sendint32(&buf, arg1);
333 }
334 
335 
336 /*
337  * ===================
338  * CONVERSION ROUTINES
339  * ===================
340  */
341 
342 Datum
344 {
345  int16 arg1 = PG_GETARG_INT16(0);
346 
347  PG_RETURN_INT32((int32) arg1);
348 }
349 
350 Datum
352 {
353  int32 arg1 = PG_GETARG_INT32(0);
354 
355  if (unlikely(arg1 < SHRT_MIN) || unlikely(arg1 > SHRT_MAX))
356  ereport(ERROR,
357  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
358  errmsg("smallint out of range")));
359 
360  PG_RETURN_INT16((int16) arg1);
361 }
362 
363 /* Cast int4 -> bool */
364 Datum
366 {
367  if (PG_GETARG_INT32(0) == 0)
368  PG_RETURN_BOOL(false);
369  else
370  PG_RETURN_BOOL(true);
371 }
372 
373 /* Cast bool -> int4 */
374 Datum
376 {
377  if (PG_GETARG_BOOL(0) == false)
378  PG_RETURN_INT32(0);
379  else
380  PG_RETURN_INT32(1);
381 }
382 
383 /*
384  * ============================
385  * COMPARISON OPERATOR ROUTINES
386  * ============================
387  */
388 
389 /*
390  * inteq - returns 1 iff arg1 == arg2
391  * intne - returns 1 iff arg1 != arg2
392  * intlt - returns 1 iff arg1 < arg2
393  * intle - returns 1 iff arg1 <= arg2
394  * intgt - returns 1 iff arg1 > arg2
395  * intge - returns 1 iff arg1 >= arg2
396  */
397 
398 Datum
400 {
401  int32 arg1 = PG_GETARG_INT32(0);
402  int32 arg2 = PG_GETARG_INT32(1);
403 
404  PG_RETURN_BOOL(arg1 == arg2);
405 }
406 
407 Datum
409 {
410  int32 arg1 = PG_GETARG_INT32(0);
411  int32 arg2 = PG_GETARG_INT32(1);
412 
413  PG_RETURN_BOOL(arg1 != arg2);
414 }
415 
416 Datum
418 {
419  int32 arg1 = PG_GETARG_INT32(0);
420  int32 arg2 = PG_GETARG_INT32(1);
421 
422  PG_RETURN_BOOL(arg1 < arg2);
423 }
424 
425 Datum
427 {
428  int32 arg1 = PG_GETARG_INT32(0);
429  int32 arg2 = PG_GETARG_INT32(1);
430 
431  PG_RETURN_BOOL(arg1 <= arg2);
432 }
433 
434 Datum
436 {
437  int32 arg1 = PG_GETARG_INT32(0);
438  int32 arg2 = PG_GETARG_INT32(1);
439 
440  PG_RETURN_BOOL(arg1 > arg2);
441 }
442 
443 Datum
445 {
446  int32 arg1 = PG_GETARG_INT32(0);
447  int32 arg2 = PG_GETARG_INT32(1);
448 
449  PG_RETURN_BOOL(arg1 >= arg2);
450 }
451 
452 Datum
454 {
455  int16 arg1 = PG_GETARG_INT16(0);
456  int16 arg2 = PG_GETARG_INT16(1);
457 
458  PG_RETURN_BOOL(arg1 == arg2);
459 }
460 
461 Datum
463 {
464  int16 arg1 = PG_GETARG_INT16(0);
465  int16 arg2 = PG_GETARG_INT16(1);
466 
467  PG_RETURN_BOOL(arg1 != arg2);
468 }
469 
470 Datum
472 {
473  int16 arg1 = PG_GETARG_INT16(0);
474  int16 arg2 = PG_GETARG_INT16(1);
475 
476  PG_RETURN_BOOL(arg1 < arg2);
477 }
478 
479 Datum
481 {
482  int16 arg1 = PG_GETARG_INT16(0);
483  int16 arg2 = PG_GETARG_INT16(1);
484 
485  PG_RETURN_BOOL(arg1 <= arg2);
486 }
487 
488 Datum
490 {
491  int16 arg1 = PG_GETARG_INT16(0);
492  int16 arg2 = PG_GETARG_INT16(1);
493 
494  PG_RETURN_BOOL(arg1 > arg2);
495 }
496 
497 Datum
499 {
500  int16 arg1 = PG_GETARG_INT16(0);
501  int16 arg2 = PG_GETARG_INT16(1);
502 
503  PG_RETURN_BOOL(arg1 >= arg2);
504 }
505 
506 Datum
508 {
509  int16 arg1 = PG_GETARG_INT16(0);
510  int32 arg2 = PG_GETARG_INT32(1);
511 
512  PG_RETURN_BOOL(arg1 == arg2);
513 }
514 
515 Datum
517 {
518  int16 arg1 = PG_GETARG_INT16(0);
519  int32 arg2 = PG_GETARG_INT32(1);
520 
521  PG_RETURN_BOOL(arg1 != arg2);
522 }
523 
524 Datum
526 {
527  int16 arg1 = PG_GETARG_INT16(0);
528  int32 arg2 = PG_GETARG_INT32(1);
529 
530  PG_RETURN_BOOL(arg1 < arg2);
531 }
532 
533 Datum
535 {
536  int16 arg1 = PG_GETARG_INT16(0);
537  int32 arg2 = PG_GETARG_INT32(1);
538 
539  PG_RETURN_BOOL(arg1 <= arg2);
540 }
541 
542 Datum
544 {
545  int16 arg1 = PG_GETARG_INT16(0);
546  int32 arg2 = PG_GETARG_INT32(1);
547 
548  PG_RETURN_BOOL(arg1 > arg2);
549 }
550 
551 Datum
553 {
554  int16 arg1 = PG_GETARG_INT16(0);
555  int32 arg2 = PG_GETARG_INT32(1);
556 
557  PG_RETURN_BOOL(arg1 >= arg2);
558 }
559 
560 Datum
562 {
563  int32 arg1 = PG_GETARG_INT32(0);
564  int16 arg2 = PG_GETARG_INT16(1);
565 
566  PG_RETURN_BOOL(arg1 == arg2);
567 }
568 
569 Datum
571 {
572  int32 arg1 = PG_GETARG_INT32(0);
573  int16 arg2 = PG_GETARG_INT16(1);
574 
575  PG_RETURN_BOOL(arg1 != arg2);
576 }
577 
578 Datum
580 {
581  int32 arg1 = PG_GETARG_INT32(0);
582  int16 arg2 = PG_GETARG_INT16(1);
583 
584  PG_RETURN_BOOL(arg1 < arg2);
585 }
586 
587 Datum
589 {
590  int32 arg1 = PG_GETARG_INT32(0);
591  int16 arg2 = PG_GETARG_INT16(1);
592 
593  PG_RETURN_BOOL(arg1 <= arg2);
594 }
595 
596 Datum
598 {
599  int32 arg1 = PG_GETARG_INT32(0);
600  int16 arg2 = PG_GETARG_INT16(1);
601 
602  PG_RETURN_BOOL(arg1 > arg2);
603 }
604 
605 Datum
607 {
608  int32 arg1 = PG_GETARG_INT32(0);
609  int16 arg2 = PG_GETARG_INT16(1);
610 
611  PG_RETURN_BOOL(arg1 >= arg2);
612 }
613 
614 
615 /*----------------------------------------------------------
616  * in_range functions for int4 and int2,
617  * including cross-data-type comparisons.
618  *
619  * Note: we provide separate intN_int8 functions for performance
620  * reasons. This forces also providing intN_int2, else cases with a
621  * smallint offset value would fail to resolve which function to use.
622  * But that's an unlikely situation, so don't duplicate code for it.
623  *---------------------------------------------------------*/
624 
625 Datum
627 {
629  int32 base = PG_GETARG_INT32(1);
630  int32 offset = PG_GETARG_INT32(2);
631  bool sub = PG_GETARG_BOOL(3);
632  bool less = PG_GETARG_BOOL(4);
633  int32 sum;
634 
635  if (offset < 0)
636  ereport(ERROR,
637  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
638  errmsg("invalid preceding or following size in window function")));
639 
640  if (sub)
641  offset = -offset; /* cannot overflow */
642 
643  if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
644  {
645  /*
646  * If sub is false, the true sum is surely more than val, so correct
647  * answer is the same as "less". If sub is true, the true sum is
648  * surely less than val, so the answer is "!less".
649  */
650  PG_RETURN_BOOL(sub ? !less : less);
651  }
652 
653  if (less)
654  PG_RETURN_BOOL(val <= sum);
655  else
656  PG_RETURN_BOOL(val >= sum);
657 }
658 
659 Datum
661 {
662  /* Doesn't seem worth duplicating code for, so just invoke int4_int4 */
664  PG_GETARG_DATUM(0),
665  PG_GETARG_DATUM(1),
667  PG_GETARG_DATUM(3),
668  PG_GETARG_DATUM(4));
669 }
670 
671 Datum
673 {
674  /* We must do all the math in int64 */
675  int64 val = (int64) PG_GETARG_INT32(0);
676  int64 base = (int64) PG_GETARG_INT32(1);
677  int64 offset = PG_GETARG_INT64(2);
678  bool sub = PG_GETARG_BOOL(3);
679  bool less = PG_GETARG_BOOL(4);
680  int64 sum;
681 
682  if (offset < 0)
683  ereport(ERROR,
684  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
685  errmsg("invalid preceding or following size in window function")));
686 
687  if (sub)
688  offset = -offset; /* cannot overflow */
689 
690  if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
691  {
692  /*
693  * If sub is false, the true sum is surely more than val, so correct
694  * answer is the same as "less". If sub is true, the true sum is
695  * surely less than val, so the answer is "!less".
696  */
697  PG_RETURN_BOOL(sub ? !less : less);
698  }
699 
700  if (less)
701  PG_RETURN_BOOL(val <= sum);
702  else
703  PG_RETURN_BOOL(val >= sum);
704 }
705 
706 Datum
708 {
709  /* We must do all the math in int32 */
711  int32 base = (int32) PG_GETARG_INT16(1);
712  int32 offset = PG_GETARG_INT32(2);
713  bool sub = PG_GETARG_BOOL(3);
714  bool less = PG_GETARG_BOOL(4);
715  int32 sum;
716 
717  if (offset < 0)
718  ereport(ERROR,
719  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
720  errmsg("invalid preceding or following size in window function")));
721 
722  if (sub)
723  offset = -offset; /* cannot overflow */
724 
725  if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
726  {
727  /*
728  * If sub is false, the true sum is surely more than val, so correct
729  * answer is the same as "less". If sub is true, the true sum is
730  * surely less than val, so the answer is "!less".
731  */
732  PG_RETURN_BOOL(sub ? !less : less);
733  }
734 
735  if (less)
736  PG_RETURN_BOOL(val <= sum);
737  else
738  PG_RETURN_BOOL(val >= sum);
739 }
740 
741 Datum
743 {
744  /* Doesn't seem worth duplicating code for, so just invoke int2_int4 */
746  PG_GETARG_DATUM(0),
747  PG_GETARG_DATUM(1),
749  PG_GETARG_DATUM(3),
750  PG_GETARG_DATUM(4));
751 }
752 
753 Datum
755 {
756  /* Doesn't seem worth duplicating code for, so just invoke int4_int8 */
760  PG_GETARG_DATUM(2),
761  PG_GETARG_DATUM(3),
762  PG_GETARG_DATUM(4));
763 }
764 
765 
766 /*
767  * int[24]pl - returns arg1 + arg2
768  * int[24]mi - returns arg1 - arg2
769  * int[24]mul - returns arg1 * arg2
770  * int[24]div - returns arg1 / arg2
771  */
772 
773 Datum
775 {
777 
778  if (unlikely(arg == PG_INT32_MIN))
779  ereport(ERROR,
780  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
781  errmsg("integer out of range")));
783 }
784 
785 Datum
787 {
789 
791 }
792 
793 Datum
795 {
796  int32 arg1 = PG_GETARG_INT32(0);
797  int32 arg2 = PG_GETARG_INT32(1);
798  int32 result;
799 
800  if (unlikely(pg_add_s32_overflow(arg1, arg2, &result)))
801  ereport(ERROR,
802  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
803  errmsg("integer out of range")));
804  PG_RETURN_INT32(result);
805 }
806 
807 Datum
809 {
810  int32 arg1 = PG_GETARG_INT32(0);
811  int32 arg2 = PG_GETARG_INT32(1);
812  int32 result;
813 
814  if (unlikely(pg_sub_s32_overflow(arg1, arg2, &result)))
815  ereport(ERROR,
816  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
817  errmsg("integer out of range")));
818  PG_RETURN_INT32(result);
819 }
820 
821 Datum
823 {
824  int32 arg1 = PG_GETARG_INT32(0);
825  int32 arg2 = PG_GETARG_INT32(1);
826  int32 result;
827 
828  if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
829  ereport(ERROR,
830  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
831  errmsg("integer out of range")));
832  PG_RETURN_INT32(result);
833 }
834 
835 Datum
837 {
838  int32 arg1 = PG_GETARG_INT32(0);
839  int32 arg2 = PG_GETARG_INT32(1);
840  int32 result;
841 
842  if (arg2 == 0)
843  {
844  ereport(ERROR,
845  (errcode(ERRCODE_DIVISION_BY_ZERO),
846  errmsg("division by zero")));
847  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
848  PG_RETURN_NULL();
849  }
850 
851  /*
852  * INT_MIN / -1 is problematic, since the result can't be represented on a
853  * two's-complement machine. Some machines produce INT_MIN, some produce
854  * zero, some throw an exception. We can dodge the problem by recognizing
855  * that division by -1 is the same as negation.
856  */
857  if (arg2 == -1)
858  {
859  if (unlikely(arg1 == PG_INT32_MIN))
860  ereport(ERROR,
861  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
862  errmsg("integer out of range")));
863  result = -arg1;
864  PG_RETURN_INT32(result);
865  }
866 
867  /* No overflow is possible */
868 
869  result = arg1 / arg2;
870 
871  PG_RETURN_INT32(result);
872 }
873 
874 Datum
876 {
878  int32 result;
879 
880  if (unlikely(pg_add_s32_overflow(arg, 1, &result)))
881  ereport(ERROR,
882  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
883  errmsg("integer out of range")));
884 
885  PG_RETURN_INT32(result);
886 }
887 
888 Datum
890 {
892 
893  if (unlikely(arg == PG_INT16_MIN))
894  ereport(ERROR,
895  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
896  errmsg("smallint out of range")));
898 }
899 
900 Datum
902 {
904 
906 }
907 
908 Datum
910 {
911  int16 arg1 = PG_GETARG_INT16(0);
912  int16 arg2 = PG_GETARG_INT16(1);
913  int16 result;
914 
915  if (unlikely(pg_add_s16_overflow(arg1, arg2, &result)))
916  ereport(ERROR,
917  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
918  errmsg("smallint out of range")));
919  PG_RETURN_INT16(result);
920 }
921 
922 Datum
924 {
925  int16 arg1 = PG_GETARG_INT16(0);
926  int16 arg2 = PG_GETARG_INT16(1);
927  int16 result;
928 
929  if (unlikely(pg_sub_s16_overflow(arg1, arg2, &result)))
930  ereport(ERROR,
931  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
932  errmsg("smallint out of range")));
933  PG_RETURN_INT16(result);
934 }
935 
936 Datum
938 {
939  int16 arg1 = PG_GETARG_INT16(0);
940  int16 arg2 = PG_GETARG_INT16(1);
941  int16 result;
942 
943  if (unlikely(pg_mul_s16_overflow(arg1, arg2, &result)))
944  ereport(ERROR,
945  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
946  errmsg("smallint out of range")));
947 
948  PG_RETURN_INT16(result);
949 }
950 
951 Datum
953 {
954  int16 arg1 = PG_GETARG_INT16(0);
955  int16 arg2 = PG_GETARG_INT16(1);
956  int16 result;
957 
958  if (arg2 == 0)
959  {
960  ereport(ERROR,
961  (errcode(ERRCODE_DIVISION_BY_ZERO),
962  errmsg("division by zero")));
963  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
964  PG_RETURN_NULL();
965  }
966 
967  /*
968  * SHRT_MIN / -1 is problematic, since the result can't be represented on
969  * a two's-complement machine. Some machines produce SHRT_MIN, some
970  * produce zero, some throw an exception. We can dodge the problem by
971  * recognizing that division by -1 is the same as negation.
972  */
973  if (arg2 == -1)
974  {
975  if (unlikely(arg1 == PG_INT16_MIN))
976  ereport(ERROR,
977  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
978  errmsg("smallint out of range")));
979  result = -arg1;
980  PG_RETURN_INT16(result);
981  }
982 
983  /* No overflow is possible */
984 
985  result = arg1 / arg2;
986 
987  PG_RETURN_INT16(result);
988 }
989 
990 Datum
992 {
993  int16 arg1 = PG_GETARG_INT16(0);
994  int32 arg2 = PG_GETARG_INT32(1);
995  int32 result;
996 
997  if (unlikely(pg_add_s32_overflow((int32) arg1, arg2, &result)))
998  ereport(ERROR,
999  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1000  errmsg("integer out of range")));
1001  PG_RETURN_INT32(result);
1002 }
1003 
1004 Datum
1006 {
1007  int16 arg1 = PG_GETARG_INT16(0);
1008  int32 arg2 = PG_GETARG_INT32(1);
1009  int32 result;
1010 
1011  if (unlikely(pg_sub_s32_overflow((int32) arg1, arg2, &result)))
1012  ereport(ERROR,
1013  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1014  errmsg("integer out of range")));
1015  PG_RETURN_INT32(result);
1016 }
1017 
1018 Datum
1020 {
1021  int16 arg1 = PG_GETARG_INT16(0);
1022  int32 arg2 = PG_GETARG_INT32(1);
1023  int32 result;
1024 
1025  if (unlikely(pg_mul_s32_overflow((int32) arg1, arg2, &result)))
1026  ereport(ERROR,
1027  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1028  errmsg("integer out of range")));
1029  PG_RETURN_INT32(result);
1030 }
1031 
1032 Datum
1034 {
1035  int16 arg1 = PG_GETARG_INT16(0);
1036  int32 arg2 = PG_GETARG_INT32(1);
1037 
1038  if (unlikely(arg2 == 0))
1039  {
1040  ereport(ERROR,
1041  (errcode(ERRCODE_DIVISION_BY_ZERO),
1042  errmsg("division by zero")));
1043  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1044  PG_RETURN_NULL();
1045  }
1046 
1047  /* No overflow is possible */
1048  PG_RETURN_INT32((int32) arg1 / arg2);
1049 }
1050 
1051 Datum
1053 {
1054  int32 arg1 = PG_GETARG_INT32(0);
1055  int16 arg2 = PG_GETARG_INT16(1);
1056  int32 result;
1057 
1058  if (unlikely(pg_add_s32_overflow(arg1, (int32) arg2, &result)))
1059  ereport(ERROR,
1060  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1061  errmsg("integer out of range")));
1062  PG_RETURN_INT32(result);
1063 }
1064 
1065 Datum
1067 {
1068  int32 arg1 = PG_GETARG_INT32(0);
1069  int16 arg2 = PG_GETARG_INT16(1);
1070  int32 result;
1071 
1072  if (unlikely(pg_sub_s32_overflow(arg1, (int32) arg2, &result)))
1073  ereport(ERROR,
1074  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1075  errmsg("integer out of range")));
1076  PG_RETURN_INT32(result);
1077 }
1078 
1079 Datum
1081 {
1082  int32 arg1 = PG_GETARG_INT32(0);
1083  int16 arg2 = PG_GETARG_INT16(1);
1084  int32 result;
1085 
1086  if (unlikely(pg_mul_s32_overflow(arg1, (int32) arg2, &result)))
1087  ereport(ERROR,
1088  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1089  errmsg("integer out of range")));
1090  PG_RETURN_INT32(result);
1091 }
1092 
1093 Datum
1095 {
1096  int32 arg1 = PG_GETARG_INT32(0);
1097  int16 arg2 = PG_GETARG_INT16(1);
1098  int32 result;
1099 
1100  if (unlikely(arg2 == 0))
1101  {
1102  ereport(ERROR,
1103  (errcode(ERRCODE_DIVISION_BY_ZERO),
1104  errmsg("division by zero")));
1105  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1106  PG_RETURN_NULL();
1107  }
1108 
1109  /*
1110  * INT_MIN / -1 is problematic, since the result can't be represented on a
1111  * two's-complement machine. Some machines produce INT_MIN, some produce
1112  * zero, some throw an exception. We can dodge the problem by recognizing
1113  * that division by -1 is the same as negation.
1114  */
1115  if (arg2 == -1)
1116  {
1117  if (unlikely(arg1 == PG_INT32_MIN))
1118  ereport(ERROR,
1119  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1120  errmsg("integer out of range")));
1121  result = -arg1;
1122  PG_RETURN_INT32(result);
1123  }
1124 
1125  /* No overflow is possible */
1126 
1127  result = arg1 / arg2;
1128 
1129  PG_RETURN_INT32(result);
1130 }
1131 
1132 Datum
1134 {
1135  int32 arg1 = PG_GETARG_INT32(0);
1136  int32 arg2 = PG_GETARG_INT32(1);
1137 
1138  if (unlikely(arg2 == 0))
1139  {
1140  ereport(ERROR,
1141  (errcode(ERRCODE_DIVISION_BY_ZERO),
1142  errmsg("division by zero")));
1143  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1144  PG_RETURN_NULL();
1145  }
1146 
1147  /*
1148  * Some machines throw a floating-point exception for INT_MIN % -1, which
1149  * is a bit silly since the correct answer is perfectly well-defined,
1150  * namely zero.
1151  */
1152  if (arg2 == -1)
1153  PG_RETURN_INT32(0);
1154 
1155  /* No overflow is possible */
1156 
1157  PG_RETURN_INT32(arg1 % arg2);
1158 }
1159 
1160 Datum
1162 {
1163  int16 arg1 = PG_GETARG_INT16(0);
1164  int16 arg2 = PG_GETARG_INT16(1);
1165 
1166  if (unlikely(arg2 == 0))
1167  {
1168  ereport(ERROR,
1169  (errcode(ERRCODE_DIVISION_BY_ZERO),
1170  errmsg("division by zero")));
1171  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1172  PG_RETURN_NULL();
1173  }
1174 
1175  /*
1176  * Some machines throw a floating-point exception for INT_MIN % -1, which
1177  * is a bit silly since the correct answer is perfectly well-defined,
1178  * namely zero. (It's not clear this ever happens when dealing with
1179  * int16, but we might as well have the test for safety.)
1180  */
1181  if (arg2 == -1)
1182  PG_RETURN_INT16(0);
1183 
1184  /* No overflow is possible */
1185 
1186  PG_RETURN_INT16(arg1 % arg2);
1187 }
1188 
1189 
1190 /* int[24]abs()
1191  * Absolute value
1192  */
1193 Datum
1195 {
1196  int32 arg1 = PG_GETARG_INT32(0);
1197  int32 result;
1198 
1199  if (unlikely(arg1 == PG_INT32_MIN))
1200  ereport(ERROR,
1201  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1202  errmsg("integer out of range")));
1203  result = (arg1 < 0) ? -arg1 : arg1;
1204  PG_RETURN_INT32(result);
1205 }
1206 
1207 Datum
1209 {
1210  int16 arg1 = PG_GETARG_INT16(0);
1211  int16 result;
1212 
1213  if (unlikely(arg1 == PG_INT16_MIN))
1214  ereport(ERROR,
1215  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1216  errmsg("smallint out of range")));
1217  result = (arg1 < 0) ? -arg1 : arg1;
1218  PG_RETURN_INT16(result);
1219 }
1220 
1221 /*
1222  * Greatest Common Divisor
1223  *
1224  * Returns the largest positive integer that exactly divides both inputs.
1225  * Special cases:
1226  * - gcd(x, 0) = gcd(0, x) = abs(x)
1227  * because 0 is divisible by anything
1228  * - gcd(0, 0) = 0
1229  * complies with the previous definition and is a common convention
1230  *
1231  * Special care must be taken if either input is INT_MIN --- gcd(0, INT_MIN),
1232  * gcd(INT_MIN, 0) and gcd(INT_MIN, INT_MIN) are all equal to abs(INT_MIN),
1233  * which cannot be represented as a 32-bit signed integer.
1234  */
1235 static int32
1237 {
1238  int32 swap;
1239  int32 a1,
1240  a2;
1241 
1242  /*
1243  * Put the greater absolute value in arg1.
1244  *
1245  * This would happen automatically in the loop below, but avoids an
1246  * expensive modulo operation, and simplifies the special-case handling
1247  * for INT_MIN below.
1248  *
1249  * We do this in negative space in order to handle INT_MIN.
1250  */
1251  a1 = (arg1 < 0) ? arg1 : -arg1;
1252  a2 = (arg2 < 0) ? arg2 : -arg2;
1253  if (a1 > a2)
1254  {
1255  swap = arg1;
1256  arg1 = arg2;
1257  arg2 = swap;
1258  }
1259 
1260  /* Special care needs to be taken with INT_MIN. See comments above. */
1261  if (arg1 == PG_INT32_MIN)
1262  {
1263  if (arg2 == 0 || arg2 == PG_INT32_MIN)
1264  ereport(ERROR,
1265  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1266  errmsg("integer out of range")));
1267 
1268  /*
1269  * Some machines throw a floating-point exception for INT_MIN % -1,
1270  * which is a bit silly since the correct answer is perfectly
1271  * well-defined, namely zero. Guard against this and just return the
1272  * result, gcd(INT_MIN, -1) = 1.
1273  */
1274  if (arg2 == -1)
1275  return 1;
1276  }
1277 
1278  /* Use the Euclidean algorithm to find the GCD */
1279  while (arg2 != 0)
1280  {
1281  swap = arg2;
1282  arg2 = arg1 % arg2;
1283  arg1 = swap;
1284  }
1285 
1286  /*
1287  * Make sure the result is positive. (We know we don't have INT_MIN
1288  * anymore).
1289  */
1290  if (arg1 < 0)
1291  arg1 = -arg1;
1292 
1293  return arg1;
1294 }
1295 
1296 Datum
1298 {
1299  int32 arg1 = PG_GETARG_INT32(0);
1300  int32 arg2 = PG_GETARG_INT32(1);
1301  int32 result;
1302 
1303  result = int4gcd_internal(arg1, arg2);
1304 
1305  PG_RETURN_INT32(result);
1306 }
1307 
1308 /*
1309  * Least Common Multiple
1310  */
1311 Datum
1313 {
1314  int32 arg1 = PG_GETARG_INT32(0);
1315  int32 arg2 = PG_GETARG_INT32(1);
1316  int32 gcd;
1317  int32 result;
1318 
1319  /*
1320  * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a
1321  * division-by-zero error below when x is zero, and an overflow error from
1322  * the GCD computation when x = INT_MIN.
1323  */
1324  if (arg1 == 0 || arg2 == 0)
1325  PG_RETURN_INT32(0);
1326 
1327  /* lcm(x, y) = abs(x / gcd(x, y) * y) */
1328  gcd = int4gcd_internal(arg1, arg2);
1329  arg1 = arg1 / gcd;
1330 
1331  if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
1332  ereport(ERROR,
1333  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1334  errmsg("integer out of range")));
1335 
1336  /* If the result is INT_MIN, it cannot be represented. */
1337  if (unlikely(result == PG_INT32_MIN))
1338  ereport(ERROR,
1339  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1340  errmsg("integer out of range")));
1341 
1342  if (result < 0)
1343  result = -result;
1344 
1345  PG_RETURN_INT32(result);
1346 }
1347 
1348 Datum
1350 {
1351  int16 arg1 = PG_GETARG_INT16(0);
1352  int16 arg2 = PG_GETARG_INT16(1);
1353 
1354  PG_RETURN_INT16((arg1 > arg2) ? arg1 : arg2);
1355 }
1356 
1357 Datum
1359 {
1360  int16 arg1 = PG_GETARG_INT16(0);
1361  int16 arg2 = PG_GETARG_INT16(1);
1362 
1363  PG_RETURN_INT16((arg1 < arg2) ? arg1 : arg2);
1364 }
1365 
1366 Datum
1368 {
1369  int32 arg1 = PG_GETARG_INT32(0);
1370  int32 arg2 = PG_GETARG_INT32(1);
1371 
1372  PG_RETURN_INT32((arg1 > arg2) ? arg1 : arg2);
1373 }
1374 
1375 Datum
1377 {
1378  int32 arg1 = PG_GETARG_INT32(0);
1379  int32 arg2 = PG_GETARG_INT32(1);
1380 
1381  PG_RETURN_INT32((arg1 < arg2) ? arg1 : arg2);
1382 }
1383 
1384 /*
1385  * Bit-pushing operators
1386  *
1387  * int[24]and - returns arg1 & arg2
1388  * int[24]or - returns arg1 | arg2
1389  * int[24]xor - returns arg1 # arg2
1390  * int[24]not - returns ~arg1
1391  * int[24]shl - returns arg1 << arg2
1392  * int[24]shr - returns arg1 >> arg2
1393  */
1394 
1395 Datum
1397 {
1398  int32 arg1 = PG_GETARG_INT32(0);
1399  int32 arg2 = PG_GETARG_INT32(1);
1400 
1401  PG_RETURN_INT32(arg1 & arg2);
1402 }
1403 
1404 Datum
1406 {
1407  int32 arg1 = PG_GETARG_INT32(0);
1408  int32 arg2 = PG_GETARG_INT32(1);
1409 
1410  PG_RETURN_INT32(arg1 | arg2);
1411 }
1412 
1413 Datum
1415 {
1416  int32 arg1 = PG_GETARG_INT32(0);
1417  int32 arg2 = PG_GETARG_INT32(1);
1418 
1419  PG_RETURN_INT32(arg1 ^ arg2);
1420 }
1421 
1422 Datum
1424 {
1425  int32 arg1 = PG_GETARG_INT32(0);
1426  int32 arg2 = PG_GETARG_INT32(1);
1427 
1428  PG_RETURN_INT32(arg1 << arg2);
1429 }
1430 
1431 Datum
1433 {
1434  int32 arg1 = PG_GETARG_INT32(0);
1435  int32 arg2 = PG_GETARG_INT32(1);
1436 
1437  PG_RETURN_INT32(arg1 >> arg2);
1438 }
1439 
1440 Datum
1442 {
1443  int32 arg1 = PG_GETARG_INT32(0);
1444 
1445  PG_RETURN_INT32(~arg1);
1446 }
1447 
1448 Datum
1450 {
1451  int16 arg1 = PG_GETARG_INT16(0);
1452  int16 arg2 = PG_GETARG_INT16(1);
1453 
1454  PG_RETURN_INT16(arg1 & arg2);
1455 }
1456 
1457 Datum
1459 {
1460  int16 arg1 = PG_GETARG_INT16(0);
1461  int16 arg2 = PG_GETARG_INT16(1);
1462 
1463  PG_RETURN_INT16(arg1 | arg2);
1464 }
1465 
1466 Datum
1468 {
1469  int16 arg1 = PG_GETARG_INT16(0);
1470  int16 arg2 = PG_GETARG_INT16(1);
1471 
1472  PG_RETURN_INT16(arg1 ^ arg2);
1473 }
1474 
1475 Datum
1477 {
1478  int16 arg1 = PG_GETARG_INT16(0);
1479 
1480  PG_RETURN_INT16(~arg1);
1481 }
1482 
1483 
1484 Datum
1486 {
1487  int16 arg1 = PG_GETARG_INT16(0);
1488  int32 arg2 = PG_GETARG_INT32(1);
1489 
1490  PG_RETURN_INT16(arg1 << arg2);
1491 }
1492 
1493 Datum
1495 {
1496  int16 arg1 = PG_GETARG_INT16(0);
1497  int32 arg2 = PG_GETARG_INT32(1);
1498 
1499  PG_RETURN_INT16(arg1 >> arg2);
1500 }
1501 
1502 /*
1503  * non-persistent numeric series generator
1504  */
1505 Datum
1507 {
1508  return generate_series_step_int4(fcinfo);
1509 }
1510 
1511 Datum
1513 {
1514  FuncCallContext *funcctx;
1515  generate_series_fctx *fctx;
1516  int32 result;
1517  MemoryContext oldcontext;
1518 
1519  /* stuff done only on the first call of the function */
1520  if (SRF_IS_FIRSTCALL())
1521  {
1522  int32 start = PG_GETARG_INT32(0);
1523  int32 finish = PG_GETARG_INT32(1);
1524  int32 step = 1;
1525 
1526  /* see if we were given an explicit step size */
1527  if (PG_NARGS() == 3)
1528  step = PG_GETARG_INT32(2);
1529  if (step == 0)
1530  ereport(ERROR,
1531  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1532  errmsg("step size cannot equal zero")));
1533 
1534  /* create a function context for cross-call persistence */
1535  funcctx = SRF_FIRSTCALL_INIT();
1536 
1537  /*
1538  * switch to memory context appropriate for multiple function calls
1539  */
1540  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1541 
1542  /* allocate memory for user context */
1543  fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1544 
1545  /*
1546  * Use fctx to keep state from call to call. Seed current with the
1547  * original start value
1548  */
1549  fctx->current = start;
1550  fctx->finish = finish;
1551  fctx->step = step;
1552 
1553  funcctx->user_fctx = fctx;
1554  MemoryContextSwitchTo(oldcontext);
1555  }
1556 
1557  /* stuff done on every call of the function */
1558  funcctx = SRF_PERCALL_SETUP();
1559 
1560  /*
1561  * get the saved state and use current as the result for this iteration
1562  */
1563  fctx = funcctx->user_fctx;
1564  result = fctx->current;
1565 
1566  if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1567  (fctx->step < 0 && fctx->current >= fctx->finish))
1568  {
1569  /*
1570  * Increment current in preparation for next iteration. If next-value
1571  * computation overflows, this is the final result.
1572  */
1573  if (pg_add_s32_overflow(fctx->current, fctx->step, &fctx->current))
1574  fctx->step = 0;
1575 
1576  /* do when there is more left to send */
1577  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
1578  }
1579  else
1580  /* do when there is no more left */
1581  SRF_RETURN_DONE(funcctx);
1582 }
1583 
1584 /*
1585  * Planner support function for generate_series(int4, int4 [, int4])
1586  */
1587 Datum
1589 {
1590  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1591  Node *ret = NULL;
1592 
1593  if (IsA(rawreq, SupportRequestRows))
1594  {
1595  /* Try to estimate the number of rows returned */
1596  SupportRequestRows *req = (SupportRequestRows *) rawreq;
1597 
1598  if (is_funcclause(req->node)) /* be paranoid */
1599  {
1600  List *args = ((FuncExpr *) req->node)->args;
1601  Node *arg1,
1602  *arg2,
1603  *arg3;
1604 
1605  /* We can use estimated argument values here */
1607  arg2 = estimate_expression_value(req->root, lsecond(args));
1608  if (list_length(args) >= 3)
1609  arg3 = estimate_expression_value(req->root, lthird(args));
1610  else
1611  arg3 = NULL;
1612 
1613  /*
1614  * If any argument is constant NULL, we can safely assume that
1615  * zero rows are returned. Otherwise, if they're all non-NULL
1616  * constants, we can calculate the number of rows that will be
1617  * returned. Use double arithmetic to avoid overflow hazards.
1618  */
1619  if ((IsA(arg1, Const) &&
1620  ((Const *) arg1)->constisnull) ||
1621  (IsA(arg2, Const) &&
1622  ((Const *) arg2)->constisnull) ||
1623  (arg3 != NULL && IsA(arg3, Const) &&
1624  ((Const *) arg3)->constisnull))
1625  {
1626  req->rows = 0;
1627  ret = (Node *) req;
1628  }
1629  else if (IsA(arg1, Const) &&
1630  IsA(arg2, Const) &&
1631  (arg3 == NULL || IsA(arg3, Const)))
1632  {
1633  double start,
1634  finish,
1635  step;
1636 
1637  start = DatumGetInt32(((Const *) arg1)->constvalue);
1638  finish = DatumGetInt32(((Const *) arg2)->constvalue);
1639  step = arg3 ? DatumGetInt32(((Const *) arg3)->constvalue) : 1;
1640 
1641  /* This equation works for either sign of step */
1642  if (step != 0)
1643  {
1644  req->rows = floor((finish - start + step) / step);
1645  ret = (Node *) req;
1646  }
1647  }
1648  }
1649  }
1650 
1651  PG_RETURN_POINTER(ret);
1652 }
#define ARR_NDIM(a)
Definition: array.h:283
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_HASNULL(a)
Definition: array.h:284
#define ARR_LBOUND(a)
Definition: array.h:289
Datum array_recv(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1271
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1559
signed short int16
Definition: c.h:428
signed int int32
Definition: c.h:429
#define PG_INT16_MIN
Definition: c.h:520
#define unlikely(x)
Definition: c.h:273
#define PG_INT32_MIN
Definition: c.h:523
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2290
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:356
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:639
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:299
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:305
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:301
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:323
static const FormData_pg_attribute a1
Definition: heap.c:141
static const FormData_pg_attribute a2
Definition: heap.c:155
long val
Definition: informix.c:664
Datum int4div(PG_FUNCTION_ARGS)
Definition: int.c:836
Datum int4shl(PG_FUNCTION_ARGS)
Definition: int.c:1423
Datum int2vectorrecv(PG_FUNCTION_ARGS)
Definition: int.c:228
Datum int2not(PG_FUNCTION_ARGS)
Definition: int.c:1476
Datum int4pl(PG_FUNCTION_ARGS)
Definition: int.c:794
Datum int2xor(PG_FUNCTION_ARGS)
Definition: int.c:1467
Datum generate_series_int4(PG_FUNCTION_ARGS)
Definition: int.c:1506
Datum int4recv(PG_FUNCTION_ARGS)
Definition: int.c:314
Datum int4um(PG_FUNCTION_ARGS)
Definition: int.c:774
Datum int4gt(PG_FUNCTION_ARGS)
Definition: int.c:435
Datum int24eq(PG_FUNCTION_ARGS)
Definition: int.c:507
Datum int2lt(PG_FUNCTION_ARGS)
Definition: int.c:471
Datum int24mi(PG_FUNCTION_ARGS)
Definition: int.c:1005
Datum int2out(PG_FUNCTION_ARGS)
Definition: int.c:74
Datum int2eq(PG_FUNCTION_ARGS)
Definition: int.c:453
Datum int42eq(PG_FUNCTION_ARGS)
Definition: int.c:561
Datum int2ge(PG_FUNCTION_ARGS)
Definition: int.c:498
Datum int2up(PG_FUNCTION_ARGS)
Definition: int.c:901
Datum int4gcd(PG_FUNCTION_ARGS)
Definition: int.c:1297
Datum int42ge(PG_FUNCTION_ARGS)
Definition: int.c:606
Datum int42mul(PG_FUNCTION_ARGS)
Definition: int.c:1080
Datum int24gt(PG_FUNCTION_ARGS)
Definition: int.c:543
Datum int2le(PG_FUNCTION_ARGS)
Definition: int.c:480
Datum int2abs(PG_FUNCTION_ARGS)
Definition: int.c:1208
#define Int2VectorSize(n)
Definition: int.c:45
Datum int4lt(PG_FUNCTION_ARGS)
Definition: int.c:417
Datum int2um(PG_FUNCTION_ARGS)
Definition: int.c:889
Datum int2shl(PG_FUNCTION_ARGS)
Definition: int.c:1485
Datum in_range_int2_int8(PG_FUNCTION_ARGS)
Definition: int.c:754
Datum int42div(PG_FUNCTION_ARGS)
Definition: int.c:1094
Datum int2mi(PG_FUNCTION_ARGS)
Definition: int.c:923
Datum int2ne(PG_FUNCTION_ARGS)
Definition: int.c:462
Datum int24le(PG_FUNCTION_ARGS)
Definition: int.c:534
Datum int2send(PG_FUNCTION_ARGS)
Definition: int.c:98
Datum int2mul(PG_FUNCTION_ARGS)
Definition: int.c:937
Datum int2smaller(PG_FUNCTION_ARGS)
Definition: int.c:1358
Datum int2mod(PG_FUNCTION_ARGS)
Definition: int.c:1161
Datum int2in(PG_FUNCTION_ARGS)
Definition: int.c:63
Datum int24mul(PG_FUNCTION_ARGS)
Definition: int.c:1019
Datum int42gt(PG_FUNCTION_ARGS)
Definition: int.c:597
Datum int4lcm(PG_FUNCTION_ARGS)
Definition: int.c:1312
Datum in_range_int4_int8(PG_FUNCTION_ARGS)
Definition: int.c:672
Datum int2div(PG_FUNCTION_ARGS)
Definition: int.c:952
Datum in_range_int2_int2(PG_FUNCTION_ARGS)
Definition: int.c:742
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:290
Datum int2shr(PG_FUNCTION_ARGS)
Definition: int.c:1494
Datum in_range_int2_int4(PG_FUNCTION_ARGS)
Definition: int.c:707
static int32 int4gcd_internal(int32 arg1, int32 arg2)
Definition: int.c:1236
Datum int2vectorout(PG_FUNCTION_ARGS)
Definition: int.c:204
Datum i4toi2(PG_FUNCTION_ARGS)
Definition: int.c:351
Datum int4ge(PG_FUNCTION_ARGS)
Definition: int.c:444
Datum int4and(PG_FUNCTION_ARGS)
Definition: int.c:1396
Datum int4send(PG_FUNCTION_ARGS)
Definition: int.c:325
Datum int24ge(PG_FUNCTION_ARGS)
Definition: int.c:552
Datum in_range_int4_int2(PG_FUNCTION_ARGS)
Definition: int.c:660
Datum int2vectorin(PG_FUNCTION_ARGS)
Definition: int.c:141
Datum bool_int4(PG_FUNCTION_ARGS)
Definition: int.c:375
Datum int4inc(PG_FUNCTION_ARGS)
Definition: int.c:875
Datum int4shr(PG_FUNCTION_ARGS)
Definition: int.c:1432
Datum int4larger(PG_FUNCTION_ARGS)
Definition: int.c:1367
Datum int24ne(PG_FUNCTION_ARGS)
Definition: int.c:516
Datum int2gt(PG_FUNCTION_ARGS)
Definition: int.c:489
Datum int24lt(PG_FUNCTION_ARGS)
Definition: int.c:525
Datum int42pl(PG_FUNCTION_ARGS)
Definition: int.c:1052
Datum in_range_int4_int4(PG_FUNCTION_ARGS)
Definition: int.c:626
Datum int4ne(PG_FUNCTION_ARGS)
Definition: int.c:408
Datum int42le(PG_FUNCTION_ARGS)
Definition: int.c:588
Datum i2toi4(PG_FUNCTION_ARGS)
Definition: int.c:343
Datum int4smaller(PG_FUNCTION_ARGS)
Definition: int.c:1376
Datum int24pl(PG_FUNCTION_ARGS)
Definition: int.c:991
Datum int4_bool(PG_FUNCTION_ARGS)
Definition: int.c:365
Datum int4mod(PG_FUNCTION_ARGS)
Definition: int.c:1133
Datum int24div(PG_FUNCTION_ARGS)
Definition: int.c:1033
Datum int4xor(PG_FUNCTION_ARGS)
Definition: int.c:1414
Datum generate_series_step_int4(PG_FUNCTION_ARGS)
Definition: int.c:1512
Datum int2larger(PG_FUNCTION_ARGS)
Definition: int.c:1349
int2vector * buildint2vector(const int16 *int2s, int n)
Definition: int.c:114
Datum int2and(PG_FUNCTION_ARGS)
Definition: int.c:1449
Datum int4not(PG_FUNCTION_ARGS)
Definition: int.c:1441
Datum int4abs(PG_FUNCTION_ARGS)
Definition: int.c:1194
Datum int4out(PG_FUNCTION_ARGS)
Definition: int.c:301
Datum generate_series_int4_support(PG_FUNCTION_ARGS)
Definition: int.c:1588
Datum int4mul(PG_FUNCTION_ARGS)
Definition: int.c:822
Datum int4eq(PG_FUNCTION_ARGS)
Definition: int.c:399
Datum int4le(PG_FUNCTION_ARGS)
Definition: int.c:426
Datum int42ne(PG_FUNCTION_ARGS)
Definition: int.c:570
Datum int4mi(PG_FUNCTION_ARGS)
Definition: int.c:808
Datum int42lt(PG_FUNCTION_ARGS)
Definition: int.c:579
Datum int2pl(PG_FUNCTION_ARGS)
Definition: int.c:909
Datum int4up(PG_FUNCTION_ARGS)
Definition: int.c:786
Datum int2vectorsend(PG_FUNCTION_ARGS)
Definition: int.c:276
Datum int2recv(PG_FUNCTION_ARGS)
Definition: int.c:87
Datum int42mi(PG_FUNCTION_ARGS)
Definition: int.c:1066
Datum int4or(PG_FUNCTION_ARGS)
Definition: int.c:1405
Datum int2or(PG_FUNCTION_ARGS)
Definition: int.c:1458
static bool pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:65
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
static bool pg_mul_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:83
static bool pg_add_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:47
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:122
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:161
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1099
void * palloc(Size size)
Definition: mcxt.c:1068
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:57
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:412
int16 pg_strtoint16(const char *s)
Definition: numutils.c:99
int pg_itoa(int16 i, char *a)
Definition: numutils.c:334
int32 pg_strtoint32(const char *s)
Definition: numutils.c:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * arg
#define FUNC_MAX_ARGS
static int list_length(const List *l)
Definition: pg_list.h:149
#define lthird(l)
Definition: pg_list.h:184
#define linitial(l)
Definition: pg_list.h:174
#define lsecond(l)
Definition: pg_list.h:179
static char * buf
Definition: pg_test_fsync.c:67
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define DatumGetInt32(X)
Definition: postgres.h:516
#define Int32GetDatum(X)
Definition: postgres.h:523
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define PointerGetDatum(X)
Definition: postgres.h:600
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
StringInfoData * StringInfo
Definition: stringinfo.h:44
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Definition: pg_list.h:51
Definition: nodes.h:574
struct PlannerInfo * root
Definition: supportnodes.h:163
int32 current
Definition: int.c:49
int32 finish
Definition: int.c:50
Definition: c.h:650
int ndim
Definition: c.h:652
int dim1
Definition: c.h:655
Oid elemtype
Definition: c.h:654
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:657
int32 dataoffset
Definition: c.h:653
int lbound1
Definition: c.h:656
static uint32 gcd(uint32 a, uint32 b)