PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
numeric.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include "access/hash.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/int8.h"
#include "utils/numeric.h"
#include "utils/sortsupport.h"
Include dependency graph for numeric.c:

Go to the source code of this file.

Data Structures

struct  NumericShort
 
struct  NumericLong
 
union  NumericChoice
 
struct  NumericData
 
struct  NumericVar
 
struct  generate_series_numeric_fctx
 
struct  NumericSortSupport
 
struct  NumericSumAccum
 
struct  NumericAggState
 
struct  Int8TransTypeData
 

Macros

#define NBASE   10000
 
#define HALF_NBASE   5000
 
#define DEC_DIGITS   4 /* decimal digits per NBASE digit */
 
#define MUL_GUARD_DIGITS   2 /* these are measured in NBASE digits */
 
#define DIV_GUARD_DIGITS   4
 
#define NUMERIC_SIGN_MASK   0xC000
 
#define NUMERIC_POS   0x0000
 
#define NUMERIC_NEG   0x4000
 
#define NUMERIC_SHORT   0x8000
 
#define NUMERIC_NAN   0xC000
 
#define NUMERIC_FLAGBITS(n)   ((n)->choice.n_header & NUMERIC_SIGN_MASK)
 
#define NUMERIC_IS_NAN(n)   (NUMERIC_FLAGBITS(n) == NUMERIC_NAN)
 
#define NUMERIC_IS_SHORT(n)   (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
 
#define NUMERIC_HDRSZ   (VARHDRSZ + sizeof(uint16) + sizeof(int16))
 
#define NUMERIC_HDRSZ_SHORT   (VARHDRSZ + sizeof(uint16))
 
#define NUMERIC_HEADER_IS_SHORT(n)   (((n)->choice.n_header & 0x8000) != 0)
 
#define NUMERIC_HEADER_SIZE(n)
 
#define NUMERIC_SHORT_SIGN_MASK   0x2000
 
#define NUMERIC_SHORT_DSCALE_MASK   0x1F80
 
#define NUMERIC_SHORT_DSCALE_SHIFT   7
 
#define NUMERIC_SHORT_DSCALE_MAX   (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)
 
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK   0x0040
 
#define NUMERIC_SHORT_WEIGHT_MASK   0x003F
 
#define NUMERIC_SHORT_WEIGHT_MAX   NUMERIC_SHORT_WEIGHT_MASK
 
#define NUMERIC_SHORT_WEIGHT_MIN   (-(NUMERIC_SHORT_WEIGHT_MASK+1))
 
#define NUMERIC_DSCALE_MASK   0x3FFF
 
#define NUMERIC_SIGN(n)
 
#define NUMERIC_DSCALE(n)
 
#define NUMERIC_WEIGHT(n)
 
#define NUMERIC_ABBREV_BITS   (SIZEOF_DATUM * BITS_PER_BYTE)
 
#define NumericAbbrevGetDatum(X)   ((Datum) SET_4_BYTES(X))
 
#define DatumGetNumericAbbrev(X)   ((int32) GET_4_BYTES(X))
 
#define NUMERIC_ABBREV_NAN   NumericAbbrevGetDatum(PG_INT32_MIN)
 
#define dump_numeric(s, n)
 
#define dump_var(s, v)
 
#define digitbuf_alloc(ndigits)   ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
 
#define digitbuf_free(buf)
 
#define init_var(v)   MemSetAligned(v, 0, sizeof(NumericVar))
 
#define NUMERIC_DIGITS(num)
 
#define NUMERIC_NDIGITS(num)   ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
 
#define NUMERIC_CAN_BE_SHORT(scale, weight)
 
#define makePolyNumAggState   makeNumericAggState
 
#define makePolyNumAggStateCurrentContext   makeNumericAggStateCurrentContext
 

Typedefs

typedef int16 NumericDigit
 
typedef struct NumericVar NumericVar
 
typedef struct NumericSumAccum NumericSumAccum
 
typedef struct NumericAggState NumericAggState
 
typedef NumericAggState PolyNumAggState
 
typedef struct Int8TransTypeData Int8TransTypeData
 

Functions

static void alloc_var (NumericVar *var, int ndigits)
 
static void free_var (NumericVar *var)
 
static void zero_var (NumericVar *var)
 
static const char * set_var_from_str (const char *str, const char *cp, NumericVar *dest)
 
static void set_var_from_num (Numeric value, NumericVar *dest)
 
static void init_var_from_num (Numeric num, NumericVar *dest)
 
static void set_var_from_var (NumericVar *value, NumericVar *dest)
 
static char * get_str_from_var (NumericVar *var)
 
static char * get_str_from_var_sci (NumericVar *var, int rscale)
 
static Numeric make_result (NumericVar *var)
 
static void apply_typmod (NumericVar *var, int32 typmod)
 
static int32 numericvar_to_int32 (NumericVar *var)
 
static bool numericvar_to_int64 (NumericVar *var, int64 *result)
 
static void int64_to_numericvar (int64 val, NumericVar *var)
 
static double numeric_to_double_no_overflow (Numeric num)
 
static double numericvar_to_double_no_overflow (NumericVar *var)
 
static Datum numeric_abbrev_convert (Datum original_datum, SortSupport ssup)
 
static bool numeric_abbrev_abort (int memtupcount, SortSupport ssup)
 
static int numeric_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static int numeric_cmp_abbrev (Datum x, Datum y, SortSupport ssup)
 
static Datum numeric_abbrev_convert_var (NumericVar *var, NumericSortSupport *nss)
 
static int cmp_numerics (Numeric num1, Numeric num2)
 
static int cmp_var (NumericVar *var1, NumericVar *var2)
 
static int cmp_var_common (const NumericDigit *var1digits, int var1ndigits, int var1weight, int var1sign, const NumericDigit *var2digits, int var2ndigits, int var2weight, int var2sign)
 
static void add_var (NumericVar *var1, NumericVar *var2, NumericVar *result)
 
static void sub_var (NumericVar *var1, NumericVar *var2, NumericVar *result)
 
static void mul_var (NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale)
 
static void div_var (NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale, bool round)
 
static void div_var_fast (NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale, bool round)
 
static int select_div_scale (NumericVar *var1, NumericVar *var2)
 
static void mod_var (NumericVar *var1, NumericVar *var2, NumericVar *result)
 
static void ceil_var (NumericVar *var, NumericVar *result)
 
static void floor_var (NumericVar *var, NumericVar *result)
 
static void sqrt_var (NumericVar *arg, NumericVar *result, int rscale)
 
static void exp_var (NumericVar *arg, NumericVar *result, int rscale)
 
static int estimate_ln_dweight (NumericVar *var)
 
static void ln_var (NumericVar *arg, NumericVar *result, int rscale)
 
static void log_var (NumericVar *base, NumericVar *num, NumericVar *result)
 
static void power_var (NumericVar *base, NumericVar *exp, NumericVar *result)
 
static void power_var_int (NumericVar *base, int exp, NumericVar *result, int rscale)
 
static int cmp_abs (NumericVar *var1, NumericVar *var2)
 
static int cmp_abs_common (const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
 
static void add_abs (NumericVar *var1, NumericVar *var2, NumericVar *result)
 
static void sub_abs (NumericVar *var1, NumericVar *var2, NumericVar *result)
 
static void round_var (NumericVar *var, int rscale)
 
static void trunc_var (NumericVar *var, int rscale)
 
static void strip_var (NumericVar *var)
 
static void compute_bucket (Numeric operand, Numeric bound1, Numeric bound2, NumericVar *count_var, NumericVar *result_var)
 
static void accum_sum_add (NumericSumAccum *accum, NumericVar *var1)
 
static void accum_sum_rescale (NumericSumAccum *accum, NumericVar *val)
 
static void accum_sum_carry (NumericSumAccum *accum)
 
static void accum_sum_reset (NumericSumAccum *accum)
 
static void accum_sum_final (NumericSumAccum *accum, NumericVar *result)
 
static void accum_sum_copy (NumericSumAccum *dst, NumericSumAccum *src)
 
static void accum_sum_combine (NumericSumAccum *accum, NumericSumAccum *accum2)
 
Datum numeric_in (PG_FUNCTION_ARGS)
 
Datum numeric_out (PG_FUNCTION_ARGS)
 
bool numeric_is_nan (Numeric num)
 
int32 numeric_maximum_size (int32 typmod)
 
char * numeric_out_sci (Numeric num, int scale)
 
char * numeric_normalize (Numeric num)
 
Datum numeric_recv (PG_FUNCTION_ARGS)
 
Datum numeric_send (PG_FUNCTION_ARGS)
 
Datum numeric_transform (PG_FUNCTION_ARGS)
 
Datum numeric (PG_FUNCTION_ARGS)
 
Datum numerictypmodin (PG_FUNCTION_ARGS)
 
Datum numerictypmodout (PG_FUNCTION_ARGS)
 
Datum numeric_abs (PG_FUNCTION_ARGS)
 
Datum numeric_uminus (PG_FUNCTION_ARGS)
 
Datum numeric_uplus (PG_FUNCTION_ARGS)
 
Datum numeric_sign (PG_FUNCTION_ARGS)
 
Datum numeric_round (PG_FUNCTION_ARGS)
 
Datum numeric_trunc (PG_FUNCTION_ARGS)
 
Datum numeric_ceil (PG_FUNCTION_ARGS)
 
Datum numeric_floor (PG_FUNCTION_ARGS)
 
Datum generate_series_numeric (PG_FUNCTION_ARGS)
 
Datum generate_series_step_numeric (PG_FUNCTION_ARGS)
 
Datum width_bucket_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_sortsupport (PG_FUNCTION_ARGS)
 
Datum numeric_cmp (PG_FUNCTION_ARGS)
 
Datum numeric_eq (PG_FUNCTION_ARGS)
 
Datum numeric_ne (PG_FUNCTION_ARGS)
 
Datum numeric_gt (PG_FUNCTION_ARGS)
 
Datum numeric_ge (PG_FUNCTION_ARGS)
 
Datum numeric_lt (PG_FUNCTION_ARGS)
 
Datum numeric_le (PG_FUNCTION_ARGS)
 
Datum hash_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_add (PG_FUNCTION_ARGS)
 
Datum numeric_sub (PG_FUNCTION_ARGS)
 
Datum numeric_mul (PG_FUNCTION_ARGS)
 
Datum numeric_div (PG_FUNCTION_ARGS)
 
Datum numeric_div_trunc (PG_FUNCTION_ARGS)
 
Datum numeric_mod (PG_FUNCTION_ARGS)
 
Datum numeric_inc (PG_FUNCTION_ARGS)
 
Datum numeric_smaller (PG_FUNCTION_ARGS)
 
Datum numeric_larger (PG_FUNCTION_ARGS)
 
Datum numeric_fac (PG_FUNCTION_ARGS)
 
Datum numeric_sqrt (PG_FUNCTION_ARGS)
 
Datum numeric_exp (PG_FUNCTION_ARGS)
 
Datum numeric_ln (PG_FUNCTION_ARGS)
 
Datum numeric_log (PG_FUNCTION_ARGS)
 
Datum numeric_power (PG_FUNCTION_ARGS)
 
Datum numeric_scale (PG_FUNCTION_ARGS)
 
Datum int4_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_int4 (PG_FUNCTION_ARGS)
 
Datum int8_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_int8 (PG_FUNCTION_ARGS)
 
Datum int2_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_int2 (PG_FUNCTION_ARGS)
 
Datum float8_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_float8 (PG_FUNCTION_ARGS)
 
Datum numeric_float8_no_overflow (PG_FUNCTION_ARGS)
 
Datum float4_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_float4 (PG_FUNCTION_ARGS)
 
static NumericAggStatemakeNumericAggState (FunctionCallInfo fcinfo, bool calcSumX2)
 
static NumericAggStatemakeNumericAggStateCurrentContext (bool calcSumX2)
 
static void do_numeric_accum (NumericAggState *state, Numeric newval)
 
static bool do_numeric_discard (NumericAggState *state, Numeric newval)
 
Datum numeric_accum (PG_FUNCTION_ARGS)
 
Datum numeric_combine (PG_FUNCTION_ARGS)
 
Datum numeric_avg_accum (PG_FUNCTION_ARGS)
 
Datum numeric_avg_combine (PG_FUNCTION_ARGS)
 
Datum numeric_avg_serialize (PG_FUNCTION_ARGS)
 
Datum numeric_avg_deserialize (PG_FUNCTION_ARGS)
 
Datum numeric_serialize (PG_FUNCTION_ARGS)
 
Datum numeric_deserialize (PG_FUNCTION_ARGS)
 
Datum numeric_accum_inv (PG_FUNCTION_ARGS)
 
Datum int2_accum (PG_FUNCTION_ARGS)
 
Datum int4_accum (PG_FUNCTION_ARGS)
 
Datum int8_accum (PG_FUNCTION_ARGS)
 
Datum numeric_poly_combine (PG_FUNCTION_ARGS)
 
Datum numeric_poly_serialize (PG_FUNCTION_ARGS)
 
Datum numeric_poly_deserialize (PG_FUNCTION_ARGS)
 
Datum int8_avg_accum (PG_FUNCTION_ARGS)
 
Datum int8_avg_combine (PG_FUNCTION_ARGS)
 
Datum int8_avg_serialize (PG_FUNCTION_ARGS)
 
Datum int8_avg_deserialize (PG_FUNCTION_ARGS)
 
Datum int2_accum_inv (PG_FUNCTION_ARGS)
 
Datum int4_accum_inv (PG_FUNCTION_ARGS)
 
Datum int8_accum_inv (PG_FUNCTION_ARGS)
 
Datum int8_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum numeric_poly_sum (PG_FUNCTION_ARGS)
 
Datum numeric_poly_avg (PG_FUNCTION_ARGS)
 
Datum numeric_avg (PG_FUNCTION_ARGS)
 
Datum numeric_sum (PG_FUNCTION_ARGS)
 
static Numeric numeric_stddev_internal (NumericAggState *state, bool variance, bool sample, bool *is_null)
 
Datum numeric_var_samp (PG_FUNCTION_ARGS)
 
Datum numeric_stddev_samp (PG_FUNCTION_ARGS)
 
Datum numeric_var_pop (PG_FUNCTION_ARGS)
 
Datum numeric_stddev_pop (PG_FUNCTION_ARGS)
 
Datum numeric_poly_var_samp (PG_FUNCTION_ARGS)
 
Datum numeric_poly_stddev_samp (PG_FUNCTION_ARGS)
 
Datum numeric_poly_var_pop (PG_FUNCTION_ARGS)
 
Datum numeric_poly_stddev_pop (PG_FUNCTION_ARGS)
 
Datum int2_sum (PG_FUNCTION_ARGS)
 
Datum int4_sum (PG_FUNCTION_ARGS)
 
Datum int8_sum (PG_FUNCTION_ARGS)
 
Datum int2_avg_accum (PG_FUNCTION_ARGS)
 
Datum int4_avg_accum (PG_FUNCTION_ARGS)
 
Datum int4_avg_combine (PG_FUNCTION_ARGS)
 
Datum int2_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum int4_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum int8_avg (PG_FUNCTION_ARGS)
 
Datum int2int4_sum (PG_FUNCTION_ARGS)
 

Variables

static NumericDigit const_zero_data [1] = {0}
 
static NumericVar const_zero
 
static NumericDigit const_one_data [1] = {1}
 
static NumericVar const_one
 
static NumericDigit const_two_data [1] = {2}
 
static NumericVar const_two
 
static NumericDigit const_ten_data [1] = {10}
 
static NumericVar const_ten
 
static NumericDigit const_zero_point_five_data [1] = {5000}
 
static NumericVar const_zero_point_five
 
static NumericDigit const_zero_point_nine_data [1] = {9000}
 
static NumericVar const_zero_point_nine
 
static NumericDigit const_one_point_one_data [2] = {1, 1000}
 
static NumericVar const_one_point_one
 
static NumericVar const_nan
 
static const int round_powers [4] = {0, 1000, 100, 10}
 

Macro Definition Documentation

#define DatumGetNumericAbbrev (   X)    ((int32) GET_4_BYTES(X))

Definition at line 361 of file numeric.c.

Referenced by numeric_cmp_abbrev().

#define digitbuf_alloc (   ndigits)    ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))

Definition at line 443 of file numeric.c.

Referenced by accum_sum_final(), add_abs(), alloc_var(), set_var_from_var(), and sub_abs().

#define digitbuf_free (   buf)
Value:
do { \
if ((buf) != NULL) \
} while (0)
void pfree(void *pointer)
Definition: mcxt.c:950
static char * buf
Definition: pg_test_fsync.c:66
#define NULL
Definition: c.h:229

Definition at line 445 of file numeric.c.

Referenced by add_abs(), alloc_var(), free_var(), set_var_from_var(), sub_abs(), and zero_var().

#define DIV_GUARD_DIGITS   4

Definition at line 97 of file numeric.c.

Referenced by div_var_fast().

#define dump_numeric (   s,
 
)

Definition at line 439 of file numeric.c.

Referenced by make_result().

#define dump_var (   s,
 
)

Definition at line 440 of file numeric.c.

#define HALF_NBASE   5000

Definition at line 94 of file numeric.c.

Referenced by div_var(), and round_var().

#define makePolyNumAggState   makeNumericAggState
#define makePolyNumAggStateCurrentContext   makeNumericAggStateCurrentContext

Definition at line 3935 of file numeric.c.

Referenced by int8_avg_deserialize(), and numeric_poly_deserialize().

#define MUL_GUARD_DIGITS   2 /* these are measured in NBASE digits */

Definition at line 96 of file numeric.c.

