PostgreSQL Source Code: src/backend/utils/adt/int.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
30
31#include <ctype.h>
32#include <limits.h>
33#include <math.h>
34
44
45#define Int2VectorSize(n) (offsetof(int2vector, values) + (n) * sizeof(int16))
46
47typedef struct
48{
53
54
55
56
57
58
59
60
61
64{
66
68}
69
70
71
72
75{
77 char *result = (char *) palloc(7);
78
81}
82
83
84
85
88{
90
92}
93
94
95
96
99{
102
106}
107
108
109
110
111
112
115{
117
119
120 if (n > 0 && int2s)
121 memcpy(result->values, int2s, n * sizeof(int16));
122
123
124
125
126
128 result->ndim = 1;
129 result->dataoffset = 0;
131 result->dim1 = n;
133
134 return result;
135}
136
137
138
139
142{
144 Node *escontext = fcinfo->context;
146 int nalloc;
147 int n;
148
149 nalloc = 32;
151
152 for (n = 0;; n++)
153 {
154 long l;
155 char *endp;
156
157 while (*intString && isspace((unsigned char) *intString))
158 intString++;
159 if (*intString == '\0')
160 break;
161
162 if (n >= nalloc)
163 {
164 nalloc *= 2;
166 }
167
168 errno = 0;
169 l = strtol(intString, &endp, 10);
170
171 if (intString == endp)
173 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
174 errmsg("invalid input syntax for type %s: \"%s\"",
175 "smallint", intString)));
176
177 if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
179 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
180 errmsg("value \"%s\" is out of range for type %s", intString,
181 "smallint")));
182
183 if (*endp && *endp != ' ')
185 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
186 errmsg("invalid input syntax for type %s: \"%s\"",
187 "smallint", intString)));
188
189 result->values[n] = l;
190 intString = endp;
191 }
192
194 result->ndim = 1;
195 result->dataoffset = 0;
197 result->dim1 = n;
199
201}
202
203
204
205
208{
210 int num,
211 nnums = int2Array->dim1;
212 char *rp;
213 char *result;
214
215
216 rp = result = (char *) palloc(nnums * 7 + 1);
217 for (num = 0; num < nnums; num++)
218 {
219 if (num != 0)
220 *rp++ = ' ';
222 }
223 *rp = '\0';
225}
226
227
228
229
232{
236
237
238
239
240
241
242
245
247 locfcinfo->args[0].isnull = false;
249 locfcinfo->args[1].isnull = false;
251 locfcinfo->args[2].isnull = false;
252
254
255 Assert(!locfcinfo->isnull);
256
257
263 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
264 errmsg("invalid int2vector data")));
265
267}
268
269
270
271
274{
276}
277
278
279
280
281
282
283
284
285
288{
290
292}
293
294
295
296
299{
301 char *result = (char *) palloc(12);
302
305}
306
307
308
309
312{
314
316}
317
318
319
320
323{
326
330}
331
332
333
334
335
336
337
338
341{
343
345}
346
349{
351
354 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
355 errmsg("smallint out of range")));
356
358}
359
360
363{
366 else
368}
369
370
373{
376 else
378}
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
397{
400
402}
403
406{
409
411}
412
415{
418
420}
421
424{
427
429}
430
433{
436
438}
439
442{
445
447}
448
451{
454
456}
457
460{
463
465}
466
469{
472
474}
475
478{
481
483}
484
487{
490
492}
493
496{
499
501}
502
505{
508
510}
511
514{
517
519}
520
523{
526
528}
529
532{
535
537}
538
541{
544
546}
547
550{
553
555}
556
559{
562
564}
565
568{
571
573}
574
577{
580
582}
583
586{
589
591}
592
595{
598
600}
601
604{
607
609}
610
611
612
613
614
615
616
617
618
619
620
621
624{
631
632 if (offset < 0)
634 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
635 errmsg("invalid preceding or following size in window function")));
636
637 if (sub)
638 offset = -offset;
639
641 {
642
643
644
645
646
648 }
649
650 if (less)
652 else
654}
655
658{
659
666}
667
670{
671
678
679 if (offset < 0)
681 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
682 errmsg("invalid preceding or following size in window function")));
683
684 if (sub)
685 offset = -offset;
686
688 {
689
690
691
692
693
695 }
696
697 if (less)
699 else
701}
702
705{
706
713
714 if (offset < 0)
716 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
717 errmsg("invalid preceding or following size in window function")));
718
719 if (sub)
720 offset = -offset;
721
723 {
724
725
726
727
728
730 }
731
732 if (less)
734 else
736}
737
740{
741
748}
749
752{
753
760}
761
762
763
764
765
766
767
768
769
772{
774
777 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
778 errmsg("integer out of range")));
780}
781
784{
786
788}
789
792{
796
799 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
800 errmsg("integer out of range")));
802}
803
806{
810
813 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
814 errmsg("integer out of range")));
816}
817
820{
824
827 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
828 errmsg("integer out of range")));
830}
831
834{
838
839 if (arg2 == 0)
840 {
842 (errcode(ERRCODE_DIVISION_BY_ZERO),
843 errmsg("division by zero")));
844
846 }
847
848
849
850
851
852
853
854 if (arg2 == -1)
855 {
858 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
859 errmsg("integer out of range")));
860 result = -arg1;
862 }
863
864
865
866 result = arg1 / arg2;
867
869}
870
873{
876
879 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
880 errmsg("integer out of range")));
881
883}
884
887{
889
892 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
893 errmsg("smallint out of range")));
895}
896
899{
901
903}
904
907{
911
914 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
915 errmsg("smallint out of range")));
917}
918
921{
925
928 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
929 errmsg("smallint out of range")));
931}
932
935{
939
942 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
943 errmsg("smallint out of range")));
944
946}
947
950{
954
955 if (arg2 == 0)
956 {
958 (errcode(ERRCODE_DIVISION_BY_ZERO),
959 errmsg("division by zero")));
960
962 }
963
964
965
966
967
968
969
970 if (arg2 == -1)
971 {
974 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
975 errmsg("smallint out of range")));
976 result = -arg1;
978 }
979
980
981
982 result = arg1 / arg2;
983
985}
986
989{
993
996 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
997 errmsg("integer out of range")));
999}
1000
1003{
1007
1010 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1011 errmsg("integer out of range")));
1013}
1014
1017{
1021
1024 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1025 errmsg("integer out of range")));
1027}
1028
1031{
1034
1036 {
1038 (errcode(ERRCODE_DIVISION_BY_ZERO),
1039 errmsg("division by zero")));
1040
1042 }
1043
1044
1046}
1047
1050{
1054
1057 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1058 errmsg("integer out of range")));
1060}
1061
1064{
1068
1071 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1072 errmsg("integer out of range")));
1074}
1075
1078{
1082
1085 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1086 errmsg("integer out of range")));
1088}
1089
1092{
1096
1098 {
1100 (errcode(ERRCODE_DIVISION_BY_ZERO),
1101 errmsg("division by zero")));
1102
1104 }
1105
1106
1107
1108
1109
1110
1111
1112 if (arg2 == -1)
1113 {
1116 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1117 errmsg("integer out of range")));
1118 result = -arg1;
1120 }
1121
1122
1123
1124 result = arg1 / arg2;
1125
1127}
1128
1131{
1134
1136 {
1138 (errcode(ERRCODE_DIVISION_BY_ZERO),
1139 errmsg("division by zero")));
1140
1142 }
1143
1144
1145
1146
1147
1148
1149 if (arg2 == -1)
1151
1152
1153
1155}
1156
1159{
1162
1164 {
1166 (errcode(ERRCODE_DIVISION_BY_ZERO),
1167 errmsg("division by zero")));
1168
1170 }
1171
1172
1173
1174
1175
1176
1177
1178 if (arg2 == -1)
1180
1181
1182
1184}
1185
1186
1187
1188
1189
1192{
1195
1198 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1199 errmsg("integer out of range")));
1200 result = (arg1 < 0) ? -arg1 : arg1;
1202}
1203
1206{
1209
1212 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1213 errmsg("smallint out of range")));
1214 result = (arg1 < 0) ? -arg1 : arg1;
1216}
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1234{
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248 a1 = (arg1 < 0) ? arg1 : -arg1;
1249 a2 = (arg2 < 0) ? arg2 : -arg2;
1251 {
1252 swap = arg1;
1253 arg1 = arg2;
1254 arg2 = swap;
1255 }
1256
1257
1259 {
1262 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1263 errmsg("integer out of range")));
1264
1265
1266
1267
1268
1269
1270
1271 if (arg2 == -1)
1272 return 1;
1273 }
1274
1275
1276 while (arg2 != 0)
1277 {
1278 swap = arg2;
1279 arg2 = arg1 % arg2;
1280 arg1 = swap;
1281 }
1282
1283
1284
1285
1286
1287 if (arg1 < 0)
1288 arg1 = -arg1;
1289
1290 return arg1;
1291}
1292
1295{
1299
1301
1303}
1304
1305
1306
1307
1310{
1315
1316
1317
1318
1319
1320
1321 if (arg1 == 0 || arg2 == 0)
1323
1324
1326 arg1 = arg1 / gcd;
1327
1330 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1331 errmsg("integer out of range")));
1332
1333
1336 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1337 errmsg("integer out of range")));
1338
1339 if (result < 0)
1340 result = -result;
1341
1343}
1344
1347{
1350
1352}
1353
1356{
1359
1361}
1362
1365{
1368
1370}
1371
1374{
1377
1379}
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1394{
1397
1399}
1400
1403{
1406
1408}
1409
1412{
1415
1417}
1418
1421{
1424
1426}
1427
1430{
1433
1435}
1436
1439{
1441
1443}
1444
1447{
1450
1452}
1453
1456{
1459
1461}
1462
1465{
1468
1470}
1471
1474{
1476
1478}
1479
1480
1483{
1486
1488}
1489
1492{
1495
1497}
1498
1499
1500
1501
1504{
1506}
1507
1510{
1515
1516
1518 {
1522
1523
1526 if (step == 0)
1528 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1529 errmsg("step size cannot equal zero")));
1530
1531
1533
1534
1535
1536
1538
1539
1541
1542
1543
1544
1545
1547 fctx->finish = finish;
1548 fctx->step = step;
1549
1552 }
1553
1554
1556
1557
1558
1559
1562
1565 {
1566
1567
1568
1569
1571 fctx->step = 0;
1572
1573
1575 }
1576 else
1577
1579}
1580
1581
1582
1583
1586{
1588 Node *ret = NULL;
1589
1591 {
1592
1594
1596 {
1599 *arg2,
1600 *arg3;
1601
1602
1607 else
1608 arg3 = NULL;
1609
1610
1611
1612
1613
1614
1615
1617 ((Const *) arg1)->constisnull) ||
1619 ((Const *) arg2)->constisnull) ||
1620 (arg3 != NULL && IsA(arg3, Const) &&
1621 ((Const *) arg3)->constisnull))
1622 {
1623 req->rows = 0;
1624 ret = (Node *) req;
1625 }
1626 else if (IsA(arg1, Const) &&
1628 (arg3 == NULL || IsA(arg3, Const)))
1629 {
1631 finish,
1632 step;
1633
1637
1638
1639 if (step != 0)
1640 {
1641 req->rows = floor((finish - start + step) / step);
1642 ret = (Node *) req;
1643 }
1644 }
1645 }
1646 }
1647
1649}
Datum array_recv(PG_FUNCTION_ARGS)
Datum array_send(PG_FUNCTION_ARGS)
Node * estimate_expression_value(PlannerInfo *root, Node *node)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
#define PG_RETURN_BYTEA_P(x)
#define PG_GETARG_POINTER(n)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
#define PG_RETURN_CSTRING(x)
#define PG_GETARG_DATUM(n)
#define LOCAL_FCINFO(name, nargs)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_INT64(n)
#define PG_RETURN_INT16(x)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
#define PG_RETURN_POINTER(x)
#define PG_RETURN_BOOL(x)
#define PG_GETARG_INT16(n)
#define SRF_IS_FIRSTCALL()
#define SRF_PERCALL_SETUP()
#define SRF_RETURN_NEXT(_funcctx, _result)
#define SRF_FIRSTCALL_INIT()
#define SRF_RETURN_DONE(_funcctx)
Assert(PointerIsAligned(start, uint64))
static const FormData_pg_attribute a1
static const FormData_pg_attribute a2
Datum int4div(PG_FUNCTION_ARGS)
Datum int4shl(PG_FUNCTION_ARGS)
Datum int2vectorrecv(PG_FUNCTION_ARGS)
Datum int2not(PG_FUNCTION_ARGS)
Datum int4pl(PG_FUNCTION_ARGS)
Datum int2xor(PG_FUNCTION_ARGS)
Datum generate_series_int4(PG_FUNCTION_ARGS)
Datum int4recv(PG_FUNCTION_ARGS)
Datum int4um(PG_FUNCTION_ARGS)
Datum int4gt(PG_FUNCTION_ARGS)
Datum int24eq(PG_FUNCTION_ARGS)
Datum int2lt(PG_FUNCTION_ARGS)
Datum int24mi(PG_FUNCTION_ARGS)
Datum int2out(PG_FUNCTION_ARGS)
Datum int2eq(PG_FUNCTION_ARGS)
Datum int42eq(PG_FUNCTION_ARGS)
Datum int2ge(PG_FUNCTION_ARGS)
Datum int2up(PG_FUNCTION_ARGS)
Datum int4gcd(PG_FUNCTION_ARGS)
Datum int42ge(PG_FUNCTION_ARGS)
Datum int42mul(PG_FUNCTION_ARGS)
Datum int24gt(PG_FUNCTION_ARGS)
Datum int2le(PG_FUNCTION_ARGS)
Datum int2abs(PG_FUNCTION_ARGS)
#define Int2VectorSize(n)
Datum int4lt(PG_FUNCTION_ARGS)
Datum int2um(PG_FUNCTION_ARGS)
Datum int2shl(PG_FUNCTION_ARGS)
Datum in_range_int2_int8(PG_FUNCTION_ARGS)
Datum int42div(PG_FUNCTION_ARGS)
Datum int2mi(PG_FUNCTION_ARGS)
Datum int2ne(PG_FUNCTION_ARGS)
Datum int24le(PG_FUNCTION_ARGS)
Datum int2send(PG_FUNCTION_ARGS)
Datum int2mul(PG_FUNCTION_ARGS)
Datum int2smaller(PG_FUNCTION_ARGS)
Datum int2mod(PG_FUNCTION_ARGS)
int2vector * buildint2vector(const int16 *int2s, int n)
Datum int2in(PG_FUNCTION_ARGS)
Datum int24mul(PG_FUNCTION_ARGS)
Datum int42gt(PG_FUNCTION_ARGS)
Datum int4lcm(PG_FUNCTION_ARGS)
Datum in_range_int4_int8(PG_FUNCTION_ARGS)
Datum int2div(PG_FUNCTION_ARGS)
Datum in_range_int2_int2(PG_FUNCTION_ARGS)
Datum int4in(PG_FUNCTION_ARGS)
Datum int2shr(PG_FUNCTION_ARGS)
Datum in_range_int2_int4(PG_FUNCTION_ARGS)
static int32 int4gcd_internal(int32 arg1, int32 arg2)
Datum int2vectorout(PG_FUNCTION_ARGS)
Datum i4toi2(PG_FUNCTION_ARGS)
Datum int4ge(PG_FUNCTION_ARGS)
Datum int4and(PG_FUNCTION_ARGS)
Datum int4send(PG_FUNCTION_ARGS)
Datum int24ge(PG_FUNCTION_ARGS)
Datum in_range_int4_int2(PG_FUNCTION_ARGS)
Datum int2vectorin(PG_FUNCTION_ARGS)
Datum bool_int4(PG_FUNCTION_ARGS)
Datum int4inc(PG_FUNCTION_ARGS)
Datum int4shr(PG_FUNCTION_ARGS)
Datum int4larger(PG_FUNCTION_ARGS)
Datum int24ne(PG_FUNCTION_ARGS)
Datum int2gt(PG_FUNCTION_ARGS)
Datum int24lt(PG_FUNCTION_ARGS)
Datum int42pl(PG_FUNCTION_ARGS)
Datum in_range_int4_int4(PG_FUNCTION_ARGS)
Datum int4ne(PG_FUNCTION_ARGS)
Datum int42le(PG_FUNCTION_ARGS)
Datum i2toi4(PG_FUNCTION_ARGS)
Datum int4smaller(PG_FUNCTION_ARGS)
Datum int24pl(PG_FUNCTION_ARGS)
Datum int4_bool(PG_FUNCTION_ARGS)
Datum int4mod(PG_FUNCTION_ARGS)
Datum int24div(PG_FUNCTION_ARGS)
Datum int4xor(PG_FUNCTION_ARGS)
Datum generate_series_step_int4(PG_FUNCTION_ARGS)
Datum int2larger(PG_FUNCTION_ARGS)
Datum int2and(PG_FUNCTION_ARGS)
Datum int4not(PG_FUNCTION_ARGS)
Datum int4abs(PG_FUNCTION_ARGS)
Datum int4out(PG_FUNCTION_ARGS)
Datum generate_series_int4_support(PG_FUNCTION_ARGS)
Datum int4mul(PG_FUNCTION_ARGS)
Datum int4eq(PG_FUNCTION_ARGS)
Datum int4le(PG_FUNCTION_ARGS)
Datum int42ne(PG_FUNCTION_ARGS)
Datum int4mi(PG_FUNCTION_ARGS)
Datum int42lt(PG_FUNCTION_ARGS)
Datum int2pl(PG_FUNCTION_ARGS)
Datum int4up(PG_FUNCTION_ARGS)
Datum int2vectorsend(PG_FUNCTION_ARGS)
Datum int2recv(PG_FUNCTION_ARGS)
Datum int42mi(PG_FUNCTION_ARGS)
Datum int4or(PG_FUNCTION_ARGS)
Datum int2or(PG_FUNCTION_ARGS)
static bool pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
static bool pg_mul_s16_overflow(int16 a, int16 b, int16 *result)
static bool pg_add_s16_overflow(int16 a, int16 b, int16 *result)
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
void * repalloc(void *pointer, Size size)
void * palloc0(Size size)
static bool is_funcclause(const void *clause)
#define IsA(nodeptr, _type_)
int pg_ltoa(int32 value, char *a)
int32 pg_strtoint32_safe(const char *s, Node *escontext)
int pg_itoa(int16 i, char *a)
int16 pg_strtoint16_safe(const char *s, Node *escontext)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static int list_length(const List *l)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
unsigned int pq_getmsgint(StringInfo msg, int b)
void pq_begintypsend(StringInfo buf)
bytea * pq_endtypsend(StringInfo buf)
static void pq_sendint32(StringInfo buf, uint32 i)
static void pq_sendint16(StringInfo buf, uint16 i)
StringInfoData * StringInfo
MemoryContext multi_call_memory_ctx
struct PlannerInfo * root
int16 values[FLEXIBLE_ARRAY_MEMBER]
static uint32 gcd(uint32 a, uint32 b)
#define SET_VARSIZE(PTR, len)