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