PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 "common/int.h"
21#include "funcapi.h"
22#include "libpq/pqformat.h"
23#include "nodes/nodeFuncs.h"
24#include "nodes/supportnodes.h"
25#include "optimizer/optimizer.h"
26#include "utils/builtins.h"
27#include "utils/fmgroids.h"
28
29typedef struct
30{
35
36
37/***********************************************************************
38 **
39 ** Routines for 64-bit integers.
40 **
41 ***********************************************************************/
42
43/*----------------------------------------------------------
44 * Formatting and conversion routines.
45 *---------------------------------------------------------*/
46
47/* int8in()
48 */
51{
52 char *num = PG_GETARG_CSTRING(0);
53
54 PG_RETURN_INT64(pg_strtoint64_safe(num, fcinfo->context));
55}
56
57
58/* int8out()
59 */
62{
64 char buf[MAXINT8LEN + 1];
65 char *result;
66 int len;
67
68 len = pg_lltoa(val, buf) + 1;
69
70 /*
71 * Since the length is already known, we do a manual palloc() and memcpy()
72 * to avoid the strlen() call that would otherwise be done in pstrdup().
73 */
74 result = palloc(len);
75 memcpy(result, buf, len);
76 PG_RETURN_CSTRING(result);
77}
78
79/*
80 * int8recv - converts external binary format to int8
81 */
89
90/*
91 * int8send - converts int8 to binary format
92 */
103
104
105/*----------------------------------------------------------
106 * Relational operators for int8s, including cross-data-type comparisons.
107 *---------------------------------------------------------*/
108
109/* int8relop()
110 * Is val1 relop val2?
111 */
112Datum
120
121Datum
129
130Datum
138
139Datum
147
148Datum
156
157Datum
165
166/* int84relop()
167 * Is 64-bit val1 relop 32-bit val2?
168 */
169Datum
177
178Datum
186
187Datum
195
196Datum
204
205Datum
213
214Datum
222
223/* int48relop()
224 * Is 32-bit val1 relop 64-bit val2?
225 */
226Datum
234
235Datum
243
244Datum
252
253Datum
261
262Datum
270
271Datum
279
280/* int82relop()
281 * Is 64-bit val1 relop 16-bit val2?
282 */
283Datum
291
292Datum
300
301Datum
309
310Datum
318
319Datum
327
328Datum
336
337/* int28relop()
338 * Is 16-bit val1 relop 64-bit val2?
339 */
340Datum
348
349Datum
357
358Datum
366
367Datum
375
376Datum
384
385Datum
393
394/*
395 * in_range support function for int8.
396 *
397 * Note: we needn't supply int8_int4 or int8_int2 variants, as implicit
398 * coercion of the offset value takes care of those scenarios just as well.
399 */
400Datum
402{
404 int64 base = PG_GETARG_INT64(1);
405 int64 offset = PG_GETARG_INT64(2);
406 bool sub = PG_GETARG_BOOL(3);
407 bool less = PG_GETARG_BOOL(4);
408 int64 sum;
409
410 if (offset < 0)
413 errmsg("invalid preceding or following size in window function")));
414
415 if (sub)
416 offset = -offset; /* cannot overflow */
417
418 if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
419 {
420 /*
421 * If sub is false, the true sum is surely more than val, so correct
422 * answer is the same as "less". If sub is true, the true sum is
423 * surely less than val, so the answer is "!less".
424 */
425 PG_RETURN_BOOL(sub ? !less : less);
426 }
427
428 if (less)
429 PG_RETURN_BOOL(val <= sum);
430 else
431 PG_RETURN_BOOL(val >= sum);
432}
433
434
435/*----------------------------------------------------------
436 * Arithmetic operators on 64-bit integers.
437 *---------------------------------------------------------*/
438
439Datum
441{
443 int64 result;
444
445 if (unlikely(arg == PG_INT64_MIN))
448 errmsg("bigint out of range")));
449 result = -arg;
450 PG_RETURN_INT64(result);
451}
452
453Datum
460
461Datum
463{
466 int64 result;
467
468 if (unlikely(pg_add_s64_overflow(arg1, arg2, &result)))
471 errmsg("bigint out of range")));
472 PG_RETURN_INT64(result);
473}
474
475Datum
477{
480 int64 result;
481
482 if (unlikely(pg_sub_s64_overflow(arg1, arg2, &result)))
485 errmsg("bigint out of range")));
486 PG_RETURN_INT64(result);
487}
488
489Datum
491{
494 int64 result;
495
496 if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
499 errmsg("bigint out of range")));
500 PG_RETURN_INT64(result);
501}
502
503Datum
505{
508 int64 result;
509
510 if (arg2 == 0)
511 {
514 errmsg("division by zero")));
515 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
517 }
518
519 /*
520 * INT64_MIN / -1 is problematic, since the result can't be represented on
521 * a two's-complement machine. Some machines produce INT64_MIN, some
522 * produce zero, some throw an exception. We can dodge the problem by
523 * recognizing that division by -1 is the same as negation.
524 */
525 if (arg2 == -1)
526 {
527 if (unlikely(arg1 == PG_INT64_MIN))
530 errmsg("bigint out of range")));
531 result = -arg1;
532 PG_RETURN_INT64(result);
533 }
534
535 /* No overflow is possible */
536
537 result = arg1 / arg2;
538
539 PG_RETURN_INT64(result);
540}
541
542/* int8abs()
543 * Absolute value
544 */
545Datum
547{
549 int64 result;
550
551 if (unlikely(arg1 == PG_INT64_MIN))
554 errmsg("bigint out of range")));
555 result = (arg1 < 0) ? -arg1 : arg1;
556 PG_RETURN_INT64(result);
557}
558
559/* int8mod()
560 * Modulo operation.
561 */
562Datum
564{
567
568 if (unlikely(arg2 == 0))
569 {
572 errmsg("division by zero")));
573 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
575 }
576
577 /*
578 * Some machines throw a floating-point exception for INT64_MIN % -1,
579 * which is a bit silly since the correct answer is perfectly
580 * well-defined, namely zero.
581 */
582 if (arg2 == -1)
584
585 /* No overflow is possible */
586
588}
589
590/*
591 * Greatest Common Divisor
592 *
593 * Returns the largest positive integer that exactly divides both inputs.
594 * Special cases:
595 * - gcd(x, 0) = gcd(0, x) = abs(x)
596 * because 0 is divisible by anything
597 * - gcd(0, 0) = 0
598 * complies with the previous definition and is a common convention
599 *
600 * Special care must be taken if either input is INT64_MIN ---
601 * gcd(0, INT64_MIN), gcd(INT64_MIN, 0) and gcd(INT64_MIN, INT64_MIN) are
602 * all equal to abs(INT64_MIN), which cannot be represented as a 64-bit signed
603 * integer.
604 */
605static int64
607{
608 int64 swap;
609 int64 a1,
610 a2;
611
612 /*
613 * Put the greater absolute value in arg1.
614 *
615 * This would happen automatically in the loop below, but avoids an
616 * expensive modulo operation, and simplifies the special-case handling
617 * for INT64_MIN below.
618 *
619 * We do this in negative space in order to handle INT64_MIN.
620 */
621 a1 = (arg1 < 0) ? arg1 : -arg1;
622 a2 = (arg2 < 0) ? arg2 : -arg2;
623 if (a1 > a2)
624 {
625 swap = arg1;
626 arg1 = arg2;
627 arg2 = swap;
628 }
629
630 /* Special care needs to be taken with INT64_MIN. See comments above. */
631 if (arg1 == PG_INT64_MIN)
632 {
633 if (arg2 == 0 || arg2 == PG_INT64_MIN)
636 errmsg("bigint out of range")));
637
638 /*
639 * Some machines throw a floating-point exception for INT64_MIN % -1,
640 * which is a bit silly since the correct answer is perfectly
641 * well-defined, namely zero. Guard against this and just return the
642 * result, gcd(INT64_MIN, -1) = 1.
643 */
644 if (arg2 == -1)
645 return 1;
646 }
647
648 /* Use the Euclidean algorithm to find the GCD */
649 while (arg2 != 0)
650 {
651 swap = arg2;
652 arg2 = arg1 % arg2;
653 arg1 = swap;
654 }
655
656 /*
657 * Make sure the result is positive. (We know we don't have INT64_MIN
658 * anymore).
659 */
660 if (arg1 < 0)
661 arg1 = -arg1;
662
663 return arg1;
664}
665
666Datum
668{
671 int64 result;
672
673 result = int8gcd_internal(arg1, arg2);
674
675 PG_RETURN_INT64(result);
676}
677
678/*
679 * Least Common Multiple
680 */
681Datum
683{
686 int64 gcd;
687 int64 result;
688
689 /*
690 * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a
691 * division-by-zero error below when x is zero, and an overflow error from
692 * the GCD computation when x = INT64_MIN.
693 */
694 if (arg1 == 0 || arg2 == 0)
696
697 /* lcm(x, y) = abs(x / gcd(x, y) * y) */
699 arg1 = arg1 / gcd;
700
701 if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
704 errmsg("bigint out of range")));
705
706 /* If the result is INT64_MIN, it cannot be represented. */
707 if (unlikely(result == PG_INT64_MIN))
710 errmsg("bigint out of range")));
711
712 if (result < 0)
713 result = -result;
714
715 PG_RETURN_INT64(result);
716}
717
718Datum
720{
722 int64 result;
723
724 if (unlikely(pg_add_s64_overflow(arg, 1, &result)))
727 errmsg("bigint out of range")));
728
729 PG_RETURN_INT64(result);
730}
731
732Datum
734{
736 int64 result;
737
738 if (unlikely(pg_sub_s64_overflow(arg, 1, &result)))
741 errmsg("bigint out of range")));
742
743 PG_RETURN_INT64(result);
744}
745
746
747/*
748 * These functions are exactly like int8inc/int8dec but are used for
749 * aggregates that count only non-null values. Since the functions are
750 * declared strict, the null checks happen before we ever get here, and all we
751 * need do is increment the state value. We could actually make these pg_proc
752 * entries point right at int8inc/int8dec, but then the opr_sanity regression
753 * test would complain about mismatched entries for a built-in function.
754 */
755
756Datum
758{
759 return int8inc(fcinfo);
760}
761
762Datum
764{
765 return int8inc(fcinfo);
766}
767
768Datum
770{
771 return int8dec(fcinfo);
772}
773
774/*
775 * int8inc_support
776 * prosupport function for int8inc() and int8inc_any()
777 */
778Datum
780{
782
784 {
787 int frameOptions = req->window_clause->frameOptions;
788
789 /* No ORDER BY clause then all rows are peers */
790 if (req->window_clause->orderClause == NIL)
791 monotonic = MONOTONICFUNC_BOTH;
792 else
793 {
794 /*
795 * Otherwise take into account the frame options. When the frame
796 * bound is the start of the window then the resulting value can
797 * never decrease, therefore is monotonically increasing
798 */
799 if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
800 monotonic |= MONOTONICFUNC_INCREASING;
801
802 /*
803 * Likewise, if the frame bound is the end of the window then the
804 * resulting value can never decrease.
805 */
806 if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
807 monotonic |= MONOTONICFUNC_DECREASING;
808 }
809
810 req->monotonic = monotonic;
812 }
813
815 {
817 Aggref *agg = req->aggref;
818
819 /*
820 * Check for COUNT(ANY) and try to convert to COUNT(*). The input
821 * argument cannot be NULL, we can't have an ORDER BY / DISTINCT in
822 * the aggregate, and agglevelsup must be 0.
823 *
824 * Technically COUNT(ANY) must have 1 arg, but be paranoid and check.
825 */
826 if (agg->aggfnoid == F_COUNT_ANY && list_length(agg->args) == 1)
827 {
828 TargetEntry *tle = (TargetEntry *) linitial(agg->args);
829 Expr *arg = tle->expr;
830
831 /* Check for unsupported cases */
832 if (agg->aggdistinct != NIL || agg->aggorder != NIL ||
833 agg->agglevelsup != 0)
835
836 /* If the arg isn't NULLable, do the conversion */
837 if (expr_is_nonnullable(req->root, arg, false))
838 {
839 Aggref *newagg;
840
841 /* We don't expect these to have been set yet */
842 Assert(agg->aggtransno == -1);
843 Assert(agg->aggtranstype == InvalidOid);
844
845 /* Convert COUNT(ANY) to COUNT(*) by making a new Aggref */
847 memcpy(newagg, agg, sizeof(Aggref));
848 newagg->aggfnoid = F_COUNT_;
849
850 /* count(*) has no args */
851 newagg->aggargtypes = NULL;
852 newagg->args = NULL;
853 newagg->aggstar = true;
854 newagg->location = -1;
855
857 }
858 }
859 }
860
862}
863
864
865Datum
867{
870 int64 result;
871
872 result = ((arg1 > arg2) ? arg1 : arg2);
873
874 PG_RETURN_INT64(result);
875}
876
877Datum
879{
882 int64 result;
883
884 result = ((arg1 < arg2) ? arg1 : arg2);
885
886 PG_RETURN_INT64(result);
887}
888
889Datum
891{
894 int64 result;
895
896 if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
899 errmsg("bigint out of range")));
900 PG_RETURN_INT64(result);
901}
902
903Datum
905{
908 int64 result;
909
910 if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
913 errmsg("bigint out of range")));
914 PG_RETURN_INT64(result);
915}
916
917Datum
919{
922 int64 result;
923
924 if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
927 errmsg("bigint out of range")));
928 PG_RETURN_INT64(result);
929}
930
931Datum
933{
936 int64 result;
937
938 if (arg2 == 0)
939 {
942 errmsg("division by zero")));
943 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
945 }
946
947 /*
948 * INT64_MIN / -1 is problematic, since the result can't be represented on
949 * a two's-complement machine. Some machines produce INT64_MIN, some
950 * produce zero, some throw an exception. We can dodge the problem by
951 * recognizing that division by -1 is the same as negation.
952 */
953 if (arg2 == -1)
954 {
955 if (unlikely(arg1 == PG_INT64_MIN))
958 errmsg("bigint out of range")));
959 result = -arg1;
960 PG_RETURN_INT64(result);
961 }
962
963 /* No overflow is possible */
964
965 result = arg1 / arg2;
966
967 PG_RETURN_INT64(result);
968}
969
970Datum
972{
975 int64 result;
976
977 if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
980 errmsg("bigint out of range")));
981 PG_RETURN_INT64(result);
982}
983
984Datum
986{
989 int64 result;
990
991 if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
994 errmsg("bigint out of range")));
995 PG_RETURN_INT64(result);
996}
997
998Datum
1000{
1003 int64 result;
1004
1005 if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
1006 ereport(ERROR,
1008 errmsg("bigint out of range")));
1009 PG_RETURN_INT64(result);
1010}
1011
1012Datum
1014{
1017
1018 if (unlikely(arg2 == 0))
1019 {
1020 ereport(ERROR,
1022 errmsg("division by zero")));
1023 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1025 }
1026
1027 /* No overflow is possible */
1029}
1030
1031Datum
1033{
1036 int64 result;
1037
1038 if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
1039 ereport(ERROR,
1041 errmsg("bigint out of range")));
1042 PG_RETURN_INT64(result);
1043}
1044
1045Datum
1047{
1050 int64 result;
1051
1052 if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
1053 ereport(ERROR,
1055 errmsg("bigint out of range")));
1056 PG_RETURN_INT64(result);
1057}
1058
1059Datum
1061{
1064 int64 result;
1065
1066 if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
1067 ereport(ERROR,
1069 errmsg("bigint out of range")));
1070 PG_RETURN_INT64(result);
1071}
1072
1073Datum
1075{
1078 int64 result;
1079
1080 if (unlikely(arg2 == 0))
1081 {
1082 ereport(ERROR,
1084 errmsg("division by zero")));
1085 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1087 }
1088
1089 /*
1090 * INT64_MIN / -1 is problematic, since the result can't be represented on
1091 * a two's-complement machine. Some machines produce INT64_MIN, some
1092 * produce zero, some throw an exception. We can dodge the problem by
1093 * recognizing that division by -1 is the same as negation.
1094 */
1095 if (arg2 == -1)
1096 {
1097 if (unlikely(arg1 == PG_INT64_MIN))
1098 ereport(ERROR,
1100 errmsg("bigint out of range")));
1101 result = -arg1;
1102 PG_RETURN_INT64(result);
1103 }
1104
1105 /* No overflow is possible */
1106
1107 result = arg1 / arg2;
1108
1109 PG_RETURN_INT64(result);
1110}
1111
1112Datum
1114{
1117 int64 result;
1118
1119 if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
1120 ereport(ERROR,
1122 errmsg("bigint out of range")));
1123 PG_RETURN_INT64(result);
1124}
1125
1126Datum
1128{
1131 int64 result;
1132
1133 if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
1134 ereport(ERROR,
1136 errmsg("bigint out of range")));
1137 PG_RETURN_INT64(result);
1138}
1139
1140Datum
1142{
1145 int64 result;
1146
1147 if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
1148 ereport(ERROR,
1150 errmsg("bigint out of range")));
1151 PG_RETURN_INT64(result);
1152}
1153
1154Datum
1156{
1159
1160 if (unlikely(arg2 == 0))
1161 {
1162 ereport(ERROR,
1164 errmsg("division by zero")));
1165 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1167 }
1168
1169 /* No overflow is possible */
1171}
1172
1173/* Binary arithmetics
1174 *
1175 * int8and - returns arg1 & arg2
1176 * int8or - returns arg1 | arg2
1177 * int8xor - returns arg1 # arg2
1178 * int8not - returns ~arg1
1179 * int8shl - returns arg1 << arg2
1180 * int8shr - returns arg1 >> arg2
1181 */
1182
1183Datum
1191
1192Datum
1200
1201Datum
1209
1210Datum
1217
1218Datum
1226
1227Datum
1235
1236/*----------------------------------------------------------
1237 * Conversion operators.
1238 *---------------------------------------------------------*/
1239
1240Datum
1247
1248Datum
1250{
1252
1254 ereport(ERROR,
1256 errmsg("integer out of range")));
1257
1259}
1260
1261Datum
1268
1269Datum
1271{
1273
1275 ereport(ERROR,
1277 errmsg("smallint out of range")));
1278
1280}
1281
1282Datum
1284{
1286 float8 result;
1287
1288 result = arg;
1289
1290 PG_RETURN_FLOAT8(result);
1291}
1292
1293/* dtoi8()
1294 * Convert float8 to 8-byte integer.
1295 */
1296Datum
1298{
1299 float8 num = PG_GETARG_FLOAT8(0);
1300
1301 /*
1302 * Get rid of any fractional part in the input. This is so we don't fail
1303 * on just-out-of-range values that would round into range. Note
1304 * assumption that rint() will pass through a NaN or Inf unchanged.
1305 */
1306 num = rint(num);
1307
1308 /* Range check */
1309 if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT64(num)))
1310 ereport(ERROR,
1312 errmsg("bigint out of range")));
1313
1314 PG_RETURN_INT64((int64) num);
1315}
1316
1317Datum
1319{
1321 float4 result;
1322
1323 result = arg;
1324
1325 PG_RETURN_FLOAT4(result);
1326}
1327
1328/* ftoi8()
1329 * Convert float4 to 8-byte integer.
1330 */
1331Datum
1333{
1334 float4 num = PG_GETARG_FLOAT4(0);
1335
1336 /*
1337 * Get rid of any fractional part in the input. This is so we don't fail
1338 * on just-out-of-range values that would round into range. Note
1339 * assumption that rint() will pass through a NaN or Inf unchanged.
1340 */
1341 num = rint(num);
1342
1343 /* Range check */
1344 if (unlikely(isnan(num) || !FLOAT4_FITS_IN_INT64(num)))
1345 ereport(ERROR,
1347 errmsg("bigint out of range")));
1348
1349 PG_RETURN_INT64((int64) num);
1350}
1351
1352Datum
1354{
1356
1357 if (unlikely(arg < 0) || unlikely(arg > PG_UINT32_MAX))
1358 ereport(ERROR,
1360 errmsg("OID out of range")));
1361
1363}
1364
1365Datum
1372
1373Datum
1380
1381/*
1382 * non-persistent numeric series generator
1383 */
1384Datum
1389
1390Datum
1392{
1395 int64 result;
1396 MemoryContext oldcontext;
1397
1398 /* stuff done only on the first call of the function */
1399 if (SRF_IS_FIRSTCALL())
1400 {
1402 int64 finish = PG_GETARG_INT64(1);
1403 int64 step = 1;
1404
1405 /* see if we were given an explicit step size */
1406 if (PG_NARGS() == 3)
1407 step = PG_GETARG_INT64(2);
1408 if (step == 0)
1409 ereport(ERROR,
1411 errmsg("step size cannot equal zero")));
1412
1413 /* create a function context for cross-call persistence */
1415
1416 /*
1417 * switch to memory context appropriate for multiple function calls
1418 */
1419 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1420
1421 /* allocate memory for user context */
1423
1424 /*
1425 * Use fctx to keep state from call to call. Seed current with the
1426 * original start value
1427 */
1428 fctx->current = start;
1429 fctx->finish = finish;
1430 fctx->step = step;
1431
1432 funcctx->user_fctx = fctx;
1433 MemoryContextSwitchTo(oldcontext);
1434 }
1435
1436 /* stuff done on every call of the function */
1438
1439 /*
1440 * get the saved state and use current as the result for this iteration
1441 */
1442 fctx = funcctx->user_fctx;
1443 result = fctx->current;
1444
1445 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1446 (fctx->step < 0 && fctx->current >= fctx->finish))
1447 {
1448 /*
1449 * Increment current in preparation for next iteration. If next-value
1450 * computation overflows, this is the final result.
1451 */
1452 if (pg_add_s64_overflow(fctx->current, fctx->step, &fctx->current))
1453 fctx->step = 0;
1454
1455 /* do when there is more left to send */
1457 }
1458 else
1459 /* do when there is no more left */
1461}
1462
1463/*
1464 * Planner support function for generate_series(int8, int8 [, int8])
1465 */
1466Datum
1468{
1470 Node *ret = NULL;
1471
1473 {
1474 /* Try to estimate the number of rows returned */
1476
1477 if (is_funcclause(req->node)) /* be paranoid */
1478 {
1479 List *args = ((FuncExpr *) req->node)->args;
1480 Node *arg1,
1481 *arg2,
1482 *arg3;
1483
1484 /* We can use estimated argument values here */
1486 arg2 = estimate_expression_value(req->root, lsecond(args));
1487 if (list_length(args) >= 3)
1488 arg3 = estimate_expression_value(req->root, lthird(args));
1489 else
1490 arg3 = NULL;
1491
1492 /*
1493 * If any argument is constant NULL, we can safely assume that
1494 * zero rows are returned. Otherwise, if they're all non-NULL
1495 * constants, we can calculate the number of rows that will be
1496 * returned. Use double arithmetic to avoid overflow hazards.
1497 */
1498 if ((IsA(arg1, Const) &&
1499 ((Const *) arg1)->constisnull) ||
1500 (IsA(arg2, Const) &&
1501 ((Const *) arg2)->constisnull) ||
1502 (arg3 != NULL && IsA(arg3, Const) &&
1503 ((Const *) arg3)->constisnull))
1504 {
1505 req->rows = 0;
1506 ret = (Node *) req;
1507 }
1508 else if (IsA(arg1, Const) &&
1509 IsA(arg2, Const) &&
1510 (arg3 == NULL || IsA(arg3, Const)))
1511 {
1512 double start,
1513 finish,
1514 step;
1515
1517 finish = DatumGetInt64(((Const *) arg2)->constvalue);
1518 step = arg3 ? DatumGetInt64(((Const *) arg3)->constvalue) : 1;
1519
1520 /* This equation works for either sign of step */
1521 if (step != 0)
1522 {
1523 req->rows = floor((finish - start + step) / step);
1524 ret = (Node *) req;
1525 }
1526 }
1527 }
1528 }
1529
1530 PG_RETURN_POINTER(ret);
1531}
#define MAXINT8LEN
Definition builtins.h:22
uint64 Oid8
Definition c.h:686
#define PG_INT32_MAX
Definition c.h:603
#define PG_UINT32_MAX
Definition c.h:604
#define FLOAT4_FITS_IN_INT64(num)
Definition c.h:1079
#define Assert(condition)
Definition c.h:873
int64_t int64
Definition c.h:543
double float8
Definition c.h:644
int16_t int16
Definition c.h:541
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1085
#define PG_INT16_MIN
Definition c.h:599
int32_t int32
Definition c.h:542
#define PG_INT64_MIN
Definition c.h:605
#define unlikely(x)
Definition c.h:412
float float4
Definition c.h:643
#define PG_INT32_MIN
Definition c.h:602
#define PG_INT16_MAX
Definition c.h:600
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition clauses.c:2408
bool expr_is_nonnullable(PlannerInfo *root, Expr *expr, bool use_rel_info)
Definition clauses.c:4345
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define PG_RETURN_INT64(x)
Definition fmgr.h:370
#define PG_NARGS()
Definition fmgr.h:203
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_RETURN_INT16(x)
Definition fmgr.h:357
#define PG_RETURN_OID8(x)
Definition fmgr.h:362
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_GETARG_FLOAT4(n)
Definition fmgr.h:282
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_RETURN_FLOAT4(x)
Definition fmgr.h:368
#define PG_RETURN_OID(x)
Definition fmgr.h:361
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define PG_GETARG_INT16(n)
Definition fmgr.h:271
#define SRF_IS_FIRSTCALL()
Definition funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition funcapi.h:328
return str start
static const FormData_pg_attribute a1
Definition heap.c:144
static const FormData_pg_attribute a2
Definition heap.c:157
long val
Definition informix.c:689
Datum int8lcm(PG_FUNCTION_ARGS)
Definition int8.c:682
Datum int84div(PG_FUNCTION_ARGS)
Definition int8.c:932
Datum int8ge(PG_FUNCTION_ARGS)
Definition int8.c:158
Datum i8tooid(PG_FUNCTION_ARGS)
Definition int8.c:1353
Datum int8dec_any(PG_FUNCTION_ARGS)
Definition int8.c:769
Datum in_range_int8_int8(PG_FUNCTION_ARGS)
Definition int8.c:401
Datum int8abs(PG_FUNCTION_ARGS)
Definition int8.c:546
Datum int48ge(PG_FUNCTION_ARGS)
Definition int8.c:272
Datum int82le(PG_FUNCTION_ARGS)
Definition int8.c:320
Datum int28lt(PG_FUNCTION_ARGS)
Definition int8.c:359
Datum int84pl(PG_FUNCTION_ARGS)
Definition int8.c:890
Datum int48pl(PG_FUNCTION_ARGS)
Definition int8.c:971
Datum int8out(PG_FUNCTION_ARGS)
Definition int8.c:61
Datum int82mi(PG_FUNCTION_ARGS)
Definition int8.c:1046
Datum int84gt(PG_FUNCTION_ARGS)
Definition int8.c:197
Datum int28ne(PG_FUNCTION_ARGS)
Definition int8.c:350
Datum int8send(PG_FUNCTION_ARGS)
Definition int8.c:94
Datum int8ne(PG_FUNCTION_ARGS)
Definition int8.c:122
Datum int84(PG_FUNCTION_ARGS)
Definition int8.c:1249
Datum int8mul(PG_FUNCTION_ARGS)
Definition int8.c:490
Datum int84ge(PG_FUNCTION_ARGS)
Definition int8.c:215
Datum int48lt(PG_FUNCTION_ARGS)
Definition int8.c:245
Datum int8gcd(PG_FUNCTION_ARGS)
Definition int8.c:667
Datum int8not(PG_FUNCTION_ARGS)
Definition int8.c:1211
Datum int8le(PG_FUNCTION_ARGS)
Definition int8.c:149
Datum int8lt(PG_FUNCTION_ARGS)
Definition int8.c:131
Datum int48eq(PG_FUNCTION_ARGS)
Definition int8.c:227
Datum generate_series_step_int8(PG_FUNCTION_ARGS)
Definition int8.c:1391
Datum generate_series_int8(PG_FUNCTION_ARGS)
Definition int8.c:1385
Datum int8eq(PG_FUNCTION_ARGS)
Definition int8.c:113
Datum generate_series_int8_support(PG_FUNCTION_ARGS)
Definition int8.c:1467
Datum int82div(PG_FUNCTION_ARGS)
Definition int8.c:1074
Datum int48le(PG_FUNCTION_ARGS)
Definition int8.c:263
Datum int28gt(PG_FUNCTION_ARGS)
Definition int8.c:368
Datum i8tod(PG_FUNCTION_ARGS)
Definition int8.c:1283
Datum int8inc_support(PG_FUNCTION_ARGS)
Definition int8.c:779
Datum int8in(PG_FUNCTION_ARGS)
Definition int8.c:50
Datum int8mod(PG_FUNCTION_ARGS)
Definition int8.c:563
Datum oidtoi8(PG_FUNCTION_ARGS)
Definition int8.c:1366
Datum int28pl(PG_FUNCTION_ARGS)
Definition int8.c:1113
Datum int84eq(PG_FUNCTION_ARGS)
Definition int8.c:170
Datum int28div(PG_FUNCTION_ARGS)
Definition int8.c:1155
Datum int8pl(PG_FUNCTION_ARGS)
Definition int8.c:462
Datum int8larger(PG_FUNCTION_ARGS)
Definition int8.c:866
Datum int8xor(PG_FUNCTION_ARGS)
Definition int8.c:1202
Datum int28(PG_FUNCTION_ARGS)
Definition int8.c:1262
Datum int8gt(PG_FUNCTION_ARGS)
Definition int8.c:140
Datum int8div(PG_FUNCTION_ARGS)
Definition int8.c:504
Datum int48ne(PG_FUNCTION_ARGS)
Definition int8.c:236
Datum int82pl(PG_FUNCTION_ARGS)
Definition int8.c:1032
Datum int48mul(PG_FUNCTION_ARGS)
Definition int8.c:999
Datum int48mi(PG_FUNCTION_ARGS)
Definition int8.c:985
Datum oidtooid8(PG_FUNCTION_ARGS)
Definition int8.c:1374
Datum int8up(PG_FUNCTION_ARGS)
Definition int8.c:454
Datum int8smaller(PG_FUNCTION_ARGS)
Definition int8.c:878
Datum int28le(PG_FUNCTION_ARGS)
Definition int8.c:377
static int64 int8gcd_internal(int64 arg1, int64 arg2)
Definition int8.c:606
Datum int82lt(PG_FUNCTION_ARGS)
Definition int8.c:302
Datum int82gt(PG_FUNCTION_ARGS)
Definition int8.c:311
Datum int82mul(PG_FUNCTION_ARGS)
Definition int8.c:1060
Datum dtoi8(PG_FUNCTION_ARGS)
Definition int8.c:1297
Datum int82ge(PG_FUNCTION_ARGS)
Definition int8.c:329
Datum int48div(PG_FUNCTION_ARGS)
Definition int8.c:1013
Datum int8inc_any(PG_FUNCTION_ARGS)
Definition int8.c:757
Datum int48gt(PG_FUNCTION_ARGS)
Definition int8.c:254
Datum int8or(PG_FUNCTION_ARGS)
Definition int8.c:1193
Datum int48(PG_FUNCTION_ARGS)
Definition int8.c:1241
Datum int8shr(PG_FUNCTION_ARGS)
Definition int8.c:1228
Datum int8recv(PG_FUNCTION_ARGS)
Definition int8.c:83
Datum int84le(PG_FUNCTION_ARGS)
Definition int8.c:206
Datum int8dec(PG_FUNCTION_ARGS)
Definition int8.c:733
Datum int84mi(PG_FUNCTION_ARGS)
Definition int8.c:904
Datum int82ne(PG_FUNCTION_ARGS)
Definition int8.c:293
Datum int82(PG_FUNCTION_ARGS)
Definition int8.c:1270
Datum int8inc_float8_float8(PG_FUNCTION_ARGS)
Definition int8.c:763
Datum int28ge(PG_FUNCTION_ARGS)
Definition int8.c:386
Datum int8shl(PG_FUNCTION_ARGS)
Definition int8.c:1219
Datum i8tof(PG_FUNCTION_ARGS)
Definition int8.c:1318
Datum int82eq(PG_FUNCTION_ARGS)
Definition int8.c:284
Datum int84ne(PG_FUNCTION_ARGS)
Definition int8.c:179
Datum int8mi(PG_FUNCTION_ARGS)
Definition int8.c:476
Datum ftoi8(PG_FUNCTION_ARGS)
Definition int8.c:1332
Datum int8and(PG_FUNCTION_ARGS)
Definition int8.c:1184
Datum int84mul(PG_FUNCTION_ARGS)
Definition int8.c:918
Datum int8um(PG_FUNCTION_ARGS)
Definition int8.c:440
Datum int28mi(PG_FUNCTION_ARGS)
Definition int8.c:1127
Datum int84lt(PG_FUNCTION_ARGS)
Definition int8.c:188
Datum int28eq(PG_FUNCTION_ARGS)
Definition int8.c:341
Datum int28mul(PG_FUNCTION_ARGS)
Definition int8.c:1141
Datum int8inc(PG_FUNCTION_ARGS)
Definition int8.c:719
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:293
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:262
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235
void * palloc(Size size)
Definition mcxt.c:1387
static bool is_funcclause(const void *clause)
Definition nodeFuncs.h:69
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define makeNode(_type_)
Definition nodes.h:161
int64 pg_strtoint64_safe(const char *s, Node *escontext)
Definition numutils.c:649
int pg_lltoa(int64 value, char *a)
Definition numutils.c:1226
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition parsenodes.h:614
#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING
Definition parsenodes.h:617
void * arg
const void size_t len
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define lthird(l)
Definition pg_list.h:188
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
static char buf[DEFAULT_XLOG_SEG_SIZE]
MonotonicFunction
Definition plannodes.h:1817
@ MONOTONICFUNC_NONE
Definition plannodes.h:1818
@ MONOTONICFUNC_DECREASING
Definition plannodes.h:1820
@ MONOTONICFUNC_INCREASING
Definition plannodes.h:1819
@ MONOTONICFUNC_BOTH
Definition plannodes.h:1821
static Datum Int64GetDatum(int64 X)
Definition postgres.h:423
static int64 DatumGetInt64(Datum X)
Definition postgres.h:413
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint64(StringInfo buf, uint64 i)
Definition pqformat.h:152
static int fb(int x)
struct StringInfoData * StringInfo
Definition string.h:15
Definition pg_list.h:54
Definition nodes.h:135
static uint32 gcd(uint32 a, uint32 b)