Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * int8.c
4 : : * Internal 64-bit integer operations
5 : : *
6 : : * Portions Copyright (c) 1996-2025, 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 : :
28 : :
29 : : typedef struct
30 : : {
31 : : int64 current;
32 : : int64 finish;
33 : : int64 step;
34 : : } generate_series_fctx;
35 : :
36 : :
37 : : /***********************************************************************
38 : : **
39 : : ** Routines for 64-bit integers.
40 : : **
41 : : ***********************************************************************/
42 : :
43 : : /*----------------------------------------------------------
44 : : * Formatting and conversion routines.
45 : : *---------------------------------------------------------*/
46 : :
47 : : /* int8in()
48 : : */
49 : : Datum
8389 tgl@sss.pgh.pa.us 50 :CBC 66587 : int8in(PG_FUNCTION_ARGS)
51 : : {
1300 peter@eisentraut.org 52 : 66587 : char *num = PG_GETARG_CSTRING(0);
53 : :
1002 tgl@sss.pgh.pa.us 54 : 66587 : PG_RETURN_INT64(pg_strtoint64_safe(num, fcinfo->context));
55 : : }
56 : :
57 : :
58 : : /* int8out()
59 : : */
60 : : Datum
9216 61 : 159037 : int8out(PG_FUNCTION_ARGS)
62 : : {
63 : 159037 : int64 val = PG_GETARG_INT64(0);
64 : : char buf[MAXINT8LEN + 1];
65 : : char *result;
66 : : int len;
67 : :
1911 drowley@postgresql.o 68 : 159037 : 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 : 159037 : result = palloc(len);
75 : 159037 : memcpy(result, buf, len);
9216 tgl@sss.pgh.pa.us 76 : 159037 : PG_RETURN_CSTRING(result);
77 : : }
78 : :
79 : : /*
80 : : * int8recv - converts external binary format to int8
81 : : */
82 : : Datum
8156 83 : 12 : int8recv(PG_FUNCTION_ARGS)
84 : : {
85 : 12 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
86 : :
87 : 12 : PG_RETURN_INT64(pq_getmsgint64(buf));
88 : : }
89 : :
90 : : /*
91 : : * int8send - converts int8 to binary format
92 : : */
93 : : Datum
94 : 2481 : int8send(PG_FUNCTION_ARGS)
95 : : {
96 : 2481 : int64 arg1 = PG_GETARG_INT64(0);
97 : : StringInfoData buf;
98 : :
99 : 2481 : pq_begintypsend(&buf);
100 : 2481 : pq_sendint64(&buf, arg1);
101 : 2481 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
102 : : }
103 : :
104 : :
105 : : /*----------------------------------------------------------
106 : : * Relational operators for int8s, including cross-data-type comparisons.
107 : : *---------------------------------------------------------*/
108 : :
109 : : /* int8relop()
110 : : * Is val1 relop val2?
111 : : */
112 : : Datum
9216 113 : 377315 : int8eq(PG_FUNCTION_ARGS)
114 : : {
115 : 377315 : int64 val1 = PG_GETARG_INT64(0);
116 : 377315 : int64 val2 = PG_GETARG_INT64(1);
117 : :
118 : 377315 : PG_RETURN_BOOL(val1 == val2);
119 : : }
120 : :
121 : : Datum
122 : 30025 : int8ne(PG_FUNCTION_ARGS)
123 : : {
124 : 30025 : int64 val1 = PG_GETARG_INT64(0);
125 : 30025 : int64 val2 = PG_GETARG_INT64(1);
126 : :
127 : 30025 : PG_RETURN_BOOL(val1 != val2);
128 : : }
129 : :
130 : : Datum
131 : 3037921 : int8lt(PG_FUNCTION_ARGS)
132 : : {
133 : 3037921 : int64 val1 = PG_GETARG_INT64(0);
134 : 3037921 : int64 val2 = PG_GETARG_INT64(1);
135 : :
136 : 3037921 : PG_RETURN_BOOL(val1 < val2);
137 : : }
138 : :
139 : : Datum
140 : 123807 : int8gt(PG_FUNCTION_ARGS)
141 : : {
142 : 123807 : int64 val1 = PG_GETARG_INT64(0);
143 : 123807 : int64 val2 = PG_GETARG_INT64(1);
144 : :
145 : 123807 : PG_RETURN_BOOL(val1 > val2);
146 : : }
147 : :
148 : : Datum
149 : 2828 : int8le(PG_FUNCTION_ARGS)
150 : : {
151 : 2828 : int64 val1 = PG_GETARG_INT64(0);
152 : 2828 : int64 val2 = PG_GETARG_INT64(1);
153 : :
154 : 2828 : PG_RETURN_BOOL(val1 <= val2);
155 : : }
156 : :
157 : : Datum
158 : 3004 : int8ge(PG_FUNCTION_ARGS)
159 : : {
160 : 3004 : int64 val1 = PG_GETARG_INT64(0);
161 : 3004 : int64 val2 = PG_GETARG_INT64(1);
162 : :
163 : 3004 : PG_RETURN_BOOL(val1 >= val2);
164 : : }
165 : :
166 : : /* int84relop()
167 : : * Is 64-bit val1 relop 32-bit val2?
168 : : */
169 : : Datum
170 : 98159 : int84eq(PG_FUNCTION_ARGS)
171 : : {
172 : 98159 : int64 val1 = PG_GETARG_INT64(0);
173 : 98159 : int32 val2 = PG_GETARG_INT32(1);
174 : :
175 : 98159 : PG_RETURN_BOOL(val1 == val2);
176 : : }
177 : :
178 : : Datum
179 : 47 : int84ne(PG_FUNCTION_ARGS)
180 : : {
181 : 47 : int64 val1 = PG_GETARG_INT64(0);
182 : 47 : int32 val2 = PG_GETARG_INT32(1);
183 : :
184 : 47 : PG_RETURN_BOOL(val1 != val2);
185 : : }
186 : :
187 : : Datum
188 : 347288 : int84lt(PG_FUNCTION_ARGS)
189 : : {
190 : 347288 : int64 val1 = PG_GETARG_INT64(0);
191 : 347288 : int32 val2 = PG_GETARG_INT32(1);
192 : :
193 : 347288 : PG_RETURN_BOOL(val1 < val2);
194 : : }
195 : :
196 : : Datum
197 : 70138 : int84gt(PG_FUNCTION_ARGS)
198 : : {
199 : 70138 : int64 val1 = PG_GETARG_INT64(0);
200 : 70138 : int32 val2 = PG_GETARG_INT32(1);
201 : :
202 : 70138 : PG_RETURN_BOOL(val1 > val2);
203 : : }
204 : :
205 : : Datum
206 : 11089 : int84le(PG_FUNCTION_ARGS)
207 : : {
208 : 11089 : int64 val1 = PG_GETARG_INT64(0);
209 : 11089 : int32 val2 = PG_GETARG_INT32(1);
210 : :
211 : 11089 : PG_RETURN_BOOL(val1 <= val2);
212 : : }
213 : :
214 : : Datum
215 : 5022 : int84ge(PG_FUNCTION_ARGS)
216 : : {
217 : 5022 : int64 val1 = PG_GETARG_INT64(0);
218 : 5022 : int32 val2 = PG_GETARG_INT32(1);
219 : :
220 : 5022 : PG_RETURN_BOOL(val1 >= val2);
221 : : }
222 : :
223 : : /* int48relop()
224 : : * Is 32-bit val1 relop 64-bit val2?
225 : : */
226 : : Datum
227 : 46001 : int48eq(PG_FUNCTION_ARGS)
228 : : {
229 : 46001 : int32 val1 = PG_GETARG_INT32(0);
230 : 46001 : int64 val2 = PG_GETARG_INT64(1);
231 : :
232 : 46001 : PG_RETURN_BOOL(val1 == val2);
233 : : }
234 : :
235 : : Datum
236 : 18 : int48ne(PG_FUNCTION_ARGS)
237 : : {
238 : 18 : int32 val1 = PG_GETARG_INT32(0);
239 : 18 : int64 val2 = PG_GETARG_INT64(1);
240 : :
241 : 18 : PG_RETURN_BOOL(val1 != val2);
242 : : }
243 : :
244 : : Datum
245 : 3309 : int48lt(PG_FUNCTION_ARGS)
246 : : {
247 : 3309 : int32 val1 = PG_GETARG_INT32(0);
248 : 3309 : int64 val2 = PG_GETARG_INT64(1);
249 : :
250 : 3309 : PG_RETURN_BOOL(val1 < val2);
251 : : }
252 : :
253 : : Datum
254 : 1635 : int48gt(PG_FUNCTION_ARGS)
255 : : {
256 : 1635 : int32 val1 = PG_GETARG_INT32(0);
257 : 1635 : int64 val2 = PG_GETARG_INT64(1);
258 : :
259 : 1635 : PG_RETURN_BOOL(val1 > val2);
260 : : }
261 : :
262 : : Datum
263 : 1914 : int48le(PG_FUNCTION_ARGS)
264 : : {
265 : 1914 : int32 val1 = PG_GETARG_INT32(0);
266 : 1914 : int64 val2 = PG_GETARG_INT64(1);
267 : :
268 : 1914 : PG_RETURN_BOOL(val1 <= val2);
269 : : }
270 : :
271 : : Datum
272 : 1737 : int48ge(PG_FUNCTION_ARGS)
273 : : {
274 : 1737 : int32 val1 = PG_GETARG_INT32(0);
275 : 1737 : int64 val2 = PG_GETARG_INT64(1);
276 : :
9171 277 : 1737 : PG_RETURN_BOOL(val1 >= val2);
278 : : }
279 : :
280 : : /* int82relop()
281 : : * Is 64-bit val1 relop 16-bit val2?
282 : : */
283 : : Datum
284 : 15 : int82eq(PG_FUNCTION_ARGS)
285 : : {
286 : 15 : int64 val1 = PG_GETARG_INT64(0);
287 : 15 : int16 val2 = PG_GETARG_INT16(1);
288 : :
289 : 15 : PG_RETURN_BOOL(val1 == val2);
290 : : }
291 : :
292 : : Datum
293 : 15 : int82ne(PG_FUNCTION_ARGS)
294 : : {
295 : 15 : int64 val1 = PG_GETARG_INT64(0);
296 : 15 : int16 val2 = PG_GETARG_INT16(1);
297 : :
298 : 15 : PG_RETURN_BOOL(val1 != val2);
299 : : }
300 : :
301 : : Datum
302 : 15 : int82lt(PG_FUNCTION_ARGS)
303 : : {
304 : 15 : int64 val1 = PG_GETARG_INT64(0);
305 : 15 : int16 val2 = PG_GETARG_INT16(1);
306 : :
307 : 15 : PG_RETURN_BOOL(val1 < val2);
308 : : }
309 : :
310 : : Datum
311 : 1614 : int82gt(PG_FUNCTION_ARGS)
312 : : {
313 : 1614 : int64 val1 = PG_GETARG_INT64(0);
314 : 1614 : int16 val2 = PG_GETARG_INT16(1);
315 : :
316 : 1614 : PG_RETURN_BOOL(val1 > val2);
317 : : }
318 : :
319 : : Datum
320 : 15 : int82le(PG_FUNCTION_ARGS)
321 : : {
322 : 15 : int64 val1 = PG_GETARG_INT64(0);
323 : 15 : int16 val2 = PG_GETARG_INT16(1);
324 : :
325 : 15 : PG_RETURN_BOOL(val1 <= val2);
326 : : }
327 : :
328 : : Datum
329 : 1614 : int82ge(PG_FUNCTION_ARGS)
330 : : {
331 : 1614 : int64 val1 = PG_GETARG_INT64(0);
332 : 1614 : int16 val2 = PG_GETARG_INT16(1);
333 : :
334 : 1614 : PG_RETURN_BOOL(val1 >= val2);
335 : : }
336 : :
337 : : /* int28relop()
338 : : * Is 16-bit val1 relop 64-bit val2?
339 : : */
340 : : Datum
341 : 927 : int28eq(PG_FUNCTION_ARGS)
342 : : {
343 : 927 : int16 val1 = PG_GETARG_INT16(0);
344 : 927 : int64 val2 = PG_GETARG_INT64(1);
345 : :
346 : 927 : PG_RETURN_BOOL(val1 == val2);
347 : : }
348 : :
349 : : Datum
350 : 1669 : int28ne(PG_FUNCTION_ARGS)
351 : : {
352 : 1669 : int16 val1 = PG_GETARG_INT16(0);
353 : 1669 : int64 val2 = PG_GETARG_INT64(1);
354 : :
355 : 1669 : PG_RETURN_BOOL(val1 != val2);
356 : : }
357 : :
358 : : Datum
359 : 1614 : int28lt(PG_FUNCTION_ARGS)
360 : : {
361 : 1614 : int16 val1 = PG_GETARG_INT16(0);
362 : 1614 : int64 val2 = PG_GETARG_INT64(1);
363 : :
364 : 1614 : PG_RETURN_BOOL(val1 < val2);
365 : : }
366 : :
367 : : Datum
368 : 1614 : int28gt(PG_FUNCTION_ARGS)
369 : : {
370 : 1614 : int16 val1 = PG_GETARG_INT16(0);
371 : 1614 : int64 val2 = PG_GETARG_INT64(1);
372 : :
373 : 1614 : PG_RETURN_BOOL(val1 > val2);
374 : : }
375 : :
376 : : Datum
377 : 1914 : int28le(PG_FUNCTION_ARGS)
378 : : {
379 : 1914 : int16 val1 = PG_GETARG_INT16(0);
380 : 1914 : int64 val2 = PG_GETARG_INT64(1);
381 : :
382 : 1914 : PG_RETURN_BOOL(val1 <= val2);
383 : : }
384 : :
385 : : Datum
386 : 1857 : int28ge(PG_FUNCTION_ARGS)
387 : : {
388 : 1857 : int16 val1 = PG_GETARG_INT16(0);
389 : 1857 : int64 val2 = PG_GETARG_INT64(1);
390 : :
9216 391 : 1857 : PG_RETURN_BOOL(val1 >= val2);
392 : : }
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 : : */
400 : : Datum
2768 401 : 54 : in_range_int8_int8(PG_FUNCTION_ARGS)
402 : : {
403 : 54 : int64 val = PG_GETARG_INT64(0);
404 : 54 : int64 base = PG_GETARG_INT64(1);
405 : 54 : int64 offset = PG_GETARG_INT64(2);
406 : 54 : bool sub = PG_GETARG_BOOL(3);
407 : 54 : bool less = PG_GETARG_BOOL(4);
408 : : int64 sum;
409 : :
410 [ - + ]: 54 : if (offset < 0)
2768 tgl@sss.pgh.pa.us 411 [ # # ]:UBC 0 : ereport(ERROR,
412 : : (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
413 : : errmsg("invalid preceding or following size in window function")));
414 : :
2768 tgl@sss.pgh.pa.us 415 [ + + ]:CBC 54 : if (sub)
416 : 27 : offset = -offset; /* cannot overflow */
417 : :
418 [ + + ]: 54 : 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 [ + + ]: 18 : PG_RETURN_BOOL(sub ? !less : less);
426 : : }
427 : :
428 [ + + ]: 36 : if (less)
429 : 18 : PG_RETURN_BOOL(val <= sum);
430 : : else
431 : 18 : PG_RETURN_BOOL(val >= sum);
432 : : }
433 : :
434 : :
435 : : /*----------------------------------------------------------
436 : : * Arithmetic operators on 64-bit integers.
437 : : *---------------------------------------------------------*/
438 : :
439 : : Datum
9216 440 : 468 : int8um(PG_FUNCTION_ARGS)
441 : : {
7642 442 : 468 : int64 arg = PG_GETARG_INT64(0);
443 : : int64 result;
444 : :
2825 andres@anarazel.de 445 [ + + ]: 468 : if (unlikely(arg == PG_INT64_MIN))
7642 tgl@sss.pgh.pa.us 446 [ + - ]: 3 : ereport(ERROR,
447 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
448 : : errmsg("bigint out of range")));
2825 andres@anarazel.de 449 : 465 : result = -arg;
7642 tgl@sss.pgh.pa.us 450 : 465 : PG_RETURN_INT64(result);
451 : : }
452 : :
453 : : Datum
8857 bruce@momjian.us 454 : 3 : int8up(PG_FUNCTION_ARGS)
455 : : {
7642 tgl@sss.pgh.pa.us 456 : 3 : int64 arg = PG_GETARG_INT64(0);
457 : :
458 : 3 : PG_RETURN_INT64(arg);
459 : : }
460 : :
461 : : Datum
9216 462 : 66108 : int8pl(PG_FUNCTION_ARGS)
463 : : {
7642 464 : 66108 : int64 arg1 = PG_GETARG_INT64(0);
465 : 66108 : int64 arg2 = PG_GETARG_INT64(1);
466 : : int64 result;
467 : :
2825 andres@anarazel.de 468 [ + + ]: 66108 : if (unlikely(pg_add_s64_overflow(arg1, arg2, &result)))
7642 tgl@sss.pgh.pa.us 469 [ + - ]: 6 : ereport(ERROR,
470 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
471 : : errmsg("bigint out of range")));
472 : 66102 : PG_RETURN_INT64(result);
473 : : }
474 : :
475 : : Datum
9216 476 : 126 : int8mi(PG_FUNCTION_ARGS)
477 : : {
7642 478 : 126 : int64 arg1 = PG_GETARG_INT64(0);
479 : 126 : int64 arg2 = PG_GETARG_INT64(1);
480 : : int64 result;
481 : :
2825 andres@anarazel.de 482 [ + + ]: 126 : if (unlikely(pg_sub_s64_overflow(arg1, arg2, &result)))
7642 tgl@sss.pgh.pa.us 483 [ + - ]: 9 : ereport(ERROR,
484 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
485 : : errmsg("bigint out of range")));
486 : 117 : PG_RETURN_INT64(result);
487 : : }
488 : :
489 : : Datum
9216 490 : 90 : int8mul(PG_FUNCTION_ARGS)
491 : : {
7642 492 : 90 : int64 arg1 = PG_GETARG_INT64(0);
493 : 90 : int64 arg2 = PG_GETARG_INT64(1);
494 : : int64 result;
495 : :
2825 andres@anarazel.de 496 [ + + ]: 90 : if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
497 [ + - ]: 9 : ereport(ERROR,
498 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
499 : : errmsg("bigint out of range")));
7642 tgl@sss.pgh.pa.us 500 : 81 : PG_RETURN_INT64(result);
501 : : }
502 : :
503 : : Datum
9216 504 : 66 : int8div(PG_FUNCTION_ARGS)
505 : : {
7642 506 : 66 : int64 arg1 = PG_GETARG_INT64(0);
507 : 66 : int64 arg2 = PG_GETARG_INT64(1);
508 : : int64 result;
509 : :
510 [ + + ]: 66 : if (arg2 == 0)
511 : : {
8077 512 [ + - ]: 3 : ereport(ERROR,
513 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
514 : : errmsg("division by zero")));
515 : : /* ensure compiler realizes we mustn't reach the division (gcc bug) */
516 : : PG_RETURN_NULL();
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 : : */
4674 525 [ + + ]: 63 : if (arg2 == -1)
526 : : {
2825 andres@anarazel.de 527 [ + - ]: 3 : if (unlikely(arg1 == PG_INT64_MIN))
4674 tgl@sss.pgh.pa.us 528 [ + - ]: 3 : ereport(ERROR,
529 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
530 : : errmsg("bigint out of range")));
2825 andres@anarazel.de 531 :UBC 0 : result = -arg1;
4674 tgl@sss.pgh.pa.us 532 : 0 : PG_RETURN_INT64(result);
533 : : }
534 : :
535 : : /* No overflow is possible */
536 : :
4674 tgl@sss.pgh.pa.us 537 :CBC 60 : result = arg1 / arg2;
538 : :
7642 539 : 60 : PG_RETURN_INT64(result);
540 : : }
541 : :
542 : : /* int8abs()
543 : : * Absolute value
544 : : */
545 : : Datum
9216 546 : 18 : int8abs(PG_FUNCTION_ARGS)
547 : : {
548 : 18 : int64 arg1 = PG_GETARG_INT64(0);
549 : : int64 result;
550 : :
2825 andres@anarazel.de 551 [ + + ]: 18 : if (unlikely(arg1 == PG_INT64_MIN))
7642 tgl@sss.pgh.pa.us 552 [ + - ]: 3 : ereport(ERROR,
553 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
554 : : errmsg("bigint out of range")));
2825 andres@anarazel.de 555 : 15 : result = (arg1 < 0) ? -arg1 : arg1;
7642 tgl@sss.pgh.pa.us 556 : 15 : PG_RETURN_INT64(result);
557 : : }
558 : :
559 : : /* int8mod()
560 : : * Modulo operation.
561 : : */
562 : : Datum
9216 563 : 27 : int8mod(PG_FUNCTION_ARGS)
564 : : {
7642 565 : 27 : int64 arg1 = PG_GETARG_INT64(0);
566 : 27 : int64 arg2 = PG_GETARG_INT64(1);
567 : :
2825 andres@anarazel.de 568 [ + + ]: 27 : if (unlikely(arg2 == 0))
569 : : {
8077 tgl@sss.pgh.pa.us 570 [ + - ]: 3 : ereport(ERROR,
571 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
572 : : errmsg("division by zero")));
573 : : /* ensure compiler realizes we mustn't reach the division (gcc bug) */
574 : : PG_RETURN_NULL();
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 : : */
4679 582 [ + + ]: 24 : if (arg2 == -1)
583 : 9 : PG_RETURN_INT64(0);
584 : :
585 : : /* No overflow is possible */
586 : :
7642 587 : 15 : PG_RETURN_INT64(arg1 % arg2);
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 : : */
605 : : static int64
2051 dean.a.rasheed@gmail 606 : 132 : int8gcd_internal(int64 arg1, int64 arg2)
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 : 132 : a1 = (arg1 < 0) ? arg1 : -arg1;
622 : 132 : a2 = (arg2 < 0) ? arg2 : -arg2;
623 [ + + ]: 132 : if (a1 > a2)
624 : : {
625 : 48 : swap = arg1;
626 : 48 : arg1 = arg2;
627 : 48 : arg2 = swap;
628 : : }
629 : :
630 : : /* Special care needs to be taken with INT64_MIN. See comments above. */
631 [ + + ]: 132 : if (arg1 == PG_INT64_MIN)
632 : : {
633 [ + + + + ]: 45 : if (arg2 == 0 || arg2 == PG_INT64_MIN)
634 [ + - ]: 6 : ereport(ERROR,
635 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
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 [ + + ]: 39 : if (arg2 == -1)
645 : 6 : return 1;
646 : : }
647 : :
648 : : /* Use the Euclidean algorithm to find the GCD */
649 [ + + ]: 615 : while (arg2 != 0)
650 : : {
651 : 495 : swap = arg2;
652 : 495 : arg2 = arg1 % arg2;
653 : 495 : arg1 = swap;
654 : : }
655 : :
656 : : /*
657 : : * Make sure the result is positive. (We know we don't have INT64_MIN
658 : : * anymore).
659 : : */
660 [ + + ]: 120 : if (arg1 < 0)
661 : 51 : arg1 = -arg1;
662 : :
663 : 120 : return arg1;
664 : : }
665 : :
666 : : Datum
667 : 90 : int8gcd(PG_FUNCTION_ARGS)
668 : : {
1941 tgl@sss.pgh.pa.us 669 : 90 : int64 arg1 = PG_GETARG_INT64(0);
670 : 90 : int64 arg2 = PG_GETARG_INT64(1);
671 : : int64 result;
672 : :
2051 dean.a.rasheed@gmail 673 : 90 : result = int8gcd_internal(arg1, arg2);
674 : :
675 : 84 : PG_RETURN_INT64(result);
676 : : }
677 : :
678 : : /*
679 : : * Least Common Multiple
680 : : */
681 : : Datum
682 : 78 : int8lcm(PG_FUNCTION_ARGS)
683 : : {
1941 tgl@sss.pgh.pa.us 684 : 78 : int64 arg1 = PG_GETARG_INT64(0);
685 : 78 : int64 arg2 = PG_GETARG_INT64(1);
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 : : */
2051 dean.a.rasheed@gmail 694 [ + + + + ]: 78 : if (arg1 == 0 || arg2 == 0)
695 : 36 : PG_RETURN_INT64(0);
696 : :
697 : : /* lcm(x, y) = abs(x / gcd(x, y) * y) */
698 : 42 : gcd = int8gcd_internal(arg1, arg2);
699 : 42 : arg1 = arg1 / gcd;
700 : :
701 [ + + ]: 42 : if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
702 [ + - ]: 3 : ereport(ERROR,
703 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
704 : : errmsg("bigint out of range")));
705 : :
706 : : /* If the result is INT64_MIN, it cannot be represented. */
707 [ + + ]: 39 : if (unlikely(result == PG_INT64_MIN))
708 [ + - ]: 3 : ereport(ERROR,
709 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
710 : : errmsg("bigint out of range")));
711 : :
712 [ + + ]: 36 : if (result < 0)
713 : 18 : result = -result;
714 : :
715 : 36 : PG_RETURN_INT64(result);
716 : : }
717 : :
718 : : Datum
8789 tgl@sss.pgh.pa.us 719 : 9979837 : int8inc(PG_FUNCTION_ARGS)
720 : : {
24 tgl@sss.pgh.pa.us 721 :GNC 9979837 : int64 arg = PG_GETARG_INT64(0);
722 : : int64 result;
723 : :
724 [ - + ]: 9979837 : if (unlikely(pg_add_s64_overflow(arg, 1, &result)))
24 tgl@sss.pgh.pa.us 725 [ # # ]:UNC 0 : ereport(ERROR,
726 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
727 : : errmsg("bigint out of range")));
728 : :
24 tgl@sss.pgh.pa.us 729 :GNC 9979837 : PG_RETURN_INT64(result);
730 : : }
731 : :
732 : : Datum
4165 tgl@sss.pgh.pa.us 733 :CBC 12 : int8dec(PG_FUNCTION_ARGS)
734 : : {
24 tgl@sss.pgh.pa.us 735 :GNC 12 : int64 arg = PG_GETARG_INT64(0);
736 : : int64 result;
737 : :
738 [ - + ]: 12 : if (unlikely(pg_sub_s64_overflow(arg, 1, &result)))
24 tgl@sss.pgh.pa.us 739 [ # # ]:UNC 0 : ereport(ERROR,
740 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
741 : : errmsg("bigint out of range")));
742 : :
24 tgl@sss.pgh.pa.us 743 :GNC 12 : 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 : :
756 : : Datum
6980 tgl@sss.pgh.pa.us 757 :CBC 600256 : int8inc_any(PG_FUNCTION_ARGS)
758 : : {
759 : 600256 : return int8inc(fcinfo);
760 : : }
761 : :
762 : : Datum
763 : 120012 : int8inc_float8_float8(PG_FUNCTION_ARGS)
764 : : {
765 : 120012 : return int8inc(fcinfo);
766 : : }
767 : :
768 : : Datum
4165 769 : 3 : int8dec_any(PG_FUNCTION_ARGS)
770 : : {
771 : 3 : return int8dec(fcinfo);
772 : : }
773 : :
774 : : /*
775 : : * int8inc_support
776 : : * prosupport function for int8inc() and int8inc_any()
777 : : */
778 : : Datum
1247 drowley@postgresql.o 779 : 292 : int8inc_support(PG_FUNCTION_ARGS)
780 : : {
781 : 292 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
782 : :
783 [ + + ]: 292 : if (IsA(rawreq, SupportRequestWFuncMonotonic))
784 : : {
785 : 39 : SupportRequestWFuncMonotonic *req = (SupportRequestWFuncMonotonic *) rawreq;
786 : 39 : MonotonicFunction monotonic = MONOTONICFUNC_NONE;
787 : 39 : int frameOptions = req->window_clause->frameOptions;
788 : :
789 : : /* No ORDER BY clause then all rows are peers */
790 [ + + ]: 39 : if (req->window_clause->orderClause == NIL)
791 : 12 : 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 [ + + ]: 27 : if (frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
800 : 21 : 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 [ + + ]: 27 : if (frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
807 : 6 : monotonic |= MONOTONICFUNC_DECREASING;
808 : : }
809 : :
810 : 39 : req->monotonic = monotonic;
811 : 39 : PG_RETURN_POINTER(req);
812 : : }
813 : :
814 : 253 : PG_RETURN_POINTER(NULL);
815 : : }
816 : :
817 : :
818 : : Datum
9216 tgl@sss.pgh.pa.us 819 : 429 : int8larger(PG_FUNCTION_ARGS)
820 : : {
7642 821 : 429 : int64 arg1 = PG_GETARG_INT64(0);
822 : 429 : int64 arg2 = PG_GETARG_INT64(1);
823 : : int64 result;
824 : :
825 : 429 : result = ((arg1 > arg2) ? arg1 : arg2);
826 : :
9216 827 : 429 : PG_RETURN_INT64(result);
828 : : }
829 : :
830 : : Datum
831 : 4365 : int8smaller(PG_FUNCTION_ARGS)
832 : : {
7642 833 : 4365 : int64 arg1 = PG_GETARG_INT64(0);
834 : 4365 : int64 arg2 = PG_GETARG_INT64(1);
835 : : int64 result;
836 : :
837 : 4365 : result = ((arg1 < arg2) ? arg1 : arg2);
838 : :
9216 839 : 4365 : PG_RETURN_INT64(result);
840 : : }
841 : :
842 : : Datum
843 : 2609 : int84pl(PG_FUNCTION_ARGS)
844 : : {
7642 845 : 2609 : int64 arg1 = PG_GETARG_INT64(0);
846 : 2609 : int32 arg2 = PG_GETARG_INT32(1);
847 : : int64 result;
848 : :
2825 andres@anarazel.de 849 [ + + ]: 2609 : if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
7642 tgl@sss.pgh.pa.us 850 [ + - ]: 3 : ereport(ERROR,
851 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
852 : : errmsg("bigint out of range")));
853 : 2606 : PG_RETURN_INT64(result);
854 : : }
855 : :
856 : : Datum
9216 857 : 64 : int84mi(PG_FUNCTION_ARGS)
858 : : {
7642 859 : 64 : int64 arg1 = PG_GETARG_INT64(0);
860 : 64 : int32 arg2 = PG_GETARG_INT32(1);
861 : : int64 result;
862 : :
2825 andres@anarazel.de 863 [ + + ]: 64 : if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
7642 tgl@sss.pgh.pa.us 864 [ + - ]: 3 : ereport(ERROR,
865 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
866 : : errmsg("bigint out of range")));
867 : 61 : PG_RETURN_INT64(result);
868 : : }
869 : :
870 : : Datum
9216 871 : 1195 : int84mul(PG_FUNCTION_ARGS)
872 : : {
7642 873 : 1195 : int64 arg1 = PG_GETARG_INT64(0);
874 : 1195 : int32 arg2 = PG_GETARG_INT32(1);
875 : : int64 result;
876 : :
2825 andres@anarazel.de 877 [ + + ]: 1195 : if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
7642 tgl@sss.pgh.pa.us 878 [ + - ]: 6 : ereport(ERROR,
879 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
880 : : errmsg("bigint out of range")));
881 : 1189 : PG_RETURN_INT64(result);
882 : : }
883 : :
884 : : Datum
9216 885 : 97 : int84div(PG_FUNCTION_ARGS)
886 : : {
7642 887 : 97 : int64 arg1 = PG_GETARG_INT64(0);
888 : 97 : int32 arg2 = PG_GETARG_INT32(1);
889 : : int64 result;
890 : :
891 [ + + ]: 97 : if (arg2 == 0)
892 : : {
8077 893 [ + - ]: 3 : ereport(ERROR,
894 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
895 : : errmsg("division by zero")));
896 : : /* ensure compiler realizes we mustn't reach the division (gcc bug) */
897 : : PG_RETURN_NULL();
898 : : }
899 : :
900 : : /*
901 : : * INT64_MIN / -1 is problematic, since the result can't be represented on
902 : : * a two's-complement machine. Some machines produce INT64_MIN, some
903 : : * produce zero, some throw an exception. We can dodge the problem by
904 : : * recognizing that division by -1 is the same as negation.
905 : : */
4674 906 [ + + ]: 94 : if (arg2 == -1)
907 : : {
2825 andres@anarazel.de 908 [ + - ]: 3 : if (unlikely(arg1 == PG_INT64_MIN))
4674 tgl@sss.pgh.pa.us 909 [ + - ]: 3 : ereport(ERROR,
910 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
911 : : errmsg("bigint out of range")));
2825 andres@anarazel.de 912 :UBC 0 : result = -arg1;
4674 tgl@sss.pgh.pa.us 913 : 0 : PG_RETURN_INT64(result);
914 : : }
915 : :
916 : : /* No overflow is possible */
917 : :
4674 tgl@sss.pgh.pa.us 918 :CBC 91 : result = arg1 / arg2;
919 : :
7642 920 : 91 : PG_RETURN_INT64(result);
921 : : }
922 : :
923 : : Datum
9216 924 : 691 : int48pl(PG_FUNCTION_ARGS)
925 : : {
7642 926 : 691 : int32 arg1 = PG_GETARG_INT32(0);
927 : 691 : int64 arg2 = PG_GETARG_INT64(1);
928 : : int64 result;
929 : :
2825 andres@anarazel.de 930 [ + + ]: 691 : if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
7642 tgl@sss.pgh.pa.us 931 [ + - ]: 3 : ereport(ERROR,
932 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
933 : : errmsg("bigint out of range")));
934 : 688 : PG_RETURN_INT64(result);
935 : : }
936 : :
937 : : Datum
9216 938 : 33 : int48mi(PG_FUNCTION_ARGS)
939 : : {
7642 940 : 33 : int32 arg1 = PG_GETARG_INT32(0);
941 : 33 : int64 arg2 = PG_GETARG_INT64(1);
942 : : int64 result;
943 : :
2825 andres@anarazel.de 944 [ + + ]: 33 : if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
7642 tgl@sss.pgh.pa.us 945 [ + - ]: 3 : ereport(ERROR,
946 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
947 : : errmsg("bigint out of range")));
948 : 30 : PG_RETURN_INT64(result);
949 : : }
950 : :
951 : : Datum
9216 952 : 111 : int48mul(PG_FUNCTION_ARGS)
953 : : {
7642 954 : 111 : int32 arg1 = PG_GETARG_INT32(0);
955 : 111 : int64 arg2 = PG_GETARG_INT64(1);
956 : : int64 result;
957 : :
2825 andres@anarazel.de 958 [ + + ]: 111 : if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
7642 tgl@sss.pgh.pa.us 959 [ + - ]: 3 : ereport(ERROR,
960 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
961 : : errmsg("bigint out of range")));
962 : 108 : PG_RETURN_INT64(result);
963 : : }
964 : :
965 : : Datum
9216 966 : 18 : int48div(PG_FUNCTION_ARGS)
967 : : {
7642 968 : 18 : int32 arg1 = PG_GETARG_INT32(0);
969 : 18 : int64 arg2 = PG_GETARG_INT64(1);
970 : :
2825 andres@anarazel.de 971 [ + + ]: 18 : if (unlikely(arg2 == 0))
972 : : {
8077 tgl@sss.pgh.pa.us 973 [ + - ]: 3 : ereport(ERROR,
974 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
975 : : errmsg("division by zero")));
976 : : /* ensure compiler realizes we mustn't reach the division (gcc bug) */
977 : : PG_RETURN_NULL();
978 : : }
979 : :
980 : : /* No overflow is possible */
7642 981 : 15 : PG_RETURN_INT64((int64) arg1 / arg2);
982 : : }
983 : :
984 : : Datum
6290 985 : 18 : int82pl(PG_FUNCTION_ARGS)
986 : : {
987 : 18 : int64 arg1 = PG_GETARG_INT64(0);
988 : 18 : int16 arg2 = PG_GETARG_INT16(1);
989 : : int64 result;
990 : :
2825 andres@anarazel.de 991 [ + + ]: 18 : if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result)))
6290 tgl@sss.pgh.pa.us 992 [ + - ]: 3 : ereport(ERROR,
993 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
994 : : errmsg("bigint out of range")));
995 : 15 : PG_RETURN_INT64(result);
996 : : }
997 : :
998 : : Datum
999 : 18 : int82mi(PG_FUNCTION_ARGS)
1000 : : {
1001 : 18 : int64 arg1 = PG_GETARG_INT64(0);
1002 : 18 : int16 arg2 = PG_GETARG_INT16(1);
1003 : : int64 result;
1004 : :
2825 andres@anarazel.de 1005 [ + + ]: 18 : if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result)))
6290 tgl@sss.pgh.pa.us 1006 [ + - ]: 3 : ereport(ERROR,
1007 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1008 : : errmsg("bigint out of range")));
1009 : 15 : PG_RETURN_INT64(result);
1010 : : }
1011 : :
1012 : : Datum
1013 : 21 : int82mul(PG_FUNCTION_ARGS)
1014 : : {
1015 : 21 : int64 arg1 = PG_GETARG_INT64(0);
1016 : 21 : int16 arg2 = PG_GETARG_INT16(1);
1017 : : int64 result;
1018 : :
2825 andres@anarazel.de 1019 [ + + ]: 21 : if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result)))
6290 tgl@sss.pgh.pa.us 1020 [ + - ]: 6 : ereport(ERROR,
1021 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1022 : : errmsg("bigint out of range")));
1023 : 15 : PG_RETURN_INT64(result);
1024 : : }
1025 : :
1026 : : Datum
1027 : 21 : int82div(PG_FUNCTION_ARGS)
1028 : : {
1029 : 21 : int64 arg1 = PG_GETARG_INT64(0);
1030 : 21 : int16 arg2 = PG_GETARG_INT16(1);
1031 : : int64 result;
1032 : :
2825 andres@anarazel.de 1033 [ + + ]: 21 : if (unlikely(arg2 == 0))
1034 : : {
6290 tgl@sss.pgh.pa.us 1035 [ + - ]: 3 : ereport(ERROR,
1036 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
1037 : : errmsg("division by zero")));
1038 : : /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1039 : : PG_RETURN_NULL();
1040 : : }
1041 : :
1042 : : /*
1043 : : * INT64_MIN / -1 is problematic, since the result can't be represented on
1044 : : * a two's-complement machine. Some machines produce INT64_MIN, some
1045 : : * produce zero, some throw an exception. We can dodge the problem by
1046 : : * recognizing that division by -1 is the same as negation.
1047 : : */
4674 1048 [ + + ]: 18 : if (arg2 == -1)
1049 : : {
2825 andres@anarazel.de 1050 [ + - ]: 3 : if (unlikely(arg1 == PG_INT64_MIN))
4674 tgl@sss.pgh.pa.us 1051 [ + - ]: 3 : ereport(ERROR,
1052 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1053 : : errmsg("bigint out of range")));
2825 andres@anarazel.de 1054 :UBC 0 : result = -arg1;
4674 tgl@sss.pgh.pa.us 1055 : 0 : PG_RETURN_INT64(result);
1056 : : }
1057 : :
1058 : : /* No overflow is possible */
1059 : :
4674 tgl@sss.pgh.pa.us 1060 :CBC 15 : result = arg1 / arg2;
1061 : :
6290 1062 : 15 : PG_RETURN_INT64(result);
1063 : : }
1064 : :
1065 : : Datum
1066 : 18 : int28pl(PG_FUNCTION_ARGS)
1067 : : {
1068 : 18 : int16 arg1 = PG_GETARG_INT16(0);
1069 : 18 : int64 arg2 = PG_GETARG_INT64(1);
1070 : : int64 result;
1071 : :
2825 andres@anarazel.de 1072 [ + + ]: 18 : if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result)))
6290 tgl@sss.pgh.pa.us 1073 [ + - ]: 3 : ereport(ERROR,
1074 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1075 : : errmsg("bigint out of range")));
1076 : 15 : PG_RETURN_INT64(result);
1077 : : }
1078 : :
1079 : : Datum
1080 : 18 : int28mi(PG_FUNCTION_ARGS)
1081 : : {
1082 : 18 : int16 arg1 = PG_GETARG_INT16(0);
1083 : 18 : int64 arg2 = PG_GETARG_INT64(1);
1084 : : int64 result;
1085 : :
2825 andres@anarazel.de 1086 [ + + ]: 18 : if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result)))
6290 tgl@sss.pgh.pa.us 1087 [ + - ]: 3 : ereport(ERROR,
1088 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1089 : : errmsg("bigint out of range")));
1090 : 15 : PG_RETURN_INT64(result);
1091 : : }
1092 : :
1093 : : Datum
1094 : 18 : int28mul(PG_FUNCTION_ARGS)
1095 : : {
1096 : 18 : int16 arg1 = PG_GETARG_INT16(0);
1097 : 18 : int64 arg2 = PG_GETARG_INT64(1);
1098 : : int64 result;
1099 : :
2825 andres@anarazel.de 1100 [ + + ]: 18 : if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result)))
6290 tgl@sss.pgh.pa.us 1101 [ + - ]: 3 : ereport(ERROR,
1102 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1103 : : errmsg("bigint out of range")));
1104 : 15 : PG_RETURN_INT64(result);
1105 : : }
1106 : :
1107 : : Datum
1108 : 18 : int28div(PG_FUNCTION_ARGS)
1109 : : {
1110 : 18 : int16 arg1 = PG_GETARG_INT16(0);
1111 : 18 : int64 arg2 = PG_GETARG_INT64(1);
1112 : :
2825 andres@anarazel.de 1113 [ + + ]: 18 : if (unlikely(arg2 == 0))
1114 : : {
6290 tgl@sss.pgh.pa.us 1115 [ + - ]: 3 : ereport(ERROR,
1116 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
1117 : : errmsg("division by zero")));
1118 : : /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1119 : : PG_RETURN_NULL();
1120 : : }
1121 : :
1122 : : /* No overflow is possible */
1123 : 15 : PG_RETURN_INT64((int64) arg1 / arg2);
1124 : : }
1125 : :
1126 : : /* Binary arithmetics
1127 : : *
1128 : : * int8and - returns arg1 & arg2
1129 : : * int8or - returns arg1 | arg2
1130 : : * int8xor - returns arg1 # arg2
1131 : : * int8not - returns ~arg1
1132 : : * int8shl - returns arg1 << arg2
1133 : : * int8shr - returns arg1 >> arg2
1134 : : */
1135 : :
1136 : : Datum
9083 peter_e@gmx.net 1137 : 21 : int8and(PG_FUNCTION_ARGS)
1138 : : {
1139 : 21 : int64 arg1 = PG_GETARG_INT64(0);
1140 : 21 : int64 arg2 = PG_GETARG_INT64(1);
1141 : :
1142 : 21 : PG_RETURN_INT64(arg1 & arg2);
1143 : : }
1144 : :
1145 : : Datum
1146 : 23 : int8or(PG_FUNCTION_ARGS)
1147 : : {
1148 : 23 : int64 arg1 = PG_GETARG_INT64(0);
1149 : 23 : int64 arg2 = PG_GETARG_INT64(1);
1150 : :
1151 : 23 : PG_RETURN_INT64(arg1 | arg2);
1152 : : }
1153 : :
1154 : : Datum
1155 : 21 : int8xor(PG_FUNCTION_ARGS)
1156 : : {
1157 : 21 : int64 arg1 = PG_GETARG_INT64(0);
1158 : 21 : int64 arg2 = PG_GETARG_INT64(1);
1159 : :
1160 : 21 : PG_RETURN_INT64(arg1 ^ arg2);
1161 : : }
1162 : :
1163 : : Datum
1164 : 15 : int8not(PG_FUNCTION_ARGS)
1165 : : {
1166 : 15 : int64 arg1 = PG_GETARG_INT64(0);
1167 : :
1168 : 15 : PG_RETURN_INT64(~arg1);
1169 : : }
1170 : :
1171 : : Datum
1172 : 23 : int8shl(PG_FUNCTION_ARGS)
1173 : : {
1174 : 23 : int64 arg1 = PG_GETARG_INT64(0);
1175 : 23 : int32 arg2 = PG_GETARG_INT32(1);
1176 : :
1177 : 23 : PG_RETURN_INT64(arg1 << arg2);
1178 : : }
1179 : :
1180 : : Datum
1181 : 15 : int8shr(PG_FUNCTION_ARGS)
1182 : : {
1183 : 15 : int64 arg1 = PG_GETARG_INT64(0);
1184 : 15 : int32 arg2 = PG_GETARG_INT32(1);
1185 : :
1186 : 15 : PG_RETURN_INT64(arg1 >> arg2);
1187 : : }
1188 : :
1189 : : /*----------------------------------------------------------
1190 : : * Conversion operators.
1191 : : *---------------------------------------------------------*/
1192 : :
1193 : : Datum
9216 tgl@sss.pgh.pa.us 1194 : 1262279 : int48(PG_FUNCTION_ARGS)
1195 : : {
7642 1196 : 1262279 : int32 arg = PG_GETARG_INT32(0);
1197 : :
1198 : 1262279 : PG_RETURN_INT64((int64) arg);
1199 : : }
1200 : :
1201 : : Datum
9216 1202 : 102260 : int84(PG_FUNCTION_ARGS)
1203 : : {
7642 1204 : 102260 : int64 arg = PG_GETARG_INT64(0);
1205 : :
2825 andres@anarazel.de 1206 [ + - + + ]: 102260 : if (unlikely(arg < PG_INT32_MIN) || unlikely(arg > PG_INT32_MAX))
8077 tgl@sss.pgh.pa.us 1207 [ + - ]: 3 : ereport(ERROR,
1208 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1209 : : errmsg("integer out of range")));
1210 : :
2825 andres@anarazel.de 1211 : 102257 : PG_RETURN_INT32((int32) arg);
1212 : : }
1213 : :
1214 : : Datum
8717 tgl@sss.pgh.pa.us 1215 : 9 : int28(PG_FUNCTION_ARGS)
1216 : : {
7642 1217 : 9 : int16 arg = PG_GETARG_INT16(0);
1218 : :
1219 : 9 : PG_RETURN_INT64((int64) arg);
1220 : : }
1221 : :
1222 : : Datum
8717 1223 : 18 : int82(PG_FUNCTION_ARGS)
1224 : : {
7642 1225 : 18 : int64 arg = PG_GETARG_INT64(0);
1226 : :
2825 andres@anarazel.de 1227 [ + - + + ]: 18 : if (unlikely(arg < PG_INT16_MIN) || unlikely(arg > PG_INT16_MAX))
8077 tgl@sss.pgh.pa.us 1228 [ + - ]: 3 : ereport(ERROR,
1229 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1230 : : errmsg("smallint out of range")));
1231 : :
2825 andres@anarazel.de 1232 : 15 : PG_RETURN_INT16((int16) arg);
1233 : : }
1234 : :
1235 : : Datum
9216 tgl@sss.pgh.pa.us 1236 : 6099 : i8tod(PG_FUNCTION_ARGS)
1237 : : {
7642 1238 : 6099 : int64 arg = PG_GETARG_INT64(0);
1239 : : float8 result;
1240 : :
1241 : 6099 : result = arg;
1242 : :
9216 1243 : 6099 : PG_RETURN_FLOAT8(result);
1244 : : }
1245 : :
1246 : : /* dtoi8()
1247 : : * Convert float8 to 8-byte integer.
1248 : : */
1249 : : Datum
1250 : 72 : dtoi8(PG_FUNCTION_ARGS)
1251 : : {
2479 1252 : 72 : float8 num = PG_GETARG_FLOAT8(0);
1253 : :
1254 : : /*
1255 : : * Get rid of any fractional part in the input. This is so we don't fail
1256 : : * on just-out-of-range values that would round into range. Note
1257 : : * assumption that rint() will pass through a NaN or Inf unchanged.
1258 : : */
1259 : 72 : num = rint(num);
1260 : :
1261 : : /* Range check */
2130 1262 [ + - + + : 72 : if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT64(num)))
+ + + + ]
8077 1263 [ + - ]: 9 : ereport(ERROR,
1264 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1265 : : errmsg("bigint out of range")));
1266 : :
2479 1267 : 63 : PG_RETURN_INT64((int64) num);
1268 : : }
1269 : :
1270 : : Datum
8389 1271 : 75 : i8tof(PG_FUNCTION_ARGS)
1272 : : {
7642 1273 : 75 : int64 arg = PG_GETARG_INT64(0);
1274 : : float4 result;
1275 : :
1276 : 75 : result = arg;
1277 : :
8389 1278 : 75 : PG_RETURN_FLOAT4(result);
1279 : : }
1280 : :
1281 : : /* ftoi8()
1282 : : * Convert float4 to 8-byte integer.
1283 : : */
1284 : : Datum
1285 : 15 : ftoi8(PG_FUNCTION_ARGS)
1286 : : {
2479 1287 : 15 : float4 num = PG_GETARG_FLOAT4(0);
1288 : :
1289 : : /*
1290 : : * Get rid of any fractional part in the input. This is so we don't fail
1291 : : * on just-out-of-range values that would round into range. Note
1292 : : * assumption that rint() will pass through a NaN or Inf unchanged.
1293 : : */
1294 : 15 : num = rint(num);
1295 : :
1296 : : /* Range check */
2130 1297 [ + - + + : 15 : if (unlikely(isnan(num) || !FLOAT4_FITS_IN_INT64(num)))
+ + + + ]
8077 1298 [ + - ]: 6 : ereport(ERROR,
1299 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1300 : : errmsg("bigint out of range")));
1301 : :
2479 1302 : 9 : PG_RETURN_INT64((int64) num);
1303 : : }
1304 : :
1305 : : Datum
8389 1306 : 10 : i8tooid(PG_FUNCTION_ARGS)
1307 : : {
7642 1308 : 10 : int64 arg = PG_GETARG_INT64(0);
1309 : :
2825 andres@anarazel.de 1310 [ + - + + ]: 10 : if (unlikely(arg < 0) || unlikely(arg > PG_UINT32_MAX))
8077 tgl@sss.pgh.pa.us 1311 [ + - ]: 3 : ereport(ERROR,
1312 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1313 : : errmsg("OID out of range")));
1314 : :
2825 andres@anarazel.de 1315 : 7 : PG_RETURN_OID((Oid) arg);
1316 : : }
1317 : :
1318 : : Datum
8389 tgl@sss.pgh.pa.us 1319 : 11 : oidtoi8(PG_FUNCTION_ARGS)
1320 : : {
7642 1321 : 11 : Oid arg = PG_GETARG_OID(0);
1322 : :
1323 : 11 : PG_RETURN_INT64((int64) arg);
1324 : : }
1325 : :
1326 : : /*
1327 : : * non-persistent numeric series generator
1328 : : */
1329 : : Datum
7886 mail@joeconway.com 1330 : 1689505 : generate_series_int8(PG_FUNCTION_ARGS)
1331 : : {
1332 : 1689505 : return generate_series_step_int8(fcinfo);
1333 : : }
1334 : :
1335 : : Datum
1336 : 1689578 : generate_series_step_int8(PG_FUNCTION_ARGS)
1337 : : {
1338 : : FuncCallContext *funcctx;
1339 : : generate_series_fctx *fctx;
1340 : : int64 result;
1341 : : MemoryContext oldcontext;
1342 : :
1343 : : /* stuff done only on the first call of the function */
1344 [ + + ]: 1689578 : if (SRF_IS_FIRSTCALL())
1345 : : {
7678 bruce@momjian.us 1346 : 30 : int64 start = PG_GETARG_INT64(0);
1347 : 30 : int64 finish = PG_GETARG_INT64(1);
1348 : 30 : int64 step = 1;
1349 : :
1350 : : /* see if we were given an explicit step size */
7886 mail@joeconway.com 1351 [ + + ]: 30 : if (PG_NARGS() == 3)
1352 : 7 : step = PG_GETARG_INT64(2);
1353 [ + + ]: 30 : if (step == 0)
1354 [ + - ]: 3 : ereport(ERROR,
1355 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1356 : : errmsg("step size cannot equal zero")));
1357 : :
1358 : : /* create a function context for cross-call persistence */
1359 : 27 : funcctx = SRF_FIRSTCALL_INIT();
1360 : :
1361 : : /*
1362 : : * switch to memory context appropriate for multiple function calls
1363 : : */
1364 : 27 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1365 : :
1366 : : /* allocate memory for user context */
1367 : 27 : fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1368 : :
1369 : : /*
1370 : : * Use fctx to keep state from call to call. Seed current with the
1371 : : * original start value
1372 : : */
1373 : 27 : fctx->current = start;
1374 : 27 : fctx->finish = finish;
1375 : 27 : fctx->step = step;
1376 : :
1377 : 27 : funcctx->user_fctx = fctx;
1378 : 27 : MemoryContextSwitchTo(oldcontext);
1379 : : }
1380 : :
1381 : : /* stuff done on every call of the function */
1382 : 1689575 : funcctx = SRF_PERCALL_SETUP();
1383 : :
1384 : : /*
1385 : : * get the saved state and use current as the result for this iteration
1386 : : */
1387 : 1689575 : fctx = funcctx->user_fctx;
1388 : 1689575 : result = fctx->current;
1389 : :
1390 [ + - + + ]: 1689575 : if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1391 [ - + - - ]: 26 : (fctx->step < 0 && fctx->current >= fctx->finish))
1392 : : {
1393 : : /*
1394 : : * Increment current in preparation for next iteration. If next-value
1395 : : * computation overflows, this is the final result.
1396 : : */
2825 andres@anarazel.de 1397 [ - + ]: 1689549 : if (pg_add_s64_overflow(fctx->current, fctx->step, &fctx->current))
5195 rhaas@postgresql.org 1398 :UBC 0 : fctx->step = 0;
1399 : :
1400 : : /* do when there is more left to send */
7886 mail@joeconway.com 1401 :CBC 1689549 : SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1402 : : }
1403 : : else
1404 : : /* do when there is no more left */
1405 : 26 : SRF_RETURN_DONE(funcctx);
1406 : : }
1407 : :
1408 : : /*
1409 : : * Planner support function for generate_series(int8, int8 [, int8])
1410 : : */
1411 : : Datum
2401 tgl@sss.pgh.pa.us 1412 : 84 : generate_series_int8_support(PG_FUNCTION_ARGS)
1413 : : {
1414 : 84 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1415 : 84 : Node *ret = NULL;
1416 : :
1417 [ + + ]: 84 : if (IsA(rawreq, SupportRequestRows))
1418 : : {
1419 : : /* Try to estimate the number of rows returned */
1420 : 27 : SupportRequestRows *req = (SupportRequestRows *) rawreq;
1421 : :
1422 [ + - ]: 27 : if (is_funcclause(req->node)) /* be paranoid */
1423 : : {
1424 : 27 : List *args = ((FuncExpr *) req->node)->args;
1425 : : Node *arg1,
1426 : : *arg2,
1427 : : *arg3;
1428 : :
1429 : : /* We can use estimated argument values here */
1430 : 27 : arg1 = estimate_expression_value(req->root, linitial(args));
1431 : 27 : arg2 = estimate_expression_value(req->root, lsecond(args));
1432 [ + + ]: 27 : if (list_length(args) >= 3)
1433 : 7 : arg3 = estimate_expression_value(req->root, lthird(args));
1434 : : else
1435 : 20 : arg3 = NULL;
1436 : :
1437 : : /*
1438 : : * If any argument is constant NULL, we can safely assume that
1439 : : * zero rows are returned. Otherwise, if they're all non-NULL
1440 : : * constants, we can calculate the number of rows that will be
1441 : : * returned. Use double arithmetic to avoid overflow hazards.
1442 : : */
1443 [ + + ]: 27 : if ((IsA(arg1, Const) &&
1444 [ + - ]: 23 : ((Const *) arg1)->constisnull) ||
1445 [ + + ]: 27 : (IsA(arg2, Const) &&
1446 [ + - + + ]: 27 : ((Const *) arg2)->constisnull) ||
1447 [ + - ]: 7 : (arg3 != NULL && IsA(arg3, Const) &&
1448 [ - + ]: 7 : ((Const *) arg3)->constisnull))
1449 : : {
2401 tgl@sss.pgh.pa.us 1450 :UBC 0 : req->rows = 0;
1451 : 0 : ret = (Node *) req;
1452 : : }
2401 tgl@sss.pgh.pa.us 1453 [ + + ]:CBC 27 : else if (IsA(arg1, Const) &&
1454 [ + + + + ]: 23 : IsA(arg2, Const) &&
1455 [ + - ]: 7 : (arg3 == NULL || IsA(arg3, Const)))
1456 : : {
1457 : : double start,
1458 : : finish,
1459 : : step;
1460 : :
1461 : 17 : start = DatumGetInt64(((Const *) arg1)->constvalue);
1462 : 17 : finish = DatumGetInt64(((Const *) arg2)->constvalue);
1463 [ + + ]: 17 : step = arg3 ? DatumGetInt64(((Const *) arg3)->constvalue) : 1;
1464 : :
1465 : : /* This equation works for either sign of step */
1466 [ + + ]: 17 : if (step != 0)
1467 : : {
1468 : 14 : req->rows = floor((finish - start + step) / step);
1469 : 14 : ret = (Node *) req;
1470 : : }
1471 : : }
1472 : : }
1473 : : }
1474 : :
1475 : 84 : PG_RETURN_POINTER(ret);
1476 : : }
|