Referenced by mul_var().

#define NUMERIC_ABBREV_BITS   (SIZEOF_DATUM * BITS_PER_BYTE)

Definition at line 354 of file numeric.c.

#define NUMERIC_ABBREV_NAN   NumericAbbrevGetDatum(PG_INT32_MIN)

Definition at line 362 of file numeric.c.

Referenced by numeric_abbrev_convert().

#define NUMERIC_CAN_BE_SHORT (   scale,
  weight 
)
Value:
(weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
#define NUMERIC_SHORT_WEIGHT_MAX
Definition: numeric.c:197
int scale
Definition: pgbench.c:106
#define NUMERIC_SHORT_WEIGHT_MIN
Definition: numeric.c:198
#define NUMERIC_SHORT_DSCALE_MAX
Definition: numeric.c:193

Definition at line 457 of file numeric.c.

Referenced by make_result(), and numeric().

#define NUMERIC_DIGITS (   num)
Value:
(num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:181

Definition at line 453 of file numeric.c.

Referenced by cmp_numerics(), hash_numeric(), init_var_from_num(), make_result(), and set_var_from_num().

#define NUMERIC_DSCALE (   n)
Value:
((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
: ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:181
#define NUMERIC_DSCALE_MASK
Definition: numeric.c:204
#define NUMERIC_SHORT_DSCALE_MASK
Definition: numeric.c:191
#define NUMERIC_SHORT_DSCALE_SHIFT
Definition: numeric.c:192

Definition at line 210 of file numeric.c.

Referenced by init_var_from_num(), make_result(), numeric(), numeric_abs(), numeric_scale(), numeric_uminus(), and set_var_from_num().

#define NUMERIC_DSCALE_MASK   0x3FFF

Definition at line 204 of file numeric.c.

Referenced by make_result(), numeric(), and numeric_recv().

#define NUMERIC_FLAGBITS (   n)    ((n)->choice.n_header & NUMERIC_SIGN_MASK)

Definition at line 169 of file numeric.c.

#define NUMERIC_HDRSZ   (VARHDRSZ + sizeof(uint16) + sizeof(int16))

Definition at line 173 of file numeric.c.

Referenced by make_result(), and numeric_maximum_size().

#define NUMERIC_HDRSZ_SHORT   (VARHDRSZ + sizeof(uint16))

Definition at line 174 of file numeric.c.

Referenced by make_result().

#define NUMERIC_HEADER_IS_SHORT (   n)    (((n)->choice.n_header & 0x8000) != 0)

Definition at line 181 of file numeric.c.

#define NUMERIC_HEADER_SIZE (   n)
Value:
(VARHDRSZ + sizeof(uint16) + \
(NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
signed short int16
Definition: c.h:255
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:181
#define VARHDRSZ
Definition: c.h:445
unsigned short uint16
Definition: c.h:267

Definition at line 182 of file numeric.c.

#define NUMERIC_IS_SHORT (   n)    (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)

Definition at line 171 of file numeric.c.

Referenced by numeric(), numeric_abs(), and numeric_uminus().

#define NUMERIC_NAN   0xC000

Definition at line 167 of file numeric.c.

Referenced by free_var(), get_str_from_var(), main(), make_result(), numeric_recv(), and set_var_from_str().

#define NUMERIC_NDIGITS (   num)    ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
#define NUMERIC_SHORT   0x8000

Definition at line 166 of file numeric.c.

Referenced by make_result().

#define NUMERIC_SHORT_DSCALE_MASK   0x1F80

Definition at line 191 of file numeric.c.

Referenced by numeric().

#define NUMERIC_SHORT_DSCALE_MAX   (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)

Definition at line 193 of file numeric.c.

#define NUMERIC_SHORT_DSCALE_SHIFT   7

Definition at line 192 of file numeric.c.

Referenced by make_result(), and numeric().

#define NUMERIC_SHORT_SIGN_MASK   0x2000

Definition at line 190 of file numeric.c.

Referenced by make_result(), numeric_abs(), and numeric_uminus().

#define NUMERIC_SHORT_WEIGHT_MASK   0x003F

Definition at line 196 of file numeric.c.

Referenced by make_result().

#define NUMERIC_SHORT_WEIGHT_MAX   NUMERIC_SHORT_WEIGHT_MASK

Definition at line 197 of file numeric.c.

#define NUMERIC_SHORT_WEIGHT_MIN   (-(NUMERIC_SHORT_WEIGHT_MASK+1))

Definition at line 198 of file numeric.c.

#define NUMERIC_SHORT_WEIGHT_SIGN_MASK   0x0040

Definition at line 195 of file numeric.c.

Referenced by make_result().

#define NUMERIC_SIGN (   n)
Value:
(((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
#define NUMERIC_POS
Definition: numeric.c:164
#define NUMERIC_FLAGBITS(n)
Definition: numeric.c:169
#define NUMERIC_NEG
Definition: numeric.c:165
#define NUMERIC_SHORT_SIGN_MASK
Definition: numeric.c:190
#define NUMERIC_IS_SHORT(n)
Definition: numeric.c:171

Definition at line 206 of file numeric.c.

Referenced by cmp_numerics(), init_var_from_num(), numeric(), numeric_sign(), numeric_uminus(), and set_var_from_num().

#define NUMERIC_SIGN_MASK   0xC000

Definition at line 163 of file numeric.c.

#define NUMERIC_WEIGHT (   n)
Value:
(((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
| ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
: ((n)->choice.n_long.n_weight))
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:181
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK
Definition: numeric.c:195
#define NUMERIC_SHORT_WEIGHT_MASK
Definition: numeric.c:196

Definition at line 214 of file numeric.c.

Referenced by cmp_numerics(), hash_numeric(), init_var_from_num(), make_result(), numeric(), and set_var_from_num().

#define NumericAbbrevGetDatum (   X)    ((Datum) SET_4_BYTES(X))

Definition at line 360 of file numeric.c.

Typedef Documentation

Definition at line 99 of file numeric.c.

Definition at line 3933 of file numeric.c.

Function Documentation

static void accum_sum_add ( NumericSumAccum accum,
NumericVar var1 
)
static

Definition at line 8818 of file numeric.c.

References accum_sum_carry(), accum_sum_rescale(), NumericVar::digits, i, NBASE, NumericVar::ndigits, NumericSumAccum::neg_digits, NumericSumAccum::num_uncarried, NUMERIC_POS, NumericSumAccum::pos_digits, NumericVar::sign, NumericVar::weight, and NumericSumAccum::weight.

Referenced by accum_sum_combine(), do_numeric_accum(), do_numeric_discard(), int8_avg_deserialize(), numeric_avg_deserialize(), numeric_deserialize(), and numeric_poly_deserialize().

8819 {
8820  int32 *accum_digits;
8821  int i,
8822  val_i;
8823  int val_ndigits;
8824  NumericDigit *val_digits;
8825 
8826  /*
8827  * If we have accumulated too many values since the last carry
8828  * propagation, do it now, to avoid overflowing. (We could allow more
8829  * than NBASE - 1, if we reserved two extra digits, rather than one, for
8830  * carry propagation. But even with NBASE - 1, this needs to be done so
8831  * seldom, that the performance difference is negligible.)
8832  */
8833  if (accum->num_uncarried == NBASE - 1)
8834  accum_sum_carry(accum);
8835 
8836  /*
8837  * Adjust the weight or scale of the old value, so that it can accommodate
8838  * the new value.
8839  */
8840  accum_sum_rescale(accum, val);
8841 
8842  /* */
8843  if (val->sign == NUMERIC_POS)
8844  accum_digits = accum->pos_digits;
8845  else
8846  accum_digits = accum->neg_digits;
8847 
8848  /* copy these values into local vars for speed in loop */
8849  val_ndigits = val->ndigits;
8850  val_digits = val->digits;
8851 
8852  i = accum->weight - val->weight;
8853  for (val_i = 0; val_i < val_ndigits; val_i++)
8854  {
8855  accum_digits[i] += (int32) val_digits[val_i];
8856  i++;
8857  }
8858 
8859  accum->num_uncarried++;
8860 }
static void accum_sum_rescale(NumericSumAccum *accum, NumericVar *val)
Definition: numeric.c:8939
#define NUMERIC_POS
Definition: numeric.c:164
int32 * neg_digits
Definition: numeric.c:344
int num_uncarried
Definition: numeric.c:341
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:8866
signed int int32
Definition: c.h:256
int16 NumericDigit
Definition: numeric.c:99
#define NBASE
Definition: numeric.c:93
int i
int32 * pos_digits
Definition: numeric.c:343
long val
Definition: informix.c:689
static void accum_sum_carry ( NumericSumAccum accum)
static

Definition at line 8866 of file numeric.c.

References Assert, NumericSumAccum::have_carry_space, i, NBASE, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, NumericSumAccum::num_uncarried, and NumericSumAccum::pos_digits.

Referenced by accum_sum_add(), and accum_sum_final().

8867 {
8868  int i;
8869  int ndigits;
8870  int32 *dig;
8871  int32 carry;
8872  int32 newdig = 0;
8873 
8874  /*
8875  * If no new values have been added since last carry propagation, nothing
8876  * to do.
8877  */
8878  if (accum->num_uncarried == 0)
8879  return;
8880 
8881  /*
8882  * We maintain that the weight of the accumulator is always one larger
8883  * than needed to hold the current value, before carrying, to make sure
8884  * there is enough space for the possible extra digit when carry is
8885  * propagated. We cannot expand the buffer here, unless we require
8886  * callers of accum_sum_final() to switch to the right memory context.
8887  */
8888  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
8889 
8890  ndigits = accum->ndigits;
8891 
8892  /* Propagate carry in the positive sum */
8893  dig = accum->pos_digits;
8894  carry = 0;
8895  for (i = ndigits - 1; i >= 0; i--)
8896  {
8897  newdig = dig[i] + carry;
8898  if (newdig >= NBASE)
8899  {
8900  carry = newdig / NBASE;
8901  newdig -= carry * NBASE;
8902  }
8903  else
8904  carry = 0;
8905  dig[i] = newdig;
8906  }
8907  /* Did we use up the digit reserved for carry propagation? */
8908  if (newdig > 0)
8909  accum->have_carry_space = false;
8910 
8911  /* And the same for the negative sum */
8912  dig = accum->neg_digits;
8913  carry = 0;
8914  for (i = ndigits - 1; i >= 0; i--)
8915  {
8916  newdig = dig[i] + carry;
8917  if (newdig >= NBASE)
8918  {
8919  carry = newdig / NBASE;
8920  newdig -= carry * NBASE;
8921  }
8922  else
8923  carry = 0;
8924  dig[i] = newdig;
8925  }
8926  if (newdig > 0)
8927  accum->have_carry_space = false;
8928 
8929  accum->num_uncarried = 0;
8930 }
int32 * neg_digits
Definition: numeric.c:344
int num_uncarried
Definition: numeric.c:341
signed int int32
Definition: c.h:256
bool have_carry_space
Definition: numeric.c:342
#define NBASE
Definition: numeric.c:93
#define Assert(condition)
Definition: c.h:675
int i
int32 * pos_digits
Definition: numeric.c:343
static void accum_sum_combine ( NumericSumAccum accum,
NumericSumAccum accum2 
)
static

Definition at line 9096 of file numeric.c.

References accum_sum_add(), accum_sum_final(), free_var(), and init_var.

Referenced by int8_avg_combine(), numeric_avg_combine(), numeric_combine(), and numeric_poly_combine().

9097 {
9098  NumericVar tmp_var;
9099 
9100  init_var(&tmp_var);
9101 
9102  accum_sum_final(accum2, &tmp_var);
9103  accum_sum_add(accum, &tmp_var);
9104 
9105  free_var(&tmp_var);
9106 }
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:9028
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static void accum_sum_add(NumericSumAccum *accum, NumericVar *var1)
Definition: numeric.c:8818
#define init_var(v)
Definition: numeric.c:451
static void accum_sum_copy ( NumericSumAccum dst,
NumericSumAccum src 
)
static

Definition at line 9079 of file numeric.c.

References NumericSumAccum::dscale, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, NumericSumAccum::num_uncarried, palloc(), NumericSumAccum::pos_digits, and NumericSumAccum::weight.

Referenced by int8_avg_combine(), numeric_avg_combine(), numeric_combine(), and numeric_poly_combine().

9080 {
9081  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
9082  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
9083 
9084  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
9085  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
9086  dst->num_uncarried = src->num_uncarried;
9087  dst->ndigits = src->ndigits;
9088  dst->weight = src->weight;
9089  dst->dscale = src->dscale;
9090 }
int32 * neg_digits
Definition: numeric.c:344
int num_uncarried
Definition: numeric.c:341
signed int int32
Definition: c.h:256
void * palloc(Size size)
Definition: mcxt.c:849
int32 * pos_digits
Definition: numeric.c:343
static void accum_sum_final ( NumericSumAccum accum,
NumericVar result 
)
static

Definition at line 9028 of file numeric.c.

References accum_sum_carry(), add_var(), Assert, NumericVar::buf, digitbuf_alloc, NumericVar::digits, NumericVar::dscale, NumericSumAccum::dscale, i, init_var, NBASE, NumericVar::ndigits, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, NUMERIC_NEG, NUMERIC_POS, NumericSumAccum::pos_digits, set_var_from_var(), NumericVar::sign, strip_var(), NumericVar::weight, and NumericSumAccum::weight.

Referenced by accum_sum_combine(), int8_avg_serialize(), numeric_avg(), numeric_avg_serialize(), numeric_poly_serialize(), numeric_serialize(), numeric_stddev_internal(), and numeric_sum().

9029 {
9030  int i;
9031  NumericVar pos_var;
9032  NumericVar neg_var;
9033 
9034  if (accum->ndigits == 0)
9035  {
9036  set_var_from_var(&const_zero, result);
9037  return;
9038  }
9039 
9040  /* Perform final carry */
9041  accum_sum_carry(accum);
9042 
9043  /* Create NumericVars representing the positive and negative sums */
9044  init_var(&pos_var);
9045  init_var(&neg_var);
9046 
9047  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
9048  pos_var.weight = neg_var.weight = accum->weight;
9049  pos_var.dscale = neg_var.dscale = accum->dscale;
9050  pos_var.sign = NUMERIC_POS;
9051  neg_var.sign = NUMERIC_NEG;
9052 
9053  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
9054  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
9055 
9056  for (i = 0; i < accum->ndigits; i++)
9057  {
9058  Assert(accum->pos_digits[i] < NBASE);
9059  pos_var.digits[i] = (int16) accum->pos_digits[i];
9060 
9061  Assert(accum->neg_digits[i] < NBASE);
9062  neg_var.digits[i] = (int16) accum->neg_digits[i];
9063  }
9064 
9065  /* And add them together */
9066  add_var(&pos_var, &neg_var, result);
9067 
9068  /* Remove leading/trailing zeroes */
9069  strip_var(result);
9070 }
signed short int16
Definition: c.h:255
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
static void strip_var(NumericVar *var)
Definition: numeric.c:8761
int32 * neg_digits
Definition: numeric.c:344
#define digitbuf_alloc(ndigits)
Definition: numeric.c:443
static void add_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6439
int ndigits
Definition: numeric.c:271
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:8866
static void set_var_from_var(NumericVar *value, NumericVar *dest)
Definition: numeric.c:5658
int dscale
Definition: numeric.c:274
static NumericVar const_zero
Definition: numeric.c:371
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
NumericDigit * buf
Definition: numeric.c:275
#define NBASE
Definition: numeric.c:93
#define Assert(condition)
Definition: c.h:675
NumericDigit * digits
Definition: numeric.c:276
int i
int32 * pos_digits
Definition: numeric.c:343
#define init_var(v)
Definition: numeric.c:451
static void accum_sum_rescale ( NumericSumAccum accum,
NumericVar val 
)
static

Definition at line 8939 of file numeric.c.

References Assert, NumericVar::dscale, NumericSumAccum::dscale, NumericSumAccum::have_carry_space, NumericVar::ndigits, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, palloc0(), pfree(), NumericSumAccum::pos_digits, NumericVar::weight, and NumericSumAccum::weight.

Referenced by accum_sum_add().

8940 {
8941  int old_weight = accum->weight;
8942  int old_ndigits = accum->ndigits;
8943  int accum_ndigits;
8944  int accum_weight;
8945  int accum_rscale;
8946  int val_rscale;
8947 
8948  accum_weight = old_weight;
8949  accum_ndigits = old_ndigits;
8950 
8951  /*
8952  * Does the new value have a larger weight? If so, enlarge the buffers,
8953  * and shift the existing value to the new weight, by adding leading
8954  * zeros.
8955  *
8956  * We enforce that the accumulator always has a weight one larger than
8957  * needed for the inputs, so that we have space for an extra digit at the
8958  * final carry-propagation phase, if necessary.
8959  */
8960  if (val->weight >= accum_weight)
8961  {
8962  accum_weight = val->weight + 1;
8963  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
8964  }
8965 
8966  /*
8967  * Even though the new value is small, we might've used up the space
8968  * reserved for the carry digit in the last call to accum_sum_carry(). If
8969  * so, enlarge to make room for another one.
8970  */
8971  else if (!accum->have_carry_space)
8972  {
8973  accum_weight++;
8974  accum_ndigits++;
8975  }
8976 
8977  /* Is the new value wider on the right side? */
8978  accum_rscale = accum_ndigits - accum_weight - 1;
8979  val_rscale = val->ndigits - val->weight - 1;
8980  if (val_rscale > accum_rscale)
8981  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
8982 
8983  if (accum_ndigits != old_ndigits ||
8984  accum_weight != old_weight)
8985  {
8986  int32 *new_pos_digits;
8987  int32 *new_neg_digits;
8988  int weightdiff;
8989 
8990  weightdiff = accum_weight - old_weight;
8991 
8992  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
8993  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
8994 
8995  if (accum->pos_digits)
8996  {
8997  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
8998  old_ndigits * sizeof(int32));
8999  pfree(accum->pos_digits);
9000 
9001  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
9002  old_ndigits * sizeof(int32));
9003  pfree(accum->neg_digits);
9004  }
9005 
9006  accum->pos_digits = new_pos_digits;
9007  accum->neg_digits = new_neg_digits;
9008 
9009  accum->weight = accum_weight;
9010  accum->ndigits = accum_ndigits;
9011 
9012  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
9013  accum->have_carry_space = true;
9014  }
9015 
9016  if (val->dscale > accum->dscale)
9017  accum->dscale = val->dscale;
9018 }
int weight
Definition: numeric.c:272
int32 * neg_digits
Definition: numeric.c:344
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
signed int int32
Definition: c.h:256
void pfree(void *pointer)
Definition: mcxt.c:950
bool have_carry_space
Definition: numeric.c:342
void * palloc0(Size size)
Definition: mcxt.c:878
#define Assert(condition)
Definition: c.h:675
int32 * pos_digits
Definition: numeric.c:343
static void accum_sum_reset ( NumericSumAccum accum)
static

Definition at line 8802 of file numeric.c.

References NumericSumAccum::dscale, i, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, and NumericSumAccum::pos_digits.

Referenced by do_numeric_discard().

8803 {
8804  int i;
8805 
8806  accum->dscale = 0;
8807  for (i = 0; i < accum->ndigits; i++)
8808  {
8809  accum->pos_digits[i] = 0;
8810  accum->neg_digits[i] = 0;
8811  }
8812 }
int32 * neg_digits
Definition: numeric.c:344
int i
int32 * pos_digits
Definition: numeric.c:343
static void add_abs ( NumericVar var1,
NumericVar var2,
NumericVar result 
)
static

Definition at line 8426 of file numeric.c.

References Assert, NumericVar::buf, digitbuf_alloc, digitbuf_free, NumericVar::digits, NumericVar::dscale, i, Max, NBASE, NumericVar::ndigits, strip_var(), and NumericVar::weight.

Referenced by add_var(), and sub_var().

8427 {
8428  NumericDigit *res_buf;
8429  NumericDigit *res_digits;
8430  int res_ndigits;
8431  int res_weight;
8432  int res_rscale,
8433  rscale1,
8434  rscale2;
8435  int res_dscale;
8436  int i,
8437  i1,
8438  i2;
8439  int carry = 0;
8440 
8441  /* copy these values into local vars for speed in inner loop */
8442  int var1ndigits = var1->ndigits;
8443  int var2ndigits = var2->ndigits;
8444  NumericDigit *var1digits = var1->digits;
8445  NumericDigit *var2digits = var2->digits;
8446 
8447  res_weight = Max(var1->weight, var2->weight) + 1;
8448 
8449  res_dscale = Max(var1->dscale, var2->dscale);
8450 
8451  /* Note: here we are figuring rscale in base-NBASE digits */
8452  rscale1 = var1->ndigits - var1->weight - 1;
8453  rscale2 = var2->ndigits - var2->weight - 1;
8454  res_rscale = Max(rscale1, rscale2);
8455 
8456  res_ndigits = res_rscale + res_weight + 1;
8457  if (res_ndigits <= 0)
8458  res_ndigits = 1;
8459 
8460  res_buf = digitbuf_alloc(res_ndigits + 1);
8461  res_buf[0] = 0; /* spare digit for later rounding */
8462  res_digits = res_buf + 1;
8463 
8464  i1 = res_rscale + var1->weight + 1;
8465  i2 = res_rscale + var2->weight + 1;
8466  for (i = res_ndigits - 1; i >= 0; i--)
8467  {
8468  i1--;
8469  i2--;
8470  if (i1 >= 0 && i1 < var1ndigits)
8471  carry += var1digits[i1];
8472  if (i2 >= 0 && i2 < var2ndigits)
8473  carry += var2digits[i2];
8474 
8475  if (carry >= NBASE)
8476  {
8477  res_digits[i] = carry - NBASE;
8478  carry = 1;
8479  }
8480  else
8481  {
8482  res_digits[i] = carry;
8483  carry = 0;
8484  }
8485  }
8486 
8487  Assert(carry == 0); /* else we failed to allow for carry out */
8488 
8489  digitbuf_free(result->buf);
8490  result->ndigits = res_ndigits;
8491  result->buf = res_buf;
8492  result->digits = res_digits;
8493  result->weight = res_weight;
8494  result->dscale = res_dscale;
8495 
8496  /* Remove leading/trailing zeroes */
8497  strip_var(result);
8498 }
int weight
Definition: numeric.c:272
static void strip_var(NumericVar *var)
Definition: numeric.c:8761
#define digitbuf_alloc(ndigits)
Definition: numeric.c:443
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
#define digitbuf_free(buf)
Definition: numeric.c:445
int16 NumericDigit
Definition: numeric.c:99
NumericDigit * buf
Definition: numeric.c:275
#define NBASE
Definition: numeric.c:93
#define Assert(condition)
Definition: c.h:675
NumericDigit * digits
Definition: numeric.c:276
int i
#define Max(x, y)
Definition: numeric.c:11
static void add_var ( NumericVar var1,
NumericVar var2,
NumericVar result 
)
static

Definition at line 6439 of file numeric.c.

References add_abs(), cmp_abs(), NumericVar::dscale, Max, NUMERIC_NEG, NUMERIC_POS, NumericVar::sign, sub_abs(), and zero_var().

Referenced by accum_sum_final(), ceil_var(), compute_bucket(), exp_var(), generate_series_step_numeric(), ln_var(), numeric_add(), numeric_inc(), sqrt_var(), and width_bucket_numeric().

6440 {
6441  /*
6442  * Decide on the signs of the two variables what to do
6443  */
6444  if (var1->sign == NUMERIC_POS)
6445  {
6446  if (var2->sign == NUMERIC_POS)
6447  {
6448  /*
6449  * Both are positive result = +(ABS(var1) + ABS(var2))
6450  */
6451  add_abs(var1, var2, result);
6452  result->sign = NUMERIC_POS;
6453  }
6454  else
6455  {
6456  /*
6457  * var1 is positive, var2 is negative Must compare absolute values
6458  */
6459  switch (cmp_abs(var1, var2))
6460  {
6461  case 0:
6462  /* ----------
6463  * ABS(var1) == ABS(var2)
6464  * result = ZERO
6465  * ----------
6466  */
6467  zero_var(result);
6468  result->dscale = Max(var1->dscale, var2->dscale);
6469  break;
6470 
6471  case 1:
6472  /* ----------
6473  * ABS(var1) > ABS(var2)
6474  * result = +(ABS(var1) - ABS(var2))
6475  * ----------
6476  */
6477  sub_abs(var1, var2, result);
6478  result->sign = NUMERIC_POS;
6479  break;
6480 
6481  case -1:
6482  /* ----------
6483  * ABS(var1) < ABS(var2)
6484  * result = -(ABS(var2) - ABS(var1))
6485  * ----------
6486  */
6487  sub_abs(var2, var1, result);
6488  result->sign = NUMERIC_NEG;
6489  break;
6490  }
6491  }
6492  }
6493  else
6494  {
6495  if (var2->sign == NUMERIC_POS)
6496  {
6497  /* ----------
6498  * var1 is negative, var2 is positive
6499  * Must compare absolute values
6500  * ----------
6501  */
6502  switch (cmp_abs(var1, var2))
6503  {
6504  case 0:
6505  /* ----------
6506  * ABS(var1) == ABS(var2)
6507  * result = ZERO
6508  * ----------
6509  */
6510  zero_var(result);
6511  result->dscale = Max(var1->dscale, var2->dscale);
6512  break;
6513 
6514  case 1:
6515  /* ----------
6516  * ABS(var1) > ABS(var2)
6517  * result = -(ABS(var1) - ABS(var2))
6518  * ----------
6519  */
6520  sub_abs(var1, var2, result);
6521  result->sign = NUMERIC_NEG;
6522  break;
6523 
6524  case -1:
6525  /* ----------
6526  * ABS(var1) < ABS(var2)
6527  * result = +(ABS(var2) - ABS(var1))
6528  * ----------
6529  */
6530  sub_abs(var2, var1, result);
6531  result->sign = NUMERIC_POS;
6532  break;
6533  }
6534  }
6535  else
6536  {
6537  /* ----------
6538  * Both are negative
6539  * result = -(ABS(var1) + ABS(var2))
6540  * ----------
6541  */
6542  add_abs(var1, var2, result);
6543  result->sign = NUMERIC_NEG;
6544  }
6545  }
6546 }
#define NUMERIC_POS
Definition: numeric.c:164
int dscale
Definition: numeric.c:274
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static void zero_var(NumericVar *var)
Definition: numeric.c:5424
static void sub_abs(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:8511
static int cmp_abs(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:8348
static void add_abs(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:8426
#define Max(x, y)
Definition: numeric.c:11
static void alloc_var ( NumericVar var,
int  ndigits 
)
static

Definition at line 5392 of file numeric.c.

References NumericVar::buf, digitbuf_alloc, digitbuf_free, NumericVar::digits, and NumericVar::ndigits.

Referenced by div_var(), div_var_fast(), int64_to_numericvar(), mul_var(), numeric_recv(), set_var_from_num(), set_var_from_str(), and sqrt_var().

5393 {
5394  digitbuf_free(var->buf);
5395  var->buf = digitbuf_alloc(ndigits + 1);
5396  var->buf[0] = 0; /* spare digit for rounding */
5397  var->digits = var->buf + 1;
5398  var->ndigits = ndigits;
5399 }
#define digitbuf_alloc(ndigits)
Definition: numeric.c:443
int ndigits
Definition: numeric.c:271
#define digitbuf_free(buf)
Definition: numeric.c:445
NumericDigit * buf
Definition: numeric.c:275
NumericDigit * digits
Definition: numeric.c:276
static void apply_typmod ( NumericVar var,
int32  typmod 
)
static

Definition at line 6010 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, ereport, errcode(), errdetail(), errmsg(), ERROR, i, maxdigits, NumericVar::ndigits, round_var(), scale, VARHDRSZ, and NumericVar::weight.

Referenced by numeric(), numeric_in(), and numeric_recv().

6011 {
6012  int precision;
6013  int scale;
6014  int maxdigits;
6015  int ddigits;
6016  int i;
6017 
6018  /* Do nothing if we have a default typmod (-1) */
6019  if (typmod < (int32) (VARHDRSZ))
6020  return;
6021 
6022  typmod -= VARHDRSZ;
6023  precision = (typmod >> 16) & 0xffff;
6024  scale = typmod & 0xffff;
6025  maxdigits = precision - scale;
6026 
6027  /* Round to target scale (and set var->dscale) */
6028  round_var(var, scale);
6029 
6030  /*
6031  * Check for overflow - note we can't do this before rounding, because
6032  * rounding could raise the weight. Also note that the var's weight could
6033  * be inflated by leading zeroes, which will be stripped before storage
6034  * but perhaps might not have been yet. In any case, we must recognize a
6035  * true zero, whose weight doesn't mean anything.
6036  */
6037  ddigits = (var->weight + 1) * DEC_DIGITS;
6038  if (ddigits > maxdigits)
6039  {
6040  /* Determine true weight; and check for all-zero result */
6041  for (i = 0; i < var->ndigits; i++)
6042  {
6043  NumericDigit dig = var->digits[i];
6044 
6045  if (dig)
6046  {
6047  /* Adjust for any high-order decimal zero digits */
6048 #if DEC_DIGITS == 4
6049  if (dig < 10)
6050  ddigits -= 3;
6051  else if (dig < 100)
6052  ddigits -= 2;
6053  else if (dig < 1000)
6054  ddigits -= 1;
6055 #elif DEC_DIGITS == 2
6056  if (dig < 10)
6057  ddigits -= 1;
6058 #elif DEC_DIGITS == 1
6059  /* no adjustment */
6060 #else
6061 #error unsupported NBASE
6062 #endif
6063  if (ddigits > maxdigits)
6064  ereport(ERROR,
6065  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6066  errmsg("numeric field overflow"),
6067  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
6068  precision, scale,
6069  /* Display 10^0 as 1 */
6070  maxdigits ? "10^" : "",
6071  maxdigits ? maxdigits : 1
6072  )));
6073  break;
6074  }
6075  ddigits -= DEC_DIGITS;
6076  }
6077  }
6078 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8593
int weight
Definition: numeric.c:272
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
int scale
Definition: pgbench.c:106
int ndigits
Definition: numeric.c:271
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:99
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
int maxdigits
Definition: informix.c:690
NumericDigit * digits
Definition: numeric.c:276
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define DEC_DIGITS
Definition: numeric.c:95
static void ceil_var ( NumericVar var,
NumericVar result 
)
static

Definition at line 7552 of file numeric.c.

References add_var(), cmp_var(), free_var(), init_var, NUMERIC_POS, set_var_from_var(), NumericVar::sign, and trunc_var().

Referenced by numeric_ceil().

7553 {
7554  NumericVar tmp;
7555 
7556  init_var(&tmp);
7557  set_var_from_var(var, &tmp);
7558 
7559  trunc_var(&tmp, 0);
7560 
7561  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
7562  add_var(&tmp, &const_one, &tmp);
7563 
7564  set_var_from_var(&tmp, result);
7565  free_var(&tmp);
7566 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8699
static int cmp_var(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:6381
static NumericVar const_one
Definition: numeric.c:375
#define NUMERIC_POS
Definition: numeric.c:164
static void add_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6439
static void set_var_from_var(NumericVar *value, NumericVar *dest)
Definition: numeric.c:5658
int sign
Definition: numeric.c:273
static void free_var(NumericVar *var)
Definition: numeric.c:5408
#define init_var(v)
Definition: numeric.c:451
static int cmp_abs ( NumericVar var1,
NumericVar var2 
)
static

Definition at line 8348 of file numeric.c.

References cmp_abs_common(), NumericVar::digits, NumericVar::ndigits, and NumericVar::weight.

Referenced by add_var(), and sub_var().

8349 {
8350  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
8351  var2->digits, var2->ndigits, var2->weight);
8352 }
int weight
Definition: numeric.c:272
int ndigits
Definition: numeric.c:271
NumericDigit * digits
Definition: numeric.c:276
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:8362
static int cmp_abs_common ( const NumericDigit var1digits,
int  var1ndigits,
int  var1weight,
const NumericDigit var2digits,
int  var2ndigits,
int  var2weight 
)
static

Definition at line 8362 of file numeric.c.

Referenced by cmp_abs(), and cmp_var_common().

8364 {
8365  int i1 = 0;
8366  int i2 = 0;
8367 
8368  /* Check any digits before the first common digit */
8369 
8370  while (var1weight > var2weight && i1 < var1ndigits)
8371  {
8372  if (var1digits[i1++] != 0)
8373  return 1;
8374  var1weight--;
8375  }
8376  while (var2weight > var1weight && i2 < var2ndigits)
8377  {
8378  if (var2digits[i2++] != 0)
8379  return -1;
8380  var2weight--;
8381  }
8382 
8383  /* At this point, either w1 == w2 or we've run out of digits */
8384 
8385  if (var1weight == var2weight)
8386  {
8387  while (i1 < var1ndigits && i2 < var2ndigits)
8388  {
8389  int stat = var1digits[i1++] - var2digits[i2++];
8390 
8391  if (stat)
8392  {
8393  if (stat > 0)
8394  return 1;
8395  return -1;
8396  }
8397  }
8398  }
8399 
8400  /*
8401  * At this point, we've run out of digits on one side or the other; so any
8402  * remaining nonzero digits imply that side is larger
8403  */
8404  while (i1 < var1ndigits)
8405  {
8406  if (var1digits[i1++] != 0)
8407  return 1;
8408  }
8409  while (i2 < var2ndigits)
8410  {
8411  if (var2digits[i2++] != 0)
8412  return -1;
8413  }
8414 
8415  return 0;
8416 }
static int cmp_numerics ( Numeric  num1,
Numeric  num2 
)
static

Definition at line 2126 of file numeric.c.

References cmp_var_common(), NUMERIC_DIGITS, NUMERIC_IS_NAN, NUMERIC_NDIGITS, NUMERIC_SIGN, NUMERIC_WEIGHT, and result.

Referenced by numeric_cmp(), numeric_eq(), numeric_fast_cmp(), numeric_ge(), numeric_gt(), numeric_larger(), numeric_le(), numeric_lt(), numeric_ne(), numeric_smaller(), and width_bucket_numeric().

2127 {
2128  int result;
2129 
2130  /*
2131  * We consider all NANs to be equal and larger than any non-NAN. This is
2132  * somewhat arbitrary; the important thing is to have a consistent sort
2133  * order.
2134  */
2135  if (NUMERIC_IS_NAN(num1))
2136  {
2137  if (NUMERIC_IS_NAN(num2))
2138  result = 0; /* NAN = NAN */
2139  else
2140  result = 1; /* NAN > non-NAN */
2141  }
2142  else if (NUMERIC_IS_NAN(num2))
2143  {
2144  result = -1; /* non-NAN < NAN */
2145  }
2146  else
2147  {
2148  result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
2149  NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
2150  NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
2151  NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
2152  }
2153 
2154  return result;
2155 }
return result
Definition: formatting.c:1633
#define NUMERIC_DIGITS(num)
Definition: numeric.c:453
#define NUMERIC_SIGN(n)
Definition: numeric.c:206
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:455
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:6396
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:214
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
static int cmp_var ( NumericVar var1,
NumericVar var2 
)
static

Definition at line 6381 of file numeric.c.

References cmp_var_common(), NumericVar::digits, NumericVar::ndigits, NumericVar::sign, and NumericVar::weight.

Referenced by ceil_var(), compute_bucket(), estimate_ln_dweight(), floor_var(), generate_series_step_numeric(), ln_var(), numeric_power(), numeric_stddev_internal(), power_var(), and sqrt_var().

6382 {
6383  return cmp_var_common(var1->digits, var1->ndigits,
6384  var1->weight, var1->sign,
6385  var2->digits, var2->ndigits,
6386  var2->weight, var2->sign);
6387 }
int weight
Definition: numeric.c:272
int ndigits
Definition: numeric.c:271
int sign
Definition: numeric.c:273
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:6396
NumericDigit * digits
Definition: numeric.c:276
static int cmp_var_common ( const NumericDigit var1digits,
int  var1ndigits,
int  var1weight,
int  var1sign,
const NumericDigit var2digits,
int  var2ndigits,
int  var2weight,
int  var2sign 
)
static

Definition at line 6396 of file numeric.c.

References cmp_abs_common(), NUMERIC_NEG, and NUMERIC_POS.

Referenced by cmp_numerics(), and cmp_var().

6400 {
6401  if (var1ndigits == 0)
6402  {
6403  if (var2ndigits == 0)
6404  return 0;
6405  if (var2sign == NUMERIC_NEG)
6406  return 1;
6407  return -1;
6408  }
6409  if (var2ndigits == 0)
6410  {
6411  if (var1sign == NUMERIC_POS)
6412  return 1;
6413  return -1;
6414  }
6415 
6416  if (var1sign == NUMERIC_POS)
6417  {
6418  if (var2sign == NUMERIC_NEG)
6419  return 1;
6420  return cmp_abs_common(var1digits, var1ndigits, var1weight,
6421  var2digits, var2ndigits, var2weight);
6422  }
6423 
6424  if (var2sign == NUMERIC_POS)
6425  return -1;
6426 
6427  return cmp_abs_common(var2digits, var2ndigits, var2weight,
6428  var1digits, var1ndigits, var1weight);
6429 }
#define NUMERIC_POS
Definition: numeric.c:164
#define NUMERIC_NEG
Definition: numeric.c:165
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:8362
static void compute_bucket ( Numeric  operand,
Numeric  bound1,
Numeric  bound2,
NumericVar count_var,
NumericVar result_var 
)
static

Definition at line 1553 of file numeric.c.

References add_var(), cmp_var(), div_var(), NumericVar::dscale, floor_var(), free_var(), init_var_from_num(), mul_var(), select_div_scale(), and sub_var().

Referenced by width_bucket_numeric().

1555 {
1556  NumericVar bound1_var;
1557  NumericVar bound2_var;
1558  NumericVar operand_var;
1559 
1560  init_var_from_num(bound1, &bound1_var);
1561  init_var_from_num(bound2, &bound2_var);
1562  init_var_from_num(operand, &operand_var);
1563 
1564  if (cmp_var(&bound1_var, &bound2_var) < 0)
1565  {
1566  sub_var(&operand_var, &bound1_var, &operand_var);
1567  sub_var(&bound2_var, &bound1_var, &bound2_var);
1568  div_var(&operand_var, &bound2_var, result_var,
1569  select_div_scale(&operand_var, &bound2_var), true);
1570  }
1571  else
1572  {
1573  sub_var(&bound1_var, &operand_var, &operand_var);
1574  sub_var(&bound1_var, &bound2_var, &bound1_var);
1575  div_var(&operand_var, &bound1_var, result_var,
1576  select_div_scale(&operand_var, &bound1_var), true);
1577  }
1578 
1579  mul_var(result_var, count_var, result_var,
1580  result_var->dscale + count_var->dscale);
1581  add_var(result_var, &const_one, result_var);
1582  floor_var(result_var, result_var);
1583 
1584  free_var(&bound1_var);
1585  free_var(&bound2_var);
1586  free_var(&operand_var);
1587 }
static int cmp_var(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:6381
static NumericVar const_one
Definition: numeric.c:375
static int select_div_scale(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:7454
static void add_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6439
static void div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:6874
int dscale
Definition: numeric.c:274
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5641
static void floor_var(NumericVar *var, NumericVar *result)
Definition: numeric.c:7576
static void mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6677
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static void sub_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6556
static void div_var ( NumericVar var1,
NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 6874 of file numeric.c.

References alloc_var(), Assert, DEC_DIGITS, NumericVar::digits, NumericVar::dscale, ereport, errcode(), errmsg(), ERROR, HALF_NBASE, i, Max, NBASE, NumericVar::ndigits, NUMERIC_NEG, NUMERIC_POS, palloc0(), pfree(), round_var(), NumericVar::sign, strip_var(), trunc_var(), NumericVar::weight, and zero_var().

Referenced by compute_bucket(), get_str_from_var_sci(), mod_var(), numeric_div(), numeric_div_trunc(), numeric_stddev_internal(), and power_var_int().

6876 {
6877  int div_ndigits;
6878  int res_ndigits;
6879  int res_sign;
6880  int res_weight;
6881  int carry;
6882  int borrow;
6883  int divisor1;
6884  int divisor2;
6885  NumericDigit *dividend;
6886  NumericDigit *divisor;
6887  NumericDigit *res_digits;
6888  int i;
6889  int j;
6890 
6891  /* copy these values into local vars for speed in inner loop */
6892  int var1ndigits = var1->ndigits;
6893  int var2ndigits = var2->ndigits;
6894 
6895  /*
6896  * First of all division by zero check; we must not be handed an
6897  * unnormalized divisor.
6898  */
6899  if (var2ndigits == 0 || var2->digits[0] == 0)
6900  ereport(ERROR,
6901  (errcode(ERRCODE_DIVISION_BY_ZERO),
6902  errmsg("division by zero")));
6903 
6904  /*
6905  * Now result zero check
6906  */
6907  if (var1ndigits == 0)
6908  {
6909  zero_var(result);
6910  result->dscale = rscale;
6911  return;
6912  }
6913 
6914  /*
6915  * Determine the result sign, weight and number of digits to calculate.
6916  * The weight figured here is correct if the emitted quotient has no
6917  * leading zero digits; otherwise strip_var() will fix things up.
6918  */
6919  if (var1->sign == var2->sign)
6920  res_sign = NUMERIC_POS;
6921  else
6922  res_sign = NUMERIC_NEG;
6923  res_weight = var1->weight - var2->weight;
6924  /* The number of accurate result digits we need to produce: */
6925  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
6926  /* ... but always at least 1 */
6927  res_ndigits = Max(res_ndigits, 1);
6928  /* If rounding needed, figure one more digit to ensure correct result */
6929  if (round)
6930  res_ndigits++;
6931 
6932  /*
6933  * The working dividend normally requires res_ndigits + var2ndigits
6934  * digits, but make it at least var1ndigits so we can load all of var1
6935  * into it. (There will be an additional digit dividend[0] in the
6936  * dividend space, but for consistency with Knuth's notation we don't
6937  * count that in div_ndigits.)
6938  */
6939  div_ndigits = res_ndigits + var2ndigits;
6940  div_ndigits = Max(div_ndigits, var1ndigits);
6941 
6942  /*
6943  * We need a workspace with room for the working dividend (div_ndigits+1
6944  * digits) plus room for the possibly-normalized divisor (var2ndigits
6945  * digits). It is convenient also to have a zero at divisor[0] with the
6946  * actual divisor data in divisor[1 .. var2ndigits]. Transferring the
6947  * digits into the workspace also allows us to realloc the result (which
6948  * might be the same as either input var) before we begin the main loop.
6949  * Note that we use palloc0 to ensure that divisor[0], dividend[0], and
6950  * any additional dividend positions beyond var1ndigits, start out 0.
6951  */
6952  dividend = (NumericDigit *)
6953  palloc0((div_ndigits + var2ndigits + 2) * sizeof(NumericDigit));
6954  divisor = dividend + (div_ndigits + 1);
6955  memcpy(dividend + 1, var1->digits, var1ndigits * sizeof(NumericDigit));
6956  memcpy(divisor + 1, var2->digits, var2ndigits * sizeof(NumericDigit));
6957 
6958  /*
6959  * Now we can realloc the result to hold the generated quotient digits.
6960  */
6961  alloc_var(result, res_ndigits);
6962  res_digits = result->digits;
6963 
6964  if (var2ndigits == 1)
6965  {
6966  /*
6967  * If there's only a single divisor digit, we can use a fast path (cf.
6968  * Knuth section 4.3.1 exercise 16).
6969  */
6970  divisor1 = divisor[1];
6971  carry = 0;
6972  for (i = 0; i < res_ndigits; i++)
6973  {
6974  carry = carry * NBASE + dividend[i + 1];
6975  res_digits[i] = carry / divisor1;
6976  carry = carry % divisor1;
6977  }
6978  }
6979  else
6980  {
6981  /*
6982  * The full multiple-place algorithm is taken from Knuth volume 2,
6983  * Algorithm 4.3.1D.
6984  *
6985  * We need the first divisor digit to be >= NBASE/2. If it isn't,
6986  * make it so by scaling up both the divisor and dividend by the
6987  * factor "d". (The reason for allocating dividend[0] above is to
6988  * leave room for possible carry here.)
6989  */
6990  if (divisor[1] < HALF_NBASE)
6991  {
6992  int d = NBASE / (divisor[1] + 1);
6993 
6994  carry = 0;
6995  for (i = var2ndigits; i > 0; i--)
6996  {
6997  carry += divisor[i] * d;
6998  divisor[i] = carry % NBASE;
6999  carry = carry / NBASE;
7000  }
7001  Assert(carry == 0);
7002  carry = 0;
7003  /* at this point only var1ndigits of dividend can be nonzero */
7004  for (i = var1ndigits; i >= 0; i--)
7005  {
7006  carry += dividend[i] * d;
7007  dividend[i] = carry % NBASE;
7008  carry = carry / NBASE;
7009  }
7010  Assert(carry == 0);
7011  Assert(divisor[1] >= HALF_NBASE);
7012  }
7013  /* First 2 divisor digits are used repeatedly in main loop */
7014  divisor1 = divisor[1];
7015  divisor2 = divisor[2];
7016 
7017  /*
7018  * Begin the main loop. Each iteration of this loop produces the j'th
7019  * quotient digit by dividing dividend[j .. j + var2ndigits] by the
7020  * divisor; this is essentially the same as the common manual
7021  * procedure for long division.
7022  */
7023  for (j = 0; j < res_ndigits; j++)
7024  {
7025  /* Estimate quotient digit from the first two dividend digits */
7026  int next2digits = dividend[j] * NBASE + dividend[j + 1];
7027  int qhat;
7028 
7029  /*
7030  * If next2digits are 0, then quotient digit must be 0 and there's
7031  * no need to adjust the working dividend. It's worth testing
7032  * here to fall out ASAP when processing trailing zeroes in a
7033  * dividend.
7034  */
7035  if (next2digits == 0)
7036  {
7037  res_digits[j] = 0;
7038  continue;
7039  }
7040 
7041  if (dividend[j] == divisor1)
7042  qhat = NBASE - 1;
7043  else
7044  qhat = next2digits / divisor1;
7045 
7046  /*
7047  * Adjust quotient digit if it's too large. Knuth proves that
7048  * after this step, the quotient digit will be either correct or
7049  * just one too large. (Note: it's OK to use dividend[j+2] here
7050  * because we know the divisor length is at least 2.)
7051  */
7052  while (divisor2 * qhat >
7053  (next2digits - qhat * divisor1) * NBASE + dividend[j + 2])
7054  qhat--;
7055 
7056  /* As above, need do nothing more when quotient digit is 0 */
7057  if (qhat > 0)
7058  {
7059  /*
7060  * Multiply the divisor by qhat, and subtract that from the
7061  * working dividend. "carry" tracks the multiplication,
7062  * "borrow" the subtraction (could we fold these together?)
7063  */
7064  carry = 0;
7065  borrow = 0;
7066  for (i = var2ndigits; i >= 0; i--)
7067  {
7068  carry += divisor[i] * qhat;
7069  borrow -= carry % NBASE;
7070  carry = carry / NBASE;
7071  borrow += dividend[j + i];
7072  if (borrow < 0)
7073  {
7074  dividend[j + i] = borrow + NBASE;
7075  borrow = -1;
7076  }
7077  else
7078  {
7079  dividend[j + i] = borrow;
7080  borrow = 0;
7081  }
7082  }
7083  Assert(carry == 0);
7084 
7085  /*
7086  * If we got a borrow out of the top dividend digit, then
7087  * indeed qhat was one too large. Fix it, and add back the
7088  * divisor to correct the working dividend. (Knuth proves
7089  * that this will occur only about 3/NBASE of the time; hence,
7090  * it's a good idea to test this code with small NBASE to be
7091  * sure this section gets exercised.)
7092  */
7093  if (borrow)
7094  {
7095  qhat--;
7096  carry = 0;
7097  for (i = var2ndigits; i >= 0; i--)
7098  {
7099  carry += dividend[j + i] + divisor[i];
7100  if (carry >= NBASE)
7101  {
7102  dividend[j + i] = carry - NBASE;
7103  carry = 1;
7104  }
7105  else
7106  {
7107  dividend[j + i] = carry;
7108  carry = 0;
7109  }
7110  }
7111  /* A carry should occur here to cancel the borrow above */
7112  Assert(carry == 1);
7113  }
7114  }
7115 
7116  /* And we're done with this quotient digit */
7117  res_digits[j] = qhat;
7118  }
7119  }
7120 
7121  pfree(dividend);
7122 
7123  /*
7124  * Finally, round or truncate the result to the requested precision.
7125  */
7126  result->weight = res_weight;
7127  result->sign = res_sign;
7128 
7129  /* Round or truncate to target rscale (and set result->dscale) */
7130  if (round)
7131  round_var(result, rscale);
7132  else
7133  trunc_var(result, rscale);
7134 
7135  /* Strip leading and trailing zeroes */
7136  strip_var(result);
7137 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8593
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8699
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
static void strip_var(NumericVar *var)
Definition: numeric.c:8761
int errcode(int sqlerrcode)
Definition: elog.c:575
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static void zero_var(NumericVar *var)
Definition: numeric.c:5424
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:99
#define HALF_NBASE
Definition: numeric.c:94
#define ereport(elevel, rest)
Definition: elog.h:122
#define NBASE
Definition: numeric.c:93
void * palloc0(Size size)
Definition: mcxt.c:878
#define Assert(condition)
Definition: c.h:675
NumericDigit * digits
Definition: numeric.c:276
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:5392
int i
#define Max(x, y)
Definition: numeric.c:11
#define DEC_DIGITS
Definition: numeric.c:95
static void div_var_fast ( NumericVar var1,
NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 7159 of file numeric.c.

References Abs, alloc_var(), Assert, DEC_DIGITS, NumericVar::digits, DIV_GUARD_DIGITS, NumericVar::dscale, ereport, errcode(), errmsg(), ERROR, i, Min, NBASE, NumericVar::ndigits, NUMERIC_NEG, NUMERIC_POS, palloc0(), pfree(), round_var(), NumericVar::sign, strip_var(), trunc_var(), NumericVar::weight, and zero_var().

Referenced by exp_var(), ln_var(), log_var(), power_var_int(), and sqrt_var().

7161 {
7162  int div_ndigits;
7163  int res_sign;
7164  int res_weight;
7165  int *div;
7166  int qdigit;
7167  int carry;
7168  int maxdiv;
7169  int newdig;
7170  NumericDigit *res_digits;
7171  double fdividend,
7172  fdivisor,
7173  fdivisorinverse,
7174  fquotient;
7175  int qi;
7176  int i;
7177 
7178  /* copy these values into local vars for speed in inner loop */
7179  int var1ndigits = var1->ndigits;
7180  int var2ndigits = var2->ndigits;
7181  NumericDigit *var1digits = var1->digits;
7182  NumericDigit *var2digits = var2->digits;
7183 
7184  /*
7185  * First of all division by zero check; we must not be handed an
7186  * unnormalized divisor.
7187  */
7188  if (var2ndigits == 0 || var2digits[0] == 0)
7189  ereport(ERROR,
7190  (errcode(ERRCODE_DIVISION_BY_ZERO),
7191  errmsg("division by zero")));
7192 
7193  /*
7194  * Now result zero check
7195  */
7196  if (var1ndigits == 0)
7197  {
7198  zero_var(result);
7199  result->dscale = rscale;
7200  return;
7201  }
7202 
7203  /*
7204  * Determine the result sign, weight and number of digits to calculate
7205  */
7206  if (var1->sign == var2->sign)
7207  res_sign = NUMERIC_POS;
7208  else
7209  res_sign = NUMERIC_NEG;
7210  res_weight = var1->weight - var2->weight + 1;
7211  /* The number of accurate result digits we need to produce: */
7212  div_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
7213  /* Add guard digits for roundoff error */
7214  div_ndigits += DIV_GUARD_DIGITS;
7215  if (div_ndigits < DIV_GUARD_DIGITS)
7216  div_ndigits = DIV_GUARD_DIGITS;
7217  /* Must be at least var1ndigits, too, to simplify data-loading loop */
7218  if (div_ndigits < var1ndigits)
7219  div_ndigits = var1ndigits;
7220 
7221  /*
7222  * We do the arithmetic in an array "div[]" of signed int's. Since
7223  * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
7224  * to avoid normalizing carries immediately.
7225  *
7226  * We start with div[] containing one zero digit followed by the
7227  * dividend's digits (plus appended zeroes to reach the desired precision
7228  * including guard digits). Each step of the main loop computes an
7229  * (approximate) quotient digit and stores it into div[], removing one
7230  * position of dividend space. A final pass of carry propagation takes
7231  * care of any mistaken quotient digits.
7232  */
7233  div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
7234  for (i = 0; i < var1ndigits; i++)
7235  div[i + 1] = var1digits[i];
7236 
7237  /*
7238  * We estimate each quotient digit using floating-point arithmetic, taking
7239  * the first four digits of the (current) dividend and divisor. This must
7240  * be float to avoid overflow. The quotient digits will generally be off
7241  * by no more than one from the exact answer.
7242  */
7243  fdivisor = (double) var2digits[0];
7244  for (i = 1; i < 4; i++)
7245  {
7246  fdivisor *= NBASE;
7247  if (i < var2ndigits)
7248  fdivisor += (double) var2digits[i];
7249  }
7250  fdivisorinverse = 1.0 / fdivisor;
7251 
7252  /*
7253  * maxdiv tracks the maximum possible absolute value of any div[] entry;
7254  * when this threatens to exceed INT_MAX, we take the time to propagate
7255  * carries. Furthermore, we need to ensure that overflow doesn't occur
7256  * during the carry propagation passes either. The carry values may have
7257  * an absolute value as high as INT_MAX/NBASE + 1, so really we must
7258  * normalize when digits threaten to exceed INT_MAX - INT_MAX/NBASE - 1.
7259  *
7260  * To avoid overflow in maxdiv itself, it represents the max absolute
7261  * value divided by NBASE-1, ie, at the top of the loop it is known that
7262  * no div[] entry has an absolute value exceeding maxdiv * (NBASE-1).
7263  *
7264  * Actually, though, that holds good only for div[] entries after div[qi];
7265  * the adjustment done at the bottom of the loop may cause div[qi + 1] to
7266  * exceed the maxdiv limit, so that div[qi] in the next iteration is
7267  * beyond the limit. This does not cause problems, as explained below.
7268  */
7269  maxdiv = 1;
7270 
7271  /*
7272  * Outer loop computes next quotient digit, which will go into div[qi]
7273  */
7274  for (qi = 0; qi < div_ndigits; qi++)
7275  {
7276  /* Approximate the current dividend value */
7277  fdividend = (double) div[qi];
7278  for (i = 1; i < 4; i++)
7279  {
7280  fdividend *= NBASE;
7281  if (qi + i <= div_ndigits)
7282  fdividend += (double) div[qi + i];
7283  }
7284  /* Compute the (approximate) quotient digit */
7285  fquotient = fdividend * fdivisorinverse;
7286  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7287  (((int) fquotient) - 1); /* truncate towards -infinity */
7288 
7289  if (qdigit != 0)
7290  {
7291  /* Do we need to normalize now? */
7292  maxdiv += Abs(qdigit);
7293  if (maxdiv > (INT_MAX - INT_MAX / NBASE - 1) / (NBASE - 1))
7294  {
7295  /* Yes, do it */
7296  carry = 0;
7297  for (i = div_ndigits; i > qi; i--)
7298  {
7299  newdig = div[i] + carry;
7300  if (newdig < 0)
7301  {
7302  carry = -((-newdig - 1) / NBASE) - 1;
7303  newdig -= carry * NBASE;
7304  }
7305  else if (newdig >= NBASE)
7306  {
7307  carry = newdig / NBASE;
7308  newdig -= carry * NBASE;
7309  }
7310  else
7311  carry = 0;
7312  div[i] = newdig;
7313  }
7314  newdig = div[qi] + carry;
7315  div[qi] = newdig;
7316 
7317  /*
7318  * All the div[] digits except possibly div[qi] are now in the
7319  * range 0..NBASE-1. We do not need to consider div[qi] in
7320  * the maxdiv value anymore, so we can reset maxdiv to 1.
7321  */
7322  maxdiv = 1;
7323 
7324  /*
7325  * Recompute the quotient digit since new info may have
7326  * propagated into the top four dividend digits
7327  */
7328  fdividend = (double) div[qi];
7329  for (i = 1; i < 4; i++)
7330  {
7331  fdividend *= NBASE;
7332  if (qi + i <= div_ndigits)
7333  fdividend += (double) div[qi + i];
7334  }
7335  /* Compute the (approximate) quotient digit */
7336  fquotient = fdividend * fdivisorinverse;
7337  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7338  (((int) fquotient) - 1); /* truncate towards -infinity */
7339  maxdiv += Abs(qdigit);
7340  }
7341 
7342  /*
7343  * Subtract off the appropriate multiple of the divisor.
7344  *
7345  * The digits beyond div[qi] cannot overflow, because we know they
7346  * will fall within the maxdiv limit. As for div[qi] itself, note
7347  * that qdigit is approximately trunc(div[qi] / vardigits[0]),
7348  * which would make the new value simply div[qi] mod vardigits[0].
7349  * The lower-order terms in qdigit can change this result by not
7350  * more than about twice INT_MAX/NBASE, so overflow is impossible.
7351  */
7352  if (qdigit != 0)
7353  {
7354  int istop = Min(var2ndigits, div_ndigits - qi + 1);
7355 
7356  for (i = 0; i < istop; i++)
7357  div[qi + i] -= qdigit * var2digits[i];
7358  }
7359  }
7360 
7361  /*
7362  * The dividend digit we are about to replace might still be nonzero.
7363  * Fold it into the next digit position.
7364  *
7365  * There is no risk of overflow here, although proving that requires
7366  * some care. Much as with the argument for div[qi] not overflowing,
7367  * if we consider the first two terms in the numerator and denominator
7368  * of qdigit, we can see that the final value of div[qi + 1] will be
7369  * approximately a remainder mod (vardigits[0]*NBASE + vardigits[1]).
7370  * Accounting for the lower-order terms is a bit complicated but ends
7371  * up adding not much more than INT_MAX/NBASE to the possible range.
7372  * Thus, div[qi + 1] cannot overflow here, and in its role as div[qi]
7373  * in the next loop iteration, it can't be large enough to cause
7374  * overflow in the carry propagation step (if any), either.
7375  *
7376  * But having said that: div[qi] can be more than INT_MAX/NBASE, as
7377  * noted above, which means that the product div[qi] * NBASE *can*
7378  * overflow. When that happens, adding it to div[qi + 1] will always
7379  * cause a canceling overflow so that the end result is correct. We
7380  * could avoid the intermediate overflow by doing the multiplication
7381  * and addition in int64 arithmetic, but so far there appears no need.
7382  */
7383  div[qi + 1] += div[qi] * NBASE;
7384 
7385  div[qi] = qdigit;
7386  }
7387 
7388  /*
7389  * Approximate and store the last quotient digit (div[div_ndigits])
7390  */
7391  fdividend = (double) div[qi];
7392  for (i = 1; i < 4; i++)
7393  fdividend *= NBASE;
7394  fquotient = fdividend * fdivisorinverse;
7395  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7396  (((int) fquotient) - 1); /* truncate towards -infinity */
7397  div[qi] = qdigit;
7398 
7399  /*
7400  * Because the quotient digits might be off by one, some of them might be
7401  * -1 or NBASE at this point. The represented value is correct in a
7402  * mathematical sense, but it doesn't look right. We do a final carry
7403  * propagation pass to normalize the digits, which we combine with storing
7404  * the result digits into the output. Note that this is still done at
7405  * full precision w/guard digits.
7406  */
7407  alloc_var(result, div_ndigits + 1);
7408  res_digits = result->digits;
7409  carry = 0;
7410  for (i = div_ndigits; i >= 0; i--)
7411  {
7412  newdig = div[i] + carry;
7413  if (newdig < 0)
7414  {
7415  carry = -((-newdig - 1) / NBASE) - 1;
7416  newdig -= carry * NBASE;
7417  }
7418  else if (newdig >= NBASE)
7419  {
7420  carry = newdig / NBASE;
7421  newdig -= carry * NBASE;
7422  }
7423  else
7424  carry = 0;
7425  res_digits[i] = newdig;
7426  }
7427  Assert(carry == 0);
7428 
7429  pfree(div);
7430 
7431  /*
7432  * Finally, round the result to the requested precision.
7433  */
7434  result->weight = res_weight;
7435  result->sign = res_sign;
7436 
7437  /* Round to target rscale (and set result->dscale) */
7438  if (round)
7439  round_var(result, rscale);
7440  else
7441  trunc_var(result, rscale);
7442 
7443  /* Strip leading and trailing zeroes */
7444  strip_var(result);
7445 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8593
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8699
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
static void strip_var(NumericVar *var)
Definition: numeric.c:8761
int errcode(int sqlerrcode)
Definition: elog.c:575
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
#define Min(x, y)
Definition: numeric.c:12
#define Abs(x)
Definition: c.h:812
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static void zero_var(NumericVar *var)
Definition: numeric.c:5424
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:99
#define ereport(elevel, rest)
Definition: elog.h:122
#define NBASE
Definition: numeric.c:93
void * palloc0(Size size)
Definition: mcxt.c:878
#define Assert(condition)
Definition: c.h:675
NumericDigit * digits
Definition: numeric.c:276
#define DIV_GUARD_DIGITS
Definition: numeric.c:97
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:5392
int i
#define DEC_DIGITS
Definition: numeric.c:95
static void do_numeric_accum ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 3249 of file numeric.c.

References accum_sum_add(), NumericAggState::agg_context, NumericAggState::calcSumX2, NumericVar::dscale, init_var, init_var_from_num(), NumericAggState::maxScale, NumericAggState::maxScaleCount, MemoryContextSwitchTo(), mul_var(), NumericAggState::N, NumericAggState::NaNcount, NUMERIC_IS_NAN, NumericAggState::sumX, and NumericAggState::sumX2.

Referenced by int2_accum(), int4_accum(), int8_accum(), int8_avg_accum(), numeric_accum(), and numeric_avg_accum().

3250 {
3251  NumericVar X;
3252  NumericVar X2;
3253  MemoryContext old_context;
3254 
3255  /* Count NaN inputs separately from all else */
3256  if (NUMERIC_IS_NAN(newval))
3257  {
3258  state->NaNcount++;
3259  return;
3260  }
3261 
3262  /* load processed number in short-lived context */
3263  init_var_from_num(newval, &X);
3264 
3265  /*
3266  * Track the highest input dscale that we've seen, to support inverse
3267  * transitions (see do_numeric_discard).
3268  */
3269  if (X.dscale > state->maxScale)
3270  {
3271  state->maxScale = X.dscale;
3272  state->maxScaleCount = 1;
3273  }
3274  else if (X.dscale == state->maxScale)
3275  state->maxScaleCount++;
3276 
3277  /* if we need X^2, calculate that in short-lived context */
3278  if (state->calcSumX2)
3279  {
3280  init_var(&X2);
3281  mul_var(&X, &X, &X2, X.dscale * 2);
3282  }
3283 
3284  /* The rest of this needs to work in the aggregate context */
3285  old_context = MemoryContextSwitchTo(state->agg_context);
3286 
3287  state->N++;
3288 
3289  /* Accumulate sums */
3290  accum_sum_add(&(state->sumX), &X);
3291 
3292  if (state->calcSumX2)
3293  accum_sum_add(&(state->sumX2), &X2);
3294 
3295  MemoryContextSwitchTo(old_context);
3296 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:3195
int dscale
Definition: numeric.c:274
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5641
static void mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6677
static void accum_sum_add(NumericSumAccum *accum, NumericVar *var1)
Definition: numeric.c:8818
NumericSumAccum sumX2
Definition: numeric.c:3198
int64 NaNcount
Definition: numeric.c:3201
int64 maxScaleCount
Definition: numeric.c:3200
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
NumericSumAccum sumX
Definition: numeric.c:3197
#define init_var(v)
Definition: numeric.c:451
static bool do_numeric_discard ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 3314 of file numeric.c.

References accum_sum_add(), accum_sum_reset(), NumericAggState::agg_context, Assert, NumericAggState::calcSumX2, NumericVar::dscale, init_var, init_var_from_num(), NumericAggState::maxScale, NumericAggState::maxScaleCount, MemoryContextSwitchTo(), mul_var(), NumericAggState::N, NumericAggState::NaNcount, NUMERIC_IS_NAN, NUMERIC_NEG, NUMERIC_POS, NumericVar::sign, NumericAggState::sumX, and NumericAggState::sumX2.

Referenced by int2_accum_inv(), int4_accum_inv(), int8_accum_inv(), int8_avg_accum_inv(), and numeric_accum_inv().

3315 {
3316  NumericVar X;
3317  NumericVar X2;
3318  MemoryContext old_context;
3319 
3320  /* Count NaN inputs separately from all else */
3321  if (NUMERIC_IS_NAN(newval))
3322  {
3323  state->NaNcount--;
3324  return true;
3325  }
3326 
3327  /* load processed number in short-lived context */
3328  init_var_from_num(newval, &X);
3329 
3330  /*
3331  * state->sumX's dscale is the maximum dscale of any of the inputs.
3332  * Removing the last input with that dscale would require us to recompute
3333  * the maximum dscale of the *remaining* inputs, which we cannot do unless
3334  * no more non-NaN inputs remain at all. So we report a failure instead,
3335  * and force the aggregation to be redone from scratch.
3336  */
3337  if (X.dscale == state->maxScale)
3338  {
3339  if (state->maxScaleCount > 1 || state->maxScale == 0)
3340  {
3341  /*
3342  * Some remaining inputs have same dscale, or dscale hasn't gotten
3343  * above zero anyway
3344  */
3345  state->maxScaleCount--;
3346  }
3347  else if (state->N == 1)
3348  {
3349  /* No remaining non-NaN inputs at all, so reset maxScale */
3350  state->maxScale = 0;
3351  state->maxScaleCount = 0;
3352  }
3353  else
3354  {
3355  /* Correct new maxScale is uncertain, must fail */
3356  return false;
3357  }
3358  }
3359 
3360  /* if we need X^2, calculate that in short-lived context */
3361  if (state->calcSumX2)
3362  {
3363  init_var(&X2);
3364  mul_var(&X, &X, &X2, X.dscale * 2);
3365  }
3366 
3367  /* The rest of this needs to work in the aggregate context */
3368  old_context = MemoryContextSwitchTo(state->agg_context);
3369 
3370  if (state->N-- > 1)
3371  {
3372  /* Negate X, to subtract it from the sum */
3373  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
3374  accum_sum_add(&(state->sumX), &X);
3375 
3376  if (state->calcSumX2)
3377  {
3378  /* Negate X^2. X^2 is always positive */
3379  X2.sign = NUMERIC_NEG;
3380  accum_sum_add(&(state->sumX2), &X2);
3381  }
3382  }
3383  else
3384  {
3385  /* Zero the sums */
3386  Assert(state->N == 0);
3387 
3388  accum_sum_reset(&state->sumX);
3389  if (state->calcSumX2)
3390  accum_sum_reset(&state->sumX2);
3391  }
3392 
3393  MemoryContextSwitchTo(old_context);
3394 
3395  return true;
3396 }
#define NUMERIC_POS
Definition: numeric.c:164
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:3195
int dscale
Definition: numeric.c:274
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5641
static void mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6677
#define Assert(condition)
Definition: c.h:675
static void accum_sum_add(NumericSumAccum *accum, NumericVar *var1)
Definition: numeric.c:8818
NumericSumAccum sumX2
Definition: numeric.c:3198
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:8802
int64 NaNcount
Definition: numeric.c:3201
int64 maxScaleCount
Definition: numeric.c:3200
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
NumericSumAccum sumX
Definition: numeric.c:3197
#define init_var(v)
Definition: numeric.c:451
static int estimate_ln_dweight ( NumericVar var)
static

Definition at line 7798 of file numeric.c.

References Abs, cmp_var(), DEC_DIGITS, NumericVar::digits, digits, free_var(), init_var, ln_var(), NBASE, NumericVar::ndigits, sub_var(), and NumericVar::weight.

Referenced by log_var(), numeric_ln(), and power_var().

7799 {
7800  int ln_dweight;
7801 
7802  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
7803  cmp_var(var, &const_one_point_one) <= 0)
7804  {
7805  /*
7806  * 0.9 <= var <= 1.1
7807  *
7808  * ln(var) has a negative weight (possibly very large). To get a
7809  * reasonably accurate result, estimate it using ln(1+x) ~= x.
7810  */
7811  NumericVar x;
7812 
7813  init_var(&x);
7814  sub_var(var, &const_one, &x);
7815 
7816  if (x.ndigits > 0)
7817  {
7818  /* Use weight of most significant decimal digit of x */
7819  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
7820  }
7821  else
7822  {
7823  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
7824  ln_dweight = 0;
7825  }
7826 
7827  free_var(&x);
7828  }
7829  else
7830  {
7831  /*
7832  * Estimate the logarithm using the first couple of digits from the
7833  * input number. This will give an accurate result whenever the input
7834  * is not too close to 1.
7835  */
7836  if (var->ndigits > 0)
7837  {
7838  int digits;
7839  int dweight;
7840  double ln_var;
7841 
7842  digits = var->digits[0];
7843  dweight = var->weight * DEC_DIGITS;
7844 
7845  if (var->ndigits > 1)
7846  {
7847  digits = digits * NBASE + var->digits[1];
7848  dweight -= DEC_DIGITS;
7849  }
7850 
7851  /*----------
7852  * We have var ~= digits * 10^dweight
7853  * so ln(var) ~= ln(digits) + dweight * ln(10)
7854  *----------
7855  */
7856  ln_var = log((double) digits) + dweight * 2.302585092994046;
7857  ln_dweight = (int) log10(Abs(ln_var));
7858  }
7859  else
7860  {
7861  /* Caller should fail on ln(0), but for the moment return zero */
7862  ln_dweight = 0;
7863  }
7864  }
7865 
7866  return ln_dweight;
7867 }
static int cmp_var(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:6381
int weight
Definition: numeric.c:272
static NumericVar const_one
Definition: numeric.c:375
static NumericVar const_zero_point_nine
Definition: numeric.c:409
int ndigits
Definition: numeric.c:271
static void ln_var(NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:7876
#define Abs(x)
Definition: c.h:812
static NumericVar const_one_point_one
Definition: numeric.c:419
#define NBASE
Definition: numeric.c:93
static void free_var(NumericVar *var)
Definition: numeric.c:5408
NumericDigit * digits
Definition: numeric.c:276
#define DEC_DIGITS
Definition: numeric.c:95
#define init_var(v)
Definition: numeric.c:451
static void sub_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6556
int digits
Definition: informix.c:691
static void exp_var ( NumericVar arg,
NumericVar result,
int  rscale 
)
static

Definition at line 7672 of file numeric.c.

References Abs, add_var(), DEC_DIGITS, div_var_fast(), NumericVar::dscale, ereport, errcode(), errmsg(), ERROR, free_var(), init_var, Max, mul_var(), NumericVar::ndigits, NUMERIC_MAX_RESULT_SCALE, NUMERIC_MIN_DISPLAY_SCALE, numericvar_to_double_no_overflow(), round_var(), set_var_from_var(), val, and NumericVar::weight.

Referenced by numeric_exp(), and power_var().

7673 {
7674  NumericVar x;
7675  NumericVar elem;
7676  NumericVar ni;
7677  double val;
7678  int dweight;
7679  int ndiv2;
7680  int sig_digits;
7681  int local_rscale;
7682 
7683  init_var(&x);
7684  init_var(&elem);
7685  init_var(&ni);
7686 
7687  set_var_from_var(arg, &x);
7688 
7689  /*
7690  * Estimate the dweight of the result using floating point arithmetic, so
7691  * that we can choose an appropriate local rscale for the calculation.
7692  */
7694 
7695  /* Guard against overflow */
7696  /* If you change this limit, see also power_var()'s limit */
7697  if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
7698  ereport(ERROR,
7699  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7700  errmsg("value overflows numeric format")));
7701 
7702  /* decimal weight = log10(e^x) = x * log10(e) */
7703  dweight = (int) (val * 0.434294481903252);
7704 
7705  /*
7706  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
7707  * 2^n, to improve the convergence rate of the Taylor series.
7708  */
7709  if (Abs(val) > 0.01)
7710  {
7711  NumericVar tmp;
7712 
7713  init_var(&tmp);
7714  set_var_from_var(&const_two, &tmp);
7715 
7716  ndiv2 = 1;
7717  val /= 2;
7718 
7719  while (Abs(val) > 0.01)
7720  {
7721  ndiv2++;
7722  val /= 2;
7723  add_var(&tmp, &tmp, &tmp);
7724  }
7725 
7726  local_rscale = x.dscale + ndiv2;
7727  div_var_fast(&x, &tmp, &x, local_rscale, true);
7728 
7729  free_var(&tmp);
7730  }
7731  else
7732  ndiv2 = 0;
7733 
7734  /*
7735  * Set the scale for the Taylor series expansion. The final result has
7736  * (dweight + rscale + 1) significant digits. In addition, we have to
7737  * raise the Taylor series result to the power 2^ndiv2, which introduces
7738  * an error of up to around log10(2^ndiv2) digits, so work with this many
7739  * extra digits of precision (plus a few more for good measure).
7740  */
7741  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
7742  sig_digits = Max(sig_digits, 0) + 8;
7743 
7744  local_rscale = sig_digits - 1;
7745 
7746  /*
7747  * Use the Taylor series
7748  *
7749  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
7750  *
7751  * Given the limited range of x, this should converge reasonably quickly.
7752  * We run the series until the terms fall below the local_rscale limit.
7753  */
7754  add_var(&const_one, &x, result);
7755 
7756  mul_var(&x, &x, &elem, local_rscale);
7757  set_var_from_var(&const_two, &ni);
7758  div_var_fast(&elem, &ni, &elem, local_rscale, true);
7759 
7760  while (elem.ndigits != 0)
7761  {
7762  add_var(result, &elem, result);
7763 
7764  mul_var(&elem, &x, &elem, local_rscale);
7765  add_var(&ni, &const_one, &ni);
7766  div_var_fast(&elem, &ni, &elem, local_rscale, true);
7767  }
7768 
7769  /*
7770  * Compensate for the argument range reduction. Since the weight of the
7771  * result doubles with each multiplication, we can reduce the local rscale
7772  * as we proceed.
7773  */
7774  while (ndiv2-- > 0)
7775  {
7776  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
7777  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
7778  mul_var(result, result, result, local_rscale);
7779  }
7780 
7781  /* Round to requested rscale */
7782  round_var(result, rscale);
7783 
7784  free_var(&x);
7785  free_var(&elem);
7786  free_var(&ni);
7787 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8593
int weight
Definition: numeric.c:272
static NumericVar const_one
Definition: numeric.c:375
int errcode(int sqlerrcode)
Definition: elog.c:575
static double numericvar_to_double_no_overflow(NumericVar *var)
Definition: numeric.c:6349
static void add_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6439
int ndigits
Definition: numeric.c:271
static void set_var_from_var(NumericVar *value, NumericVar *dest)
Definition: numeric.c:5658
int dscale
Definition: numeric.c:274
static NumericVar const_two
Definition: numeric.c:379
#define Abs(x)
Definition: c.h:812
#define ERROR
Definition: elog.h:43
#define NUMERIC_MIN_DISPLAY_SCALE
Definition: numeric.h:30
#define NUMERIC_MAX_RESULT_SCALE
Definition: numeric.h:32
#define ereport(elevel, rest)
Definition: elog.h:122
static void mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6677
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static void div_var_fast(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:7159
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define Max(x, y)
Definition: numeric.c:11
#define DEC_DIGITS
Definition: numeric.c:95
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum float4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3132 of file numeric.c.

References buf, free_var(), init_var, make_result(), PG_GETARG_FLOAT4, PG_RETURN_NUMERIC, result, set_var_from_str(), and val.

3133 {
3135  Numeric res;
3137  char buf[FLT_DIG + 100];
3138 
3139  if (isnan(val))
3141 
3142  sprintf(buf, "%.*g", FLT_DIG, val);
3143 
3144  init_var(&result);
3145 
3146  /* Assume we need not worry about leading/trailing spaces */
3147  (void) set_var_from_str(buf, buf, &result);
3148 
3149  res = make_result(&result);
3150 
3151  free_var(&result);
3152 
3153  PG_RETURN_NUMERIC(res);
3154 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
return result
Definition: formatting.c:1633
static NumericVar const_nan
Definition: numeric.c:422
static char * buf
Definition: pg_test_fsync.c:66
static Numeric make_result(NumericVar *var)
Definition: numeric.c:5923
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:245
float float4
Definition: c.h:380
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:5448
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum float8_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3066 of file numeric.c.

References buf, free_var(), init_var, make_result(), PG_GETARG_FLOAT8, PG_RETURN_NUMERIC, result, set_var_from_str(), and val.

3067 {
3069  Numeric res;
3071  char buf[DBL_DIG + 100];
3072 
3073  if (isnan(val))
3075 
3076  sprintf(buf, "%.*g", DBL_DIG, val);
3077 
3078  init_var(&result);
3079 
3080  /* Assume we need not worry about leading/trailing spaces */
3081  (void) set_var_from_str(buf, buf, &result);
3082 
3083  res = make_result(&result);
3084 
3085  free_var(&result);
3086 
3087  PG_RETURN_NUMERIC(res);
3088 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
return result
Definition: formatting.c:1633
static NumericVar const_nan
Definition: numeric.c:422
double float8
Definition: c.h:381
static char * buf
Definition: pg_test_fsync.c:66
static Numeric make_result(NumericVar *var)
Definition: numeric.c:5923
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:5448
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
static void floor_var ( NumericVar var,
NumericVar result 
)
static

Definition at line 7576 of file numeric.c.

References cmp_var(), free_var(), init_var, NUMERIC_NEG, set_var_from_var(), NumericVar::sign, sub_var(), and trunc_var().

Referenced by compute_bucket(), and numeric_floor().

7577 {
7578  NumericVar tmp;
7579 
7580  init_var(&tmp);
7581  set_var_from_var(var, &tmp);
7582 
7583  trunc_var(&tmp, 0);
7584 
7585  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
7586  sub_var(&tmp, &const_one, &tmp);
7587 
7588  set_var_from_var(&tmp, result);
7589  free_var(&tmp);
7590 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8699
static int cmp_var(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:6381
static NumericVar const_one
Definition: numeric.c:375
static void set_var_from_var(NumericVar *value, NumericVar *dest)
Definition: numeric.c:5658
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static void free_var(NumericVar *var)
Definition: numeric.c:5408
#define init_var(v)
Definition: numeric.c:451
static void sub_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6556
static void free_var ( NumericVar var)
static
Datum generate_series_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1357 of file numeric.c.

References generate_series_step_numeric().

1358 {
1359  return generate_series_step_numeric(fcinfo);
1360 }
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1363
Datum generate_series_step_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1363 of file numeric.c.

References add_var(), cmp_var(), const_one, generate_series_numeric_fctx::current, ereport, errcode(), errmsg(), ERROR, init_var, init_var_from_num(), make_result(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NUMERIC_IS_NAN, NUMERIC_NEG, NUMERIC_POS, NumericGetDatum, palloc(), PG_GETARG_NUMERIC, PG_NARGS, result, set_var_from_num(), set_var_from_var(), NumericVar::sign, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_numeric_fctx::step, generate_series_numeric_fctx::stop, and FuncCallContext::user_fctx.

Referenced by generate_series_numeric().

1364 {
1366  FuncCallContext *funcctx;
1367  MemoryContext oldcontext;
1368 
1369  if (SRF_IS_FIRSTCALL())
1370  {
1371  Numeric start_num = PG_GETARG_NUMERIC(0);
1372  Numeric stop_num = PG_GETARG_NUMERIC(1);
1373  NumericVar steploc = const_one;
1374 
1375  /* handle NaN in start and stop values */
1376  if (NUMERIC_IS_NAN(start_num))
1377  ereport(ERROR,
1378  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1379  errmsg("start value cannot be NaN")));
1380 
1381  if (NUMERIC_IS_NAN(stop_num))
1382  ereport(ERROR,
1383  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1384  errmsg("stop value cannot be NaN")));
1385 
1386  /* see if we were given an explicit step size */
1387  if (PG_NARGS() == 3)
1388  {
1389  Numeric step_num = PG_GETARG_NUMERIC(2);
1390 
1391  if (NUMERIC_IS_NAN(step_num))
1392  ereport(ERROR,
1393  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1394  errmsg("step size cannot be NaN")));
1395 
1396  init_var_from_num(step_num, &steploc);
1397 
1398  if (cmp_var(&steploc, &const_zero) == 0)
1399  ereport(ERROR,
1400  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1401  errmsg("step size cannot equal zero")));
1402  }
1403 
1404  /* create a function context for cross-call persistence */
1405  funcctx = SRF_FIRSTCALL_INIT();
1406 
1407  /*
1408  * Switch to memory context appropriate for multiple function calls.
1409  */
1410  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1411 
1412  /* allocate memory for user context */
1413  fctx = (generate_series_numeric_fctx *)
1415 
1416  /*
1417  * Use fctx to keep state from call to call. Seed current with the
1418  * original start value. We must copy the start_num and stop_num
1419  * values rather than pointing to them, since we may have detoasted
1420  * them in the per-call context.
1421  */
1422  init_var(&fctx->current);
1423  init_var(&fctx->stop);
1424  init_var(&fctx->step);
1425 
1426  set_var_from_num(start_num, &fctx->current);
1427  set_var_from_num(stop_num, &fctx->stop);
1428  set_var_from_var(&steploc, &fctx->step);
1429 
1430  funcctx->user_fctx = fctx;
1431  MemoryContextSwitchTo(oldcontext);
1432  }
1433 
1434  /* stuff done on every call of the function */
1435  funcctx = SRF_PERCALL_SETUP();
1436 
1437  /*
1438  * Get the saved state and use current state as the result of this
1439  * iteration.
1440  */
1441  fctx = funcctx->user_fctx;
1442 
1443  if ((fctx->step.sign == NUMERIC_POS &&
1444  cmp_var(&fctx->current, &fctx->stop) <= 0) ||
1445  (fctx->step.sign == NUMERIC_NEG &&
1446  cmp_var(&fctx->current, &fctx->stop) >= 0))
1447  {
1448  Numeric result = make_result(&fctx->current);
1449 
1450  /* switch to memory context appropriate for iteration calculation */
1451  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1452 
1453  /* increment current in preparation for next iteration */
1454  add_var(&fctx->current, &fctx->step, &fctx->current);
1455  MemoryContextSwitchTo(oldcontext);
1456 
1457  /* do when there is more left to send */
1458  SRF_RETURN_NEXT(funcctx, NumericGetDatum(result));
1459  }
1460  else
1461  /* do when there is no more left */
1462  SRF_RETURN_DONE(funcctx);
1463 }
static int cmp_var(NumericVar *var1, NumericVar *var2)
Definition: numeric.c:6381
static NumericVar const_one
Definition: numeric.c:375
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define NumericGetDatum(X)
Definition: numeric.h:51
#define NUMERIC_POS
Definition: numeric.c:164
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
static void add_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
Definition: numeric.c:6439
static void set_var_from_var(NumericVar *value, NumericVar *dest)
Definition: numeric.c:5658
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
static NumericVar const_zero
Definition: numeric.c:371
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
#define ERROR
Definition: elog.h:43
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5641
static Numeric make_result(NumericVar *var)
Definition: numeric.c:5923
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
static void set_var_from_num(Numeric value, NumericVar *dest)
Definition: numeric.c:5610
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define PG_NARGS()
Definition: fmgr.h:168
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
#define init_var(v)
Definition: numeric.c:451
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
static char * get_str_from_var ( NumericVar var)
static

Definition at line 5684 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, NumericVar::dscale, i, NumericVar::ndigits, NUMERIC_NEG, palloc(), NumericVar::sign, and NumericVar::weight.

Referenced by get_str_from_var_sci(), numeric_normalize(), numeric_out(), and numericvar_to_double_no_overflow().

5685 {
5686  int dscale;
5687  char *str;
5688  char *cp;
5689  char *endcp;
5690  int i;
5691  int d;
5692  NumericDigit dig;
5693 
5694 #if DEC_DIGITS > 1
5695  NumericDigit d1;
5696 #endif
5697 
5698  dscale = var->dscale;
5699 
5700  /*
5701  * Allocate space for the result.
5702  *
5703  * i is set to the # of decimal digits before decimal point. dscale is the
5704  * # of decimal digits we will print after decimal point. We may generate
5705  * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
5706  * need room for sign, decimal point, null terminator.
5707  */
5708  i = (var->weight + 1) * DEC_DIGITS;
5709  if (i <= 0)
5710  i = 1;
5711 
5712  str = palloc(i + dscale + DEC_DIGITS + 2);
5713  cp = str;
5714 
5715  /*
5716  * Output a dash for negative values
5717  */
5718  if (var->sign == NUMERIC_NEG)
5719  *cp++ = '-';
5720 
5721  /*
5722  * Output all digits before the decimal point
5723  */
5724  if (var->weight < 0)
5725  {
5726  d = var->weight + 1;
5727  *cp++ = '0';
5728  }
5729  else
5730  {
5731  for (d = 0; d <= var->weight; d++)
5732  {
5733  dig = (d < var->ndigits) ? var->digits[d] : 0;
5734  /* In the first digit, suppress extra leading decimal zeroes */
5735 #if DEC_DIGITS == 4
5736  {
5737  bool putit = (d > 0);
5738 
5739  d1 = dig / 1000;
5740  dig -= d1 * 1000;
5741  putit |= (d1 > 0);
5742  if (putit)
5743  *cp++ = d1 + '0';
5744  d1 = dig / 100;
5745  dig -= d1 * 100;
5746  putit |= (d1 > 0);
5747  if (putit)
5748  *cp++ = d1 + '0';
5749  d1 = dig / 10;
5750  dig -= d1 * 10;
5751  putit |= (d1 > 0);
5752  if (putit)
5753  *cp++ = d1 + '0';
5754  *cp++ = dig + '0';
5755  }
5756 #elif DEC_DIGITS == 2
5757  d1 = dig / 10;
5758  dig -= d1 * 10;
5759  if (d1 > 0 || d > 0)
5760  *cp++ = d1 + '0';
5761  *cp++ = dig + '0';
5762 #elif DEC_DIGITS == 1
5763  *cp++ = dig + '0';
5764 #else
5765 #error unsupported NBASE
5766 #endif
5767  }
5768  }
5769 
5770  /*
5771  * If requested, output a decimal point and all the digits that follow it.
5772  * We initially put out a multiple of DEC_DIGITS digits, then truncate if
5773  * needed.
5774  */
5775  if (dscale > 0)
5776  {
5777  *cp++ = '.';
5778  endcp = cp + dscale;
5779  for (i = 0; i < dscale; d++, i += DEC_DIGITS)
5780  {
5781  dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
5782 #if DEC_DIGITS == 4
5783  d1 = dig / 1000;
5784  dig -= d1 * 1000;
5785  *cp++ = d1 + '0';
5786  d1 = dig / 100;
5787  dig -= d1 * 100;
5788  *cp++ = d1 + '0';
5789  d1 = dig / 10;
5790  dig -= d1 * 10;
5791  *cp++ = d1 + '0';
5792  *cp++ = dig + '0';
5793 #elif DEC_DIGITS == 2
5794  d1 = dig / 10;
5795  dig -= d1 * 10;
5796  *cp++ = d1 + '0';
5797  *cp++ = dig + '0';
5798 #elif DEC_DIGITS == 1
5799  *cp++ = dig + '0';
5800 #else
5801 #error unsupported NBASE
5802 #endif
5803  }
5804  cp = endcp;
5805  }
5806 
5807  /*
5808  * terminate the string and return it
5809  */
5810  *cp = '\0';
5811  return str;
5812 }
int weight
Definition: numeric.c:272
static void error(void)
Definition: sql-dyntest.c:147
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
int16 NumericDigit
Definition: numeric.c:99
#define NBASE
Definition: numeric.c:93
NumericDigit * digits
Definition: numeric.c:276
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define DEC_DIGITS
Definition: numeric.c:95
static char * get_str_from_var_sci ( NumericVar var,
int  rscale 
)
static

Definition at line 5837 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, div_var(), free_var(), get_str_from_var(), init_var, NumericVar::ndigits, palloc(), pfree(), power_var_int(), snprintf(), and NumericVar::weight.

Referenced by numeric_out_sci().

5838 {
5839  int32 exponent;
5840  NumericVar denominator;
5841  NumericVar significand;
5842  int denom_scale;
5843  size_t len;
5844  char *str;
5845  char *sig_out;
5846 
5847  if (rscale < 0)
5848  rscale = 0;
5849 
5850  /*
5851  * Determine the exponent of this number in normalised form.
5852  *
5853  * This is the exponent required to represent the number with only one
5854  * significant digit before the decimal place.
5855  */
5856  if (var->ndigits > 0)
5857  {
5858  exponent = (var->weight + 1) * DEC_DIGITS;
5859 
5860  /*
5861  * Compensate for leading decimal zeroes in the first numeric digit by
5862  * decrementing the exponent.
5863  */
5864  exponent -= DEC_DIGITS - (int) log10(var->digits[0]);
5865  }
5866  else
5867  {
5868  /*
5869  * If var has no digits, then it must be zero.
5870  *
5871  * Zero doesn't technically have a meaningful exponent in normalised
5872  * notation, but we just display the exponent as zero for consistency
5873  * of output.
5874  */
5875  exponent = 0;
5876  }
5877 
5878  /*
5879  * The denominator is set to 10 raised to the power of the exponent.
5880  *
5881  * We then divide var by the denominator to get the significand, rounding
5882  * to rscale decimal digits in the process.
5883  */
5884  if (exponent < 0)
5885  denom_scale = -exponent;
5886  else
5887  denom_scale = 0;
5888 
5889  init_var(&denominator);
5890  init_var(&significand);
5891 
5892  power_var_int(&const_ten, exponent, &denominator, denom_scale);
5893  div_var(var, &denominator, &significand, rscale, true);
5894  sig_out = get_str_from_var(&significand);
5895 
5896  free_var(&denominator);
5897  free_var(&significand);
5898 
5899  /*
5900  * Allocate space for the result.
5901  *
5902  * In addition to the significand, we need room for the exponent
5903  * decoration ("e"), the sign of the exponent, up to 10 digits for the
5904  * exponent itself, and of course the null terminator.
5905  */
5906  len = strlen(sig_out) + 13;
5907  str = palloc(len);
5908  snprintf(str, len, "%se%+03d", sig_out, exponent);
5909 
5910  pfree(sig_out);
5911 
5912  return str;
5913 }
int weight
Definition: numeric.c:272
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int ndigits
Definition: numeric.c:271
static void div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:6874
signed int int32
Definition: c.h:256
void pfree(void *pointer)
Definition: mcxt.c:950
static void power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
Definition: numeric.c:8158
static char * get_str_from_var(NumericVar *var)
Definition: numeric.c:5684
static void free_var(NumericVar *var)
Definition: numeric.c:5408
NumericDigit * digits
Definition: numeric.c:276
static NumericVar const_ten
Definition: numeric.c:384
void * palloc(Size size)
Definition: mcxt.c:849
#define DEC_DIGITS
Definition: numeric.c:95
#define init_var(v)
Definition: numeric.c:451
Datum hash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2158 of file numeric.c.

References Assert, digits, hash_any(), i, NUMERIC_DIGITS, NUMERIC_IS_NAN, NUMERIC_NDIGITS, NUMERIC_WEIGHT, PG_GETARG_NUMERIC, PG_RETURN_DATUM, PG_RETURN_UINT32, and result.

Referenced by JsonbHashScalarValue().

2159 {
2160  Numeric key = PG_GETARG_NUMERIC(0);
2161  Datum digit_hash;
2162  Datum result;
2163  int weight;
2164  int start_offset;
2165  int end_offset;
2166  int i;
2167  int hash_len;
2169 
2170  /* If it's NaN, don't try to hash the rest of the fields */
2171  if (NUMERIC_IS_NAN(key))
2172  PG_RETURN_UINT32(0);
2173 
2174  weight = NUMERIC_WEIGHT(key);
2175  start_offset = 0;
2176  end_offset = 0;
2177 
2178  /*
2179  * Omit any leading or trailing zeros from the input to the hash. The
2180  * numeric implementation *should* guarantee that leading and trailing
2181  * zeros are suppressed, but we're paranoid. Note that we measure the
2182  * starting and ending offsets in units of NumericDigits, not bytes.
2183  */
2184  digits = NUMERIC_DIGITS(key);
2185  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2186  {
2187  if (digits[i] != (NumericDigit) 0)
2188  break;
2189 
2190  start_offset++;
2191 
2192  /*
2193  * The weight is effectively the # of digits before the decimal point,
2194  * so decrement it for each leading zero we skip.
2195  */
2196  weight--;
2197  }
2198 
2199  /*
2200  * If there are no non-zero digits, then the value of the number is zero,
2201  * regardless of any other fields.
2202  */
2203  if (NUMERIC_NDIGITS(key) == start_offset)
2204  PG_RETURN_UINT32(-1);
2205 
2206  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2207  {
2208  if (digits[i] != (NumericDigit) 0)
2209  break;
2210 
2211  end_offset++;
2212  }
2213 
2214  /* If we get here, there should be at least one non-zero digit */
2215  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2216 
2217  /*
2218  * Note that we don't hash on the Numeric's scale, since two numerics can
2219  * compare equal but have different scales. We also don't hash on the
2220  * sign, although we could: since a sign difference implies inequality,
2221  * this shouldn't affect correctness.
2222  */
2223  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2224  digit_hash = hash_any((unsigned char *) (NUMERIC_DIGITS(key) + start_offset),
2225  hash_len * sizeof(NumericDigit));
2226 
2227  /* Mix in the weight, via XOR */
2228  result = digit_hash ^ weight;
2229 
2230  PG_RETURN_DATUM(result);
2231 }
return result
Definition: formatting.c:1633
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:315
#define NUMERIC_DIGITS(num)
Definition: numeric.c:453
int16 NumericDigit
Definition: numeric.c:99
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:455
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:214
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#define Assert(condition)
Definition: c.h:675
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
int i
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
int digits
Definition: informix.c:691
static void init_var_from_num ( Numeric  num,
NumericVar dest 
)
static

Definition at line 5641 of file numeric.c.

References NumericVar::buf, NumericVar::digits, NumericVar::dscale, NumericVar::ndigits, NULL, NUMERIC_DIGITS, NUMERIC_DSCALE, NUMERIC_NDIGITS, NUMERIC_SIGN, NUMERIC_WEIGHT, NumericVar::sign, and NumericVar::weight.

Referenced by compute_bucket(), do_numeric_accum(), do_numeric_discard(), generate_series_step_numeric(), int8_avg_deserialize(), numeric_abbrev_convert(), numeric_add(), numeric_avg_deserialize(), numeric_ceil(), numeric_deserialize(), numeric_div(), numeric_div_trunc(), numeric_exp(), numeric_floor(), numeric_inc(), numeric_int2(), numeric_int4(), numeric_int8(), numeric_ln(), numeric_log(), numeric_mod(), numeric_mul(), numeric_normalize(), numeric_out(), numeric_out_sci(), numeric_poly_deserialize(), numeric_power(), numeric_send(), numeric_sqrt(), and numeric_sub().

5642 {
5643  dest->ndigits = NUMERIC_NDIGITS(num);
5644  dest->weight = NUMERIC_WEIGHT(num);
5645  dest->sign = NUMERIC_SIGN(num);
5646  dest->dscale = NUMERIC_DSCALE(num);
5647  dest->digits = NUMERIC_DIGITS(num);
5648  dest->buf = NULL; /* digits array is not palloc'd */
5649 }
#define NUMERIC_DSCALE(n)
Definition: numeric.c:210
int weight
Definition: numeric.c:272
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
int sign
Definition: numeric.c:273
#define NUMERIC_DIGITS(num)
Definition: numeric.c:453
#define NUMERIC_SIGN(n)
Definition: numeric.c:206
NumericDigit * buf
Definition: numeric.c:275
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:455
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:214
#define NULL
Definition: c.h:229
NumericDigit * digits
Definition: numeric.c:276
Datum int2_accum ( PG_FUNCTION_ARGS  )

Definition at line 3939 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_accum(), int2_numeric(), makePolyNumAggState, newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_INT16, PG_GETARG_POINTER, and PG_RETURN_POINTER.

3940 {
3942 
3943  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
3944 
3945  /* Create the state data on the first call */
3946  if (state == NULL)
3947  state = makePolyNumAggState(fcinfo, true);
3948 
3949  if (!PG_ARGISNULL(1))
3950  {
3951 #ifdef HAVE_INT128
3952  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
3953 #else
3954  Numeric newval;
3955 
3957  PG_GETARG_DATUM(1)));
3958  do_numeric_accum(state, newval);
3959 #endif
3960  }
3961 
3962  PG_RETURN_POINTER(state);
3963 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Datum int2_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3012
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3249
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define makePolyNumAggState
Definition: numeric.c:3934
Datum int2_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4416 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_discard(), elog, ERROR, int2_numeric(), newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_INT16, PG_GETARG_POINTER, and PG_RETURN_POINTER.

4417 {
4419 
4420  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4421 
4422  /* Should not get here with no state */
4423  if (state == NULL)
4424  elog(ERROR, "int2_accum_inv called with NULL state");
4425 
4426  if (!PG_ARGISNULL(1))
4427  {
4428 #ifdef HAVE_INT128
4429  do_int128_discard(state, (int128) PG_GETARG_INT16(1));
4430 #else
4431  Numeric newval;
4432 
4434  PG_GETARG_DATUM(1)));
4435 
4436  /* Should never fail, all inputs have dscale 0 */
4437  if (!do_numeric_discard(state, newval))
4438  elog(ERROR, "do_numeric_discard failed unexpectedly");
4439 #endif
4440  }
4441 
4442  PG_RETURN_POINTER(state);
4443 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Datum int2_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3012
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3314
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
Datum int2_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 5107 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, newval, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT16, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

5108 {
5109  ArrayType *transarray;
5111  Int8TransTypeData *transdata;
5112 
5113  /*
5114  * If we're invoked as an aggregate, we can cheat and modify our first
5115  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5116  * a copy of it before scribbling on it.
5117  */
5118  if (AggCheckCallContext(fcinfo, NULL))
5119  transarray = PG_GETARG_ARRAYTYPE_P(0);
5120  else
5121  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5122 
5123  if (ARR_HASNULL(transarray) ||
5124  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5125  elog(ERROR, "expected 2-element int8 array");
5126 
5127  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5128  transdata->count++;
5129  transdata->sum += newval;
5130 
5131  PG_RETURN_ARRAYTYPE_P(transarray);
5132 }
signed short int16
Definition: c.h:255
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:245
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define NULL
Definition: c.h:229
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define elog
Definition: elog.h:219
Datum int2_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 5194 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, newval, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT16, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

5195 {
5196  ArrayType *transarray;
5198  Int8TransTypeData *transdata;
5199 
5200  /*
5201  * If we're invoked as an aggregate, we can cheat and modify our first
5202  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5203  * a copy of it before scribbling on it.
5204  */
5205  if (AggCheckCallContext(fcinfo, NULL))
5206  transarray = PG_GETARG_ARRAYTYPE_P(0);
5207  else
5208  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5209 
5210  if (ARR_HASNULL(transarray) ||
5211  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5212  elog(ERROR, "expected 2-element int8 array");
5213 
5214  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5215  transdata->count--;
5216  transdata->sum -= newval;
5217 
5218  PG_RETURN_ARRAYTYPE_P(transarray);
5219 }
signed short int16
Definition: c.h:255
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:245
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define NULL
Definition: c.h:229
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define elog
Definition: elog.h:219
Datum int2_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3012 of file numeric.c.

References free_var(), init_var, int64_to_numericvar(), make_result(), PG_GETARG_INT16, PG_RETURN_NUMERIC, result, and val.

Referenced by int2_accum(), and int2_accum_inv().

3013 {
3014  int16 val = PG_GETARG_INT16(0);
3015  Numeric res;
3017 
3018  init_var(&result);
3019 
3020  int64_to_numericvar((int64) val, &result);
3021 
3022  res = make_result(&result);
3023 
3024  free_var(&result);
3025 
3026  PG_RETURN_NUMERIC(res);
3027 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
signed short int16
Definition: c.h:255
return result
Definition: formatting.c:1633
static Numeric make_result(NumericVar *var)
Definition: numeric.c:5923
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:6157
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum int2_sum ( PG_FUNCTION_ARGS  )

Definition at line 4955 of file numeric.c.

References AggCheckCallContext(), newval, NULL, PG_ARGISNULL, PG_GETARG_INT16, PG_GETARG_INT64, PG_GETARG_POINTER, PG_RETURN_INT64, PG_RETURN_NULL, and PG_RETURN_POINTER.

4956 {
4957  int64 newval;
4958 
4959  if (PG_ARGISNULL(0))
4960  {
4961  /* No non-null input seen so far... */
4962  if (PG_ARGISNULL(1))
4963  PG_RETURN_NULL(); /* still no non-null */
4964  /* This is the first non-null input. */
4965  newval = (int64) PG_GETARG_INT16(1);
4966  PG_RETURN_INT64(newval);
4967  }
4968 
4969  /*
4970  * If we're invoked as an aggregate, we can cheat and modify our first
4971  * parameter in-place to avoid palloc overhead. If not, we need to return
4972  * the new value of the transition variable. (If int8 is pass-by-value,
4973  * then of course this is useless as well as incorrect, so just ifdef it
4974  * out.)
4975  */
4976 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
4977  if (AggCheckCallContext(fcinfo, NULL))
4978  {
4979  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
4980 
4981  /* Leave the running sum unchanged in the new input is null */
4982  if (!PG_ARGISNULL(1))
4983  *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
4984 
4985  PG_RETURN_POINTER(oldsum);
4986  }
4987  else
4988 #endif
4989  {
4990  int64 oldsum = PG_GETARG_INT64(0);
4991 
4992  /* Leave sum unchanged if new input is null. */
4993  if (PG_ARGISNULL(1))
4994  PG_RETURN_INT64(oldsum);
4995 
4996  /* OK to do the addition. */
4997  newval = oldsum + (int64) PG_GETARG_INT16(1);
4998 
4999  PG_RETURN_INT64(newval);
5000  }
5001 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum int2int4_sum ( PG_FUNCTION_ARGS  )

Definition at line 5279 of file numeric.c.

References ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, Int64GetDatumFast, PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_NULL, and Int8TransTypeData::sum.

5280 {
5281  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
5282  Int8TransTypeData *transdata;
5283 
5284  if (ARR_HASNULL(transarray) ||
5285  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5286  elog(ERROR, "expected 2-element int8 array");
5287  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5288 
5289  /* SQL defines SUM of no values to be NULL */
5290  if (transdata->count == 0)
5291  PG_RETURN_NULL();
5292 
5293  PG_RETURN_DATUM(Int64GetDatumFast(transdata->sum));
5294 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define Int64GetDatumFast(X)
Definition: postgres.h:781
#define elog
Definition: elog.h:219
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum int4_accum ( PG_FUNCTION_ARGS  )

Definition at line 3966 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_accum(), int4_numeric(), makePolyNumAggState, newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_INT32, PG_GETARG_POINTER, and PG_RETURN_POINTER.

3967 {
3969 
3970  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
3971 
3972  /* Create the state data on the first call */
3973  if (state == NULL)
3974  state = makePolyNumAggState(fcinfo, true);
3975 
3976  if (!PG_ARGISNULL(1))
3977  {
3978 #ifdef HAVE_INT128
3979  do_int128_accum(state, (int128) PG_GETARG_INT32(1));
3980 #else
3981  Numeric newval;
3982 
3984  PG_GETARG_DATUM(1)));
3985  do_numeric_accum(state, newval);
3986 #endif
3987  }
3988 
3989  PG_RETURN_POINTER(state);
3990 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3249
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2902
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define makePolyNumAggState
Definition: numeric.c:3934
Datum int4_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4446 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_discard(), elog, ERROR, int4_numeric(), newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_INT32, PG_GETARG_POINTER, and PG_RETURN_POINTER.

4447 {
4449 
4450  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4451 
4452  /* Should not get here with no state */
4453  if (state == NULL)
4454  elog(ERROR, "int4_accum_inv called with NULL state");
4455 
4456  if (!PG_ARGISNULL(1))
4457  {
4458 #ifdef HAVE_INT128
4459  do_int128_discard(state, (int128) PG_GETARG_INT32(1));
4460 #else
4461  Numeric newval;
4462 
4464  PG_GETARG_DATUM(1)));
4465 
4466  /* Should never fail, all inputs have dscale 0 */
4467  if (!do_numeric_discard(state, newval))
4468  elog(ERROR, "do_numeric_discard failed unexpectedly");
4469 #endif
4470  }
4471 
4472  PG_RETURN_POINTER(state);
4473 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3314
#define ERROR
Definition: elog.h:43
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2902
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
Datum int4_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 5135 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, newval, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT32, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

5136 {
5137  ArrayType *transarray;
5139  Int8TransTypeData *transdata;
5140 
5141  /*
5142  * If we're invoked as an aggregate, we can cheat and modify our first
5143  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5144  * a copy of it before scribbling on it.
5145  */
5146  if (AggCheckCallContext(fcinfo, NULL))
5147  transarray = PG_GETARG_ARRAYTYPE_P(0);
5148  else
5149  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5150 
5151  if (ARR_HASNULL(transarray) ||
5152  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5153  elog(ERROR, "expected 2-element int8 array");
5154 
5155  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5156  transdata->count++;
5157  transdata->sum += newval;
5158 
5159  PG_RETURN_ARRAYTYPE_P(transarray);
5160 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:245
signed int int32
Definition: c.h:256
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define NULL
Definition: c.h:229
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define elog
Definition: elog.h:219
Datum int4_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 5222 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, newval, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT32, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

5223 {
5224  ArrayType *transarray;
5226  Int8TransTypeData *transdata;
5227 
5228  /*
5229  * If we're invoked as an aggregate, we can cheat and modify our first
5230  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5231  * a copy of it before scribbling on it.
5232  */
5233  if (AggCheckCallContext(fcinfo, NULL))
5234  transarray = PG_GETARG_ARRAYTYPE_P(0);
5235  else
5236  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5237 
5238  if (ARR_HASNULL(transarray) ||
5239  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5240  elog(ERROR, "expected 2-element int8 array");
5241 
5242  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5243  transdata->count--;
5244  transdata->sum -= newval;
5245 
5246  PG_RETURN_ARRAYTYPE_P(transarray);
5247 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:245
signed int int32
Definition: c.h:256
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define NULL
Definition: c.h:229
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define elog
Definition: elog.h:219
Datum int4_avg_combine ( PG_FUNCTION_ARGS  )

Definition at line 5163 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, NULL, PG_GETARG_ARRAYTYPE_P, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

5164 {
5165  ArrayType *transarray1;
5166  ArrayType *transarray2;
5167  Int8TransTypeData *state1;
5168  Int8TransTypeData *state2;
5169 
5170  if (!AggCheckCallContext(fcinfo, NULL))
5171  elog(ERROR, "aggregate function called in non-aggregate context");
5172 
5173  transarray1 = PG_GETARG_ARRAYTYPE_P(0);
5174  transarray2 = PG_GETARG_ARRAYTYPE_P(1);
5175 
5176  if (ARR_HASNULL(transarray1) ||
5177  ARR_SIZE(transarray1) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5178  elog(ERROR, "expected 2-element int8 array");
5179 
5180  if (ARR_HASNULL(transarray2) ||
5181  ARR_SIZE(transarray2) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5182  elog(ERROR, "expected 2-element int8 array");
5183 
5184  state1 = (Int8TransTypeData *) ARR_DATA_PTR(transarray1);
5185  state2 = (Int8TransTypeData *) ARR_DATA_PTR(transarray2);
5186 
5187  state1->count += state2->count;
5188  state1->sum += state2->sum;
5189 
5190  PG_RETURN_ARRAYTYPE_P(transarray1);
5191 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define NULL
Definition: c.h:229
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define elog
Definition: elog.h:219
Datum int4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2902 of file numeric.c.

References free_var(), init_var, int64_to_numericvar(), make_result(), PG_GETARG_INT32, PG_RETURN_NUMERIC, result, and val.

Referenced by gbt_numeric_penalty(), int4_accum(), int4_accum_inv(), numeric_to_char(), and numeric_to_number().

2903 {
2904  int32 val = PG_GETARG_INT32(0);
2905  Numeric res;
2907 
2908  init_var(&result);
2909 
2910  int64_to_numericvar((int64) val, &result);
2911 
2912  res = make_result(&result);
2913 
2914  free_var(&result);
2915 
2916  PG_RETURN_NUMERIC(res);
2917 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
return result
Definition: formatting.c:1633
signed int int32
Definition: c.h:256
static Numeric make_result(NumericVar *var)
Definition: numeric.c:5923
static void free_var(NumericVar *var)
Definition: numeric.c:5408
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:6157
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum int4_sum ( PG_FUNCTION_ARGS  )

Definition at line 5004 of file numeric.c.

References AggCheckCallContext(), newval, NULL, PG_ARGISNULL, PG_GETARG_INT32, PG_GETARG_INT64, PG_GETARG_POINTER, PG_RETURN_INT64, PG_RETURN_NULL, and PG_RETURN_POINTER.

5005 {
5006  int64 newval;
5007 
5008  if (PG_ARGISNULL(0))
5009  {
5010  /* No non-null input seen so far... */
5011  if (PG_ARGISNULL(1))
5012  PG_RETURN_NULL(); /* still no non-null */
5013  /* This is the first non-null input. */
5014  newval = (int64) PG_GETARG_INT32(1);
5015  PG_RETURN_INT64(newval);
5016  }
5017 
5018  /*
5019  * If we're invoked as an aggregate, we can cheat and modify our first
5020  * parameter in-place to avoid palloc overhead. If not, we need to return
5021  * the new value of the transition variable. (If int8 is pass-by-value,
5022  * then of course this is useless as well as incorrect, so just ifdef it
5023  * out.)
5024  */
5025 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
5026  if (AggCheckCallContext(fcinfo, NULL))
5027  {
5028  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
5029 
5030  /* Leave the running sum unchanged in the new input is null */
5031  if (!PG_ARGISNULL(1))
5032  *oldsum = *oldsum + (int64) PG_GETARG_INT32(1);
5033 
5034  PG_RETURN_POINTER(oldsum);
5035  }
5036  else
5037 #endif
5038  {
5039  int64 oldsum = PG_GETARG_INT64(0);
5040 
5041  /* Leave sum unchanged if new input is null. */
5042  if (PG_ARGISNULL(1))
5043  PG_RETURN_INT64(oldsum);
5044 
5045  /* OK to do the addition. */
5046  newval = oldsum + (int64) PG_GETARG_INT32(1);
5047 
5048  PG_RETURN_INT64(newval);
5049  }
5050 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
#define PG_RETURN_NULL()
Definition: fmgr.h:305
static void int64_to_numericvar ( int64  val,
NumericVar var 
)
static

Definition at line 6157 of file numeric.c.

References alloc_var(), DEC_DIGITS, NumericVar::digits, NumericVar::dscale, NBASE, NumericVar::ndigits, NUMERIC_NEG, NUMERIC_POS, NumericVar::sign, val, and NumericVar::weight.

Referenced by int2_numeric(), int4_numeric(), int8_numeric(), numeric_fac(), numeric_stddev_internal(), and width_bucket_numeric().

6158 {
6159  uint64 uval,
6160  newuval;
6161  NumericDigit *ptr;
6162  int ndigits;
6163 
6164  /* int64 can require at most 19 decimal digits; add one for safety */
6165  alloc_var(var, 20 / DEC_DIGITS);
6166  if (val < 0)
6167  {
6168  var->sign = NUMERIC_NEG;
6169  uval = -val;
6170  }
6171  else
6172  {
6173  var->sign = NUMERIC_POS;
6174  uval = val;
6175  }
6176  var->dscale = 0;
6177  if (val == 0)
6178  {
6179  var->ndigits = 0;
6180  var->weight = 0;
6181  return;
6182  }
6183  ptr = var->digits + var->ndigits;
6184  ndigits = 0;
6185  do
6186  {
6187  ptr--;
6188  ndigits++;
6189  newuval = uval / NBASE;
6190  *ptr = uval - newuval * NBASE;
6191  uval = newuval;
6192  } while (uval);
6193  var->digits = ptr;
6194  var->ndigits = ndigits;
6195  var->weight = ndigits - 1;
6196 }
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
int16 NumericDigit
Definition: numeric.c:99
#define NBASE
Definition: numeric.c:93
NumericDigit * digits
Definition: numeric.c:276
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:5392
#define DEC_DIGITS
Definition: numeric.c:95
long val
Definition: informix.c:689
Datum int8_accum ( PG_FUNCTION_ARGS  )

Definition at line 3993 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_accum(), int8_numeric(), makeNumericAggState(), newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, and PG_RETURN_POINTER.

3994 {
3996 
3997  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
3998 
3999  /* Create the state data on the first call */
4000  if (state == NULL)
4001  state = makeNumericAggState(fcinfo, true);
4002 
4003  if (!PG_ARGISNULL(1))
4004  {
4005  Numeric newval;
4006 
4008  PG_GETARG_DATUM(1)));
4009  do_numeric_accum(state, newval);
4010  }
4011 
4012  PG_RETURN_POINTER(state);
4013 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2968
static NumericAggState * makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
Definition: numeric.c:3209
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3249
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
Datum int8_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4476 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_discard(), elog, ERROR, int8_numeric(), newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, and PG_RETURN_POINTER.

4477 {
4479 
4480  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
4481 
4482  /* Should not get here with no state */
4483  if (state == NULL)
4484  elog(ERROR, "int8_accum_inv called with NULL state");
4485 
4486  if (!PG_ARGISNULL(1))
4487  {
4488  Numeric newval;
4489 
4491  PG_GETARG_DATUM(1)));
4492 
4493  /* Should never fail, all inputs have dscale 0 */
4494  if (!do_numeric_discard(state, newval))
4495  elog(ERROR, "do_numeric_discard failed unexpectedly");
4496  }
4497 
4498  PG_RETURN_POINTER(state);
4499 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3314
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2968
#define ERROR
Definition: elog.h:43
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
Datum int8_avg ( PG_FUNCTION_ARGS  )

Definition at line 5250 of file numeric.c.

References ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, DirectFunctionCall1, DirectFunctionCall2, elog, ERROR, Int64GetDatumFast, int8_numeric(), numeric_div(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_NULL, and Int8TransTypeData::sum.

5251 {
5252  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
5253  Int8TransTypeData *transdata;
5254  Datum countd,
5255  sumd;
5256 
5257  if (ARR_HASNULL(transarray) ||
5258  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5259  elog(ERROR, "expected 2-element int8 array");
5260  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5261 
5262  /* SQL defines AVG of no values to be NULL */
5263  if (transdata->count == 0)
5264  PG_RETURN_NULL();
5265 
5267  Int64GetDatumFast(transdata->count));
5269  Int64GetDatumFast(transdata->sum));
5270 
5272 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2968
Datum numeric_div(PG_FUNCTION_ARGS)
Definition: numeric.c:2366
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define Int64GetDatumFast(X)
Definition: postgres.h:781
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum int8_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 4219 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_accum(), int8_numeric(), makePolyNumAggState, newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_INT64, PG_GETARG_POINTER, and PG_RETURN_POINTER.

4220 {
4222 
4223  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4224 
4225  /* Create the state data on the first call */
4226  if (state == NULL)
4227  state = makePolyNumAggState(fcinfo, false);
4228 
4229  if (!PG_ARGISNULL(1))
4230  {
4231 #ifdef HAVE_INT128
4232  do_int128_accum(state, (int128) PG_GETARG_INT64(1));
4233 #else
4234  Numeric newval;
4235 
4237  PG_GETARG_DATUM(1)));
4238  do_numeric_accum(state, newval);
4239 #endif
4240  }
4241 
4242  PG_RETURN_POINTER(state);
4243 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2968
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3249
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
#define makePolyNumAggState
Definition: numeric.c:3934
Datum int8_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4502 of file numeric.c.

References DatumGetNumeric, DirectFunctionCall1, do_numeric_discard(), elog, ERROR, int8_numeric(), newval, NULL, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_INT64, PG_GETARG_POINTER, and PG_RETURN_POINTER.

4503 {
4505 
4506  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4507 
4508  /* Should not get here with no state */
4509  if (state == NULL)
4510  elog(ERROR, "int8_avg_accum_inv called with NULL state");
4511 
4512  if (!PG_ARGISNULL(1))
4513  {
4514 #ifdef HAVE_INT128
4515  do_int128_discard(state, (int128) PG_GETARG_INT64(1));
4516 #else
4517  Numeric newval;
4518 
4520  PG_GETARG_DATUM(1)));
4521 
4522  /* Should never fail, all inputs have dscale 0 */
4523  if (!do_numeric_discard(state, newval))
4524  elog(ERROR, "do_numeric_discard failed unexpectedly");
4525 #endif
4526  }
4527 
4528  PG_RETURN_POINTER(state);
4529 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3314
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2968
#define ERROR
Definition: elog.h:43
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum int8_avg_combine ( PG_FUNCTION_ARGS  )

Definition at line 4250 of file numeric.c.

References accum_sum_combine(), accum_sum_copy(), AggCheckCallContext(), elog, ERROR, makePolyNumAggState, MemoryContextSwitchTo(), NumericAggState::N, NULL, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_POINTER, and NumericAggState::sumX.

4251 {
4252  PolyNumAggState *state1;
4253  PolyNumAggState *state2;
4254  MemoryContext agg_context;
4255  MemoryContext old_context;
4256 
4257  if (!AggCheckCallContext(fcinfo, &agg_context))
4258  elog(ERROR, "aggregate function called in non-aggregate context");
4259 
4260  state1 = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4261  state2 = PG_ARGISNULL(1) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(1);
4262 
4263  if (state2 == NULL)
4264  PG_RETURN_POINTER(state1);
4265 
4266  /* manually copy all fields from state2 to state1 */
4267  if (state1 == NULL)
4268  {
4269  old_context = MemoryContextSwitchTo(agg_context);
4270 
4271  state1 = makePolyNumAggState(fcinfo, false);
4272  state1->N = state2->N;
4273 
4274 #ifdef HAVE_INT128
4275  state1->sumX = state2->sumX;
4276 #else
4277  accum_sum_copy(&state1->sumX, &state2->sumX);
4278 #endif
4279  MemoryContextSwitchTo(old_context);
4280 
4281  PG_RETURN_POINTER(state1);
4282  }
4283 
4284  if (state2->N > 0)
4285  {
4286  state1->N += state2->N;
4287 
4288 #ifdef HAVE_INT128
4289  state1->sumX += state2->sumX;
4290 #else
4291  /* The rest of this needs to work in the aggregate context */
4292  old_context = MemoryContextSwitchTo(agg_context);
4293 
4294  /* Accumulate sums */
4295  accum_sum_combine(&state1->sumX, &state2->sumX);
4296 
4297  MemoryContextSwitchTo(old_context);
4298 #endif
4299 
4300  }
4301  PG_RETURN_POINTER(state1);
4302 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define ERROR
Definition: elog.h:43
static void accum_sum_copy(NumericSumAccum *dst, NumericSumAccum *src)
Definition: numeric.c:9079
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
static void accum_sum_combine(NumericSumAccum *accum, NumericSumAccum *accum2)
Definition: numeric.c:9096
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define elog
Definition: elog.h:219
NumericSumAccum sumX
Definition: numeric.c:3197
#define makePolyNumAggState
Definition: numeric.c:3934
Datum int8_avg_deserialize ( PG_FUNCTION_ARGS  )

Definition at line 4367 of file numeric.c.

References accum_sum_add(), AggCheckCallContext(), appendBinaryStringInfo(), buf, StringInfoData::data, DatumGetNumeric, DirectFunctionCall3, elog, ERROR, init_var_from_num(), initStringInfo(), InvalidOid, makePolyNumAggStateCurrentContext, NumericAggState::N, NULL, numeric_recv(), pfree(), PG_GETARG_BYTEA_PP, PG_RETURN_POINTER, PointerGetDatum, pq_getmsgend(), pq_getmsgint64(), result, NumericAggState::sumX, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4368 {
4369  bytea *sstate;
4372  Datum temp;
4373  NumericVar num;
4374 
4375  if (!AggCheckCallContext(fcinfo, NULL))
4376  elog(ERROR, "aggregate function called in non-aggregate context");
4377 
4378  sstate = PG_GETARG_BYTEA_PP(0);
4379 
4380  /*
4381  * Copy the bytea into a StringInfo so that we can "receive" it using the
4382  * standard recv-function infrastructure.
4383  */
4384  initStringInfo(&buf);
4386  VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
4387 
4388  result = makePolyNumAggStateCurrentContext(false);
4389 
4390  /* N */
4391  result->N = pq_getmsgint64(&buf);
4392 
4393  /* sumX */
4395  PointerGetDatum(&buf),
4396  InvalidOid,
4397  -1);
4398  init_var_from_num(DatumGetNumeric(temp), &num);
4399 #ifdef HAVE_INT128
4400  numericvar_to_int128(&num, &result->sumX);
4401 #else
4402  accum_sum_add(&result->sumX, &num);
4403 #endif
4404 
4405  pq_getmsgend(&buf);
4406  pfree(buf.data);
4407 
4408  PG_RETURN_POINTER(result);
4409 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define VARDATA_ANY(PTR)
Definition: postgres.h:347