PostgreSQL Source Code  git master
numeric.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * numeric.c
4  * An exact numeric data type for the Postgres database system
5  *
6  * Original coding 1998, Jan Wieck. Heavily revised 2003, Tom Lane.
7  *
8  * Many of the algorithmic ideas are borrowed from David M. Smith's "FM"
9  * multiple-precision math library, most recently published as Algorithm
10  * 786: Multiple-Precision Complex Arithmetic and Functions, ACM
11  * Transactions on Mathematical Software, Vol. 24, No. 4, December 1998,
12  * pages 359-367.
13  *
14  * Copyright (c) 1998-2024, PostgreSQL Global Development Group
15  *
16  * IDENTIFICATION
17  * src/backend/utils/adt/numeric.c
18  *
19  *-------------------------------------------------------------------------
20  */
21 
22 #include "postgres.h"
23 
24 #include <ctype.h>
25 #include <float.h>
26 #include <limits.h>
27 #include <math.h>
28 
29 #include "common/hashfn.h"
30 #include "common/int.h"
31 #include "funcapi.h"
32 #include "lib/hyperloglog.h"
33 #include "libpq/pqformat.h"
34 #include "miscadmin.h"
35 #include "nodes/nodeFuncs.h"
36 #include "nodes/supportnodes.h"
37 #include "utils/array.h"
38 #include "utils/builtins.h"
39 #include "utils/float.h"
40 #include "utils/guc.h"
41 #include "utils/numeric.h"
42 #include "utils/pg_lsn.h"
43 #include "utils/sortsupport.h"
44 
45 /* ----------
46  * Uncomment the following to enable compilation of dump_numeric()
47  * and dump_var() and to get a dump of any result produced by make_result().
48  * ----------
49 #define NUMERIC_DEBUG
50  */
51 
52 
53 /* ----------
54  * Local data types
55  *
56  * Numeric values are represented in a base-NBASE floating point format.
57  * Each "digit" ranges from 0 to NBASE-1. The type NumericDigit is signed
58  * and wide enough to store a digit. We assume that NBASE*NBASE can fit in
59  * an int. Although the purely calculational routines could handle any even
60  * NBASE that's less than sqrt(INT_MAX), in practice we are only interested
61  * in NBASE a power of ten, so that I/O conversions and decimal rounding
62  * are easy. Also, it's actually more efficient if NBASE is rather less than
63  * sqrt(INT_MAX), so that there is "headroom" for mul_var and div_var_fast to
64  * postpone processing carries.
65  *
66  * Values of NBASE other than 10000 are considered of historical interest only
67  * and are no longer supported in any sense; no mechanism exists for the client
68  * to discover the base, so every client supporting binary mode expects the
69  * base-10000 format. If you plan to change this, also note the numeric
70  * abbreviation code, which assumes NBASE=10000.
71  * ----------
72  */
73 
74 #if 0
75 #define NBASE 10
76 #define HALF_NBASE 5
77 #define DEC_DIGITS 1 /* decimal digits per NBASE digit */
78 #define MUL_GUARD_DIGITS 4 /* these are measured in NBASE digits */
79 #define DIV_GUARD_DIGITS 8
80 
81 typedef signed char NumericDigit;
82 #endif
83 
84 #if 0
85 #define NBASE 100
86 #define HALF_NBASE 50
87 #define DEC_DIGITS 2 /* decimal digits per NBASE digit */
88 #define MUL_GUARD_DIGITS 3 /* these are measured in NBASE digits */
89 #define DIV_GUARD_DIGITS 6
90 
91 typedef signed char NumericDigit;
92 #endif
93 
94 #if 1
95 #define NBASE 10000
96 #define HALF_NBASE 5000
97 #define DEC_DIGITS 4 /* decimal digits per NBASE digit */
98 #define MUL_GUARD_DIGITS 2 /* these are measured in NBASE digits */
99 #define DIV_GUARD_DIGITS 4
100 
102 #endif
103 
104 /*
105  * The Numeric type as stored on disk.
106  *
107  * If the high bits of the first word of a NumericChoice (n_header, or
108  * n_short.n_header, or n_long.n_sign_dscale) are NUMERIC_SHORT, then the
109  * numeric follows the NumericShort format; if they are NUMERIC_POS or
110  * NUMERIC_NEG, it follows the NumericLong format. If they are NUMERIC_SPECIAL,
111  * the value is a NaN or Infinity. We currently always store SPECIAL values
112  * using just two bytes (i.e. only n_header), but previous releases used only
113  * the NumericLong format, so we might find 4-byte NaNs (though not infinities)
114  * on disk if a database has been migrated using pg_upgrade. In either case,
115  * the low-order bits of a special value's header are reserved and currently
116  * should always be set to zero.
117  *
118  * In the NumericShort format, the remaining 14 bits of the header word
119  * (n_short.n_header) are allocated as follows: 1 for sign (positive or
120  * negative), 6 for dynamic scale, and 7 for weight. In practice, most
121  * commonly-encountered values can be represented this way.
122  *
123  * In the NumericLong format, the remaining 14 bits of the header word
124  * (n_long.n_sign_dscale) represent the display scale; and the weight is
125  * stored separately in n_weight.
126  *
127  * NOTE: by convention, values in the packed form have been stripped of
128  * all leading and trailing zero digits (where a "digit" is of base NBASE).
129  * In particular, if the value is zero, there will be no digits at all!
130  * The weight is arbitrary in that case, but we normally set it to zero.
131  */
132 
134 {
135  uint16 n_header; /* Sign + display scale + weight */
137 };
138 
140 {
141  uint16 n_sign_dscale; /* Sign + display scale */
142  int16 n_weight; /* Weight of 1st digit */
144 };
145 
147 {
148  uint16 n_header; /* Header word */
149  struct NumericLong n_long; /* Long form (4-byte header) */
150  struct NumericShort n_short; /* Short form (2-byte header) */
151 };
152 
154 {
155  int32 vl_len_; /* varlena header (do not touch directly!) */
156  union NumericChoice choice; /* choice of format */
157 };
158 
159 
160 /*
161  * Interpretation of high bits.
162  */
163 
164 #define NUMERIC_SIGN_MASK 0xC000
165 #define NUMERIC_POS 0x0000
166 #define NUMERIC_NEG 0x4000
167 #define NUMERIC_SHORT 0x8000
168 #define NUMERIC_SPECIAL 0xC000
169 
170 #define NUMERIC_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_SIGN_MASK)
171 #define NUMERIC_IS_SHORT(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
172 #define NUMERIC_IS_SPECIAL(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SPECIAL)
173 
174 #define NUMERIC_HDRSZ (VARHDRSZ + sizeof(uint16) + sizeof(int16))
175 #define NUMERIC_HDRSZ_SHORT (VARHDRSZ + sizeof(uint16))
176 
177 /*
178  * If the flag bits are NUMERIC_SHORT or NUMERIC_SPECIAL, we want the short
179  * header; otherwise, we want the long one. Instead of testing against each
180  * value, we can just look at the high bit, for a slight efficiency gain.
181  */
182 #define NUMERIC_HEADER_IS_SHORT(n) (((n)->choice.n_header & 0x8000) != 0)
183 #define NUMERIC_HEADER_SIZE(n) \
184  (VARHDRSZ + sizeof(uint16) + \
185  (NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
186 
187 /*
188  * Definitions for special values (NaN, positive infinity, negative infinity).
189  *
190  * The two bits after the NUMERIC_SPECIAL bits are 00 for NaN, 01 for positive
191  * infinity, 11 for negative infinity. (This makes the sign bit match where
192  * it is in a short-format value, though we make no use of that at present.)
193  * We could mask off the remaining bits before testing the active bits, but
194  * currently those bits must be zeroes, so masking would just add cycles.
195  */
196 #define NUMERIC_EXT_SIGN_MASK 0xF000 /* high bits plus NaN/Inf flag bits */
197 #define NUMERIC_NAN 0xC000
198 #define NUMERIC_PINF 0xD000
199 #define NUMERIC_NINF 0xF000
200 #define NUMERIC_INF_SIGN_MASK 0x2000
201 
202 #define NUMERIC_EXT_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_EXT_SIGN_MASK)
203 #define NUMERIC_IS_NAN(n) ((n)->choice.n_header == NUMERIC_NAN)
204 #define NUMERIC_IS_PINF(n) ((n)->choice.n_header == NUMERIC_PINF)
205 #define NUMERIC_IS_NINF(n) ((n)->choice.n_header == NUMERIC_NINF)
206 #define NUMERIC_IS_INF(n) \
207  (((n)->choice.n_header & ~NUMERIC_INF_SIGN_MASK) == NUMERIC_PINF)
208 
209 /*
210  * Short format definitions.
211  */
212 
213 #define NUMERIC_SHORT_SIGN_MASK 0x2000
214 #define NUMERIC_SHORT_DSCALE_MASK 0x1F80
215 #define NUMERIC_SHORT_DSCALE_SHIFT 7
216 #define NUMERIC_SHORT_DSCALE_MAX \
217  (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)
218 #define NUMERIC_SHORT_WEIGHT_SIGN_MASK 0x0040
219 #define NUMERIC_SHORT_WEIGHT_MASK 0x003F
220 #define NUMERIC_SHORT_WEIGHT_MAX NUMERIC_SHORT_WEIGHT_MASK
221 #define NUMERIC_SHORT_WEIGHT_MIN (-(NUMERIC_SHORT_WEIGHT_MASK+1))
222 
223 /*
224  * Extract sign, display scale, weight. These macros extract field values
225  * suitable for the NumericVar format from the Numeric (on-disk) format.
226  *
227  * Note that we don't trouble to ensure that dscale and weight read as zero
228  * for an infinity; however, that doesn't matter since we never convert
229  * "special" numerics to NumericVar form. Only the constants defined below
230  * (const_nan, etc) ever represent a non-finite value as a NumericVar.
231  */
232 
233 #define NUMERIC_DSCALE_MASK 0x3FFF
234 #define NUMERIC_DSCALE_MAX NUMERIC_DSCALE_MASK
235 
236 #define NUMERIC_SIGN(n) \
237  (NUMERIC_IS_SHORT(n) ? \
238  (((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
239  NUMERIC_NEG : NUMERIC_POS) : \
240  (NUMERIC_IS_SPECIAL(n) ? \
241  NUMERIC_EXT_FLAGBITS(n) : NUMERIC_FLAGBITS(n)))
242 #define NUMERIC_DSCALE(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
243  ((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
244  >> NUMERIC_SHORT_DSCALE_SHIFT \
245  : ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
246 #define NUMERIC_WEIGHT(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
247  (((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
248  ~NUMERIC_SHORT_WEIGHT_MASK : 0) \
249  | ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
250  : ((n)->choice.n_long.n_weight))
251 
252 /*
253  * Maximum weight of a stored Numeric value (based on the use of int16 for the
254  * weight in NumericLong). Note that intermediate values held in NumericVar
255  * and NumericSumAccum variables may have much larger weights.
256  */
257 #define NUMERIC_WEIGHT_MAX PG_INT16_MAX
258 
259 /* ----------
260  * NumericVar is the format we use for arithmetic. The digit-array part
261  * is the same as the NumericData storage format, but the header is more
262  * complex.
263  *
264  * The value represented by a NumericVar is determined by the sign, weight,
265  * ndigits, and digits[] array. If it is a "special" value (NaN or Inf)
266  * then only the sign field matters; ndigits should be zero, and the weight
267  * and dscale fields are ignored.
268  *
269  * Note: the first digit of a NumericVar's value is assumed to be multiplied
270  * by NBASE ** weight. Another way to say it is that there are weight+1
271  * digits before the decimal point. It is possible to have weight < 0.
272  *
273  * buf points at the physical start of the palloc'd digit buffer for the
274  * NumericVar. digits points at the first digit in actual use (the one
275  * with the specified weight). We normally leave an unused digit or two
276  * (preset to zeroes) between buf and digits, so that there is room to store
277  * a carry out of the top digit without reallocating space. We just need to
278  * decrement digits (and increment weight) to make room for the carry digit.
279  * (There is no such extra space in a numeric value stored in the database,
280  * only in a NumericVar in memory.)
281  *
282  * If buf is NULL then the digit buffer isn't actually palloc'd and should
283  * not be freed --- see the constants below for an example.
284  *
285  * dscale, or display scale, is the nominal precision expressed as number
286  * of digits after the decimal point (it must always be >= 0 at present).
287  * dscale may be more than the number of physically stored fractional digits,
288  * implying that we have suppressed storage of significant trailing zeroes.
289  * It should never be less than the number of stored digits, since that would
290  * imply hiding digits that are present. NOTE that dscale is always expressed
291  * in *decimal* digits, and so it may correspond to a fractional number of
292  * base-NBASE digits --- divide by DEC_DIGITS to convert to NBASE digits.
293  *
294  * rscale, or result scale, is the target precision for a computation.
295  * Like dscale it is expressed as number of *decimal* digits after the decimal
296  * point, and is always >= 0 at present.
297  * Note that rscale is not stored in variables --- it's figured on-the-fly
298  * from the dscales of the inputs.
299  *
300  * While we consistently use "weight" to refer to the base-NBASE weight of
301  * a numeric value, it is convenient in some scale-related calculations to
302  * make use of the base-10 weight (ie, the approximate log10 of the value).
303  * To avoid confusion, such a decimal-units weight is called a "dweight".
304  *
305  * NB: All the variable-level functions are written in a style that makes it
306  * possible to give one and the same variable as argument and destination.
307  * This is feasible because the digit buffer is separate from the variable.
308  * ----------
309  */
310 typedef struct NumericVar
311 {
312  int ndigits; /* # of digits in digits[] - can be 0! */
313  int weight; /* weight of first digit */
314  int sign; /* NUMERIC_POS, _NEG, _NAN, _PINF, or _NINF */
315  int dscale; /* display scale */
316  NumericDigit *buf; /* start of palloc'd space for digits[] */
317  NumericDigit *digits; /* base-NBASE digits */
319 
320 
321 /* ----------
322  * Data for generate_series
323  * ----------
324  */
325 typedef struct
326 {
331 
332 
333 /* ----------
334  * Sort support.
335  * ----------
336  */
337 typedef struct
338 {
339  void *buf; /* buffer for short varlenas */
340  int64 input_count; /* number of non-null values seen */
341  bool estimating; /* true if estimating cardinality */
342 
343  hyperLogLogState abbr_card; /* cardinality estimator */
345 
346 
347 /* ----------
348  * Fast sum accumulator.
349  *
350  * NumericSumAccum is used to implement SUM(), and other standard aggregates
351  * that track the sum of input values. It uses 32-bit integers to store the
352  * digits, instead of the normal 16-bit integers (with NBASE=10000). This
353  * way, we can safely accumulate up to NBASE - 1 values without propagating
354  * carry, before risking overflow of any of the digits. 'num_uncarried'
355  * tracks how many values have been accumulated without propagating carry.
356  *
357  * Positive and negative values are accumulated separately, in 'pos_digits'
358  * and 'neg_digits'. This is simpler and faster than deciding whether to add
359  * or subtract from the current value, for each new value (see sub_var() for
360  * the logic we avoid by doing this). Both buffers are of same size, and
361  * have the same weight and scale. In accum_sum_final(), the positive and
362  * negative sums are added together to produce the final result.
363  *
364  * When a new value has a larger ndigits or weight than the accumulator
365  * currently does, the accumulator is enlarged to accommodate the new value.
366  * We normally have one zero digit reserved for carry propagation, and that
367  * is indicated by the 'have_carry_space' flag. When accum_sum_carry() uses
368  * up the reserved digit, it clears the 'have_carry_space' flag. The next
369  * call to accum_sum_add() will enlarge the buffer, to make room for the
370  * extra digit, and set the flag again.
371  *
372  * To initialize a new accumulator, simply reset all fields to zeros.
373  *
374  * The accumulator does not handle NaNs.
375  * ----------
376  */
377 typedef struct NumericSumAccum
378 {
379  int ndigits;
380  int weight;
381  int dscale;
387 
388 
389 /*
390  * We define our own macros for packing and unpacking abbreviated-key
391  * representations for numeric values in order to avoid depending on
392  * USE_FLOAT8_BYVAL. The type of abbreviation we use is based only on
393  * the size of a datum, not the argument-passing convention for float8.
394  *
395  * The range of abbreviations for finite values is from +PG_INT64/32_MAX
396  * to -PG_INT64/32_MAX. NaN has the abbreviation PG_INT64/32_MIN, and we
397  * define the sort ordering to make that work out properly (see further
398  * comments below). PINF and NINF share the abbreviations of the largest
399  * and smallest finite abbreviation classes.
400  */
401 #define NUMERIC_ABBREV_BITS (SIZEOF_DATUM * BITS_PER_BYTE)
402 #if SIZEOF_DATUM == 8
403 #define NumericAbbrevGetDatum(X) ((Datum) (X))
404 #define DatumGetNumericAbbrev(X) ((int64) (X))
405 #define NUMERIC_ABBREV_NAN NumericAbbrevGetDatum(PG_INT64_MIN)
406 #define NUMERIC_ABBREV_PINF NumericAbbrevGetDatum(-PG_INT64_MAX)
407 #define NUMERIC_ABBREV_NINF NumericAbbrevGetDatum(PG_INT64_MAX)
408 #else
409 #define NumericAbbrevGetDatum(X) ((Datum) (X))
410 #define DatumGetNumericAbbrev(X) ((int32) (X))
411 #define NUMERIC_ABBREV_NAN NumericAbbrevGetDatum(PG_INT32_MIN)
412 #define NUMERIC_ABBREV_PINF NumericAbbrevGetDatum(-PG_INT32_MAX)
413 #define NUMERIC_ABBREV_NINF NumericAbbrevGetDatum(PG_INT32_MAX)
414 #endif
415 
416 
417 /* ----------
418  * Some preinitialized constants
419  * ----------
420  */
421 static const NumericDigit const_zero_data[1] = {0};
422 static const NumericVar const_zero =
423 {0, 0, NUMERIC_POS, 0, NULL, (NumericDigit *) const_zero_data};
424 
425 static const NumericDigit const_one_data[1] = {1};
426 static const NumericVar const_one =
427 {1, 0, NUMERIC_POS, 0, NULL, (NumericDigit *) const_one_data};
428 
430 {1, 0, NUMERIC_NEG, 0, NULL, (NumericDigit *) const_one_data};
431 
432 static const NumericDigit const_two_data[1] = {2};
433 static const NumericVar const_two =
434 {1, 0, NUMERIC_POS, 0, NULL, (NumericDigit *) const_two_data};
435 
436 #if DEC_DIGITS == 4
437 static const NumericDigit const_zero_point_nine_data[1] = {9000};
438 #elif DEC_DIGITS == 2
439 static const NumericDigit const_zero_point_nine_data[1] = {90};
440 #elif DEC_DIGITS == 1
441 static const NumericDigit const_zero_point_nine_data[1] = {9};
442 #endif
445 
446 #if DEC_DIGITS == 4
447 static const NumericDigit const_one_point_one_data[2] = {1, 1000};
448 #elif DEC_DIGITS == 2
449 static const NumericDigit const_one_point_one_data[2] = {1, 10};
450 #elif DEC_DIGITS == 1
451 static const NumericDigit const_one_point_one_data[2] = {1, 1};
452 #endif
455 
456 static const NumericVar const_nan =
457 {0, 0, NUMERIC_NAN, 0, NULL, NULL};
458 
459 static const NumericVar const_pinf =
460 {0, 0, NUMERIC_PINF, 0, NULL, NULL};
461 
462 static const NumericVar const_ninf =
463 {0, 0, NUMERIC_NINF, 0, NULL, NULL};
464 
465 #if DEC_DIGITS == 4
466 static const int round_powers[4] = {0, 1000, 100, 10};
467 #endif
468 
469 
470 /* ----------
471  * Local functions
472  * ----------
473  */
474 
475 #ifdef NUMERIC_DEBUG
476 static void dump_numeric(const char *str, Numeric num);
477 static void dump_var(const char *str, NumericVar *var);
478 #else
479 #define dump_numeric(s,n)
480 #define dump_var(s,v)
481 #endif
482 
483 #define digitbuf_alloc(ndigits) \
484  ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
485 #define digitbuf_free(buf) \
486  do { \
487  if ((buf) != NULL) \
488  pfree(buf); \
489  } while (0)
490 
491 #define init_var(v) memset(v, 0, sizeof(NumericVar))
492 
493 #define NUMERIC_DIGITS(num) (NUMERIC_HEADER_IS_SHORT(num) ? \
494  (num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
495 #define NUMERIC_NDIGITS(num) \
496  ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
497 #define NUMERIC_CAN_BE_SHORT(scale,weight) \
498  ((scale) <= NUMERIC_SHORT_DSCALE_MAX && \
499  (weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
500  (weight) >= NUMERIC_SHORT_WEIGHT_MIN)
501 
502 static void alloc_var(NumericVar *var, int ndigits);
503 static void free_var(NumericVar *var);
504 static void zero_var(NumericVar *var);
505 
506 static bool set_var_from_str(const char *str, const char *cp,
507  NumericVar *dest, const char **endptr,
508  Node *escontext);
509 static bool set_var_from_non_decimal_integer_str(const char *str,
510  const char *cp, int sign,
511  int base, NumericVar *dest,
512  const char **endptr,
513  Node *escontext);
514 static void set_var_from_num(Numeric num, NumericVar *dest);
515 static void init_var_from_num(Numeric num, NumericVar *dest);
516 static void set_var_from_var(const NumericVar *value, NumericVar *dest);
517 static char *get_str_from_var(const NumericVar *var);
518 static char *get_str_from_var_sci(const NumericVar *var, int rscale);
519 
520 static void numericvar_serialize(StringInfo buf, const NumericVar *var);
522 
523 static Numeric duplicate_numeric(Numeric num);
524 static Numeric make_result(const NumericVar *var);
525 static Numeric make_result_opt_error(const NumericVar *var, bool *have_error);
526 
527 static bool apply_typmod(NumericVar *var, int32 typmod, Node *escontext);
528 static bool apply_typmod_special(Numeric num, int32 typmod, Node *escontext);
529 
530 static bool numericvar_to_int32(const NumericVar *var, int32 *result);
531 static bool numericvar_to_int64(const NumericVar *var, int64 *result);
532 static void int64_to_numericvar(int64 val, NumericVar *var);
533 static bool numericvar_to_uint64(const NumericVar *var, uint64 *result);
534 #ifdef HAVE_INT128
535 static bool numericvar_to_int128(const NumericVar *var, int128 *result);
536 static void int128_to_numericvar(int128 val, NumericVar *var);
537 #endif
538 static double numericvar_to_double_no_overflow(const NumericVar *var);
539 
540 static Datum numeric_abbrev_convert(Datum original_datum, SortSupport ssup);
541 static bool numeric_abbrev_abort(int memtupcount, SortSupport ssup);
542 static int numeric_fast_cmp(Datum x, Datum y, SortSupport ssup);
543 static int numeric_cmp_abbrev(Datum x, Datum y, SortSupport ssup);
544 
546  NumericSortSupport *nss);
547 
548 static int cmp_numerics(Numeric num1, Numeric num2);
549 static int cmp_var(const NumericVar *var1, const NumericVar *var2);
550 static int cmp_var_common(const NumericDigit *var1digits, int var1ndigits,
551  int var1weight, int var1sign,
552  const NumericDigit *var2digits, int var2ndigits,
553  int var2weight, int var2sign);
554 static void add_var(const NumericVar *var1, const NumericVar *var2,
555  NumericVar *result);
556 static void sub_var(const NumericVar *var1, const NumericVar *var2,
557  NumericVar *result);
558 static void mul_var(const NumericVar *var1, const NumericVar *var2,
559  NumericVar *result,
560  int rscale);
561 static void mul_var_short(const NumericVar *var1, const NumericVar *var2,
562  NumericVar *result);
563 static void div_var(const NumericVar *var1, const NumericVar *var2,
564  NumericVar *result,
565  int rscale, bool round);
566 static void div_var_fast(const NumericVar *var1, const NumericVar *var2,
567  NumericVar *result, int rscale, bool round);
568 static void div_var_int(const NumericVar *var, int ival, int ival_weight,
569  NumericVar *result, int rscale, bool round);
570 #ifdef HAVE_INT128
571 static void div_var_int64(const NumericVar *var, int64 ival, int ival_weight,
572  NumericVar *result, int rscale, bool round);
573 #endif
574 static int select_div_scale(const NumericVar *var1, const NumericVar *var2);
575 static void mod_var(const NumericVar *var1, const NumericVar *var2,
576  NumericVar *result);
577 static void div_mod_var(const NumericVar *var1, const NumericVar *var2,
578  NumericVar *quot, NumericVar *rem);
579 static void ceil_var(const NumericVar *var, NumericVar *result);
580 static void floor_var(const NumericVar *var, NumericVar *result);
581 
582 static void gcd_var(const NumericVar *var1, const NumericVar *var2,
583  NumericVar *result);
584 static void sqrt_var(const NumericVar *arg, NumericVar *result, int rscale);
585 static void exp_var(const NumericVar *arg, NumericVar *result, int rscale);
586 static int estimate_ln_dweight(const NumericVar *var);
587 static void ln_var(const NumericVar *arg, NumericVar *result, int rscale);
588 static void log_var(const NumericVar *base, const NumericVar *num,
589  NumericVar *result);
590 static void power_var(const NumericVar *base, const NumericVar *exp,
591  NumericVar *result);
592 static void power_var_int(const NumericVar *base, int exp, int exp_dscale,
593  NumericVar *result);
594 static void power_ten_int(int exp, NumericVar *result);
595 static void random_var(pg_prng_state *state, const NumericVar *rmin,
596  const NumericVar *rmax, NumericVar *result);
597 
598 static int cmp_abs(const NumericVar *var1, const NumericVar *var2);
599 static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits,
600  int var1weight,
601  const NumericDigit *var2digits, int var2ndigits,
602  int var2weight);
603 static void add_abs(const NumericVar *var1, const NumericVar *var2,
604  NumericVar *result);
605 static void sub_abs(const NumericVar *var1, const NumericVar *var2,
606  NumericVar *result);
607 static void round_var(NumericVar *var, int rscale);
608 static void trunc_var(NumericVar *var, int rscale);
609 static void strip_var(NumericVar *var);
610 static void compute_bucket(Numeric operand, Numeric bound1, Numeric bound2,
611  const NumericVar *count_var,
612  NumericVar *result_var);
613 
614 static void accum_sum_add(NumericSumAccum *accum, const NumericVar *val);
615 static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val);
616 static void accum_sum_carry(NumericSumAccum *accum);
617 static void accum_sum_reset(NumericSumAccum *accum);
618 static void accum_sum_final(NumericSumAccum *accum, NumericVar *result);
619 static void accum_sum_copy(NumericSumAccum *dst, NumericSumAccum *src);
620 static void accum_sum_combine(NumericSumAccum *accum, NumericSumAccum *accum2);
621 
622 
623 /* ----------------------------------------------------------------------
624  *
625  * Input-, output- and rounding-functions
626  *
627  * ----------------------------------------------------------------------
628  */
629 
630 
631 /*
632  * numeric_in() -
633  *
634  * Input function for numeric data type
635  */
636 Datum
638 {
639  char *str = PG_GETARG_CSTRING(0);
640 #ifdef NOT_USED
641  Oid typelem = PG_GETARG_OID(1);
642 #endif
643  int32 typmod = PG_GETARG_INT32(2);
644  Node *escontext = fcinfo->context;
645  Numeric res;
646  const char *cp;
647  const char *numstart;
648  int sign;
649 
650  /* Skip leading spaces */
651  cp = str;
652  while (*cp)
653  {
654  if (!isspace((unsigned char) *cp))
655  break;
656  cp++;
657  }
658 
659  /*
660  * Process the number's sign. This duplicates logic in set_var_from_str(),
661  * but it's worth doing here, since it simplifies the handling of
662  * infinities and non-decimal integers.
663  */
664  numstart = cp;
665  sign = NUMERIC_POS;
666 
667  if (*cp == '+')
668  cp++;
669  else if (*cp == '-')
670  {
671  sign = NUMERIC_NEG;
672  cp++;
673  }
674 
675  /*
676  * Check for NaN and infinities. We recognize the same strings allowed by
677  * float8in().
678  *
679  * Since all other legal inputs have a digit or a decimal point after the
680  * sign, we need only check for NaN/infinity if that's not the case.
681  */
682  if (!isdigit((unsigned char) *cp) && *cp != '.')
683  {
684  /*
685  * The number must be NaN or infinity; anything else can only be a
686  * syntax error. Note that NaN mustn't have a sign.
687  */
688  if (pg_strncasecmp(numstart, "NaN", 3) == 0)
689  {
691  cp = numstart + 3;
692  }
693  else if (pg_strncasecmp(cp, "Infinity", 8) == 0)
694  {
696  cp += 8;
697  }
698  else if (pg_strncasecmp(cp, "inf", 3) == 0)
699  {
701  cp += 3;
702  }
703  else
704  goto invalid_syntax;
705 
706  /*
707  * Check for trailing junk; there should be nothing left but spaces.
708  *
709  * We intentionally do this check before applying the typmod because
710  * we would like to throw any trailing-junk syntax error before any
711  * semantic error resulting from apply_typmod_special().
712  */
713  while (*cp)
714  {
715  if (!isspace((unsigned char) *cp))
716  goto invalid_syntax;
717  cp++;
718  }
719 
720  if (!apply_typmod_special(res, typmod, escontext))
721  PG_RETURN_NULL();
722  }
723  else
724  {
725  /*
726  * We have a normal numeric value, which may be a non-decimal integer
727  * or a regular decimal number.
728  */
730  int base;
731  bool have_error;
732 
733  init_var(&value);
734 
735  /*
736  * Determine the number's base by looking for a non-decimal prefix
737  * indicator ("0x", "0o", or "0b").
738  */
739  if (cp[0] == '0')
740  {
741  switch (cp[1])
742  {
743  case 'x':
744  case 'X':
745  base = 16;
746  break;
747  case 'o':
748  case 'O':
749  base = 8;
750  break;
751  case 'b':
752  case 'B':
753  base = 2;
754  break;
755  default:
756  base = 10;
757  }
758  }
759  else
760  base = 10;
761 
762  /* Parse the rest of the number and apply the sign */
763  if (base == 10)
764  {
765  if (!set_var_from_str(str, cp, &value, &cp, escontext))
766  PG_RETURN_NULL();
767  value.sign = sign;
768  }
769  else
770  {
771  if (!set_var_from_non_decimal_integer_str(str, cp + 2, sign, base,
772  &value, &cp, escontext))
773  PG_RETURN_NULL();
774  }
775 
776  /*
777  * Should be nothing left but spaces. As above, throw any typmod error
778  * after finishing syntax check.
779  */
780  while (*cp)
781  {
782  if (!isspace((unsigned char) *cp))
783  goto invalid_syntax;
784  cp++;
785  }
786 
787  if (!apply_typmod(&value, typmod, escontext))
788  PG_RETURN_NULL();
789 
790  res = make_result_opt_error(&value, &have_error);
791 
792  if (have_error)
793  ereturn(escontext, (Datum) 0,
794  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
795  errmsg("value overflows numeric format")));
796 
797  free_var(&value);
798  }
799 
801 
802 invalid_syntax:
803  ereturn(escontext, (Datum) 0,
804  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
805  errmsg("invalid input syntax for type %s: \"%s\"",
806  "numeric", str)));
807 }
808 
809 
810 /*
811  * numeric_out() -
812  *
813  * Output function for numeric data type
814  */
815 Datum
817 {
818  Numeric num = PG_GETARG_NUMERIC(0);
819  NumericVar x;
820  char *str;
821 
822  /*
823  * Handle NaN and infinities
824  */
825  if (NUMERIC_IS_SPECIAL(num))
826  {
827  if (NUMERIC_IS_PINF(num))
828  PG_RETURN_CSTRING(pstrdup("Infinity"));
829  else if (NUMERIC_IS_NINF(num))
830  PG_RETURN_CSTRING(pstrdup("-Infinity"));
831  else
832  PG_RETURN_CSTRING(pstrdup("NaN"));
833  }
834 
835  /*
836  * Get the number in the variable format.
837  */
838  init_var_from_num(num, &x);
839 
840  str = get_str_from_var(&x);
841 
843 }
844 
845 /*
846  * numeric_is_nan() -
847  *
848  * Is Numeric value a NaN?
849  */
850 bool
852 {
853  return NUMERIC_IS_NAN(num);
854 }
855 
856 /*
857  * numeric_is_inf() -
858  *
859  * Is Numeric value an infinity?
860  */
861 bool
863 {
864  return NUMERIC_IS_INF(num);
865 }
866 
867 /*
868  * numeric_is_integral() -
869  *
870  * Is Numeric value integral?
871  */
872 static bool
874 {
875  NumericVar arg;
876 
877  /* Reject NaN, but infinities are considered integral */
878  if (NUMERIC_IS_SPECIAL(num))
879  {
880  if (NUMERIC_IS_NAN(num))
881  return false;
882  return true;
883  }
884 
885  /* Integral if there are no digits to the right of the decimal point */
886  init_var_from_num(num, &arg);
887 
888  return (arg.ndigits == 0 || arg.ndigits <= arg.weight + 1);
889 }
890 
891 /*
892  * make_numeric_typmod() -
893  *
894  * Pack numeric precision and scale values into a typmod. The upper 16 bits
895  * are used for the precision (though actually not all these bits are needed,
896  * since the maximum allowed precision is 1000). The lower 16 bits are for
897  * the scale, but since the scale is constrained to the range [-1000, 1000],
898  * we use just the lower 11 of those 16 bits, and leave the remaining 5 bits
899  * unset, for possible future use.
900  *
901  * For purely historical reasons VARHDRSZ is then added to the result, thus
902  * the unused space in the upper 16 bits is not all as freely available as it
903  * might seem. (We can't let the result overflow to a negative int32, as
904  * other parts of the system would interpret that as not-a-valid-typmod.)
905  */
906 static inline int32
907 make_numeric_typmod(int precision, int scale)
908 {
909  return ((precision << 16) | (scale & 0x7ff)) + VARHDRSZ;
910 }
911 
912 /*
913  * Because of the offset, valid numeric typmods are at least VARHDRSZ
914  */
915 static inline bool
917 {
918  return typmod >= (int32) VARHDRSZ;
919 }
920 
921 /*
922  * numeric_typmod_precision() -
923  *
924  * Extract the precision from a numeric typmod --- see make_numeric_typmod().
925  */
926 static inline int
928 {
929  return ((typmod - VARHDRSZ) >> 16) & 0xffff;
930 }
931 
932 /*
933  * numeric_typmod_scale() -
934  *
935  * Extract the scale from a numeric typmod --- see make_numeric_typmod().
936  *
937  * Note that the scale may be negative, so we must do sign extension when
938  * unpacking it. We do this using the bit hack (x^1024)-1024, which sign
939  * extends an 11-bit two's complement number x.
940  */
941 static inline int
943 {
944  return (((typmod - VARHDRSZ) & 0x7ff) ^ 1024) - 1024;
945 }
946 
947 /*
948  * numeric_maximum_size() -
949  *
950  * Maximum size of a numeric with given typmod, or -1 if unlimited/unknown.
951  */
952 int32
954 {
955  int precision;
956  int numeric_digits;
957 
958  if (!is_valid_numeric_typmod(typmod))
959  return -1;
960 
961  /* precision (ie, max # of digits) is in upper bits of typmod */
962  precision = numeric_typmod_precision(typmod);
963 
964  /*
965  * This formula computes the maximum number of NumericDigits we could need
966  * in order to store the specified number of decimal digits. Because the
967  * weight is stored as a number of NumericDigits rather than a number of
968  * decimal digits, it's possible that the first NumericDigit will contain
969  * only a single decimal digit. Thus, the first two decimal digits can
970  * require two NumericDigits to store, but it isn't until we reach
971  * DEC_DIGITS + 2 decimal digits that we potentially need a third
972  * NumericDigit.
973  */
974  numeric_digits = (precision + 2 * (DEC_DIGITS - 1)) / DEC_DIGITS;
975 
976  /*
977  * In most cases, the size of a numeric will be smaller than the value
978  * computed below, because the varlena header will typically get toasted
979  * down to a single byte before being stored on disk, and it may also be
980  * possible to use a short numeric header. But our job here is to compute
981  * the worst case.
982  */
983  return NUMERIC_HDRSZ + (numeric_digits * sizeof(NumericDigit));
984 }
985 
986 /*
987  * numeric_out_sci() -
988  *
989  * Output function for numeric data type in scientific notation.
990  */
991 char *
993 {
994  NumericVar x;
995  char *str;
996 
997  /*
998  * Handle NaN and infinities
999  */
1000  if (NUMERIC_IS_SPECIAL(num))
1001  {
1002  if (NUMERIC_IS_PINF(num))
1003  return pstrdup("Infinity");
1004  else if (NUMERIC_IS_NINF(num))
1005  return pstrdup("-Infinity");
1006  else
1007  return pstrdup("NaN");
1008  }
1009 
1010  init_var_from_num(num, &x);
1011 
1013 
1014  return str;
1015 }
1016 
1017 /*
1018  * numeric_normalize() -
1019  *
1020  * Output function for numeric data type, suppressing insignificant trailing
1021  * zeroes and then any trailing decimal point. The intent of this is to
1022  * produce strings that are equal if and only if the input numeric values
1023  * compare equal.
1024  */
1025 char *
1027 {
1028  NumericVar x;
1029  char *str;
1030  int last;
1031 
1032  /*
1033  * Handle NaN and infinities
1034  */
1035  if (NUMERIC_IS_SPECIAL(num))
1036  {
1037  if (NUMERIC_IS_PINF(num))
1038  return pstrdup("Infinity");
1039  else if (NUMERIC_IS_NINF(num))
1040  return pstrdup("-Infinity");
1041  else
1042  return pstrdup("NaN");
1043  }
1044 
1045  init_var_from_num(num, &x);
1046 
1047  str = get_str_from_var(&x);
1048 
1049  /* If there's no decimal point, there's certainly nothing to remove. */
1050  if (strchr(str, '.') != NULL)
1051  {
1052  /*
1053  * Back up over trailing fractional zeroes. Since there is a decimal
1054  * point, this loop will terminate safely.
1055  */
1056  last = strlen(str) - 1;
1057  while (str[last] == '0')
1058  last--;
1059 
1060  /* We want to get rid of the decimal point too, if it's now last. */
1061  if (str[last] == '.')
1062  last--;
1063 
1064  /* Delete whatever we backed up over. */
1065  str[last + 1] = '\0';
1066  }
1067 
1068  return str;
1069 }
1070 
1071 /*
1072  * numeric_recv - converts external binary format to numeric
1073  *
1074  * External format is a sequence of int16's:
1075  * ndigits, weight, sign, dscale, NumericDigits.
1076  */
1077 Datum
1079 {
1081 
1082 #ifdef NOT_USED
1083  Oid typelem = PG_GETARG_OID(1);
1084 #endif
1085  int32 typmod = PG_GETARG_INT32(2);
1086  NumericVar value;
1087  Numeric res;
1088  int len,
1089  i;
1090 
1091  init_var(&value);
1092 
1093  len = (uint16) pq_getmsgint(buf, sizeof(uint16));
1094 
1095  alloc_var(&value, len);
1096 
1097  value.weight = (int16) pq_getmsgint(buf, sizeof(int16));
1098  /* we allow any int16 for weight --- OK? */
1099 
1100  value.sign = (uint16) pq_getmsgint(buf, sizeof(uint16));
1101  if (!(value.sign == NUMERIC_POS ||
1102  value.sign == NUMERIC_NEG ||
1103  value.sign == NUMERIC_NAN ||
1104  value.sign == NUMERIC_PINF ||
1105  value.sign == NUMERIC_NINF))
1106  ereport(ERROR,
1107  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1108  errmsg("invalid sign in external \"numeric\" value")));
1109 
1110  value.dscale = (uint16) pq_getmsgint(buf, sizeof(uint16));
1111  if ((value.dscale & NUMERIC_DSCALE_MASK) != value.dscale)
1112  ereport(ERROR,
1113  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1114  errmsg("invalid scale in external \"numeric\" value")));
1115 
1116  for (i = 0; i < len; i++)
1117  {
1118  NumericDigit d = pq_getmsgint(buf, sizeof(NumericDigit));
1119 
1120  if (d < 0 || d >= NBASE)
1121  ereport(ERROR,
1122  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1123  errmsg("invalid digit in external \"numeric\" value")));
1124  value.digits[i] = d;
1125  }
1126 
1127  /*
1128  * If the given dscale would hide any digits, truncate those digits away.
1129  * We could alternatively throw an error, but that would take a bunch of
1130  * extra code (about as much as trunc_var involves), and it might cause
1131  * client compatibility issues. Be careful not to apply trunc_var to
1132  * special values, as it could do the wrong thing; we don't need it
1133  * anyway, since make_result will ignore all but the sign field.
1134  *
1135  * After doing that, be sure to check the typmod restriction.
1136  */
1137  if (value.sign == NUMERIC_POS ||
1138  value.sign == NUMERIC_NEG)
1139  {
1140  trunc_var(&value, value.dscale);
1141 
1142  (void) apply_typmod(&value, typmod, NULL);
1143 
1144  res = make_result(&value);
1145  }
1146  else
1147  {
1148  /* apply_typmod_special wants us to make the Numeric first */
1149  res = make_result(&value);
1150 
1151  (void) apply_typmod_special(res, typmod, NULL);
1152  }
1153 
1154  free_var(&value);
1155 
1157 }
1158 
1159 /*
1160  * numeric_send - converts numeric to binary format
1161  */
1162 Datum
1164 {
1165  Numeric num = PG_GETARG_NUMERIC(0);
1166  NumericVar x;
1168  int i;
1169 
1170  init_var_from_num(num, &x);
1171 
1172  pq_begintypsend(&buf);
1173 
1174  pq_sendint16(&buf, x.ndigits);
1175  pq_sendint16(&buf, x.weight);
1176  pq_sendint16(&buf, x.sign);
1177  pq_sendint16(&buf, x.dscale);
1178  for (i = 0; i < x.ndigits; i++)
1179  pq_sendint16(&buf, x.digits[i]);
1180 
1182 }
1183 
1184 
1185 /*
1186  * numeric_support()
1187  *
1188  * Planner support function for the numeric() length coercion function.
1189  *
1190  * Flatten calls that solely represent increases in allowable precision.
1191  * Scale changes mutate every datum, so they are unoptimizable. Some values,
1192  * e.g. 1E-1001, can only fit into an unconstrained numeric, so a change from
1193  * an unconstrained numeric to any constrained numeric is also unoptimizable.
1194  */
1195 Datum
1197 {
1198  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1199  Node *ret = NULL;
1200 
1201  if (IsA(rawreq, SupportRequestSimplify))
1202  {
1204  FuncExpr *expr = req->fcall;
1205  Node *typmod;
1206 
1207  Assert(list_length(expr->args) >= 2);
1208 
1209  typmod = (Node *) lsecond(expr->args);
1210 
1211  if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
1212  {
1213  Node *source = (Node *) linitial(expr->args);
1214  int32 old_typmod = exprTypmod(source);
1215  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1216  int32 old_scale = numeric_typmod_scale(old_typmod);
1217  int32 new_scale = numeric_typmod_scale(new_typmod);
1218  int32 old_precision = numeric_typmod_precision(old_typmod);
1219  int32 new_precision = numeric_typmod_precision(new_typmod);
1220 
1221  /*
1222  * If new_typmod is invalid, the destination is unconstrained;
1223  * that's always OK. If old_typmod is valid, the source is
1224  * constrained, and we're OK if the scale is unchanged and the
1225  * precision is not decreasing. See further notes in function
1226  * header comment.
1227  */
1228  if (!is_valid_numeric_typmod(new_typmod) ||
1229  (is_valid_numeric_typmod(old_typmod) &&
1230  new_scale == old_scale && new_precision >= old_precision))
1231  ret = relabel_to_typmod(source, new_typmod);
1232  }
1233  }
1234 
1235  PG_RETURN_POINTER(ret);
1236 }
1237 
1238 /*
1239  * numeric() -
1240  *
1241  * This is a special function called by the Postgres database system
1242  * before a value is stored in a tuple's attribute. The precision and
1243  * scale of the attribute have to be applied on the value.
1244  */
1245 Datum
1247 {
1248  Numeric num = PG_GETARG_NUMERIC(0);
1249  int32 typmod = PG_GETARG_INT32(1);
1250  Numeric new;
1251  int precision;
1252  int scale;
1253  int ddigits;
1254  int maxdigits;
1255  int dscale;
1256  NumericVar var;
1257 
1258  /*
1259  * Handle NaN and infinities: if apply_typmod_special doesn't complain,
1260  * just return a copy of the input.
1261  */
1262  if (NUMERIC_IS_SPECIAL(num))
1263  {
1264  (void) apply_typmod_special(num, typmod, NULL);
1266  }
1267 
1268  /*
1269  * If the value isn't a valid type modifier, simply return a copy of the
1270  * input value
1271  */
1272  if (!is_valid_numeric_typmod(typmod))
1274 
1275  /*
1276  * Get the precision and scale out of the typmod value
1277  */
1278  precision = numeric_typmod_precision(typmod);
1279  scale = numeric_typmod_scale(typmod);
1280  maxdigits = precision - scale;
1281 
1282  /* The target display scale is non-negative */
1283  dscale = Max(scale, 0);
1284 
1285  /*
1286  * If the number is certainly in bounds and due to the target scale no
1287  * rounding could be necessary, just make a copy of the input and modify
1288  * its scale fields, unless the larger scale forces us to abandon the
1289  * short representation. (Note we assume the existing dscale is
1290  * honest...)
1291  */
1292  ddigits = (NUMERIC_WEIGHT(num) + 1) * DEC_DIGITS;
1293  if (ddigits <= maxdigits && scale >= NUMERIC_DSCALE(num)
1294  && (NUMERIC_CAN_BE_SHORT(dscale, NUMERIC_WEIGHT(num))
1295  || !NUMERIC_IS_SHORT(num)))
1296  {
1297  new = duplicate_numeric(num);
1298  if (NUMERIC_IS_SHORT(num))
1299  new->choice.n_short.n_header =
1301  | (dscale << NUMERIC_SHORT_DSCALE_SHIFT);
1302  else
1303  new->choice.n_long.n_sign_dscale = NUMERIC_SIGN(new) |
1304  ((uint16) dscale & NUMERIC_DSCALE_MASK);
1305  PG_RETURN_NUMERIC(new);
1306  }
1307 
1308  /*
1309  * We really need to fiddle with things - unpack the number into a
1310  * variable and let apply_typmod() do it.
1311  */
1312  init_var(&var);
1313 
1314  set_var_from_num(num, &var);
1315  (void) apply_typmod(&var, typmod, NULL);
1316  new = make_result(&var);
1317 
1318  free_var(&var);
1319 
1320  PG_RETURN_NUMERIC(new);
1321 }
1322 
1323 Datum
1325 {
1327  int32 *tl;
1328  int n;
1329  int32 typmod;
1330 
1331  tl = ArrayGetIntegerTypmods(ta, &n);
1332 
1333  if (n == 2)
1334  {
1335  if (tl[0] < 1 || tl[0] > NUMERIC_MAX_PRECISION)
1336  ereport(ERROR,
1337  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1338  errmsg("NUMERIC precision %d must be between 1 and %d",
1339  tl[0], NUMERIC_MAX_PRECISION)));
1340  if (tl[1] < NUMERIC_MIN_SCALE || tl[1] > NUMERIC_MAX_SCALE)
1341  ereport(ERROR,
1342  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1343  errmsg("NUMERIC scale %d must be between %d and %d",
1345  typmod = make_numeric_typmod(tl[0], tl[1]);
1346  }
1347  else if (n == 1)
1348  {
1349  if (tl[0] < 1 || tl[0] > NUMERIC_MAX_PRECISION)
1350  ereport(ERROR,
1351  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1352  errmsg("NUMERIC precision %d must be between 1 and %d",
1353  tl[0], NUMERIC_MAX_PRECISION)));
1354  /* scale defaults to zero */
1355  typmod = make_numeric_typmod(tl[0], 0);
1356  }
1357  else
1358  {
1359  ereport(ERROR,
1360  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1361  errmsg("invalid NUMERIC type modifier")));
1362  typmod = 0; /* keep compiler quiet */
1363  }
1364 
1365  PG_RETURN_INT32(typmod);
1366 }
1367 
1368 Datum
1370 {
1371  int32 typmod = PG_GETARG_INT32(0);
1372  char *res = (char *) palloc(64);
1373 
1374  if (is_valid_numeric_typmod(typmod))
1375  snprintf(res, 64, "(%d,%d)",
1376  numeric_typmod_precision(typmod),
1377  numeric_typmod_scale(typmod));
1378  else
1379  *res = '\0';
1380 
1382 }
1383 
1384 
1385 /* ----------------------------------------------------------------------
1386  *
1387  * Sign manipulation, rounding and the like
1388  *
1389  * ----------------------------------------------------------------------
1390  */
1391 
1392 Datum
1394 {
1395  Numeric num = PG_GETARG_NUMERIC(0);
1396  Numeric res;
1397 
1398  /*
1399  * Do it the easy way directly on the packed format
1400  */
1401  res = duplicate_numeric(num);
1402 
1403  if (NUMERIC_IS_SHORT(num))
1404  res->choice.n_short.n_header =
1406  else if (NUMERIC_IS_SPECIAL(num))
1407  {
1408  /* This changes -Inf to Inf, and doesn't affect NaN */
1409  res->choice.n_short.n_header =
1411  }
1412  else
1413  res->choice.n_long.n_sign_dscale = NUMERIC_POS | NUMERIC_DSCALE(num);
1414 
1416 }
1417 
1418 
1419 Datum
1421 {
1422  Numeric num = PG_GETARG_NUMERIC(0);
1423  Numeric res;
1424 
1425  /*
1426  * Do it the easy way directly on the packed format
1427  */
1428  res = duplicate_numeric(num);
1429 
1430  if (NUMERIC_IS_SPECIAL(num))
1431  {
1432  /* Flip the sign, if it's Inf or -Inf */
1433  if (!NUMERIC_IS_NAN(num))
1434  res->choice.n_short.n_header =
1436  }
1437 
1438  /*
1439  * The packed format is known to be totally zero digit trimmed always. So
1440  * once we've eliminated specials, we can identify a zero by the fact that
1441  * there are no digits at all. Do nothing to a zero.
1442  */
1443  else if (NUMERIC_NDIGITS(num) != 0)
1444  {
1445  /* Else, flip the sign */
1446  if (NUMERIC_IS_SHORT(num))
1447  res->choice.n_short.n_header =
1449  else if (NUMERIC_SIGN(num) == NUMERIC_POS)
1450  res->choice.n_long.n_sign_dscale =
1451  NUMERIC_NEG | NUMERIC_DSCALE(num);
1452  else
1453  res->choice.n_long.n_sign_dscale =
1454  NUMERIC_POS | NUMERIC_DSCALE(num);
1455  }
1456 
1458 }
1459 
1460 
1461 Datum
1463 {
1464  Numeric num = PG_GETARG_NUMERIC(0);
1465 
1467 }
1468 
1469 
1470 /*
1471  * numeric_sign_internal() -
1472  *
1473  * Returns -1 if the argument is less than 0, 0 if the argument is equal
1474  * to 0, and 1 if the argument is greater than zero. Caller must have
1475  * taken care of the NaN case, but we can handle infinities here.
1476  */
1477 static int
1479 {
1480  if (NUMERIC_IS_SPECIAL(num))
1481  {
1482  Assert(!NUMERIC_IS_NAN(num));
1483  /* Must be Inf or -Inf */
1484  if (NUMERIC_IS_PINF(num))
1485  return 1;
1486  else
1487  return -1;
1488  }
1489 
1490  /*
1491  * The packed format is known to be totally zero digit trimmed always. So
1492  * once we've eliminated specials, we can identify a zero by the fact that
1493  * there are no digits at all.
1494  */
1495  else if (NUMERIC_NDIGITS(num) == 0)
1496  return 0;
1497  else if (NUMERIC_SIGN(num) == NUMERIC_NEG)
1498  return -1;
1499  else
1500  return 1;
1501 }
1502 
1503 /*
1504  * numeric_sign() -
1505  *
1506  * returns -1 if the argument is less than 0, 0 if the argument is equal
1507  * to 0, and 1 if the argument is greater than zero.
1508  */
1509 Datum
1511 {
1512  Numeric num = PG_GETARG_NUMERIC(0);
1513 
1514  /*
1515  * Handle NaN (infinities can be handled normally)
1516  */
1517  if (NUMERIC_IS_NAN(num))
1519 
1520  switch (numeric_sign_internal(num))
1521  {
1522  case 0:
1524  case 1:
1526  case -1:
1528  }
1529 
1530  Assert(false);
1531  return (Datum) 0;
1532 }
1533 
1534 
1535 /*
1536  * numeric_round() -
1537  *
1538  * Round a value to have 'scale' digits after the decimal point.
1539  * We allow negative 'scale', implying rounding before the decimal
1540  * point --- Oracle interprets rounding that way.
1541  */
1542 Datum
1544 {
1545  Numeric num = PG_GETARG_NUMERIC(0);
1547  Numeric res;
1548  NumericVar arg;
1549 
1550  /*
1551  * Handle NaN and infinities
1552  */
1553  if (NUMERIC_IS_SPECIAL(num))
1555 
1556  /*
1557  * Limit the scale value to avoid possible overflow in calculations.
1558  *
1559  * These limits are based on the maximum number of digits a Numeric value
1560  * can have before and after the decimal point, but we must allow for one
1561  * extra digit before the decimal point, in case the most significant
1562  * digit rounds up; we must check if that causes Numeric overflow.
1563  */
1564  scale = Max(scale, -(NUMERIC_WEIGHT_MAX + 1) * DEC_DIGITS - 1);
1566 
1567  /*
1568  * Unpack the argument and round it at the proper digit position
1569  */
1570  init_var(&arg);
1571  set_var_from_num(num, &arg);
1572 
1573  round_var(&arg, scale);
1574 
1575  /* We don't allow negative output dscale */
1576  if (scale < 0)
1577  arg.dscale = 0;
1578 
1579  /*
1580  * Return the rounded result
1581  */
1582  res = make_result(&arg);
1583 
1584  free_var(&arg);
1586 }
1587 
1588 
1589 /*
1590  * numeric_trunc() -
1591  *
1592  * Truncate a value to have 'scale' digits after the decimal point.
1593  * We allow negative 'scale', implying a truncation before the decimal
1594  * point --- Oracle interprets truncation that way.
1595  */
1596 Datum
1598 {
1599  Numeric num = PG_GETARG_NUMERIC(0);
1601  Numeric res;
1602  NumericVar arg;
1603 
1604  /*
1605  * Handle NaN and infinities
1606  */
1607  if (NUMERIC_IS_SPECIAL(num))
1609 
1610  /*
1611  * Limit the scale value to avoid possible overflow in calculations.
1612  *
1613  * These limits are based on the maximum number of digits a Numeric value
1614  * can have before and after the decimal point.
1615  */
1618 
1619  /*
1620  * Unpack the argument and truncate it at the proper digit position
1621  */
1622  init_var(&arg);
1623  set_var_from_num(num, &arg);
1624 
1625  trunc_var(&arg, scale);
1626 
1627  /* We don't allow negative output dscale */
1628  if (scale < 0)
1629  arg.dscale = 0;
1630 
1631  /*
1632  * Return the truncated result
1633  */
1634  res = make_result(&arg);
1635 
1636  free_var(&arg);
1638 }
1639 
1640 
1641 /*
1642  * numeric_ceil() -
1643  *
1644  * Return the smallest integer greater than or equal to the argument
1645  */
1646 Datum
1648 {
1649  Numeric num = PG_GETARG_NUMERIC(0);
1650  Numeric res;
1651  NumericVar result;
1652 
1653  /*
1654  * Handle NaN and infinities
1655  */
1656  if (NUMERIC_IS_SPECIAL(num))
1658 
1659  init_var_from_num(num, &result);
1660  ceil_var(&result, &result);
1661 
1662  res = make_result(&result);
1663  free_var(&result);
1664 
1666 }
1667 
1668 
1669 /*
1670  * numeric_floor() -
1671  *
1672  * Return the largest integer equal to or less than the argument
1673  */
1674 Datum
1676 {
1677  Numeric num = PG_GETARG_NUMERIC(0);
1678  Numeric res;
1679  NumericVar result;
1680 
1681  /*
1682  * Handle NaN and infinities
1683  */
1684  if (NUMERIC_IS_SPECIAL(num))
1686 
1687  init_var_from_num(num, &result);
1688  floor_var(&result, &result);
1689 
1690  res = make_result(&result);
1691  free_var(&result);
1692 
1694 }
1695 
1696 
1697 /*
1698  * generate_series_numeric() -
1699  *
1700  * Generate series of numeric.
1701  */
1702 Datum
1704 {
1705  return generate_series_step_numeric(fcinfo);
1706 }
1707 
1708 Datum
1710 {
1712  FuncCallContext *funcctx;
1713  MemoryContext oldcontext;
1714 
1715  if (SRF_IS_FIRSTCALL())
1716  {
1717  Numeric start_num = PG_GETARG_NUMERIC(0);
1718  Numeric stop_num = PG_GETARG_NUMERIC(1);
1719  NumericVar steploc = const_one;
1720 
1721  /* Reject NaN and infinities in start and stop values */
1722  if (NUMERIC_IS_SPECIAL(start_num))
1723  {
1724  if (NUMERIC_IS_NAN(start_num))
1725  ereport(ERROR,
1726  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1727  errmsg("start value cannot be NaN")));
1728  else
1729  ereport(ERROR,
1730  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1731  errmsg("start value cannot be infinity")));
1732  }
1733  if (NUMERIC_IS_SPECIAL(stop_num))
1734  {
1735  if (NUMERIC_IS_NAN(stop_num))
1736  ereport(ERROR,
1737  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1738  errmsg("stop value cannot be NaN")));
1739  else
1740  ereport(ERROR,
1741  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1742  errmsg("stop value cannot be infinity")));
1743  }
1744 
1745  /* see if we were given an explicit step size */
1746  if (PG_NARGS() == 3)
1747  {
1748  Numeric step_num = PG_GETARG_NUMERIC(2);
1749 
1750  if (NUMERIC_IS_SPECIAL(step_num))
1751  {
1752  if (NUMERIC_IS_NAN(step_num))
1753  ereport(ERROR,
1754  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1755  errmsg("step size cannot be NaN")));
1756  else
1757  ereport(ERROR,
1758  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1759  errmsg("step size cannot be infinity")));
1760  }
1761 
1762  init_var_from_num(step_num, &steploc);
1763 
1764  if (cmp_var(&steploc, &const_zero) == 0)
1765  ereport(ERROR,
1766  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1767  errmsg("step size cannot equal zero")));
1768  }
1769 
1770  /* create a function context for cross-call persistence */
1771  funcctx = SRF_FIRSTCALL_INIT();
1772 
1773  /*
1774  * Switch to memory context appropriate for multiple function calls.
1775  */
1776  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1777 
1778  /* allocate memory for user context */
1779  fctx = (generate_series_numeric_fctx *)
1781 
1782  /*
1783  * Use fctx to keep state from call to call. Seed current with the
1784  * original start value. We must copy the start_num and stop_num
1785  * values rather than pointing to them, since we may have detoasted
1786  * them in the per-call context.
1787  */
1788  init_var(&fctx->current);
1789  init_var(&fctx->stop);
1790  init_var(&fctx->step);
1791 
1792  set_var_from_num(start_num, &fctx->current);
1793  set_var_from_num(stop_num, &fctx->stop);
1794  set_var_from_var(&steploc, &fctx->step);
1795 
1796  funcctx->user_fctx = fctx;
1797  MemoryContextSwitchTo(oldcontext);
1798  }
1799 
1800  /* stuff done on every call of the function */
1801  funcctx = SRF_PERCALL_SETUP();
1802 
1803  /*
1804  * Get the saved state and use current state as the result of this
1805  * iteration.
1806  */
1807  fctx = funcctx->user_fctx;
1808 
1809  if ((fctx->step.sign == NUMERIC_POS &&
1810  cmp_var(&fctx->current, &fctx->stop) <= 0) ||
1811  (fctx->step.sign == NUMERIC_NEG &&
1812  cmp_var(&fctx->current, &fctx->stop) >= 0))
1813  {
1814  Numeric result = make_result(&fctx->current);
1815 
1816  /* switch to memory context appropriate for iteration calculation */
1817  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1818 
1819  /* increment current in preparation for next iteration */
1820  add_var(&fctx->current, &fctx->step, &fctx->current);
1821  MemoryContextSwitchTo(oldcontext);
1822 
1823  /* do when there is more left to send */
1824  SRF_RETURN_NEXT(funcctx, NumericGetDatum(result));
1825  }
1826  else
1827  /* do when there is no more left */
1828  SRF_RETURN_DONE(funcctx);
1829 }
1830 
1831 
1832 /*
1833  * Implements the numeric version of the width_bucket() function
1834  * defined by SQL2003. See also width_bucket_float8().
1835  *
1836  * 'bound1' and 'bound2' are the lower and upper bounds of the
1837  * histogram's range, respectively. 'count' is the number of buckets
1838  * in the histogram. width_bucket() returns an integer indicating the
1839  * bucket number that 'operand' belongs to in an equiwidth histogram
1840  * with the specified characteristics. An operand smaller than the
1841  * lower bound is assigned to bucket 0. An operand greater than the
1842  * upper bound is assigned to an additional bucket (with number
1843  * count+1). We don't allow "NaN" for any of the numeric arguments.
1844  */
1845 Datum
1847 {
1848  Numeric operand = PG_GETARG_NUMERIC(0);
1849  Numeric bound1 = PG_GETARG_NUMERIC(1);
1850  Numeric bound2 = PG_GETARG_NUMERIC(2);
1851  int32 count = PG_GETARG_INT32(3);
1852  NumericVar count_var;
1853  NumericVar result_var;
1854  int32 result;
1855 
1856  if (count <= 0)
1857  ereport(ERROR,
1858  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1859  errmsg("count must be greater than zero")));
1860 
1861  if (NUMERIC_IS_SPECIAL(operand) ||
1862  NUMERIC_IS_SPECIAL(bound1) ||
1863  NUMERIC_IS_SPECIAL(bound2))
1864  {
1865  if (NUMERIC_IS_NAN(operand) ||
1866  NUMERIC_IS_NAN(bound1) ||
1867  NUMERIC_IS_NAN(bound2))
1868  ereport(ERROR,
1869  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1870  errmsg("operand, lower bound, and upper bound cannot be NaN")));
1871  /* We allow "operand" to be infinite; cmp_numerics will cope */
1872  if (NUMERIC_IS_INF(bound1) || NUMERIC_IS_INF(bound2))
1873  ereport(ERROR,
1874  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1875  errmsg("lower and upper bounds must be finite")));
1876  }
1877 
1878  init_var(&result_var);
1879  init_var(&count_var);
1880 
1881  /* Convert 'count' to a numeric, for ease of use later */
1882  int64_to_numericvar((int64) count, &count_var);
1883 
1884  switch (cmp_numerics(bound1, bound2))
1885  {
1886  case 0:
1887  ereport(ERROR,
1888  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1889  errmsg("lower bound cannot equal upper bound")));
1890  break;
1891 
1892  /* bound1 < bound2 */
1893  case -1:
1894  if (cmp_numerics(operand, bound1) < 0)
1895  set_var_from_var(&const_zero, &result_var);
1896  else if (cmp_numerics(operand, bound2) >= 0)
1897  add_var(&count_var, &const_one, &result_var);
1898  else
1899  compute_bucket(operand, bound1, bound2, &count_var,
1900  &result_var);
1901  break;
1902 
1903  /* bound1 > bound2 */
1904  case 1:
1905  if (cmp_numerics(operand, bound1) > 0)
1906  set_var_from_var(&const_zero, &result_var);
1907  else if (cmp_numerics(operand, bound2) <= 0)
1908  add_var(&count_var, &const_one, &result_var);
1909  else
1910  compute_bucket(operand, bound1, bound2, &count_var,
1911  &result_var);
1912  break;
1913  }
1914 
1915  /* if result exceeds the range of a legal int4, we ereport here */
1916  if (!numericvar_to_int32(&result_var, &result))
1917  ereport(ERROR,
1918  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1919  errmsg("integer out of range")));
1920 
1921  free_var(&count_var);
1922  free_var(&result_var);
1923 
1924  PG_RETURN_INT32(result);
1925 }
1926 
1927 /*
1928  * 'operand' is inside the bucket range, so determine the correct
1929  * bucket for it to go in. The calculations performed by this function
1930  * are derived directly from the SQL2003 spec. Note however that we
1931  * multiply by count before dividing, to avoid unnecessary roundoff error.
1932  */
1933 static void
1934 compute_bucket(Numeric operand, Numeric bound1, Numeric bound2,
1935  const NumericVar *count_var, NumericVar *result_var)
1936 {
1937  NumericVar bound1_var;
1938  NumericVar bound2_var;
1939  NumericVar operand_var;
1940 
1941  init_var_from_num(bound1, &bound1_var);
1942  init_var_from_num(bound2, &bound2_var);
1943  init_var_from_num(operand, &operand_var);
1944 
1945  /*
1946  * Per spec, bound1 is inclusive and bound2 is exclusive, and so we have
1947  * bound1 <= operand < bound2 or bound1 >= operand > bound2. Either way,
1948  * the result is ((operand - bound1) * count) / (bound2 - bound1) + 1,
1949  * where the quotient is computed using floor division (i.e., division to
1950  * zero decimal places with truncation), which guarantees that the result
1951  * is in the range [1, count]. Reversing the bounds doesn't affect the
1952  * computation, because the signs cancel out when dividing.
1953  */
1954  sub_var(&operand_var, &bound1_var, &operand_var);
1955  sub_var(&bound2_var, &bound1_var, &bound2_var);
1956 
1957  mul_var(&operand_var, count_var, &operand_var,
1958  operand_var.dscale + count_var->dscale);
1959  div_var(&operand_var, &bound2_var, result_var, 0, false);
1960  add_var(result_var, &const_one, result_var);
1961 
1962  free_var(&bound1_var);
1963  free_var(&bound2_var);
1964  free_var(&operand_var);
1965 }
1966 
1967 /* ----------------------------------------------------------------------
1968  *
1969  * Comparison functions
1970  *
1971  * Note: btree indexes need these routines not to leak memory; therefore,
1972  * be careful to free working copies of toasted datums. Most places don't
1973  * need to be so careful.
1974  *
1975  * Sort support:
1976  *
1977  * We implement the sortsupport strategy routine in order to get the benefit of
1978  * abbreviation. The ordinary numeric comparison can be quite slow as a result
1979  * of palloc/pfree cycles (due to detoasting packed values for alignment);
1980  * while this could be worked on itself, the abbreviation strategy gives more
1981  * speedup in many common cases.
1982  *
1983  * Two different representations are used for the abbreviated form, one in
1984  * int32 and one in int64, whichever fits into a by-value Datum. In both cases
1985  * the representation is negated relative to the original value, because we use
1986  * the largest negative value for NaN, which sorts higher than other values. We
1987  * convert the absolute value of the numeric to a 31-bit or 63-bit positive
1988  * value, and then negate it if the original number was positive.
1989  *
1990  * We abort the abbreviation process if the abbreviation cardinality is below
1991  * 0.01% of the row count (1 per 10k non-null rows). The actual break-even
1992  * point is somewhat below that, perhaps 1 per 30k (at 1 per 100k there's a
1993  * very small penalty), but we don't want to build up too many abbreviated
1994  * values before first testing for abort, so we take the slightly pessimistic
1995  * number. We make no attempt to estimate the cardinality of the real values,
1996  * since it plays no part in the cost model here (if the abbreviation is equal,
1997  * the cost of comparing equal and unequal underlying values is comparable).
1998  * We discontinue even checking for abort (saving us the hashing overhead) if
1999  * the estimated cardinality gets to 100k; that would be enough to support many
2000  * billions of rows while doing no worse than breaking even.
2001  *
2002  * ----------------------------------------------------------------------
2003  */
2004 
2005 /*
2006  * Sort support strategy routine.
2007  */
2008 Datum
2010 {
2012 
2013  ssup->comparator = numeric_fast_cmp;
2014 
2015  if (ssup->abbreviate)
2016  {
2017  NumericSortSupport *nss;
2018  MemoryContext oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2019 
2020  nss = palloc(sizeof(NumericSortSupport));
2021 
2022  /*
2023  * palloc a buffer for handling unaligned packed values in addition to
2024  * the support struct
2025  */
2026  nss->buf = palloc(VARATT_SHORT_MAX + VARHDRSZ + 1);
2027 
2028  nss->input_count = 0;
2029  nss->estimating = true;
2030  initHyperLogLog(&nss->abbr_card, 10);
2031 
2032  ssup->ssup_extra = nss;
2033 
2034  ssup->abbrev_full_comparator = ssup->comparator;
2038 
2039  MemoryContextSwitchTo(oldcontext);
2040  }
2041 
2042  PG_RETURN_VOID();
2043 }
2044 
2045 /*
2046  * Abbreviate a numeric datum, handling NaNs and detoasting
2047  * (must not leak memory!)
2048  */
2049 static Datum
2051 {
2052  NumericSortSupport *nss = ssup->ssup_extra;
2053  void *original_varatt = PG_DETOAST_DATUM_PACKED(original_datum);
2054  Numeric value;
2055  Datum result;
2056 
2057  nss->input_count += 1;
2058 
2059  /*
2060  * This is to handle packed datums without needing a palloc/pfree cycle;
2061  * we keep and reuse a buffer large enough to handle any short datum.
2062  */
2063  if (VARATT_IS_SHORT(original_varatt))
2064  {
2065  void *buf = nss->buf;
2066  Size sz = VARSIZE_SHORT(original_varatt) - VARHDRSZ_SHORT;
2067 
2069 
2070  SET_VARSIZE(buf, VARHDRSZ + sz);
2071  memcpy(VARDATA(buf), VARDATA_SHORT(original_varatt), sz);
2072 
2073  value = (Numeric) buf;
2074  }
2075  else
2076  value = (Numeric) original_varatt;
2077 
2079  {
2080  if (NUMERIC_IS_PINF(value))
2081  result = NUMERIC_ABBREV_PINF;
2082  else if (NUMERIC_IS_NINF(value))
2083  result = NUMERIC_ABBREV_NINF;
2084  else
2085  result = NUMERIC_ABBREV_NAN;
2086  }
2087  else
2088  {
2089  NumericVar var;
2090 
2091  init_var_from_num(value, &var);
2092 
2093  result = numeric_abbrev_convert_var(&var, nss);
2094  }
2095 
2096  /* should happen only for external/compressed toasts */
2097  if ((Pointer) original_varatt != DatumGetPointer(original_datum))
2098  pfree(original_varatt);
2099 
2100  return result;
2101 }
2102 
2103 /*
2104  * Consider whether to abort abbreviation.
2105  *
2106  * We pay no attention to the cardinality of the non-abbreviated data. There is
2107  * no reason to do so: unlike text, we have no fast check for equal values, so
2108  * we pay the full overhead whenever the abbreviations are equal regardless of
2109  * whether the underlying values are also equal.
2110  */
2111 static bool
2112 numeric_abbrev_abort(int memtupcount, SortSupport ssup)
2113 {
2114  NumericSortSupport *nss = ssup->ssup_extra;
2115  double abbr_card;
2116 
2117  if (memtupcount < 10000 || nss->input_count < 10000 || !nss->estimating)
2118  return false;
2119 
2120  abbr_card = estimateHyperLogLog(&nss->abbr_card);
2121 
2122  /*
2123  * If we have >100k distinct values, then even if we were sorting many
2124  * billion rows we'd likely still break even, and the penalty of undoing
2125  * that many rows of abbrevs would probably not be worth it. Stop even
2126  * counting at that point.
2127  */
2128  if (abbr_card > 100000.0)
2129  {
2130 #ifdef TRACE_SORT
2131  if (trace_sort)
2132  elog(LOG,
2133  "numeric_abbrev: estimation ends at cardinality %f"
2134  " after " INT64_FORMAT " values (%d rows)",
2135  abbr_card, nss->input_count, memtupcount);
2136 #endif
2137  nss->estimating = false;
2138  return false;
2139  }
2140 
2141  /*
2142  * Target minimum cardinality is 1 per ~10k of non-null inputs. (The
2143  * break even point is somewhere between one per 100k rows, where
2144  * abbreviation has a very slight penalty, and 1 per 10k where it wins by
2145  * a measurable percentage.) We use the relatively pessimistic 10k
2146  * threshold, and add a 0.5 row fudge factor, because it allows us to
2147  * abort earlier on genuinely pathological data where we've had exactly
2148  * one abbreviated value in the first 10k (non-null) rows.
2149  */
2150  if (abbr_card < nss->input_count / 10000.0 + 0.5)
2151  {
2152 #ifdef TRACE_SORT
2153  if (trace_sort)
2154  elog(LOG,
2155  "numeric_abbrev: aborting abbreviation at cardinality %f"
2156  " below threshold %f after " INT64_FORMAT " values (%d rows)",
2157  abbr_card, nss->input_count / 10000.0 + 0.5,
2158  nss->input_count, memtupcount);
2159 #endif
2160  return true;
2161  }
2162 
2163 #ifdef TRACE_SORT
2164  if (trace_sort)
2165  elog(LOG,
2166  "numeric_abbrev: cardinality %f"
2167  " after " INT64_FORMAT " values (%d rows)",
2168  abbr_card, nss->input_count, memtupcount);
2169 #endif
2170 
2171  return false;
2172 }
2173 
2174 /*
2175  * Non-fmgr interface to the comparison routine to allow sortsupport to elide
2176  * the fmgr call. The saving here is small given how slow numeric comparisons
2177  * are, but it is a required part of the sort support API when abbreviations
2178  * are performed.
2179  *
2180  * Two palloc/pfree cycles could be saved here by using persistent buffers for
2181  * aligning short-varlena inputs, but this has not so far been considered to
2182  * be worth the effort.
2183  */
2184 static int
2186 {
2187  Numeric nx = DatumGetNumeric(x);
2188  Numeric ny = DatumGetNumeric(y);
2189  int result;
2190 
2191  result = cmp_numerics(nx, ny);
2192 
2193  if ((Pointer) nx != DatumGetPointer(x))
2194  pfree(nx);
2195  if ((Pointer) ny != DatumGetPointer(y))
2196  pfree(ny);
2197 
2198  return result;
2199 }
2200 
2201 /*
2202  * Compare abbreviations of values. (Abbreviations may be equal where the true
2203  * values differ, but if the abbreviations differ, they must reflect the
2204  * ordering of the true values.)
2205  */
2206 static int
2208 {
2209  /*
2210  * NOTE WELL: this is intentionally backwards, because the abbreviation is
2211  * negated relative to the original value, to handle NaN/infinity cases.
2212  */
2214  return 1;
2216  return -1;
2217  return 0;
2218 }
2219 
2220 /*
2221  * Abbreviate a NumericVar according to the available bit size.
2222  *
2223  * The 31-bit value is constructed as:
2224  *
2225  * 0 + 7bits digit weight + 24 bits digit value
2226  *
2227  * where the digit weight is in single decimal digits, not digit words, and
2228  * stored in excess-44 representation[1]. The 24-bit digit value is the 7 most
2229  * significant decimal digits of the value converted to binary. Values whose
2230  * weights would fall outside the representable range are rounded off to zero
2231  * (which is also used to represent actual zeros) or to 0x7FFFFFFF (which
2232  * otherwise cannot occur). Abbreviation therefore fails to gain any advantage
2233  * where values are outside the range 10^-44 to 10^83, which is not considered
2234  * to be a serious limitation, or when values are of the same magnitude and
2235  * equal in the first 7 decimal digits, which is considered to be an
2236  * unavoidable limitation given the available bits. (Stealing three more bits
2237  * to compare another digit would narrow the range of representable weights by
2238  * a factor of 8, which starts to look like a real limiting factor.)
2239  *
2240  * (The value 44 for the excess is essentially arbitrary)
2241  *
2242  * The 63-bit value is constructed as:
2243  *
2244  * 0 + 7bits weight + 4 x 14-bit packed digit words
2245  *
2246  * The weight in this case is again stored in excess-44, but this time it is
2247  * the original weight in digit words (i.e. powers of 10000). The first four
2248  * digit words of the value (if present; trailing zeros are assumed as needed)
2249  * are packed into 14 bits each to form the rest of the value. Again,
2250  * out-of-range values are rounded off to 0 or 0x7FFFFFFFFFFFFFFF. The
2251  * representable range in this case is 10^-176 to 10^332, which is considered
2252  * to be good enough for all practical purposes, and comparison of 4 words
2253  * means that at least 13 decimal digits are compared, which is considered to
2254  * be a reasonable compromise between effectiveness and efficiency in computing
2255  * the abbreviation.
2256  *
2257  * (The value 44 for the excess is even more arbitrary here, it was chosen just
2258  * to match the value used in the 31-bit case)
2259  *
2260  * [1] - Excess-k representation means that the value is offset by adding 'k'
2261  * and then treated as unsigned, so the smallest representable value is stored
2262  * with all bits zero. This allows simple comparisons to work on the composite
2263  * value.
2264  */
2265 
2266 #if NUMERIC_ABBREV_BITS == 64
2267 
2268 static Datum
2270 {
2271  int ndigits = var->ndigits;
2272  int weight = var->weight;
2273  int64 result;
2274 
2275  if (ndigits == 0 || weight < -44)
2276  {
2277  result = 0;
2278  }
2279  else if (weight > 83)
2280  {
2281  result = PG_INT64_MAX;
2282  }
2283  else
2284  {
2285  result = ((int64) (weight + 44) << 56);
2286 
2287  switch (ndigits)
2288  {
2289  default:
2290  result |= ((int64) var->digits[3]);
2291  /* FALLTHROUGH */
2292  case 3:
2293  result |= ((int64) var->digits[2]) << 14;
2294  /* FALLTHROUGH */
2295  case 2:
2296  result |= ((int64) var->digits[1]) << 28;
2297  /* FALLTHROUGH */
2298  case 1:
2299  result |= ((int64) var->digits[0]) << 42;
2300  break;
2301  }
2302  }
2303 
2304  /* the abbrev is negated relative to the original */
2305  if (var->sign == NUMERIC_POS)
2306  result = -result;
2307 
2308  if (nss->estimating)
2309  {
2310  uint32 tmp = ((uint32) result
2311  ^ (uint32) ((uint64) result >> 32));
2312 
2314  }
2315 
2316  return NumericAbbrevGetDatum(result);
2317 }
2318 
2319 #endif /* NUMERIC_ABBREV_BITS == 64 */
2320 
2321 #if NUMERIC_ABBREV_BITS == 32
2322 
2323 static Datum
2325 {
2326  int ndigits = var->ndigits;
2327  int weight = var->weight;
2328  int32 result;
2329 
2330  if (ndigits == 0 || weight < -11)
2331  {
2332  result = 0;
2333  }
2334  else if (weight > 20)
2335  {
2336  result = PG_INT32_MAX;
2337  }
2338  else
2339  {
2340  NumericDigit nxt1 = (ndigits > 1) ? var->digits[1] : 0;
2341 
2342  weight = (weight + 11) * 4;
2343 
2344  result = var->digits[0];
2345 
2346  /*
2347  * "result" now has 1 to 4 nonzero decimal digits. We pack in more
2348  * digits to make 7 in total (largest we can fit in 24 bits)
2349  */
2350 
2351  if (result > 999)
2352  {
2353  /* already have 4 digits, add 3 more */
2354  result = (result * 1000) + (nxt1 / 10);
2355  weight += 3;
2356  }
2357  else if (result > 99)
2358  {
2359  /* already have 3 digits, add 4 more */
2360  result = (result * 10000) + nxt1;
2361  weight += 2;
2362  }
2363  else if (result > 9)
2364  {
2365  NumericDigit nxt2 = (ndigits > 2) ? var->digits[2] : 0;
2366 
2367  /* already have 2 digits, add 5 more */
2368  result = (result * 100000) + (nxt1 * 10) + (nxt2 / 1000);
2369  weight += 1;
2370  }
2371  else
2372  {
2373  NumericDigit nxt2 = (ndigits > 2) ? var->digits[2] : 0;
2374 
2375  /* already have 1 digit, add 6 more */
2376  result = (result * 1000000) + (nxt1 * 100) + (nxt2 / 100);
2377  }
2378 
2379  result = result | (weight << 24);
2380  }
2381 
2382  /* the abbrev is negated relative to the original */
2383  if (var->sign == NUMERIC_POS)
2384  result = -result;
2385 
2386  if (nss->estimating)
2387  {
2388  uint32 tmp = (uint32) result;
2389 
2391  }
2392 
2393  return NumericAbbrevGetDatum(result);
2394 }
2395 
2396 #endif /* NUMERIC_ABBREV_BITS == 32 */
2397 
2398 /*
2399  * Ordinary (non-sortsupport) comparisons follow.
2400  */
2401 
2402 Datum
2404 {
2405  Numeric num1 = PG_GETARG_NUMERIC(0);
2406  Numeric num2 = PG_GETARG_NUMERIC(1);
2407  int result;
2408 
2409  result = cmp_numerics(num1, num2);
2410 
2411  PG_FREE_IF_COPY(num1, 0);
2412  PG_FREE_IF_COPY(num2, 1);
2413 
2414  PG_RETURN_INT32(result);
2415 }
2416 
2417 
2418 Datum
2420 {
2421  Numeric num1 = PG_GETARG_NUMERIC(0);
2422  Numeric num2 = PG_GETARG_NUMERIC(1);
2423  bool result;
2424 
2425  result = cmp_numerics(num1, num2) == 0;
2426 
2427  PG_FREE_IF_COPY(num1, 0);
2428  PG_FREE_IF_COPY(num2, 1);
2429 
2430  PG_RETURN_BOOL(result);
2431 }
2432 
2433 Datum
2435 {
2436  Numeric num1 = PG_GETARG_NUMERIC(0);
2437  Numeric num2 = PG_GETARG_NUMERIC(1);
2438  bool result;
2439 
2440  result = cmp_numerics(num1, num2) != 0;
2441 
2442  PG_FREE_IF_COPY(num1, 0);
2443  PG_FREE_IF_COPY(num2, 1);
2444 
2445  PG_RETURN_BOOL(result);
2446 }
2447 
2448 Datum
2450 {
2451  Numeric num1 = PG_GETARG_NUMERIC(0);
2452  Numeric num2 = PG_GETARG_NUMERIC(1);
2453  bool result;
2454 
2455  result = cmp_numerics(num1, num2) > 0;
2456 
2457  PG_FREE_IF_COPY(num1, 0);
2458  PG_FREE_IF_COPY(num2, 1);
2459 
2460  PG_RETURN_BOOL(result);
2461 }
2462 
2463 Datum
2465 {
2466  Numeric num1 = PG_GETARG_NUMERIC(0);
2467  Numeric num2 = PG_GETARG_NUMERIC(1);
2468  bool result;
2469 
2470  result = cmp_numerics(num1, num2) >= 0;
2471 
2472  PG_FREE_IF_COPY(num1, 0);
2473  PG_FREE_IF_COPY(num2, 1);
2474 
2475  PG_RETURN_BOOL(result);
2476 }
2477 
2478 Datum
2480 {
2481  Numeric num1 = PG_GETARG_NUMERIC(0);
2482  Numeric num2 = PG_GETARG_NUMERIC(1);
2483  bool result;
2484 
2485  result = cmp_numerics(num1, num2) < 0;
2486 
2487  PG_FREE_IF_COPY(num1, 0);
2488  PG_FREE_IF_COPY(num2, 1);
2489 
2490  PG_RETURN_BOOL(result);
2491 }
2492 
2493 Datum
2495 {
2496  Numeric num1 = PG_GETARG_NUMERIC(0);
2497  Numeric num2 = PG_GETARG_NUMERIC(1);
2498  bool result;
2499 
2500  result = cmp_numerics(num1, num2) <= 0;
2501 
2502  PG_FREE_IF_COPY(num1, 0);
2503  PG_FREE_IF_COPY(num2, 1);
2504 
2505  PG_RETURN_BOOL(result);
2506 }
2507 
2508 static int
2510 {
2511  int result;
2512 
2513  /*
2514  * We consider all NANs to be equal and larger than any non-NAN (including
2515  * Infinity). This is somewhat arbitrary; the important thing is to have
2516  * a consistent sort order.
2517  */
2518  if (NUMERIC_IS_SPECIAL(num1))
2519  {
2520  if (NUMERIC_IS_NAN(num1))
2521  {
2522  if (NUMERIC_IS_NAN(num2))
2523  result = 0; /* NAN = NAN */
2524  else
2525  result = 1; /* NAN > non-NAN */
2526  }
2527  else if (NUMERIC_IS_PINF(num1))
2528  {
2529  if (NUMERIC_IS_NAN(num2))
2530  result = -1; /* PINF < NAN */
2531  else if (NUMERIC_IS_PINF(num2))
2532  result = 0; /* PINF = PINF */
2533  else
2534  result = 1; /* PINF > anything else */
2535  }
2536  else /* num1 must be NINF */
2537  {
2538  if (NUMERIC_IS_NINF(num2))
2539  result = 0; /* NINF = NINF */
2540  else
2541  result = -1; /* NINF < anything else */
2542  }
2543  }
2544  else if (NUMERIC_IS_SPECIAL(num2))
2545  {
2546  if (NUMERIC_IS_NINF(num2))
2547  result = 1; /* normal > NINF */
2548  else
2549  result = -1; /* normal < NAN or PINF */
2550  }
2551  else
2552  {
2553  result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
2554  NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
2555  NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
2556  NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
2557  }
2558 
2559  return result;
2560 }
2561 
2562 /*
2563  * in_range support function for numeric.
2564  */
2565 Datum
2567 {
2569  Numeric base = PG_GETARG_NUMERIC(1);
2570  Numeric offset = PG_GETARG_NUMERIC(2);
2571  bool sub = PG_GETARG_BOOL(3);
2572  bool less = PG_GETARG_BOOL(4);
2573  bool result;
2574 
2575  /*
2576  * Reject negative (including -Inf) or NaN offset. Negative is per spec,
2577  * and NaN is because appropriate semantics for that seem non-obvious.
2578  */
2579  if (NUMERIC_IS_NAN(offset) ||
2580  NUMERIC_IS_NINF(offset) ||
2581  NUMERIC_SIGN(offset) == NUMERIC_NEG)
2582  ereport(ERROR,
2583  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2584  errmsg("invalid preceding or following size in window function")));
2585 
2586  /*
2587  * Deal with cases where val and/or base is NaN, following the rule that
2588  * NaN sorts after non-NaN (cf cmp_numerics). The offset cannot affect
2589  * the conclusion.
2590  */
2591  if (NUMERIC_IS_NAN(val))
2592  {
2593  if (NUMERIC_IS_NAN(base))
2594  result = true; /* NAN = NAN */
2595  else
2596  result = !less; /* NAN > non-NAN */
2597  }
2598  else if (NUMERIC_IS_NAN(base))
2599  {
2600  result = less; /* non-NAN < NAN */
2601  }
2602 
2603  /*
2604  * Deal with infinite offset (necessarily +Inf, at this point).
2605  */
2606  else if (NUMERIC_IS_SPECIAL(offset))
2607  {
2608  Assert(NUMERIC_IS_PINF(offset));
2609  if (sub ? NUMERIC_IS_PINF(base) : NUMERIC_IS_NINF(base))
2610  {
2611  /*
2612  * base +/- offset would produce NaN, so return true for any val
2613  * (see in_range_float8_float8() for reasoning).
2614  */
2615  result = true;
2616  }
2617  else if (sub)
2618  {
2619  /* base - offset must be -inf */
2620  if (less)
2621  result = NUMERIC_IS_NINF(val); /* only -inf is <= sum */
2622  else
2623  result = true; /* any val is >= sum */
2624  }
2625  else
2626  {
2627  /* base + offset must be +inf */
2628  if (less)
2629  result = true; /* any val is <= sum */
2630  else
2631  result = NUMERIC_IS_PINF(val); /* only +inf is >= sum */
2632  }
2633  }
2634 
2635  /*
2636  * Deal with cases where val and/or base is infinite. The offset, being
2637  * now known finite, cannot affect the conclusion.
2638  */
2639  else if (NUMERIC_IS_SPECIAL(val))
2640  {
2641  if (NUMERIC_IS_PINF(val))
2642  {
2643  if (NUMERIC_IS_PINF(base))
2644  result = true; /* PINF = PINF */
2645  else
2646  result = !less; /* PINF > any other non-NAN */
2647  }
2648  else /* val must be NINF */
2649  {
2650  if (NUMERIC_IS_NINF(base))
2651  result = true; /* NINF = NINF */
2652  else
2653  result = less; /* NINF < anything else */
2654  }
2655  }
2656  else if (NUMERIC_IS_SPECIAL(base))
2657  {
2658  if (NUMERIC_IS_NINF(base))
2659  result = !less; /* normal > NINF */
2660  else
2661  result = less; /* normal < PINF */
2662  }
2663  else
2664  {
2665  /*
2666  * Otherwise go ahead and compute base +/- offset. While it's
2667  * possible for this to overflow the numeric format, it's unlikely
2668  * enough that we don't take measures to prevent it.
2669  */
2670  NumericVar valv;
2671  NumericVar basev;
2672  NumericVar offsetv;
2673  NumericVar sum;
2674 
2675  init_var_from_num(val, &valv);
2676  init_var_from_num(base, &basev);
2677  init_var_from_num(offset, &offsetv);
2678  init_var(&sum);
2679 
2680  if (sub)
2681  sub_var(&basev, &offsetv, &sum);
2682  else
2683  add_var(&basev, &offsetv, &sum);
2684 
2685  if (less)
2686  result = (cmp_var(&valv, &sum) <= 0);
2687  else
2688  result = (cmp_var(&valv, &sum) >= 0);
2689 
2690  free_var(&sum);
2691  }
2692 
2693  PG_FREE_IF_COPY(val, 0);
2694  PG_FREE_IF_COPY(base, 1);
2695  PG_FREE_IF_COPY(offset, 2);
2696 
2697  PG_RETURN_BOOL(result);
2698 }
2699 
2700 Datum
2702 {
2704  Datum digit_hash;
2705  Datum result;
2706  int weight;
2707  int start_offset;
2708  int end_offset;
2709  int i;
2710  int hash_len;
2712 
2713  /* If it's NaN or infinity, don't try to hash the rest of the fields */
2714  if (NUMERIC_IS_SPECIAL(key))
2715  PG_RETURN_UINT32(0);
2716 
2717  weight = NUMERIC_WEIGHT(key);
2718  start_offset = 0;
2719  end_offset = 0;
2720 
2721  /*
2722  * Omit any leading or trailing zeros from the input to the hash. The
2723  * numeric implementation *should* guarantee that leading and trailing
2724  * zeros are suppressed, but we're paranoid. Note that we measure the
2725  * starting and ending offsets in units of NumericDigits, not bytes.
2726  */
2728  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2729  {
2730  if (digits[i] != (NumericDigit) 0)
2731  break;
2732 
2733  start_offset++;
2734 
2735  /*
2736  * The weight is effectively the # of digits before the decimal point,
2737  * so decrement it for each leading zero we skip.
2738  */
2739  weight--;
2740  }
2741 
2742  /*
2743  * If there are no non-zero digits, then the value of the number is zero,
2744  * regardless of any other fields.
2745  */
2746  if (NUMERIC_NDIGITS(key) == start_offset)
2747  PG_RETURN_UINT32(-1);
2748 
2749  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2750  {
2751  if (digits[i] != (NumericDigit) 0)
2752  break;
2753 
2754  end_offset++;
2755  }
2756 
2757  /* If we get here, there should be at least one non-zero digit */
2758  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2759 
2760  /*
2761  * Note that we don't hash on the Numeric's scale, since two numerics can
2762  * compare equal but have different scales. We also don't hash on the
2763  * sign, although we could: since a sign difference implies inequality,
2764  * this shouldn't affect correctness.
2765  */
2766  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2767  digit_hash = hash_any((unsigned char *) (NUMERIC_DIGITS(key) + start_offset),
2768  hash_len * sizeof(NumericDigit));
2769 
2770  /* Mix in the weight, via XOR */
2771  result = digit_hash ^ weight;
2772 
2773  PG_RETURN_DATUM(result);
2774 }
2775 
2776 /*
2777  * Returns 64-bit value by hashing a value to a 64-bit value, with a seed.
2778  * Otherwise, similar to hash_numeric.
2779  */
2780 Datum
2782 {
2784  uint64 seed = PG_GETARG_INT64(1);
2785  Datum digit_hash;
2786  Datum result;
2787  int weight;
2788  int start_offset;
2789  int end_offset;
2790  int i;
2791  int hash_len;
2793 
2794  /* If it's NaN or infinity, don't try to hash the rest of the fields */
2795  if (NUMERIC_IS_SPECIAL(key))
2796  PG_RETURN_UINT64(seed);
2797 
2798  weight = NUMERIC_WEIGHT(key);
2799  start_offset = 0;
2800  end_offset = 0;
2801 
2803  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2804  {
2805  if (digits[i] != (NumericDigit) 0)
2806  break;
2807 
2808  start_offset++;
2809 
2810  weight--;
2811  }
2812 
2813  if (NUMERIC_NDIGITS(key) == start_offset)
2814  PG_RETURN_UINT64(seed - 1);
2815 
2816  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2817  {
2818  if (digits[i] != (NumericDigit) 0)
2819  break;
2820 
2821  end_offset++;
2822  }
2823 
2824  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2825 
2826  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2827  digit_hash = hash_any_extended((unsigned char *) (NUMERIC_DIGITS(key)
2828  + start_offset),
2829  hash_len * sizeof(NumericDigit),
2830  seed);
2831 
2832  result = UInt64GetDatum(DatumGetUInt64(digit_hash) ^ weight);
2833 
2834  PG_RETURN_DATUM(result);
2835 }
2836 
2837 
2838 /* ----------------------------------------------------------------------
2839  *
2840  * Basic arithmetic functions
2841  *
2842  * ----------------------------------------------------------------------
2843  */
2844 
2845 
2846 /*
2847  * numeric_add() -
2848  *
2849  * Add two numerics
2850  */
2851 Datum
2853 {
2854  Numeric num1 = PG_GETARG_NUMERIC(0);
2855  Numeric num2 = PG_GETARG_NUMERIC(1);
2856  Numeric res;
2857 
2858  res = numeric_add_opt_error(num1, num2, NULL);
2859 
2861 }
2862 
2863 /*
2864  * numeric_add_opt_error() -
2865  *
2866  * Internal version of numeric_add(). If "*have_error" flag is provided,
2867  * on error it's set to true, NULL returned. This is helpful when caller
2868  * need to handle errors by itself.
2869  */
2870 Numeric
2871 numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
2872 {
2873  NumericVar arg1;
2874  NumericVar arg2;
2875  NumericVar result;
2876  Numeric res;
2877 
2878  /*
2879  * Handle NaN and infinities
2880  */
2881  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
2882  {
2883  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
2884  return make_result(&const_nan);
2885  if (NUMERIC_IS_PINF(num1))
2886  {
2887  if (NUMERIC_IS_NINF(num2))
2888  return make_result(&const_nan); /* Inf + -Inf */
2889  else
2890  return make_result(&const_pinf);
2891  }
2892  if (NUMERIC_IS_NINF(num1))
2893  {
2894  if (NUMERIC_IS_PINF(num2))
2895  return make_result(&const_nan); /* -Inf + Inf */
2896  else
2897  return make_result(&const_ninf);
2898  }
2899  /* by here, num1 must be finite, so num2 is not */
2900  if (NUMERIC_IS_PINF(num2))
2901  return make_result(&const_pinf);
2902  Assert(NUMERIC_IS_NINF(num2));
2903  return make_result(&const_ninf);
2904  }
2905 
2906  /*
2907  * Unpack the values, let add_var() compute the result and return it.
2908  */
2909  init_var_from_num(num1, &arg1);
2910  init_var_from_num(num2, &arg2);
2911 
2912  init_var(&result);
2913  add_var(&arg1, &arg2, &result);
2914 
2915  res = make_result_opt_error(&result, have_error);
2916 
2917  free_var(&result);
2918 
2919  return res;
2920 }
2921 
2922 
2923 /*
2924  * numeric_sub() -
2925  *
2926  * Subtract one numeric from another
2927  */
2928 Datum
2930 {
2931  Numeric num1 = PG_GETARG_NUMERIC(0);
2932  Numeric num2 = PG_GETARG_NUMERIC(1);
2933  Numeric res;
2934 
2935  res = numeric_sub_opt_error(num1, num2, NULL);
2936 
2938 }
2939 
2940 
2941 /*
2942  * numeric_sub_opt_error() -
2943  *
2944  * Internal version of numeric_sub(). If "*have_error" flag is provided,
2945  * on error it's set to true, NULL returned. This is helpful when caller
2946  * need to handle errors by itself.
2947  */
2948 Numeric
2949 numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
2950 {
2951  NumericVar arg1;
2952  NumericVar arg2;
2953  NumericVar result;
2954  Numeric res;
2955 
2956  /*
2957  * Handle NaN and infinities
2958  */
2959  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
2960  {
2961  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
2962  return make_result(&const_nan);
2963  if (NUMERIC_IS_PINF(num1))
2964  {
2965  if (NUMERIC_IS_PINF(num2))
2966  return make_result(&const_nan); /* Inf - Inf */
2967  else
2968  return make_result(&const_pinf);
2969  }
2970  if (NUMERIC_IS_NINF(num1))
2971  {
2972  if (NUMERIC_IS_NINF(num2))
2973  return make_result(&const_nan); /* -Inf - -Inf */
2974  else
2975  return make_result(&const_ninf);
2976  }
2977  /* by here, num1 must be finite, so num2 is not */
2978  if (NUMERIC_IS_PINF(num2))
2979  return make_result(&const_ninf);
2980  Assert(NUMERIC_IS_NINF(num2));
2981  return make_result(&const_pinf);
2982  }
2983 
2984  /*
2985  * Unpack the values, let sub_var() compute the result and return it.
2986  */
2987  init_var_from_num(num1, &arg1);
2988  init_var_from_num(num2, &arg2);
2989 
2990  init_var(&result);
2991  sub_var(&arg1, &arg2, &result);
2992 
2993  res = make_result_opt_error(&result, have_error);
2994 
2995  free_var(&result);
2996 
2997  return res;
2998 }
2999 
3000 
3001 /*
3002  * numeric_mul() -
3003  *
3004  * Calculate the product of two numerics
3005  */
3006 Datum
3008 {
3009  Numeric num1 = PG_GETARG_NUMERIC(0);
3010  Numeric num2 = PG_GETARG_NUMERIC(1);
3011  Numeric res;
3012 
3013  res = numeric_mul_opt_error(num1, num2, NULL);
3014 
3016 }
3017 
3018 
3019 /*
3020  * numeric_mul_opt_error() -
3021  *
3022  * Internal version of numeric_mul(). If "*have_error" flag is provided,
3023  * on error it's set to true, NULL returned. This is helpful when caller
3024  * need to handle errors by itself.
3025  */
3026 Numeric
3027 numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
3028 {
3029  NumericVar arg1;
3030  NumericVar arg2;
3031  NumericVar result;
3032  Numeric res;
3033 
3034  /*
3035  * Handle NaN and infinities
3036  */
3037  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3038  {
3039  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
3040  return make_result(&const_nan);
3041  if (NUMERIC_IS_PINF(num1))
3042  {
3043  switch (numeric_sign_internal(num2))
3044  {
3045  case 0:
3046  return make_result(&const_nan); /* Inf * 0 */
3047  case 1:
3048  return make_result(&const_pinf);
3049  case -1:
3050  return make_result(&const_ninf);
3051  }
3052  Assert(false);
3053  }
3054  if (NUMERIC_IS_NINF(num1))
3055  {
3056  switch (numeric_sign_internal(num2))
3057  {
3058  case 0:
3059  return make_result(&const_nan); /* -Inf * 0 */
3060  case 1:
3061  return make_result(&const_ninf);
3062  case -1:
3063  return make_result(&const_pinf);
3064  }
3065  Assert(false);
3066  }
3067  /* by here, num1 must be finite, so num2 is not */
3068  if (NUMERIC_IS_PINF(num2))
3069  {
3070  switch (numeric_sign_internal(num1))
3071  {
3072  case 0:
3073  return make_result(&const_nan); /* 0 * Inf */
3074  case 1:
3075  return make_result(&const_pinf);
3076  case -1:
3077  return make_result(&const_ninf);
3078  }
3079  Assert(false);
3080  }
3081  Assert(NUMERIC_IS_NINF(num2));
3082  switch (numeric_sign_internal(num1))
3083  {
3084  case 0:
3085  return make_result(&const_nan); /* 0 * -Inf */
3086  case 1:
3087  return make_result(&const_ninf);
3088  case -1:
3089  return make_result(&const_pinf);
3090  }
3091  Assert(false);
3092  }
3093 
3094  /*
3095  * Unpack the values, let mul_var() compute the result and return it.
3096  * Unlike add_var() and sub_var(), mul_var() will round its result. In the
3097  * case of numeric_mul(), which is invoked for the * operator on numerics,
3098  * we request exact representation for the product (rscale = sum(dscale of
3099  * arg1, dscale of arg2)). If the exact result has more digits after the
3100  * decimal point than can be stored in a numeric, we round it. Rounding
3101  * after computing the exact result ensures that the final result is
3102  * correctly rounded (rounding in mul_var() using a truncated product
3103  * would not guarantee this).
3104  */
3105  init_var_from_num(num1, &arg1);
3106  init_var_from_num(num2, &arg2);
3107 
3108  init_var(&result);
3109  mul_var(&arg1, &arg2, &result, arg1.dscale + arg2.dscale);
3110 
3111  if (result.dscale > NUMERIC_DSCALE_MAX)
3112  round_var(&result, NUMERIC_DSCALE_MAX);
3113 
3114  res = make_result_opt_error(&result, have_error);
3115 
3116  free_var(&result);
3117 
3118  return res;
3119 }
3120 
3121 
3122 /*
3123  * numeric_div() -
3124  *
3125  * Divide one numeric into another
3126  */
3127 Datum
3129 {
3130  Numeric num1 = PG_GETARG_NUMERIC(0);
3131  Numeric num2 = PG_GETARG_NUMERIC(1);
3132  Numeric res;
3133 
3134  res = numeric_div_opt_error(num1, num2, NULL);
3135 
3137 }
3138 
3139 
3140 /*
3141  * numeric_div_opt_error() -
3142  *
3143  * Internal version of numeric_div(). If "*have_error" flag is provided,
3144  * on error it's set to true, NULL returned. This is helpful when caller
3145  * need to handle errors by itself.
3146  */
3147 Numeric
3148 numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
3149 {
3150  NumericVar arg1;
3151  NumericVar arg2;
3152  NumericVar result;
3153  Numeric res;
3154  int rscale;
3155 
3156  if (have_error)
3157  *have_error = false;
3158 
3159  /*
3160  * Handle NaN and infinities
3161  */
3162  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3163  {
3164  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
3165  return make_result(&const_nan);
3166  if (NUMERIC_IS_PINF(num1))
3167  {
3168  if (NUMERIC_IS_SPECIAL(num2))
3169  return make_result(&const_nan); /* Inf / [-]Inf */
3170  switch (numeric_sign_internal(num2))
3171  {
3172  case 0:
3173  if (have_error)
3174  {
3175  *have_error = true;
3176  return NULL;
3177  }
3178  ereport(ERROR,
3179  (errcode(ERRCODE_DIVISION_BY_ZERO),
3180  errmsg("division by zero")));
3181  break;
3182  case 1:
3183  return make_result(&const_pinf);
3184  case -1:
3185  return make_result(&const_ninf);
3186  }
3187  Assert(false);
3188  }
3189  if (NUMERIC_IS_NINF(num1))
3190  {
3191  if (NUMERIC_IS_SPECIAL(num2))
3192  return make_result(&const_nan); /* -Inf / [-]Inf */
3193  switch (numeric_sign_internal(num2))
3194  {
3195  case 0:
3196  if (have_error)
3197  {
3198  *have_error = true;
3199  return NULL;
3200  }
3201  ereport(ERROR,
3202  (errcode(ERRCODE_DIVISION_BY_ZERO),
3203  errmsg("division by zero")));
3204  break;
3205  case 1:
3206  return make_result(&const_ninf);
3207  case -1:
3208  return make_result(&const_pinf);
3209  }
3210  Assert(false);
3211  }
3212  /* by here, num1 must be finite, so num2 is not */
3213 
3214  /*
3215  * POSIX would have us return zero or minus zero if num1 is zero, and
3216  * otherwise throw an underflow error. But the numeric type doesn't
3217  * really do underflow, so let's just return zero.
3218  */
3219  return make_result(&const_zero);
3220  }
3221 
3222  /*
3223  * Unpack the arguments
3224  */
3225  init_var_from_num(num1, &arg1);
3226  init_var_from_num(num2, &arg2);
3227 
3228  init_var(&result);
3229 
3230  /*
3231  * Select scale for division result
3232  */
3233  rscale = select_div_scale(&arg1, &arg2);
3234 
3235  /*
3236  * If "have_error" is provided, check for division by zero here
3237  */
3238  if (have_error && (arg2.ndigits == 0 || arg2.digits[0] == 0))
3239  {
3240  *have_error = true;
3241  return NULL;
3242  }
3243 
3244  /*
3245  * Do the divide and return the result
3246  */
3247  div_var(&arg1, &arg2, &result, rscale, true);
3248 
3249  res = make_result_opt_error(&result, have_error);
3250 
3251  free_var(&result);
3252 
3253  return res;
3254 }
3255 
3256 
3257 /*
3258  * numeric_div_trunc() -
3259  *
3260  * Divide one numeric into another, truncating the result to an integer
3261  */
3262 Datum
3264 {
3265  Numeric num1 = PG_GETARG_NUMERIC(0);
3266  Numeric num2 = PG_GETARG_NUMERIC(1);
3267  NumericVar arg1;
3268  NumericVar arg2;
3269  NumericVar result;
3270  Numeric res;
3271 
3272  /*
3273  * Handle NaN and infinities
3274  */
3275  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3276  {
3277  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
3279  if (NUMERIC_IS_PINF(num1))
3280  {
3281  if (NUMERIC_IS_SPECIAL(num2))
3282  PG_RETURN_NUMERIC(make_result(&const_nan)); /* Inf / [-]Inf */
3283  switch (numeric_sign_internal(num2))
3284  {
3285  case 0:
3286  ereport(ERROR,
3287  (errcode(ERRCODE_DIVISION_BY_ZERO),
3288  errmsg("division by zero")));
3289  break;
3290  case 1:
3292  case -1:
3294  }
3295  Assert(false);
3296  }
3297  if (NUMERIC_IS_NINF(num1))
3298  {
3299  if (NUMERIC_IS_SPECIAL(num2))
3300  PG_RETURN_NUMERIC(make_result(&const_nan)); /* -Inf / [-]Inf */
3301  switch (numeric_sign_internal(num2))
3302  {
3303  case 0:
3304  ereport(ERROR,
3305  (errcode(ERRCODE_DIVISION_BY_ZERO),
3306  errmsg("division by zero")));
3307  break;
3308  case 1:
3310  case -1:
3312  }
3313  Assert(false);
3314  }
3315  /* by here, num1 must be finite, so num2 is not */
3316 
3317  /*
3318  * POSIX would have us return zero or minus zero if num1 is zero, and
3319  * otherwise throw an underflow error. But the numeric type doesn't
3320  * really do underflow, so let's just return zero.
3321  */
3323  }
3324 
3325  /*
3326  * Unpack the arguments
3327  */
3328  init_var_from_num(num1, &arg1);
3329  init_var_from_num(num2, &arg2);
3330 
3331  init_var(&result);
3332 
3333  /*
3334  * Do the divide and return the result
3335  */
3336  div_var(&arg1, &arg2, &result, 0, false);
3337 
3338  res = make_result(&result);
3339 
3340  free_var(&result);
3341 
3343 }
3344 
3345 
3346 /*
3347  * numeric_mod() -
3348  *
3349  * Calculate the modulo of two numerics
3350  */
3351 Datum
3353 {
3354  Numeric num1 = PG_GETARG_NUMERIC(0);
3355  Numeric num2 = PG_GETARG_NUMERIC(1);
3356  Numeric res;
3357 
3358  res = numeric_mod_opt_error(num1, num2, NULL);
3359 
3361 }
3362 
3363 
3364 /*
3365  * numeric_mod_opt_error() -
3366  *
3367  * Internal version of numeric_mod(). If "*have_error" flag is provided,
3368  * on error it's set to true, NULL returned. This is helpful when caller
3369  * need to handle errors by itself.
3370  */
3371 Numeric
3372 numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
3373 {
3374  Numeric res;
3375  NumericVar arg1;
3376  NumericVar arg2;
3377  NumericVar result;
3378 
3379  if (have_error)
3380  *have_error = false;
3381 
3382  /*
3383  * Handle NaN and infinities. We follow POSIX fmod() on this, except that
3384  * POSIX treats x-is-infinite and y-is-zero identically, raising EDOM and
3385  * returning NaN. We choose to throw error only for y-is-zero.
3386  */
3387  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3388  {
3389  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
3390  return make_result(&const_nan);
3391  if (NUMERIC_IS_INF(num1))
3392  {
3393  if (numeric_sign_internal(num2) == 0)
3394  {
3395  if (have_error)
3396  {
3397  *have_error = true;
3398  return NULL;
3399  }
3400  ereport(ERROR,
3401  (errcode(ERRCODE_DIVISION_BY_ZERO),
3402  errmsg("division by zero")));
3403  }
3404  /* Inf % any nonzero = NaN */
3405  return make_result(&const_nan);
3406  }
3407  /* num2 must be [-]Inf; result is num1 regardless of sign of num2 */
3408  return duplicate_numeric(num1);
3409  }
3410 
3411  init_var_from_num(num1, &arg1);
3412  init_var_from_num(num2, &arg2);
3413 
3414  init_var(&result);
3415 
3416  /*
3417  * If "have_error" is provided, check for division by zero here
3418  */
3419  if (have_error && (arg2.ndigits == 0 || arg2.digits[0] == 0))
3420  {
3421  *have_error = true;
3422  return NULL;
3423  }
3424 
3425  mod_var(&arg1, &arg2, &result);
3426 
3427  res = make_result_opt_error(&result, NULL);
3428 
3429  free_var(&result);
3430 
3431  return res;
3432 }
3433 
3434 
3435 /*
3436  * numeric_inc() -
3437  *
3438  * Increment a number by one
3439  */
3440 Datum
3442 {
3443  Numeric num = PG_GETARG_NUMERIC(0);
3444  NumericVar arg;
3445  Numeric res;
3446 
3447  /*
3448  * Handle NaN and infinities
3449  */
3450  if (NUMERIC_IS_SPECIAL(num))
3452 
3453  /*
3454  * Compute the result and return it
3455  */
3456  init_var_from_num(num, &arg);
3457 
3458  add_var(&arg, &const_one, &arg);
3459 
3460  res = make_result(&arg);
3461 
3462  free_var(&arg);
3463 
3465 }
3466 
3467 
3468 /*
3469  * numeric_smaller() -
3470  *
3471  * Return the smaller of two numbers
3472  */
3473 Datum
3475 {
3476  Numeric num1 = PG_GETARG_NUMERIC(0);
3477  Numeric num2 = PG_GETARG_NUMERIC(1);
3478 
3479  /*
3480  * Use cmp_numerics so that this will agree with the comparison operators,
3481  * particularly as regards comparisons involving NaN.
3482  */
3483  if (cmp_numerics(num1, num2) < 0)
3484  PG_RETURN_NUMERIC(num1);
3485  else
3486  PG_RETURN_NUMERIC(num2);
3487 }
3488 
3489 
3490 /*
3491  * numeric_larger() -
3492  *
3493  * Return the larger of two numbers
3494  */
3495 Datum
3497 {
3498  Numeric num1 = PG_GETARG_NUMERIC(0);
3499  Numeric num2 = PG_GETARG_NUMERIC(1);
3500 
3501  /*
3502  * Use cmp_numerics so that this will agree with the comparison operators,
3503  * particularly as regards comparisons involving NaN.
3504  */
3505  if (cmp_numerics(num1, num2) > 0)
3506  PG_RETURN_NUMERIC(num1);
3507  else
3508  PG_RETURN_NUMERIC(num2);
3509 }
3510 
3511 
3512 /* ----------------------------------------------------------------------
3513  *
3514  * Advanced math functions
3515  *
3516  * ----------------------------------------------------------------------
3517  */
3518 
3519 /*
3520  * numeric_gcd() -
3521  *
3522  * Calculate the greatest common divisor of two numerics
3523  */
3524 Datum
3526 {
3527  Numeric num1 = PG_GETARG_NUMERIC(0);
3528  Numeric num2 = PG_GETARG_NUMERIC(1);
3529  NumericVar arg1;
3530  NumericVar arg2;
3531  NumericVar result;
3532  Numeric res;
3533 
3534  /*
3535  * Handle NaN and infinities: we consider the result to be NaN in all such
3536  * cases.
3537  */
3538  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3540 
3541  /*
3542  * Unpack the arguments
3543  */
3544  init_var_from_num(num1, &arg1);
3545  init_var_from_num(num2, &arg2);
3546 
3547  init_var(&result);
3548 
3549  /*
3550  * Find the GCD and return the result
3551  */
3552  gcd_var(&arg1, &arg2, &result);
3553 
3554  res = make_result(&result);
3555 
3556  free_var(&result);
3557 
3559 }
3560 
3561 
3562 /*
3563  * numeric_lcm() -
3564  *
3565  * Calculate the least common multiple of two numerics
3566  */
3567 Datum
3569 {
3570  Numeric num1 = PG_GETARG_NUMERIC(0);
3571  Numeric num2 = PG_GETARG_NUMERIC(1);
3572  NumericVar arg1;
3573  NumericVar arg2;
3574  NumericVar result;
3575  Numeric res;
3576 
3577  /*
3578  * Handle NaN and infinities: we consider the result to be NaN in all such
3579  * cases.
3580  */
3581  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3583 
3584  /*
3585  * Unpack the arguments
3586  */
3587  init_var_from_num(num1, &arg1);
3588  init_var_from_num(num2, &arg2);
3589 
3590  init_var(&result);
3591 
3592  /*
3593  * Compute the result using lcm(x, y) = abs(x / gcd(x, y) * y), returning
3594  * zero if either input is zero.
3595  *
3596  * Note that the division is guaranteed to be exact, returning an integer
3597  * result, so the LCM is an integral multiple of both x and y. A display
3598  * scale of Min(x.dscale, y.dscale) would be sufficient to represent it,
3599  * but as with other numeric functions, we choose to return a result whose
3600  * display scale is no smaller than either input.
3601  */
3602  if (arg1.ndigits == 0 || arg2.ndigits == 0)
3603  set_var_from_var(&const_zero, &result);
3604  else
3605  {
3606  gcd_var(&arg1, &arg2, &result);
3607  div_var(&arg1, &result, &result, 0, false);
3608  mul_var(&arg2, &result, &result, arg2.dscale);
3609  result.sign = NUMERIC_POS;
3610  }
3611 
3612  result.dscale = Max(arg1.dscale, arg2.dscale);
3613 
3614  res = make_result(&result);
3615 
3616  free_var(&result);
3617 
3619 }
3620 
3621 
3622 /*
3623  * numeric_fac()
3624  *
3625  * Compute factorial
3626  */
3627 Datum
3629 {
3630  int64 num = PG_GETARG_INT64(0);
3631  Numeric res;
3632  NumericVar fact;
3633  NumericVar result;
3634 
3635  if (num < 0)
3636  ereport(ERROR,
3637  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3638  errmsg("factorial of a negative number is undefined")));
3639  if (num <= 1)
3640  {
3643  }
3644  /* Fail immediately if the result would overflow */
3645  if (num > 32177)
3646  ereport(ERROR,
3647  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3648  errmsg("value overflows numeric format")));
3649 
3650  init_var(&fact);
3651  init_var(&result);
3652 
3653  int64_to_numericvar(num, &result);
3654 
3655  for (num = num - 1; num > 1; num--)
3656  {
3657  /* this loop can take awhile, so allow it to be interrupted */
3659 
3660  int64_to_numericvar(num, &fact);
3661 
3662  mul_var(&result, &fact, &result, 0);
3663  }
3664 
3665  res = make_result(&result);
3666 
3667  free_var(&fact);
3668  free_var(&result);
3669 
3671 }
3672 
3673 
3674 /*
3675  * numeric_sqrt() -
3676  *
3677  * Compute the square root of a numeric.
3678  */
3679 Datum
3681 {
3682  Numeric num = PG_GETARG_NUMERIC(0);
3683  Numeric res;
3684  NumericVar arg;
3685  NumericVar result;
3686  int sweight;
3687  int rscale;
3688 
3689  /*
3690  * Handle NaN and infinities
3691  */
3692  if (NUMERIC_IS_SPECIAL(num))
3693  {
3694  /* error should match that in sqrt_var() */
3695  if (NUMERIC_IS_NINF(num))
3696  ereport(ERROR,
3697  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3698  errmsg("cannot take square root of a negative number")));
3699  /* For NAN or PINF, just duplicate the input */
3701  }
3702 
3703  /*
3704  * Unpack the argument and determine the result scale. We choose a scale
3705  * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any
3706  * case not less than the input's dscale.
3707  */
3708  init_var_from_num(num, &arg);
3709 
3710  init_var(&result);
3711 
3712  /*
3713  * Assume the input was normalized, so arg.weight is accurate. The result
3714  * then has at least sweight = floor(arg.weight * DEC_DIGITS / 2 + 1)
3715  * digits before the decimal point. When DEC_DIGITS is even, we can save
3716  * a few cycles, since the division is exact and there is no need to round
3717  * towards negative infinity.
3718  */
3719 #if DEC_DIGITS == ((DEC_DIGITS / 2) * 2)
3720  sweight = arg.weight * DEC_DIGITS / 2 + 1;
3721 #else
3722  if (arg.weight >= 0)
3723  sweight = arg.weight * DEC_DIGITS / 2 + 1;
3724  else
3725  sweight = 1 - (1 - arg.weight * DEC_DIGITS) / 2;
3726 #endif
3727 
3728  rscale = NUMERIC_MIN_SIG_DIGITS - sweight;
3729  rscale = Max(rscale, arg.dscale);
3730  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
3731  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
3732 
3733  /*
3734  * Let sqrt_var() do the calculation and return the result.
3735  */
3736  sqrt_var(&arg, &result, rscale);
3737 
3738  res = make_result(&result);
3739 
3740  free_var(&result);
3741 
3743 }
3744 
3745 
3746 /*
3747  * numeric_exp() -
3748  *
3749  * Raise e to the power of x
3750  */
3751 Datum
3753 {
3754  Numeric num = PG_GETARG_NUMERIC(0);
3755  Numeric res;
3756  NumericVar arg;
3757  NumericVar result;
3758  int rscale;
3759  double val;
3760 
3761  /*
3762  * Handle NaN and infinities
3763  */
3764  if (NUMERIC_IS_SPECIAL(num))
3765  {
3766  /* Per POSIX, exp(-Inf) is zero */
3767  if (NUMERIC_IS_NINF(num))
3769  /* For NAN or PINF, just duplicate the input */
3771  }
3772 
3773  /*
3774  * Unpack the argument and determine the result scale. We choose a scale
3775  * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any
3776  * case not less than the input's dscale.
3777  */
3778  init_var_from_num(num, &arg);
3779 
3780  init_var(&result);
3781 
3782  /* convert input to float8, ignoring overflow */
3784 
3785  /*
3786  * log10(result) = num * log10(e), so this is approximately the decimal
3787  * weight of the result:
3788  */
3789  val *= 0.434294481903252;
3790 
3791  /* limit to something that won't cause integer overflow */
3794 
3795  rscale = NUMERIC_MIN_SIG_DIGITS - (int) val;
3796  rscale = Max(rscale, arg.dscale);
3797  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
3798  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
3799 
3800  /*
3801  * Let exp_var() do the calculation and return the result.
3802  */
3803  exp_var(&arg, &result, rscale);
3804 
3805  res = make_result(&result);
3806 
3807  free_var(&result);
3808 
3810 }
3811 
3812 
3813 /*
3814  * numeric_ln() -
3815  *
3816  * Compute the natural logarithm of x
3817  */
3818 Datum
3820 {
3821  Numeric num = PG_GETARG_NUMERIC(0);
3822  Numeric res;
3823  NumericVar arg;
3824  NumericVar result;
3825  int ln_dweight;
3826  int rscale;
3827 
3828  /*
3829  * Handle NaN and infinities
3830  */
3831  if (NUMERIC_IS_SPECIAL(num))
3832  {
3833  if (NUMERIC_IS_NINF(num))
3834  ereport(ERROR,
3835  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3836  errmsg("cannot take logarithm of a negative number")));
3837  /* For NAN or PINF, just duplicate the input */
3839  }
3840 
3841  init_var_from_num(num, &arg);
3842  init_var(&result);
3843 
3844  /* Estimated dweight of logarithm */
3845  ln_dweight = estimate_ln_dweight(&arg);
3846 
3847  rscale = NUMERIC_MIN_SIG_DIGITS - ln_dweight;
3848  rscale = Max(rscale, arg.dscale);
3849  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
3850  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
3851 
3852  ln_var(&arg, &result, rscale);
3853 
3854  res = make_result(&result);
3855 
3856  free_var(&result);
3857 
3859 }
3860 
3861 
3862 /*
3863  * numeric_log() -
3864  *
3865  * Compute the logarithm of x in a given base
3866  */
3867 Datum
3869 {
3870  Numeric num1 = PG_GETARG_NUMERIC(0);
3871  Numeric num2 = PG_GETARG_NUMERIC(1);
3872  Numeric res;
3873  NumericVar arg1;
3874  NumericVar arg2;
3875  NumericVar result;
3876 
3877  /*
3878  * Handle NaN and infinities
3879  */
3880  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3881  {
3882  int sign1,
3883  sign2;
3884 
3885  if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
3887  /* fail on negative inputs including -Inf, as log_var would */
3888  sign1 = numeric_sign_internal(num1);
3889  sign2 = numeric_sign_internal(num2);
3890  if (sign1 < 0 || sign2 < 0)
3891  ereport(ERROR,
3892  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3893  errmsg("cannot take logarithm of a negative number")));
3894  /* fail on zero inputs, as log_var would */
3895  if (sign1 == 0 || sign2 == 0)
3896  ereport(ERROR,
3897  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3898  errmsg("cannot take logarithm of zero")));
3899  if (NUMERIC_IS_PINF(num1))
3900  {
3901  /* log(Inf, Inf) reduces to Inf/Inf, so it's NaN */
3902  if (NUMERIC_IS_PINF(num2))
3904  /* log(Inf, finite-positive) is zero (we don't throw underflow) */
3906  }
3907  Assert(NUMERIC_IS_PINF(num2));
3908  /* log(finite-positive, Inf) is Inf */
3910  }
3911 
3912  /*
3913  * Initialize things
3914  */
3915  init_var_from_num(num1, &arg1);
3916  init_var_from_num(num2, &arg2);
3917  init_var(&result);
3918 
3919  /*
3920  * Call log_var() to compute and return the result; note it handles scale
3921  * selection itself.
3922  */
3923  log_var(&arg1, &arg2, &result);
3924 
3925  res = make_result(&result);
3926 
3927  free_var(&result);
3928 
3930 }
3931 
3932 
3933 /*
3934  * numeric_power() -
3935  *
3936  * Raise x to the power of y
3937  */
3938 Datum
3940 {
3941  Numeric num1 = PG_GETARG_NUMERIC(0);
3942  Numeric num2 = PG_GETARG_NUMERIC(1);
3943  Numeric res;
3944  NumericVar arg1;
3945  NumericVar arg2;
3946  NumericVar result;
3947  int sign1,
3948  sign2;
3949 
3950  /*
3951  * Handle NaN and infinities
3952  */
3953  if (NUMERIC_IS_SPECIAL(num1) || NUMERIC_IS_SPECIAL(num2))
3954  {
3955  /*
3956  * We follow the POSIX spec for pow(3), which says that NaN ^ 0 = 1,
3957  * and 1 ^ NaN = 1, while all other cases with NaN inputs yield NaN
3958  * (with no error).
3959  */
3960  if (NUMERIC_IS_NAN(num1))
3961  {
3962  if (!NUMERIC_IS_SPECIAL(num2))
3963  {
3964  init_var_from_num(num2, &arg2);
3965  if (cmp_var(&arg2, &const_zero) == 0)
3967  }
3969  }
3970  if (NUMERIC_IS_NAN(num2))
3971  {
3972  if (!NUMERIC_IS_SPECIAL(num1))
3973  {
3974  init_var_from_num(num1, &arg1);
3975  if (cmp_var(&arg1, &const_one) == 0)
3977  }
3979  }
3980  /* At least one input is infinite, but error rules still apply */
3981  sign1 = numeric_sign_internal(num1);
3982  sign2 = numeric_sign_internal(num2);
3983  if (sign1 == 0 && sign2 < 0)
3984  ereport(ERROR,
3985  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3986  errmsg("zero raised to a negative power is undefined")));
3987  if (sign1 < 0 && !numeric_is_integral(num2))
3988  ereport(ERROR,
3989  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3990  errmsg("a negative number raised to a non-integer power yields a complex result")));
3991 
3992  /*
3993  * POSIX gives this series of rules for pow(3) with infinite inputs:
3994  *
3995  * For any value of y, if x is +1, 1.0 shall be returned.
3996  */
3997  if (!NUMERIC_IS_SPECIAL(num1))
3998  {
3999  init_var_from_num(num1, &arg1);
4000  if (cmp_var(&arg1, &const_one) == 0)
4002  }
4003 
4004  /*
4005  * For any value of x, if y is [-]0, 1.0 shall be returned.
4006  */
4007  if (sign2 == 0)
4009 
4010  /*
4011  * For any odd integer value of y > 0, if x is [-]0, [-]0 shall be
4012  * returned. For y > 0 and not an odd integer, if x is [-]0, +0 shall
4013  * be returned. (Since we don't deal in minus zero, we need not
4014  * distinguish these two cases.)
4015  */
4016  if (sign1 == 0 && sign2 > 0)
4018 
4019  /*
4020  * If x is -1, and y is [-]Inf, 1.0 shall be returned.
4021  *
4022  * For |x| < 1, if y is -Inf, +Inf shall be returned.
4023  *
4024  * For |x| > 1, if y is -Inf, +0 shall be returned.
4025  *
4026  * For |x| < 1, if y is +Inf, +0 shall be returned.
4027  *
4028  * For |x| > 1, if y is +Inf, +Inf shall be returned.
4029  */
4030  if (NUMERIC_IS_INF(num2))
4031  {
4032  bool abs_x_gt_one;
4033 
4034  if (NUMERIC_IS_SPECIAL(num1))
4035  abs_x_gt_one = true; /* x is either Inf or -Inf */
4036  else
4037  {
4038  init_var_from_num(num1, &arg1);
4039  if (cmp_var(&arg1, &const_minus_one) == 0)
4041  arg1.sign = NUMERIC_POS; /* now arg1 = abs(x) */
4042  abs_x_gt_one = (cmp_var(&arg1, &const_one) > 0);
4043  }
4044  if (abs_x_gt_one == (sign2 > 0))
4046  else
4048  }
4049 
4050  /*
4051  * For y < 0, if x is +Inf, +0 shall be returned.
4052  *
4053  * For y > 0, if x is +Inf, +Inf shall be returned.
4054  */
4055  if (NUMERIC_IS_PINF(num1))
4056  {
4057  if (sign2 > 0)
4059  else
4061  }
4062 
4063  Assert(NUMERIC_IS_NINF(num1));
4064 
4065  /*
4066  * For y an odd integer < 0, if x is -Inf, -0 shall be returned. For
4067  * y < 0 and not an odd integer, if x is -Inf, +0 shall be returned.
4068  * (Again, we need not distinguish these two cases.)
4069  */
4070  if (sign2 < 0)
4072 
4073  /*
4074  * For y an odd integer > 0, if x is -Inf, -Inf shall be returned. For
4075  * y > 0 and not an odd integer, if x is -Inf, +Inf shall be returned.
4076  */
4077  init_var_from_num(num2, &arg2);
4078  if (arg2.ndigits > 0 && arg2.ndigits == arg2.weight + 1 &&
4079  (arg2.digits[arg2.ndigits - 1] & 1))
4081  else
4083  }
4084 
4085  /*
4086  * The SQL spec requires that we emit a particular SQLSTATE error code for
4087  * certain error conditions. Specifically, we don't return a
4088  * divide-by-zero error code for 0 ^ -1. Raising a negative number to a
4089  * non-integer power must produce the same error code, but that case is
4090  * handled in power_var().
4091  */
4092  sign1 = numeric_sign_internal(num1);
4093  sign2 = numeric_sign_internal(num2);
4094 
4095  if (sign1 == 0 && sign2 < 0)
4096  ereport(ERROR,
4097  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
4098  errmsg("zero raised to a negative power is undefined")));
4099 
4100  /*
4101  * Initialize things
4102  */
4103  init_var(&result);
4104  init_var_from_num(num1, &arg1);
4105  init_var_from_num(num2, &arg2);
4106 
4107  /*
4108  * Call power_var() to compute and return the result; note it handles
4109  * scale selection itself.
4110  */
4111  power_var(&arg1, &arg2, &result);
4112 
4113  res = make_result(&result);
4114 
4115  free_var(&result);
4116 
4118 }
4119 
4120 /*
4121  * numeric_scale() -
4122  *
4123  * Returns the scale, i.e. the count of decimal digits in the fractional part
4124  */
4125 Datum
4127 {
4128  Numeric num = PG_GETARG_NUMERIC(0);
4129 
4130  if (NUMERIC_IS_SPECIAL(num))
4131  PG_RETURN_NULL();
4132 
4134 }
4135 
4136 /*
4137  * Calculate minimum scale for value.
4138  */
4139 static int
4141 {
4142  int min_scale;
4143  int last_digit_pos;
4144 
4145  /*
4146  * Ordinarily, the input value will be "stripped" so that the last
4147  * NumericDigit is nonzero. But we don't want to get into an infinite
4148  * loop if it isn't, so explicitly find the last nonzero digit.
4149  */
4150  last_digit_pos = var->ndigits - 1;
4151  while (last_digit_pos >= 0 &&
4152  var->digits[last_digit_pos] == 0)
4153  last_digit_pos--;
4154 
4155  if (last_digit_pos >= 0)
4156  {
4157  /* compute min_scale assuming that last ndigit has no zeroes */
4158  min_scale = (last_digit_pos - var->weight) * DEC_DIGITS;
4159 
4160  /*
4161  * We could get a negative result if there are no digits after the
4162  * decimal point. In this case the min_scale must be zero.
4163  */
4164  if (min_scale > 0)
4165  {
4166  /*
4167  * Reduce min_scale if trailing digit(s) in last NumericDigit are
4168  * zero.
4169  */
4170  NumericDigit last_digit = var->digits[last_digit_pos];
4171 
4172  while (last_digit % 10 == 0)
4173  {
4174  min_scale--;
4175  last_digit /= 10;
4176  }
4177  }
4178  else
4179  min_scale = 0;
4180  }
4181  else
4182  min_scale = 0; /* result if input is zero */
4183 
4184  return min_scale;
4185 }
4186 
4187 /*
4188  * Returns minimum scale required to represent supplied value without loss.
4189  */
4190 Datum
4192 {
4193  Numeric num = PG_GETARG_NUMERIC(0);
4194  NumericVar arg;
4195  int min_scale;
4196 
4197  if (NUMERIC_IS_SPECIAL(num))
4198  PG_RETURN_NULL();
4199 
4200  init_var_from_num(num, &arg);
4201  min_scale = get_min_scale(&arg);
4202  free_var(&arg);
4203 
4204  PG_RETURN_INT32(min_scale);
4205 }
4206 
4207 /*
4208  * Reduce scale of numeric value to represent supplied value without loss.
4209  */
4210 Datum
4212 {
4213  Numeric num = PG_GETARG_NUMERIC(0);
4214  Numeric res;
4215  NumericVar result;
4216 
4217  if (NUMERIC_IS_SPECIAL(num))
4219 
4220  init_var_from_num(num, &result);
4221  result.dscale = get_min_scale(&result);
4222  res = make_result(&result);
4223  free_var(&result);
4224 
4226 }
4227 
4228 /*
4229  * Return a random numeric value in the range [rmin, rmax].
4230  */
4231 Numeric
4233 {
4234  NumericVar rmin_var;
4235  NumericVar rmax_var;
4236  NumericVar result;
4237  Numeric res;
4238 
4239  /* Range bounds must not be NaN/infinity */
4240  if (NUMERIC_IS_SPECIAL(rmin))
4241  {
4242  if (NUMERIC_IS_NAN(rmin))
4243  ereport(ERROR,
4244  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4245  errmsg("lower bound cannot be NaN"));
4246  else
4247  ereport(ERROR,
4248  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4249  errmsg("lower bound cannot be infinity"));
4250  }
4251  if (NUMERIC_IS_SPECIAL(rmax))
4252  {
4253  if (NUMERIC_IS_NAN(rmax))
4254  ereport(ERROR,
4255  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4256  errmsg("upper bound cannot be NaN"));
4257  else
4258  ereport(ERROR,
4259  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4260  errmsg("upper bound cannot be infinity"));
4261  }
4262 
4263  /* Return a random value in the range [rmin, rmax] */
4264  init_var_from_num(rmin, &rmin_var);
4265  init_var_from_num(rmax, &rmax_var);
4266 
4267  init_var(&result);
4268 
4269  random_var(state, &rmin_var, &rmax_var, &result);
4270 
4271  res = make_result(&result);
4272 
4273  free_var(&result);
4274 
4275  return res;
4276 }
4277 
4278 
4279 /* ----------------------------------------------------------------------
4280  *
4281  * Type conversion functions
4282  *
4283  * ----------------------------------------------------------------------
4284  */
4285 
4286 Numeric
4288 {
4289  Numeric res;
4290  NumericVar result;
4291 
4292  init_var(&result);
4293 
4294  int64_to_numericvar(val, &result);
4295 
4296  res = make_result(&result);
4297 
4298  free_var(&result);
4299 
4300  return res;
4301 }
4302 
4303 /*
4304  * Convert val1/(10**log10val2) to numeric. This is much faster than normal
4305  * numeric division.
4306  */
4307 Numeric
4308 int64_div_fast_to_numeric(int64 val1, int log10val2)
4309 {
4310  Numeric res;
4311  NumericVar result;
4312  int rscale;
4313  int w;
4314  int m;
4315 
4316  init_var(&result);
4317 
4318  /* result scale */
4319  rscale = log10val2 < 0 ? 0 : log10val2;
4320 
4321  /* how much to decrease the weight by */
4322  w = log10val2 / DEC_DIGITS;
4323  /* how much is left to divide by */
4324  m = log10val2 % DEC_DIGITS;
4325  if (m < 0)
4326  {
4327  m += DEC_DIGITS;
4328  w--;
4329  }
4330 
4331  /*
4332  * If there is anything left to divide by (10^m with 0 < m < DEC_DIGITS),
4333  * multiply the dividend by 10^(DEC_DIGITS - m), and shift the weight by
4334  * one more.
4335  */
4336  if (m > 0)
4337  {
4338 #if DEC_DIGITS == 4
4339  static const int pow10[] = {1, 10, 100, 1000};
4340 #elif DEC_DIGITS == 2
4341  static const int pow10[] = {1, 10};
4342 #elif DEC_DIGITS == 1
4343  static const int pow10[] = {1};
4344 #else
4345 #error unsupported NBASE
4346 #endif
4347  int64 factor = pow10[DEC_DIGITS - m];
4348  int64 new_val1;
4349 
4350  StaticAssertDecl(lengthof(pow10) == DEC_DIGITS, "mismatch with DEC_DIGITS");
4351 
4352  if (unlikely(pg_mul_s64_overflow(val1, factor, &new_val1)))
4353  {
4354 #ifdef HAVE_INT128
4355  /* do the multiplication using 128-bit integers */
4356  int128 tmp;
4357 
4358  tmp = (int128) val1 * (int128) factor;
4359 
4360  int128_to_numericvar(tmp, &result);
4361 #else
4362  /* do the multiplication using numerics */
4363  NumericVar tmp;
4364 
4365  init_var(&tmp);
4366 
4367  int64_to_numericvar(val1, &result);
4368  int64_to_numericvar(factor, &tmp);
4369  mul_var(&result, &tmp, &result, 0);
4370 
4371  free_var(&tmp);
4372 #endif
4373  }
4374  else
4375  int64_to_numericvar(new_val1, &result);
4376 
4377  w++;
4378  }
4379  else
4380  int64_to_numericvar(val1, &result);
4381 
4382  result.weight -= w;
4383  result.dscale = rscale;
4384 
4385  res = make_result(&result);
4386 
4387  free_var(&result);
4388 
4389  return res;
4390 }
4391 
4392 Datum
4394 {
4395  int32 val = PG_GETARG_INT32(0);
4396 
4398 }
4399 
4400 int32
4401 numeric_int4_opt_error(Numeric num, bool *have_error)
4402 {
4403  NumericVar x;
4404  int32 result;
4405 
4406  if (have_error)
4407  *have_error = false;
4408 
4409  if (NUMERIC_IS_SPECIAL(num))
4410  {
4411  if (have_error)
4412  {
4413  *have_error = true;
4414  return 0;
4415  }
4416  else
4417  {
4418  if (NUMERIC_IS_NAN(num))
4419  ereport(ERROR,
4420  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4421  errmsg("cannot convert NaN to %s", "integer")));
4422  else
4423  ereport(ERROR,
4424  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4425  errmsg("cannot convert infinity to %s", "integer")));
4426  }
4427  }
4428 
4429  /* Convert to variable format, then convert to int4 */
4430  init_var_from_num(num, &x);
4431 
4432  if (!numericvar_to_int32(&x, &result))
4433  {
4434  if (have_error)
4435  {
4436  *have_error = true;
4437  return 0;
4438  }
4439  else
4440  {
4441  ereport(ERROR,
4442  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4443  errmsg("integer out of range")));
4444  }
4445  }
4446 
4447  return result;
4448 }
4449 
4450 Datum
4452 {
4453  Numeric num = PG_GETARG_NUMERIC(0);
4454 
4456 }
4457 
4458 /*
4459  * Given a NumericVar, convert it to an int32. If the NumericVar
4460  * exceeds the range of an int32, false is returned, otherwise true is returned.
4461  * The input NumericVar is *not* free'd.
4462  */
4463 static bool
4465 {
4466  int64 val;
4467 
4468  if (!numericvar_to_int64(var, &val))
4469  return false;
4470 
4472  return false;
4473 
4474  /* Down-convert to int4 */
4475  *result = (int32) val;
4476 
4477  return true;
4478 }
4479 
4480 Datum
4482 {
4483  int64 val = PG_GETARG_INT64(0);
4484 
4486 }
4487 
4488 int64
4489 numeric_int8_opt_error(Numeric num, bool *have_error)
4490 {
4491  NumericVar x;
4492  int64 result;
4493 
4494  if (have_error)
4495  *have_error = false;
4496 
4497  if (NUMERIC_IS_SPECIAL(num))
4498  {
4499  if (have_error)
4500  {
4501  *have_error = true;
4502  return 0;
4503  }
4504  else
4505  {
4506  if (NUMERIC_IS_NAN(num))
4507  ereport(ERROR,
4508  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4509  errmsg("cannot convert NaN to %s", "bigint")));
4510  else
4511  ereport(ERROR,
4512  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4513  errmsg("cannot convert infinity to %s", "bigint")));
4514  }
4515  }
4516 
4517  /* Convert to variable format, then convert to int8 */
4518  init_var_from_num(num, &x);
4519 
4520  if (!numericvar_to_int64(&x, &result))
4521  {
4522  if (have_error)
4523  {
4524  *have_error = true;
4525  return 0;
4526  }
4527  else
4528  {
4529  ereport(ERROR,
4530  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4531  errmsg("bigint out of range")));
4532  }
4533  }
4534 
4535  return result;
4536 }
4537 
4538 Datum
4540 {
4541  Numeric num = PG_GETARG_NUMERIC(0);
4542 
4544 }
4545 
4546 
4547 Datum
4549 {
4550  int16 val = PG_GETARG_INT16(0);
4551 
4553 }
4554 
4555 
4556 Datum
4558 {
4559  Numeric num = PG_GETARG_NUMERIC(0);
4560  NumericVar x;
4561  int64 val;
4562  int16 result;
4563 
4564  if (NUMERIC_IS_SPECIAL(num))
4565  {
4566  if (NUMERIC_IS_NAN(num))
4567  ereport(ERROR,
4568  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4569  errmsg("cannot convert NaN to %s", "smallint")));
4570  else
4571  ereport(ERROR,
4572  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4573  errmsg("cannot convert infinity to %s", "smallint")));
4574  }
4575 
4576  /* Convert to variable format and thence to int8 */
4577  init_var_from_num(num, &x);
4578 
4579  if (!numericvar_to_int64(&x, &val))
4580  ereport(ERROR,
4581  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4582  errmsg("smallint out of range")));
4583 
4585  ereport(ERROR,
4586  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4587  errmsg("smallint out of range")));
4588 
4589  /* Down-convert to int2 */
4590  result = (int16) val;
4591 
4592  PG_RETURN_INT16(result);
4593 }
4594 
4595 
4596 Datum
4598 {
4600  Numeric res;
4601  NumericVar result;
4602  char buf[DBL_DIG + 100];
4603  const char *endptr;
4604 
4605  if (isnan(val))
4607 
4608  if (isinf(val))
4609  {
4610  if (val < 0)
4612  else
4614  }
4615 
4616  snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
4617 
4618  init_var(&result);
4619 
4620  /* Assume we need not worry about leading/trailing spaces */
4621  (void) set_var_from_str(buf, buf, &result, &endptr, NULL);
4622 
4623  res = make_result(&result);
4624 
4625  free_var(&result);
4626 
4628 }
4629 
4630 
4631 Datum
4633 {
4634  Numeric num = PG_GETARG_NUMERIC(0);
4635  char *tmp;
4636  Datum result;
4637 
4638  if (NUMERIC_IS_SPECIAL(num))
4639  {
4640  if (NUMERIC_IS_PINF(num))
4642  else if (NUMERIC_IS_NINF(num))
4644  else
4646  }
4647 
4649  NumericGetDatum(num)));
4650 
4652 
4653  pfree(tmp);
4654 
4655  PG_RETURN_DATUM(result);
4656 }
4657 
4658 
4659 /*
4660  * Convert numeric to float8; if out of range, return +/- HUGE_VAL
4661  *
4662  * (internal helper function, not directly callable from SQL)
4663  */
4664 Datum
4666 {
4667  Numeric num = PG_GETARG_NUMERIC(0);
4668  double val;
4669 
4670  if (NUMERIC_IS_SPECIAL(num))
4671  {
4672  if (NUMERIC_IS_PINF(num))
4673  val = HUGE_VAL;
4674  else if (NUMERIC_IS_NINF(num))
4675  val = -HUGE_VAL;
4676  else
4677  val = get_float8_nan();
4678  }
4679  else
4680  {
4681  NumericVar x;
4682 
4683  init_var_from_num(num, &x);
4685  }
4686 
4688 }
4689 
4690 Datum
4692 {
4694  Numeric res;
4695  NumericVar result;
4696  char buf[FLT_DIG + 100];
4697  const char *endptr;
4698 
4699  if (isnan(val))
4701 
4702  if (isinf(val))
4703  {
4704  if (val < 0)
4706  else
4708  }
4709 
4710  snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
4711 
4712  init_var(&result);
4713 
4714  /* Assume we need not worry about leading/trailing spaces */
4715  (void) set_var_from_str(buf, buf, &result, &endptr, NULL);
4716 
4717  res = make_result(&result);
4718 
4719  free_var(&result);
4720 
4722 }
4723 
4724 
4725 Datum
4727 {
4728  Numeric num = PG_GETARG_NUMERIC(0);
4729  char *tmp;
4730  Datum result;
4731 
4732  if (NUMERIC_IS_SPECIAL(num))
4733  {
4734  if (NUMERIC_IS_PINF(num))
4736  else if (NUMERIC_IS_NINF(num))
4738  else
4740  }
4741 
4743  NumericGetDatum(num)));
4744 
4746 
4747  pfree(tmp);
4748 
4749  PG_RETURN_DATUM(result);
4750 }
4751 
4752 
4753 Datum
4755 {
4756  Numeric num = PG_GETARG_NUMERIC(0);
4757  NumericVar x;
4758  XLogRecPtr result;
4759 
4760  if (NUMERIC_IS_SPECIAL(num))
4761  {
4762  if (NUMERIC_IS_NAN(num))
4763  ereport(ERROR,
4764  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4765  errmsg("cannot convert NaN to %s", "pg_lsn")));
4766  else
4767  ereport(ERROR,
4768  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4769  errmsg("cannot convert infinity to %s", "pg_lsn")));
4770  }
4771 
4772  /* Convert to variable format and thence to pg_lsn */
4773  init_var_from_num(num, &x);
4774 
4775  if (!numericvar_to_uint64(&x, (uint64 *) &result))
4776  ereport(ERROR,
4777  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4778  errmsg("pg_lsn out of range")));
4779 
4780  PG_RETURN_LSN(result);
4781 }
4782 
4783 
4784 /* ----------------------------------------------------------------------
4785  *
4786  * Aggregate functions
4787  *
4788  * The transition datatype for all these aggregates is declared as INTERNAL.
4789  * Actually, it's a pointer to a NumericAggState allocated in the aggregate
4790  * context. The digit buffers for the NumericVars will be there too.
4791  *
4792  * On platforms which support 128-bit integers some aggregates instead use a
4793  * 128-bit integer based transition datatype to speed up calculations.
4794  *
4795  * ----------------------------------------------------------------------
4796  */
4797 
4798 typedef struct NumericAggState
4799 {
4800  bool calcSumX2; /* if true, calculate sumX2 */
4801  MemoryContext agg_context; /* context we're calculating in */
4802  int64 N; /* count of processed numbers */
4803  NumericSumAccum sumX; /* sum of processed numbers */
4804  NumericSumAccum sumX2; /* sum of squares of processed numbers */
4805  int maxScale; /* maximum scale seen so far */
4806  int64 maxScaleCount; /* number of values seen with maximum scale */
4807  /* These counts are *not* included in N! Use NA_TOTAL_COUNT() as needed */
4808  int64 NaNcount; /* count of NaN values */
4809  int64 pInfcount; /* count of +Inf values */
4810  int64 nInfcount; /* count of -Inf values */
4812 
4813 #define NA_TOTAL_COUNT(na) \
4814  ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)
4815 
4816 /*
4817  * Prepare state data for a numeric aggregate function that needs to compute
4818  * sum, count and optionally sum of squares of the input.
4819  */
4820 static NumericAggState *
4821 makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
4822 {
4824  MemoryContext agg_context;
4825  MemoryContext old_context;
4826 
4827  if (!AggCheckCallContext(fcinfo, &agg_context))
4828  elog(ERROR, "aggregate function called in non-aggregate context");
4829 
4830  old_context = MemoryContextSwitchTo(agg_context);
4831 
4833  state->calcSumX2 = calcSumX2;
4834  state->agg_context = agg_context;
4835 
4836  MemoryContextSwitchTo(old_context);
4837 
4838  return state;
4839 }
4840 
4841 /*
4842  * Like makeNumericAggState(), but allocate the state in the current memory
4843  * context.
4844  */
4845 static NumericAggState *
4847 {
4849 
4851  state->calcSumX2 = calcSumX2;
4852  state->agg_context = CurrentMemoryContext;
4853 
4854  return state;
4855 }
4856 
4857 /*
4858  * Accumulate a new input value for numeric aggregate functions.
4859  */
4860 static void
4862 {
4863  NumericVar X;
4864  NumericVar X2;
4865  MemoryContext old_context;
4866 
4867  /* Count NaN/infinity inputs separately from all else */
4869  {
4870  if (NUMERIC_IS_PINF(newval))
4871  state->pInfcount++;
4872  else if (NUMERIC_IS_NINF(newval))
4873  state->nInfcount++;
4874  else
4875  state->NaNcount++;
4876  return;
4877  }
4878 
4879  /* load processed number in short-lived context */
4881 
4882  /*
4883  * Track the highest input dscale that we've seen, to support inverse
4884  * transitions (see do_numeric_discard).
4885  */
4886  if (X.dscale > state->maxScale)
4887  {
4888  state->maxScale = X.dscale;
4889  state->maxScaleCount = 1;
4890  }
4891  else if (X.dscale == state->maxScale)
4892  state->maxScaleCount++;
4893 
4894  /* if we need X^2, calculate that in short-lived context */
4895  if (state->calcSumX2)
4896  {
4897  init_var(&X2);
4898  mul_var(&X, &X, &X2, X.dscale * 2);
4899  }
4900 
4901  /* The rest of this needs to work in the aggregate context */
4902  old_context = MemoryContextSwitchTo(state->agg_context);
4903 
4904  state->N++;
4905 
4906  /* Accumulate sums */
4907  accum_sum_add(&(state->sumX), &X);
4908 
4909  if (state->calcSumX2)
4910  accum_sum_add(&(state->sumX2), &X2);
4911 
4912  MemoryContextSwitchTo(old_context);
4913 }
4914 
4915 /*
4916  * Attempt to remove an input value from the aggregated state.
4917  *
4918  * If the value cannot be removed then the function will return false; the
4919  * possible reasons for failing are described below.
4920  *
4921  * If we aggregate the values 1.01 and 2 then the result will be 3.01.
4922  * If we are then asked to un-aggregate the 1.01 then we must fail as we
4923  * won't be able to tell what the new aggregated value's dscale should be.
4924  * We don't want to return 2.00 (dscale = 2), since the sum's dscale would
4925  * have been zero if we'd really aggregated only 2.
4926  *
4927  * Note: alternatively, we could count the number of inputs with each possible
4928  * dscale (up to some sane limit). Not yet clear if it's worth the trouble.
4929  */
4930 static bool
4932 {
4933  NumericVar X;
4934  NumericVar X2;
4935  MemoryContext old_context;
4936 
4937  /* Count NaN/infinity inputs separately from all else */
4939  {
4940  if (NUMERIC_IS_PINF(newval))
4941  state->pInfcount--;
4942  else if (NUMERIC_IS_NINF(newval))
4943  state->nInfcount--;
4944  else
4945  state->NaNcount--;
4946  return true;
4947  }
4948 
4949  /* load processed number in short-lived context */
4951 
4952  /*
4953  * state->sumX's dscale is the maximum dscale of any of the inputs.
4954  * Removing the last input with that dscale would require us to recompute
4955  * the maximum dscale of the *remaining* inputs, which we cannot do unless
4956  * no more non-NaN inputs remain at all. So we report a failure instead,
4957  * and force the aggregation to be redone from scratch.
4958  */
4959  if (X.dscale == state->maxScale)
4960  {
4961  if (state->maxScaleCount > 1 || state->maxScale == 0)
4962  {
4963  /*
4964  * Some remaining inputs have same dscale, or dscale hasn't gotten
4965  * above zero anyway
4966  */
4967  state->maxScaleCount--;
4968  }
4969  else if (state->N == 1)
4970  {
4971  /* No remaining non-NaN inputs at all, so reset maxScale */
4972  state->maxScale = 0;
4973  state->maxScaleCount = 0;
4974  }
4975  else
4976  {
4977  /* Correct new maxScale is uncertain, must fail */
4978  return false;
4979  }
4980  }
4981 
4982  /* if we need X^2, calculate that in short-lived context */
4983  if (state->calcSumX2)
4984  {
4985  init_var(&X2);
4986  mul_var(&X, &X, &X2, X.dscale * 2);
4987  }
4988 
4989  /* The rest of this needs to work in the aggregate context */
4990  old_context = MemoryContextSwitchTo(state->agg_context);
4991 
4992  if (state->N-- > 1)
4993  {
4994  /* Negate X, to subtract it from the sum */
4995  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
4996  accum_sum_add(&(state->sumX), &X);
4997 
4998  if (state->calcSumX2)
4999  {
5000  /* Negate X^2. X^2 is always positive */
5001  X2.sign = NUMERIC_NEG;
5002  accum_sum_add(&(state->sumX2), &X2);
5003  }
5004  }
5005  else
5006  {
5007  /* Zero the sums */
5008  Assert(state->N == 0);
5009 
5010  accum_sum_reset(&state->sumX);
5011  if (state->calcSumX2)
5012  accum_sum_reset(&state->sumX2);
5013  }
5014 
5015  MemoryContextSwitchTo(old_context);
5016 
5017  return true;
5018 }
5019 
5020 /*
5021  * Generic transition function for numeric aggregates that require sumX2.
5022  */
5023 Datum
5025 {
5027 
5028  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
5029 
5030  /* Create the state data on the first call */
5031  if (state == NULL)
5032  state = makeNumericAggState(fcinfo, true);
5033 
5034  if (!PG_ARGISNULL(1))
5036 
5038 }
5039 
5040 /*
5041  * Generic combine function for numeric aggregates which require sumX2
5042  */
5043 Datum
5045 {
5046  NumericAggState *state1;
5047  NumericAggState *state2;
5048  MemoryContext agg_context;
5049  MemoryContext old_context;
5050 
5051  if (!AggCheckCallContext(fcinfo, &agg_context))
5052  elog(ERROR, "aggregate function called in non-aggregate context");
5053 
5054  state1 = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
5055  state2 = PG_ARGISNULL(1) ? NULL : (NumericAggState *) PG_GETARG_POINTER(1);
5056 
5057  if (state2 == NULL)
5058  PG_RETURN_POINTER(state1);
5059 
5060  /* manually copy all fields from state2 to state1 */
5061  if (state1 == NULL)
5062  {
5063  old_context = MemoryContextSwitchTo(agg_context);
5064 
5065  state1 = makeNumericAggStateCurrentContext(true);
5066  state1->N = state2->N;
5067  state1->NaNcount = state2->NaNcount;
5068  state1->pInfcount = state2->pInfcount;
5069  state1->nInfcount = state2->nInfcount;
5070  state1->maxScale = state2->maxScale;
5071  state1->maxScaleCount = state2->maxScaleCount;
5072 
5073  accum_sum_copy(&state1->sumX, &state2->sumX);
5074  accum_sum_copy(&state1->sumX2, &state2->sumX2);
5075 
5076  MemoryContextSwitchTo(old_context);
5077 
5078  PG_RETURN_POINTER(state1);
5079  }
5080 
5081  state1->N += state2->N;
5082  state1->NaNcount += state2->NaNcount;
5083  state1->pInfcount += state2->pInfcount;
5084  state1->nInfcount += state2->nInfcount;
5085 
5086  if (state2->N > 0)
5087  {
5088  /*
5089  * These are currently only needed for moving aggregates, but let's do
5090  * the right thing anyway...
5091  */
5092  if (state2->maxScale > state1->maxScale)
5093  {
5094  state1->maxScale = state2->maxScale;
5095  state1->maxScaleCount = state2->maxScaleCount;
5096  }
5097  else if (state2->maxScale == state1->maxScale)
5098  state1->maxScaleCount += state2->maxScaleCount;
5099 
5100  /* The rest of this needs to work in the aggregate context */
5101  old_context = MemoryContextSwitchTo(agg_context);
5102 
5103  /* Accumulate sums */
5104  accum_sum_combine(&state1->sumX, &state2->sumX);
5105  accum_sum_combine(&state1->sumX2, &state2->sumX2);
5106 
5107  MemoryContextSwitchTo(old_context);
5108  }
5109  PG_RETURN_POINTER(state1);
5110 }
5111 
5112 /*
5113  * Generic transition function for numeric aggregates that don't require sumX2.
5114  */
5115 Datum
5117 {
5119 
5120  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
5121 
5122  /* Create the state data on the first call */
5123  if (state == NULL)
5124  state = makeNumericAggState(fcinfo, false);
5125 
5126  if (!PG_ARGISNULL(1))
5128 
5130 }
5131 
5132 /*
5133  * Combine function for numeric aggregates which don't require sumX2
5134  */
5135 Datum
5137 {
5138  NumericAggState *state1;
5139  NumericAggState *state2;
5140  MemoryContext agg_context;
5141  MemoryContext old_context;
5142 
5143  if (!AggCheckCallContext(fcinfo, &agg_context))
5144  elog(ERROR, "aggregate function called in non-aggregate context");
5145 
5146  state1 = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
5147  state2 = PG_ARGISNULL(1) ? NULL : (NumericAggState *) PG_GETARG_POINTER(1);
5148 
5149  if (state2 == NULL)
5150  PG_RETURN_POINTER(state1);
5151 
5152  /* manually copy all fields from state2 to state1 */
5153  if (state1 == NULL)
5154  {
5155  old_context = MemoryContextSwitchTo(agg_context);
5156 
5157  state1 = makeNumericAggStateCurrentContext(false);
5158  state1->N = state2->N;
5159  state1->NaNcount = state2->NaNcount;
5160  state1->pInfcount = state2->pInfcount;
5161  state1->nInfcount = state2->nInfcount;
5162  state1->maxScale = state2->maxScale;
5163  state1->maxScaleCount = state2->maxScaleCount;
5164 
5165  accum_sum_copy(&state1->sumX, &state2->sumX);
5166 
5167  MemoryContextSwitchTo(old_context);
5168 
5169  PG_RETURN_POINTER(state1);
5170  }
5171 
5172  state1->N += state2->N;
5173  state1->NaNcount += state2->NaNcount;
5174  state1->pInfcount += state2->pInfcount;
5175  state1->nInfcount += state2->nInfcount;
5176 
5177  if (state2->N > 0)
5178  {
5179  /*
5180  * These are currently only needed for moving aggregates, but let's do
5181  * the right thing anyway...
5182  */
5183  if (state2->maxScale > state1->maxScale)
5184  {
5185  state1->maxScale = state2->maxScale;
5186  state1->maxScaleCount = state2->maxScaleCount;
5187  }
5188  else if (state2->maxScale == state1->maxScale)
5189  state1->maxScaleCount += state2->maxScaleCount;
5190 
5191  /* The rest of this needs to work in the aggregate context */
5192  old_context = MemoryContextSwitchTo(agg_context);
5193 
5194  /* Accumulate sums */
5195  accum_sum_combine(&state1->sumX, &state2->sumX);
5196 
5197  MemoryContextSwitchTo(old_context);
5198  }
5199  PG_RETURN_POINTER(state1);
5200 }
5201 
5202 /*
5203  * numeric_avg_serialize
5204  * Serialize NumericAggState for numeric aggregates that don't require
5205  * sumX2.
5206  */
5207 Datum
5209 {
5212  bytea *result;
5213  NumericVar tmp_var;
5214 
5215  /* Ensure we disallow calling when not in aggregate context */
5216  if (!AggCheckCallContext(fcinfo, NULL))
5217  elog(ERROR, "aggregate function called in non-aggregate context");
5218 
5220 
5221  init_var(&tmp_var);
5222 
5223  pq_begintypsend(&buf);
5224 
5225  /* N */
5226  pq_sendint64(&buf, state->N);
5227 
5228  /* sumX */
5229  accum_sum_final(&state->sumX, &tmp_var);
5230  numericvar_serialize(&buf, &tmp_var);
5231 
5232  /* maxScale */
5233  pq_sendint32(&buf, state->maxScale);
5234 
5235  /* maxScaleCount */
5236  pq_sendint64(&buf, state->maxScaleCount);
5237 
5238  /* NaNcount */
5239  pq_sendint64(&buf, state->NaNcount);
5240 
5241  /* pInfcount */
5242  pq_sendint64(&buf, state->pInfcount);
5243 
5244  /* nInfcount */
5245  pq_sendint64(&buf, state->nInfcount);
5246 
5247  result = pq_endtypsend(&buf);
5248 
5249  free_var(&tmp_var);
5250 
5251  PG_RETURN_BYTEA_P(result);
5252 }
5253 
5254 /*
5255  * numeric_avg_deserialize
5256  * Deserialize bytea into NumericAggState for numeric aggregates that
5257  * don't require sumX2.
5258  */
5259 Datum
5261 {
5262  bytea *sstate;
5263  NumericAggState *result;
5265  NumericVar tmp_var;
5266 
5267  if (!AggCheckCallContext(fcinfo, NULL))
5268  elog(ERROR, "aggregate function called in non-aggregate context");
5269 
5270  sstate = PG_GETARG_BYTEA_PP(0);
5271 
5272  init_var(&tmp_var);
5273 
5274  /*
5275  * Initialize a StringInfo so that we can "receive" it using the standard
5276  * recv-function infrastructure.
5277  */
5279  VARSIZE_ANY_EXHDR(sstate));
5280 
5281  result = makeNumericAggStateCurrentContext(false);
5282 
5283  /* N */
5284  result->N = pq_getmsgint64(&buf);
5285 
5286  /* sumX */
5287  numericvar_deserialize(&buf, &tmp_var);
5288  accum_sum_add(&(result->sumX), &tmp_var);
5289 
5290  /* maxScale */
5291  result->maxScale = pq_getmsgint(&buf, 4);
5292 
5293  /* maxScaleCount */
5294  result->maxScaleCount = pq_getmsgint64(&buf);
5295 
5296  /* NaNcount */
5297  result->NaNcount = pq_getmsgint64(&buf);
5298 
5299  /* pInfcount */
5300  result->pInfcount = pq_getmsgint64(&buf);
5301 
5302  /* nInfcount */
5303  result->nInfcount = pq_getmsgint64(&buf);
5304 
5305  pq_getmsgend(&buf);
5306 
5307  free_var(&tmp_var);
5308 
5309  PG_RETURN_POINTER(result);
5310 }
5311 
5312 /*
5313  * numeric_serialize
5314  * Serialization function for NumericAggState for numeric aggregates that
5315  * require sumX2.
5316  */
5317 Datum
5319 {
5322  bytea *result;
5323  NumericVar tmp_var;
5324 
5325  /* Ensure we disallow calling when not in aggregate context */
5326  if (!AggCheckCallContext(fcinfo, NULL))
5327  elog(ERROR, "aggregate function called in non-aggregate context");
5328 
5330 
5331  init_var(&tmp_var);
5332 
5333  pq_begintypsend(&buf);
5334 
5335  /* N */
5336  pq_sendint64(&buf, state->N);
5337 
5338  /* sumX */
5339  accum_sum_final(&state->sumX, &tmp_var);
5340  numericvar_serialize(&buf, &tmp_var);
5341 
5342  /* sumX2 */
5343  accum_sum_final(&state->sumX2, &tmp_var);
5344  numericvar_serialize(&buf, &tmp_var);
5345 
5346  /* maxScale */
5347  pq_sendint32(&buf, state->maxScale);
5348 
5349  /* maxScaleCount */
5350  pq_sendint64(&buf, state->maxScaleCount);
5351 
5352  /* NaNcount */
5353  pq_sendint64(&buf, state->NaNcount);
5354 
5355  /* pInfcount */
5356  pq_sendint64(&buf, state->pInfcount);
5357 
5358  /* nInfcount */
5359  pq_sendint64(&buf, state->nInfcount);
5360 
5361  result = pq_endtypsend(&buf);
5362 
5363  free_var(&tmp_var);
5364 
5365  PG_RETURN_BYTEA_P(result);
5366 }
5367 
5368 /*
5369  * numeric_deserialize
5370  * Deserialization function for NumericAggState for numeric aggregates that
5371  * require sumX2.
5372  */
5373 Datum
5375 {
5376  bytea *sstate;
5377  NumericAggState *result;
5379  NumericVar tmp_var;
5380 
5381  if (!AggCheckCallContext(fcinfo, NULL))
5382  elog(ERROR, "aggregate function called in non-aggregate context");
5383 
5384  sstate = PG_GETARG_BYTEA_PP(0);
5385 
5386  init_var(&tmp_var);
5387 
5388  /*
5389  * Initialize a StringInfo so that we can "receive" it using the standard
5390  * recv-function infrastructure.
5391  */
5393  VARSIZE_ANY_EXHDR(sstate));
5394 
5395  result = makeNumericAggStateCurrentContext(false);
5396 
5397  /* N */
5398  result->N = pq_getmsgint64(&buf);
5399 
5400  /* sumX */
5401  numericvar_deserialize(&buf, &tmp_var);
5402  accum_sum_add(&(result->sumX), &tmp_var);
5403 
5404  /* sumX2 */
5405  numericvar_deserialize(&buf, &tmp_var);
5406  accum_sum_add(&(result->sumX2), &tmp_var);
5407 
5408  /* maxScale */
5409  result->maxScale = pq_getmsgint(&buf, 4);
5410 
5411  /* maxScaleCount */
5412  result->maxScaleCount = pq_getmsgint64(&buf);
5413 
5414  /* NaNcount */
5415  result->NaNcount = pq_getmsgint64(&buf);
5416 
5417  /* pInfcount */
5418  result->pInfcount = pq_getmsgint64(&buf);
5419 
5420  /* nInfcount */
5421  result->nInfcount = pq_getmsgint64(&buf);
5422 
5423  pq_getmsgend(&buf);
5424 
5425  free_var(&tmp_var);
5426 
5427  PG_RETURN_POINTER(result);
5428 }
5429 
5430 /*
5431  * Generic inverse transition function for numeric aggregates
5432  * (with or without requirement for X^2).
5433  */
5434 Datum
5436 {
5438 
5439  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
5440 
5441  /* Should not get here with no state */
5442  if (state == NULL)
5443  elog(ERROR, "numeric_accum_inv called with NULL state");
5444 
5445  if (!PG_ARGISNULL(1))
5446  {
5447  /* If we fail to perform the inverse transition, return NULL */
5449  PG_RETURN_NULL();
5450  }
5451 
5453 }
5454 
5455 
5456 /*
5457  * Integer data types in general use Numeric accumulators to share code
5458  * and avoid risk of overflow.
5459  *
5460  * However for performance reasons optimized special-purpose accumulator
5461  * routines are used when possible.
5462  *
5463  * On platforms with 128-bit integer support, the 128-bit routines will be
5464  * used when sum(X) or sum(X*X) fit into 128-bit.
5465  *
5466  * For 16 and 32 bit inputs, the N and sum(X) fit into 64-bit so the 64-bit
5467  * accumulators will be used for SUM and AVG of these data types.
5468  */
5469 
5470 #ifdef HAVE_INT128
5471 typedef struct Int128AggState
5472 {
5473  bool calcSumX2; /* if true, calculate sumX2 */
5474  int64 N; /* count of processed numbers */
5475  int128 sumX; /* sum of processed numbers */
5476  int128 sumX2; /* sum of squares of processed numbers */
5477 } Int128AggState;
5478 
5479 /*
5480  * Prepare state data for a 128-bit aggregate function that needs to compute
5481  * sum, count and optionally sum of squares of the input.
5482  */
5483 static Int128AggState *
5484 makeInt128AggState(FunctionCallInfo fcinfo, bool calcSumX2)
5485 {
5486  Int128AggState *state;
5487  MemoryContext agg_context;
5488  MemoryContext old_context;
5489 
5490  if (!AggCheckCallContext(fcinfo, &agg_context))
5491  elog(ERROR, "aggregate function called in non-aggregate context");
5492 
5493  old_context = MemoryContextSwitchTo(agg_context);
5494 
5495  state = (Int128AggState *) palloc0(sizeof(Int128AggState));
5496  state->calcSumX2 = calcSumX2;
5497 
5498  MemoryContextSwitchTo(old_context);
5499 
5500  return state;
5501 }
5502 
5503 /*
5504  * Like makeInt128AggState(), but allocate the state in the current memory
5505  * context.
5506  */
5507 static Int128AggState *
5508 makeInt128AggStateCurrentContext(bool calcSumX2)
5509 {
5510  Int128AggState *state;
5511 
5512  state = (Int128AggState *) palloc0(sizeof(Int128AggState));
5513  state->calcSumX2 = calcSumX2;
5514 
5515  return state;
5516 }
5517 
5518 /*
5519  * Accumulate a new input value for 128-bit aggregate functions.
5520  */
5521 static void
5522 do_int128_accum(Int128AggState *state, int128 newval)
5523 {
5524  if (state->calcSumX2)
5525  state->sumX2 += newval * newval;
5526 
5527  state->sumX += newval;
5528  state->N++;
5529 }
5530 
5531 /*
5532  * Remove an input value from the aggregated state.
5533  */
5534 static void
5535 do_int128_discard(Int128AggState *state, int128 newval)
5536 {
5537  if (state->calcSumX2)
5538  state->sumX2 -= newval * newval;
5539 
5540  state->sumX -= newval;
5541  state->N--;
5542 }
5543 
5544 typedef Int128AggState PolyNumAggState;
5545 #define makePolyNumAggState makeInt128AggState
5546 #define makePolyNumAggStateCurrentContext makeInt128AggStateCurrentContext
5547 #else
5549 #define makePolyNumAggState makeNumericAggState
5550 #define makePolyNumAggStateCurrentContext makeNumericAggStateCurrentContext
5551 #endif
5552 
5553 Datum
5555 {
5557 
5558  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5559 
5560  /* Create the state data on the first call */
5561  if (state == NULL)
5562  state = makePolyNumAggState(fcinfo, true);
5563 
5564  if (!PG_ARGISNULL(1))
5565  {
5566 #ifdef HAVE_INT128
5567  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
5568 #else
5570 #endif
5571  }
5572 
5574 }
5575 
5576 Datum
5578 {
5580 
5581  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5582 
5583  /* Create the state data on the first call */
5584  if (state == NULL)
5585  state = makePolyNumAggState(fcinfo, true);
5586 
5587  if (!PG_ARGISNULL(1))
5588  {
5589 #ifdef HAVE_INT128
5590  do_int128_accum(state, (int128) PG_GETARG_INT32(1));
5591 #else
5593 #endif
5594  }
5595 
5597 }
5598 
5599 Datum
5601 {
5603 
5604  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
5605 
5606  /* Create the state data on the first call */
5607  if (state == NULL)
5608  state = makeNumericAggState(fcinfo, true);
5609 
5610  if (!PG_ARGISNULL(1))
5612 
5614 }
5615 
5616 /*
5617  * Combine function for numeric aggregates which require sumX2
5618  */
5619 Datum
5621 {
5622  PolyNumAggState *state1;
5623  PolyNumAggState *state2;
5624  MemoryContext agg_context;
5625  MemoryContext old_context;
5626 
5627  if (!AggCheckCallContext(fcinfo, &agg_context))
5628  elog(ERROR, "aggregate function called in non-aggregate context");
5629 
5630  state1 = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5631  state2 = PG_ARGISNULL(1) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(1);
5632 
5633  if (state2 == NULL)
5634  PG_RETURN_POINTER(state1);
5635 
5636  /* manually copy all fields from state2 to state1 */
5637  if (state1 == NULL)
5638  {
5639  old_context = MemoryContextSwitchTo(agg_context);
5640 
5641  state1 = makePolyNumAggState(fcinfo, true);
5642  state1->N = state2->N;
5643 
5644 #ifdef HAVE_INT128
5645  state1->sumX = state2->sumX;
5646  state1->sumX2 = state2->sumX2;
5647 #else
5648  accum_sum_copy(&state1->sumX, &state2->sumX);
5649  accum_sum_copy(&state1->sumX2, &state2->sumX2);
5650 #endif
5651 
5652  MemoryContextSwitchTo(old_context);
5653 
5654  PG_RETURN_POINTER(state1);
5655  }
5656 
5657  if (state2->N > 0)
5658  {
5659  state1->N += state2->N;
5660 
5661 #ifdef HAVE_INT128
5662  state1->sumX += state2->sumX;
5663  state1->sumX2 += state2->sumX2;
5664 #else
5665  /* The rest of this needs to work in the aggregate context */
5666  old_context = MemoryContextSwitchTo(agg_context);
5667 
5668  /* Accumulate sums */
5669  accum_sum_combine(&state1->sumX, &state2->sumX);
5670  accum_sum_combine(&state1->sumX2, &state2->sumX2);
5671 
5672  MemoryContextSwitchTo(old_context);
5673 #endif
5674 
5675  }
5676  PG_RETURN_POINTER(state1);
5677 }
5678 
5679 /*
5680  * numeric_poly_serialize
5681  * Serialize PolyNumAggState into bytea for aggregate functions which
5682  * require sumX2.
5683  */
5684 Datum
5686 {
5689  bytea *result;
5690  NumericVar tmp_var;
5691 
5692  /* Ensure we disallow calling when not in aggregate context */
5693  if (!AggCheckCallContext(fcinfo, NULL))
5694  elog(ERROR, "aggregate function called in non-aggregate context");
5695 
5697 
5698  /*
5699  * If the platform supports int128 then sumX and sumX2 will be a 128 bit
5700  * integer type. Here we'll convert that into a numeric type so that the
5701  * combine state is in the same format for both int128 enabled machines
5702  * and machines which don't support that type. The logic here is that one
5703  * day we might like to send these over to another server for further
5704  * processing and we want a standard format to work with.
5705  */
5706 
5707  init_var(&tmp_var);
5708 
5709  pq_begintypsend(&buf);
5710 
5711  /* N */
5712  pq_sendint64(&buf, state->N);
5713 
5714  /* sumX */
5715 #ifdef HAVE_INT128
5716  int128_to_numericvar(state->sumX, &tmp_var);
5717 #else
5718  accum_sum_final(&state->sumX, &tmp_var);
5719 #endif
5720  numericvar_serialize(&buf, &tmp_var);
5721 
5722  /* sumX2 */
5723 #ifdef HAVE_INT128
5724  int128_to_numericvar(state->sumX2, &tmp_var);
5725 #else
5726  accum_sum_final(&state->sumX2, &tmp_var);
5727 #endif
5728  numericvar_serialize(&buf, &tmp_var);
5729 
5730  result = pq_endtypsend(&buf);
5731 
5732  free_var(&tmp_var);
5733 
5734  PG_RETURN_BYTEA_P(result);
5735 }
5736 
5737 /*
5738  * numeric_poly_deserialize
5739  * Deserialize PolyNumAggState from bytea for aggregate functions which
5740  * require sumX2.
5741  */
5742 Datum
5744 {
5745  bytea *sstate;
5746  PolyNumAggState *result;
5748  NumericVar tmp_var;
5749 
5750  if (!AggCheckCallContext(fcinfo, NULL))
5751  elog(ERROR, "aggregate function called in non-aggregate context");
5752 
5753  sstate = PG_GETARG_BYTEA_PP(0);
5754 
5755  init_var(&tmp_var);
5756 
5757  /*
5758  * Initialize a StringInfo so that we can "receive" it using the standard
5759  * recv-function infrastructure.
5760  */
5762  VARSIZE_ANY_EXHDR(sstate));
5763 
5764  result = makePolyNumAggStateCurrentContext(false);
5765 
5766  /* N */
5767  result->N = pq_getmsgint64(&buf);
5768 
5769  /* sumX */
5770  numericvar_deserialize(&buf, &tmp_var);
5771 #ifdef HAVE_INT128
5772  numericvar_to_int128(&tmp_var, &result->sumX);
5773 #else
5774  accum_sum_add(&result->sumX, &tmp_var);
5775 #endif
5776 
5777  /* sumX2 */
5778  numericvar_deserialize(&buf, &tmp_var);
5779 #ifdef HAVE_INT128
5780  numericvar_to_int128(&tmp_var, &result->sumX2);
5781 #else
5782  accum_sum_add(&result->sumX2, &tmp_var);
5783 #endif
5784 
5785  pq_getmsgend(&buf);
5786 
5787  free_var(&tmp_var);
5788 
5789  PG_RETURN_POINTER(result);
5790 }
5791 
5792 /*
5793  * Transition function for int8 input when we don't need sumX2.
5794  */
5795 Datum
5797 {
5799 
5800  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5801 
5802  /* Create the state data on the first call */
5803  if (state == NULL)
5804  state = makePolyNumAggState(fcinfo, false);
5805 
5806  if (!PG_ARGISNULL(1))
5807  {
5808 #ifdef HAVE_INT128
5809  do_int128_accum(state, (int128) PG_GETARG_INT64(1));
5810 #else
5812 #endif
5813  }
5814 
5816 }
5817 
5818 /*
5819  * Combine function for PolyNumAggState for aggregates which don't require
5820  * sumX2
5821  */
5822 Datum
5824 {
5825  PolyNumAggState *state1;
5826  PolyNumAggState *state2;
5827  MemoryContext agg_context;
5828  MemoryContext old_context;
5829 
5830  if (!AggCheckCallContext(fcinfo, &agg_context))
5831  elog(ERROR, "aggregate function called in non-aggregate context");
5832 
5833  state1 = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5834  state2 = PG_ARGISNULL(1) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(1);
5835 
5836  if (state2 == NULL)
5837  PG_RETURN_POINTER(state1);
5838 
5839  /* manually copy all fields from state2 to state1 */
5840  if (state1 == NULL)
5841  {
5842  old_context = MemoryContextSwitchTo(agg_context);
5843 
5844  state1 = makePolyNumAggState(fcinfo, false);
5845  state1->N = state2->N;
5846 
5847 #ifdef HAVE_INT128
5848  state1->sumX = state2->sumX;
5849 #else
5850  accum_sum_copy(&state1->sumX, &state2->sumX);
5851 #endif
5852  MemoryContextSwitchTo(old_context);
5853 
5854  PG_RETURN_POINTER(state1);
5855  }
5856 
5857  if (state2->N > 0)
5858  {
5859  state1->N += state2->N;
5860 
5861 #ifdef HAVE_INT128
5862  state1->sumX += state2->sumX;
5863 #else
5864  /* The rest of this needs to work in the aggregate context */
5865  old_context = MemoryContextSwitchTo(agg_context);
5866 
5867  /* Accumulate sums */
5868  accum_sum_combine(&state1->sumX, &state2->sumX);
5869 
5870  MemoryContextSwitchTo(old_context);
5871 #endif
5872 
5873  }
5874  PG_RETURN_POINTER(state1);
5875 }
5876 
5877 /*
5878  * int8_avg_serialize
5879  * Serialize PolyNumAggState into bytea using the standard
5880  * recv-function infrastructure.
5881  */
5882 Datum
5884 {
5887  bytea *result;
5888  NumericVar tmp_var;
5889 
5890  /* Ensure we disallow calling when not in aggregate context */
5891  if (!AggCheckCallContext(fcinfo, NULL))
5892  elog(ERROR, "aggregate function called in non-aggregate context");
5893 
5895 
5896  /*
5897  * If the platform supports int128 then sumX will be a 128 integer type.
5898  * Here we'll convert that into a numeric type so that the combine state
5899  * is in the same format for both int128 enabled machines and machines
5900  * which don't support that type. The logic here is that one day we might
5901  * like to send these over to another server for further processing and we
5902  * want a standard format to work with.
5903  */
5904 
5905  init_var(&tmp_var);
5906 
5907  pq_begintypsend(&buf);
5908 
5909  /* N */
5910  pq_sendint64(&buf, state->N);
5911 
5912  /* sumX */
5913 #ifdef HAVE_INT128
5914  int128_to_numericvar(state->sumX, &tmp_var);
5915 #else
5916  accum_sum_final(&state->sumX, &tmp_var);
5917 #endif
5918  numericvar_serialize(&buf, &tmp_var);
5919 
5920  result = pq_endtypsend(&buf);
5921 
5922  free_var(&tmp_var);
5923 
5924  PG_RETURN_BYTEA_P(result);
5925 }
5926 
5927 /*
5928  * int8_avg_deserialize
5929  * Deserialize bytea back into PolyNumAggState.
5930  */
5931 Datum
5933 {
5934  bytea *sstate;
5935  PolyNumAggState *result;
5937  NumericVar tmp_var;
5938 
5939  if (!AggCheckCallContext(fcinfo, NULL))
5940  elog(ERROR, "aggregate function called in non-aggregate context");
5941 
5942  sstate = PG_GETARG_BYTEA_PP(0);
5943 
5944  init_var(&tmp_var);
5945 
5946  /*
5947  * Initialize a StringInfo so that we can "receive" it using the standard
5948  * recv-function infrastructure.
5949  */
5951  VARSIZE_ANY_EXHDR(sstate));
5952 
5953  result = makePolyNumAggStateCurrentContext(false);
5954 
5955  /* N */
5956  result->N = pq_getmsgint64(&buf);
5957 
5958  /* sumX */
5959  numericvar_deserialize(&buf, &tmp_var);
5960 #ifdef HAVE_INT128
5961  numericvar_to_int128(&tmp_var, &result->sumX);
5962 #else
5963  accum_sum_add(&result->sumX, &tmp_var);
5964 #endif
5965 
5966  pq_getmsgend(&buf);
5967 
5968  free_var(&tmp_var);
5969 
5970  PG_RETURN_POINTER(result);
5971 }
5972 
5973 /*
5974  * Inverse transition functions to go with the above.
5975  */
5976 
5977 Datum
5979 {
5981 
5982  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5983 
5984  /* Should not get here with no state */
5985  if (state == NULL)
5986  elog(ERROR, "int2_accum_inv called with NULL state");
5987 
5988  if (!PG_ARGISNULL(1))
5989  {
5990 #ifdef HAVE_INT128
5991  do_int128_discard(state, (int128) PG_GETARG_INT16(1));
5992 #else
5993  /* Should never fail, all inputs have dscale 0 */
5995  elog(ERROR, "do_numeric_discard failed unexpectedly");
5996 #endif
5997  }
5998 
6000 }
6001 
6002 Datum
6004 {
6006 
6007  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6008 
6009  /* Should not get here with no state */
6010  if (state == NULL)
6011  elog(ERROR, "int4_accum_inv called with NULL state");
6012 
6013  if (!PG_ARGISNULL(1))
6014  {
6015 #ifdef HAVE_INT128
6016  do_int128_discard(state, (int128) PG_GETARG_INT32(1));
6017 #else
6018  /* Should never fail, all inputs have dscale 0 */
6020  elog(ERROR, "do_numeric_discard failed unexpectedly");
6021 #endif
6022  }
6023 
6025 }
6026 
6027 Datum
6029 {
6031 
6032  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6033 
6034  /* Should not get here with no state */
6035  if (state == NULL)
6036  elog(ERROR, "int8_accum_inv called with NULL state");
6037 
6038  if (!PG_ARGISNULL(1))
6039  {
6040  /* Should never fail, all inputs have dscale 0 */
6042  elog(ERROR, "do_numeric_discard failed unexpectedly");
6043  }
6044 
6046 }
6047 
6048 Datum
6050 {
6052 
6053  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6054 
6055  /* Should not get here with no state */
6056  if (state == NULL)
6057  elog(ERROR, "int8_avg_accum_inv called with NULL state");
6058 
6059  if (!PG_ARGISNULL(1))
6060  {
6061 #ifdef HAVE_INT128
6062  do_int128_discard(state, (int128) PG_GETARG_INT64(1));
6063 #else
6064  /* Should never fail, all inputs have dscale 0 */
6066  elog(ERROR, "do_numeric_discard failed unexpectedly");
6067 #endif
6068  }
6069 
6071 }
6072 
6073 Datum
6075 {
6076 #ifdef HAVE_INT128
6078  Numeric res;
6079  NumericVar result;
6080 
6081  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6082 
6083  /* If there were no non-null inputs, return NULL */
6084  if (state == NULL || state->N == 0)
6085  PG_RETURN_NULL();
6086 
6087  init_var(&result);
6088 
6089  int128_to_numericvar(state->sumX, &result);
6090 
6091  res = make_result(&result);
6092 
6093  free_var(&result);
6094 
6096 #else
6097  return numeric_sum(fcinfo);
6098 #endif
6099 }
6100 
6101 Datum
6103 {
6104 #ifdef HAVE_INT128
6106  NumericVar result;
6107  Datum countd,
6108  sumd;
6109 
6110  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6111 
6112  /* If there were no non-null inputs, return NULL */
6113  if (state == NULL || state->N == 0)
6114  PG_RETURN_NULL();
6115 
6116  init_var(&result);
6117 
6118  int128_to_numericvar(state->sumX, &result);
6119 
6120  countd = NumericGetDatum(int64_to_numeric(state->N));
6121  sumd = NumericGetDatum(make_result(&result));
6122 
6123  free_var(&result);
6124 
6126 #else
6127  return numeric_avg(fcinfo);
6128 #endif
6129 }
6130 
6131 Datum
6133 {
6135  Datum N_datum;
6136  Datum sumX_datum;
6137  NumericVar sumX_var;
6138 
6139  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6140 
6141  /* If there were no non-null inputs, return NULL */
6142  if (state == NULL || NA_TOTAL_COUNT(state) == 0)
6143  PG_RETURN_NULL();
6144 
6145  if (state->NaNcount > 0) /* there was at least one NaN input */
6147 
6148  /* adding plus and minus infinities gives NaN */
6149  if (state->pInfcount > 0 && state->nInfcount > 0)
6151  if (state->pInfcount > 0)
6153  if (state->nInfcount > 0)
6155 
6156  N_datum = NumericGetDatum(int64_to_numeric(state->N));
6157 
6158  init_var(&sumX_var);
6159  accum_sum_final(&state->sumX, &sumX_var);
6160  sumX_datum = NumericGetDatum(make_result(&sumX_var));
6161  free_var(&sumX_var);
6162 
6163  PG_RETURN_DATUM(DirectFunctionCall2(numeric_div, sumX_datum, N_datum));
6164 }
6165 
6166 Datum
6168 {
6170  NumericVar sumX_var;
6171  Numeric result;
6172 
6173  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6174 
6175  /* If there were no non-null inputs, return NULL */
6176  if (state == NULL || NA_TOTAL_COUNT(state) == 0)
6177  PG_RETURN_NULL();
6178 
6179  if (state->NaNcount > 0) /* there was at least one NaN input */
6181 
6182  /* adding plus and minus infinities gives NaN */
6183  if (state->pInfcount > 0 && state->nInfcount > 0)
6185  if (state->pInfcount > 0)
6187  if (state->nInfcount > 0)
6189 
6190  init_var(&sumX_var);
6191  accum_sum_final(&state->sumX, &sumX_var);
6192  result = make_result(&sumX_var);
6193  free_var(&sumX_var);
6194 
6195  PG_RETURN_NUMERIC(result);
6196 }
6197 
6198 /*
6199  * Workhorse routine for the standard deviance and variance
6200  * aggregates. 'state' is aggregate's transition state.
6201  * 'variance' specifies whether we should calculate the
6202  * variance or the standard deviation. 'sample' indicates whether the
6203  * caller is interested in the sample or the population
6204  * variance/stddev.
6205  *
6206  * If appropriate variance statistic is undefined for the input,
6207  * *is_null is set to true and NULL is returned.
6208  */
6209 static Numeric
6211  bool variance, bool sample,
6212  bool *is_null)
6213 {
6214  Numeric res;
6215  NumericVar vN,
6216  vsumX,
6217  vsumX2,
6218  vNminus1;
6219  int64 totCount;
6220  int rscale;
6221 
6222  /*
6223  * Sample stddev and variance are undefined when N <= 1; population stddev
6224  * is undefined when N == 0. Return NULL in either case (note that NaNs
6225  * and infinities count as normal inputs for this purpose).
6226  */
6227  if (state == NULL || (totCount = NA_TOTAL_COUNT(state)) == 0)
6228  {
6229  *is_null = true;
6230  return NULL;
6231  }
6232 
6233  if (sample && totCount <= 1)
6234  {
6235  *is_null = true;
6236  return NULL;
6237  }
6238 
6239  *is_null = false;
6240 
6241  /*
6242  * Deal with NaN and infinity cases. By analogy to the behavior of the
6243  * float8 functions, any infinity input produces NaN output.
6244  */
6245  if (state->NaNcount > 0 || state->pInfcount > 0 || state->nInfcount > 0)
6246  return make_result(&const_nan);
6247 
6248  /* OK, normal calculation applies */
6249  init_var(&vN);
6250  init_var(&vsumX);
6251  init_var(&vsumX2);
6252 
6253  int64_to_numericvar(state->N, &vN);
6254  accum_sum_final(&(state->sumX), &vsumX);
6255  accum_sum_final(&(state->sumX2), &vsumX2);
6256 
6257  init_var(&vNminus1);
6258  sub_var(&vN, &const_one, &vNminus1);
6259 
6260  /* compute rscale for mul_var calls */
6261  rscale = vsumX.dscale * 2;
6262 
6263  mul_var(&vsumX, &vsumX, &vsumX, rscale); /* vsumX = sumX * sumX */
6264  mul_var(&vN, &vsumX2, &vsumX2, rscale); /* vsumX2 = N * sumX2 */
6265  sub_var(&vsumX2, &vsumX, &vsumX2); /* N * sumX2 - sumX * sumX */
6266 
6267  if (cmp_var(&vsumX2, &const_zero) <= 0)
6268  {
6269  /* Watch out for roundoff error producing a negative numerator */
6271  }
6272  else
6273  {
6274  if (sample)
6275  mul_var(&vN, &vNminus1, &vNminus1, 0); /* N * (N - 1) */
6276  else
6277  mul_var(&vN, &vN, &vNminus1, 0); /* N * N */
6278  rscale = select_div_scale(&vsumX2, &vNminus1);
6279  div_var(&vsumX2, &vNminus1, &vsumX, rscale, true); /* variance */
6280  if (!variance)
6281  sqrt_var(&vsumX, &vsumX, rscale); /* stddev */
6282 
6283  res = make_result(&vsumX);
6284  }
6285 
6286  free_var(&vNminus1);
6287  free_var(&vsumX);
6288  free_var(&vsumX2);
6289 
6290  return res;
6291 }
6292 
6293 Datum
6295 {
6297  Numeric res;
6298  bool is_null;
6299 
6300  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6301 
6302  res = numeric_stddev_internal(state, true, true, &is_null);
6303 
6304  if (is_null)
6305  PG_RETURN_NULL();
6306  else
6308 }
6309 
6310 Datum
6312 {
6314  Numeric res;
6315  bool is_null;
6316 
6317  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6318 
6319  res = numeric_stddev_internal(state, false, true, &is_null);
6320 
6321  if (is_null)
6322  PG_RETURN_NULL();
6323  else
6325 }
6326 
6327 Datum
6329 {
6331  Numeric res;
6332  bool is_null;
6333 
6334  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6335 
6336  res = numeric_stddev_internal(state, true, false, &is_null);
6337 
6338  if (is_null)
6339  PG_RETURN_NULL();
6340  else
6342 }
6343 
6344 Datum
6346 {
6348  Numeric res;
6349  bool is_null;
6350 
6351  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
6352 
6353  res = numeric_stddev_internal(state, false, false, &is_null);
6354 
6355  if (is_null)
6356  PG_RETURN_NULL();
6357  else
6359 }
6360 
6361 #ifdef HAVE_INT128
6362 static Numeric
6363 numeric_poly_stddev_internal(Int128AggState *state,
6364  bool variance, bool sample,
6365  bool *is_null)
6366 {
6367  NumericAggState numstate;
6368  Numeric res;
6369 
6370  /* Initialize an empty agg state */
6371  memset(&numstate, 0, sizeof(NumericAggState));
6372 
6373  if (state)
6374  {
6375  NumericVar tmp_var;
6376 
6377  numstate.N = state->N;
6378 
6379  init_var(&tmp_var);
6380 
6381  int128_to_numericvar(state->sumX, &tmp_var);
6382  accum_sum_add(&numstate.sumX, &tmp_var);
6383 
6384  int128_to_numericvar(state->sumX2, &tmp_var);
6385  accum_sum_add(&numstate.sumX2, &tmp_var);
6386 
6387  free_var(&tmp_var);
6388  }
6389 
6390  res = numeric_stddev_internal(&numstate, variance, sample, is_null);
6391 
6392  if (numstate.sumX.ndigits > 0)
6393  {
6394  pfree(numstate.sumX.pos_digits);
6395  pfree(numstate.sumX.neg_digits);
6396  }
6397  if (numstate.sumX2.ndigits > 0)
6398  {
6399  pfree(numstate.sumX2.pos_digits);
6400  pfree(numstate.sumX2.neg_digits);
6401  }
6402 
6403  return res;
6404 }
6405 #endif
6406 
6407 Datum
6409 {
6410 #ifdef HAVE_INT128
6412  Numeric res;
6413  bool is_null;
6414 
6415  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6416 
6417  res = numeric_poly_stddev_internal(state, true, true, &is_null);
6418 
6419  if (is_null)
6420  PG_RETURN_NULL();
6421  else
6423 #else
6424  return numeric_var_samp(fcinfo);
6425 #endif
6426 }
6427 
6428 Datum
6430 {
6431 #ifdef HAVE_INT128
6433  Numeric res;
6434  bool is_null;
6435 
6436  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6437 
6438  res = numeric_poly_stddev_internal(state, false, true, &is_null);
6439 
6440  if (is_null)
6441  PG_RETURN_NULL();
6442  else
6444 #else
6445  return numeric_stddev_samp(fcinfo);
6446 #endif
6447 }
6448 
6449 Datum
6451 {
6452 #ifdef HAVE_INT128
6454  Numeric res;
6455  bool is_null;
6456 
6457  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6458 
6459  res = numeric_poly_stddev_internal(state, true, false, &is_null);
6460 
6461  if (is_null)
6462  PG_RETURN_NULL();
6463  else
6465 #else
6466  return numeric_var_pop(fcinfo);
6467 #endif
6468 }
6469 
6470 Datum
6472 {
6473 #ifdef HAVE_INT128
6475  Numeric res;
6476  bool is_null;
6477 
6478  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
6479 
6480  res = numeric_poly_stddev_internal(state, false, false, &is_null);
6481 
6482  if (is_null)
6483  PG_RETURN_NULL();
6484  else
6486 #else
6487  return numeric_stddev_pop(fcinfo);
6488 #endif
6489 }
6490 
6491 /*
6492  * SUM transition functions for integer datatypes.
6493  *
6494  * To avoid overflow, we use accumulators wider than the input datatype.
6495  * A Numeric accumulator is needed for int8 input; for int4 and int2
6496  * inputs, we use int8 accumulators which should be sufficient for practical
6497  * purposes. (The latter two therefore don't really belong in this file,
6498  * but we keep them here anyway.)
6499  *
6500  * Because SQL defines the SUM() of no values to be NULL, not zero,
6501  * the initial condition of the transition data value needs to be NULL. This
6502  * means we can't rely on ExecAgg to automatically insert the first non-null
6503  * data value into the transition data: it doesn't know how to do the type
6504  * conversion. The upshot is that these routines have to be marked non-strict
6505  * and handle substitution of the first non-null input themselves.
6506  *
6507  * Note: these functions are used only in plain aggregation mode.
6508  * In moving-aggregate mode, we use intX_avg_accum and intX_avg_accum_inv.
6509  */
6510 
6511 Datum
6513 {
6514  int64 newval;
6515 
6516  if (PG_ARGISNULL(0))
6517  {
6518  /* No non-null input seen so far... */
6519  if (PG_ARGISNULL(1))
6520  PG_RETURN_NULL(); /* still no non-null */
6521  /* This is the first non-null input. */
6522  newval = (int64) PG_GETARG_INT16(1);
6524  }
6525 
6526  /*
6527  * If we're invoked as an aggregate, we can cheat and modify our first
6528  * parameter in-place to avoid palloc overhead. If not, we need to return
6529  * the new value of the transition variable. (If int8 is pass-by-value,
6530  * then of course this is useless as well as incorrect, so just ifdef it
6531  * out.)
6532  */
6533 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
6534  if (AggCheckCallContext(fcinfo, NULL))
6535  {
6536  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
6537 
6538  /* Leave the running sum unchanged in the new input is null */
6539  if (!PG_ARGISNULL(1))
6540  *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
6541 
6542  PG_RETURN_POINTER(oldsum);
6543  }
6544  else
6545 #endif
6546  {
6547  int64 oldsum = PG_GETARG_INT64(0);
6548 
6549  /* Leave sum unchanged if new input is null. */
6550  if (PG_ARGISNULL(1))
6551  PG_RETURN_INT64(oldsum);
6552 
6553  /* OK to do the addition. */
6554  newval = oldsum + (int64) PG_GETARG_INT16(1);
6555 
6557  }
6558 }
6559 
6560 Datum
6562 {
6563  int64 newval;
6564 
6565  if (PG_ARGISNULL(0))
6566  {
6567  /* No non-null input seen so far... */
6568  if (PG_ARGISNULL(1))
6569  PG_RETURN_NULL(); /* still no non-null */
6570  /* This is the first non-null input. */
6571  newval = (int64) PG_GETARG_INT32(1);
6573  }
6574 
6575  /*
6576  * If we're invoked as an aggregate, we can cheat and modify our first
6577  * parameter in-place to avoid palloc overhead. If not, we need to return
6578  * the new value of the transition variable. (If int8 is pass-by-value,
6579  * then of course this is useless as well as incorrect, so just ifdef it
6580  * out.)
6581  */
6582 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
6583  if (AggCheckCallContext(fcinfo, NULL))
6584  {
6585  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
6586 
6587  /* Leave the running sum unchanged in the new input is null */
6588  if (!PG_ARGISNULL(1))
6589  *oldsum = *oldsum + (int64) PG_GETARG_INT32(1);
6590 
6591  PG_RETURN_POINTER(oldsum);
6592  }
6593  else
6594 #endif
6595  {
6596  int64 oldsum = PG_GETARG_INT64(0);
6597 
6598  /* Leave sum unchanged if new input is null. */
6599  if (PG_ARGISNULL(1))
6600  PG_RETURN_INT64(oldsum);
6601 
6602  /* OK to do the addition. */
6603  newval = oldsum + (int64) PG_GETARG_INT32(1);
6604 
6606  }
6607 }
6608 
6609 /*
6610  * Note: this function is obsolete, it's no longer used for SUM(int8).
6611  */
6612 Datum
6614 {
6615  Numeric oldsum;
6616 
6617  if (PG_ARGISNULL(0))
6618  {
6619  /* No non-null input seen so far... */
6620  if (PG_ARGISNULL(1))
6621  PG_RETURN_NULL(); /* still no non-null */
6622  /* This is the first non-null input. */
6624  }
6625 
6626  /*
6627  * Note that we cannot special-case the aggregate case here, as we do for
6628  * int2_sum and int4_sum: numeric is of variable size, so we cannot modify
6629  * our first parameter in-place.
6630  */
6631 
6632  oldsum = PG_GETARG_NUMERIC(0);
6633 
6634  /* Leave sum unchanged if new input is null. */
6635  if (PG_ARGISNULL(1))
6636  PG_RETURN_NUMERIC(oldsum);
6637 
6638  /* OK to do the addition. */
6640  NumericGetDatum(oldsum),
6642 }
6643 
6644 
6645 /*
6646  * Routines for avg(int2) and avg(int4). The transition datatype
6647  * is a two-element int8 array, holding count and sum.
6648  *
6649  * These functions are also used for sum(int2) and sum(int4) when
6650  * operating in moving-aggregate mode, since for correct inverse transitions
6651  * we need to count the inputs.
6652  */
6653 
6654 typedef struct Int8TransTypeData
6655 {
6656  int64 count;
6657  int64 sum;
6659 
6660 Datum
6662 {
6663  ArrayType *transarray;
6665  Int8TransTypeData *transdata;
6666 
6667  /*
6668  * If we're invoked as an aggregate, we can cheat and modify our first
6669  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6670  * a copy of it before scribbling on it.
6671  */
6672  if (AggCheckCallContext(fcinfo, NULL))
6673  transarray = PG_GETARG_ARRAYTYPE_P(0);
6674  else
6675  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6676 
6677  if (ARR_HASNULL(transarray) ||
6678  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6679  elog(ERROR, "expected 2-element int8 array");
6680 
6681  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6682  transdata->count++;
6683  transdata->sum += newval;
6684 
6685  PG_RETURN_ARRAYTYPE_P(transarray);
6686 }
6687 
6688 Datum
6690 {
6691  ArrayType *transarray;
6693  Int8TransTypeData *transdata;
6694 
6695  /*
6696  * If we're invoked as an aggregate, we can cheat and modify our first
6697  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6698  * a copy of it before scribbling on it.
6699  */
6700  if (AggCheckCallContext(fcinfo, NULL))
6701  transarray = PG_GETARG_ARRAYTYPE_P(0);
6702  else
6703  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6704 
6705  if (ARR_HASNULL(transarray) ||
6706  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6707  elog(ERROR, "expected 2-element int8 array");
6708 
6709  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6710  transdata->count++;
6711  transdata->sum += newval;
6712 
6713  PG_RETURN_ARRAYTYPE_P(transarray);
6714 }
6715 
6716 Datum
6718 {
6719  ArrayType *transarray1;
6720  ArrayType *transarray2;
6721  Int8TransTypeData *state1;
6722  Int8TransTypeData *state2;
6723 
6724  if (!AggCheckCallContext(fcinfo, NULL))
6725  elog(ERROR, "aggregate function called in non-aggregate context");
6726 
6727  transarray1 = PG_GETARG_ARRAYTYPE_P(0);
6728  transarray2 = PG_GETARG_ARRAYTYPE_P(1);
6729 
6730  if (ARR_HASNULL(transarray1) ||
6731  ARR_SIZE(transarray1) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6732  elog(ERROR, "expected 2-element int8 array");
6733 
6734  if (ARR_HASNULL(transarray2) ||
6735  ARR_SIZE(transarray2) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6736  elog(ERROR, "expected 2-element int8 array");
6737 
6738  state1 = (Int8TransTypeData *) ARR_DATA_PTR(transarray1);
6739  state2 = (Int8TransTypeData *) ARR_DATA_PTR(transarray2);
6740 
6741  state1->count += state2->count;
6742  state1->sum += state2->sum;
6743 
6744  PG_RETURN_ARRAYTYPE_P(transarray1);
6745 }
6746 
6747 Datum
6749 {
6750  ArrayType *transarray;
6752  Int8TransTypeData *transdata;
6753 
6754  /*
6755  * If we're invoked as an aggregate, we can cheat and modify our first
6756  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6757  * a copy of it before scribbling on it.
6758  */
6759  if (AggCheckCallContext(fcinfo, NULL))
6760  transarray = PG_GETARG_ARRAYTYPE_P(0);
6761  else
6762  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6763 
6764  if (ARR_HASNULL(transarray) ||
6765  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6766  elog(ERROR, "expected 2-element int8 array");
6767 
6768  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6769  transdata->count--;
6770  transdata->sum -= newval;
6771 
6772  PG_RETURN_ARRAYTYPE_P(transarray);
6773 }
6774 
6775 Datum
6777 {
6778  ArrayType *transarray;
6780  Int8TransTypeData *transdata;
6781 
6782  /*
6783  * If we're invoked as an aggregate, we can cheat and modify our first
6784  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6785  * a copy of it before scribbling on it.
6786  */
6787  if (AggCheckCallContext(fcinfo, NULL))
6788  transarray = PG_GETARG_ARRAYTYPE_P(0);
6789  else
6790  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6791 
6792  if (ARR_HASNULL(transarray) ||
6793  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6794  elog(ERROR, "expected 2-element int8 array");
6795 
6796  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6797  transdata->count--;
6798  transdata->sum -= newval;
6799 
6800  PG_RETURN_ARRAYTYPE_P(transarray);
6801 }
6802 
6803 Datum
6805 {
6806  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
6807  Int8TransTypeData *transdata;
6808  Datum countd,
6809  sumd;
6810 
6811  if (ARR_HASNULL(transarray) ||
6812  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6813  elog(ERROR, "expected 2-element int8 array");
6814  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6815 
6816  /* SQL defines AVG of no values to be NULL */
6817  if (transdata->count == 0)
6818  PG_RETURN_NULL();
6819 
6820  countd = NumericGetDatum(int64_to_numeric(transdata->count));
6821  sumd = NumericGetDatum(int64_to_numeric(transdata->sum));
6822 
6824 }
6825 
6826 /*
6827  * SUM(int2) and SUM(int4) both return int8, so we can use this
6828  * final function for both.
6829  */
6830 Datum
6832 {
6833  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
6834  Int8TransTypeData *transdata;
6835 
6836  if (ARR_HASNULL(transarray) ||
6837  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6838  elog(ERROR, "expected 2-element int8 array");
6839  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6840 
6841  /* SQL defines SUM of no values to be NULL */
6842  if (transdata->count == 0)
6843  PG_RETURN_NULL();
6844 
6845  PG_RETURN_DATUM(Int64GetDatumFast(transdata->sum));
6846 }
6847 
6848 
6849 /* ----------------------------------------------------------------------
6850  *
6851  * Debug support
6852  *
6853  * ----------------------------------------------------------------------
6854  */
6855 
6856 #ifdef NUMERIC_DEBUG
6857 
6858 /*
6859  * dump_numeric() - Dump a value in the db storage format for debugging
6860  */
6861 static void
6862 dump_numeric(const char *str, Numeric num)
6863 {
6865  int ndigits;
6866  int i;
6867 
6868  ndigits = NUMERIC_NDIGITS(num);
6869 
6870  printf("%s: NUMERIC w=%d d=%d ", str,
6871  NUMERIC_WEIGHT(num), NUMERIC_DSCALE(num));
6872  switch (NUMERIC_SIGN(num))
6873  {
6874  case NUMERIC_POS:
6875  printf("POS");
6876  break;
6877  case NUMERIC_NEG:
6878  printf("NEG");
6879  break;
6880  case NUMERIC_NAN:
6881  printf("NaN");
6882  break;
6883  case NUMERIC_PINF:
6884  printf("Infinity");
6885  break;
6886  case NUMERIC_NINF:
6887  printf("-Infinity");
6888  break;
6889  default:
6890  printf("SIGN=0x%x", NUMERIC_SIGN(num));
6891  break;
6892  }
6893 
6894  for (i = 0; i < ndigits; i++)
6895  printf(" %0*d", DEC_DIGITS, digits[i]);
6896  printf("\n");
6897 }
6898 
6899 
6900 /*
6901  * dump_var() - Dump a value in the variable format for debugging
6902  */
6903 static void
6904 dump_var(const char *str, NumericVar *var)
6905 {
6906  int i;
6907 
6908  printf("%s: VAR w=%d d=%d ", str, var->weight, var->dscale);
6909  switch (var->sign)
6910  {
6911  case NUMERIC_POS:
6912  printf("POS");
6913  break;
6914  case NUMERIC_NEG:
6915  printf("NEG");
6916  break;
6917  case NUMERIC_NAN:
6918  printf("NaN");
6919  break;
6920  case NUMERIC_PINF:
6921  printf("Infinity");
6922  break;
6923  case NUMERIC_NINF:
6924  printf("-Infinity");
6925  break;
6926  default:
6927  printf("SIGN=0x%x", var->sign);
6928  break;
6929  }
6930 
6931  for (i = 0; i < var->ndigits; i++)
6932  printf(" %0*d", DEC_DIGITS, var->digits[i]);
6933 
6934  printf("\n");
6935 }
6936 #endif /* NUMERIC_DEBUG */
6937 
6938 
6939 /* ----------------------------------------------------------------------
6940  *
6941  * Local functions follow
6942  *
6943  * In general, these do not support "special" (NaN or infinity) inputs;
6944  * callers should handle those possibilities first.
6945  * (There are one or two exceptions, noted in their header comments.)
6946  *
6947  * ----------------------------------------------------------------------
6948  */
6949 
6950 
6951 /*
6952  * alloc_var() -
6953  *
6954  * Allocate a digit buffer of ndigits digits (plus a spare digit for rounding)
6955  */
6956 static void
6957 alloc_var(NumericVar *var, int ndigits)
6958 {
6959  digitbuf_free(var->buf);
6960  var->buf = digitbuf_alloc(ndigits + 1);
6961  var->buf[0] = 0; /* spare digit for rounding */
6962  var->digits = var->buf + 1;
6963  var->ndigits = ndigits;
6964 }
6965 
6966 
6967 /*
6968  * free_var() -
6969  *
6970  * Return the digit buffer of a variable to the free pool
6971  */
6972 static void
6974 {
6975  digitbuf_free(var->buf);
6976  var->buf = NULL;
6977  var->digits = NULL;
6978  var->sign = NUMERIC_NAN;
6979 }
6980 
6981 
6982 /*
6983  * zero_var() -
6984  *
6985  * Set a variable to ZERO.
6986  * Note: its dscale is not touched.
6987  */
6988 static void
6990 {
6991  digitbuf_free(var->buf);
6992  var->buf = NULL;
6993  var->digits = NULL;
6994  var->ndigits = 0;
6995  var->weight = 0; /* by convention; doesn't really matter */
6996  var->sign = NUMERIC_POS; /* anything but NAN... */
6997 }
6998 
6999 
7000 /*
7001  * set_var_from_str()
7002  *
7003  * Parse a string and put the number into a variable
7004  *
7005  * This function does not handle leading or trailing spaces. It returns
7006  * the end+1 position parsed into *endptr, so that caller can check for
7007  * trailing spaces/garbage if deemed necessary.
7008  *
7009  * cp is the place to actually start parsing; str is what to use in error
7010  * reports. (Typically cp would be the same except advanced over spaces.)
7011  *
7012  * Returns true on success, false on failure (if escontext points to an
7013  * ErrorSaveContext; otherwise errors are thrown).
7014  */
7015 static bool
7016 set_var_from_str(const char *str, const char *cp,
7017  NumericVar *dest, const char **endptr,
7018  Node *escontext)
7019 {
7020  bool have_dp = false;
7021  int i;
7022  unsigned char *decdigits;
7023  int sign = NUMERIC_POS;
7024  int dweight = -1;
7025  int ddigits;
7026  int dscale = 0;
7027  int weight;
7028  int ndigits;
7029  int offset;
7031 
7032  /*
7033  * We first parse the string to extract decimal digits and determine the
7034  * correct decimal weight. Then convert to NBASE representation.
7035  */
7036  switch (*cp)
7037  {
7038  case '+':
7039  sign = NUMERIC_POS;
7040  cp++;
7041  break;
7042 
7043  case '-':
7044  sign = NUMERIC_NEG;
7045  cp++;
7046  break;
7047  }
7048 
7049  if (*cp == '.')
7050  {
7051  have_dp = true;
7052  cp++;
7053  }
7054 
7055  if (!isdigit((unsigned char) *cp))
7056  goto invalid_syntax;
7057 
7058  decdigits = (unsigned char *) palloc(strlen(cp) + DEC_DIGITS * 2);
7059 
7060  /* leading padding for digit alignment later */
7061  memset(decdigits, 0, DEC_DIGITS);
7062  i = DEC_DIGITS;
7063 
7064  while (*cp)
7065  {
7066  if (isdigit((unsigned char) *cp))
7067  {
7068  decdigits[i++] = *cp++ - '0';
7069  if (!have_dp)
7070  dweight++;
7071  else
7072  dscale++;
7073  }
7074  else if (*cp == '.')
7075  {
7076  if (have_dp)
7077  goto invalid_syntax;
7078  have_dp = true;
7079  cp++;
7080  /* decimal point must not be followed by underscore */
7081  if (*cp == '_')
7082  goto invalid_syntax;
7083  }
7084  else if (*cp == '_')
7085  {
7086  /* underscore must be followed by more digits */
7087  cp++;
7088  if (!isdigit((unsigned char) *cp))
7089  goto invalid_syntax;
7090  }
7091  else
7092  break;
7093  }
7094 
7095  ddigits = i - DEC_DIGITS;
7096  /* trailing padding for digit alignment later */
7097  memset(decdigits + i, 0, DEC_DIGITS - 1);
7098 
7099  /* Handle exponent, if any */
7100  if (*cp == 'e' || *cp == 'E')
7101  {
7102  int64 exponent = 0;
7103  bool neg = false;
7104 
7105  /*
7106  * At this point, dweight and dscale can't be more than about
7107  * INT_MAX/2 due to the MaxAllocSize limit on string length, so
7108  * constraining the exponent similarly should be enough to prevent
7109  * integer overflow in this function. If the value is too large to
7110  * fit in storage format, make_result() will complain about it later;
7111  * for consistency use the same ereport errcode/text as make_result().
7112  */
7113 
7114  /* exponent sign */
7115  cp++;
7116  if (*cp == '+')
7117  cp++;
7118  else if (*cp == '-')
7119  {
7120  neg = true;
7121  cp++;
7122  }
7123 
7124  /* exponent digits */
7125  if (!isdigit((unsigned char) *cp))
7126  goto invalid_syntax;
7127 
7128  while (*cp)
7129  {
7130  if (isdigit((unsigned char) *cp))
7131  {
7132  exponent = exponent * 10 + (*cp++ - '0');
7133  if (exponent > PG_INT32_MAX / 2)
7134  goto out_of_range;
7135  }
7136  else if (*cp == '_')
7137  {
7138  /* underscore must be followed by more digits */
7139  cp++;
7140  if (!isdigit((unsigned char) *cp))
7141  goto invalid_syntax;
7142  }
7143  else
7144  break;
7145  }
7146 
7147  if (neg)
7148  exponent = -exponent;
7149 
7150  dweight += (int) exponent;
7151  dscale -= (int) exponent;
7152  if (dscale < 0)
7153  dscale = 0;
7154  }
7155 
7156  /*
7157  * Okay, convert pure-decimal representation to base NBASE. First we need
7158  * to determine the converted weight and ndigits. offset is the number of
7159  * decimal zeroes to insert before the first given digit to have a
7160  * correctly aligned first NBASE digit.
7161  */
7162  if (dweight >= 0)
7163  weight = (dweight + 1 + DEC_DIGITS - 1) / DEC_DIGITS - 1;
7164  else
7165  weight = -((-dweight - 1) / DEC_DIGITS + 1);
7166  offset = (weight + 1) * DEC_DIGITS - (dweight + 1);
7167  ndigits = (ddigits + offset + DEC_DIGITS - 1) / DEC_DIGITS;
7168 
7169  alloc_var(dest, ndigits);
7170  dest->sign = sign;
7171  dest->weight = weight;
7172  dest->dscale = dscale;
7173 
7174  i = DEC_DIGITS - offset;
7175  digits = dest->digits;
7176 
7177  while (ndigits-- > 0)
7178  {
7179 #if DEC_DIGITS == 4
7180  *digits++ = ((decdigits[i] * 10 + decdigits[i + 1]) * 10 +
7181  decdigits[i + 2]) * 10 + decdigits[i + 3];
7182 #elif DEC_DIGITS == 2
7183  *digits++ = decdigits[i] * 10 + decdigits[i + 1];
7184 #elif DEC_DIGITS == 1
7185  *digits++ = decdigits[i];
7186 #else
7187 #error unsupported NBASE
7188 #endif
7189  i += DEC_DIGITS;
7190  }
7191 
7192  pfree(decdigits);
7193 
7194  /* Strip any leading/trailing zeroes, and normalize weight if zero */
7195  strip_var(dest);
7196 
7197  /* Return end+1 position for caller */
7198  *endptr = cp;
7199 
7200  return true;
7201 
7202 out_of_range:
7203  ereturn(escontext, false,
7204  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7205  errmsg("value overflows numeric format")));
7206 
7207 invalid_syntax:
7208  ereturn(escontext, false,
7209  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
7210  errmsg("invalid input syntax for type %s: \"%s\"",
7211  "numeric", str)));
7212 }
7213 
7214 
7215 /*
7216  * Return the numeric value of a single hex digit.
7217  */
7218 static inline int
7219 xdigit_value(char dig)
7220 {
7221  return dig >= '0' && dig <= '9' ? dig - '0' :
7222  dig >= 'a' && dig <= 'f' ? dig - 'a' + 10 :
7223  dig >= 'A' && dig <= 'F' ? dig - 'A' + 10 : -1;
7224 }
7225 
7226 /*
7227  * set_var_from_non_decimal_integer_str()
7228  *
7229  * Parse a string containing a non-decimal integer
7230  *
7231  * This function does not handle leading or trailing spaces. It returns
7232  * the end+1 position parsed into *endptr, so that caller can check for
7233  * trailing spaces/garbage if deemed necessary.
7234  *
7235  * cp is the place to actually start parsing; str is what to use in error
7236  * reports. The number's sign and base prefix indicator (e.g., "0x") are
7237  * assumed to have already been parsed, so cp should point to the number's
7238  * first digit in the base specified.
7239  *
7240  * base is expected to be 2, 8 or 16.
7241  *
7242  * Returns true on success, false on failure (if escontext points to an
7243  * ErrorSaveContext; otherwise errors are thrown).
7244  */
7245 static bool
7246 set_var_from_non_decimal_integer_str(const char *str, const char *cp, int sign,
7247  int base, NumericVar *dest,
7248  const char **endptr, Node *escontext)
7249 {
7250  const char *firstdigit = cp;
7251  int64 tmp;
7252  int64 mul;
7253  NumericVar tmp_var;
7254 
7255  init_var(&tmp_var);
7256 
7257  zero_var(dest);
7258 
7259  /*
7260  * Process input digits in groups that fit in int64. Here "tmp" is the
7261  * value of the digits in the group, and "mul" is base^n, where n is the
7262  * number of digits in the group. Thus tmp < mul, and we must start a new
7263  * group when mul * base threatens to overflow PG_INT64_MAX.
7264  */
7265  tmp = 0;
7266  mul = 1;
7267 
7268  if (base == 16)
7269  {
7270  while (*cp)
7271  {
7272  if (isxdigit((unsigned char) *cp))
7273  {
7274  if (mul > PG_INT64_MAX / 16)
7275  {
7276  /* Add the contribution from this group of digits */
7277  int64_to_numericvar(mul, &tmp_var);
7278  mul_var(dest, &tmp_var, dest, 0);
7279  int64_to_numericvar(tmp, &tmp_var);
7280  add_var(dest, &tmp_var, dest);
7281 
7282  /* Result will overflow if weight overflows int16 */
7283  if (dest->weight > NUMERIC_WEIGHT_MAX)
7284  goto out_of_range;
7285 
7286  /* Begin a new group */
7287  tmp = 0;
7288  mul = 1;
7289  }
7290 
7291  tmp = tmp * 16 + xdigit_value(*cp++);
7292  mul = mul * 16;
7293  }
7294  else if (*cp == '_')
7295  {
7296  /* Underscore must be followed by more digits */
7297  cp++;
7298  if (!isxdigit((unsigned char) *cp))
7299  goto invalid_syntax;
7300  }
7301  else
7302  break;
7303  }
7304  }
7305  else if (base == 8)
7306  {
7307  while (*cp)
7308  {
7309  if (*cp >= '0' && *cp <= '7')
7310  {
7311  if (mul > PG_INT64_MAX / 8)
7312  {
7313  /* Add the contribution from this group of digits */
7314  int64_to_numericvar(mul, &tmp_var);
7315  mul_var(dest, &tmp_var, dest, 0);
7316  int64_to_numericvar(tmp, &tmp_var);
7317  add_var(dest, &tmp_var, dest);
7318 
7319  /* Result will overflow if weight overflows int16 */
7320  if (dest->weight > NUMERIC_WEIGHT_MAX)
7321  goto out_of_range;
7322 
7323  /* Begin a new group */
7324  tmp = 0;
7325  mul = 1;
7326  }
7327 
7328  tmp = tmp * 8 + (*cp++ - '0');
7329  mul = mul * 8;
7330  }
7331  else if (*cp == '_')
7332  {
7333  /* Underscore must be followed by more digits */
7334  cp++;
7335  if (*cp < '0' || *cp > '7')
7336  goto invalid_syntax;
7337  }
7338  else
7339  break;
7340  }
7341  }
7342  else if (base == 2)
7343  {
7344  while (*cp)
7345  {
7346  if (*cp >= '0' && *cp <= '1')
7347  {
7348  if (mul > PG_INT64_MAX / 2)
7349  {
7350  /* Add the contribution from this group of digits */
7351  int64_to_numericvar(mul, &tmp_var);
7352  mul_var(dest, &tmp_var, dest, 0);
7353  int64_to_numericvar(tmp, &tmp_var);
7354  add_var(dest, &tmp_var, dest);
7355 
7356  /* Result will overflow if weight overflows int16 */
7357  if (dest->weight > NUMERIC_WEIGHT_MAX)
7358  goto out_of_range;
7359 
7360  /* Begin a new group */
7361  tmp = 0;
7362  mul = 1;
7363  }
7364 
7365  tmp = tmp * 2 + (*cp++ - '0');
7366  mul = mul * 2;
7367  }
7368  else if (*cp == '_')
7369  {
7370  /* Underscore must be followed by more digits */
7371  cp++;
7372  if (*cp < '0' || *cp > '1')
7373  goto invalid_syntax;
7374  }
7375  else
7376  break;
7377  }
7378  }
7379  else
7380  /* Should never happen; treat as invalid input */
7381  goto invalid_syntax;
7382 
7383  /* Check that we got at least one digit */
7384  if (unlikely(cp == firstdigit))
7385  goto invalid_syntax;
7386 
7387  /* Add the contribution from the final group of digits */
7388  int64_to_numericvar(mul, &tmp_var);
7389  mul_var(dest, &tmp_var, dest, 0);
7390  int64_to_numericvar(tmp, &tmp_var);
7391  add_var(dest, &tmp_var, dest);
7392 
7393  if (dest->weight > NUMERIC_WEIGHT_MAX)
7394  goto out_of_range;
7395 
7396  dest->sign = sign;
7397 
7398  free_var(&tmp_var);
7399 
7400  /* Return end+1 position for caller */
7401  *endptr = cp;
7402 
7403  return true;
7404 
7405 out_of_range:
7406  ereturn(escontext, false,
7407  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7408  errmsg("value overflows numeric format")));
7409 
7410 invalid_syntax:
7411  ereturn(escontext, false,
7412  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
7413  errmsg("invalid input syntax for type %s: \"%s\"",
7414  "numeric", str)));
7415 }
7416 
7417 
7418 /*
7419  * set_var_from_num() -
7420  *
7421  * Convert the packed db format into a variable
7422  */
7423 static void
7425 {
7426  int ndigits;
7427 
7428  ndigits = NUMERIC_NDIGITS(num);
7429 
7430  alloc_var(dest, ndigits);
7431 
7432  dest->weight = NUMERIC_WEIGHT(num);
7433  dest->sign = NUMERIC_SIGN(num);
7434  dest->dscale = NUMERIC_DSCALE(num);
7435 
7436  memcpy(dest->digits, NUMERIC_DIGITS(num), ndigits * sizeof(NumericDigit));
7437 }
7438 
7439 
7440 /*
7441  * init_var_from_num() -
7442  *
7443  * Initialize a variable from packed db format. The digits array is not
7444  * copied, which saves some cycles when the resulting var is not modified.
7445  * Also, there's no need to call free_var(), as long as you don't assign any
7446  * other value to it (with set_var_* functions, or by using the var as the
7447  * destination of a function like add_var())
7448  *
7449  * CAUTION: Do not modify the digits buffer of a var initialized with this
7450  * function, e.g by calling round_var() or trunc_var(), as the changes will
7451  * propagate to the original Numeric! It's OK to use it as the destination
7452  * argument of one of the calculational functions, though.
7453  */
7454 static void
7456 {
7457  dest->ndigits = NUMERIC_NDIGITS(num);
7458  dest->weight = NUMERIC_WEIGHT(num);
7459  dest->sign = NUMERIC_SIGN(num);
7460  dest->dscale = NUMERIC_DSCALE(num);
7461  dest->digits = NUMERIC_DIGITS(num);
7462  dest->buf = NULL; /* digits array is not palloc'd */
7463 }
7464 
7465 
7466 /*
7467  * set_var_from_var() -
7468  *
7469  * Copy one variable into another
7470  */
7471 static void
7473 {
7474  NumericDigit *newbuf;
7475 
7476  newbuf = digitbuf_alloc(value->ndigits + 1);
7477  newbuf[0] = 0; /* spare digit for rounding */
7478  if (value->ndigits > 0) /* else value->digits might be null */
7479  memcpy(newbuf + 1, value->digits,
7480  value->ndigits * sizeof(NumericDigit));
7481 
7482  digitbuf_free(dest->buf);
7483 
7484  memmove(dest, value, sizeof(NumericVar));
7485  dest->buf = newbuf;
7486  dest->digits = newbuf + 1;
7487 }
7488 
7489 
7490 /*
7491  * get_str_from_var() -
7492  *
7493  * Convert a var to text representation (guts of numeric_out).
7494  * The var is displayed to the number of digits indicated by its dscale.
7495  * Returns a palloc'd string.
7496  */
7497 static char *
7499 {
7500  int dscale;
7501  char *str;
7502  char *cp;
7503  char *endcp;
7504  int i;
7505  int d;
7506  NumericDigit dig;
7507 
7508 #if DEC_DIGITS > 1
7509  NumericDigit d1;
7510 #endif
7511 
7512  dscale = var->dscale;
7513 
7514  /*
7515  * Allocate space for the result.
7516  *
7517  * i is set to the # of decimal digits before decimal point. dscale is the
7518  * # of decimal digits we will print after decimal point. We may generate
7519  * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
7520  * need room for sign, decimal point, null terminator.
7521  */
7522  i = (var->weight + 1) * DEC_DIGITS;
7523  if (i <= 0)
7524  i = 1;
7525 
7526  str = palloc(i + dscale + DEC_DIGITS + 2);
7527  cp = str;
7528 
7529  /*
7530  * Output a dash for negative values
7531  */
7532  if (var->sign == NUMERIC_NEG)
7533  *cp++ = '-';
7534 
7535  /*
7536  * Output all digits before the decimal point
7537  */
7538  if (var->weight < 0)
7539  {
7540  d = var->weight + 1;
7541  *cp++ = '0';
7542  }
7543  else
7544  {
7545  for (d = 0; d <= var->weight; d++)
7546  {
7547  dig = (d < var->ndigits) ? var->digits[d] : 0;
7548  /* In the first digit, suppress extra leading decimal zeroes */
7549 #if DEC_DIGITS == 4
7550  {
7551  bool putit = (d > 0);
7552 
7553  d1 = dig / 1000;
7554  dig -= d1 * 1000;
7555  putit |= (d1 > 0);
7556  if (putit)
7557  *cp++ = d1 + '0';
7558  d1 = dig / 100;
7559  dig -= d1 * 100;
7560  putit |= (d1 > 0);
7561  if (putit)
7562  *cp++ = d1 + '0';
7563  d1 = dig / 10;
7564  dig -= d1 * 10;
7565  putit |= (d1 > 0);
7566  if (putit)
7567  *cp++ = d1 + '0';
7568  *cp++ = dig + '0';
7569  }
7570 #elif DEC_DIGITS == 2
7571  d1 = dig / 10;
7572  dig -= d1 * 10;
7573  if (d1 > 0 || d > 0)
7574  *cp++ = d1 + '0';
7575  *cp++ = dig + '0';
7576 #elif DEC_DIGITS == 1
7577  *cp++ = dig + '0';
7578 #else
7579 #error unsupported NBASE
7580 #endif
7581  }
7582  }
7583 
7584  /*
7585  * If requested, output a decimal point and all the digits that follow it.
7586  * We initially put out a multiple of DEC_DIGITS digits, then truncate if
7587  * needed.
7588  */
7589  if (dscale > 0)
7590  {
7591  *cp++ = '.';
7592  endcp = cp + dscale;
7593  for (i = 0; i < dscale; d++, i += DEC_DIGITS)
7594  {
7595  dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
7596 #if DEC_DIGITS == 4
7597  d1 = dig / 1000;
7598  dig -= d1 * 1000;
7599  *cp++ = d1 + '0';
7600  d1 = dig / 100;
7601  dig -= d1 * 100;
7602  *cp++ = d1 + '0';
7603  d1 = dig / 10;
7604  dig -= d1 * 10;
7605  *cp++ = d1 + '0';
7606  *cp++ = dig + '0';
7607 #elif DEC_DIGITS == 2
7608  d1 = dig / 10;
7609  dig -= d1 * 10;
7610  *cp++ = d1 + '0';
7611  *cp++ = dig + '0';
7612 #elif DEC_DIGITS == 1
7613  *cp++ = dig + '0';
7614 #else
7615 #error unsupported NBASE
7616 #endif
7617  }
7618  cp = endcp;
7619  }
7620 
7621  /*
7622  * terminate the string and return it
7623  */
7624  *cp = '\0';
7625  return str;
7626 }
7627 
7628 /*
7629  * get_str_from_var_sci() -
7630  *
7631  * Convert a var to a normalised scientific notation text representation.
7632  * This function does the heavy lifting for numeric_out_sci().
7633  *
7634  * This notation has the general form a * 10^b, where a is known as the
7635  * "significand" and b is known as the "exponent".
7636  *
7637  * Because we can't do superscript in ASCII (and because we want to copy
7638  * printf's behaviour) we display the exponent using E notation, with a
7639  * minimum of two exponent digits.
7640  *
7641  * For example, the value 1234 could be output as 1.2e+03.
7642  *
7643  * We assume that the exponent can fit into an int32.
7644  *
7645  * rscale is the number of decimal digits desired after the decimal point in
7646  * the output, negative values will be treated as meaning zero.
7647  *
7648  * Returns a palloc'd string.
7649  */
7650 static char *
7651 get_str_from_var_sci(const NumericVar *var, int rscale)
7652 {
7653  int32 exponent;
7654  NumericVar tmp_var;
7655  size_t len;
7656  char *str;
7657  char *sig_out;
7658 
7659  if (rscale < 0)
7660  rscale = 0;
7661 
7662  /*
7663  * Determine the exponent of this number in normalised form.
7664  *
7665  * This is the exponent required to represent the number with only one
7666  * significant digit before the decimal place.
7667  */
7668  if (var->ndigits > 0)
7669  {
7670  exponent = (var->weight + 1) * DEC_DIGITS;
7671 
7672  /*
7673  * Compensate for leading decimal zeroes in the first numeric digit by
7674  * decrementing the exponent.
7675  */
7676  exponent -= DEC_DIGITS - (int) log10(var->digits[0]);
7677  }
7678  else
7679  {
7680  /*
7681  * If var has no digits, then it must be zero.
7682  *
7683  * Zero doesn't technically have a meaningful exponent in normalised
7684  * notation, but we just display the exponent as zero for consistency
7685  * of output.
7686  */
7687  exponent = 0;
7688  }
7689 
7690  /*
7691  * Divide var by 10^exponent to get the significand, rounding to rscale
7692  * decimal digits in the process.
7693  */
7694  init_var(&tmp_var);
7695 
7696  power_ten_int(exponent, &tmp_var);
7697  div_var(var, &tmp_var, &tmp_var, rscale, true);
7698  sig_out = get_str_from_var(&tmp_var);
7699 
7700  free_var(&tmp_var);
7701 
7702  /*
7703  * Allocate space for the result.
7704  *
7705  * In addition to the significand, we need room for the exponent
7706  * decoration ("e"), the sign of the exponent, up to 10 digits for the
7707  * exponent itself, and of course the null terminator.
7708  */
7709  len = strlen(sig_out) + 13;
7710  str = palloc(len);
7711  snprintf(str, len, "%se%+03d", sig_out, exponent);
7712 
7713  pfree(sig_out);
7714 
7715  return str;
7716 }
7717 
7718 
7719 /*
7720  * numericvar_serialize - serialize NumericVar to binary format
7721  *
7722  * At variable level, no checks are performed on the weight or dscale, allowing
7723  * us to pass around intermediate values with higher precision than supported
7724  * by the numeric type. Note: this is incompatible with numeric_send/recv(),
7725  * which use 16-bit integers for these fields.
7726  */
7727 static void
7729 {
7730  int i;
7731 
7732  pq_sendint32(buf, var->ndigits);
7733  pq_sendint32(buf, var->weight);
7734  pq_sendint32(buf, var->sign);
7735  pq_sendint32(buf, var->dscale);
7736  for (i = 0; i < var->ndigits; i++)
7737  pq_sendint16(buf, var->digits[i]);
7738 }
7739 
7740 /*
7741  * numericvar_deserialize - deserialize binary format to NumericVar
7742  */
7743 static void
7745 {
7746  int len,
7747  i;
7748 
7749  len = pq_getmsgint(buf, sizeof(int32));
7750 
7751  alloc_var(var, len); /* sets var->ndigits */
7752 
7753  var->weight = pq_getmsgint(buf, sizeof(int32));
7754  var->sign = pq_getmsgint(buf, sizeof(int32));
7755  var->dscale = pq_getmsgint(buf, sizeof(int32));
7756  for (i = 0; i < len; i++)
7757  var->digits[i] = pq_getmsgint(buf, sizeof(int16));
7758 }
7759 
7760 
7761 /*
7762  * duplicate_numeric() - copy a packed-format Numeric
7763  *
7764  * This will handle NaN and Infinity cases.
7765  */
7766 static Numeric
7768 {
7769  Numeric res;
7770 
7771  res = (Numeric) palloc(VARSIZE(num));
7772  memcpy(res, num, VARSIZE(num));
7773  return res;
7774 }
7775 
7776 /*
7777  * make_result_opt_error() -
7778  *
7779  * Create the packed db numeric format in palloc()'d memory from
7780  * a variable. This will handle NaN and Infinity cases.
7781  *
7782  * If "have_error" isn't NULL, on overflow *have_error is set to true and
7783  * NULL is returned. This is helpful when caller needs to handle errors.
7784  */
7785 static Numeric
7786 make_result_opt_error(const NumericVar *var, bool *have_error)
7787 {
7788  Numeric result;
7789  NumericDigit *digits = var->digits;
7790  int weight = var->weight;
7791  int sign = var->sign;
7792  int n;
7793  Size len;
7794 
7795  if (have_error)
7796  *have_error = false;
7797 
7799  {
7800  /*
7801  * Verify valid special value. This could be just an Assert, perhaps,
7802  * but it seems worthwhile to expend a few cycles to ensure that we
7803  * never write any nonzero reserved bits to disk.
7804  */
7805  if (!(sign == NUMERIC_NAN ||
7806  sign == NUMERIC_PINF ||
7807  sign == NUMERIC_NINF))
7808  elog(ERROR, "invalid numeric sign value 0x%x", sign);
7809 
7810  result = (Numeric) palloc(NUMERIC_HDRSZ_SHORT);
7811 
7813  result->choice.n_header = sign;
7814  /* the header word is all we need */
7815 
7816  dump_numeric("make_result()", result);
7817  return result;
7818  }
7819 
7820  n = var->ndigits;
7821 
7822  /* truncate leading zeroes */
7823  while (n > 0 && *digits == 0)
7824  {
7825  digits++;
7826  weight--;
7827  n--;
7828  }
7829  /* truncate trailing zeroes */
7830  while (n > 0 && digits[n - 1] == 0)
7831  n--;
7832 
7833  /* If zero result, force to weight=0 and positive sign */
7834  if (n == 0)
7835  {
7836  weight = 0;
7837  sign = NUMERIC_POS;
7838  }
7839 
7840  /* Build the result */
7841  if (NUMERIC_CAN_BE_SHORT(var->dscale, weight))
7842  {
7843  len = NUMERIC_HDRSZ_SHORT + n * sizeof(NumericDigit);
7844  result = (Numeric) palloc(len);
7845  SET_VARSIZE(result, len);
7846  result->choice.n_short.n_header =
7848  : NUMERIC_SHORT)
7850  | (weight < 0 ? NUMERIC_SHORT_WEIGHT_SIGN_MASK : 0)
7851  | (weight & NUMERIC_SHORT_WEIGHT_MASK);
7852  }
7853  else
7854  {
7855  len = NUMERIC_HDRSZ + n * sizeof(NumericDigit);
7856  result = (Numeric) palloc(len);
7857  SET_VARSIZE(result, len);
7858  result->choice.n_long.n_sign_dscale =
7859  sign | (var->dscale & NUMERIC_DSCALE_MASK);
7860  result->choice.n_long.n_weight = weight;
7861  }
7862 
7863  Assert(NUMERIC_NDIGITS(result) == n);
7864  if (n > 0)
7865  memcpy(NUMERIC_DIGITS(result), digits, n * sizeof(NumericDigit));
7866 
7867  /* Check for overflow of int16 fields */
7868  if (NUMERIC_WEIGHT(result) != weight ||
7869  NUMERIC_DSCALE(result) != var->dscale)
7870  {
7871  if (have_error)
7872  {
7873  *have_error = true;
7874  return NULL;
7875  }
7876  else
7877  {
7878  ereport(ERROR,
7879  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7880  errmsg("value overflows numeric format")));
7881  }
7882  }
7883 
7884  dump_numeric("make_result()", result);
7885  return result;
7886 }
7887 
7888 
7889 /*
7890  * make_result() -
7891  *
7892  * An interface to make_result_opt_error() without "have_error" argument.
7893  */
7894 static Numeric
7896 {
7897  return make_result_opt_error(var, NULL);
7898 }
7899 
7900 
7901 /*
7902  * apply_typmod() -
7903  *
7904  * Do bounds checking and rounding according to the specified typmod.
7905  * Note that this is only applied to normal finite values.
7906  *
7907  * Returns true on success, false on failure (if escontext points to an
7908  * ErrorSaveContext; otherwise errors are thrown).
7909  */
7910 static bool
7911 apply_typmod(NumericVar *var, int32 typmod, Node *escontext)
7912 {
7913  int precision;
7914  int scale;
7915  int maxdigits;
7916  int ddigits;
7917  int i;
7918 
7919  /* Do nothing if we have an invalid typmod */
7920  if (!is_valid_numeric_typmod(typmod))
7921  return true;
7922 
7923  precision = numeric_typmod_precision(typmod);
7924  scale = numeric_typmod_scale(typmod);
7925  maxdigits = precision - scale;
7926 
7927  /* Round to target scale (and set var->dscale) */
7928  round_var(var, scale);
7929 
7930  /* but don't allow var->dscale to be negative */
7931  if (var->dscale < 0)
7932  var->dscale = 0;
7933 
7934  /*
7935  * Check for overflow - note we can't do this before rounding, because
7936  * rounding could raise the weight. Also note that the var's weight could
7937  * be inflated by leading zeroes, which will be stripped before storage
7938  * but perhaps might not have been yet. In any case, we must recognize a
7939  * true zero, whose weight doesn't mean anything.
7940  */
7941  ddigits = (var->weight + 1) * DEC_DIGITS;
7942  if (ddigits > maxdigits)
7943  {
7944  /* Determine true weight; and check for all-zero result */
7945  for (i = 0; i < var->ndigits; i++)
7946  {
7947  NumericDigit dig = var->digits[i];
7948 
7949  if (dig)
7950  {
7951  /* Adjust for any high-order decimal zero digits */
7952 #if DEC_DIGITS == 4
7953  if (dig < 10)
7954  ddigits -= 3;
7955  else if (dig < 100)
7956  ddigits -= 2;
7957  else if (dig < 1000)
7958  ddigits -= 1;
7959 #elif DEC_DIGITS == 2
7960  if (dig < 10)
7961  ddigits -= 1;
7962 #elif DEC_DIGITS == 1
7963  /* no adjustment */
7964 #else
7965 #error unsupported NBASE
7966 #endif
7967  if (ddigits > maxdigits)
7968  ereturn(escontext, false,
7969  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7970  errmsg("numeric field overflow"),
7971  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
7972  precision, scale,
7973  /* Display 10^0 as 1 */
7974  maxdigits ? "10^" : "",
7975  maxdigits ? maxdigits : 1
7976  )));
7977  break;
7978  }
7979  ddigits -= DEC_DIGITS;
7980  }
7981  }
7982 
7983  return true;
7984 }
7985 
7986 /*
7987  * apply_typmod_special() -
7988  *
7989  * Do bounds checking according to the specified typmod, for an Inf or NaN.
7990  * For convenience of most callers, the value is presented in packed form.
7991  *
7992  * Returns true on success, false on failure (if escontext points to an
7993  * ErrorSaveContext; otherwise errors are thrown).
7994  */
7995 static bool
7996 apply_typmod_special(Numeric num, int32 typmod, Node *escontext)
7997 {
7998  int precision;
7999  int scale;
8000 
8001  Assert(NUMERIC_IS_SPECIAL(num)); /* caller error if not */
8002 
8003  /*
8004  * NaN is allowed regardless of the typmod; that's rather dubious perhaps,
8005  * but it's a longstanding behavior. Inf is rejected if we have any
8006  * typmod restriction, since an infinity shouldn't be claimed to fit in
8007  * any finite number of digits.
8008  */
8009  if (NUMERIC_IS_NAN(num))
8010  return true;
8011 
8012  /* Do nothing if we have a default typmod (-1) */
8013  if (!is_valid_numeric_typmod(typmod))
8014  return true;
8015 
8016  precision = numeric_typmod_precision(typmod);
8017  scale = numeric_typmod_scale(typmod);
8018 
8019  ereturn(escontext, false,
8020  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8021  errmsg("numeric field overflow"),
8022  errdetail("A field with precision %d, scale %d cannot hold an infinite value.",
8023  precision, scale)));
8024 }
8025 
8026 
8027 /*
8028  * Convert numeric to int8, rounding if needed.
8029  *
8030  * If overflow, return false (no error is raised). Return true if okay.
8031  */
8032 static bool
8033 numericvar_to_int64(const NumericVar *var, int64 *result)
8034 {
8036  int ndigits;
8037  int weight;
8038  int i;
8039  int64 val;
8040  bool neg;
8041  NumericVar rounded;
8042 
8043  /* Round to nearest integer */
8044  init_var(&rounded);
8045  set_var_from_var(var, &rounded);
8046  round_var(&rounded, 0);
8047 
8048  /* Check for zero input */
8049  strip_var(&rounded);
8050  ndigits = rounded.ndigits;
8051  if (ndigits == 0)
8052  {
8053  *result = 0;
8054  free_var(&rounded);
8055  return true;
8056  }
8057 
8058  /*
8059  * For input like 10000000000, we must treat stripped digits as real. So
8060  * the loop assumes there are weight+1 digits before the decimal point.
8061  */
8062  weight = rounded.weight;
8063  Assert(weight >= 0 && ndigits <= weight + 1);
8064 
8065  /*
8066  * Construct the result. To avoid issues with converting a value
8067  * corresponding to INT64_MIN (which can't be represented as a positive 64
8068  * bit two's complement integer), accumulate value as a negative number.
8069  */
8070  digits = rounded.digits;
8071  neg = (rounded.sign == NUMERIC_NEG);
8072  val = -digits[0];
8073  for (i = 1; i <= weight; i++)
8074  {
8076  {
8077  free_var(&rounded);
8078  return false;
8079  }
8080 
8081  if (i < ndigits)
8082  {
8084  {
8085  free_var(&rounded);
8086  return false;
8087  }
8088  }
8089  }
8090 
8091  free_var(&rounded);
8092 
8093  if (!neg)
8094  {
8095  if (unlikely(val == PG_INT64_MIN))
8096  return false;
8097  val = -val;
8098  }
8099  *result = val;
8100 
8101  return true;
8102 }
8103 
8104 /*
8105  * Convert int8 value to numeric.
8106  */
8107 static void
8109 {
8110  uint64 uval,
8111  newuval;
8112  NumericDigit *ptr;
8113  int ndigits;
8114 
8115  /* int64 can require at most 19 decimal digits; add one for safety */
8116  alloc_var(var, 20 / DEC_DIGITS);
8117  if (val < 0)
8118  {
8119  var->sign = NUMERIC_NEG;
8120  uval = -val;
8121  }
8122  else
8123  {
8124  var->sign = NUMERIC_POS;
8125  uval = val;
8126  }
8127  var->dscale = 0;
8128  if (val == 0)
8129  {
8130  var->ndigits = 0;
8131  var->weight = 0;
8132  return;
8133  }
8134  ptr = var->digits + var->ndigits;
8135  ndigits = 0;
8136  do
8137  {
8138  ptr--;
8139  ndigits++;
8140  newuval = uval / NBASE;
8141  *ptr = uval - newuval * NBASE;
8142  uval = newuval;
8143  } while (uval);
8144  var->digits = ptr;
8145  var->ndigits = ndigits;
8146  var->weight = ndigits - 1;
8147 }
8148 
8149 /*
8150  * Convert numeric to uint64, rounding if needed.
8151  *
8152  * If overflow, return false (no error is raised). Return true if okay.
8153  */
8154 static bool
8155 numericvar_to_uint64(const NumericVar *var, uint64 *result)
8156 {
8158  int ndigits;
8159  int weight;
8160  int i;
8161  uint64 val;
8162  NumericVar rounded;
8163 
8164  /* Round to nearest integer */
8165  init_var(&rounded);
8166  set_var_from_var(var, &rounded);
8167  round_var(&rounded, 0);
8168 
8169  /* Check for zero input */
8170  strip_var(&rounded);
8171  ndigits = rounded.ndigits;
8172  if (ndigits == 0)
8173  {
8174  *result = 0;
8175  free_var(&rounded);
8176  return true;
8177  }
8178 
8179  /* Check for negative input */
8180  if (rounded.sign == NUMERIC_NEG)
8181  {
8182  free_var(&rounded);
8183  return false;
8184  }
8185 
8186  /*
8187  * For input like 10000000000, we must treat stripped digits as real. So
8188  * the loop assumes there are weight+1 digits before the decimal point.
8189  */
8190  weight = rounded.weight;
8191  Assert(weight >= 0 && ndigits <= weight + 1);
8192 
8193  /* Construct the result */
8194  digits = rounded.digits;
8195  val = digits[0];
8196  for (i = 1; i <= weight; i++)
8197  {
8199  {
8200  free_var(&rounded);
8201  return false;
8202  }
8203 
8204  if (i < ndigits)
8205  {
8207  {
8208  free_var(&rounded);
8209  return false;
8210  }
8211  }
8212  }
8213 
8214  free_var(&rounded);
8215 
8216  *result = val;
8217 
8218  return true;
8219 }
8220 
8221 #ifdef HAVE_INT128
8222 /*
8223  * Convert numeric to int128, rounding if needed.
8224  *
8225  * If overflow, return false (no error is raised). Return true if okay.
8226  */
8227 static bool
8228 numericvar_to_int128(const NumericVar *var, int128 *result)
8229 {
8231  int ndigits;
8232  int weight;
8233  int i;
8234  int128 val,
8235  oldval;
8236  bool neg;
8237  NumericVar rounded;
8238 
8239  /* Round to nearest integer */
8240  init_var(&rounded);
8241  set_var_from_var(var, &rounded);
8242  round_var(&rounded, 0);
8243 
8244  /* Check for zero input */
8245  strip_var(&rounded);
8246  ndigits = rounded.ndigits;
8247  if (ndigits == 0)
8248  {
8249  *result = 0;
8250  free_var(&rounded);
8251  return true;
8252  }
8253 
8254  /*
8255  * For input like 10000000000, we must treat stripped digits as real. So
8256  * the loop assumes there are weight+1 digits before the decimal point.
8257  */
8258  weight = rounded.weight;
8259  Assert(weight >= 0 && ndigits <= weight + 1);
8260 
8261  /* Construct the result */
8262  digits = rounded.digits;
8263  neg = (rounded.sign == NUMERIC_NEG);
8264  val = digits[0];
8265  for (i = 1; i <= weight; i++)
8266  {
8267  oldval = val;
8268  val *= NBASE;
8269  if (i < ndigits)
8270  val += digits[i];
8271 
8272  /*
8273  * The overflow check is a bit tricky because we want to accept
8274  * INT128_MIN, which will overflow the positive accumulator. We can
8275  * detect this case easily though because INT128_MIN is the only
8276  * nonzero value for which -val == val (on a two's complement machine,
8277  * anyway).
8278  */
8279  if ((val / NBASE) != oldval) /* possible overflow? */
8280  {
8281  if (!neg || (-val) != val || val == 0 || oldval < 0)
8282  {
8283  free_var(&rounded);
8284  return false;
8285  }
8286  }
8287  }
8288 
8289  free_var(&rounded);
8290 
8291  *result = neg ? -val : val;
8292  return true;
8293 }
8294 
8295 /*
8296  * Convert 128 bit integer to numeric.
8297  */
8298 static void
8299 int128_to_numericvar(int128 val, NumericVar *var)
8300 {
8301  uint128 uval,
8302  newuval;
8303  NumericDigit *ptr;
8304  int ndigits;
8305 
8306  /* int128 can require at most 39 decimal digits; add one for safety */
8307  alloc_var(var, 40 / DEC_DIGITS);
8308  if (val < 0)
8309  {
8310  var->sign = NUMERIC_NEG;
8311  uval = -val;
8312  }
8313  else
8314  {
8315  var->sign = NUMERIC_POS;
8316  uval = val;
8317  }
8318  var->dscale = 0;
8319  if (val == 0)
8320  {
8321  var->ndigits = 0;
8322  var->weight = 0;
8323  return;
8324  }
8325  ptr = var->digits + var->ndigits;
8326  ndigits = 0;
8327  do
8328  {
8329  ptr--;
8330  ndigits++;
8331  newuval = uval / NBASE;
8332  *ptr = uval - newuval * NBASE;
8333  uval = newuval;
8334  } while (uval);
8335  var->digits = ptr;
8336  var->ndigits = ndigits;
8337  var->weight = ndigits - 1;
8338 }
8339 #endif
8340 
8341 /*
8342  * Convert a NumericVar to float8; if out of range, return +/- HUGE_VAL
8343  */
8344 static double
8346 {
8347  char *tmp;
8348  double val;
8349  char *endptr;
8350 
8351  tmp = get_str_from_var(var);
8352 
8353  /* unlike float8in, we ignore ERANGE from strtod */
8354  val = strtod(tmp, &endptr);
8355  if (*endptr != '\0')
8356  {
8357  /* shouldn't happen ... */
8358  ereport(ERROR,
8359  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
8360  errmsg("invalid input syntax for type %s: \"%s\"",
8361  "double precision", tmp)));
8362  }
8363 
8364  pfree(tmp);
8365 
8366  return val;
8367 }
8368 
8369 
8370 /*
8371  * cmp_var() -
8372  *
8373  * Compare two values on variable level. We assume zeroes have been
8374  * truncated to no digits.
8375  */
8376 static int
8377 cmp_var(const NumericVar *var1, const NumericVar *var2)
8378 {
8379  return cmp_var_common(var1->digits, var1->ndigits,
8380  var1->weight, var1->sign,
8381  var2->digits, var2->ndigits,
8382  var2->weight, var2->sign);
8383 }
8384 
8385 /*
8386  * cmp_var_common() -
8387  *
8388  * Main routine of cmp_var(). This function can be used by both
8389  * NumericVar and Numeric.
8390  */
8391 static int
8392 cmp_var_common(const NumericDigit *var1digits, int var1ndigits,
8393  int var1weight, int var1sign,
8394  const NumericDigit *var2digits, int var2ndigits,
8395  int var2weight, int var2sign)
8396 {
8397  if (var1ndigits == 0)
8398  {
8399  if (var2ndigits == 0)
8400  return 0;
8401  if (var2sign == NUMERIC_NEG)
8402  return 1;
8403  return -1;
8404  }
8405  if (var2ndigits == 0)
8406  {
8407  if (var1sign == NUMERIC_POS)
8408  return 1;
8409  return -1;
8410  }
8411 
8412  if (var1sign == NUMERIC_POS)
8413  {
8414  if (var2sign == NUMERIC_NEG)
8415  return 1;
8416  return cmp_abs_common(var1digits, var1ndigits, var1weight,
8417  var2digits, var2ndigits, var2weight);
8418  }
8419 
8420  if (var2sign == NUMERIC_POS)
8421  return -1;
8422 
8423  return cmp_abs_common(var2digits, var2ndigits, var2weight,
8424  var1digits, var1ndigits, var1weight);
8425 }
8426 
8427 
8428 /*
8429  * add_var() -
8430  *
8431  * Full version of add functionality on variable level (handling signs).
8432  * result might point to one of the operands too without danger.
8433  */
8434 static void
8435 add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
8436 {
8437  /*
8438  * Decide on the signs of the two variables what to do
8439  */
8440  if (var1->sign == NUMERIC_POS)
8441  {
8442  if (var2->sign == NUMERIC_POS)
8443  {
8444  /*
8445  * Both are positive result = +(ABS(var1) + ABS(var2))
8446  */
8447  add_abs(var1, var2, result);
8448  result->sign = NUMERIC_POS;
8449  }
8450  else
8451  {
8452  /*
8453  * var1 is positive, var2 is negative Must compare absolute values
8454  */
8455  switch (cmp_abs(var1, var2))
8456  {
8457  case 0:
8458  /* ----------
8459  * ABS(var1) == ABS(var2)
8460  * result = ZERO
8461  * ----------
8462  */
8463  zero_var(result);
8464  result->dscale = Max(var1->dscale, var2->dscale);
8465  break;
8466 
8467  case 1:
8468  /* ----------
8469  * ABS(var1) > ABS(var2)
8470  * result = +(ABS(var1) - ABS(var2))
8471  * ----------
8472  */
8473  sub_abs(var1, var2, result);
8474  result->sign = NUMERIC_POS;
8475  break;
8476 
8477  case -1:
8478  /* ----------
8479  * ABS(var1) < ABS(var2)
8480  * result = -(ABS(var2) - ABS(var1))
8481  * ----------
8482  */
8483  sub_abs(var2, var1, result);
8484  result->sign = NUMERIC_NEG;
8485  break;
8486  }
8487  }
8488  }
8489  else
8490  {
8491  if (var2->sign == NUMERIC_POS)
8492  {
8493  /* ----------
8494  * var1 is negative, var2 is positive
8495  * Must compare absolute values
8496  * ----------
8497  */
8498  switch (cmp_abs(var1, var2))
8499  {
8500  case 0:
8501  /* ----------
8502  * ABS(var1) == ABS(var2)
8503  * result = ZERO
8504  * ----------
8505  */
8506  zero_var(result);
8507  result->dscale = Max(var1->dscale, var2->dscale);
8508  break;
8509 
8510  case 1:
8511  /* ----------
8512  * ABS(var1) > ABS(var2)
8513  * result = -(ABS(var1) - ABS(var2))
8514  * ----------
8515  */
8516  sub_abs(var1, var2, result);
8517  result->sign = NUMERIC_NEG;
8518  break;
8519 
8520  case -1:
8521  /* ----------
8522  * ABS(var1) < ABS(var2)
8523  * result = +(ABS(var2) - ABS(var1))
8524  * ----------
8525  */
8526  sub_abs(var2, var1, result);
8527  result->sign = NUMERIC_POS;
8528  break;
8529  }
8530  }
8531  else
8532  {
8533  /* ----------
8534  * Both are negative
8535  * result = -(ABS(var1) + ABS(var2))
8536  * ----------
8537  */
8538  add_abs(var1, var2, result);
8539  result->sign = NUMERIC_NEG;
8540  }
8541  }
8542 }
8543 
8544 
8545 /*
8546  * sub_var() -
8547  *
8548  * Full version of sub functionality on variable level (handling signs).
8549  * result might point to one of the operands too without danger.
8550  */
8551 static void
8552 sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
8553 {
8554  /*
8555  * Decide on the signs of the two variables what to do
8556  */
8557  if (var1->sign == NUMERIC_POS)
8558  {
8559  if (var2->sign == NUMERIC_NEG)
8560  {
8561  /* ----------
8562  * var1 is positive, var2 is negative
8563  * result = +(ABS(var1) + ABS(var2))
8564  * ----------
8565  */
8566  add_abs(var1, var2, result);
8567  result->sign = NUMERIC_POS;
8568  }
8569  else
8570  {
8571  /* ----------
8572  * Both are positive
8573  * Must compare absolute values
8574  * ----------
8575  */
8576  switch (cmp_abs(var1, var2))
8577  {
8578  case 0:
8579  /* ----------
8580  * ABS(var1) == ABS(var2)
8581  * result = ZERO
8582  * ----------
8583  */
8584  zero_var(result);
8585  result->dscale = Max(var1->dscale, var2->dscale);
8586  break;
8587 
8588  case 1:
8589  /* ----------
8590  * ABS(var1) > ABS(var2)
8591  * result = +(ABS(var1) - ABS(var2))
8592  * ----------
8593  */
8594  sub_abs(var1, var2, result);
8595  result->sign = NUMERIC_POS;
8596  break;
8597 
8598  case -1:
8599  /* ----------
8600  * ABS(var1) < ABS(var2)
8601  * result = -(ABS(var2) - ABS(var1))
8602  * ----------
8603  */
8604  sub_abs(var2, var1, result);
8605  result->sign = NUMERIC_NEG;
8606  break;
8607  }
8608  }
8609  }
8610  else
8611  {
8612  if (var2->sign == NUMERIC_NEG)
8613  {
8614  /* ----------
8615  * Both are negative
8616  * Must compare absolute values
8617  * ----------
8618  */
8619  switch (cmp_abs(var1, var2))
8620  {
8621  case 0:
8622  /* ----------
8623  * ABS(var1) == ABS(var2)
8624  * result = ZERO
8625  * ----------
8626  */
8627  zero_var(result);
8628  result->dscale = Max(var1->dscale, var2->dscale);
8629  break;
8630 
8631  case 1:
8632  /* ----------
8633  * ABS(var1) > ABS(var2)
8634  * result = -(ABS(var1) - ABS(var2))
8635  * ----------
8636  */
8637  sub_abs(var1, var2, result);
8638  result->sign = NUMERIC_NEG;
8639  break;
8640 
8641  case -1:
8642  /* ----------
8643  * ABS(var1) < ABS(var2)
8644  * result = +(ABS(var2) - ABS(var1))
8645  * ----------
8646  */
8647  sub_abs(var2, var1, result);
8648  result->sign = NUMERIC_POS;
8649  break;
8650  }
8651  }
8652  else
8653  {
8654  /* ----------
8655  * var1 is negative, var2 is positive
8656  * result = -(ABS(var1) + ABS(var2))
8657  * ----------
8658  */
8659  add_abs(var1, var2, result);
8660  result->sign = NUMERIC_NEG;
8661  }
8662  }
8663 }
8664 
8665 
8666 /*
8667  * mul_var() -
8668  *
8669  * Multiplication on variable level. Product of var1 * var2 is stored
8670  * in result. Result is rounded to no more than rscale fractional digits.
8671  */
8672 static void
8673 mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result,
8674  int rscale)
8675 {
8676  int res_ndigits;
8677  int res_sign;
8678  int res_weight;
8679  int maxdigits;
8680  int *dig;
8681  int carry;
8682  int maxdig;
8683  int newdig;
8684  int var1ndigits;
8685  int var2ndigits;
8686  NumericDigit *var1digits;
8687  NumericDigit *var2digits;
8688  NumericDigit *res_digits;
8689  int i,
8690  i1,
8691  i2;
8692 
8693  /*
8694  * Arrange for var1 to be the shorter of the two numbers. This improves
8695  * performance because the inner multiplication loop is much simpler than
8696  * the outer loop, so it's better to have a smaller number of iterations
8697  * of the outer loop. This also reduces the number of times that the
8698  * accumulator array needs to be normalized.
8699  */
8700  if (var1->ndigits > var2->ndigits)
8701  {
8702  const NumericVar *tmp = var1;
8703 
8704  var1 = var2;
8705  var2 = tmp;
8706  }
8707 
8708  /* copy these values into local vars for speed in inner loop */
8709  var1ndigits = var1->ndigits;
8710  var2ndigits = var2->ndigits;
8711  var1digits = var1->digits;
8712  var2digits = var2->digits;
8713 
8714  if (var1ndigits == 0)
8715  {
8716  /* one or both inputs is zero; so is result */
8717  zero_var(result);
8718  result->dscale = rscale;
8719  return;
8720  }
8721 
8722  /*
8723  * If var1 has 1-4 digits and the exact result was requested, delegate to
8724  * mul_var_short() which uses a faster direct multiplication algorithm.
8725  */
8726  if (var1ndigits <= 4 && rscale == var1->dscale + var2->dscale)
8727  {
8728  mul_var_short(var1, var2, result);
8729  return;
8730  }
8731 
8732  /* Determine result sign and (maximum possible) weight */
8733  if (var1->sign == var2->sign)
8734  res_sign = NUMERIC_POS;
8735  else
8736  res_sign = NUMERIC_NEG;
8737  res_weight = var1->weight + var2->weight + 2;
8738 
8739  /*
8740  * Determine the number of result digits to compute. If the exact result
8741  * would have more than rscale fractional digits, truncate the computation
8742  * with MUL_GUARD_DIGITS guard digits, i.e., ignore input digits that
8743  * would only contribute to the right of that. (This will give the exact
8744  * rounded-to-rscale answer unless carries out of the ignored positions
8745  * would have propagated through more than MUL_GUARD_DIGITS digits.)
8746  *
8747  * Note: an exact computation could not produce more than var1ndigits +
8748  * var2ndigits digits, but we allocate one extra output digit in case
8749  * rscale-driven rounding produces a carry out of the highest exact digit.
8750  */
8751  res_ndigits = var1ndigits + var2ndigits + 1;
8752  maxdigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS +
8754  res_ndigits = Min(res_ndigits, maxdigits);
8755 
8756  if (res_ndigits < 3)
8757  {
8758  /* All input digits will be ignored; so result is zero */
8759  zero_var(result);
8760  result->dscale = rscale;
8761  return;
8762  }
8763 
8764  /*
8765  * We do the arithmetic in an array "dig[]" of signed int's. Since
8766  * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
8767  * to avoid normalizing carries immediately.
8768  *
8769  * maxdig tracks the maximum possible value of any dig[] entry; when this
8770  * threatens to exceed INT_MAX, we take the time to propagate carries.
8771  * Furthermore, we need to ensure that overflow doesn't occur during the
8772  * carry propagation passes either. The carry values could be as much as
8773  * INT_MAX/NBASE, so really we must normalize when digits threaten to
8774  * exceed INT_MAX - INT_MAX/NBASE.
8775  *
8776  * To avoid overflow in maxdig itself, it actually represents the max
8777  * possible value divided by NBASE-1, ie, at the top of the loop it is
8778  * known that no dig[] entry exceeds maxdig * (NBASE-1).
8779  */
8780  dig = (int *) palloc0(res_ndigits * sizeof(int));
8781  maxdig = 0;
8782 
8783  /*
8784  * The least significant digits of var1 should be ignored if they don't
8785  * contribute directly to the first res_ndigits digits of the result that
8786  * we are computing.
8787  *
8788  * Digit i1 of var1 and digit i2 of var2 are multiplied and added to digit
8789  * i1+i2+2 of the accumulator array, so we need only consider digits of
8790  * var1 for which i1 <= res_ndigits - 3.
8791  */
8792  for (i1 = Min(var1ndigits - 1, res_ndigits - 3); i1 >= 0; i1--)
8793  {
8794  NumericDigit var1digit = var1digits[i1];
8795 
8796  if (var1digit == 0)
8797  continue;
8798 
8799  /* Time to normalize? */
8800  maxdig += var1digit;
8801  if (maxdig > (INT_MAX - INT_MAX / NBASE) / (NBASE - 1))
8802  {
8803  /* Yes, do it */
8804  carry = 0;
8805  for (i = res_ndigits - 1; i >= 0; i--)
8806  {
8807  newdig = dig[i] + carry;
8808  if (newdig >= NBASE)
8809  {
8810  carry = newdig / NBASE;
8811  newdig -= carry * NBASE;
8812  }
8813  else
8814  carry = 0;
8815  dig[i] = newdig;
8816  }
8817  Assert(carry == 0);
8818  /* Reset maxdig to indicate new worst-case */
8819  maxdig = 1 + var1digit;
8820  }
8821 
8822  /*
8823  * Add the appropriate multiple of var2 into the accumulator.
8824  *
8825  * As above, digits of var2 can be ignored if they don't contribute,
8826  * so we only include digits for which i1+i2+2 < res_ndigits.
8827  *
8828  * This inner loop is the performance bottleneck for multiplication,
8829  * so we want to keep it simple enough so that it can be
8830  * auto-vectorized. Accordingly, process the digits left-to-right
8831  * even though schoolbook multiplication would suggest right-to-left.
8832  * Since we aren't propagating carries in this loop, the order does
8833  * not matter.
8834  */
8835  {
8836  int i2limit = Min(var2ndigits, res_ndigits - i1 - 2);
8837  int *dig_i1_2 = &dig[i1 + 2];
8838 
8839  for (i2 = 0; i2 < i2limit; i2++)
8840  dig_i1_2[i2] += var1digit * var2digits[i2];
8841  }
8842  }
8843 
8844  /*
8845  * Now we do a final carry propagation pass to normalize the result, which
8846  * we combine with storing the result digits into the output. Note that
8847  * this is still done at full precision w/guard digits.
8848  */
8849  alloc_var(result, res_ndigits);
8850  res_digits = result->digits;
8851  carry = 0;
8852  for (i = res_ndigits - 1; i >= 0; i--)
8853  {
8854  newdig = dig[i] + carry;
8855  if (newdig >= NBASE)
8856  {
8857  carry = newdig / NBASE;
8858  newdig -= carry * NBASE;
8859  }
8860  else
8861  carry = 0;
8862  res_digits[i] = newdig;
8863  }
8864  Assert(carry == 0);
8865 
8866  pfree(dig);
8867 
8868  /*
8869  * Finally, round the result to the requested precision.
8870  */
8871  result->weight = res_weight;
8872  result->sign = res_sign;
8873 
8874  /* Round to target rscale (and set result->dscale) */
8875  round_var(result, rscale);
8876 
8877  /* Strip leading and trailing zeroes */
8878  strip_var(result);
8879 }
8880 
8881 
8882 /*
8883  * mul_var_short() -
8884  *
8885  * Special-case multiplication function used when var1 has 1-4 digits, var2
8886  * has at least as many digits as var1, and the exact product var1 * var2 is
8887  * requested.
8888  */
8889 static void
8890 mul_var_short(const NumericVar *var1, const NumericVar *var2,
8891  NumericVar *result)
8892 {
8893  int var1ndigits = var1->ndigits;
8894  int var2ndigits = var2->ndigits;
8895  NumericDigit *var1digits = var1->digits;
8896  NumericDigit *var2digits = var2->digits;
8897  int res_sign;
8898  int res_weight;
8899  int res_ndigits;
8900  NumericDigit *res_buf;
8901  NumericDigit *res_digits;
8902  uint32 carry;
8903  uint32 term;
8904 
8905  /* Check preconditions */
8906  Assert(var1ndigits >= 1);
8907  Assert(var1ndigits <= 4);
8908  Assert(var2ndigits >= var1ndigits);
8909 
8910  /*
8911  * Determine the result sign, weight, and number of digits to calculate.
8912  * The weight figured here is correct if the product has no leading zero
8913  * digits; otherwise strip_var() will fix things up. Note that, unlike
8914  * mul_var(), we do not need to allocate an extra output digit, because we
8915  * are not rounding here.
8916  */
8917  if (var1->sign == var2->sign)
8918  res_sign = NUMERIC_POS;
8919  else
8920  res_sign = NUMERIC_NEG;
8921  res_weight = var1->weight + var2->weight + 1;
8922  res_ndigits = var1ndigits + var2ndigits;
8923 
8924  /* Allocate result digit array */
8925  res_buf = digitbuf_alloc(res_ndigits + 1);
8926  res_buf[0] = 0; /* spare digit for later rounding */
8927  res_digits = res_buf + 1;
8928 
8929  /*
8930  * Compute the result digits in reverse, in one pass, propagating the
8931  * carry up as we go. The i'th result digit consists of the sum of the
8932  * products var1digits[i1] * var2digits[i2] for which i = i1 + i2 + 1.
8933  */
8934  switch (var1ndigits)
8935  {
8936  case 1:
8937  /* ---------
8938  * 1-digit case:
8939  * var1ndigits = 1
8940  * var2ndigits >= 1
8941  * res_ndigits = var2ndigits + 1
8942  * ----------
8943  */
8944  carry = 0;
8945  for (int i = res_ndigits - 2; i >= 0; i--)
8946  {
8947  term = (uint32) var1digits[0] * var2digits[i] + carry;
8948  res_digits[i + 1] = (NumericDigit) (term % NBASE);
8949  carry = term / NBASE;
8950  }
8951  res_digits[0] = (NumericDigit) carry;
8952  break;
8953 
8954  case 2:
8955  /* ---------
8956  * 2-digit case:
8957  * var1ndigits = 2
8958  * var2ndigits >= 2
8959  * res_ndigits = var2ndigits + 2
8960  * ----------
8961  */
8962  /* last result digit and carry */
8963  term = (uint32) var1digits[1] * var2digits[res_ndigits - 3];
8964  res_digits[res_ndigits - 1] = (NumericDigit) (term % NBASE);
8965  carry = term / NBASE;
8966 
8967  /* remaining digits, except for the first two */
8968  for (int i = res_ndigits - 3; i >= 1; i--)
8969  {
8970  term = (uint32) var1digits[0] * var2digits[i] +
8971  (uint32) var1digits[1] * var2digits[i - 1] + carry;
8972  res_digits[i + 1] = (NumericDigit) (term % NBASE);
8973  carry = term / NBASE;
8974  }
8975 
8976  /* first two digits */
8977  term = (uint32) var1digits[0] * var2digits[0] + carry;
8978  res_digits[1] = (NumericDigit) (term % NBASE);
8979  res_digits[0] = (NumericDigit) (term / NBASE);
8980  break;
8981 
8982  case 3:
8983  /* ---------
8984  * 3-digit case:
8985  * var1ndigits = 3
8986  * var2ndigits >= 3
8987  * res_ndigits = var2ndigits + 3
8988  * ----------
8989  */
8990  /* last two result digits */
8991  term = (uint32) var1digits[2] * var2digits[res_ndigits - 4];
8992  res_digits[res_ndigits - 1] = (NumericDigit) (term % NBASE);
8993  carry = term / NBASE;
8994 
8995  term = (uint32) var1digits[1] * var2digits[res_ndigits - 4] +
8996  (uint32) var1digits[2] * var2digits[res_ndigits - 5] + carry;
8997  res_digits[res_ndigits - 2] = (NumericDigit) (term % NBASE);
8998  carry = term / NBASE;
8999 
9000  /* remaining digits, except for the first three */
9001  for (int i = res_ndigits - 4; i >= 2; i--)
9002  {
9003  term = (uint32) var1digits[0] * var2digits[i] +
9004  (uint32) var1digits[1] * var2digits[i - 1] +
9005  (uint32) var1digits[2] * var2digits[i - 2] + carry;
9006  res_digits[i + 1] = (NumericDigit) (term % NBASE);
9007  carry = term / NBASE;
9008  }
9009 
9010  /* first three digits */
9011  term = (uint32) var1digits[0] * var2digits[1] +
9012  (uint32) var1digits[1] * var2digits[0] + carry;
9013  res_digits[2] = (NumericDigit) (term % NBASE);
9014  carry = term / NBASE;
9015 
9016  term = (uint32) var1digits[0] * var2digits[0] + carry;
9017  res_digits[1] = (NumericDigit) (term % NBASE);
9018  res_digits[0] = (NumericDigit) (term / NBASE);
9019  break;
9020 
9021  case 4:
9022  /* ---------
9023  * 4-digit case:
9024  * var1ndigits = 4
9025  * var2ndigits >= 4
9026  * res_ndigits = var2ndigits + 4
9027  * ----------
9028  */
9029  /* last three result digits */
9030  term = (uint32) var1digits[3] * var2digits[res_ndigits - 5];
9031  res_digits[res_ndigits - 1] = (NumericDigit) (term % NBASE);
9032  carry = term / NBASE;
9033 
9034  term = (uint32) var1digits[2] * var2digits[res_ndigits - 5] +
9035  (uint32) var1digits[3] * var2digits[res_ndigits - 6] + carry;
9036  res_digits[res_ndigits - 2] = (NumericDigit) (term % NBASE);
9037  carry = term / NBASE;
9038 
9039  term = (uint32) var1digits[1] * var2digits[res_ndigits - 5] +
9040  (uint32) var1digits[2] * var2digits[res_ndigits - 6] +
9041  (uint32) var1digits[3] * var2digits[res_ndigits - 7] + carry;
9042  res_digits[res_ndigits - 3] = (NumericDigit) (term % NBASE);
9043  carry = term / NBASE;
9044 
9045  /* remaining digits, except for the first four */
9046  for (int i = res_ndigits - 5; i >= 3; i--)
9047  {
9048  term = (uint32) var1digits[0] * var2digits[i] +
9049  (uint32) var1digits[1] * var2digits[i - 1] +
9050  (uint32) var1digits[2] * var2digits[i - 2] +
9051  (uint32) var1digits[3] * var2digits[i - 3] + carry;
9052  res_digits[i + 1] = (NumericDigit) (term % NBASE);
9053  carry = term / NBASE;
9054  }
9055 
9056  /* first four digits */
9057  term = (uint32) var1digits[0] * var2digits[2] +
9058  (uint32) var1digits[1] * var2digits[1] +
9059  (uint32) var1digits[2] * var2digits[0] + carry;
9060  res_digits[3] = (NumericDigit) (term % NBASE);
9061  carry = term / NBASE;
9062 
9063  term = (uint32) var1digits[0] * var2digits[1] +
9064  (uint32) var1digits[1] * var2digits[0] + carry;
9065  res_digits[2] = (NumericDigit) (term % NBASE);
9066  carry = term / NBASE;
9067 
9068  term = (uint32) var1digits[0] * var2digits[0] + carry;
9069  res_digits[1] = (NumericDigit) (term % NBASE);
9070  res_digits[0] = (NumericDigit) (term / NBASE);
9071  break;
9072  }
9073 
9074  /* Store the product in result */
9075  digitbuf_free(result->buf);
9076  result->ndigits = res_ndigits;
9077  result->buf = res_buf;
9078  result->digits = res_digits;
9079  result->weight = res_weight;
9080  result->sign = res_sign;
9081  result->dscale = var1->dscale + var2->dscale;
9082 
9083  /* Strip leading and trailing zeroes */
9084  strip_var(result);
9085 }
9086 
9087 
9088 /*
9089  * div_var() -
9090  *
9091  * Division on variable level. Quotient of var1 / var2 is stored in result.
9092  * The quotient is figured to exactly rscale fractional digits.
9093  * If round is true, it is rounded at the rscale'th digit; if false, it
9094  * is truncated (towards zero) at that digit.
9095  */
9096 static void
9097 div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result,
9098  int rscale, bool round)
9099 {
9100  int div_ndigits;
9101  int res_ndigits;
9102  int res_sign;
9103  int res_weight;
9104  int carry;
9105  int borrow;
9106  int divisor1;
9107  int divisor2;
9108  NumericDigit *dividend;
9109  NumericDigit *divisor;
9110  NumericDigit *res_digits;
9111  int i;
9112  int j;
9113 
9114  /* copy these values into local vars for speed in inner loop */
9115  int var1ndigits = var1->ndigits;
9116  int var2ndigits = var2->ndigits;
9117 
9118  /*
9119  * First of all division by zero check; we must not be handed an
9120  * unnormalized divisor.
9121  */
9122  if (var2ndigits == 0 || var2->digits[0] == 0)
9123  ereport(ERROR,
9124  (errcode(ERRCODE_DIVISION_BY_ZERO),
9125  errmsg("division by zero")));
9126 
9127  /*
9128  * If the divisor has just one or two digits, delegate to div_var_int(),
9129  * which uses fast short division.
9130  *
9131  * Similarly, on platforms with 128-bit integer support, delegate to
9132  * div_var_int64() for divisors with three or four digits.
9133  */
9134  if (var2ndigits <= 2)
9135  {
9136  int idivisor;
9137  int idivisor_weight;
9138 
9139  idivisor = var2->digits[0];
9140  idivisor_weight = var2->weight;
9141  if (var2ndigits == 2)
9142  {
9143  idivisor = idivisor * NBASE + var2->digits[1];
9144  idivisor_weight--;
9145  }
9146  if (var2->sign == NUMERIC_NEG)
9147  idivisor = -idivisor;
9148 
9149  div_var_int(var1, idivisor, idivisor_weight, result, rscale, round);
9150  return;
9151  }
9152 #ifdef HAVE_INT128
9153  if (var2ndigits <= 4)
9154  {
9155  int64 idivisor;
9156  int idivisor_weight;
9157 
9158  idivisor = var2->digits[0];
9159  idivisor_weight = var2->weight;
9160  for (i = 1; i < var2ndigits; i++)
9161  {
9162  idivisor = idivisor * NBASE + var2->digits[i];
9163  idivisor_weight--;
9164  }
9165  if (var2->sign == NUMERIC_NEG)
9166  idivisor = -idivisor;
9167 
9168  div_var_int64(var1, idivisor, idivisor_weight, result, rscale, round);
9169  return;
9170  }
9171 #endif
9172 
9173  /*
9174  * Otherwise, perform full long division.
9175  */
9176 
9177  /* Result zero check */
9178  if (var1ndigits == 0)
9179  {
9180  zero_var(result);
9181  result->dscale = rscale;
9182  return;
9183  }
9184 
9185  /*
9186  * Determine the result sign, weight and number of digits to calculate.
9187  * The weight figured here is correct if the emitted quotient has no
9188  * leading zero digits; otherwise strip_var() will fix things up.
9189  */
9190  if (var1->sign == var2->sign)
9191  res_sign = NUMERIC_POS;
9192  else
9193  res_sign = NUMERIC_NEG;
9194  res_weight = var1->weight - var2->weight;
9195  /* The number of accurate result digits we need to produce: */
9196  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
9197  /* ... but always at least 1 */
9198  res_ndigits = Max(res_ndigits, 1);
9199  /* If rounding needed, figure one more digit to ensure correct result */
9200  if (round)
9201  res_ndigits++;
9202 
9203  /*
9204  * The working dividend normally requires res_ndigits + var2ndigits
9205  * digits, but make it at least var1ndigits so we can load all of var1
9206  * into it. (There will be an additional digit dividend[0] in the
9207  * dividend space, but for consistency with Knuth's notation we don't
9208  * count that in div_ndigits.)
9209  */
9210  div_ndigits = res_ndigits + var2ndigits;
9211  div_ndigits = Max(div_ndigits, var1ndigits);
9212 
9213  /*
9214  * We need a workspace with room for the working dividend (div_ndigits+1
9215  * digits) plus room for the possibly-normalized divisor (var2ndigits
9216  * digits). It is convenient also to have a zero at divisor[0] with the
9217  * actual divisor data in divisor[1 .. var2ndigits]. Transferring the
9218  * digits into the workspace also allows us to realloc the result (which
9219  * might be the same as either input var) before we begin the main loop.
9220  * Note that we use palloc0 to ensure that divisor[0], dividend[0], and
9221  * any additional dividend positions beyond var1ndigits, start out 0.
9222  */
9223  dividend = (NumericDigit *)
9224  palloc0((div_ndigits + var2ndigits + 2) * sizeof(NumericDigit));
9225  divisor = dividend + (div_ndigits + 1);
9226  memcpy(dividend + 1, var1->digits, var1ndigits * sizeof(NumericDigit));
9227  memcpy(divisor + 1, var2->digits, var2ndigits * sizeof(NumericDigit));
9228 
9229  /*
9230  * Now we can realloc the result to hold the generated quotient digits.
9231  */
9232  alloc_var(result, res_ndigits);
9233  res_digits = result->digits;
9234 
9235  /*
9236  * The full multiple-place algorithm is taken from Knuth volume 2,
9237  * Algorithm 4.3.1D.
9238  *
9239  * We need the first divisor digit to be >= NBASE/2. If it isn't, make it
9240  * so by scaling up both the divisor and dividend by the factor "d". (The
9241  * reason for allocating dividend[0] above is to leave room for possible
9242  * carry here.)
9243  */
9244  if (divisor[1] < HALF_NBASE)
9245  {
9246  int d = NBASE / (divisor[1] + 1);
9247 
9248  carry = 0;
9249  for (i = var2ndigits; i > 0; i--)
9250  {
9251  carry += divisor[i] * d;
9252  divisor[i] = carry % NBASE;
9253  carry = carry / NBASE;
9254  }
9255  Assert(carry == 0);
9256  carry = 0;
9257  /* at this point only var1ndigits of dividend can be nonzero */
9258  for (i = var1ndigits; i >= 0; i--)
9259  {
9260  carry += dividend[i] * d;
9261  dividend[i] = carry % NBASE;
9262  carry = carry / NBASE;
9263  }
9264  Assert(carry == 0);
9265  Assert(divisor[1] >= HALF_NBASE);
9266  }
9267  /* First 2 divisor digits are used repeatedly in main loop */
9268  divisor1 = divisor[1];
9269  divisor2 = divisor[2];
9270 
9271  /*
9272  * Begin the main loop. Each iteration of this loop produces the j'th
9273  * quotient digit by dividing dividend[j .. j + var2ndigits] by the
9274  * divisor; this is essentially the same as the common manual procedure
9275  * for long division.
9276  */
9277  for (j = 0; j < res_ndigits; j++)
9278  {
9279  /* Estimate quotient digit from the first two dividend digits */
9280  int next2digits = dividend[j] * NBASE + dividend[j + 1];
9281  int qhat;
9282 
9283  /*
9284  * If next2digits are 0, then quotient digit must be 0 and there's no
9285  * need to adjust the working dividend. It's worth testing here to
9286  * fall out ASAP when processing trailing zeroes in a dividend.
9287  */
9288  if (next2digits == 0)
9289  {
9290  res_digits[j] = 0;
9291  continue;
9292  }
9293 
9294  if (dividend[j] == divisor1)
9295  qhat = NBASE - 1;
9296  else
9297  qhat = next2digits / divisor1;
9298 
9299  /*
9300  * Adjust quotient digit if it's too large. Knuth proves that after
9301  * this step, the quotient digit will be either correct or just one
9302  * too large. (Note: it's OK to use dividend[j+2] here because we
9303  * know the divisor length is at least 2.)
9304  */
9305  while (divisor2 * qhat >
9306  (next2digits - qhat * divisor1) * NBASE + dividend[j + 2])
9307  qhat--;
9308 
9309  /* As above, need do nothing more when quotient digit is 0 */
9310  if (qhat > 0)
9311  {
9312  NumericDigit *dividend_j = &dividend[j];
9313 
9314  /*
9315  * Multiply the divisor by qhat, and subtract that from the
9316  * working dividend. The multiplication and subtraction are
9317  * folded together here, noting that qhat <= NBASE (since it might
9318  * be one too large), and so the intermediate result "tmp_result"
9319  * is in the range [-NBASE^2, NBASE - 1], and "borrow" is in the
9320  * range [0, NBASE].
9321  */
9322  borrow = 0;
9323  for (i = var2ndigits; i >= 0; i--)
9324  {
9325  int tmp_result;
9326 
9327  tmp_result = dividend_j[i] - borrow - divisor[i] * qhat;
9328  borrow = (NBASE - 1 - tmp_result) / NBASE;
9329  dividend_j[i] = tmp_result + borrow * NBASE;
9330  }
9331 
9332  /*
9333  * If we got a borrow out of the top dividend digit, then indeed
9334  * qhat was one too large. Fix it, and add back the divisor to
9335  * correct the working dividend. (Knuth proves that this will
9336  * occur only about 3/NBASE of the time; hence, it's a good idea
9337  * to test this code with small NBASE to be sure this section gets
9338  * exercised.)
9339  */
9340  if (borrow)
9341  {
9342  qhat--;
9343  carry = 0;
9344  for (i = var2ndigits; i >= 0; i--)
9345  {
9346  carry += dividend_j[i] + divisor[i];
9347  if (carry >= NBASE)
9348  {
9349  dividend_j[i] = carry - NBASE;
9350  carry = 1;
9351  }
9352  else
9353  {
9354  dividend_j[i] = carry;
9355  carry = 0;
9356  }
9357  }
9358  /* A carry should occur here to cancel the borrow above */
9359  Assert(carry == 1);
9360  }
9361  }
9362 
9363  /* And we're done with this quotient digit */
9364  res_digits[j] = qhat;
9365  }
9366 
9367  pfree(dividend);
9368 
9369  /*
9370  * Finally, round or truncate the result to the requested precision.
9371  */
9372  result->weight = res_weight;
9373  result->sign = res_sign;
9374 
9375  /* Round or truncate to target rscale (and set result->dscale) */
9376  if (round)
9377  round_var(result, rscale);
9378  else
9379  trunc_var(result, rscale);
9380 
9381  /* Strip leading and trailing zeroes */
9382  strip_var(result);
9383 }
9384 
9385 
9386 /*
9387  * div_var_fast() -
9388  *
9389  * This has the same API as div_var, but is implemented using the division
9390  * algorithm from the "FM" library, rather than Knuth's schoolbook-division
9391  * approach. This is significantly faster but can produce inaccurate
9392  * results, because it sometimes has to propagate rounding to the left,
9393  * and so we can never be entirely sure that we know the requested digits
9394  * exactly. We compute DIV_GUARD_DIGITS extra digits, but there is
9395  * no certainty that that's enough. We use this only in the transcendental
9396  * function calculation routines, where everything is approximate anyway.
9397  *
9398  * Although we provide a "round" argument for consistency with div_var,
9399  * it is unwise to use this function with round=false. In truncation mode
9400  * it is possible to get a result with no significant digits, for example
9401  * with rscale=0 we might compute 0.99999... and truncate that to 0 when
9402  * the correct answer is 1.
9403  */
9404 static void
9405 div_var_fast(const NumericVar *var1, const NumericVar *var2,
9406  NumericVar *result, int rscale, bool round)
9407 {
9408  int div_ndigits;
9409  int load_ndigits;
9410  int res_sign;
9411  int res_weight;
9412  int *div;
9413  int qdigit;
9414  int carry;
9415  int maxdiv;
9416  int newdig;
9417  NumericDigit *res_digits;
9418  double fdividend,
9419  fdivisor,
9420  fdivisorinverse,
9421  fquotient;
9422  int qi;
9423  int i;
9424 
9425  /* copy these values into local vars for speed in inner loop */
9426  int var1ndigits = var1->ndigits;
9427  int var2ndigits = var2->ndigits;
9428  NumericDigit *var1digits = var1->digits;
9429  NumericDigit *var2digits = var2->digits;
9430 
9431  /*
9432  * First of all division by zero check; we must not be handed an
9433  * unnormalized divisor.
9434  */
9435  if (var2ndigits == 0 || var2digits[0] == 0)
9436  ereport(ERROR,
9437  (errcode(ERRCODE_DIVISION_BY_ZERO),
9438  errmsg("division by zero")));
9439 
9440  /*
9441  * If the divisor has just one or two digits, delegate to div_var_int(),
9442  * which uses fast short division.
9443  *
9444  * Similarly, on platforms with 128-bit integer support, delegate to
9445  * div_var_int64() for divisors with three or four digits.
9446  */
9447  if (var2ndigits <= 2)
9448  {
9449  int idivisor;
9450  int idivisor_weight;
9451 
9452  idivisor = var2->digits[0];
9453  idivisor_weight = var2->weight;
9454  if (var2ndigits == 2)
9455  {
9456  idivisor = idivisor * NBASE + var2->digits[1];
9457  idivisor_weight--;
9458  }
9459  if (var2->sign == NUMERIC_NEG)
9460  idivisor = -idivisor;
9461 
9462  div_var_int(var1, idivisor, idivisor_weight, result, rscale, round);
9463  return;
9464  }
9465 #ifdef HAVE_INT128
9466  if (var2ndigits <= 4)
9467  {
9468  int64 idivisor;
9469  int idivisor_weight;
9470 
9471  idivisor = var2->digits[0];
9472  idivisor_weight = var2->weight;
9473  for (i = 1; i < var2ndigits; i++)
9474  {
9475  idivisor = idivisor * NBASE + var2->digits[i];
9476  idivisor_weight--;
9477  }
9478  if (var2->sign == NUMERIC_NEG)
9479  idivisor = -idivisor;
9480 
9481  div_var_int64(var1, idivisor, idivisor_weight, result, rscale, round);
9482  return;
9483  }
9484 #endif
9485 
9486  /*
9487  * Otherwise, perform full long division.
9488  */
9489 
9490  /* Result zero check */
9491  if (var1ndigits == 0)
9492  {
9493  zero_var(result);
9494  result->dscale = rscale;
9495  return;
9496  }
9497 
9498  /*
9499  * Determine the result sign, weight and number of digits to calculate
9500  */
9501  if (var1->sign == var2->sign)
9502  res_sign = NUMERIC_POS;
9503  else
9504  res_sign = NUMERIC_NEG;
9505  res_weight = var1->weight - var2->weight + 1;
9506  /* The number of accurate result digits we need to produce: */
9507  div_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
9508  /* Add guard digits for roundoff error */
9509  div_ndigits += DIV_GUARD_DIGITS;
9510  if (div_ndigits < DIV_GUARD_DIGITS)
9511  div_ndigits = DIV_GUARD_DIGITS;
9512 
9513  /*
9514  * We do the arithmetic in an array "div[]" of signed int's. Since
9515  * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
9516  * to avoid normalizing carries immediately.
9517  *
9518  * We start with div[] containing one zero digit followed by the
9519  * dividend's digits (plus appended zeroes to reach the desired precision
9520  * including guard digits). Each step of the main loop computes an
9521  * (approximate) quotient digit and stores it into div[], removing one
9522  * position of dividend space. A final pass of carry propagation takes
9523  * care of any mistaken quotient digits.
9524  *
9525  * Note that div[] doesn't necessarily contain all of the digits from the
9526  * dividend --- the desired precision plus guard digits might be less than
9527  * the dividend's precision. This happens, for example, in the square
9528  * root algorithm, where we typically divide a 2N-digit number by an
9529  * N-digit number, and only require a result with N digits of precision.
9530  */
9531  div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
9532  load_ndigits = Min(div_ndigits, var1ndigits);
9533  for (i = 0; i < load_ndigits; i++)
9534  div[i + 1] = var1digits[i];
9535 
9536  /*
9537  * We estimate each quotient digit using floating-point arithmetic, taking
9538  * the first four digits of the (current) dividend and divisor. This must
9539  * be float to avoid overflow. The quotient digits will generally be off
9540  * by no more than one from the exact answer.
9541  */
9542  fdivisor = (double) var2digits[0];
9543  for (i = 1; i < 4; i++)
9544  {
9545  fdivisor *= NBASE;
9546  if (i < var2ndigits)
9547  fdivisor += (double) var2digits[i];
9548  }
9549  fdivisorinverse = 1.0 / fdivisor;
9550 
9551  /*
9552  * maxdiv tracks the maximum possible absolute value of any div[] entry;
9553  * when this threatens to exceed INT_MAX, we take the time to propagate
9554  * carries. Furthermore, we need to ensure that overflow doesn't occur
9555  * during the carry propagation passes either. The carry values may have
9556  * an absolute value as high as INT_MAX/NBASE + 1, so really we must
9557  * normalize when digits threaten to exceed INT_MAX - INT_MAX/NBASE - 1.
9558  *
9559  * To avoid overflow in maxdiv itself, it represents the max absolute
9560  * value divided by NBASE-1, ie, at the top of the loop it is known that
9561  * no div[] entry has an absolute value exceeding maxdiv * (NBASE-1).
9562  *
9563  * Actually, though, that holds good only for div[] entries after div[qi];
9564  * the adjustment done at the bottom of the loop may cause div[qi + 1] to
9565  * exceed the maxdiv limit, so that div[qi] in the next iteration is
9566  * beyond the limit. This does not cause problems, as explained below.
9567  */
9568  maxdiv = 1;
9569 
9570  /*
9571  * Outer loop computes next quotient digit, which will go into div[qi]
9572  */
9573  for (qi = 0; qi < div_ndigits; qi++)
9574  {
9575  /* Approximate the current dividend value */
9576  fdividend = (double) div[qi];
9577  for (i = 1; i < 4; i++)
9578  {
9579  fdividend *= NBASE;
9580  if (qi + i <= div_ndigits)
9581  fdividend += (double) div[qi + i];
9582  }
9583  /* Compute the (approximate) quotient digit */
9584  fquotient = fdividend * fdivisorinverse;
9585  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
9586  (((int) fquotient) - 1); /* truncate towards -infinity */
9587 
9588  if (qdigit != 0)
9589  {
9590  /* Do we need to normalize now? */
9591  maxdiv += abs(qdigit);
9592  if (maxdiv > (INT_MAX - INT_MAX / NBASE - 1) / (NBASE - 1))
9593  {
9594  /*
9595  * Yes, do it. Note that if var2ndigits is much smaller than
9596  * div_ndigits, we can save a significant amount of effort
9597  * here by noting that we only need to normalise those div[]
9598  * entries touched where prior iterations subtracted multiples
9599  * of the divisor.
9600  */
9601  carry = 0;
9602  for (i = Min(qi + var2ndigits - 2, div_ndigits); i > qi; i--)
9603  {
9604  newdig = div[i] + carry;
9605  if (newdig < 0)
9606  {
9607  carry = -((-newdig - 1) / NBASE) - 1;
9608  newdig -= carry * NBASE;
9609  }
9610  else if (newdig >= NBASE)
9611  {
9612  carry = newdig / NBASE;
9613  newdig -= carry * NBASE;
9614  }
9615  else
9616  carry = 0;
9617  div[i] = newdig;
9618  }
9619  newdig = div[qi] + carry;
9620  div[qi] = newdig;
9621 
9622  /*
9623  * All the div[] digits except possibly div[qi] are now in the
9624  * range 0..NBASE-1. We do not need to consider div[qi] in
9625  * the maxdiv value anymore, so we can reset maxdiv to 1.
9626  */
9627  maxdiv = 1;
9628 
9629  /*
9630  * Recompute the quotient digit since new info may have
9631  * propagated into the top four dividend digits
9632  */
9633  fdividend = (double) div[qi];
9634  for (i = 1; i < 4; i++)
9635  {
9636  fdividend *= NBASE;
9637  if (qi + i <= div_ndigits)
9638  fdividend += (double) div[qi + i];
9639  }
9640  /* Compute the (approximate) quotient digit */
9641  fquotient = fdividend * fdivisorinverse;
9642  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
9643  (((int) fquotient) - 1); /* truncate towards -infinity */
9644  maxdiv += abs(qdigit);
9645  }
9646 
9647  /*
9648  * Subtract off the appropriate multiple of the divisor.
9649  *
9650  * The digits beyond div[qi] cannot overflow, because we know they
9651  * will fall within the maxdiv limit. As for div[qi] itself, note
9652  * that qdigit is approximately trunc(div[qi] / vardigits[0]),
9653  * which would make the new value simply div[qi] mod vardigits[0].
9654  * The lower-order terms in qdigit can change this result by not
9655  * more than about twice INT_MAX/NBASE, so overflow is impossible.
9656  *
9657  * This inner loop is the performance bottleneck for division, so
9658  * code it in the same way as the inner loop of mul_var() so that
9659  * it can be auto-vectorized. We cast qdigit to NumericDigit
9660  * before multiplying to allow the compiler to generate more
9661  * efficient code (using 16-bit multiplication), which is safe
9662  * since we know that the quotient digit is off by at most one, so
9663  * there is no overflow risk.
9664  */
9665  if (qdigit != 0)
9666  {
9667  int istop = Min(var2ndigits, div_ndigits - qi + 1);
9668  int *div_qi = &div[qi];
9669 
9670  for (i = 0; i < istop; i++)
9671  div_qi[i] -= ((NumericDigit) qdigit) * var2digits[i];
9672  }
9673  }
9674 
9675  /*
9676  * The dividend digit we are about to replace might still be nonzero.
9677  * Fold it into the next digit position.
9678  *
9679  * There is no risk of overflow here, although proving that requires
9680  * some care. Much as with the argument for div[qi] not overflowing,
9681  * if we consider the first two terms in the numerator and denominator
9682  * of qdigit, we can see that the final value of div[qi + 1] will be
9683  * approximately a remainder mod (vardigits[0]*NBASE + vardigits[1]).
9684  * Accounting for the lower-order terms is a bit complicated but ends
9685  * up adding not much more than INT_MAX/NBASE to the possible range.
9686  * Thus, div[qi + 1] cannot overflow here, and in its role as div[qi]
9687  * in the next loop iteration, it can't be large enough to cause
9688  * overflow in the carry propagation step (if any), either.
9689  *
9690  * But having said that: div[qi] can be more than INT_MAX/NBASE, as
9691  * noted above, which means that the product div[qi] * NBASE *can*
9692  * overflow. When that happens, adding it to div[qi + 1] will always
9693  * cause a canceling overflow so that the end result is correct. We
9694  * could avoid the intermediate overflow by doing the multiplication
9695  * and addition in int64 arithmetic, but so far there appears no need.
9696  */
9697  div[qi + 1] += div[qi] * NBASE;
9698 
9699  div[qi] = qdigit;
9700  }
9701 
9702  /*
9703  * Approximate and store the last quotient digit (div[div_ndigits])
9704  */
9705  fdividend = (double) div[qi];
9706  for (i = 1; i < 4; i++)
9707  fdividend *= NBASE;
9708  fquotient = fdividend * fdivisorinverse;
9709  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
9710  (((int) fquotient) - 1); /* truncate towards -infinity */
9711  div[qi] = qdigit;
9712 
9713  /*
9714  * Because the quotient digits might be off by one, some of them might be
9715  * -1 or NBASE at this point. The represented value is correct in a
9716  * mathematical sense, but it doesn't look right. We do a final carry
9717  * propagation pass to normalize the digits, which we combine with storing
9718  * the result digits into the output. Note that this is still done at
9719  * full precision w/guard digits.
9720  */
9721  alloc_var(result, div_ndigits + 1);
9722  res_digits = result->digits;
9723  carry = 0;
9724  for (i = div_ndigits; i >= 0; i--)
9725  {
9726  newdig = div[i] + carry;
9727  if (newdig < 0)
9728  {
9729  carry = -((-newdig - 1) / NBASE) - 1;
9730  newdig -= carry * NBASE;
9731  }
9732  else if (newdig >= NBASE)
9733  {
9734  carry = newdig / NBASE;
9735  newdig -= carry * NBASE;
9736  }
9737  else
9738  carry = 0;
9739  res_digits[i] = newdig;
9740  }
9741  Assert(carry == 0);
9742 
9743  pfree(div);
9744 
9745  /*
9746  * Finally, round the result to the requested precision.
9747  */
9748  result->weight = res_weight;
9749  result->sign = res_sign;
9750 
9751  /* Round to target rscale (and set result->dscale) */
9752  if (round)
9753  round_var(result, rscale);
9754  else
9755  trunc_var(result, rscale);
9756 
9757  /* Strip leading and trailing zeroes */
9758  strip_var(result);
9759 }
9760 
9761 
9762 /*
9763  * div_var_int() -
9764  *
9765  * Divide a numeric variable by a 32-bit integer with the specified weight.
9766  * The quotient var / (ival * NBASE^ival_weight) is stored in result.
9767  */
9768 static void
9769 div_var_int(const NumericVar *var, int ival, int ival_weight,
9770  NumericVar *result, int rscale, bool round)
9771 {
9772  NumericDigit *var_digits = var->digits;
9773  int var_ndigits = var->ndigits;
9774  int res_sign;
9775  int res_weight;
9776  int res_ndigits;
9777  NumericDigit *res_buf;
9778  NumericDigit *res_digits;
9779  uint32 divisor;
9780  int i;
9781 
9782  /* Guard against division by zero */
9783  if (ival == 0)
9784  ereport(ERROR,
9785  errcode(ERRCODE_DIVISION_BY_ZERO),
9786  errmsg("division by zero"));
9787 
9788  /* Result zero check */
9789  if (var_ndigits == 0)
9790  {
9791  zero_var(result);
9792  result->dscale = rscale;
9793  return;
9794  }
9795 
9796  /*
9797  * Determine the result sign, weight and number of digits to calculate.
9798  * The weight figured here is correct if the emitted quotient has no
9799  * leading zero digits; otherwise strip_var() will fix things up.
9800  */
9801  if (var->sign == NUMERIC_POS)
9802  res_sign = ival > 0 ? NUMERIC_POS : NUMERIC_NEG;
9803  else
9804  res_sign = ival > 0 ? NUMERIC_NEG : NUMERIC_POS;
9805  res_weight = var->weight - ival_weight;
9806  /* The number of accurate result digits we need to produce: */
9807  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
9808  /* ... but always at least 1 */
9809  res_ndigits = Max(res_ndigits, 1);
9810  /* If rounding needed, figure one more digit to ensure correct result */
9811  if (round)
9812  res_ndigits++;
9813 
9814  res_buf = digitbuf_alloc(res_ndigits + 1);
9815  res_buf[0] = 0; /* spare digit for later rounding */
9816  res_digits = res_buf + 1;
9817 
9818  /*
9819  * Now compute the quotient digits. This is the short division algorithm
9820  * described in Knuth volume 2, section 4.3.1 exercise 16, except that we
9821  * allow the divisor to exceed the internal base.
9822  *
9823  * In this algorithm, the carry from one digit to the next is at most
9824  * divisor - 1. Therefore, while processing the next digit, carry may
9825  * become as large as divisor * NBASE - 1, and so it requires a 64-bit
9826  * integer if this exceeds UINT_MAX.
9827  */
9828  divisor = abs(ival);
9829 
9830  if (divisor <= UINT_MAX / NBASE)
9831  {
9832  /* carry cannot overflow 32 bits */
9833  uint32 carry = 0;
9834 
9835  for (i = 0; i < res_ndigits; i++)
9836  {
9837  carry = carry * NBASE + (i < var_ndigits ? var_digits[i] : 0);
9838  res_digits[i] = (NumericDigit) (carry / divisor);
9839  carry = carry % divisor;
9840  }
9841  }
9842  else
9843  {
9844  /* carry may exceed 32 bits */
9845  uint64 carry = 0;
9846 
9847  for (i = 0; i < res_ndigits; i++)
9848  {
9849  carry = carry * NBASE + (i < var_ndigits ? var_digits[i] : 0);
9850  res_digits[i] = (NumericDigit) (carry / divisor);
9851  carry = carry % divisor;
9852  }
9853  }
9854 
9855  /* Store the quotient in result */
9856  digitbuf_free(result->buf);
9857  result->ndigits = res_ndigits;
9858  result->buf = res_buf;
9859  result->digits = res_digits;
9860  result->weight = res_weight;
9861  result->sign = res_sign;
9862 
9863  /* Round or truncate to target rscale (and set result->dscale) */
9864  if (round)
9865  round_var(result, rscale);
9866  else
9867  trunc_var(result, rscale);
9868 
9869  /* Strip leading/trailing zeroes */
9870  strip_var(result);
9871 }
9872 
9873 
9874 #ifdef HAVE_INT128
9875 /*
9876  * div_var_int64() -
9877  *
9878  * Divide a numeric variable by a 64-bit integer with the specified weight.
9879  * The quotient var / (ival * NBASE^ival_weight) is stored in result.
9880  *
9881  * This duplicates the logic in div_var_int(), so any changes made there
9882  * should be made here too.
9883  */
9884 static void
9885 div_var_int64(const NumericVar *var, int64 ival, int ival_weight,
9886  NumericVar *result, int rscale, bool round)
9887 {
9888  NumericDigit *var_digits = var->digits;
9889  int var_ndigits = var->ndigits;
9890  int res_sign;
9891  int res_weight;
9892  int res_ndigits;
9893  NumericDigit *res_buf;
9894  NumericDigit *res_digits;
9895  uint64 divisor;
9896  int i;
9897 
9898  /* Guard against division by zero */
9899  if (ival == 0)
9900  ereport(ERROR,
9901  errcode(ERRCODE_DIVISION_BY_ZERO),
9902  errmsg("division by zero"));
9903 
9904  /* Result zero check */
9905  if (var_ndigits == 0)
9906  {
9907  zero_var(result);
9908  result->dscale = rscale;
9909  return;
9910  }
9911 
9912  /*
9913  * Determine the result sign, weight and number of digits to calculate.
9914  * The weight figured here is correct if the emitted quotient has no
9915  * leading zero digits; otherwise strip_var() will fix things up.
9916  */
9917  if (var->sign == NUMERIC_POS)
9918  res_sign = ival > 0 ? NUMERIC_POS : NUMERIC_NEG;
9919  else
9920  res_sign = ival > 0 ? NUMERIC_NEG : NUMERIC_POS;
9921  res_weight = var->weight - ival_weight;
9922  /* The number of accurate result digits we need to produce: */
9923  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
9924  /* ... but always at least 1 */
9925  res_ndigits = Max(res_ndigits, 1);
9926  /* If rounding needed, figure one more digit to ensure correct result */
9927  if (round)
9928  res_ndigits++;
9929 
9930  res_buf = digitbuf_alloc(res_ndigits + 1);
9931  res_buf[0] = 0; /* spare digit for later rounding */
9932  res_digits = res_buf + 1;
9933 
9934  /*
9935  * Now compute the quotient digits. This is the short division algorithm
9936  * described in Knuth volume 2, section 4.3.1 exercise 16, except that we
9937  * allow the divisor to exceed the internal base.
9938  *
9939  * In this algorithm, the carry from one digit to the next is at most
9940  * divisor - 1. Therefore, while processing the next digit, carry may
9941  * become as large as divisor * NBASE - 1, and so it requires a 128-bit
9942  * integer if this exceeds PG_UINT64_MAX.
9943  */
9944  divisor = i64abs(ival);
9945 
9946  if (divisor <= PG_UINT64_MAX / NBASE)
9947  {
9948  /* carry cannot overflow 64 bits */
9949  uint64 carry = 0;
9950 
9951  for (i = 0; i < res_ndigits; i++)
9952  {
9953  carry = carry * NBASE + (i < var_ndigits ? var_digits[i] : 0);
9954  res_digits[i] = (NumericDigit) (carry / divisor);
9955  carry = carry % divisor;
9956  }
9957  }
9958  else
9959  {
9960  /* carry may exceed 64 bits */
9961  uint128 carry = 0;
9962 
9963  for (i = 0; i < res_ndigits; i++)
9964  {
9965  carry = carry * NBASE + (i < var_ndigits ? var_digits[i] : 0);
9966  res_digits[i] = (NumericDigit) (carry / divisor);
9967  carry = carry % divisor;
9968  }
9969  }
9970 
9971  /* Store the quotient in result */
9972  digitbuf_free(result->buf);
9973  result->ndigits = res_ndigits;
9974  result->buf = res_buf;
9975  result->digits = res_digits;
9976  result->weight = res_weight;
9977  result->sign = res_sign;
9978 
9979  /* Round or truncate to target rscale (and set result->dscale) */
9980  if (round)
9981  round_var(result, rscale);
9982  else
9983  trunc_var(result, rscale);
9984 
9985  /* Strip leading/trailing zeroes */
9986  strip_var(result);
9987 }
9988 #endif
9989 
9990 
9991 /*
9992  * Default scale selection for division
9993  *
9994  * Returns the appropriate result scale for the division result.
9995  */
9996 static int
9997 select_div_scale(const NumericVar *var1, const NumericVar *var2)
9998 {
9999  int weight1,
10000  weight2,
10001  qweight,
10002  i;
10003  NumericDigit firstdigit1,
10004  firstdigit2;
10005  int rscale;
10006 
10007  /*
10008  * The result scale of a division isn't specified in any SQL standard. For
10009  * PostgreSQL we select a result scale that will give at least
10010  * NUMERIC_MIN_SIG_DIGITS significant digits, so that numeric gives a
10011  * result no less accurate than float8; but use a scale not less than
10012  * either input's display scale.
10013  */
10014 
10015  /* Get the actual (normalized) weight and first digit of each input */
10016 
10017  weight1 = 0; /* values to use if var1 is zero */
10018  firstdigit1 = 0;
10019  for (i = 0; i < var1->ndigits; i++)
10020  {
10021  firstdigit1 = var1->digits[i];
10022  if (firstdigit1 != 0)
10023  {
10024  weight1 = var1->weight - i;
10025  break;
10026  }
10027  }
10028 
10029  weight2 = 0; /* values to use if var2 is zero */
10030  firstdigit2 = 0;
10031  for (i = 0; i < var2->ndigits; i++)
10032  {
10033  firstdigit2 = var2->digits[i];
10034  if (firstdigit2 != 0)
10035  {
10036  weight2 = var2->weight - i;
10037  break;
10038  }
10039  }
10040 
10041  /*
10042  * Estimate weight of quotient. If the two first digits are equal, we
10043  * can't be sure, but assume that var1 is less than var2.
10044  */
10045  qweight = weight1 - weight2;
10046  if (firstdigit1 <= firstdigit2)
10047  qweight--;
10048 
10049  /* Select result scale */
10050  rscale = NUMERIC_MIN_SIG_DIGITS - qweight * DEC_DIGITS;
10051  rscale = Max(rscale, var1->dscale);
10052  rscale = Max(rscale, var2->dscale);
10053  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
10054  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
10055 
10056  return rscale;
10057 }
10058 
10059 
10060 /*
10061  * mod_var() -
10062  *
10063  * Calculate the modulo of two numerics at variable level
10064  */
10065 static void
10066 mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
10067 {
10068  NumericVar tmp;
10069 
10070  init_var(&tmp);
10071 
10072  /* ---------
10073  * We do this using the equation
10074  * mod(x,y) = x - trunc(x/y)*y
10075  * div_var can be persuaded to give us trunc(x/y) directly.
10076  * ----------
10077  */
10078  div_var(var1, var2, &tmp, 0, false);
10079 
10080  mul_var(var2, &tmp, &tmp, var2->dscale);
10081 
10082  sub_var(var1, &tmp, result);
10083 
10084  free_var(&tmp);
10085 }
10086 
10087 
10088 /*
10089  * div_mod_var() -
10090  *
10091  * Calculate the truncated integer quotient and numeric remainder of two
10092  * numeric variables. The remainder is precise to var2's dscale.
10093  */
10094 static void
10095 div_mod_var(const NumericVar *var1, const NumericVar *var2,
10096  NumericVar *quot, NumericVar *rem)
10097 {
10098  NumericVar q;
10099  NumericVar r;
10100 
10101  init_var(&q);
10102  init_var(&r);
10103 
10104  /*
10105  * Use div_var_fast() to get an initial estimate for the integer quotient.
10106  * This might be inaccurate (per the warning in div_var_fast's comments),
10107  * but we can correct it below.
10108  */
10109  div_var_fast(var1, var2, &q, 0, false);
10110 
10111  /* Compute initial estimate of remainder using the quotient estimate. */
10112  mul_var(var2, &q, &r, var2->dscale);
10113  sub_var(var1, &r, &r);
10114 
10115  /*
10116  * Adjust the results if necessary --- the remainder should have the same
10117  * sign as var1, and its absolute value should be less than the absolute
10118  * value of var2.
10119  */
10120  while (r.ndigits != 0 && r.sign != var1->sign)
10121  {
10122  /* The absolute value of the quotient is too large */
10123  if (var1->sign == var2->sign)
10124  {
10125  sub_var(&q, &const_one, &q);
10126  add_var(&r, var2, &r);
10127  }
10128  else
10129  {
10130  add_var(&q, &const_one, &q);
10131  sub_var(&r, var2, &r);
10132  }
10133  }
10134 
10135  while (cmp_abs(&r, var2) >= 0)
10136  {
10137  /* The absolute value of the quotient is too small */
10138  if (var1->sign == var2->sign)
10139  {
10140  add_var(&q, &const_one, &q);
10141  sub_var(&r, var2, &r);
10142  }
10143  else
10144  {
10145  sub_var(&q, &const_one, &q);
10146  add_var(&r, var2, &r);
10147  }
10148  }
10149 
10150  set_var_from_var(&q, quot);
10151  set_var_from_var(&r, rem);
10152 
10153  free_var(&q);
10154  free_var(&r);
10155 }
10156 
10157 
10158 /*
10159  * ceil_var() -
10160  *
10161  * Return the smallest integer greater than or equal to the argument
10162  * on variable level
10163  */
10164 static void
10165 ceil_var(const NumericVar *var, NumericVar *result)
10166 {
10167  NumericVar tmp;
10168 
10169  init_var(&tmp);
10170  set_var_from_var(var, &tmp);
10171 
10172  trunc_var(&tmp, 0);
10173 
10174  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
10175  add_var(&tmp, &const_one, &tmp);
10176 
10177  set_var_from_var(&tmp, result);
10178  free_var(&tmp);
10179 }
10180 
10181 
10182 /*
10183  * floor_var() -
10184  *
10185  * Return the largest integer equal to or less than the argument
10186  * on variable level
10187  */
10188 static void
10189 floor_var(const NumericVar *var, NumericVar *result)
10190 {
10191  NumericVar tmp;
10192 
10193  init_var(&tmp);
10194  set_var_from_var(var, &tmp);
10195 
10196  trunc_var(&tmp, 0);
10197 
10198  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
10199  sub_var(&tmp, &const_one, &tmp);
10200 
10201  set_var_from_var(&tmp, result);
10202  free_var(&tmp);
10203 }
10204 
10205 
10206 /*
10207  * gcd_var() -
10208  *
10209  * Calculate the greatest common divisor of two numerics at variable level
10210  */
10211 static void
10212 gcd_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
10213 {
10214  int res_dscale;
10215  int cmp;
10216  NumericVar tmp_arg;
10217  NumericVar mod;
10218 
10219  res_dscale = Max(var1->dscale, var2->dscale);
10220 
10221  /*
10222  * Arrange for var1 to be the number with the greater absolute value.
10223  *
10224  * This would happen automatically in the loop below, but avoids an
10225  * expensive modulo operation.
10226  */
10227  cmp = cmp_abs(var1, var2);
10228  if (cmp < 0)
10229  {
10230  const NumericVar *tmp = var1;
10231 
10232  var1 = var2;
10233  var2 = tmp;
10234  }
10235 
10236  /*
10237  * Also avoid the taking the modulo if the inputs have the same absolute
10238  * value, or if the smaller input is zero.
10239  */
10240  if (cmp == 0 || var2->ndigits == 0)
10241  {
10242  set_var_from_var(var1, result);
10243  result->sign = NUMERIC_POS;
10244  result->dscale = res_dscale;
10245  return;
10246  }
10247 
10248  init_var(&tmp_arg);
10249  init_var(&mod);
10250 
10251  /* Use the Euclidean algorithm to find the GCD */
10252  set_var_from_var(var1, &tmp_arg);
10253  set_var_from_var(var2, result);
10254 
10255  for (;;)
10256  {
10257  /* this loop can take a while, so allow it to be interrupted */
10259 
10260  mod_var(&tmp_arg, result, &mod);
10261  if (mod.ndigits == 0)
10262  break;
10263  set_var_from_var(result, &tmp_arg);
10264  set_var_from_var(&mod, result);
10265  }
10266  result->sign = NUMERIC_POS;
10267  result->dscale = res_dscale;
10268 
10269  free_var(&tmp_arg);
10270  free_var(&mod);
10271 }
10272 
10273 
10274 /*
10275  * sqrt_var() -
10276  *
10277  * Compute the square root of x using the Karatsuba Square Root algorithm.
10278  * NOTE: we allow rscale < 0 here, implying rounding before the decimal
10279  * point.
10280  */
10281 static void
10282 sqrt_var(const NumericVar *arg, NumericVar *result, int rscale)
10283 {
10284  int stat;
10285  int res_weight;
10286  int res_ndigits;
10287  int src_ndigits;
10288  int step;
10289  int ndigits[32];
10290  int blen;
10291  int64 arg_int64;
10292  int src_idx;
10293  int64 s_int64;
10294  int64 r_int64;
10295  NumericVar s_var;
10296  NumericVar r_var;
10297  NumericVar a0_var;
10298  NumericVar a1_var;
10299  NumericVar q_var;
10300  NumericVar u_var;
10301 
10302  stat = cmp_var(arg, &const_zero);
10303  if (stat == 0)
10304  {
10305  zero_var(result);
10306  result->dscale = rscale;
10307  return;
10308  }
10309 
10310  /*
10311  * SQL2003 defines sqrt() in terms of power, so we need to emit the right
10312  * SQLSTATE error code if the operand is negative.
10313  */
10314  if (stat < 0)
10315  ereport(ERROR,
10316  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
10317  errmsg("cannot take square root of a negative number")));
10318 
10319  init_var(&s_var);
10320  init_var(&r_var);
10321  init_var(&a0_var);
10322  init_var(&a1_var);
10323  init_var(&q_var);
10324  init_var(&u_var);
10325 
10326  /*
10327  * The result weight is half the input weight, rounded towards minus
10328  * infinity --- res_weight = floor(arg->weight / 2).
10329  */
10330  if (arg->weight >= 0)
10331  res_weight = arg->weight / 2;
10332  else
10333  res_weight = -((-arg->weight - 1) / 2 + 1);
10334 
10335  /*
10336  * Number of NBASE digits to compute. To ensure correct rounding, compute
10337  * at least 1 extra decimal digit. We explicitly allow rscale to be
10338  * negative here, but must always compute at least 1 NBASE digit. Thus
10339  * res_ndigits = res_weight + 1 + ceil((rscale + 1) / DEC_DIGITS) or 1.
10340  */
10341  if (rscale + 1 >= 0)
10342  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS) / DEC_DIGITS;
10343  else
10344  res_ndigits = res_weight + 1 - (-rscale - 1) / DEC_DIGITS;
10345  res_ndigits = Max(res_ndigits, 1);
10346 
10347  /*
10348  * Number of source NBASE digits logically required to produce a result
10349  * with this precision --- every digit before the decimal point, plus 2
10350  * for each result digit after the decimal point (or minus 2 for each
10351  * result digit we round before the decimal point).
10352  */
10353  src_ndigits = arg->weight + 1 + (res_ndigits - res_weight - 1) * 2;
10354  src_ndigits = Max(src_ndigits, 1);
10355 
10356  /* ----------
10357  * From this point on, we treat the input and the result as integers and
10358  * compute the integer square root and remainder using the Karatsuba
10359  * Square Root algorithm, which may be written recursively as follows:
10360  *
10361  * SqrtRem(n = a3*b^3 + a2*b^2 + a1*b + a0):
10362  * [ for some base b, and coefficients a0,a1,a2,a3 chosen so that
10363  * 0 <= a0,a1,a2 < b and a3 >= b/4 ]
10364  * Let (s,r) = SqrtRem(a3*b + a2)
10365  * Let (q,u) = DivRem(r*b + a1, 2*s)
10366  * Let s = s*b + q
10367  * Let r = u*b + a0 - q^2
10368  * If r < 0 Then
10369  * Let r = r + s
10370  * Let s = s - 1
10371  * Let r = r + s
10372  * Return (s,r)
10373  *
10374  * See "Karatsuba Square Root", Paul Zimmermann, INRIA Research Report
10375  * RR-3805, November 1999. At the time of writing this was available
10376  * on the net at <https://hal.inria.fr/inria-00072854>.
10377  *
10378  * The way to read the assumption "n = a3*b^3 + a2*b^2 + a1*b + a0" is
10379  * "choose a base b such that n requires at least four base-b digits to
10380  * express; then those digits are a3,a2,a1,a0, with a3 possibly larger
10381  * than b". For optimal performance, b should have approximately a
10382  * quarter the number of digits in the input, so that the outer square
10383  * root computes roughly twice as many digits as the inner one. For
10384  * simplicity, we choose b = NBASE^blen, an integer power of NBASE.
10385  *
10386  * We implement the algorithm iteratively rather than recursively, to
10387  * allow the working variables to be reused. With this approach, each
10388  * digit of the input is read precisely once --- src_idx tracks the number
10389  * of input digits used so far.
10390  *
10391  * The array ndigits[] holds the number of NBASE digits of the input that
10392  * will have been used at the end of each iteration, which roughly doubles
10393  * each time. Note that the array elements are stored in reverse order,
10394  * so if the final iteration requires src_ndigits = 37 input digits, the
10395  * array will contain [37,19,11,7,5,3], and we would start by computing
10396  * the square root of the 3 most significant NBASE digits.
10397  *
10398  * In each iteration, we choose blen to be the largest integer for which
10399  * the input number has a3 >= b/4, when written in the form above. In
10400  * general, this means blen = src_ndigits / 4 (truncated), but if
10401  * src_ndigits is a multiple of 4, that might lead to the coefficient a3
10402  * being less than b/4 (if the first input digit is less than NBASE/4), in
10403  * which case we choose blen = src_ndigits / 4 - 1. The number of digits
10404  * in the inner square root is then src_ndigits - 2*blen. So, for
10405  * example, if we have src_ndigits = 26 initially, the array ndigits[]
10406  * will be either [26,14,8,4] or [26,14,8,6,4], depending on the size of
10407  * the first input digit.
10408  *
10409  * Additionally, we can put an upper bound on the number of steps required
10410  * as follows --- suppose that the number of source digits is an n-bit
10411  * number in the range [2^(n-1), 2^n-1], then blen will be in the range
10412  * [2^(n-3)-1, 2^(n-2)-1] and the number of digits in the inner square
10413  * root will be in the range [2^(n-2), 2^(n-1)+1]. In the next step, blen
10414  * will be in the range [2^(n-4)-1, 2^(n-3)] and the number of digits in
10415  * the next inner square root will be in the range [2^(n-3), 2^(n-2)+1].
10416  * This pattern repeats, and in the worst case the array ndigits[] will
10417  * contain [2^n-1, 2^(n-1)+1, 2^(n-2)+1, ... 9, 5, 3], and the computation
10418  * will require n steps. Therefore, since all digit array sizes are
10419  * signed 32-bit integers, the number of steps required is guaranteed to
10420  * be less than 32.
10421  * ----------
10422  */
10423  step = 0;
10424  while ((ndigits[step] = src_ndigits) > 4)
10425  {
10426  /* Choose b so that a3 >= b/4, as described above */
10427  blen = src_ndigits / 4;
10428  if (blen * 4 == src_ndigits && arg->digits[0] < NBASE / 4)
10429  blen--;
10430 
10431  /* Number of digits in the next step (inner square root) */
10432  src_ndigits -= 2 * blen;
10433  step++;
10434  }
10435 
10436  /*
10437  * First iteration (innermost square root and remainder):
10438  *
10439  * Here src_ndigits <= 4, and the input fits in an int64. Its square root
10440  * has at most 9 decimal digits, so estimate it using double precision
10441  * arithmetic, which will in fact almost certainly return the correct
10442  * result with no further correction required.
10443  */
10444  arg_int64 = arg->digits[0];
10445  for (src_idx = 1; src_idx < src_ndigits; src_idx++)
10446  {
10447  arg_int64 *= NBASE;
10448  if (src_idx < arg->ndigits)
10449  arg_int64 += arg->digits[src_idx];
10450  }
10451 
10452  s_int64 = (int64) sqrt((double) arg_int64);
10453  r_int64 = arg_int64 - s_int64 * s_int64;
10454 
10455  /*
10456  * Use Newton's method to correct the result, if necessary.
10457  *
10458  * This uses integer division with truncation to compute the truncated
10459  * integer square root by iterating using the formula x -> (x + n/x) / 2.
10460  * This is known to converge to isqrt(n), unless n+1 is a perfect square.
10461  * If n+1 is a perfect square, the sequence will oscillate between the two
10462  * values isqrt(n) and isqrt(n)+1, so we can be assured of convergence by
10463  * checking the remainder.
10464  */
10465  while (r_int64 < 0 || r_int64 > 2 * s_int64)
10466  {
10467  s_int64 = (s_int64 + arg_int64 / s_int64) / 2;
10468  r_int64 = arg_int64 - s_int64 * s_int64;
10469  }
10470 
10471  /*
10472  * Iterations with src_ndigits <= 8:
10473  *
10474  * The next 1 or 2 iterations compute larger (outer) square roots with
10475  * src_ndigits <= 8, so the result still fits in an int64 (even though the
10476  * input no longer does) and we can continue to compute using int64
10477  * variables to avoid more expensive numeric computations.
10478  *
10479  * It is fairly easy to see that there is no risk of the intermediate
10480  * values below overflowing 64-bit integers. In the worst case, the
10481  * previous iteration will have computed a 3-digit square root (of a
10482  * 6-digit input less than NBASE^6 / 4), so at the start of this
10483  * iteration, s will be less than NBASE^3 / 2 = 10^12 / 2, and r will be
10484  * less than 10^12. In this case, blen will be 1, so numer will be less
10485  * than 10^17, and denom will be less than 10^12 (and hence u will also be
10486  * less than 10^12). Finally, since q^2 = u*b + a0 - r, we can also be
10487  * sure that q^2 < 10^17. Therefore all these quantities fit comfortably
10488  * in 64-bit integers.
10489  */
10490  step--;
10491  while (step >= 0 && (src_ndigits = ndigits[step]) <= 8)
10492  {
10493  int b;
10494  int a0;
10495  int a1;
10496  int i;
10497  int64 numer;
10498  int64 denom;
10499  int64 q;
10500  int64 u;
10501 
10502  blen = (src_ndigits - src_idx) / 2;
10503 
10504  /* Extract a1 and a0, and compute b */
10505  a0 = 0;
10506  a1 = 0;
10507  b = 1;
10508 
10509  for (i = 0; i < blen; i++, src_idx++)
10510  {
10511  b *= NBASE;
10512  a1 *= NBASE;
10513  if (src_idx < arg->ndigits)
10514  a1 += arg->digits[src_idx];
10515  }
10516 
10517  for (i = 0; i < blen; i++, src_idx++)
10518  {
10519  a0 *= NBASE;
10520  if (src_idx < arg->ndigits)
10521  a0 += arg->digits[src_idx];
10522  }
10523 
10524  /* Compute (q,u) = DivRem(r*b + a1, 2*s) */
10525  numer = r_int64 * b + a1;
10526  denom = 2 * s_int64;
10527  q = numer / denom;
10528  u = numer - q * denom;
10529 
10530  /* Compute s = s*b + q and r = u*b + a0 - q^2 */
10531  s_int64 = s_int64 * b + q;
10532  r_int64 = u * b + a0 - q * q;
10533 
10534  if (r_int64 < 0)
10535  {
10536  /* s is too large by 1; set r += s, s--, r += s */
10537  r_int64 += s_int64;
10538  s_int64--;
10539  r_int64 += s_int64;
10540  }
10541 
10542  Assert(src_idx == src_ndigits); /* All input digits consumed */
10543  step--;
10544  }
10545 
10546  /*
10547  * On platforms with 128-bit integer support, we can further delay the
10548  * need to use numeric variables.
10549  */
10550 #ifdef HAVE_INT128
10551  if (step >= 0)
10552  {
10553  int128 s_int128;
10554  int128 r_int128;
10555 
10556  s_int128 = s_int64;
10557  r_int128 = r_int64;
10558 
10559  /*
10560  * Iterations with src_ndigits <= 16:
10561  *
10562  * The result fits in an int128 (even though the input doesn't) so we
10563  * use int128 variables to avoid more expensive numeric computations.
10564  */
10565  while (step >= 0 && (src_ndigits = ndigits[step]) <= 16)
10566  {
10567  int64 b;
10568  int64 a0;
10569  int64 a1;
10570  int64 i;
10571  int128 numer;
10572  int128 denom;
10573  int128 q;
10574  int128 u;
10575 
10576  blen = (src_ndigits - src_idx) / 2;
10577 
10578  /* Extract a1 and a0, and compute b */
10579  a0 = 0;
10580  a1 = 0;
10581  b = 1;
10582 
10583  for (i = 0; i < blen; i++, src_idx++)
10584  {
10585  b *= NBASE;
10586  a1 *= NBASE;
10587  if (src_idx < arg->ndigits)
10588  a1 += arg->digits[src_idx];
10589  }
10590 
10591  for (i = 0; i < blen; i++, src_idx++)
10592  {
10593  a0 *= NBASE;
10594  if (src_idx < arg->ndigits)
10595  a0 += arg->digits[src_idx];
10596  }
10597 
10598  /* Compute (q,u) = DivRem(r*b + a1, 2*s) */
10599  numer = r_int128 * b + a1;
10600  denom = 2 * s_int128;
10601  q = numer / denom;
10602  u = numer - q * denom;
10603 
10604  /* Compute s = s*b + q and r = u*b + a0 - q^2 */
10605  s_int128 = s_int128 * b + q;
10606  r_int128 = u * b + a0 - q * q;
10607 
10608  if (r_int128 < 0)
10609  {
10610  /* s is too large by 1; set r += s, s--, r += s */
10611  r_int128 += s_int128;
10612  s_int128--;
10613  r_int128 += s_int128;
10614  }
10615 
10616  Assert(src_idx == src_ndigits); /* All input digits consumed */
10617  step--;
10618  }
10619 
10620  /*
10621  * All remaining iterations require numeric variables. Convert the
10622  * integer values to NumericVar and continue. Note that in the final
10623  * iteration we don't need the remainder, so we can save a few cycles
10624  * there by not fully computing it.
10625  */
10626  int128_to_numericvar(s_int128, &s_var);
10627  if (step >= 0)
10628  int128_to_numericvar(r_int128, &r_var);
10629  }
10630  else
10631  {
10632  int64_to_numericvar(s_int64, &s_var);
10633  /* step < 0, so we certainly don't need r */
10634  }
10635 #else /* !HAVE_INT128 */
10636  int64_to_numericvar(s_int64, &s_var);
10637  if (step >= 0)
10638  int64_to_numericvar(r_int64, &r_var);
10639 #endif /* HAVE_INT128 */
10640 
10641  /*
10642  * The remaining iterations with src_ndigits > 8 (or 16, if have int128)
10643  * use numeric variables.
10644  */
10645  while (step >= 0)
10646  {
10647  int tmp_len;
10648 
10649  src_ndigits = ndigits[step];
10650  blen = (src_ndigits - src_idx) / 2;
10651 
10652  /* Extract a1 and a0 */
10653  if (src_idx < arg->ndigits)
10654  {
10655  tmp_len = Min(blen, arg->ndigits - src_idx);
10656  alloc_var(&a1_var, tmp_len);
10657  memcpy(a1_var.digits, arg->digits + src_idx,
10658  tmp_len * sizeof(NumericDigit));
10659  a1_var.weight = blen - 1;
10660  a1_var.sign = NUMERIC_POS;
10661  a1_var.dscale = 0;
10662  strip_var(&a1_var);
10663  }
10664  else
10665  {
10666  zero_var(&a1_var);
10667  a1_var.dscale = 0;
10668  }
10669  src_idx += blen;
10670 
10671  if (src_idx < arg->ndigits)
10672  {
10673  tmp_len = Min(blen, arg->ndigits - src_idx);
10674  alloc_var(&a0_var, tmp_len);
10675  memcpy(a0_var.digits, arg->digits + src_idx,
10676  tmp_len * sizeof(NumericDigit));
10677  a0_var.weight = blen - 1;
10678  a0_var.sign = NUMERIC_POS;
10679  a0_var.dscale = 0;
10680  strip_var(&a0_var);
10681  }
10682  else
10683  {
10684  zero_var(&a0_var);
10685  a0_var.dscale = 0;
10686  }
10687  src_idx += blen;
10688 
10689  /* Compute (q,u) = DivRem(r*b + a1, 2*s) */
10690  set_var_from_var(&r_var, &q_var);
10691  q_var.weight += blen;
10692  add_var(&q_var, &a1_var, &q_var);
10693  add_var(&s_var, &s_var, &u_var);
10694  div_mod_var(&q_var, &u_var, &q_var, &u_var);
10695 
10696  /* Compute s = s*b + q */
10697  s_var.weight += blen;
10698  add_var(&s_var, &q_var, &s_var);
10699 
10700  /*
10701  * Compute r = u*b + a0 - q^2.
10702  *
10703  * In the final iteration, we don't actually need r; we just need to
10704  * know whether it is negative, so that we know whether to adjust s.
10705  * So instead of the final subtraction we can just compare.
10706  */
10707  u_var.weight += blen;
10708  add_var(&u_var, &a0_var, &u_var);
10709  mul_var(&q_var, &q_var, &q_var, 0);
10710 
10711  if (step > 0)
10712  {
10713  /* Need r for later iterations */
10714  sub_var(&u_var, &q_var, &r_var);
10715  if (r_var.sign == NUMERIC_NEG)
10716  {
10717  /* s is too large by 1; set r += s, s--, r += s */
10718  add_var(&r_var, &s_var, &r_var);
10719  sub_var(&s_var, &const_one, &s_var);
10720  add_var(&r_var, &s_var, &r_var);
10721  }
10722  }
10723  else
10724  {
10725  /* Don't need r anymore, except to test if s is too large by 1 */
10726  if (cmp_var(&u_var, &q_var) < 0)
10727  sub_var(&s_var, &const_one, &s_var);
10728  }
10729 
10730  Assert(src_idx == src_ndigits); /* All input digits consumed */
10731  step--;
10732  }
10733 
10734  /*
10735  * Construct the final result, rounding it to the requested precision.
10736  */
10737  set_var_from_var(&s_var, result);
10738  result->weight = res_weight;
10739  result->sign = NUMERIC_POS;
10740 
10741  /* Round to target rscale (and set result->dscale) */
10742  round_var(result, rscale);
10743 
10744  /* Strip leading and trailing zeroes */
10745  strip_var(result);
10746 
10747  free_var(&s_var);
10748  free_var(&r_var);
10749  free_var(&a0_var);
10750  free_var(&a1_var);
10751  free_var(&q_var);
10752  free_var(&u_var);
10753 }
10754 
10755 
10756 /*
10757  * exp_var() -
10758  *
10759  * Raise e to the power of x, computed to rscale fractional digits
10760  */
10761 static void
10762 exp_var(const NumericVar *arg, NumericVar *result, int rscale)
10763 {
10764  NumericVar x;
10765  NumericVar elem;
10766  int ni;
10767  double val;
10768  int dweight;
10769  int ndiv2;
10770  int sig_digits;
10771  int local_rscale;
10772 
10773  init_var(&x);
10774  init_var(&elem);
10775 
10776  set_var_from_var(arg, &x);
10777 
10778  /*
10779  * Estimate the dweight of the result using floating point arithmetic, so
10780  * that we can choose an appropriate local rscale for the calculation.
10781  */
10783 
10784  /* Guard against overflow/underflow */
10785  /* If you change this limit, see also power_var()'s limit */
10786  if (fabs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
10787  {
10788  if (val > 0)
10789  ereport(ERROR,
10790  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10791  errmsg("value overflows numeric format")));
10792  zero_var(result);
10793  result->dscale = rscale;
10794  return;
10795  }
10796 
10797  /* decimal weight = log10(e^x) = x * log10(e) */
10798  dweight = (int) (val * 0.434294481903252);
10799 
10800  /*
10801  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
10802  * 2^ndiv2, to improve the convergence rate of the Taylor series.
10803  *
10804  * Note that the overflow check above ensures that fabs(x) < 6000, which
10805  * means that ndiv2 <= 20 here.
10806  */
10807  if (fabs(val) > 0.01)
10808  {
10809  ndiv2 = 1;
10810  val /= 2;
10811 
10812  while (fabs(val) > 0.01)
10813  {
10814  ndiv2++;
10815  val /= 2;
10816  }
10817 
10818  local_rscale = x.dscale + ndiv2;
10819  div_var_int(&x, 1 << ndiv2, 0, &x, local_rscale, true);
10820  }
10821  else
10822  ndiv2 = 0;
10823 
10824  /*
10825  * Set the scale for the Taylor series expansion. The final result has
10826  * (dweight + rscale + 1) significant digits. In addition, we have to
10827  * raise the Taylor series result to the power 2^ndiv2, which introduces
10828  * an error of up to around log10(2^ndiv2) digits, so work with this many
10829  * extra digits of precision (plus a few more for good measure).
10830  */
10831  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
10832  sig_digits = Max(sig_digits, 0) + 8;
10833 
10834  local_rscale = sig_digits - 1;
10835 
10836  /*
10837  * Use the Taylor series
10838  *
10839  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
10840  *
10841  * Given the limited range of x, this should converge reasonably quickly.
10842  * We run the series until the terms fall below the local_rscale limit.
10843  */
10844  add_var(&const_one, &x, result);
10845 
10846  mul_var(&x, &x, &elem, local_rscale);
10847  ni = 2;
10848  div_var_int(&elem, ni, 0, &elem, local_rscale, true);
10849 
10850  while (elem.ndigits != 0)
10851  {
10852  add_var(result, &elem, result);
10853 
10854  mul_var(&elem, &x, &elem, local_rscale);
10855  ni++;
10856  div_var_int(&elem, ni, 0, &elem, local_rscale, true);
10857  }
10858 
10859  /*
10860  * Compensate for the argument range reduction. Since the weight of the
10861  * result doubles with each multiplication, we can reduce the local rscale
10862  * as we proceed.
10863  */
10864  while (ndiv2-- > 0)
10865  {
10866  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
10867  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
10868  mul_var(result, result, result, local_rscale);
10869  }
10870 
10871  /* Round to requested rscale */
10872  round_var(result, rscale);
10873 
10874  free_var(&x);
10875  free_var(&elem);
10876 }
10877 
10878 
10879 /*
10880  * Estimate the dweight of the most significant decimal digit of the natural
10881  * logarithm of a number.
10882  *
10883  * Essentially, we're approximating log10(abs(ln(var))). This is used to
10884  * determine the appropriate rscale when computing natural logarithms.
10885  *
10886  * Note: many callers call this before range-checking the input. Therefore,
10887  * we must be robust against values that are invalid to apply ln() to.
10888  * We don't wish to throw an error here, so just return zero in such cases.
10889  */
10890 static int
10892 {
10893  int ln_dweight;
10894 
10895  /* Caller should fail on ln(negative), but for the moment return zero */
10896  if (var->sign != NUMERIC_POS)
10897  return 0;
10898 
10899  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
10900  cmp_var(var, &const_one_point_one) <= 0)
10901  {
10902  /*
10903  * 0.9 <= var <= 1.1
10904  *
10905  * ln(var) has a negative weight (possibly very large). To get a
10906  * reasonably accurate result, estimate it using ln(1+x) ~= x.
10907  */
10908  NumericVar x;
10909 
10910  init_var(&x);
10911  sub_var(var, &const_one, &x);
10912 
10913  if (x.ndigits > 0)
10914  {
10915  /* Use weight of most significant decimal digit of x */
10916  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
10917  }
10918  else
10919  {
10920  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
10921  ln_dweight = 0;
10922  }
10923 
10924  free_var(&x);
10925  }
10926  else
10927  {
10928  /*
10929  * Estimate the logarithm using the first couple of digits from the
10930  * input number. This will give an accurate result whenever the input
10931  * is not too close to 1.
10932  */
10933  if (var->ndigits > 0)
10934  {
10935  int digits;
10936  int dweight;
10937  double ln_var;
10938 
10939  digits = var->digits[0];
10940  dweight = var->weight * DEC_DIGITS;
10941 
10942  if (var->ndigits > 1)
10943  {
10944  digits = digits * NBASE + var->digits[1];
10945  dweight -= DEC_DIGITS;
10946  }
10947 
10948  /*----------
10949  * We have var ~= digits * 10^dweight
10950  * so ln(var) ~= ln(digits) + dweight * ln(10)
10951  *----------
10952  */
10953  ln_var = log((double) digits) + dweight * 2.302585092994046;
10954  ln_dweight = (int) log10(fabs(ln_var));
10955  }
10956  else
10957  {
10958  /* Caller should fail on ln(0), but for the moment return zero */
10959  ln_dweight = 0;
10960  }
10961  }
10962 
10963  return ln_dweight;
10964 }
10965 
10966 
10967 /*
10968  * ln_var() -
10969  *
10970  * Compute the natural log of x
10971  */
10972 static void
10973 ln_var(const NumericVar *arg, NumericVar *result, int rscale)
10974 {
10975  NumericVar x;
10976  NumericVar xx;
10977  int ni;
10978  NumericVar elem;
10979  NumericVar fact;
10980  int nsqrt;
10981  int local_rscale;
10982  int cmp;
10983 
10984  cmp = cmp_var(arg, &const_zero);
10985  if (cmp == 0)
10986  ereport(ERROR,
10987  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
10988  errmsg("cannot take logarithm of zero")));
10989  else if (cmp < 0)
10990  ereport(ERROR,
10991  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
10992  errmsg("cannot take logarithm of a negative number")));
10993 
10994  init_var(&x);
10995  init_var(&xx);
10996  init_var(&elem);
10997  init_var(&fact);
10998 
10999  set_var_from_var(arg, &x);
11000  set_var_from_var(&const_two, &fact);
11001 
11002  /*
11003  * Reduce input into range 0.9 < x < 1.1 with repeated sqrt() operations.
11004  *
11005  * The final logarithm will have up to around rscale+6 significant digits.
11006  * Each sqrt() will roughly halve the weight of x, so adjust the local
11007  * rscale as we work so that we keep this many significant digits at each
11008  * step (plus a few more for good measure).
11009  *
11010  * Note that we allow local_rscale < 0 during this input reduction
11011  * process, which implies rounding before the decimal point. sqrt_var()
11012  * explicitly supports this, and it significantly reduces the work
11013  * required to reduce very large inputs to the required range. Once the
11014  * input reduction is complete, x.weight will be 0 and its display scale
11015  * will be non-negative again.
11016  */
11017  nsqrt = 0;
11018  while (cmp_var(&x, &const_zero_point_nine) <= 0)
11019  {
11020  local_rscale = rscale - x.weight * DEC_DIGITS / 2 + 8;
11021  sqrt_var(&x, &x, local_rscale);
11022  mul_var(&fact, &const_two, &fact, 0);
11023  nsqrt++;
11024  }
11025  while (cmp_var(&x, &const_one_point_one) >= 0)
11026  {
11027  local_rscale = rscale - x.weight * DEC_DIGITS / 2 + 8;
11028  sqrt_var(&x, &x, local_rscale);
11029  mul_var(&fact, &const_two, &fact, 0);
11030  nsqrt++;
11031  }
11032 
11033  /*
11034  * We use the Taylor series for 0.5 * ln((1+z)/(1-z)),
11035  *
11036  * z + z^3/3 + z^5/5 + ...
11037  *
11038  * where z = (x-1)/(x+1) is in the range (approximately) -0.053 .. 0.048
11039  * due to the above range-reduction of x.
11040  *
11041  * The convergence of this is not as fast as one would like, but is
11042  * tolerable given that z is small.
11043  *
11044  * The Taylor series result will be multiplied by 2^(nsqrt+1), which has a
11045  * decimal weight of (nsqrt+1) * log10(2), so work with this many extra
11046  * digits of precision (plus a few more for good measure).
11047  */
11048  local_rscale = rscale + (int) ((nsqrt + 1) * 0.301029995663981) + 8;
11049 
11050  sub_var(&x, &const_one, result);
11051  add_var(&x, &const_one, &elem);
11052  div_var_fast(result, &elem, result, local_rscale, true);
11053  set_var_from_var(result, &xx);
11054  mul_var(result, result, &x, local_rscale);
11055 
11056  ni = 1;
11057 
11058  for (;;)
11059  {
11060  ni += 2;
11061  mul_var(&xx, &x, &xx, local_rscale);
11062  div_var_int(&xx, ni, 0, &elem, local_rscale, true);
11063 
11064  if (elem.ndigits == 0)
11065  break;
11066 
11067  add_var(result, &elem, result);
11068 
11069  if (elem.weight < (result->weight - local_rscale * 2 / DEC_DIGITS))
11070  break;
11071  }
11072 
11073  /* Compensate for argument range reduction, round to requested rscale */
11074  mul_var(result, &fact, result, rscale);
11075 
11076  free_var(&x);
11077  free_var(&xx);
11078  free_var(&elem);
11079  free_var(&fact);
11080 }
11081 
11082 
11083 /*
11084  * log_var() -
11085  *
11086  * Compute the logarithm of num in a given base.
11087  *
11088  * Note: this routine chooses dscale of the result.
11089  */
11090 static void
11091 log_var(const NumericVar *base, const NumericVar *num, NumericVar *result)
11092 {
11093  NumericVar ln_base;
11094  NumericVar ln_num;
11095  int ln_base_dweight;
11096  int ln_num_dweight;
11097  int result_dweight;
11098  int rscale;
11099  int ln_base_rscale;
11100  int ln_num_rscale;
11101 
11102  init_var(&ln_base);
11103  init_var(&ln_num);
11104 
11105  /* Estimated dweights of ln(base), ln(num) and the final result */
11106  ln_base_dweight = estimate_ln_dweight(base);
11107  ln_num_dweight = estimate_ln_dweight(num);
11108  result_dweight = ln_num_dweight - ln_base_dweight;
11109 
11110  /*
11111  * Select the scale of the result so that it will have at least
11112  * NUMERIC_MIN_SIG_DIGITS significant digits and is not less than either
11113  * input's display scale.
11114  */
11115  rscale = NUMERIC_MIN_SIG_DIGITS - result_dweight;
11116  rscale = Max(rscale, base->dscale);
11117  rscale = Max(rscale, num->dscale);
11118  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
11119  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
11120 
11121  /*
11122  * Set the scales for ln(base) and ln(num) so that they each have more
11123  * significant digits than the final result.
11124  */
11125  ln_base_rscale = rscale + result_dweight - ln_base_dweight + 8;
11126  ln_base_rscale = Max(ln_base_rscale, NUMERIC_MIN_DISPLAY_SCALE);
11127 
11128  ln_num_rscale = rscale + result_dweight - ln_num_dweight + 8;
11129  ln_num_rscale = Max(ln_num_rscale, NUMERIC_MIN_DISPLAY_SCALE);
11130 
11131  /* Form natural logarithms */
11132  ln_var(base, &ln_base, ln_base_rscale);
11133  ln_var(num, &ln_num, ln_num_rscale);
11134 
11135  /* Divide and round to the required scale */
11136  div_var_fast(&ln_num, &ln_base, result, rscale, true);
11137 
11138  free_var(&ln_num);
11139  free_var(&ln_base);
11140 }
11141 
11142 
11143 /*
11144  * power_var() -
11145  *
11146  * Raise base to the power of exp
11147  *
11148  * Note: this routine chooses dscale of the result.
11149  */
11150 static void
11151 power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
11152 {
11153  int res_sign;
11154  NumericVar abs_base;
11155  NumericVar ln_base;
11156  NumericVar ln_num;
11157  int ln_dweight;
11158  int rscale;
11159  int sig_digits;
11160  int local_rscale;
11161  double val;
11162 
11163  /* If exp can be represented as an integer, use power_var_int */
11164  if (exp->ndigits == 0 || exp->ndigits <= exp->weight + 1)
11165  {
11166  /* exact integer, but does it fit in int? */
11167  int64 expval64;
11168 
11169  if (numericvar_to_int64(exp, &expval64))
11170  {
11171  if (expval64 >= PG_INT32_MIN && expval64 <= PG_INT32_MAX)
11172  {
11173  /* Okay, use power_var_int */
11174  power_var_int(base, (int) expval64, exp->dscale, result);
11175  return;
11176  }
11177  }
11178  }
11179 
11180  /*
11181  * This avoids log(0) for cases of 0 raised to a non-integer. 0 ^ 0 is
11182  * handled by power_var_int().
11183  */
11184  if (cmp_var(base, &const_zero) == 0)
11185  {
11186  set_var_from_var(&const_zero, result);
11187  result->dscale = NUMERIC_MIN_SIG_DIGITS; /* no need to round */
11188  return;
11189  }
11190 
11191  init_var(&abs_base);
11192  init_var(&ln_base);
11193  init_var(&ln_num);
11194 
11195  /*
11196  * If base is negative, insist that exp be an integer. The result is then
11197  * positive if exp is even and negative if exp is odd.
11198  */
11199  if (base->sign == NUMERIC_NEG)
11200  {
11201  /*
11202  * Check that exp is an integer. This error code is defined by the
11203  * SQL standard, and matches other errors in numeric_power().
11204  */
11205  if (exp->ndigits > 0 && exp->ndigits > exp->weight + 1)
11206  ereport(ERROR,
11207  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
11208  errmsg("a negative number raised to a non-integer power yields a complex result")));
11209 
11210  /* Test if exp is odd or even */
11211  if (exp->ndigits > 0 && exp->ndigits == exp->weight + 1 &&
11212  (exp->digits[exp->ndigits - 1] & 1))
11213  res_sign = NUMERIC_NEG;
11214  else
11215  res_sign = NUMERIC_POS;
11216 
11217  /* Then work with abs(base) below */
11218  set_var_from_var(base, &abs_base);
11219  abs_base.sign = NUMERIC_POS;
11220  base = &abs_base;
11221  }
11222  else
11223  res_sign = NUMERIC_POS;
11224 
11225  /*----------
11226  * Decide on the scale for the ln() calculation. For this we need an
11227  * estimate of the weight of the result, which we obtain by doing an
11228  * initial low-precision calculation of exp * ln(base).
11229  *
11230  * We want result = e ^ (exp * ln(base))
11231  * so result dweight = log10(result) = exp * ln(base) * log10(e)
11232  *
11233  * We also perform a crude overflow test here so that we can exit early if
11234  * the full-precision result is sure to overflow, and to guard against
11235  * integer overflow when determining the scale for the real calculation.
11236  * exp_var() supports inputs up to NUMERIC_MAX_RESULT_SCALE * 3, so the
11237  * result will overflow if exp * ln(base) >= NUMERIC_MAX_RESULT_SCALE * 3.
11238  * Since the values here are only approximations, we apply a small fuzz
11239  * factor to this overflow test and let exp_var() determine the exact
11240  * overflow threshold so that it is consistent for all inputs.
11241  *----------
11242  */
11243  ln_dweight = estimate_ln_dweight(base);
11244 
11245  /*
11246  * Set the scale for the low-precision calculation, computing ln(base) to
11247  * around 8 significant digits. Note that ln_dweight may be as small as
11248  * -NUMERIC_DSCALE_MAX, so the scale may exceed NUMERIC_MAX_DISPLAY_SCALE
11249  * here.
11250  */
11251  local_rscale = 8 - ln_dweight;
11252  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
11253 
11254  ln_var(base, &ln_base, local_rscale);
11255 
11256  mul_var(&ln_base, exp, &ln_num, local_rscale);
11257 
11259 
11260  /* initial overflow/underflow test with fuzz factor */
11261  if (fabs(val) > NUMERIC_MAX_RESULT_SCALE * 3.01)
11262  {
11263  if (val > 0)
11264  ereport(ERROR,
11265  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
11266  errmsg("value overflows numeric format")));
11267  zero_var(result);
11269  return;
11270  }
11271 
11272  val *= 0.434294481903252; /* approximate decimal result weight */
11273 
11274  /* choose the result scale */
11275  rscale = NUMERIC_MIN_SIG_DIGITS - (int) val;
11276  rscale = Max(rscale, base->dscale);
11277  rscale = Max(rscale, exp->dscale);
11278  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
11279  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
11280 
11281  /* significant digits required in the result */
11282  sig_digits = rscale + (int) val;
11283  sig_digits = Max(sig_digits, 0);
11284 
11285  /* set the scale for the real exp * ln(base) calculation */
11286  local_rscale = sig_digits - ln_dweight + 8;
11287  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
11288 
11289  /* and do the real calculation */
11290 
11291  ln_var(base, &ln_base, local_rscale);
11292 
11293  mul_var(&ln_base, exp, &ln_num, local_rscale);
11294 
11295  exp_var(&ln_num, result, rscale);
11296 
11297  if (res_sign == NUMERIC_NEG && result->ndigits > 0)
11298  result->sign = NUMERIC_NEG;
11299 
11300  free_var(&ln_num);
11301  free_var(&ln_base);
11302  free_var(&abs_base);
11303 }
11304 
11305 /*
11306  * power_var_int() -
11307  *
11308  * Raise base to the power of exp, where exp is an integer.
11309  *
11310  * Note: this routine chooses dscale of the result.
11311  */
11312 static void
11313 power_var_int(const NumericVar *base, int exp, int exp_dscale,
11314  NumericVar *result)
11315 {
11316  double f;
11317  int p;
11318  int i;
11319  int rscale;
11320  int sig_digits;
11321  unsigned int mask;
11322  bool neg;
11323  NumericVar base_prod;
11324  int local_rscale;
11325 
11326  /*
11327  * Choose the result scale. For this we need an estimate of the decimal
11328  * weight of the result, which we obtain by approximating using double
11329  * precision arithmetic.
11330  *
11331  * We also perform crude overflow/underflow tests here so that we can exit
11332  * early if the result is sure to overflow/underflow, and to guard against
11333  * integer overflow when choosing the result scale.
11334  */
11335  if (base->ndigits != 0)
11336  {
11337  /*----------
11338  * Choose f (double) and p (int) such that base ~= f * 10^p.
11339  * Then log10(result) = log10(base^exp) ~= exp * (log10(f) + p).
11340  *----------
11341  */
11342  f = base->digits[0];
11343  p = base->weight * DEC_DIGITS;
11344 
11345  for (i = 1; i < base->ndigits && i * DEC_DIGITS < 16; i++)
11346  {
11347  f = f * NBASE + base->digits[i];
11348  p -= DEC_DIGITS;
11349  }
11350 
11351  f = exp * (log10(f) + p); /* approximate decimal result weight */
11352  }
11353  else
11354  f = 0; /* result is 0 or 1 (weight 0), or error */
11355 
11356  /* overflow/underflow tests with fuzz factors */
11357  if (f > (NUMERIC_WEIGHT_MAX + 1) * DEC_DIGITS)
11358  ereport(ERROR,
11359  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
11360  errmsg("value overflows numeric format")));
11361  if (f + 1 < -NUMERIC_MAX_DISPLAY_SCALE)
11362  {
11363  zero_var(result);
11365  return;
11366  }
11367 
11368  /*
11369  * Choose the result scale in the same way as power_var(), so it has at
11370  * least NUMERIC_MIN_SIG_DIGITS significant digits and is not less than
11371  * either input's display scale.
11372  */
11373  rscale = NUMERIC_MIN_SIG_DIGITS - (int) f;
11374  rscale = Max(rscale, base->dscale);
11375  rscale = Max(rscale, exp_dscale);
11376  rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
11377  rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
11378 
11379  /* Handle some common special cases, as well as corner cases */
11380  switch (exp)
11381  {
11382  case 0:
11383 
11384  /*
11385  * While 0 ^ 0 can be either 1 or indeterminate (error), we treat
11386  * it as 1 because most programming languages do this. SQL:2003
11387  * also requires a return value of 1.
11388  * https://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_power
11389  */
11390  set_var_from_var(&const_one, result);
11391  result->dscale = rscale; /* no need to round */
11392  return;
11393  case 1:
11394  set_var_from_var(base, result);
11395  round_var(result, rscale);
11396  return;
11397  case -1:
11398  div_var(&const_one, base, result, rscale, true);
11399  return;
11400  case 2:
11401  mul_var(base, base, result, rscale);
11402  return;
11403  default:
11404  break;
11405  }
11406 
11407  /* Handle the special case where the base is zero */
11408  if (base->ndigits == 0)
11409  {
11410  if (exp < 0)
11411  ereport(ERROR,
11412  (errcode(ERRCODE_DIVISION_BY_ZERO),
11413  errmsg("division by zero")));
11414  zero_var(result);
11415  result->dscale = rscale;
11416  return;
11417  }
11418 
11419  /*
11420  * The general case repeatedly multiplies base according to the bit
11421  * pattern of exp.
11422  *
11423  * The local rscale used for each multiplication is varied to keep a fixed
11424  * number of significant digits, sufficient to give the required result
11425  * scale.
11426  */
11427 
11428  /*
11429  * Approximate number of significant digits in the result. Note that the
11430  * underflow test above, together with the choice of rscale, ensures that
11431  * this approximation is necessarily > 0.
11432  */
11433  sig_digits = 1 + rscale + (int) f;
11434 
11435  /*
11436  * The multiplications to produce the result may introduce an error of up
11437  * to around log10(abs(exp)) digits, so work with this many extra digits
11438  * of precision (plus a few more for good measure).
11439  */
11440  sig_digits += (int) log(fabs((double) exp)) + 8;
11441 
11442  /*
11443  * Now we can proceed with the multiplications.
11444  */
11445  neg = (exp < 0);
11446  mask = abs(exp);
11447 
11448  init_var(&base_prod);
11449  set_var_from_var(base, &base_prod);
11450 
11451  if (mask & 1)
11452  set_var_from_var(base, result);
11453  else
11454  set_var_from_var(&const_one, result);
11455 
11456  while ((mask >>= 1) > 0)
11457  {
11458  /*
11459  * Do the multiplications using rscales large enough to hold the
11460  * results to the required number of significant digits, but don't
11461  * waste time by exceeding the scales of the numbers themselves.
11462  */
11463  local_rscale = sig_digits - 2 * base_prod.weight * DEC_DIGITS;
11464  local_rscale = Min(local_rscale, 2 * base_prod.dscale);
11465  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
11466 
11467  mul_var(&base_prod, &base_prod, &base_prod, local_rscale);
11468 
11469  if (mask & 1)
11470  {
11471  local_rscale = sig_digits -
11472  (base_prod.weight + result->weight) * DEC_DIGITS;
11473  local_rscale = Min(local_rscale,
11474  base_prod.dscale + result->dscale);
11475  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
11476 
11477  mul_var(&base_prod, result, result, local_rscale);
11478  }
11479 
11480  /*
11481  * When abs(base) > 1, the number of digits to the left of the decimal
11482  * point in base_prod doubles at each iteration, so if exp is large we
11483  * could easily spend large amounts of time and memory space doing the
11484  * multiplications. But once the weight exceeds what will fit in
11485  * int16, the final result is guaranteed to overflow (or underflow, if
11486  * exp < 0), so we can give up before wasting too many cycles.
11487  */
11488  if (base_prod.weight > NUMERIC_WEIGHT_MAX ||
11489  result->weight > NUMERIC_WEIGHT_MAX)
11490  {
11491  /* overflow, unless neg, in which case result should be 0 */
11492  if (!neg)
11493  ereport(ERROR,
11494  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
11495  errmsg("value overflows numeric format")));
11496  zero_var(result);
11497  neg = false;
11498  break;
11499  }
11500  }
11501 
11502  free_var(&base_prod);
11503 
11504  /* Compensate for input sign, and round to requested rscale */
11505  if (neg)
11506  div_var_fast(&const_one, result, result, rscale, true);
11507  else
11508  round_var(result, rscale);
11509 }
11510 
11511 /*
11512  * power_ten_int() -
11513  *
11514  * Raise ten to the power of exp, where exp is an integer. Note that unlike
11515  * power_var_int(), this does no overflow/underflow checking or rounding.
11516  */
11517 static void
11518 power_ten_int(int exp, NumericVar *result)
11519 {
11520  /* Construct the result directly, starting from 10^0 = 1 */
11521  set_var_from_var(&const_one, result);
11522 
11523  /* Scale needed to represent the result exactly */
11524  result->dscale = exp < 0 ? -exp : 0;
11525 
11526  /* Base-NBASE weight of result and remaining exponent */
11527  if (exp >= 0)
11528  result->weight = exp / DEC_DIGITS;
11529  else
11530  result->weight = (exp + 1) / DEC_DIGITS - 1;
11531 
11532  exp -= result->weight * DEC_DIGITS;
11533 
11534  /* Final adjustment of the result's single NBASE digit */
11535  while (exp-- > 0)
11536  result->digits[0] *= 10;
11537 }
11538 
11539 /*
11540  * random_var() - return a random value in the range [rmin, rmax].
11541  */
11542 static void
11544  const NumericVar *rmax, NumericVar *result)
11545 {
11546  int rscale;
11547  NumericVar rlen;
11548  int res_ndigits;
11549  int n;
11550  int pow10;
11551  int i;
11552  uint64 rlen64;
11553  int rlen64_ndigits;
11554 
11555  rscale = Max(rmin->dscale, rmax->dscale);
11556 
11557  /* Compute rlen = rmax - rmin and check the range bounds */
11558  init_var(&rlen);
11559  sub_var(rmax, rmin, &rlen);
11560 
11561  if (rlen.sign == NUMERIC_NEG)
11562  ereport(ERROR,
11563  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11564  errmsg("lower bound must be less than or equal to upper bound"));
11565 
11566  /* Special case for an empty range */
11567  if (rlen.ndigits == 0)
11568  {
11569  set_var_from_var(rmin, result);
11570  result->dscale = rscale;
11571  free_var(&rlen);
11572  return;
11573  }
11574 
11575  /*
11576  * Otherwise, select a random value in the range [0, rlen = rmax - rmin],
11577  * and shift it to the required range by adding rmin.
11578  */
11579 
11580  /* Required result digits */
11581  res_ndigits = rlen.weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
11582 
11583  /*
11584  * To get the required rscale, the final result digit must be a multiple
11585  * of pow10 = 10^n, where n = (-rscale) mod DEC_DIGITS.
11586  */
11587  n = ((rscale + DEC_DIGITS - 1) / DEC_DIGITS) * DEC_DIGITS - rscale;
11588  pow10 = 1;
11589  for (i = 0; i < n; i++)
11590  pow10 *= 10;
11591 
11592  /*
11593  * To choose a random value uniformly from the range [0, rlen], we choose
11594  * from the slightly larger range [0, rlen2], where rlen2 is formed from
11595  * rlen by copying the first 4 NBASE digits, and setting all remaining
11596  * decimal digits to "9".
11597  *
11598  * Without loss of generality, we can ignore the weight of rlen2 and treat
11599  * it as a pure integer for the purposes of this discussion. The process
11600  * above gives rlen2 + 1 = rlen64 * 10^N, for some integer N, where rlen64
11601  * is a 64-bit integer formed from the first 4 NBASE digits copied from
11602  * rlen. Since this trivially factors into smaller pieces that fit in
11603  * 64-bit integers, the task of choosing a random value uniformly from the
11604  * rlen2 + 1 possible values in [0, rlen2] is much simpler.
11605  *
11606  * If the random value selected is too large, it is rejected, and we try
11607  * again until we get a result <= rlen, ensuring that the overall result
11608  * is uniform (no particular value is any more likely than any other).
11609  *
11610  * Since rlen64 holds 4 NBASE digits from rlen, it contains at least
11611  * DEC_DIGITS * 3 + 1 decimal digits (i.e., at least 13 decimal digits,
11612  * when DEC_DIGITS is 4). Therefore the probability of needing to reject
11613  * the value chosen and retry is less than 1e-13.
11614  */
11615  rlen64 = (uint64) rlen.digits[0];
11616  rlen64_ndigits = 1;
11617  while (rlen64_ndigits < res_ndigits && rlen64_ndigits < 4)
11618  {
11619  rlen64 *= NBASE;
11620  if (rlen64_ndigits < rlen.ndigits)
11621  rlen64 += rlen.digits[rlen64_ndigits];
11622  rlen64_ndigits++;
11623  }
11624 
11625  /* Loop until we get a result <= rlen */
11626  do
11627  {
11628  NumericDigit *res_digits;
11629  uint64 rand;
11630  int whole_ndigits;
11631 
11632  alloc_var(result, res_ndigits);
11633  result->sign = NUMERIC_POS;
11634  result->weight = rlen.weight;
11635  result->dscale = rscale;
11636  res_digits = result->digits;
11637 
11638  /*
11639  * Set the first rlen64_ndigits using a random value in [0, rlen64].
11640  *
11641  * If this is the whole result, and rscale is not a multiple of
11642  * DEC_DIGITS (pow10 from above is not 1), then we need this to be a
11643  * multiple of pow10.
11644  */
11645  if (rlen64_ndigits == res_ndigits && pow10 != 1)
11646  rand = pg_prng_uint64_range(state, 0, rlen64 / pow10) * pow10;
11647  else
11648  rand = pg_prng_uint64_range(state, 0, rlen64);
11649 
11650  for (i = rlen64_ndigits - 1; i >= 0; i--)
11651  {
11652  res_digits[i] = (NumericDigit) (rand % NBASE);
11653  rand = rand / NBASE;
11654  }
11655 
11656  /*
11657  * Set the remaining digits to random values in range [0, NBASE),
11658  * noting that the last digit needs to be a multiple of pow10.
11659  */
11660  whole_ndigits = res_ndigits;
11661  if (pow10 != 1)
11662  whole_ndigits--;
11663 
11664  /* Set whole digits in groups of 4 for best performance */
11665  i = rlen64_ndigits;
11666  while (i < whole_ndigits - 3)
11667  {
11668  rand = pg_prng_uint64_range(state, 0,
11669  (uint64) NBASE * NBASE * NBASE * NBASE - 1);
11670  res_digits[i++] = (NumericDigit) (rand % NBASE);
11671  rand = rand / NBASE;
11672  res_digits[i++] = (NumericDigit) (rand % NBASE);
11673  rand = rand / NBASE;
11674  res_digits[i++] = (NumericDigit) (rand % NBASE);
11675  rand = rand / NBASE;
11676  res_digits[i++] = (NumericDigit) rand;
11677  }
11678 
11679  /* Remaining whole digits */
11680  while (i < whole_ndigits)
11681  {
11682  rand = pg_prng_uint64_range(state, 0, NBASE - 1);
11683  res_digits[i++] = (NumericDigit) rand;
11684  }
11685 
11686  /* Final partial digit (multiple of pow10) */
11687  if (i < res_ndigits)
11688  {
11689  rand = pg_prng_uint64_range(state, 0, NBASE / pow10 - 1) * pow10;
11690  res_digits[i] = (NumericDigit) rand;
11691  }
11692 
11693  /* Remove leading/trailing zeroes */
11694  strip_var(result);
11695 
11696  /* If result > rlen, try again */
11697 
11698  } while (cmp_var(result, &rlen) > 0);
11699 
11700  /* Offset the result to the required range */
11701  add_var(result, rmin, result);
11702 
11703  free_var(&rlen);
11704 }
11705 
11706 
11707 /* ----------------------------------------------------------------------
11708  *
11709  * Following are the lowest level functions that operate unsigned
11710  * on the variable level
11711  *
11712  * ----------------------------------------------------------------------
11713  */
11714 
11715 
11716 /* ----------
11717  * cmp_abs() -
11718  *
11719  * Compare the absolute values of var1 and var2
11720  * Returns: -1 for ABS(var1) < ABS(var2)
11721  * 0 for ABS(var1) == ABS(var2)
11722  * 1 for ABS(var1) > ABS(var2)
11723  * ----------
11724  */
11725 static int
11726 cmp_abs(const NumericVar *var1, const NumericVar *var2)
11727 {
11728  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
11729  var2->digits, var2->ndigits, var2->weight);
11730 }
11731 
11732 /* ----------
11733  * cmp_abs_common() -
11734  *
11735  * Main routine of cmp_abs(). This function can be used by both
11736  * NumericVar and Numeric.
11737  * ----------
11738  */
11739 static int
11740 cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight,
11741  const NumericDigit *var2digits, int var2ndigits, int var2weight)
11742 {
11743  int i1 = 0;
11744  int i2 = 0;
11745 
11746  /* Check any digits before the first common digit */
11747 
11748  while (var1weight > var2weight && i1 < var1ndigits)
11749  {
11750  if (var1digits[i1++] != 0)
11751  return 1;
11752  var1weight--;
11753  }
11754  while (var2weight > var1weight && i2 < var2ndigits)
11755  {
11756  if (var2digits[i2++] != 0)
11757  return -1;
11758  var2weight--;
11759  }
11760 
11761  /* At this point, either w1 == w2 or we've run out of digits */
11762 
11763  if (var1weight == var2weight)
11764  {
11765  while (i1 < var1ndigits && i2 < var2ndigits)
11766  {
11767  int stat = var1digits[i1++] - var2digits[i2++];
11768 
11769  if (stat)
11770  {
11771  if (stat > 0)
11772  return 1;
11773  return -1;
11774  }
11775  }
11776  }
11777 
11778  /*
11779  * At this point, we've run out of digits on one side or the other; so any
11780  * remaining nonzero digits imply that side is larger
11781  */
11782  while (i1 < var1ndigits)
11783  {
11784  if (var1digits[i1++] != 0)
11785  return 1;
11786  }
11787  while (i2 < var2ndigits)
11788  {
11789  if (var2digits[i2++] != 0)
11790  return -1;
11791  }
11792 
11793  return 0;
11794 }
11795 
11796 
11797 /*
11798  * add_abs() -
11799  *
11800  * Add the absolute values of two variables into result.
11801  * result might point to one of the operands without danger.
11802  */
11803 static void
11804 add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
11805 {
11806  NumericDigit *res_buf;
11807  NumericDigit *res_digits;
11808  int res_ndigits;
11809  int res_weight;
11810  int res_rscale,
11811  rscale1,
11812  rscale2;
11813  int res_dscale;
11814  int i,
11815  i1,
11816  i2;
11817  int carry = 0;
11818 
11819  /* copy these values into local vars for speed in inner loop */
11820  int var1ndigits = var1->ndigits;
11821  int var2ndigits = var2->ndigits;
11822  NumericDigit *var1digits = var1->digits;
11823  NumericDigit *var2digits = var2->digits;
11824 
11825  res_weight = Max(var1->weight, var2->weight) + 1;
11826 
11827  res_dscale = Max(var1->dscale, var2->dscale);
11828 
11829  /* Note: here we are figuring rscale in base-NBASE digits */
11830  rscale1 = var1->ndigits - var1->weight - 1;
11831  rscale2 = var2->ndigits - var2->weight - 1;
11832  res_rscale = Max(rscale1, rscale2);
11833 
11834  res_ndigits = res_rscale + res_weight + 1;
11835  if (res_ndigits <= 0)
11836  res_ndigits = 1;
11837 
11838  res_buf = digitbuf_alloc(res_ndigits + 1);
11839  res_buf[0] = 0; /* spare digit for later rounding */
11840  res_digits = res_buf + 1;
11841 
11842  i1 = res_rscale + var1->weight + 1;
11843  i2 = res_rscale + var2->weight + 1;
11844  for (i = res_ndigits - 1; i >= 0; i--)
11845  {
11846  i1--;
11847  i2--;
11848  if (i1 >= 0 && i1 < var1ndigits)
11849  carry += var1digits[i1];
11850  if (i2 >= 0 && i2 < var2ndigits)
11851  carry += var2digits[i2];
11852 
11853  if (carry >= NBASE)
11854  {
11855  res_digits[i] = carry - NBASE;
11856  carry = 1;
11857  }
11858  else
11859  {
11860  res_digits[i] = carry;
11861  carry = 0;
11862  }
11863  }
11864 
11865  Assert(carry == 0); /* else we failed to allow for carry out */
11866 
11867  digitbuf_free(result->buf);
11868  result->ndigits = res_ndigits;
11869  result->buf = res_buf;
11870  result->digits = res_digits;
11871  result->weight = res_weight;
11872  result->dscale = res_dscale;
11873 
11874  /* Remove leading/trailing zeroes */
11875  strip_var(result);
11876 }
11877 
11878 
11879 /*
11880  * sub_abs()
11881  *
11882  * Subtract the absolute value of var2 from the absolute value of var1
11883  * and store in result. result might point to one of the operands
11884  * without danger.
11885  *
11886  * ABS(var1) MUST BE GREATER OR EQUAL ABS(var2) !!!
11887  */
11888 static void
11889 sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
11890 {
11891  NumericDigit *res_buf;
11892  NumericDigit *res_digits;
11893  int res_ndigits;
11894  int res_weight;
11895  int res_rscale,
11896  rscale1,
11897  rscale2;
11898  int res_dscale;
11899  int i,
11900  i1,
11901  i2;
11902  int borrow = 0;
11903 
11904  /* copy these values into local vars for speed in inner loop */
11905  int var1ndigits = var1->ndigits;
11906  int var2ndigits = var2->ndigits;
11907  NumericDigit *var1digits = var1->digits;
11908  NumericDigit *var2digits = var2->digits;
11909 
11910  res_weight = var1->weight;
11911 
11912  res_dscale = Max(var1->dscale, var2->dscale);
11913 
11914  /* Note: here we are figuring rscale in base-NBASE digits */
11915  rscale1 = var1->ndigits - var1->weight - 1;
11916  rscale2 = var2->ndigits - var2->weight - 1;
11917  res_rscale = Max(rscale1, rscale2);
11918 
11919  res_ndigits = res_rscale + res_weight + 1;
11920  if (res_ndigits <= 0)
11921  res_ndigits = 1;
11922 
11923  res_buf = digitbuf_alloc(res_ndigits + 1);
11924  res_buf[0] = 0; /* spare digit for later rounding */
11925  res_digits = res_buf + 1;
11926 
11927  i1 = res_rscale + var1->weight + 1;
11928  i2 = res_rscale + var2->weight + 1;
11929  for (i = res_ndigits - 1; i >= 0; i--)
11930  {
11931  i1--;
11932  i2--;
11933  if (i1 >= 0 && i1 < var1ndigits)
11934  borrow += var1digits[i1];
11935  if (i2 >= 0 && i2 < var2ndigits)
11936  borrow -= var2digits[i2];
11937 
11938  if (borrow < 0)
11939  {
11940  res_digits[i] = borrow + NBASE;
11941  borrow = -1;
11942  }
11943  else
11944  {
11945  res_digits[i] = borrow;
11946  borrow = 0;
11947  }
11948  }
11949 
11950  Assert(borrow == 0); /* else caller gave us var1 < var2 */
11951 
11952  digitbuf_free(result->buf);
11953  result->ndigits = res_ndigits;
11954  result->buf = res_buf;
11955  result->digits = res_digits;
11956  result->weight = res_weight;
11957  result->dscale = res_dscale;
11958 
11959  /* Remove leading/trailing zeroes */
11960  strip_var(result);
11961 }
11962 
11963 /*
11964  * round_var
11965  *
11966  * Round the value of a variable to no more than rscale decimal digits
11967  * after the decimal point. NOTE: we allow rscale < 0 here, implying
11968  * rounding before the decimal point.
11969  */
11970 static void
11971 round_var(NumericVar *var, int rscale)
11972 {
11973  NumericDigit *digits = var->digits;
11974  int di;
11975  int ndigits;
11976  int carry;
11977 
11978  var->dscale = rscale;
11979 
11980  /* decimal digits wanted */
11981  di = (var->weight + 1) * DEC_DIGITS + rscale;
11982 
11983  /*
11984  * If di = 0, the value loses all digits, but could round up to 1 if its
11985  * first extra digit is >= 5. If di < 0 the result must be 0.
11986  */
11987  if (di < 0)
11988  {
11989  var->ndigits = 0;
11990  var->weight = 0;
11991  var->sign = NUMERIC_POS;
11992  }
11993  else
11994  {
11995  /* NBASE digits wanted */
11996  ndigits = (di + DEC_DIGITS - 1) / DEC_DIGITS;
11997 
11998  /* 0, or number of decimal digits to keep in last NBASE digit */
11999  di %= DEC_DIGITS;
12000 
12001  if (ndigits < var->ndigits ||
12002  (ndigits == var->ndigits && di > 0))
12003  {
12004  var->ndigits = ndigits;
12005 
12006 #if DEC_DIGITS == 1
12007  /* di must be zero */
12008  carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
12009 #else
12010  if (di == 0)
12011  carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
12012  else
12013  {
12014  /* Must round within last NBASE digit */
12015  int extra,
12016  pow10;
12017 
12018 #if DEC_DIGITS == 4
12019  pow10 = round_powers[di];
12020 #elif DEC_DIGITS == 2
12021  pow10 = 10;
12022 #else
12023 #error unsupported NBASE
12024 #endif
12025  extra = digits[--ndigits] % pow10;
12026  digits[ndigits] -= extra;
12027  carry = 0;
12028  if (extra >= pow10 / 2)
12029  {
12030  pow10 += digits[ndigits];
12031  if (pow10 >= NBASE)
12032  {
12033  pow10 -= NBASE;
12034  carry = 1;
12035  }
12036  digits[ndigits] = pow10;
12037  }
12038  }
12039 #endif
12040 
12041  /* Propagate carry if needed */
12042  while (carry)
12043  {
12044  carry += digits[--ndigits];
12045  if (carry >= NBASE)
12046  {
12047  digits[ndigits] = carry - NBASE;
12048  carry = 1;
12049  }
12050  else
12051  {
12052  digits[ndigits] = carry;
12053  carry = 0;
12054  }
12055  }
12056 
12057  if (ndigits < 0)
12058  {
12059  Assert(ndigits == -1); /* better not have added > 1 digit */
12060  Assert(var->digits > var->buf);
12061  var->digits--;
12062  var->ndigits++;
12063  var->weight++;
12064  }
12065  }
12066  }
12067 }
12068 
12069 /*
12070  * trunc_var
12071  *
12072  * Truncate (towards zero) the value of a variable at rscale decimal digits
12073  * after the decimal point. NOTE: we allow rscale < 0 here, implying
12074  * truncation before the decimal point.
12075  */
12076 static void
12077 trunc_var(NumericVar *var, int rscale)
12078 {
12079  int di;
12080  int ndigits;
12081 
12082  var->dscale = rscale;
12083 
12084  /* decimal digits wanted */
12085  di = (var->weight + 1) * DEC_DIGITS + rscale;
12086 
12087  /*
12088  * If di <= 0, the value loses all digits.
12089  */
12090  if (di <= 0)
12091  {
12092  var->ndigits = 0;
12093  var->weight = 0;
12094  var->sign = NUMERIC_POS;
12095  }
12096  else
12097  {
12098  /* NBASE digits wanted */
12099  ndigits = (di + DEC_DIGITS - 1) / DEC_DIGITS;
12100 
12101  if (ndigits <= var->ndigits)
12102  {
12103  var->ndigits = ndigits;
12104 
12105 #if DEC_DIGITS == 1
12106  /* no within-digit stuff to worry about */
12107 #else
12108  /* 0, or number of decimal digits to keep in last NBASE digit */
12109  di %= DEC_DIGITS;
12110 
12111  if (di > 0)
12112  {
12113  /* Must truncate within last NBASE digit */
12114  NumericDigit *digits = var->digits;
12115  int extra,
12116  pow10;
12117 
12118 #if DEC_DIGITS == 4
12119  pow10 = round_powers[di];
12120 #elif DEC_DIGITS == 2
12121  pow10 = 10;
12122 #else
12123 #error unsupported NBASE
12124 #endif
12125  extra = digits[--ndigits] % pow10;
12126  digits[ndigits] -= extra;
12127  }
12128 #endif
12129  }
12130  }
12131 }
12132 
12133 /*
12134  * strip_var
12135  *
12136  * Strip any leading and trailing zeroes from a numeric variable
12137  */
12138 static void
12140 {
12141  NumericDigit *digits = var->digits;
12142  int ndigits = var->ndigits;
12143 
12144  /* Strip leading zeroes */
12145  while (ndigits > 0 && *digits == 0)
12146  {
12147  digits++;
12148  var->weight--;
12149  ndigits--;
12150  }
12151 
12152  /* Strip trailing zeroes */
12153  while (ndigits > 0 && digits[ndigits - 1] == 0)
12154  ndigits--;
12155 
12156  /* If it's zero, normalize the sign and weight */
12157  if (ndigits == 0)
12158  {
12159  var->sign = NUMERIC_POS;
12160  var->weight = 0;
12161  }
12162 
12163  var->digits = digits;
12164  var->ndigits = ndigits;
12165 }
12166 
12167 
12168 /* ----------------------------------------------------------------------
12169  *
12170  * Fast sum accumulator functions
12171  *
12172  * ----------------------------------------------------------------------
12173  */
12174 
12175 /*
12176  * Reset the accumulator's value to zero. The buffers to hold the digits
12177  * are not free'd.
12178  */
12179 static void
12181 {
12182  int i;
12183 
12184  accum->dscale = 0;
12185  for (i = 0; i < accum->ndigits; i++)
12186  {
12187  accum->pos_digits[i] = 0;
12188  accum->neg_digits[i] = 0;
12189  }
12190 }
12191 
12192 /*
12193  * Accumulate a new value.
12194  */
12195 static void
12197 {
12198  int32 *accum_digits;
12199  int i,
12200  val_i;
12201  int val_ndigits;
12202  NumericDigit *val_digits;
12203 
12204  /*
12205  * If we have accumulated too many values since the last carry
12206  * propagation, do it now, to avoid overflowing. (We could allow more
12207  * than NBASE - 1, if we reserved two extra digits, rather than one, for
12208  * carry propagation. But even with NBASE - 1, this needs to be done so
12209  * seldom, that the performance difference is negligible.)
12210  */
12211  if (accum->num_uncarried == NBASE - 1)
12212  accum_sum_carry(accum);
12213 
12214  /*
12215  * Adjust the weight or scale of the old value, so that it can accommodate
12216  * the new value.
12217  */
12218  accum_sum_rescale(accum, val);
12219 
12220  /* */
12221  if (val->sign == NUMERIC_POS)
12222  accum_digits = accum->pos_digits;
12223  else
12224  accum_digits = accum->neg_digits;
12225 
12226  /* copy these values into local vars for speed in loop */
12227  val_ndigits = val->ndigits;
12228  val_digits = val->digits;
12229 
12230  i = accum->weight - val->weight;
12231  for (val_i = 0; val_i < val_ndigits; val_i++)
12232  {
12233  accum_digits[i] += (int32) val_digits[val_i];
12234  i++;
12235  }
12236 
12237  accum->num_uncarried++;
12238 }
12239 
12240 /*
12241  * Propagate carries.
12242  */
12243 static void
12245 {
12246  int i;
12247  int ndigits;
12248  int32 *dig;
12249  int32 carry;
12250  int32 newdig = 0;
12251 
12252  /*
12253  * If no new values have been added since last carry propagation, nothing
12254  * to do.
12255  */
12256  if (accum->num_uncarried == 0)
12257  return;
12258 
12259  /*
12260  * We maintain that the weight of the accumulator is always one larger
12261  * than needed to hold the current value, before carrying, to make sure
12262  * there is enough space for the possible extra digit when carry is
12263  * propagated. We cannot expand the buffer here, unless we require
12264  * callers of accum_sum_final() to switch to the right memory context.
12265  */
12266  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
12267 
12268  ndigits = accum->ndigits;
12269 
12270  /* Propagate carry in the positive sum */
12271  dig = accum->pos_digits;
12272  carry = 0;
12273  for (i = ndigits - 1; i >= 0; i--)
12274  {
12275  newdig = dig[i] + carry;
12276  if (newdig >= NBASE)
12277  {
12278  carry = newdig / NBASE;
12279  newdig -= carry * NBASE;
12280  }
12281  else
12282  carry = 0;
12283  dig[i] = newdig;
12284  }
12285  /* Did we use up the digit reserved for carry propagation? */
12286  if (newdig > 0)
12287  accum->have_carry_space = false;
12288 
12289  /* And the same for the negative sum */
12290  dig = accum->neg_digits;
12291  carry = 0;
12292  for (i = ndigits - 1; i >= 0; i--)
12293  {
12294  newdig = dig[i] + carry;
12295  if (newdig >= NBASE)
12296  {
12297  carry = newdig / NBASE;
12298  newdig -= carry * NBASE;
12299  }
12300  else
12301  carry = 0;
12302  dig[i] = newdig;
12303  }
12304  if (newdig > 0)
12305  accum->have_carry_space = false;
12306 
12307  accum->num_uncarried = 0;
12308 }
12309 
12310 /*
12311  * Re-scale accumulator to accommodate new value.
12312  *
12313  * If the new value has more digits than the current digit buffers in the
12314  * accumulator, enlarge the buffers.
12315  */
12316 static void
12318 {
12319  int old_weight = accum->weight;
12320  int old_ndigits = accum->ndigits;
12321  int accum_ndigits;
12322  int accum_weight;
12323  int accum_rscale;
12324  int val_rscale;
12325 
12326  accum_weight = old_weight;
12327  accum_ndigits = old_ndigits;
12328 
12329  /*
12330  * Does the new value have a larger weight? If so, enlarge the buffers,
12331  * and shift the existing value to the new weight, by adding leading
12332  * zeros.
12333  *
12334  * We enforce that the accumulator always has a weight one larger than
12335  * needed for the inputs, so that we have space for an extra digit at the
12336  * final carry-propagation phase, if necessary.
12337  */
12338  if (val->weight >= accum_weight)
12339  {
12340  accum_weight = val->weight + 1;
12341  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
12342  }
12343 
12344  /*
12345  * Even though the new value is small, we might've used up the space
12346  * reserved for the carry digit in the last call to accum_sum_carry(). If
12347  * so, enlarge to make room for another one.
12348  */
12349  else if (!accum->have_carry_space)
12350  {
12351  accum_weight++;
12352  accum_ndigits++;
12353  }
12354 
12355  /* Is the new value wider on the right side? */
12356  accum_rscale = accum_ndigits - accum_weight - 1;
12357  val_rscale = val->ndigits - val->weight - 1;
12358  if (val_rscale > accum_rscale)
12359  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
12360 
12361  if (accum_ndigits != old_ndigits ||
12362  accum_weight != old_weight)
12363  {
12364  int32 *new_pos_digits;
12365  int32 *new_neg_digits;
12366  int weightdiff;
12367 
12368  weightdiff = accum_weight - old_weight;
12369 
12370  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
12371  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
12372 
12373  if (accum->pos_digits)
12374  {
12375  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
12376  old_ndigits * sizeof(int32));
12377  pfree(accum->pos_digits);
12378 
12379  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
12380  old_ndigits * sizeof(int32));
12381  pfree(accum->neg_digits);
12382  }
12383 
12384  accum->pos_digits = new_pos_digits;
12385  accum->neg_digits = new_neg_digits;
12386 
12387  accum->weight = accum_weight;
12388  accum->ndigits = accum_ndigits;
12389 
12390  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
12391  accum->have_carry_space = true;
12392  }
12393 
12394  if (val->dscale > accum->dscale)
12395  accum->dscale = val->dscale;
12396 }
12397 
12398 /*
12399  * Return the current value of the accumulator. This perform final carry
12400  * propagation, and adds together the positive and negative sums.
12401  *
12402  * Unlike all the other routines, the caller is not required to switch to
12403  * the memory context that holds the accumulator.
12404  */
12405 static void
12407 {
12408  int i;
12409  NumericVar pos_var;
12410  NumericVar neg_var;
12411 
12412  if (accum->ndigits == 0)
12413  {
12414  set_var_from_var(&const_zero, result);
12415  return;
12416  }
12417 
12418  /* Perform final carry */
12419  accum_sum_carry(accum);
12420 
12421  /* Create NumericVars representing the positive and negative sums */
12422  init_var(&pos_var);
12423  init_var(&neg_var);
12424 
12425  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
12426  pos_var.weight = neg_var.weight = accum->weight;
12427  pos_var.dscale = neg_var.dscale = accum->dscale;
12428  pos_var.sign = NUMERIC_POS;
12429  neg_var.sign = NUMERIC_NEG;
12430 
12431  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
12432  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
12433 
12434  for (i = 0; i < accum->ndigits; i++)
12435  {
12436  Assert(accum->pos_digits[i] < NBASE);
12437  pos_var.digits[i] = (int16) accum->pos_digits[i];
12438 
12439  Assert(accum->neg_digits[i] < NBASE);
12440  neg_var.digits[i] = (int16) accum->neg_digits[i];
12441  }
12442 
12443  /* And add them together */
12444  add_var(&pos_var, &neg_var, result);
12445 
12446  /* Remove leading/trailing zeroes */
12447  strip_var(result);
12448 }
12449 
12450 /*
12451  * Copy an accumulator's state.
12452  *
12453  * 'dst' is assumed to be uninitialized beforehand. No attempt is made at
12454  * freeing old values.
12455  */
12456 static void
12458 {
12459  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
12460  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
12461 
12462  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
12463  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
12464  dst->num_uncarried = src->num_uncarried;
12465  dst->ndigits = src->ndigits;
12466  dst->weight = src->weight;
12467  dst->dscale = src->dscale;
12468 }
12469 
12470 /*
12471  * Add the current value of 'accum2' into 'accum'.
12472  */
12473 static void
12475 {
12476  NumericVar tmp_var;
12477 
12478  init_var(&tmp_var);
12479 
12480  accum_sum_final(accum2, &tmp_var);
12481  accum_sum_add(accum, &tmp_var);
12482 
12483  free_var(&tmp_var);
12484 }
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:264
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
#define ARR_DATA_PTR(a)
Definition: array.h:322
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:265
#define ARR_SIZE(a)
Definition: array.h:289
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:291
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:233
Datum numeric_stddev_samp(PG_FUNCTION_ARGS)
Definition: numeric.c:6311
static const NumericVar const_two
Definition: numeric.c:433
static void mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10066
static const NumericVar const_pinf
Definition: numeric.c:459
static char * get_str_from_var_sci(const NumericVar *var, int rscale)
Definition: numeric.c:7651
#define NUMERIC_INF_SIGN_MASK
Definition: numeric.c:200
#define dump_numeric(s, n)
Definition: numeric.c:479
#define NUMERIC_CAN_BE_SHORT(scale, weight)
Definition: numeric.c:497
static void ceil_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:10165
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:12244
#define DatumGetNumericAbbrev(X)
Definition: numeric.c:410
#define NUMERIC_HDRSZ_SHORT
Definition: numeric.c:175
#define digitbuf_free(buf)
Definition: numeric.c:485
Datum numeric_poly_var_samp(PG_FUNCTION_ARGS)
Definition: numeric.c:6408
static const NumericDigit const_zero_point_nine_data[1]
Definition: numeric.c:437
Datum numeric_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:5024
static void exp_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:10762
Datum numeric_poly_serialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5685
Datum int2_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:5978
static Datum numeric_abbrev_convert_var(const NumericVar *var, NumericSortSupport *nss)
Datum numeric_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:5435
Datum numeric_div(PG_FUNCTION_ARGS)
Definition: numeric.c:3128
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2929
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:953
static bool numeric_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: numeric.c:2112
Datum numeric_poly_var_pop(PG_FUNCTION_ARGS)
Definition: numeric.c:6450
Datum numeric_avg_serialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5208
Datum hash_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2701
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
Definition: numeric.c:2781
static bool numeric_is_integral(Numeric num)
Definition: numeric.c:873
static NumericAggState * makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
Definition: numeric.c:4821
Datum numeric_var_pop(PG_FUNCTION_ARGS)
Definition: numeric.c:6328
static int get_min_scale(NumericVar *var)
Definition: numeric.c:4140
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:11889
int32 numeric_int4_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4401
static void power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
Definition: numeric.c:11151
static bool is_valid_numeric_typmod(int32 typmod)
Definition: numeric.c:916
static void div_var_int(const NumericVar *var, int ival, int ival_weight, NumericVar *result, int rscale, bool round)
Definition: numeric.c:9769
static int cmp_numerics(Numeric num1, Numeric num2)
Definition: numeric.c:2509
#define NUMERIC_NINF
Definition: numeric.c:199
Datum numeric_avg(PG_FUNCTION_ARGS)
Definition: numeric.c:6132
static void numericvar_serialize(StringInfo buf, const NumericVar *var)
Definition: numeric.c:7728
static NumericAggState * makeNumericAggStateCurrentContext(bool calcSumX2)
Definition: numeric.c:4846
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:12406
static void power_var_int(const NumericVar *base, int exp, int exp_dscale, NumericVar *result)
Definition: numeric.c:11313
static void free_var(NumericVar *var)
Definition: numeric.c:6973
#define NUMERIC_DSCALE(n)
Definition: numeric.c:242
#define NUMERIC_SIGN(n)
Definition: numeric.c:236
Datum int2_sum(PG_FUNCTION_ARGS)
Definition: numeric.c:6512
static int numeric_typmod_scale(int32 typmod)
Definition: numeric.c:942
char * numeric_normalize(Numeric num)
Definition: numeric.c:1026
Datum numeric_poly_sum(PG_FUNCTION_ARGS)
Definition: numeric.c:6074
Datum int2_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:5554
Datum float8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4597
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:12077
Datum numeric_round(PG_FUNCTION_ARGS)
Definition: numeric.c:1543
static void set_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7424
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:12196
Datum numeric_cmp(PG_FUNCTION_ARGS)
Definition: numeric.c:2403
Datum int4_avg_combine(PG_FUNCTION_ARGS)
Definition: numeric.c:6717
Datum width_bucket_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1846
Datum int8_sum(PG_FUNCTION_ARGS)
Definition: numeric.c:6613
Datum int8_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:6028
Datum numerictypmodout(PG_FUNCTION_ARGS)
Definition: numeric.c:1369
int16 NumericDigit
Definition: numeric.c:101
static Numeric duplicate_numeric(Numeric num)
Definition: numeric.c:7767
#define makePolyNumAggState
Definition: numeric.c:5549
Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3372
static int32 make_numeric_typmod(int precision, int scale)
Definition: numeric.c:907
Datum numeric_recv(PG_FUNCTION_ARGS)
Definition: numeric.c:1078
Numeric numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3148
#define dump_var(s, v)
Definition: numeric.c:480
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8552
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4287
Datum numeric_trim_scale(PG_FUNCTION_ARGS)
Definition: numeric.c:4211
Datum int2int4_sum(PG_FUNCTION_ARGS)
Definition: numeric.c:6831
static Numeric numeric_stddev_internal(NumericAggState *state, bool variance, bool sample, bool *is_null)
Definition: numeric.c:6210
Numeric random_numeric(pg_prng_state *state, Numeric rmin, Numeric rmax)
Definition: numeric.c:4232
Datum float4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4691
Datum numeric_poly_stddev_samp(PG_FUNCTION_ARGS)
Definition: numeric.c:6429
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:11804
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1709
Datum numeric_int8(PG_FUNCTION_ARGS)
Definition: numeric.c:4539
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:4861
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4393
Datum numeric_sum(PG_FUNCTION_ARGS)
Definition: numeric.c:6167
Datum numeric_ge(PG_FUNCTION_ARGS)
Definition: numeric.c:2464
static const NumericVar const_ninf
Definition: numeric.c:462
#define DIV_GUARD_DIGITS
Definition: numeric.c:99
Datum numeric_uplus(PG_FUNCTION_ARGS)
Definition: numeric.c:1462
Datum numeric_stddev_pop(PG_FUNCTION_ARGS)
Definition: numeric.c:6345
Datum numeric_uminus(PG_FUNCTION_ARGS)
Definition: numeric.c:1420
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8435
static Datum numeric_abbrev_convert(Datum original_datum, SortSupport ssup)
Definition: numeric.c:2050
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:172
#define NA_TOTAL_COUNT(na)
Definition: numeric.c:4813
Datum numeric_le(PG_FUNCTION_ARGS)
Definition: numeric.c:2494
Datum numeric_ceil(PG_FUNCTION_ARGS)
Definition: numeric.c:1647
Datum numeric_send(PG_FUNCTION_ARGS)
Definition: numeric.c:1163
struct Int8TransTypeData Int8TransTypeData
static void gcd_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10212
static bool set_var_from_non_decimal_integer_str(const char *str, const char *cp, int sign, int base, NumericVar *dest, const char **endptr, Node *escontext)
Definition: numeric.c:7246
Datum numerictypmodin(PG_FUNCTION_ARGS)
Definition: numeric.c:1324
Datum in_range_numeric_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2566
Datum numeric_power(PG_FUNCTION_ARGS)
Definition: numeric.c:3939
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4308
static const NumericVar const_minus_one
Definition: numeric.c:429
static void div_var_fast(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:9405
static bool numericvar_to_int32(const NumericVar *var, int32 *result)
Definition: numeric.c:4464
static int numeric_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: numeric.c:2185
static void floor_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:10189
Datum numeric_int4(PG_FUNCTION_ARGS)
Definition: numeric.c:4451
Datum numeric_gcd(PG_FUNCTION_ARGS)
Definition: numeric.c:3525
static const NumericDigit const_one_data[1]
Definition: numeric.c:425
Datum numeric_poly_combine(PG_FUNCTION_ARGS)
Definition: numeric.c:5620
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7455
static const NumericDigit const_two_data[1]
Definition: numeric.c:432
Datum numeric_fac(PG_FUNCTION_ARGS)
Definition: numeric.c:3628
Datum numeric_serialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5318
#define NUMERIC_SHORT_WEIGHT_MASK
Definition: numeric.c:219
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:204
static void zero_var(NumericVar *var)
Definition: numeric.c:6989
Datum numeric_ne(PG_FUNCTION_ARGS)
Definition: numeric.c:2434
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK
Definition: numeric.c:218
Datum numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1246
Datum int4_avg_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:6776
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:4931
Datum int8_avg_serialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5883
Datum numeric_exp(PG_FUNCTION_ARGS)
Definition: numeric.c:3752
static int numeric_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
Definition: numeric.c:2207
Datum numeric_log(PG_FUNCTION_ARGS)
Definition: numeric.c:3868
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8673
Datum numeric_scale(PG_FUNCTION_ARGS)
Definition: numeric.c:4126
static Numeric make_result_opt_error(const NumericVar *var, bool *have_error)
Definition: numeric.c:7786
Datum numeric_sqrt(PG_FUNCTION_ARGS)
Definition: numeric.c:3680
static bool apply_typmod_special(Numeric num, int32 typmod, Node *escontext)
Definition: numeric.c:7996
Datum int4_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:6003
Datum numeric_poly_deserialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5743
#define NUMERIC_IS_INF(n)
Definition: numeric.c:206
Datum numeric_inc(PG_FUNCTION_ARGS)
Definition: numeric.c:3441
static void random_var(pg_prng_state *state, const NumericVar *rmin, const NumericVar *rmax, NumericVar *result)
Definition: numeric.c:11543
static char * get_str_from_var(const NumericVar *var)
Definition: numeric.c:7498
Datum int8_avg_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:6049
Datum int4_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:5577
Datum generate_series_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1703
#define NUMERIC_NEG
Definition: numeric.c:166
#define NUMERIC_ABBREV_PINF
Definition: numeric.c:412
#define NBASE
Definition: numeric.c:95
static void compute_bucket(Numeric operand, Numeric bound1, Numeric bound2, const NumericVar *count_var, NumericVar *result_var)
Definition: numeric.c:1934
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:816
Datum numeric_int2(PG_FUNCTION_ARGS)
Definition: numeric.c:4557
int64 numeric_int8_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4489
#define digitbuf_alloc(ndigits)
Definition: numeric.c:483
struct NumericAggState NumericAggState
#define NUMERIC_SHORT
Definition: numeric.c:167
static void sqrt_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:10282
Datum numeric_trunc(PG_FUNCTION_ARGS)
Definition: numeric.c:1597
static bool set_var_from_str(const char *str, const char *cp, NumericVar *dest, const char **endptr, Node *escontext)
Definition: numeric.c:7016
#define NUMERIC_HDRSZ
Definition: numeric.c:174
static int estimate_ln_dweight(const NumericVar *var)
Definition: numeric.c:10891
Datum numeric_lcm(PG_FUNCTION_ARGS)
Definition: numeric.c:3568
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:8377
Datum numeric_div_trunc(PG_FUNCTION_ARGS)
Definition: numeric.c:3263
static int numeric_sign_internal(Numeric num)
Definition: numeric.c:1478
#define NUMERIC_SPECIAL
Definition: numeric.c:168
Datum int8_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:5600
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:9997
Datum numeric_avg_deserialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5260
static void mul_var_short(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8890
#define NUMERIC_ABBREV_NAN
Definition: numeric.c:411
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:11740
Datum numeric_poly_stddev_pop(PG_FUNCTION_ARGS)
Definition: numeric.c:6471
static bool apply_typmod(NumericVar *var, int32 typmod, Node *escontext)
Definition: numeric.c:7911
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:205
Datum numeric_float8(PG_FUNCTION_ARGS)
Definition: numeric.c:4632
Datum int2_avg_accum_inv(PG_FUNCTION_ARGS)
Definition: numeric.c:6748
static const NumericVar const_zero_point_nine
Definition: numeric.c:443
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:11726
static const NumericVar const_zero
Definition: numeric.c:422
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:637
#define NUMERIC_ABBREV_NINF
Definition: numeric.c:413
static void accum_sum_combine(NumericSumAccum *accum, NumericSumAccum *accum2)
Definition: numeric.c:12474
static void numericvar_deserialize(StringInfo buf, NumericVar *var)
Definition: numeric.c:7744
static const int round_powers[4]
Definition: numeric.c:466
Datum numeric_mod(PG_FUNCTION_ARGS)
Definition: numeric.c:3352
struct NumericSumAccum NumericSumAccum
bool numeric_is_nan(Numeric num)
Definition: numeric.c:851
#define NUMERIC_SIGN_MASK
Definition: numeric.c:164
Datum numeric_lt(PG_FUNCTION_ARGS)
Definition: numeric.c:2479
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:9097
#define NUMERIC_DSCALE_MASK
Definition: numeric.c:233
Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2949
Datum numeric_var_samp(PG_FUNCTION_ARGS)
Definition: numeric.c:6294
Datum numeric_sortsupport(PG_FUNCTION_ARGS)
Definition: numeric.c:2009
Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3027
struct NumericVar NumericVar
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:8108
Datum int8_avg_deserialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5932
static bool numericvar_to_int64(const NumericVar *var, int64 *result)
Definition: numeric.c:8033
Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS)
Definition: numeric.c:4665
Datum numeric_combine(PG_FUNCTION_ARGS)
Definition: numeric.c:5044
Datum int8_avg_combine(PG_FUNCTION_ARGS)
Definition: numeric.c:5823
Datum int8_avg_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:5796
Datum numeric_sign(PG_FUNCTION_ARGS)
Definition: numeric.c:1510
Datum int2_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4548
static int numeric_typmod_precision(int32 typmod)
Definition: numeric.c:927
#define NUMERIC_DSCALE_MAX
Definition: numeric.c:234
Datum int4_sum(PG_FUNCTION_ARGS)
Definition: numeric.c:6561
#define NUMERIC_NAN
Definition: numeric.c:197
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:6957
static int xdigit_value(char dig)
Definition: numeric.c:7219
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7895
static const NumericDigit const_one_point_one_data[2]
Definition: numeric.c:447
#define NUMERIC_SHORT_DSCALE_MASK
Definition: numeric.c:214
#define NumericAbbrevGetDatum(X)
Definition: numeric.c:409
#define NUMERIC_SHORT_DSCALE_SHIFT
Definition: numeric.c:215
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:12180
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:246
Datum numeric_add(PG_FUNCTION_ARGS)
Definition: numeric.c:2852
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:11971
Datum numeric_poly_avg(PG_FUNCTION_ARGS)
Definition: numeric.c:6102
Datum numeric_eq(PG_FUNCTION_ARGS)
Definition: numeric.c:2419
#define DEC_DIGITS
Definition: numeric.c:97
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7472
static void log_var(const NumericVar *base, const NumericVar *num, NumericVar *result)
Definition: numeric.c:11091
Datum numeric_ln(PG_FUNCTION_ARGS)
Definition: numeric.c:3819
#define NUMERIC_SHORT_SIGN_MASK
Definition: numeric.c:213
static bool numericvar_to_uint64(const NumericVar *var, uint64 *result)
Definition: numeric.c:8155
Datum numeric_abs(PG_FUNCTION_ARGS)
Definition: numeric.c:1393
Datum int8_avg(PG_FUNCTION_ARGS)
Definition: numeric.c:6804
#define MUL_GUARD_DIGITS
Definition: numeric.c:98
#define NUMERIC_PINF
Definition: numeric.c:198
Datum numeric_avg_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:5116
Datum numeric_larger(PG_FUNCTION_ARGS)
Definition: numeric.c:3496
static const NumericVar const_one_point_one
Definition: numeric.c:453
Datum int4_avg_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:6689
static void strip_var(NumericVar *var)
Definition: numeric.c:12139
static const NumericVar const_nan
Definition: numeric.c:456
#define NUMERIC_IS_SHORT(n)
Definition: numeric.c:171
Datum numeric_smaller(PG_FUNCTION_ARGS)
Definition: numeric.c:3474
Datum numeric_gt(PG_FUNCTION_ARGS)
Definition: numeric.c:2449
Datum numeric_deserialize(PG_FUNCTION_ARGS)
Definition: numeric.c:5374
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:992
#define NUMERIC_DIGITS(num)
Definition: numeric.c:493
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:4481
Datum numeric_mul(PG_FUNCTION_ARGS)
Definition: numeric.c:3007
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:203
#define init_var(v)
Definition: numeric.c:491
#define makePolyNumAggStateCurrentContext
Definition: numeric.c:5550
static const NumericDigit const_zero_data[1]
Definition: numeric.c:421
static double numericvar_to_double_no_overflow(const NumericVar *var)
Definition: numeric.c:8345
Datum numeric_avg_combine(PG_FUNCTION_ARGS)
Definition: numeric.c:5136
#define NUMERIC_POS
Definition: numeric.c:165
Datum numeric_min_scale(PG_FUNCTION_ARGS)
Definition: numeric.c:4191
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2871
#define HALF_NBASE
Definition: numeric.c:96
#define NUMERIC_WEIGHT_MAX
Definition: numeric.c:257
Datum numeric_float4(PG_FUNCTION_ARGS)
Definition: numeric.c:4726
Datum numeric_support(PG_FUNCTION_ARGS)
Definition: numeric.c:1196
static const NumericVar const_one
Definition: numeric.c:426
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:495
NumericAggState PolyNumAggState
Definition: numeric.c:5548
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:12317
static int cmp_var_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, int var1sign, const NumericDigit *var2digits, int var2ndigits, int var2weight, int var2sign)
Definition: numeric.c:8392
bool numeric_is_inf(Numeric num)
Definition: numeric.c:862
static void ln_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:10973
Datum numeric_pg_lsn(PG_FUNCTION_ARGS)
Definition: numeric.c:4754
static void power_ten_int(int exp, NumericVar *result)
Definition: numeric.c:11518
static void accum_sum_copy(NumericSumAccum *dst, NumericSumAccum *src)
Definition: numeric.c:12457
Datum numeric_floor(PG_FUNCTION_ARGS)
Definition: numeric.c:1675
Datum int2_avg_accum(PG_FUNCTION_ARGS)
Definition: numeric.c:6661
static void div_mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *quot, NumericVar *rem)
Definition: numeric.c:10095
unsigned short uint16
Definition: c.h:505
unsigned int uint32
Definition: c.h:506
#define PG_INT32_MAX
Definition: c.h:589
signed short int16
Definition: c.h:493
signed int int32
Definition: c.h:494
#define INT64_FORMAT
Definition: c.h:548
char * Pointer
Definition: c.h:483
#define VARHDRSZ
Definition: c.h:692
#define Assert(condition)
Definition: c.h:858
double float8
Definition: c.h:630
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:398
#define PG_INT16_MIN
Definition: c.h:585
#define PG_INT64_MAX
Definition: c.h:592
#define PG_INT64_MIN
Definition: c.h:591
#define unlikely(x)
Definition: c.h:311
#define lengthof(array)
Definition: c.h:788
#define PG_UINT64_MAX
Definition: c.h:593
float float4
Definition: c.h:629
#define PG_INT32_MIN
Definition: c.h:588
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:936
#define PG_INT16_MAX
Definition: c.h:586
size_t Size
Definition: c.h:605
#define i64abs(i)
Definition: c.h:1307
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
Datum float8in(PG_FUNCTION_ARGS)
Definition: float.c:365
Datum float4in(PG_FUNCTION_ARGS)
Definition: float.c:165
static float4 get_float4_infinity(void)
Definition: float.h:74
static float4 get_float4_nan(void)
Definition: float.h:111
static float8 get_float8_infinity(void)
Definition: float.h:94
static float8 get_float8_nan(void)
Definition: float.h:123
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_DETOAST_DATUM_PACKED(datum)
Definition: fmgr.h:248
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
#define PG_RETURN_INT16(x)
Definition: fmgr.h:356
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:366
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h: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
#define newval
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
const char * str
static const FormData_pg_attribute a1
Definition: heap.c:142
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:66
double estimateHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:186
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
Definition: hyperloglog.c:167
long val
Definition: informix.c:670
int maxdigits
Definition: informix.c:671
static struct @155 value
char sign
Definition: informix.c:674
int digits
Definition: informix.c:672
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:219
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:188
static bool pg_add_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:380
static bool pg_mul_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:414
#define Min(x, y)
Definition: numeric.c:14
#define Max(x, y)
Definition: numeric.c:13
int y
Definition: isn.c:72
int b
Definition: isn.c:70
int x
Definition: isn.c:71
int j
Definition: isn.c:74
int i
Definition: isn.c:73
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void * palloc(Size size)
Definition: mcxt.c:1317
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4511
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:684
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define NUMERIC_MIN_SCALE
Definition: numeric.h:34
#define NUMERIC_MAX_PRECISION
Definition: numeric.h:32
#define NUMERIC_MAX_RESULT_SCALE
Definition: numeric.h:43
#define NUMERIC_MAX_DISPLAY_SCALE
Definition: numeric.h:40
#define NUMERIC_MIN_SIG_DIGITS
Definition: numeric.h:50
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:61
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:78
struct NumericData * Numeric
Definition: numeric.h:54
#define NUMERIC_MAX_SCALE
Definition: numeric.h:35
#define NUMERIC_MIN_DISPLAY_SCALE
Definition: numeric.h:41
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:80
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73
void * arg
const void size_t len
while(p+4<=pend)
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:34
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
Definition: pg_prng.c:144
static rewind_source * source
Definition: pg_rewind.c:89
static char * buf
Definition: pg_test_fsync.c:73
static int scale
Definition: pgbench.c:181
#define snprintf
Definition: port.h:238
#define printf(...)
Definition: port.h:244
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:222
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
#define Int64GetDatumFast(X)
Definition: postgres.h:554
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
uintptr_t Datum
Definition: postgres.h:64
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:436
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
unsigned int Oid
Definition: postgres_ext.h:31
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:635
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:152
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136
MemoryContextSwitchTo(old_ctx)
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static void error(void)
Definition: sql-dyntest.c:147
StringInfoData * StringInfo
Definition: stringinfo.h:54
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
Definition: stringinfo.h:130
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
List * args
Definition: primnodes.h:768
Definition: nodes.h:129
int64 NaNcount
Definition: numeric.c:4808
int64 nInfcount
Definition: numeric.c:4810
NumericSumAccum sumX
Definition: numeric.c:4803
int64 maxScaleCount
Definition: numeric.c:4806
NumericSumAccum sumX2
Definition: numeric.c:4804
int64 pInfcount
Definition: numeric.c:4809
MemoryContext agg_context
Definition: numeric.c:4801
union NumericChoice choice
Definition: numeric.c:156
int32 vl_len_
Definition: numeric.c:155
int16 n_weight
Definition: numeric.c:142
uint16 n_sign_dscale
Definition: numeric.c:141
NumericDigit n_data[FLEXIBLE_ARRAY_MEMBER]
Definition: numeric.c:143
NumericDigit n_data[FLEXIBLE_ARRAY_MEMBER]
Definition: numeric.c:136
uint16 n_header
Definition: numeric.c:135
hyperLogLogState abbr_card
Definition: numeric.c:343
bool have_carry_space
Definition: numeric.c:383
int32 * pos_digits
Definition: numeric.c:384
int num_uncarried
Definition: numeric.c:382
int32 * neg_digits
Definition: numeric.c:385
int ndigits
Definition: numeric.c:312
NumericDigit * digits
Definition: numeric.c:317
int dscale
Definition: numeric.c:315
int sign
Definition: numeric.c:314
NumericDigit * buf
Definition: numeric.c:316
int weight
Definition: numeric.c:313
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:172
void * ssup_extra
Definition: sortsupport.h:87
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:182
Definition: regguts.h:323
Definition: c.h:687
bool trace_sort
Definition: tuplesort.c:125
struct NumericLong n_long
Definition: numeric.c:149
uint16 n_header
Definition: numeric.c:148
struct NumericShort n_short
Definition: numeric.c:150
#define VARHDRSZ_SHORT
Definition: varatt.h:255
#define VARSIZE_SHORT(PTR)
Definition: varatt.h:281
#define VARATT_IS_SHORT(PTR)
Definition: varatt.h:302
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARSIZE(PTR)
Definition: varatt.h:279
#define VARATT_SHORT_MAX
Definition: varatt.h:257
#define VARDATA_SHORT(PTR)
Definition: varatt.h:282
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
#define stat
Definition: win32_port.h:284
uint64 XLogRecPtr
Definition: xlogdefs.h:21