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 (const NumericVar *value, NumericVar *dest)
 
static char * get_str_from_var (const NumericVar *var)
 
static char * get_str_from_var_sci (const NumericVar *var, int rscale)
 
static Numeric make_result (const NumericVar *var)
 
static void apply_typmod (NumericVar *var, int32 typmod)
 
static int32 numericvar_to_int32 (const NumericVar *var)
 
static bool numericvar_to_int64 (const 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 (const 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 (const NumericVar *var, NumericSortSupport *nss)
 
static int cmp_numerics (Numeric num1, Numeric num2)
 
static int cmp_var (const NumericVar *var1, const 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 (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void sub_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void mul_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
 
static void div_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
 
static void div_var_fast (const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
 
static int select_div_scale (const NumericVar *var1, const NumericVar *var2)
 
static void mod_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void ceil_var (const NumericVar *var, NumericVar *result)
 
static void floor_var (const NumericVar *var, NumericVar *result)
 
static void sqrt_var (const NumericVar *arg, NumericVar *result, int rscale)
 
static void exp_var (const NumericVar *arg, NumericVar *result, int rscale)
 
static int estimate_ln_dweight (const NumericVar *var)
 
static void ln_var (const NumericVar *arg, NumericVar *result, int rscale)
 
static void log_var (const NumericVar *base, const NumericVar *num, NumericVar *result)
 
static void power_var (const NumericVar *base, const NumericVar *exp, NumericVar *result)
 
static void power_var_int (const NumericVar *base, int exp, NumericVar *result, int rscale)
 
static int cmp_abs (const NumericVar *var1, const 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 (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void sub_abs (const NumericVar *var1, const 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, const NumericVar *count_var, NumericVar *result_var)
 
static void accum_sum_add (NumericSumAccum *accum, const NumericVar *var1)
 
static void accum_sum_rescale (NumericSumAccum *accum, const 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 hash_numeric_extended (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 const NumericDigit const_zero_data [1] = {0}
 
static const NumericVar const_zero
 
static const NumericDigit const_one_data [1] = {1}
 
static const NumericVar const_one
 
static const NumericDigit const_two_data [1] = {2}
 
static const NumericVar const_two
 
static const NumericDigit const_ten_data [1] = {10}
 
static const NumericVar const_ten
 
static const NumericDigit const_zero_point_five_data [1] = {5000}
 
static const NumericVar const_zero_point_five
 
static const NumericDigit const_zero_point_nine_data [1] = {9000}
 
static const NumericVar const_zero_point_nine
 
static const NumericDigit const_one_point_one_data [2] = {1, 1000}
 
static const NumericVar const_one_point_one
 
static const 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:949
static char * buf
Definition: pg_test_fsync.c:67

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 4005 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(), hash_numeric_extended(), 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:245
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:181
#define VARHDRSZ
Definition: c.h:439
unsigned short uint16
Definition: c.h:257

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(), hash_numeric_extended(), 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 4003 of file numeric.c.

Function Documentation

static void accum_sum_add ( NumericSumAccum accum,
const NumericVar var1 
)
static

Definition at line 8888 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().

8889 {
8890  int32 *accum_digits;
8891  int i,
8892  val_i;
8893  int val_ndigits;
8894  NumericDigit *val_digits;
8895 
8896  /*
8897  * If we have accumulated too many values since the last carry
8898  * propagation, do it now, to avoid overflowing. (We could allow more
8899  * than NBASE - 1, if we reserved two extra digits, rather than one, for
8900  * carry propagation. But even with NBASE - 1, this needs to be done so
8901  * seldom, that the performance difference is negligible.)
8902  */
8903  if (accum->num_uncarried == NBASE - 1)
8904  accum_sum_carry(accum);
8905 
8906  /*
8907  * Adjust the weight or scale of the old value, so that it can accommodate
8908  * the new value.
8909  */
8910  accum_sum_rescale(accum, val);
8911 
8912  /* */
8913  if (val->sign == NUMERIC_POS)
8914  accum_digits = accum->pos_digits;
8915  else
8916  accum_digits = accum->neg_digits;
8917 
8918  /* copy these values into local vars for speed in loop */
8919  val_ndigits = val->ndigits;
8920  val_digits = val->digits;
8921 
8922  i = accum->weight - val->weight;
8923  for (val_i = 0; val_i < val_ndigits; val_i++)
8924  {
8925  accum_digits[i] += (int32) val_digits[val_i];
8926  i++;
8927  }
8928 
8929  accum->num_uncarried++;
8930 }
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:9009
#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:8936
signed int int32
Definition: c.h:246
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 8936 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().

8937 {
8938  int i;
8939  int ndigits;
8940  int32 *dig;
8941  int32 carry;
8942  int32 newdig = 0;
8943 
8944  /*
8945  * If no new values have been added since last carry propagation, nothing
8946  * to do.
8947  */
8948  if (accum->num_uncarried == 0)
8949  return;
8950 
8951  /*
8952  * We maintain that the weight of the accumulator is always one larger
8953  * than needed to hold the current value, before carrying, to make sure
8954  * there is enough space for the possible extra digit when carry is
8955  * propagated. We cannot expand the buffer here, unless we require
8956  * callers of accum_sum_final() to switch to the right memory context.
8957  */
8958  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
8959 
8960  ndigits = accum->ndigits;
8961 
8962  /* Propagate carry in the positive sum */
8963  dig = accum->pos_digits;
8964  carry = 0;
8965  for (i = ndigits - 1; i >= 0; i--)
8966  {
8967  newdig = dig[i] + carry;
8968  if (newdig >= NBASE)
8969  {
8970  carry = newdig / NBASE;
8971  newdig -= carry * NBASE;
8972  }
8973  else
8974  carry = 0;
8975  dig[i] = newdig;
8976  }
8977  /* Did we use up the digit reserved for carry propagation? */
8978  if (newdig > 0)
8979  accum->have_carry_space = false;
8980 
8981  /* And the same for the negative sum */
8982  dig = accum->neg_digits;
8983  carry = 0;
8984  for (i = ndigits - 1; i >= 0; i--)
8985  {
8986  newdig = dig[i] + carry;
8987  if (newdig >= NBASE)
8988  {
8989  carry = newdig / NBASE;
8990  newdig -= carry * NBASE;
8991  }
8992  else
8993  carry = 0;
8994  dig[i] = newdig;
8995  }
8996  if (newdig > 0)
8997  accum->have_carry_space = false;
8998 
8999  accum->num_uncarried = 0;
9000 }
int32 * neg_digits
Definition: numeric.c:344
int num_uncarried
Definition: numeric.c:341
signed int int32
Definition: c.h:246
bool have_carry_space
Definition: numeric.c:342
#define NBASE
Definition: numeric.c:93
#define Assert(condition)
Definition: c.h:664
int i
int32 * pos_digits
Definition: numeric.c:343
static void accum_sum_combine ( NumericSumAccum accum,
NumericSumAccum accum2 
)
static

Definition at line 9166 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().

9167 {
9168  NumericVar tmp_var;
9169 
9170  init_var(&tmp_var);
9171 
9172  accum_sum_final(accum2, &tmp_var);
9173  accum_sum_add(accum, &tmp_var);
9174 
9175  free_var(&tmp_var);
9176 }
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:9098
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:8888
static void free_var(NumericVar *var)
Definition: numeric.c:5478
#define init_var(v)
Definition: numeric.c:451
static void accum_sum_copy ( NumericSumAccum dst,
NumericSumAccum src 
)
static

Definition at line 9149 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().

9150 {
9151  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
9152  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
9153 
9154  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
9155  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
9156  dst->num_uncarried = src->num_uncarried;
9157  dst->ndigits = src->ndigits;
9158  dst->weight = src->weight;
9159  dst->dscale = src->dscale;
9160 }
int32 * neg_digits
Definition: numeric.c:344
int num_uncarried
Definition: numeric.c:341
signed int int32
Definition: c.h:246
void * palloc(Size size)
Definition: mcxt.c:848
int32 * pos_digits
Definition: numeric.c:343
static void accum_sum_final ( NumericSumAccum accum,
NumericVar result 
)
static

Definition at line 9098 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().

9099 {
9100  int i;
9101  NumericVar pos_var;
9102  NumericVar neg_var;
9103 
9104  if (accum->ndigits == 0)
9105  {
9106  set_var_from_var(&const_zero, result);
9107  return;
9108  }
9109 
9110  /* Perform final carry */
9111  accum_sum_carry(accum);
9112 
9113  /* Create NumericVars representing the positive and negative sums */
9114  init_var(&pos_var);
9115  init_var(&neg_var);
9116 
9117  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
9118  pos_var.weight = neg_var.weight = accum->weight;
9119  pos_var.dscale = neg_var.dscale = accum->dscale;
9120  pos_var.sign = NUMERIC_POS;
9121  neg_var.sign = NUMERIC_NEG;
9122 
9123  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
9124  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
9125 
9126  for (i = 0; i < accum->ndigits; i++)
9127  {
9128  Assert(accum->pos_digits[i] < NBASE);
9129  pos_var.digits[i] = (int16) accum->pos_digits[i];
9130 
9131  Assert(accum->neg_digits[i] < NBASE);
9132  neg_var.digits[i] = (int16) accum->neg_digits[i];
9133  }
9134 
9135  /* And add them together */
9136  add_var(&pos_var, &neg_var, result);
9137 
9138  /* Remove leading/trailing zeroes */
9139  strip_var(result);
9140 }
signed short int16
Definition: c.h:245
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
static void strip_var(NumericVar *var)
Definition: numeric.c:8831
int32 * neg_digits
Definition: numeric.c:344
#define digitbuf_alloc(ndigits)
Definition: numeric.c:443
int ndigits
Definition: numeric.c:271
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:8936
int dscale
Definition: numeric.c:274
#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
static const NumericVar const_zero
Definition: numeric.c:371
#define Assert(condition)
Definition: c.h:664
NumericDigit * digits
Definition: numeric.c:276
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6509
int i
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:5728
int32 * pos_digits
Definition: numeric.c:343
#define init_var(v)
Definition: numeric.c:451
static void accum_sum_rescale ( NumericSumAccum accum,
const NumericVar val 
)
static

Definition at line 9009 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().

9010 {
9011  int old_weight = accum->weight;
9012  int old_ndigits = accum->ndigits;
9013  int accum_ndigits;
9014  int accum_weight;
9015  int accum_rscale;
9016  int val_rscale;
9017 
9018  accum_weight = old_weight;
9019  accum_ndigits = old_ndigits;
9020 
9021  /*
9022  * Does the new value have a larger weight? If so, enlarge the buffers,
9023  * and shift the existing value to the new weight, by adding leading
9024  * zeros.
9025  *
9026  * We enforce that the accumulator always has a weight one larger than
9027  * needed for the inputs, so that we have space for an extra digit at the
9028  * final carry-propagation phase, if necessary.
9029  */
9030  if (val->weight >= accum_weight)
9031  {
9032  accum_weight = val->weight + 1;
9033  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
9034  }
9035 
9036  /*
9037  * Even though the new value is small, we might've used up the space
9038  * reserved for the carry digit in the last call to accum_sum_carry(). If
9039  * so, enlarge to make room for another one.
9040  */
9041  else if (!accum->have_carry_space)
9042  {
9043  accum_weight++;
9044  accum_ndigits++;
9045  }
9046 
9047  /* Is the new value wider on the right side? */
9048  accum_rscale = accum_ndigits - accum_weight - 1;
9049  val_rscale = val->ndigits - val->weight - 1;
9050  if (val_rscale > accum_rscale)
9051  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
9052 
9053  if (accum_ndigits != old_ndigits ||
9054  accum_weight != old_weight)
9055  {
9056  int32 *new_pos_digits;
9057  int32 *new_neg_digits;
9058  int weightdiff;
9059 
9060  weightdiff = accum_weight - old_weight;
9061 
9062  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
9063  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
9064 
9065  if (accum->pos_digits)
9066  {
9067  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
9068  old_ndigits * sizeof(int32));
9069  pfree(accum->pos_digits);
9070 
9071  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
9072  old_ndigits * sizeof(int32));
9073  pfree(accum->neg_digits);
9074  }
9075 
9076  accum->pos_digits = new_pos_digits;
9077  accum->neg_digits = new_neg_digits;
9078 
9079  accum->weight = accum_weight;
9080  accum->ndigits = accum_ndigits;
9081 
9082  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
9083  accum->have_carry_space = true;
9084  }
9085 
9086  if (val->dscale > accum->dscale)
9087  accum->dscale = val->dscale;
9088 }
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:246
void pfree(void *pointer)
Definition: mcxt.c:949
bool have_carry_space
Definition: numeric.c:342
void * palloc0(Size size)
Definition: mcxt.c:877
#define Assert(condition)
Definition: c.h:664
int32 * pos_digits
Definition: numeric.c:343
static void accum_sum_reset ( NumericSumAccum accum)
static

Definition at line 8872 of file numeric.c.

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

Referenced by do_numeric_discard().

8873 {
8874  int i;
8875 
8876  accum->dscale = 0;
8877  for (i = 0; i < accum->ndigits; i++)
8878  {
8879  accum->pos_digits[i] = 0;
8880  accum->neg_digits[i] = 0;
8881  }
8882 }
int32 * neg_digits
Definition: numeric.c:344
int i
int32 * pos_digits
Definition: numeric.c:343
static void add_abs ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

Definition at line 8496 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().

8497 {
8498  NumericDigit *res_buf;
8499  NumericDigit *res_digits;
8500  int res_ndigits;
8501  int res_weight;
8502  int res_rscale,
8503  rscale1,
8504  rscale2;
8505  int res_dscale;
8506  int i,
8507  i1,
8508  i2;
8509  int carry = 0;
8510 
8511  /* copy these values into local vars for speed in inner loop */
8512  int var1ndigits = var1->ndigits;
8513  int var2ndigits = var2->ndigits;
8514  NumericDigit *var1digits = var1->digits;
8515  NumericDigit *var2digits = var2->digits;
8516 
8517  res_weight = Max(var1->weight, var2->weight) + 1;
8518 
8519  res_dscale = Max(var1->dscale, var2->dscale);
8520 
8521  /* Note: here we are figuring rscale in base-NBASE digits */
8522  rscale1 = var1->ndigits - var1->weight - 1;
8523  rscale2 = var2->ndigits - var2->weight - 1;
8524  res_rscale = Max(rscale1, rscale2);
8525 
8526  res_ndigits = res_rscale + res_weight + 1;
8527  if (res_ndigits <= 0)
8528  res_ndigits = 1;
8529 
8530  res_buf = digitbuf_alloc(res_ndigits + 1);
8531  res_buf[0] = 0; /* spare digit for later rounding */
8532  res_digits = res_buf + 1;
8533 
8534  i1 = res_rscale + var1->weight + 1;
8535  i2 = res_rscale + var2->weight + 1;
8536  for (i = res_ndigits - 1; i >= 0; i--)
8537  {
8538  i1--;
8539  i2--;
8540  if (i1 >= 0 && i1 < var1ndigits)
8541  carry += var1digits[i1];
8542  if (i2 >= 0 && i2 < var2ndigits)
8543  carry += var2digits[i2];
8544 
8545  if (carry >= NBASE)
8546  {
8547  res_digits[i] = carry - NBASE;
8548  carry = 1;
8549  }
8550  else
8551  {
8552  res_digits[i] = carry;
8553  carry = 0;
8554  }
8555  }
8556 
8557  Assert(carry == 0); /* else we failed to allow for carry out */
8558 
8559  digitbuf_free(result->buf);
8560  result->ndigits = res_ndigits;
8561  result->buf = res_buf;
8562  result->digits = res_digits;
8563  result->weight = res_weight;
8564  result->dscale = res_dscale;
8565 
8566  /* Remove leading/trailing zeroes */
8567  strip_var(result);
8568 }
int weight
Definition: numeric.c:272
static void strip_var(NumericVar *var)
Definition: numeric.c:8831
#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:664
NumericDigit * digits
Definition: numeric.c:276
int i
#define Max(x, y)
Definition: numeric.c:11
static void add_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

Definition at line 6509 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().

6510 {
6511  /*
6512  * Decide on the signs of the two variables what to do
6513  */
6514  if (var1->sign == NUMERIC_POS)
6515  {
6516  if (var2->sign == NUMERIC_POS)
6517  {
6518  /*
6519  * Both are positive result = +(ABS(var1) + ABS(var2))
6520  */
6521  add_abs(var1, var2, result);
6522  result->sign = NUMERIC_POS;
6523  }
6524  else
6525  {
6526  /*
6527  * var1 is positive, var2 is negative Must compare absolute values
6528  */
6529  switch (cmp_abs(var1, var2))
6530  {
6531  case 0:
6532  /* ----------
6533  * ABS(var1) == ABS(var2)
6534  * result = ZERO
6535  * ----------
6536  */
6537  zero_var(result);
6538  result->dscale = Max(var1->dscale, var2->dscale);
6539  break;
6540 
6541  case 1:
6542  /* ----------
6543  * ABS(var1) > ABS(var2)
6544  * result = +(ABS(var1) - ABS(var2))
6545  * ----------
6546  */
6547  sub_abs(var1, var2, result);
6548  result->sign = NUMERIC_POS;
6549  break;
6550 
6551  case -1:
6552  /* ----------
6553  * ABS(var1) < ABS(var2)
6554  * result = -(ABS(var2) - ABS(var1))
6555  * ----------
6556  */
6557  sub_abs(var2, var1, result);
6558  result->sign = NUMERIC_NEG;
6559  break;
6560  }
6561  }
6562  }
6563  else
6564  {
6565  if (var2->sign == NUMERIC_POS)
6566  {
6567  /* ----------
6568  * var1 is negative, var2 is positive
6569  * Must compare absolute values
6570  * ----------
6571  */
6572  switch (cmp_abs(var1, var2))
6573  {
6574  case 0:
6575  /* ----------
6576  * ABS(var1) == ABS(var2)
6577  * result = ZERO
6578  * ----------
6579  */
6580  zero_var(result);
6581  result->dscale = Max(var1->dscale, var2->dscale);
6582  break;
6583 
6584  case 1:
6585  /* ----------
6586  * ABS(var1) > ABS(var2)
6587  * result = -(ABS(var1) - ABS(var2))
6588  * ----------
6589  */
6590  sub_abs(var1, var2, result);
6591  result->sign = NUMERIC_NEG;
6592  break;
6593 
6594  case -1:
6595  /* ----------
6596  * ABS(var1) < ABS(var2)
6597  * result = +(ABS(var2) - ABS(var1))
6598  * ----------
6599  */
6600  sub_abs(var2, var1, result);
6601  result->sign = NUMERIC_POS;
6602  break;
6603  }
6604  }
6605  else
6606  {
6607  /* ----------
6608  * Both are negative
6609  * result = -(ABS(var1) + ABS(var2))
6610  * ----------
6611  */
6612  add_abs(var1, var2, result);
6613  result->sign = NUMERIC_NEG;
6614  }
6615  }
6616 }
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8581
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8496
#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:5494
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:8418
#define Max(x, y)
Definition: numeric.c:11
static void alloc_var ( NumericVar var,
int  ndigits 
)
static

Definition at line 5462 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().

5463 {
5464  digitbuf_free(var->buf);
5465  var->buf = digitbuf_alloc(ndigits + 1);
5466  var->buf[0] = 0; /* spare digit for rounding */
5467  var->digits = var->buf + 1;
5468  var->ndigits = ndigits;
5469 }
#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 6080 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().

6081 {
6082  int precision;
6083  int scale;
6084  int maxdigits;
6085  int ddigits;
6086  int i;
6087 
6088  /* Do nothing if we have a default typmod (-1) */
6089  if (typmod < (int32) (VARHDRSZ))
6090  return;
6091 
6092  typmod -= VARHDRSZ;
6093  precision = (typmod >> 16) & 0xffff;
6094  scale = typmod & 0xffff;
6095  maxdigits = precision - scale;
6096 
6097  /* Round to target scale (and set var->dscale) */
6098  round_var(var, scale);
6099 
6100  /*
6101  * Check for overflow - note we can't do this before rounding, because
6102  * rounding could raise the weight. Also note that the var's weight could
6103  * be inflated by leading zeroes, which will be stripped before storage
6104  * but perhaps might not have been yet. In any case, we must recognize a
6105  * true zero, whose weight doesn't mean anything.
6106  */
6107  ddigits = (var->weight + 1) * DEC_DIGITS;
6108  if (ddigits > maxdigits)
6109  {
6110  /* Determine true weight; and check for all-zero result */
6111  for (i = 0; i < var->ndigits; i++)
6112  {
6113  NumericDigit dig = var->digits[i];
6114 
6115  if (dig)
6116  {
6117  /* Adjust for any high-order decimal zero digits */
6118 #if DEC_DIGITS == 4
6119  if (dig < 10)
6120  ddigits -= 3;
6121  else if (dig < 100)
6122  ddigits -= 2;
6123  else if (dig < 1000)
6124  ddigits -= 1;
6125 #elif DEC_DIGITS == 2
6126  if (dig < 10)
6127  ddigits -= 1;
6128 #elif DEC_DIGITS == 1
6129  /* no adjustment */
6130 #else
6131 #error unsupported NBASE
6132 #endif
6133  if (ddigits > maxdigits)
6134  ereport(ERROR,
6135  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6136  errmsg("numeric field overflow"),
6137  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
6138  precision, scale,
6139  /* Display 10^0 as 1 */
6140  maxdigits ? "10^" : "",
6141  maxdigits ? maxdigits : 1
6142  )));
6143  break;
6144  }
6145  ddigits -= DEC_DIGITS;
6146  }
6147  }
6148 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8663
int weight
Definition: numeric.c:272
#define VARHDRSZ
Definition: c.h:439
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:246
#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 ( const NumericVar var,
NumericVar result 
)
static

Definition at line 7622 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().

7623 {
7624  NumericVar tmp;
7625 
7626  init_var(&tmp);
7627  set_var_from_var(var, &tmp);
7628 
7629  trunc_var(&tmp, 0);
7630 
7631  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
7632  add_var(&tmp, &const_one, &tmp);
7633 
7634  set_var_from_var(&tmp, result);
7635  free_var(&tmp);
7636 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8769
static const NumericVar const_one
Definition: numeric.c:375
#define NUMERIC_POS
Definition: numeric.c:164
int sign
Definition: numeric.c:273
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6451
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6509
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:5728
#define init_var(v)
Definition: numeric.c:451
static int cmp_abs ( const NumericVar var1,
const NumericVar var2 
)
static

Definition at line 8418 of file numeric.c.

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

Referenced by add_var(), and sub_var().

8419 {
8420  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
8421  var2->digits, var2->ndigits, var2->weight);
8422 }
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:8432
static int cmp_abs_common ( const NumericDigit var1digits,
int  var1ndigits,
int  var1weight,
const NumericDigit var2digits,
int  var2ndigits,
int  var2weight 
)
static

Definition at line 8432 of file numeric.c.

Referenced by cmp_abs(), and cmp_var_common().

8434 {
8435  int i1 = 0;
8436  int i2 = 0;
8437 
8438  /* Check any digits before the first common digit */
8439 
8440  while (var1weight > var2weight && i1 < var1ndigits)
8441  {
8442  if (var1digits[i1++] != 0)
8443  return 1;
8444  var1weight--;
8445  }
8446  while (var2weight > var1weight && i2 < var2ndigits)
8447  {
8448  if (var2digits[i2++] != 0)
8449  return -1;
8450  var2weight--;
8451  }
8452 
8453  /* At this point, either w1 == w2 or we've run out of digits */
8454 
8455  if (var1weight == var2weight)
8456  {
8457  while (i1 < var1ndigits && i2 < var2ndigits)
8458  {
8459  int stat = var1digits[i1++] - var2digits[i2++];
8460 
8461  if (stat)
8462  {
8463  if (stat > 0)
8464  return 1;
8465  return -1;
8466  }
8467  }
8468  }
8469 
8470  /*
8471  * At this point, we've run out of digits on one side or the other; so any
8472  * remaining nonzero digits imply that side is larger
8473  */
8474  while (i1 < var1ndigits)
8475  {
8476  if (var1digits[i1++] != 0)
8477  return 1;
8478  }
8479  while (i2 < var2ndigits)
8480  {
8481  if (var2digits[i2++] != 0)
8482  return -1;
8483  }
8484 
8485  return 0;
8486 }
static int cmp_numerics ( Numeric  num1,
Numeric  num2 
)
static

Definition at line 2136 of file numeric.c.

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

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().

2137 {
2138  int result;
2139 
2140  /*
2141  * We consider all NANs to be equal and larger than any non-NAN. This is
2142  * somewhat arbitrary; the important thing is to have a consistent sort
2143  * order.
2144  */
2145  if (NUMERIC_IS_NAN(num1))
2146  {
2147  if (NUMERIC_IS_NAN(num2))
2148  result = 0; /* NAN = NAN */
2149  else
2150  result = 1; /* NAN > non-NAN */
2151  }
2152  else if (NUMERIC_IS_NAN(num2))
2153  {
2154  result = -1; /* non-NAN < NAN */
2155  }
2156  else
2157  {
2158  result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
2159  NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
2160  NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
2161  NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
2162  }
2163 
2164  return result;
2165 }
#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:6466
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:214
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
static int cmp_var ( const NumericVar var1,
const NumericVar var2 
)
static

Definition at line 6451 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().

6452 {
6453  return cmp_var_common(var1->digits, var1->ndigits,
6454  var1->weight, var1->sign,
6455  var2->digits, var2->ndigits,
6456  var2->weight, var2->sign);
6457 }
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:6466
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 6466 of file numeric.c.

References cmp_abs_common(), NUMERIC_NEG, and NUMERIC_POS.

Referenced by cmp_numerics(), and cmp_var().

6470 {
6471  if (var1ndigits == 0)
6472  {
6473  if (var2ndigits == 0)
6474  return 0;
6475  if (var2sign == NUMERIC_NEG)
6476  return 1;
6477  return -1;
6478  }
6479  if (var2ndigits == 0)
6480  {
6481  if (var1sign == NUMERIC_POS)
6482  return 1;
6483  return -1;
6484  }
6485 
6486  if (var1sign == NUMERIC_POS)
6487  {
6488  if (var2sign == NUMERIC_NEG)
6489  return 1;
6490  return cmp_abs_common(var1digits, var1ndigits, var1weight,
6491  var2digits, var2ndigits, var2weight);
6492  }
6493 
6494  if (var2sign == NUMERIC_POS)
6495  return -1;
6496 
6497  return cmp_abs_common(var2digits, var2ndigits, var2weight,
6498  var1digits, var1ndigits, var1weight);
6499 }
#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:8432
static void compute_bucket ( Numeric  operand,
Numeric  bound1,
Numeric  bound2,
const NumericVar count_var,
NumericVar result_var 
)
static

Definition at line 1563 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().

1565 {
1566  NumericVar bound1_var;
1567  NumericVar bound2_var;
1568  NumericVar operand_var;
1569 
1570  init_var_from_num(bound1, &bound1_var);
1571  init_var_from_num(bound2, &bound2_var);
1572  init_var_from_num(operand, &operand_var);
1573 
1574  if (cmp_var(&bound1_var, &bound2_var) < 0)
1575  {
1576  sub_var(&operand_var, &bound1_var, &operand_var);
1577  sub_var(&bound2_var, &bound1_var, &bound2_var);
1578  div_var(&operand_var, &bound2_var, result_var,
1579  select_div_scale(&operand_var, &bound2_var), true);
1580  }
1581  else
1582  {
1583  sub_var(&bound1_var, &operand_var, &operand_var);
1584  sub_var(&bound1_var, &bound2_var, &bound1_var);
1585  div_var(&operand_var, &bound1_var, result_var,
1586  select_div_scale(&operand_var, &bound1_var), true);
1587  }
1588 
1589  mul_var(result_var, count_var, result_var,
1590  result_var->dscale + count_var->dscale);
1591  add_var(result_var, &const_one, result_var);
1592  floor_var(result_var, result_var);
1593 
1594  free_var(&bound1_var);
1595  free_var(&bound2_var);
1596  free_var(&operand_var);
1597 }
static const NumericVar const_one
Definition: numeric.c:375
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:6944
int dscale
Definition: numeric.c:274
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7524
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5711
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6451
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6747
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6509
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6626
static void floor_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:7646
static void div_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 6944 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().

6946 {
6947  int div_ndigits;
6948  int res_ndigits;
6949  int res_sign;
6950  int res_weight;
6951  int carry;
6952  int borrow;
6953  int divisor1;
6954  int divisor2;
6955  NumericDigit *dividend;
6956  NumericDigit *divisor;
6957  NumericDigit *res_digits;
6958  int i;
6959  int j;
6960 
6961  /* copy these values into local vars for speed in inner loop */
6962  int var1ndigits = var1->ndigits;
6963  int var2ndigits = var2->ndigits;
6964 
6965  /*
6966  * First of all division by zero check; we must not be handed an
6967  * unnormalized divisor.
6968  */
6969  if (var2ndigits == 0 || var2->digits[0] == 0)
6970  ereport(ERROR,
6971  (errcode(ERRCODE_DIVISION_BY_ZERO),
6972  errmsg("division by zero")));
6973 
6974  /*
6975  * Now result zero check
6976  */
6977  if (var1ndigits == 0)
6978  {
6979  zero_var(result);
6980  result->dscale = rscale;
6981  return;
6982  }
6983 
6984  /*
6985  * Determine the result sign, weight and number of digits to calculate.
6986  * The weight figured here is correct if the emitted quotient has no
6987  * leading zero digits; otherwise strip_var() will fix things up.
6988  */
6989  if (var1->sign == var2->sign)
6990  res_sign = NUMERIC_POS;
6991  else
6992  res_sign = NUMERIC_NEG;
6993  res_weight = var1->weight - var2->weight;
6994  /* The number of accurate result digits we need to produce: */
6995  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
6996  /* ... but always at least 1 */
6997  res_ndigits = Max(res_ndigits, 1);
6998  /* If rounding needed, figure one more digit to ensure correct result */
6999  if (round)
7000  res_ndigits++;
7001 
7002  /*
7003  * The working dividend normally requires res_ndigits + var2ndigits
7004  * digits, but make it at least var1ndigits so we can load all of var1
7005  * into it. (There will be an additional digit dividend[0] in the
7006  * dividend space, but for consistency with Knuth's notation we don't
7007  * count that in div_ndigits.)
7008  */
7009  div_ndigits = res_ndigits + var2ndigits;
7010  div_ndigits = Max(div_ndigits, var1ndigits);
7011 
7012  /*
7013  * We need a workspace with room for the working dividend (div_ndigits+1
7014  * digits) plus room for the possibly-normalized divisor (var2ndigits
7015  * digits). It is convenient also to have a zero at divisor[0] with the
7016  * actual divisor data in divisor[1 .. var2ndigits]. Transferring the
7017  * digits into the workspace also allows us to realloc the result (which
7018  * might be the same as either input var) before we begin the main loop.
7019  * Note that we use palloc0 to ensure that divisor[0], dividend[0], and
7020  * any additional dividend positions beyond var1ndigits, start out 0.
7021  */
7022  dividend = (NumericDigit *)
7023  palloc0((div_ndigits + var2ndigits + 2) * sizeof(NumericDigit));
7024  divisor = dividend + (div_ndigits + 1);
7025  memcpy(dividend + 1, var1->digits, var1ndigits * sizeof(NumericDigit));
7026  memcpy(divisor + 1, var2->digits, var2ndigits * sizeof(NumericDigit));
7027 
7028  /*
7029  * Now we can realloc the result to hold the generated quotient digits.
7030  */
7031  alloc_var(result, res_ndigits);
7032  res_digits = result->digits;
7033 
7034  if (var2ndigits == 1)
7035  {
7036  /*
7037  * If there's only a single divisor digit, we can use a fast path (cf.
7038  * Knuth section 4.3.1 exercise 16).
7039  */
7040  divisor1 = divisor[1];
7041  carry = 0;
7042  for (i = 0; i < res_ndigits; i++)
7043  {
7044  carry = carry * NBASE + dividend[i + 1];
7045  res_digits[i] = carry / divisor1;
7046  carry = carry % divisor1;
7047  }
7048  }
7049  else
7050  {
7051  /*
7052  * The full multiple-place algorithm is taken from Knuth volume 2,
7053  * Algorithm 4.3.1D.
7054  *
7055  * We need the first divisor digit to be >= NBASE/2. If it isn't,
7056  * make it so by scaling up both the divisor and dividend by the
7057  * factor "d". (The reason for allocating dividend[0] above is to
7058  * leave room for possible carry here.)
7059  */
7060  if (divisor[1] < HALF_NBASE)
7061  {
7062  int d = NBASE / (divisor[1] + 1);
7063 
7064  carry = 0;
7065  for (i = var2ndigits; i > 0; i--)
7066  {
7067  carry += divisor[i] * d;
7068  divisor[i] = carry % NBASE;
7069  carry = carry / NBASE;
7070  }
7071  Assert(carry == 0);
7072  carry = 0;
7073  /* at this point only var1ndigits of dividend can be nonzero */
7074  for (i = var1ndigits; i >= 0; i--)
7075  {
7076  carry += dividend[i] * d;
7077  dividend[i] = carry % NBASE;
7078  carry = carry / NBASE;
7079  }
7080  Assert(carry == 0);
7081  Assert(divisor[1] >= HALF_NBASE);
7082  }
7083  /* First 2 divisor digits are used repeatedly in main loop */
7084  divisor1 = divisor[1];
7085  divisor2 = divisor[2];
7086 
7087  /*
7088  * Begin the main loop. Each iteration of this loop produces the j'th
7089  * quotient digit by dividing dividend[j .. j + var2ndigits] by the
7090  * divisor; this is essentially the same as the common manual
7091  * procedure for long division.
7092  */
7093  for (j = 0; j < res_ndigits; j++)
7094  {
7095  /* Estimate quotient digit from the first two dividend digits */
7096  int next2digits = dividend[j] * NBASE + dividend[j + 1];
7097  int qhat;
7098 
7099  /*
7100  * If next2digits are 0, then quotient digit must be 0 and there's
7101  * no need to adjust the working dividend. It's worth testing
7102  * here to fall out ASAP when processing trailing zeroes in a
7103  * dividend.
7104  */
7105  if (next2digits == 0)
7106  {
7107  res_digits[j] = 0;
7108  continue;
7109  }
7110 
7111  if (dividend[j] == divisor1)
7112  qhat = NBASE - 1;
7113  else
7114  qhat = next2digits / divisor1;
7115 
7116  /*
7117  * Adjust quotient digit if it's too large. Knuth proves that
7118  * after this step, the quotient digit will be either correct or
7119  * just one too large. (Note: it's OK to use dividend[j+2] here
7120  * because we know the divisor length is at least 2.)
7121  */
7122  while (divisor2 * qhat >
7123  (next2digits - qhat * divisor1) * NBASE + dividend[j + 2])
7124  qhat--;
7125 
7126  /* As above, need do nothing more when quotient digit is 0 */
7127  if (qhat > 0)
7128  {
7129  /*
7130  * Multiply the divisor by qhat, and subtract that from the
7131  * working dividend. "carry" tracks the multiplication,
7132  * "borrow" the subtraction (could we fold these together?)
7133  */
7134  carry = 0;
7135  borrow = 0;
7136  for (i = var2ndigits; i >= 0; i--)
7137  {
7138  carry += divisor[i] * qhat;
7139  borrow -= carry % NBASE;
7140  carry = carry / NBASE;
7141  borrow += dividend[j + i];
7142  if (borrow < 0)
7143  {
7144  dividend[j + i] = borrow + NBASE;
7145  borrow = -1;
7146  }
7147  else
7148  {
7149  dividend[j + i] = borrow;
7150  borrow = 0;
7151  }
7152  }
7153  Assert(carry == 0);
7154 
7155  /*
7156  * If we got a borrow out of the top dividend digit, then
7157  * indeed qhat was one too large. Fix it, and add back the
7158  * divisor to correct the working dividend. (Knuth proves
7159  * that this will occur only about 3/NBASE of the time; hence,
7160  * it's a good idea to test this code with small NBASE to be
7161  * sure this section gets exercised.)
7162  */
7163  if (borrow)
7164  {
7165  qhat--;
7166  carry = 0;
7167  for (i = var2ndigits; i >= 0; i--)
7168  {
7169  carry += dividend[j + i] + divisor[i];
7170  if (carry >= NBASE)
7171  {
7172  dividend[j + i] = carry - NBASE;
7173  carry = 1;
7174  }
7175  else
7176  {
7177  dividend[j + i] = carry;
7178  carry = 0;
7179  }
7180  }
7181  /* A carry should occur here to cancel the borrow above */
7182  Assert(carry == 1);
7183  }
7184  }
7185 
7186  /* And we're done with this quotient digit */
7187  res_digits[j] = qhat;
7188  }
7189  }
7190 
7191  pfree(dividend);
7192 
7193  /*
7194  * Finally, round or truncate the result to the requested precision.
7195  */
7196  result->weight = res_weight;
7197  result->sign = res_sign;
7198 
7199  /* Round or truncate to target rscale (and set result->dscale) */
7200  if (round)
7201  round_var(result, rscale);
7202  else
7203  trunc_var(result, rscale);
7204 
7205  /* Strip leading and trailing zeroes */
7206  strip_var(result);
7207 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8663
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8769
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
static void strip_var(NumericVar *var)
Definition: numeric.c:8831
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:5494
void pfree(void *pointer)
Definition: mcxt.c:949
#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:877
#define Assert(condition)
Definition: c.h:664
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:5462
int i
#define Max(x, y)
Definition: numeric.c:11
#define DEC_DIGITS
Definition: numeric.c:95
static void div_var_fast ( const NumericVar var1,
const NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 7229 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().

7231 {
7232  int div_ndigits;
7233  int res_sign;
7234  int res_weight;
7235  int *div;
7236  int qdigit;
7237  int carry;
7238  int maxdiv;
7239  int newdig;
7240  NumericDigit *res_digits;
7241  double fdividend,
7242  fdivisor,
7243  fdivisorinverse,
7244  fquotient;
7245  int qi;
7246  int i;
7247 
7248  /* copy these values into local vars for speed in inner loop */
7249  int var1ndigits = var1->ndigits;
7250  int var2ndigits = var2->ndigits;
7251  NumericDigit *var1digits = var1->digits;
7252  NumericDigit *var2digits = var2->digits;
7253 
7254  /*
7255  * First of all division by zero check; we must not be handed an
7256  * unnormalized divisor.
7257  */
7258  if (var2ndigits == 0 || var2digits[0] == 0)
7259  ereport(ERROR,
7260  (errcode(ERRCODE_DIVISION_BY_ZERO),
7261  errmsg("division by zero")));
7262 
7263  /*
7264  * Now result zero check
7265  */
7266  if (var1ndigits == 0)
7267  {
7268  zero_var(result);
7269  result->dscale = rscale;
7270  return;
7271  }
7272 
7273  /*
7274  * Determine the result sign, weight and number of digits to calculate
7275  */
7276  if (var1->sign == var2->sign)
7277  res_sign = NUMERIC_POS;
7278  else
7279  res_sign = NUMERIC_NEG;
7280  res_weight = var1->weight - var2->weight + 1;
7281  /* The number of accurate result digits we need to produce: */
7282  div_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
7283  /* Add guard digits for roundoff error */
7284  div_ndigits += DIV_GUARD_DIGITS;
7285  if (div_ndigits < DIV_GUARD_DIGITS)
7286  div_ndigits = DIV_GUARD_DIGITS;
7287  /* Must be at least var1ndigits, too, to simplify data-loading loop */
7288  if (div_ndigits < var1ndigits)
7289  div_ndigits = var1ndigits;
7290 
7291  /*
7292  * We do the arithmetic in an array "div[]" of signed int's. Since
7293  * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
7294  * to avoid normalizing carries immediately.
7295  *
7296  * We start with div[] containing one zero digit followed by the
7297  * dividend's digits (plus appended zeroes to reach the desired precision
7298  * including guard digits). Each step of the main loop computes an
7299  * (approximate) quotient digit and stores it into div[], removing one
7300  * position of dividend space. A final pass of carry propagation takes
7301  * care of any mistaken quotient digits.
7302  */
7303  div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
7304  for (i = 0; i < var1ndigits; i++)
7305  div[i + 1] = var1digits[i];
7306 
7307  /*
7308  * We estimate each quotient digit using floating-point arithmetic, taking
7309  * the first four digits of the (current) dividend and divisor. This must
7310  * be float to avoid overflow. The quotient digits will generally be off
7311  * by no more than one from the exact answer.
7312  */
7313  fdivisor = (double) var2digits[0];
7314  for (i = 1; i < 4; i++)
7315  {
7316  fdivisor *= NBASE;
7317  if (i < var2ndigits)
7318  fdivisor += (double) var2digits[i];
7319  }
7320  fdivisorinverse = 1.0 / fdivisor;
7321 
7322  /*
7323  * maxdiv tracks the maximum possible absolute value of any div[] entry;
7324  * when this threatens to exceed INT_MAX, we take the time to propagate
7325  * carries. Furthermore, we need to ensure that overflow doesn't occur
7326  * during the carry propagation passes either. The carry values may have
7327  * an absolute value as high as INT_MAX/NBASE + 1, so really we must
7328  * normalize when digits threaten to exceed INT_MAX - INT_MAX/NBASE - 1.
7329  *
7330  * To avoid overflow in maxdiv itself, it represents the max absolute
7331  * value divided by NBASE-1, ie, at the top of the loop it is known that
7332  * no div[] entry has an absolute value exceeding maxdiv * (NBASE-1).
7333  *
7334  * Actually, though, that holds good only for div[] entries after div[qi];
7335  * the adjustment done at the bottom of the loop may cause div[qi + 1] to
7336  * exceed the maxdiv limit, so that div[qi] in the next iteration is
7337  * beyond the limit. This does not cause problems, as explained below.
7338  */
7339  maxdiv = 1;
7340 
7341  /*
7342  * Outer loop computes next quotient digit, which will go into div[qi]
7343  */
7344  for (qi = 0; qi < div_ndigits; qi++)
7345  {
7346  /* Approximate the current dividend value */
7347  fdividend = (double) div[qi];
7348  for (i = 1; i < 4; i++)
7349  {
7350  fdividend *= NBASE;
7351  if (qi + i <= div_ndigits)
7352  fdividend += (double) div[qi + i];
7353  }
7354  /* Compute the (approximate) quotient digit */
7355  fquotient = fdividend * fdivisorinverse;
7356  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7357  (((int) fquotient) - 1); /* truncate towards -infinity */
7358 
7359  if (qdigit != 0)
7360  {
7361  /* Do we need to normalize now? */
7362  maxdiv += Abs(qdigit);
7363  if (maxdiv > (INT_MAX - INT_MAX / NBASE - 1) / (NBASE - 1))
7364  {
7365  /* Yes, do it */
7366  carry = 0;
7367  for (i = div_ndigits; i > qi; i--)
7368  {
7369  newdig = div[i] + carry;
7370  if (newdig < 0)
7371  {
7372  carry = -((-newdig - 1) / NBASE) - 1;
7373  newdig -= carry * NBASE;
7374  }
7375  else if (newdig >= NBASE)
7376  {
7377  carry = newdig / NBASE;
7378  newdig -= carry * NBASE;
7379  }
7380  else
7381  carry = 0;
7382  div[i] = newdig;
7383  }
7384  newdig = div[qi] + carry;
7385  div[qi] = newdig;
7386 
7387  /*
7388  * All the div[] digits except possibly div[qi] are now in the
7389  * range 0..NBASE-1. We do not need to consider div[qi] in
7390  * the maxdiv value anymore, so we can reset maxdiv to 1.
7391  */
7392  maxdiv = 1;
7393 
7394  /*
7395  * Recompute the quotient digit since new info may have
7396  * propagated into the top four dividend digits
7397  */
7398  fdividend = (double) div[qi];
7399  for (i = 1; i < 4; i++)
7400  {
7401  fdividend *= NBASE;
7402  if (qi + i <= div_ndigits)
7403  fdividend += (double) div[qi + i];
7404  }
7405  /* Compute the (approximate) quotient digit */
7406  fquotient = fdividend * fdivisorinverse;
7407  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7408  (((int) fquotient) - 1); /* truncate towards -infinity */
7409  maxdiv += Abs(qdigit);
7410  }
7411 
7412  /*
7413  * Subtract off the appropriate multiple of the divisor.
7414  *
7415  * The digits beyond div[qi] cannot overflow, because we know they
7416  * will fall within the maxdiv limit. As for div[qi] itself, note
7417  * that qdigit is approximately trunc(div[qi] / vardigits[0]),
7418  * which would make the new value simply div[qi] mod vardigits[0].
7419  * The lower-order terms in qdigit can change this result by not
7420  * more than about twice INT_MAX/NBASE, so overflow is impossible.
7421  */
7422  if (qdigit != 0)
7423  {
7424  int istop = Min(var2ndigits, div_ndigits - qi + 1);
7425 
7426  for (i = 0; i < istop; i++)
7427  div[qi + i] -= qdigit * var2digits[i];
7428  }
7429  }
7430 
7431  /*
7432  * The dividend digit we are about to replace might still be nonzero.
7433  * Fold it into the next digit position.
7434  *
7435  * There is no risk of overflow here, although proving that requires
7436  * some care. Much as with the argument for div[qi] not overflowing,
7437  * if we consider the first two terms in the numerator and denominator
7438  * of qdigit, we can see that the final value of div[qi + 1] will be
7439  * approximately a remainder mod (vardigits[0]*NBASE + vardigits[1]).
7440  * Accounting for the lower-order terms is a bit complicated but ends
7441  * up adding not much more than INT_MAX/NBASE to the possible range.
7442  * Thus, div[qi + 1] cannot overflow here, and in its role as div[qi]
7443  * in the next loop iteration, it can't be large enough to cause
7444  * overflow in the carry propagation step (if any), either.
7445  *
7446  * But having said that: div[qi] can be more than INT_MAX/NBASE, as
7447  * noted above, which means that the product div[qi] * NBASE *can*
7448  * overflow. When that happens, adding it to div[qi + 1] will always
7449  * cause a canceling overflow so that the end result is correct. We
7450  * could avoid the intermediate overflow by doing the multiplication
7451  * and addition in int64 arithmetic, but so far there appears no need.
7452  */
7453  div[qi + 1] += div[qi] * NBASE;
7454 
7455  div[qi] = qdigit;
7456  }
7457 
7458  /*
7459  * Approximate and store the last quotient digit (div[div_ndigits])
7460  */
7461  fdividend = (double) div[qi];
7462  for (i = 1; i < 4; i++)
7463  fdividend *= NBASE;
7464  fquotient = fdividend * fdivisorinverse;
7465  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7466  (((int) fquotient) - 1); /* truncate towards -infinity */
7467  div[qi] = qdigit;
7468 
7469  /*
7470  * Because the quotient digits might be off by one, some of them might be
7471  * -1 or NBASE at this point. The represented value is correct in a
7472  * mathematical sense, but it doesn't look right. We do a final carry
7473  * propagation pass to normalize the digits, which we combine with storing
7474  * the result digits into the output. Note that this is still done at
7475  * full precision w/guard digits.
7476  */
7477  alloc_var(result, div_ndigits + 1);
7478  res_digits = result->digits;
7479  carry = 0;
7480  for (i = div_ndigits; i >= 0; i--)
7481  {
7482  newdig = div[i] + carry;
7483  if (newdig < 0)
7484  {
7485  carry = -((-newdig - 1) / NBASE) - 1;
7486  newdig -= carry * NBASE;
7487  }
7488  else if (newdig >= NBASE)
7489  {
7490  carry = newdig / NBASE;
7491  newdig -= carry * NBASE;
7492  }
7493  else
7494  carry = 0;
7495  res_digits[i] = newdig;
7496  }
7497  Assert(carry == 0);
7498 
7499  pfree(div);
7500 
7501  /*
7502  * Finally, round the result to the requested precision.
7503  */
7504  result->weight = res_weight;
7505  result->sign = res_sign;
7506 
7507  /* Round to target rscale (and set result->dscale) */
7508  if (round)
7509  round_var(result, rscale);
7510  else
7511  trunc_var(result, rscale);
7512 
7513  /* Strip leading and trailing zeroes */
7514  strip_var(result);
7515 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8663
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8769
int weight
Definition: numeric.c:272
#define NUMERIC_POS
Definition: numeric.c:164
static void strip_var(NumericVar *var)
Definition: numeric.c:8831
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:801
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static void zero_var(NumericVar *var)
Definition: numeric.c:5494
void pfree(void *pointer)
Definition: mcxt.c:949
#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:877
#define Assert(condition)
Definition: c.h:664
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:5462
int i
#define DEC_DIGITS
Definition: numeric.c:95
static void do_numeric_accum ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 3319 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().

3320 {
3321  NumericVar X;
3322  NumericVar X2;
3323  MemoryContext old_context;
3324 
3325  /* Count NaN inputs separately from all else */
3326  if (NUMERIC_IS_NAN(newval))
3327  {
3328  state->NaNcount++;
3329  return;
3330  }
3331 
3332  /* load processed number in short-lived context */
3333  init_var_from_num(newval, &X);
3334 
3335  /*
3336  * Track the highest input dscale that we've seen, to support inverse
3337  * transitions (see do_numeric_discard).
3338  */
3339  if (X.dscale > state->maxScale)
3340  {
3341  state->maxScale = X.dscale;
3342  state->maxScaleCount = 1;
3343  }
3344  else if (X.dscale == state->maxScale)
3345  state->maxScaleCount++;
3346 
3347  /* if we need X^2, calculate that in short-lived context */
3348  if (state->calcSumX2)
3349  {
3350  init_var(&X2);
3351  mul_var(&X, &X, &X2, X.dscale * 2);
3352  }
3353 
3354  /* The rest of this needs to work in the aggregate context */
3355  old_context = MemoryContextSwitchTo(state->agg_context);
3356 
3357  state->N++;
3358 
3359  /* Accumulate sums */
3360  accum_sum_add(&(state->sumX), &X);
3361 
3362  if (state->calcSumX2)
3363  accum_sum_add(&(state->sumX2), &X2);
3364 
3365  MemoryContextSwitchTo(old_context);
3366 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:3265
int dscale
Definition: numeric.c:274
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:8888
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5711
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6747
NumericSumAccum sumX2
Definition: numeric.c:3268
int64 NaNcount
Definition: numeric.c:3271
int64 maxScaleCount
Definition: numeric.c:3270
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
NumericSumAccum sumX
Definition: numeric.c:3267
#define init_var(v)
Definition: numeric.c:451
static bool do_numeric_discard ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 3384 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().

3385 {
3386  NumericVar X;
3387  NumericVar X2;
3388  MemoryContext old_context;
3389 
3390  /* Count NaN inputs separately from all else */
3391  if (NUMERIC_IS_NAN(newval))
3392  {
3393  state->NaNcount--;
3394  return true;
3395  }
3396 
3397  /* load processed number in short-lived context */
3398  init_var_from_num(newval, &X);
3399 
3400  /*
3401  * state->sumX's dscale is the maximum dscale of any of the inputs.
3402  * Removing the last input with that dscale would require us to recompute
3403  * the maximum dscale of the *remaining* inputs, which we cannot do unless
3404  * no more non-NaN inputs remain at all. So we report a failure instead,
3405  * and force the aggregation to be redone from scratch.
3406  */
3407  if (X.dscale == state->maxScale)
3408  {
3409  if (state->maxScaleCount > 1 || state->maxScale == 0)
3410  {
3411  /*
3412  * Some remaining inputs have same dscale, or dscale hasn't gotten
3413  * above zero anyway
3414  */
3415  state->maxScaleCount--;
3416  }
3417  else if (state->N == 1)
3418  {
3419  /* No remaining non-NaN inputs at all, so reset maxScale */
3420  state->maxScale = 0;
3421  state->maxScaleCount = 0;
3422  }
3423  else
3424  {
3425  /* Correct new maxScale is uncertain, must fail */
3426  return false;
3427  }
3428  }
3429 
3430  /* if we need X^2, calculate that in short-lived context */
3431  if (state->calcSumX2)
3432  {
3433  init_var(&X2);
3434  mul_var(&X, &X, &X2, X.dscale * 2);
3435  }
3436 
3437  /* The rest of this needs to work in the aggregate context */
3438  old_context = MemoryContextSwitchTo(state->agg_context);
3439 
3440  if (state->N-- > 1)
3441  {
3442  /* Negate X, to subtract it from the sum */
3443  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
3444  accum_sum_add(&(state->sumX), &X);
3445 
3446  if (state->calcSumX2)
3447  {
3448  /* Negate X^2. X^2 is always positive */
3449  X2.sign = NUMERIC_NEG;
3450  accum_sum_add(&(state->sumX2), &X2);
3451  }
3452  }
3453  else
3454  {
3455  /* Zero the sums */
3456  Assert(state->N == 0);
3457 
3458  accum_sum_reset(&state->sumX);
3459  if (state->calcSumX2)
3460  accum_sum_reset(&state->sumX2);
3461  }
3462 
3463  MemoryContextSwitchTo(old_context);
3464 
3465  return true;
3466 }
#define NUMERIC_POS
Definition: numeric.c:164
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:3265
int dscale
Definition: numeric.c:274
#define NUMERIC_NEG
Definition: numeric.c:165
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:8888
int sign
Definition: numeric.c:273
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5711
#define Assert(condition)
Definition: c.h:664
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6747
NumericSumAccum sumX2
Definition: numeric.c:3268
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:8872
int64 NaNcount
Definition: numeric.c:3271
int64 maxScaleCount
Definition: numeric.c:3270
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
NumericSumAccum sumX
Definition: numeric.c:3267
#define init_var(v)
Definition: numeric.c:451
static int estimate_ln_dweight ( const NumericVar var)
static

Definition at line 7868 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().

7869 {
7870  int ln_dweight;
7871 
7872  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
7873  cmp_var(var, &const_one_point_one) <= 0)
7874  {
7875  /*
7876  * 0.9 <= var <= 1.1
7877  *
7878  * ln(var) has a negative weight (possibly very large). To get a
7879  * reasonably accurate result, estimate it using ln(1+x) ~= x.
7880  */
7881  NumericVar x;
7882 
7883  init_var(&x);
7884  sub_var(var, &const_one, &x);
7885 
7886  if (x.ndigits > 0)
7887  {
7888  /* Use weight of most significant decimal digit of x */
7889  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
7890  }
7891  else
7892  {
7893  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
7894  ln_dweight = 0;
7895  }
7896 
7897  free_var(&x);
7898  }
7899  else
7900  {
7901  /*
7902  * Estimate the logarithm using the first couple of digits from the
7903  * input number. This will give an accurate result whenever the input
7904  * is not too close to 1.
7905  */
7906  if (var->ndigits > 0)
7907  {
7908  int digits;
7909  int dweight;
7910  double ln_var;
7911 
7912  digits = var->digits[0];
7913  dweight = var->weight * DEC_DIGITS;
7914 
7915  if (var->ndigits > 1)
7916  {
7917  digits = digits * NBASE + var->digits[1];
7918  dweight -= DEC_DIGITS;
7919  }
7920 
7921  /*----------
7922  * We have var ~= digits * 10^dweight
7923  * so ln(var) ~= ln(digits) + dweight * ln(10)
7924  *----------
7925  */
7926  ln_var = log((double) digits) + dweight * 2.302585092994046;
7927  ln_dweight = (int) log10(Abs(ln_var));
7928  }
7929  else
7930  {
7931  /* Caller should fail on ln(0), but for the moment return zero */
7932  ln_dweight = 0;
7933  }
7934  }
7935 
7936  return ln_dweight;
7937 }
int weight
Definition: numeric.c:272
static const NumericVar const_zero_point_nine
Definition: numeric.c:409
static const NumericVar const_one
Definition: numeric.c:375
static void ln_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:7946
int ndigits
Definition: numeric.c:271
#define Abs(x)
Definition: c.h:801
#define NBASE
Definition: numeric.c:93
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6451
static void free_var(NumericVar *var)
Definition: numeric.c:5478
NumericDigit * digits
Definition: numeric.c:276
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6626
#define DEC_DIGITS
Definition: numeric.c:95
static const NumericVar const_one_point_one
Definition: numeric.c:419
#define init_var(v)
Definition: numeric.c:451
int digits
Definition: informix.c:691
static void exp_var ( const NumericVar arg,
NumericVar result,
int  rscale 
)
static

Definition at line 7742 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().

7743 {
7744  NumericVar x;
7745  NumericVar elem;
7746  NumericVar ni;
7747  double val;
7748  int dweight;
7749  int ndiv2;
7750  int sig_digits;
7751  int local_rscale;
7752 
7753  init_var(&x);
7754  init_var(&elem);
7755  init_var(&ni);
7756 
7757  set_var_from_var(arg, &x);
7758 
7759  /*
7760  * Estimate the dweight of the result using floating point arithmetic, so
7761  * that we can choose an appropriate local rscale for the calculation.
7762  */
7764 
7765  /* Guard against overflow */
7766  /* If you change this limit, see also power_var()'s limit */
7767  if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
7768  ereport(ERROR,
7769  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7770  errmsg("value overflows numeric format")));
7771 
7772  /* decimal weight = log10(e^x) = x * log10(e) */
7773  dweight = (int) (val * 0.434294481903252);
7774 
7775  /*
7776  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
7777  * 2^n, to improve the convergence rate of the Taylor series.
7778  */
7779  if (Abs(val) > 0.01)
7780  {
7781  NumericVar tmp;
7782 
7783  init_var(&tmp);
7784  set_var_from_var(&const_two, &tmp);
7785 
7786  ndiv2 = 1;
7787  val /= 2;
7788 
7789  while (Abs(val) > 0.01)
7790  {
7791  ndiv2++;
7792  val /= 2;
7793  add_var(&tmp, &tmp, &tmp);
7794  }
7795 
7796  local_rscale = x.dscale + ndiv2;
7797  div_var_fast(&x, &tmp, &x, local_rscale, true);
7798 
7799  free_var(&tmp);
7800  }
7801  else
7802  ndiv2 = 0;
7803 
7804  /*
7805  * Set the scale for the Taylor series expansion. The final result has
7806  * (dweight + rscale + 1) significant digits. In addition, we have to
7807  * raise the Taylor series result to the power 2^ndiv2, which introduces
7808  * an error of up to around log10(2^ndiv2) digits, so work with this many
7809  * extra digits of precision (plus a few more for good measure).
7810  */
7811  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
7812  sig_digits = Max(sig_digits, 0) + 8;
7813 
7814  local_rscale = sig_digits - 1;
7815 
7816  /*
7817  * Use the Taylor series
7818  *
7819  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
7820  *
7821  * Given the limited range of x, this should converge reasonably quickly.
7822  * We run the series until the terms fall below the local_rscale limit.
7823  */
7824  add_var(&const_one, &x, result);
7825 
7826  mul_var(&x, &x, &elem, local_rscale);
7827  set_var_from_var(&const_two, &ni);
7828  div_var_fast(&elem, &ni, &elem, local_rscale, true);
7829 
7830  while (elem.ndigits != 0)
7831  {
7832  add_var(result, &elem, result);
7833 
7834  mul_var(&elem, &x, &elem, local_rscale);
7835  add_var(&ni, &const_one, &ni);
7836  div_var_fast(&elem, &ni, &elem, local_rscale, true);
7837  }
7838 
7839  /*
7840  * Compensate for the argument range reduction. Since the weight of the
7841  * result doubles with each multiplication, we can reduce the local rscale
7842  * as we proceed.
7843  */
7844  while (ndiv2-- > 0)
7845  {
7846  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
7847  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
7848  mul_var(result, result, result, local_rscale);
7849  }
7850 
7851  /* Round to requested rscale */
7852  round_var(result, rscale);
7853 
7854  free_var(&x);
7855  free_var(&elem);
7856  free_var(&ni);
7857 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:8663
int weight
Definition: numeric.c:272
static const NumericVar const_one
Definition: numeric.c:375
int errcode(int sqlerrcode)
Definition: elog.c:575
int ndigits
Definition: numeric.c:271
int dscale
Definition: numeric.c:274
#define Abs(x)
Definition: c.h:801
#define ERROR
Definition: elog.h:43
static double numericvar_to_double_no_overflow(const NumericVar *var)
Definition: numeric.c:6419
#define NUMERIC_MIN_DISPLAY_SCALE
Definition: numeric.h:30
#define NUMERIC_MAX_RESULT_SCALE
Definition: numeric.h:32
static void div_var_fast(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:7229
#define ereport(elevel, rest)
Definition: elog.h:122
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static const NumericVar const_two
Definition: numeric.c:379
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:6747
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6509
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define Max(x, y)
Definition: numeric.c:11
#define DEC_DIGITS
Definition: numeric.c:95
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:5728
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum float4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3202 of file numeric.c.

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

3203 {
3205  Numeric res;
3206  NumericVar result;
3207  char buf[FLT_DIG + 100];
3208 
3209  if (isnan(val))
3211 
3212  sprintf(buf, "%.*g", FLT_DIG, val);
3213 
3214  init_var(&result);
3215 
3216  /* Assume we need not worry about leading/trailing spaces */
3217  (void) set_var_from_str(buf, buf, &result);
3218 
3219  res = make_result(&result);
3220 
3221  free_var(&result);
3222 
3223  PG_RETURN_NUMERIC(res);
3224 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:245
float float4
Definition: c.h:374
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:5518
static const NumericVar const_nan
Definition: numeric.c:422
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:5993
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum float8_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3136 of file numeric.c.

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

3137 {
3139  Numeric res;
3140  NumericVar result;
3141  char buf[DBL_DIG + 100];
3142 
3143  if (isnan(val))
3145 
3146  sprintf(buf, "%.*g", DBL_DIG, val);
3147 
3148  init_var(&result);
3149 
3150  /* Assume we need not worry about leading/trailing spaces */
3151  (void) set_var_from_str(buf, buf, &result);
3152 
3153  res = make_result(&result);
3154 
3155  free_var(&result);
3156 
3157  PG_RETURN_NUMERIC(res);
3158 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
double float8
Definition: c.h:375
static char * buf
Definition: pg_test_fsync.c:67
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:5518
static const NumericVar const_nan
Definition: numeric.c:422
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:5993
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
static void floor_var ( const NumericVar var,
NumericVar result 
)
static

Definition at line 7646 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().

7647 {
7648  NumericVar tmp;
7649 
7650  init_var(&tmp);
7651  set_var_from_var(var, &tmp);
7652 
7653  trunc_var(&tmp, 0);
7654 
7655  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
7656  sub_var(&tmp, &const_one, &tmp);
7657 
7658  set_var_from_var(&tmp, result);
7659  free_var(&tmp);
7660 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:8769
static const NumericVar const_one
Definition: numeric.c:375
#define NUMERIC_NEG
Definition: numeric.c:165
int sign
Definition: numeric.c:273
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6451
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6626
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:5728
#define init_var(v)
Definition: numeric.c:451
Datum generate_series_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1367 of file numeric.c.

References generate_series_step_numeric().

1368 {
1369  return generate_series_step_numeric(fcinfo);
1370 }
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1373
Datum generate_series_step_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1373 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, 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().

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

Definition at line 5754 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().

5755 {
5756  int dscale;
5757  char *str;
5758  char *cp;
5759  char *endcp;
5760  int i;
5761  int d;
5762  NumericDigit dig;
5763 
5764 #if DEC_DIGITS > 1
5765  NumericDigit d1;
5766 #endif
5767 
5768  dscale = var->dscale;
5769 
5770  /*
5771  * Allocate space for the result.
5772  *
5773  * i is set to the # of decimal digits before decimal point. dscale is the
5774  * # of decimal digits we will print after decimal point. We may generate
5775  * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
5776  * need room for sign, decimal point, null terminator.
5777  */
5778  i = (var->weight + 1) * DEC_DIGITS;
5779  if (i <= 0)
5780  i = 1;
5781 
5782  str = palloc(i + dscale + DEC_DIGITS + 2);
5783  cp = str;
5784 
5785  /*
5786  * Output a dash for negative values
5787  */
5788  if (var->sign == NUMERIC_NEG)
5789  *cp++ = '-';
5790 
5791  /*
5792  * Output all digits before the decimal point
5793  */
5794  if (var->weight < 0)
5795  {
5796  d = var->weight + 1;
5797  *cp++ = '0';
5798  }
5799  else
5800  {
5801  for (d = 0; d <= var->weight; d++)
5802  {
5803  dig = (d < var->ndigits) ? var->digits[d] : 0;
5804  /* In the first digit, suppress extra leading decimal zeroes */
5805 #if DEC_DIGITS == 4
5806  {
5807  bool putit = (d > 0);
5808 
5809  d1 = dig / 1000;
5810  dig -= d1 * 1000;
5811  putit |= (d1 > 0);
5812  if (putit)
5813  *cp++ = d1 + '0';
5814  d1 = dig / 100;
5815  dig -= d1 * 100;
5816  putit |= (d1 > 0);
5817  if (putit)
5818  *cp++ = d1 + '0';
5819  d1 = dig / 10;
5820  dig -= d1 * 10;
5821  putit |= (d1 > 0);
5822  if (putit)
5823  *cp++ = d1 + '0';
5824  *cp++ = dig + '0';
5825  }
5826 #elif DEC_DIGITS == 2
5827  d1 = dig / 10;
5828  dig -= d1 * 10;
5829  if (d1 > 0 || d > 0)
5830  *cp++ = d1 + '0';
5831  *cp++ = dig + '0';
5832 #elif DEC_DIGITS == 1
5833  *cp++ = dig + '0';
5834 #else
5835 #error unsupported NBASE
5836 #endif
5837  }
5838  }
5839 
5840  /*
5841  * If requested, output a decimal point and all the digits that follow it.
5842  * We initially put out a multiple of DEC_DIGITS digits, then truncate if
5843  * needed.
5844  */
5845  if (dscale > 0)
5846  {
5847  *cp++ = '.';
5848  endcp = cp + dscale;
5849  for (i = 0; i < dscale; d++, i += DEC_DIGITS)
5850  {
5851  dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
5852 #if DEC_DIGITS == 4
5853  d1 = dig / 1000;
5854  dig -= d1 * 1000;
5855  *cp++ = d1 + '0';
5856  d1 = dig / 100;
5857  dig -= d1 * 100;
5858  *cp++ = d1 + '0';
5859  d1 = dig / 10;
5860  dig -= d1 * 10;
5861  *cp++ = d1 + '0';
5862  *cp++ = dig + '0';
5863 #elif DEC_DIGITS == 2
5864  d1 = dig / 10;
5865  dig -= d1 * 10;
5866  *cp++ = d1 + '0';
5867  *cp++ = dig + '0';
5868 #elif DEC_DIGITS == 1
5869  *cp++ = dig + '0';
5870 #else
5871 #error unsupported NBASE
5872 #endif
5873  }
5874  cp = endcp;
5875  }
5876 
5877  /*
5878  * terminate the string and return it
5879  */
5880  *cp = '\0';
5881  return str;
5882 }
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:848
int i
#define DEC_DIGITS
Definition: numeric.c:95
static char * get_str_from_var_sci ( const NumericVar var,
int  rscale 
)
static

Definition at line 5907 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().

5908 {
5909  int32 exponent;
5910  NumericVar denominator;
5911  NumericVar significand;
5912  int denom_scale;
5913  size_t len;
5914  char *str;
5915  char *sig_out;
5916 
5917  if (rscale < 0)
5918  rscale = 0;
5919 
5920  /*
5921  * Determine the exponent of this number in normalised form.
5922  *
5923  * This is the exponent required to represent the number with only one
5924  * significant digit before the decimal place.
5925  */
5926  if (var->ndigits > 0)
5927  {
5928  exponent = (var->weight + 1) * DEC_DIGITS;
5929 
5930  /*
5931  * Compensate for leading decimal zeroes in the first numeric digit by
5932  * decrementing the exponent.
5933  */
5934  exponent -= DEC_DIGITS - (int) log10(var->digits[0]);
5935  }
5936  else
5937  {
5938  /*
5939  * If var has no digits, then it must be zero.
5940  *
5941  * Zero doesn't technically have a meaningful exponent in normalised
5942  * notation, but we just display the exponent as zero for consistency
5943  * of output.
5944  */
5945  exponent = 0;
5946  }
5947 
5948  /*
5949  * The denominator is set to 10 raised to the power of the exponent.
5950  *
5951  * We then divide var by the denominator to get the significand, rounding
5952  * to rscale decimal digits in the process.
5953  */
5954  if (exponent < 0)
5955  denom_scale = -exponent;
5956  else
5957  denom_scale = 0;
5958 
5959  init_var(&denominator);
5960  init_var(&significand);
5961 
5962  power_var_int(&const_ten, exponent, &denominator, denom_scale);
5963  div_var(var, &denominator, &significand, rscale, true);
5964  sig_out = get_str_from_var(&significand);
5965 
5966  free_var(&denominator);
5967  free_var(&significand);
5968 
5969  /*
5970  * Allocate space for the result.
5971  *
5972  * In addition to the significand, we need room for the exponent
5973  * decoration ("e"), the sign of the exponent, up to 10 digits for the
5974  * exponent itself, and of course the null terminator.
5975  */
5976  len = strlen(sig_out) + 13;
5977  str = palloc(len);
5978  snprintf(str, len, "%se%+03d", sig_out, exponent);
5979 
5980  pfree(sig_out);
5981 
5982  return str;
5983 }
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(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:6944
signed int int32
Definition: c.h:246
void pfree(void *pointer)
Definition: mcxt.c:949
static void power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
Definition: numeric.c:8228
static const NumericVar const_ten
Definition: numeric.c:384
static void free_var(NumericVar *var)
Definition: numeric.c:5478
NumericDigit * digits
Definition: numeric.c:276
void * palloc(Size size)
Definition: mcxt.c:848
static char * get_str_from_var(const NumericVar *var)
Definition: numeric.c:5754
#define DEC_DIGITS
Definition: numeric.c:95
#define init_var(v)
Definition: numeric.c:451
Datum hash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2168 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, and PG_RETURN_UINT32.

Referenced by JsonbHashScalarValue().

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

Definition at line 2248 of file numeric.c.

References Assert, DatumGetUInt64, digits, hash_any_extended(), i, NUMERIC_DIGITS, NUMERIC_IS_NAN, NUMERIC_NDIGITS, NUMERIC_WEIGHT, PG_GETARG_INT64, PG_GETARG_NUMERIC, PG_RETURN_DATUM, PG_RETURN_UINT64, and UInt64GetDatum.

Referenced by JsonbHashScalarValueExtended().

2249 {
2250  Numeric key = PG_GETARG_NUMERIC(0);
2251  uint64 seed = PG_GETARG_INT64(1);
2252  Datum digit_hash;
2253  Datum result;
2254  int weight;
2255  int start_offset;
2256  int end_offset;
2257  int i;
2258  int hash_len;
2260 
2261  if (NUMERIC_IS_NAN(key))
2262  PG_RETURN_UINT64(seed);
2263 
2264  weight = NUMERIC_WEIGHT(key);
2265  start_offset = 0;
2266  end_offset = 0;
2267 
2268  digits = NUMERIC_DIGITS(key);
2269  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2270  {
2271  if (digits[i] != (NumericDigit) 0)
2272  break;
2273 
2274  start_offset++;
2275 
2276  weight--;
2277  }
2278 
2279  if (NUMERIC_NDIGITS(key) == start_offset)
2280  PG_RETURN_UINT64(seed - 1);
2281 
2282  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2283  {
2284  if (digits[i] != (NumericDigit) 0)
2285  break;
2286 
2287  end_offset++;
2288  }
2289 
2290  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2291 
2292  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2293  digit_hash = hash_any_extended((unsigned char *) (NUMERIC_DIGITS(key)
2294  + start_offset),
2295  hash_len * sizeof(NumericDigit),
2296  seed);
2297 
2298  result = UInt64GetDatum(DatumGetUInt64(digit_hash) ^ weight);
2299 
2300  PG_RETURN_DATUM(result);
2301 }
#define UInt64GetDatum(X)
Definition: postgres.h:654
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:328
#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 DatumGetUInt64(X)
Definition: postgres.h:640
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:214
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#define Assert(condition)
Definition: c.h:664
int i
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634
int digits
Definition: informix.c:691
static void init_var_from_num ( Numeric  num,
NumericVar dest 
)
static

Definition at line 5711 of file numeric.c.

References NumericVar::buf, NumericVar::digits, NumericVar::dscale, NumericVar::ndigits, 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().

5712 {
5713  dest->ndigits = NUMERIC_NDIGITS(num);
5714  dest->weight = NUMERIC_WEIGHT(num);
5715  dest->sign = NUMERIC_SIGN(num);
5716  dest->dscale = NUMERIC_DSCALE(num);
5717  dest->digits = NUMERIC_DIGITS(num);
5718  dest->buf = NULL; /* digits array is not palloc'd */
5719 }
#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
NumericDigit * digits
Definition: numeric.c:276
Datum int2_accum ( PG_FUNCTION_ARGS  )

Definition at line 4009 of file numeric.c.

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

4010 {
4012 
4013  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4014 
4015  /* Create the state data on the first call */
4016  if (state == NULL)
4017  state = makePolyNumAggState(fcinfo, true);
4018 
4019  if (!PG_ARGISNULL(1))
4020  {
4021 #ifdef HAVE_INT128
4022  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
4023 #else
4024  Numeric newval;
4025 
4027  PG_GETARG_DATUM(1)));
4028  do_numeric_accum(state, newval);
4029 #endif
4030  }
4031 
4032  PG_RETURN_POINTER(state);
4033 }
#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:3082
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define makePolyNumAggState
Definition: numeric.c:4004
Datum int2_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4486 of file numeric.c.

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

4487 {
4489 
4490  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4491 
4492  /* Should not get here with no state */
4493  if (state == NULL)
4494  elog(ERROR, "int2_accum_inv called with NULL state");
4495 
4496  if (!PG_ARGISNULL(1))
4497  {
4498 #ifdef HAVE_INT128
4499  do_int128_discard(state, (int128) PG_GETARG_INT16(1));
4500 #else
4501  Numeric newval;
4502 
4504  PG_GETARG_DATUM(1)));
4505 
4506  /* Should never fail, all inputs have dscale 0 */
4507  if (!do_numeric_discard(state, newval))
4508  elog(ERROR, "do_numeric_discard failed unexpectedly");
4509 #endif
4510  }
4511 
4512  PG_RETURN_POINTER(state);
4513 }
#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:3082
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3384
#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
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
Datum int2_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 5177 of file numeric.c.

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

5178 {
5179  ArrayType *transarray;
5181  Int8TransTypeData *transdata;
5182 
5183  /*
5184  * If we're invoked as an aggregate, we can cheat and modify our first
5185  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5186  * a copy of it before scribbling on it.
5187  */
5188  if (AggCheckCallContext(fcinfo, NULL))
5189  transarray = PG_GETARG_ARRAYTYPE_P(0);
5190  else
5191  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5192 
5193  if (ARR_HASNULL(transarray) ||
5194  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5195  elog(ERROR, "expected 2-element int8 array");
5196 
5197  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5198  transdata->count++;
5199  transdata->sum += newval;
5200 
5201  PG_RETURN_ARRAYTYPE_P(transarray);
5202 }
signed short int16
Definition: c.h:245
#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 newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#define elog
Definition: elog.h:219
Datum int2_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 5264 of file numeric.c.

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

5265 {
5266  ArrayType *transarray;
5268  Int8TransTypeData *transdata;
5269 
5270  /*
5271  * If we're invoked as an aggregate, we can cheat and modify our first
5272  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5273  * a copy of it before scribbling on it.
5274  */
5275  if (AggCheckCallContext(fcinfo, NULL))
5276  transarray = PG_GETARG_ARRAYTYPE_P(0);
5277  else
5278  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5279 
5280  if (ARR_HASNULL(transarray) ||
5281  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5282  elog(ERROR, "expected 2-element int8 array");
5283 
5284  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5285  transdata->count--;
5286  transdata->sum -= newval;
5287 
5288  PG_RETURN_ARRAYTYPE_P(transarray);
5289 }
signed short int16
Definition: c.h:245
#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 newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#define elog
Definition: elog.h:219
Datum int2_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3082 of file numeric.c.

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

Referenced by int2_accum(), and int2_accum_inv().

3083 {
3084  int16 val = PG_GETARG_INT16(0);
3085  Numeric res;
3086  NumericVar result;
3087 
3088  init_var(&result);
3089 
3090  int64_to_numericvar((int64) val, &result);
3091 
3092  res = make_result(&result);
3093 
3094  free_var(&result);
3095 
3096  PG_RETURN_NUMERIC(res);
3097 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
signed short int16
Definition: c.h:245
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:5993
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:6227
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum int2_sum ( PG_FUNCTION_ARGS  )

Definition at line 5025 of file numeric.c.

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

5026 {
5027  int64 newval;
5028 
5029  if (PG_ARGISNULL(0))
5030  {
5031  /* No non-null input seen so far... */
5032  if (PG_ARGISNULL(1))
5033  PG_RETURN_NULL(); /* still no non-null */
5034  /* This is the first non-null input. */
5035  newval = (int64) PG_GETARG_INT16(1);
5036  PG_RETURN_INT64(newval);
5037  }
5038 
5039  /*
5040  * If we're invoked as an aggregate, we can cheat and modify our first
5041  * parameter in-place to avoid palloc overhead. If not, we need to return
5042  * the new value of the transition variable. (If int8 is pass-by-value,
5043  * then of course this is useless as well as incorrect, so just ifdef it
5044  * out.)
5045  */
5046 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
5047  if (AggCheckCallContext(fcinfo, NULL))
5048  {
5049  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
5050 
5051  /* Leave the running sum unchanged in the new input is null */
5052  if (!PG_ARGISNULL(1))
5053  *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
5054 
5055  PG_RETURN_POINTER(oldsum);
5056  }
5057  else
5058 #endif
5059  {
5060  int64 oldsum = PG_GETARG_INT64(0);
5061 
5062  /* Leave sum unchanged if new input is null. */
5063  if (PG_ARGISNULL(1))
5064  PG_RETURN_INT64(oldsum);
5065 
5066  /* OK to do the addition. */
5067  newval = oldsum + (int64) PG_GETARG_INT16(1);
5068 
5069  PG_RETURN_INT64(newval);
5070  }
5071 }
#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 newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#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 5349 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.

5350 {
5351  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
5352  Int8TransTypeData *transdata;
5353 
5354  if (ARR_HASNULL(transarray) ||
5355  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5356  elog(ERROR, "expected 2-element int8 array");
5357  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5358 
5359  /* SQL defines SUM of no values to be NULL */
5360  if (transdata->count == 0)
5361  PG_RETURN_NULL();
5362 
5363  PG_RETURN_DATUM(Int64GetDatumFast(transdata->sum));
5364 }
#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 4036 of file numeric.c.

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

4037 {
4039 
4040  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4041 
4042  /* Create the state data on the first call */
4043  if (state == NULL)
4044  state = makePolyNumAggState(fcinfo, true);
4045 
4046  if (!PG_ARGISNULL(1))
4047  {
4048 #ifdef HAVE_INT128
4049  do_int128_accum(state, (int128) PG_GETARG_INT32(1));
4050 #else
4051  Numeric newval;
4052 
4054  PG_GETARG_DATUM(1)));
4055  do_numeric_accum(state, newval);
4056 #endif
4057  }
4058 
4059  PG_RETURN_POINTER(state);
4060 }
#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:585
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3319
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2972
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define makePolyNumAggState
Definition: numeric.c:4004
Datum int4_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4516 of file numeric.c.

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

4517 {
4519 
4520  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4521 
4522  /* Should not get here with no state */
4523  if (state == NULL)
4524  elog(ERROR, "int4_accum_inv called with NULL state");
4525 
4526  if (!PG_ARGISNULL(1))
4527  {
4528 #ifdef HAVE_INT128
4529  do_int128_discard(state, (int128) PG_GETARG_INT32(1));
4530 #else
4531  Numeric newval;
4532 
4534  PG_GETARG_DATUM(1)));
4535 
4536  /* Should never fail, all inputs have dscale 0 */
4537  if (!do_numeric_discard(state, newval))
4538  elog(ERROR, "do_numeric_discard failed unexpectedly");
4539 #endif
4540  }
4541 
4542  PG_RETURN_POINTER(state);
4543 }
#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:585
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3384
#define ERROR
Definition: elog.h:43
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2972
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
Datum int4_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 5205 of file numeric.c.

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

5206 {
5207  ArrayType *transarray;
5209  Int8TransTypeData *transdata;
5210 
5211  /*
5212  * If we're invoked as an aggregate, we can cheat and modify our first
5213  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5214  * a copy of it before scribbling on it.
5215  */
5216  if (AggCheckCallContext(fcinfo, NULL))
5217  transarray = PG_GETARG_ARRAYTYPE_P(0);
5218  else
5219  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5220 
5221  if (ARR_HASNULL(transarray) ||
5222  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5223  elog(ERROR, "expected 2-element int8 array");
5224 
5225  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5226  transdata->count++;
5227  transdata->sum += newval;
5228 
5229  PG_RETURN_ARRAYTYPE_P(transarray);
5230 }
#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:246
#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 newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#define elog
Definition: elog.h:219
Datum int4_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 5292 of file numeric.c.

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

5293 {
5294  ArrayType *transarray;
5296  Int8TransTypeData *transdata;
5297 
5298  /*
5299  * If we're invoked as an aggregate, we can cheat and modify our first
5300  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5301  * a copy of it before scribbling on it.
5302  */
5303  if (AggCheckCallContext(fcinfo, NULL))
5304  transarray = PG_GETARG_ARRAYTYPE_P(0);
5305  else
5306  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5307 
5308  if (ARR_HASNULL(transarray) ||
5309  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5310  elog(ERROR, "expected 2-element int8 array");
5311 
5312  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5313  transdata->count--;
5314  transdata->sum -= newval;
5315 
5316  PG_RETURN_ARRAYTYPE_P(transarray);
5317 }
#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:246
#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 newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#define elog
Definition: elog.h:219
Datum int4_avg_combine ( PG_FUNCTION_ARGS  )

Definition at line 5233 of file numeric.c.

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

5234 {
5235  ArrayType *transarray1;
5236  ArrayType *transarray2;
5237  Int8TransTypeData *state1;
5238  Int8TransTypeData *state2;
5239 
5240  if (!AggCheckCallContext(fcinfo, NULL))
5241  elog(ERROR, "aggregate function called in non-aggregate context");
5242 
5243  transarray1 = PG_GETARG_ARRAYTYPE_P(0);
5244  transarray2 = PG_GETARG_ARRAYTYPE_P(1);
5245 
5246  if (ARR_HASNULL(transarray1) ||
5247  ARR_SIZE(transarray1) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5248  elog(ERROR, "expected 2-element int8 array");
5249 
5250  if (ARR_HASNULL(transarray2) ||
5251  ARR_SIZE(transarray2) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5252  elog(ERROR, "expected 2-element int8 array");
5253 
5254  state1 = (Int8TransTypeData *) ARR_DATA_PTR(transarray1);
5255  state2 = (Int8TransTypeData *) ARR_DATA_PTR(transarray2);
5256 
5257  state1->count += state2->count;
5258  state1->sum += state2->sum;
5259 
5260  PG_RETURN_ARRAYTYPE_P(transarray1);
5261 }
#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
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#define elog
Definition: elog.h:219
Datum int4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2972 of file numeric.c.

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

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

2973 {
2974  int32 val = PG_GETARG_INT32(0);
2975  Numeric res;
2976  NumericVar result;
2977 
2978  init_var(&result);
2979 
2980  int64_to_numericvar((int64) val, &result);
2981 
2982  res = make_result(&result);
2983 
2984  free_var(&result);
2985 
2986  PG_RETURN_NUMERIC(res);
2987 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:246
static void free_var(NumericVar *var)
Definition: numeric.c:5478
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:5993
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:6227
long val
Definition: informix.c:689
#define init_var(v)
Definition: numeric.c:451
Datum int4_sum ( PG_FUNCTION_ARGS  )

Definition at line 5074 of file numeric.c.

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

5075 {
5076  int64 newval;
5077 
5078  if (PG_ARGISNULL(0))
5079  {
5080  /* No non-null input seen so far... */
5081  if (PG_ARGISNULL(1))
5082  PG_RETURN_NULL(); /* still no non-null */
5083  /* This is the first non-null input. */
5084  newval = (int64) PG_GETARG_INT32(1);
5085  PG_RETURN_INT64(newval);
5086  }
5087 
5088  /*
5089  * If we're invoked as an aggregate, we can cheat and modify our first
5090  * parameter in-place to avoid palloc overhead. If not, we need to return
5091  * the new value of the transition variable. (If int8 is pass-by-value,
5092  * then of course this is useless as well as incorrect, so just ifdef it
5093  * out.)
5094  */
5095 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
5096  if (AggCheckCallContext(fcinfo, NULL))
5097  {
5098  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
5099 
5100  /* Leave the running sum unchanged in the new input is null */
5101  if (!PG_ARGISNULL(1))
5102  *oldsum = *oldsum + (int64) PG_GETARG_INT32(1);
5103 
5104  PG_RETURN_POINTER(oldsum);
5105  }
5106  else
5107 #endif
5108  {
5109  int64 oldsum = PG_GETARG_INT64(0);
5110 
5111  /* Leave sum unchanged if new input is null. */
5112  if (PG_ARGISNULL(1))
5113  PG_RETURN_INT64(oldsum);
5114 
5115  /* OK to do the addition. */
5116  newval = oldsum + (int64) PG_GETARG_INT32(1);
5117 
5118  PG_RETURN_INT64(newval);
5119  }
5120 }
#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 newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#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 6227 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().

6228 {
6229  uint64 uval,
6230  newuval;
6231  NumericDigit *ptr;
6232  int ndigits;
6233 
6234  /* int64 can require at most 19 decimal digits; add one for safety */
6235  alloc_var(var, 20 / DEC_DIGITS);
6236  if (val < 0)
6237  {
6238  var->sign = NUMERIC_NEG;
6239  uval = -val;
6240  }
6241  else
6242  {
6243  var->sign = NUMERIC_POS;
6244  uval = val;
6245  }
6246  var->dscale = 0;
6247  if (val == 0)
6248  {
6249  var->ndigits = 0;
6250  var->weight = 0;
6251  return;
6252  }
6253  ptr = var->digits + var->ndigits;
6254  ndigits = 0;
6255  do
6256  {
6257  ptr--;
6258  ndigits++;
6259  newuval = uval / NBASE;
6260  *ptr = uval - newuval * NBASE;
6261  uval = newuval;
6262  } while (uval);
6263  var->digits = ptr;
6264  var->ndigits = ndigits;
6265  var->weight = ndigits - 1;
6266 }
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:5462
#define DEC_DIGITS
Definition: numeric.c:95
long val
Definition: informix.c:689
Datum int8_accum ( PG_FUNCTION_ARGS  )

Definition at line 4063 of file numeric.c.

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

4064 {
4066 
4067  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
4068 
4069  /* Create the state data on the first call */
4070  if (state == NULL)
4071  state = makeNumericAggState(fcinfo, true);
4072 
4073  if (!PG_ARGISNULL(1))
4074  {
4075  Numeric newval;
4076 
4078  PG_GETARG_DATUM(1)));
4079  do_numeric_accum(state, newval);
4080  }
4081 
4082  PG_RETURN_POINTER(state);
4083 }
#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:585
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3038
static NumericAggState * makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
Definition: numeric.c:3279
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3319
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
Datum int8_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4546 of file numeric.c.

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

4547 {
4549 
4550  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
4551 
4552  /* Should not get here with no state */
4553  if (state == NULL)
4554  elog(ERROR, "int8_accum_inv called with NULL state");
4555 
4556  if (!PG_ARGISNULL(1))
4557  {
4558  Numeric newval;
4559 
4561  PG_GETARG_DATUM(1)));
4562 
4563  /* Should never fail, all inputs have dscale 0 */
4564  if (!do_numeric_discard(state, newval))
4565  elog(ERROR, "do_numeric_discard failed unexpectedly");
4566  }
4567 
4568  PG_RETURN_POINTER(state);
4569 }
#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:585
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3384
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3038
#define ERROR
Definition: elog.h:43
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define elog
Definition: elog.h:219
Datum int8_avg ( PG_FUNCTION_ARGS  )

Definition at line 5320 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.

5321 {
5322  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
5323  Int8TransTypeData *transdata;
5324  Datum countd,
5325  sumd;
5326 
5327  if (ARR_HASNULL(transarray) ||
5328  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5329  elog(ERROR, "expected 2-element int8 array");
5330  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5331 
5332  /* SQL defines AVG of no values to be NULL */
5333  if (transdata->count == 0)
5334  PG_RETURN_NULL();
5335 
5337  Int64GetDatumFast(transdata->count));
5339  Int64GetDatumFast(transdata->sum));
5340 
5342 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3038
Datum numeric_div(PG_FUNCTION_ARGS)
Definition: numeric.c:2436
#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:587
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum int8_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 4289 of file numeric.c.

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

4290 {
4292 
4293  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4294 
4295  /* Create the state data on the first call */
4296  if (state == NULL)
4297  state = makePolyNumAggState(fcinfo, false);
4298 
4299  if (!PG_ARGISNULL(1))
4300  {
4301 #ifdef HAVE_INT128
4302  do_int128_accum(state, (int128) PG_GETARG_INT64(1));
4303 #else
4304  Numeric newval;
4305 
4307  PG_GETARG_DATUM(1)));
4308  do_numeric_accum(state, newval);
4309 #endif
4310  }
4311 
4312  PG_RETURN_POINTER(state);
4313 }
#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:585
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3038
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3319
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
#define makePolyNumAggState
Definition: numeric.c:4004
Datum int8_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4572 of file numeric.c.

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

4573 {
4575 
4576  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4577 
4578  /* Should not get here with no state */
4579  if (state == NULL)
4580  elog(ERROR, "int8_avg_accum_inv called with NULL state");
4581 
4582  if (!PG_ARGISNULL(1))
4583  {
4584 #ifdef HAVE_INT128
4585  do_int128_discard(state, (int128) PG_GETARG_INT64(1));
4586 #else
4587  Numeric newval;
4588 
4590  PG_GETARG_DATUM(1)));
4591 
4592  /* Should never fail, all inputs have dscale 0 */
4593  if (!do_numeric_discard(state, newval))
4594  elog(ERROR, "do_numeric_discard failed unexpectedly");
4595 #endif
4596  }
4597 
4598  PG_RETURN_POINTER(state);
4599 }
#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:585
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3384
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3038
#define ERROR
Definition: elog.h:43
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define DatumGetNumeric(X)
Definition: numeric.h:49
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 4320 of file numeric.c.

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

4321 {
4322  PolyNumAggState *state1;
4323  PolyNumAggState *state2;
4324  MemoryContext agg_context;
4325  MemoryContext old_context;
4326 
4327  if (!AggCheckCallContext(fcinfo, &agg_context))
4328  elog(ERROR, "aggregate function called in non-aggregate context");
4329 
4330  state1 = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4331  state2 = PG_ARGISNULL(1) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(1);
4332 
4333  if (state2 == NULL)
4334  PG_RETURN_POINTER(state1);
4335 
4336  /* manually copy all fields from state2 to state1 */
4337  if (state1 == NULL)
4338  {
4339  old_context = MemoryContextSwitchTo(agg_context);
4340 
4341  state1 = makePolyNumAggState(fcinfo, false);
4342  state1->N = state2->N;
4343 
4344 #ifdef HAVE_INT128
4345  state1->sumX = state2->sumX;
4346 #else
4347  accum_sum_copy(&state1->sumX, &state2->sumX);
4348 #endif
4349  MemoryContextSwitchTo(old_context);
4350 
4351  PG_RETURN_POINTER(state1);
4352  }
4353 
4354  if (state2->N > 0)
4355  {
4356  state1->N += state2->N;
4357 
4358 #ifdef HAVE_INT128
4359  state1->sumX += state2->sumX;
4360 #else
4361  /* The rest of this needs to work in the aggregate context */
4362  old_context = MemoryContextSwitchTo(agg_context);
4363 
4364  /* Accumulate sums */
4365  accum_sum_combine(&state1->sumX, &state2->sumX);
4366 
4367  MemoryContextSwitchTo(old_context);
4368 #endif
4369 
4370  }
4371  PG_RETURN_POINTER(state1);
4372 }
#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:9149
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
static void accum_sum_combine(NumericSumAccum *accum, NumericSumAccum *accum2)
Definition: numeric.c:9166
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4031
#define elog
Definition: elog.h:219
NumericSumAccum sumX
Definition: numeric.c:3267
#define makePolyNumAggState
Definition: numeric.c:4004
Datum int8_avg_deserialize ( PG_FUNCTION_ARGS  )

Definition at line 4437 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, numeric_recv(), pfree(), PG_GETARG_BYTEA_PP, PG_RETURN_POINTER, PointerGetDatum, pq_getmsgend(), pq_getmsgint64(), NumericAggState::sumX, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4438 {
4439  bytea *sstate;
4440  PolyNumAggState *result;
4442  Datum temp;
4443  NumericVar num;
4444 
4445  if (!AggCheckCallContext(fcinfo, NULL))
4446  elog(ERROR, "aggregate function called in non-aggregate context");
4447 
4448  sstate = PG_GETARG_BYTEA_PP(0);
4449 
4450  /*
4451  * Copy the bytea into a StringInfo so that we can "receive" it using the
4452  * standard recv-function infrastructure.
4453  */
4454  initStringInfo(&buf);
4456  VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
4457