PostgreSQL Source Code  git master
numeric.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include "catalog/pg_type.h"
#include "common/int.h"
#include "funcapi.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/hashutils.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) (X))
 
#define DatumGetNumericAbbrev(X)   ((int32) (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 Numeric make_result_opt_error (const NumericVar *var, bool *error)
 
static void apply_typmod (NumericVar *var, int32 typmod)
 
static bool numericvar_to_int32 (const NumericVar *var, int32 *result)
 
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_support (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 in_range_numeric_numeric (PG_FUNCTION_ARGS)
 
Datum hash_numeric (PG_FUNCTION_ARGS)
 
Datum hash_numeric_extended (PG_FUNCTION_ARGS)
 
Datum numeric_add (PG_FUNCTION_ARGS)
 
Numeric numeric_add_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_sub (PG_FUNCTION_ARGS)
 
Numeric numeric_sub_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_mul (PG_FUNCTION_ARGS)
 
Numeric numeric_mul_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_div (PG_FUNCTION_ARGS)
 
Numeric numeric_div_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_div_trunc (PG_FUNCTION_ARGS)
 
Datum numeric_mod (PG_FUNCTION_ARGS)
 
Numeric numeric_mod_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
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)
 
static int get_min_scale (NumericVar *var)
 
Datum numeric_min_scale (PG_FUNCTION_ARGS)
 
Datum numeric_trim_scale (PG_FUNCTION_ARGS)
 
Datum int4_numeric (PG_FUNCTION_ARGS)
 
int32 numeric_int4_opt_error (Numeric num, bool *have_error)
 
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

◆ DatumGetNumericAbbrev

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

Definition at line 364 of file numeric.c.

Referenced by numeric_cmp_abbrev().

◆ DEC_DIGITS

◆ digitbuf_alloc

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

Definition at line 446 of file numeric.c.

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

◆ digitbuf_free

#define digitbuf_free (   buf)
Value:
do { \
if ((buf) != NULL) \
pfree(buf); \
} while (0)
static char * buf
Definition: pg_test_fsync.c:67

Definition at line 448 of file numeric.c.

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

◆ DIV_GUARD_DIGITS

#define DIV_GUARD_DIGITS   4

Definition at line 100 of file numeric.c.

Referenced by div_var_fast().

◆ dump_numeric

#define dump_numeric (   s,
 
)

Definition at line 442 of file numeric.c.

Referenced by int2int4_sum(), and make_result_opt_error().

◆ dump_var

#define dump_var (   s,
 
)

Definition at line 443 of file numeric.c.

Referenced by int2int4_sum().

◆ HALF_NBASE

#define HALF_NBASE   5000

Definition at line 97 of file numeric.c.

Referenced by div_var(), and round_var().

◆ init_var

◆ makePolyNumAggState

#define makePolyNumAggState   makeNumericAggState

◆ makePolyNumAggStateCurrentContext

#define makePolyNumAggStateCurrentContext   makeNumericAggStateCurrentContext

Definition at line 4353 of file numeric.c.

Referenced by int8_avg_deserialize(), and numeric_poly_deserialize().

◆ MUL_GUARD_DIGITS

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

Definition at line 99 of file numeric.c.

Referenced by mul_var().

◆ NBASE

◆ NUMERIC_ABBREV_BITS

#define NUMERIC_ABBREV_BITS   (SIZEOF_DATUM * BITS_PER_BYTE)

Definition at line 357 of file numeric.c.

◆ NUMERIC_ABBREV_NAN

#define NUMERIC_ABBREV_NAN   NumericAbbrevGetDatum(PG_INT32_MIN)

Definition at line 365 of file numeric.c.

Referenced by numeric_abbrev_convert().

◆ NUMERIC_CAN_BE_SHORT

#define NUMERIC_CAN_BE_SHORT (   scale,
  weight 
)
Value:
(weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
#define NUMERIC_SHORT_WEIGHT_MAX
Definition: numeric.c:200
int scale
Definition: pgbench.c:153
#define NUMERIC_SHORT_WEIGHT_MIN
Definition: numeric.c:201
#define NUMERIC_SHORT_DSCALE_MAX
Definition: numeric.c:196

Definition at line 460 of file numeric.c.

Referenced by make_result_opt_error(), and numeric().

◆ NUMERIC_DIGITS

#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:184

Definition at line 456 of file numeric.c.

Referenced by cmp_numerics(), hash_numeric(), hash_numeric_extended(), init_var_from_num(), int2int4_sum(), make_result_opt_error(), and set_var_from_num().

◆ NUMERIC_DSCALE

#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:184
#define NUMERIC_DSCALE_MASK
Definition: numeric.c:207
#define NUMERIC_SHORT_DSCALE_MASK
Definition: numeric.c:194
#define NUMERIC_SHORT_DSCALE_SHIFT
Definition: numeric.c:195

Definition at line 213 of file numeric.c.

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

◆ NUMERIC_DSCALE_MASK

#define NUMERIC_DSCALE_MASK   0x3FFF

Definition at line 207 of file numeric.c.

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

◆ NUMERIC_FLAGBITS

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

Definition at line 172 of file numeric.c.

◆ NUMERIC_HDRSZ

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

Definition at line 176 of file numeric.c.

Referenced by make_result_opt_error(), and numeric_maximum_size().

◆ NUMERIC_HDRSZ_SHORT

#define NUMERIC_HDRSZ_SHORT   (VARHDRSZ + sizeof(uint16))

Definition at line 177 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_HEADER_IS_SHORT

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

Definition at line 184 of file numeric.c.

◆ NUMERIC_HEADER_SIZE

#define NUMERIC_HEADER_SIZE (   n)
Value:
(VARHDRSZ + sizeof(uint16) + \
(NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
signed short int16
Definition: c.h:346
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:184
#define VARHDRSZ
Definition: c.h:562
unsigned short uint16
Definition: c.h:358

Definition at line 185 of file numeric.c.

◆ NUMERIC_IS_NAN

◆ NUMERIC_IS_SHORT

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

Definition at line 174 of file numeric.c.

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

◆ NUMERIC_NAN

#define NUMERIC_NAN   0xC000

◆ NUMERIC_NDIGITS

◆ NUMERIC_NEG

◆ NUMERIC_POS

◆ NUMERIC_SHORT

#define NUMERIC_SHORT   0x8000

Definition at line 169 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SHORT_DSCALE_MASK

#define NUMERIC_SHORT_DSCALE_MASK   0x1F80

Definition at line 194 of file numeric.c.

Referenced by numeric().

◆ NUMERIC_SHORT_DSCALE_MAX

#define NUMERIC_SHORT_DSCALE_MAX   (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)

Definition at line 196 of file numeric.c.

◆ NUMERIC_SHORT_DSCALE_SHIFT

#define NUMERIC_SHORT_DSCALE_SHIFT   7

Definition at line 195 of file numeric.c.

Referenced by make_result_opt_error(), and numeric().

◆ NUMERIC_SHORT_SIGN_MASK

#define NUMERIC_SHORT_SIGN_MASK   0x2000

Definition at line 193 of file numeric.c.

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

◆ NUMERIC_SHORT_WEIGHT_MASK

#define NUMERIC_SHORT_WEIGHT_MASK   0x003F

Definition at line 199 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SHORT_WEIGHT_MAX

#define NUMERIC_SHORT_WEIGHT_MAX   NUMERIC_SHORT_WEIGHT_MASK

Definition at line 200 of file numeric.c.

◆ NUMERIC_SHORT_WEIGHT_MIN

#define NUMERIC_SHORT_WEIGHT_MIN   (-(NUMERIC_SHORT_WEIGHT_MASK+1))

Definition at line 201 of file numeric.c.

◆ NUMERIC_SHORT_WEIGHT_SIGN_MASK

#define NUMERIC_SHORT_WEIGHT_SIGN_MASK   0x0040

Definition at line 198 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SIGN

#define NUMERIC_SIGN (   n)
Value:
(((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
NUMERIC_NEG : NUMERIC_POS) : NUMERIC_FLAGBITS(n))
#define NUMERIC_POS
Definition: numeric.c:167
#define NUMERIC_FLAGBITS(n)
Definition: numeric.c:172
#define NUMERIC_SHORT_SIGN_MASK
Definition: numeric.c:193
#define NUMERIC_IS_SHORT(n)
Definition: numeric.c:174

Definition at line 209 of file numeric.c.

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

◆ NUMERIC_SIGN_MASK

#define NUMERIC_SIGN_MASK   0xC000

Definition at line 166 of file numeric.c.

◆ NUMERIC_WEIGHT

#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:184
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK
Definition: numeric.c:198
#define NUMERIC_SHORT_WEIGHT_MASK
Definition: numeric.c:199

Definition at line 217 of file numeric.c.

Referenced by cmp_numerics(), hash_numeric(), hash_numeric_extended(), init_var_from_num(), int2int4_sum(), make_result_opt_error(), numeric(), and set_var_from_num().

◆ NumericAbbrevGetDatum

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

Definition at line 363 of file numeric.c.

Referenced by numeric_cmp_abbrev().

Typedef Documentation

◆ Int8TransTypeData

◆ NumericAggState

◆ NumericDigit

Definition at line 102 of file numeric.c.

◆ NumericSumAccum

◆ NumericVar

typedef struct NumericVar NumericVar

◆ PolyNumAggState

Definition at line 4351 of file numeric.c.

Function Documentation

◆ accum_sum_add()

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

Definition at line 9267 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(), numeric_poly_deserialize(), and numeric_stddev_pop().

9268 {
9269  int32 *accum_digits;
9270  int i,
9271  val_i;
9272  int val_ndigits;
9273  NumericDigit *val_digits;
9274 
9275  /*
9276  * If we have accumulated too many values since the last carry
9277  * propagation, do it now, to avoid overflowing. (We could allow more
9278  * than NBASE - 1, if we reserved two extra digits, rather than one, for
9279  * carry propagation. But even with NBASE - 1, this needs to be done so
9280  * seldom, that the performance difference is negligible.)
9281  */
9282  if (accum->num_uncarried == NBASE - 1)
9283  accum_sum_carry(accum);
9284 
9285  /*
9286  * Adjust the weight or scale of the old value, so that it can accommodate
9287  * the new value.
9288  */
9289  accum_sum_rescale(accum, val);
9290 
9291  /* */
9292  if (val->sign == NUMERIC_POS)
9293  accum_digits = accum->pos_digits;
9294  else
9295  accum_digits = accum->neg_digits;
9296 
9297  /* copy these values into local vars for speed in loop */
9298  val_ndigits = val->ndigits;
9299  val_digits = val->digits;
9300 
9301  i = accum->weight - val->weight;
9302  for (val_i = 0; val_i < val_ndigits; val_i++)
9303  {
9304  accum_digits[i] += (int32) val_digits[val_i];
9305  i++;
9306  }
9307 
9308  accum->num_uncarried++;
9309 }
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:9388
#define NUMERIC_POS
Definition: numeric.c:167
int32 * neg_digits
Definition: numeric.c:347
int num_uncarried
Definition: numeric.c:344
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:9315
signed int int32
Definition: c.h:347
int16 NumericDigit
Definition: numeric.c:102
#define NBASE
Definition: numeric.c:96
int i
int32 * pos_digits
Definition: numeric.c:346
long val
Definition: informix.c:664

◆ accum_sum_carry()

static void accum_sum_carry ( NumericSumAccum accum)
static

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

9316 {
9317  int i;
9318  int ndigits;
9319  int32 *dig;
9320  int32 carry;
9321  int32 newdig = 0;
9322 
9323  /*
9324  * If no new values have been added since last carry propagation, nothing
9325  * to do.
9326  */
9327  if (accum->num_uncarried == 0)
9328  return;
9329 
9330  /*
9331  * We maintain that the weight of the accumulator is always one larger
9332  * than needed to hold the current value, before carrying, to make sure
9333  * there is enough space for the possible extra digit when carry is
9334  * propagated. We cannot expand the buffer here, unless we require
9335  * callers of accum_sum_final() to switch to the right memory context.
9336  */
9337  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
9338 
9339  ndigits = accum->ndigits;
9340 
9341  /* Propagate carry in the positive sum */
9342  dig = accum->pos_digits;
9343  carry = 0;
9344  for (i = ndigits - 1; i >= 0; i--)
9345  {
9346  newdig = dig[i] + carry;
9347  if (newdig >= NBASE)
9348  {
9349  carry = newdig / NBASE;
9350  newdig -= carry * NBASE;
9351  }
9352  else
9353  carry = 0;
9354  dig[i] = newdig;
9355  }
9356  /* Did we use up the digit reserved for carry propagation? */
9357  if (newdig > 0)
9358  accum->have_carry_space = false;
9359 
9360  /* And the same for the negative sum */
9361  dig = accum->neg_digits;
9362  carry = 0;
9363  for (i = ndigits - 1; i >= 0; i--)
9364  {
9365  newdig = dig[i] + carry;
9366  if (newdig >= NBASE)
9367  {
9368  carry = newdig / NBASE;
9369  newdig -= carry * NBASE;
9370  }
9371  else
9372  carry = 0;
9373  dig[i] = newdig;
9374  }
9375  if (newdig > 0)
9376  accum->have_carry_space = false;
9377 
9378  accum->num_uncarried = 0;
9379 }
int32 * neg_digits
Definition: numeric.c:347
int num_uncarried
Definition: numeric.c:344
signed int int32
Definition: c.h:347
bool have_carry_space
Definition: numeric.c:345
#define NBASE
Definition: numeric.c:96
#define Assert(condition)
Definition: c.h:739
int i
int32 * pos_digits
Definition: numeric.c:346

◆ accum_sum_combine()

static void accum_sum_combine ( NumericSumAccum accum,
NumericSumAccum accum2 
)
static

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

9546 {
9547  NumericVar tmp_var;
9548 
9549  init_var(&tmp_var);
9550 
9551  accum_sum_final(accum2, &tmp_var);
9552  accum_sum_add(accum, &tmp_var);
9553 
9554  free_var(&tmp_var);
9555 }
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:9477
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:9267
static void free_var(NumericVar *var)
Definition: numeric.c:5826
#define init_var(v)
Definition: numeric.c:454

◆ accum_sum_copy()

static void accum_sum_copy ( NumericSumAccum dst,
NumericSumAccum src 
)
static

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

9529 {
9530  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
9531  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
9532 
9533  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
9534  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
9535  dst->num_uncarried = src->num_uncarried;
9536  dst->ndigits = src->ndigits;
9537  dst->weight = src->weight;
9538  dst->dscale = src->dscale;
9539 }
int32 * neg_digits
Definition: numeric.c:347
int num_uncarried
Definition: numeric.c:344
signed int int32
Definition: c.h:347
void * palloc(Size size)
Definition: mcxt.c:949
int32 * pos_digits
Definition: numeric.c:346

◆ accum_sum_final()

static void accum_sum_final ( NumericSumAccum accum,
NumericVar result 
)
static

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

9478 {
9479  int i;
9480  NumericVar pos_var;
9481  NumericVar neg_var;
9482 
9483  if (accum->ndigits == 0)
9484  {
9485  set_var_from_var(&const_zero, result);
9486  return;
9487  }
9488 
9489  /* Perform final carry */
9490  accum_sum_carry(accum);
9491 
9492  /* Create NumericVars representing the positive and negative sums */
9493  init_var(&pos_var);
9494  init_var(&neg_var);
9495 
9496  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
9497  pos_var.weight = neg_var.weight = accum->weight;
9498  pos_var.dscale = neg_var.dscale = accum->dscale;
9499  pos_var.sign = NUMERIC_POS;
9500  neg_var.sign = NUMERIC_NEG;
9501 
9502  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
9503  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
9504 
9505  for (i = 0; i < accum->ndigits; i++)
9506  {
9507  Assert(accum->pos_digits[i] < NBASE);
9508  pos_var.digits[i] = (int16) accum->pos_digits[i];
9509 
9510  Assert(accum->neg_digits[i] < NBASE);
9511  neg_var.digits[i] = (int16) accum->neg_digits[i];
9512  }
9513 
9514  /* And add them together */
9515  add_var(&pos_var, &neg_var, result);
9516 
9517  /* Remove leading/trailing zeroes */
9518  strip_var(result);
9519 }
signed short int16
Definition: c.h:346
int weight
Definition: numeric.c:275
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:9210
int32 * neg_digits
Definition: numeric.c:347
#define digitbuf_alloc(ndigits)
Definition: numeric.c:446
int ndigits
Definition: numeric.c:274
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:9315
int dscale
Definition: numeric.c:277
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
NumericDigit * buf
Definition: numeric.c:278
#define NBASE
Definition: numeric.c:96
static const NumericVar const_zero
Definition: numeric.c:374
#define Assert(condition)
Definition: c.h:739
NumericDigit * digits
Definition: numeric.c:279
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6888
int i
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6076
int32 * pos_digits
Definition: numeric.c:346
#define init_var(v)
Definition: numeric.c:454

◆ accum_sum_rescale()

static void accum_sum_rescale ( NumericSumAccum accum,
const NumericVar val 
)
static

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

9389 {
9390  int old_weight = accum->weight;
9391  int old_ndigits = accum->ndigits;
9392  int accum_ndigits;
9393  int accum_weight;
9394  int accum_rscale;
9395  int val_rscale;
9396 
9397  accum_weight = old_weight;
9398  accum_ndigits = old_ndigits;
9399 
9400  /*
9401  * Does the new value have a larger weight? If so, enlarge the buffers,
9402  * and shift the existing value to the new weight, by adding leading
9403  * zeros.
9404  *
9405  * We enforce that the accumulator always has a weight one larger than
9406  * needed for the inputs, so that we have space for an extra digit at the
9407  * final carry-propagation phase, if necessary.
9408  */
9409  if (val->weight >= accum_weight)
9410  {
9411  accum_weight = val->weight + 1;
9412  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
9413  }
9414 
9415  /*
9416  * Even though the new value is small, we might've used up the space
9417  * reserved for the carry digit in the last call to accum_sum_carry(). If
9418  * so, enlarge to make room for another one.
9419  */
9420  else if (!accum->have_carry_space)
9421  {
9422  accum_weight++;
9423  accum_ndigits++;
9424  }
9425 
9426  /* Is the new value wider on the right side? */
9427  accum_rscale = accum_ndigits - accum_weight - 1;
9428  val_rscale = val->ndigits - val->weight - 1;
9429  if (val_rscale > accum_rscale)
9430  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
9431 
9432  if (accum_ndigits != old_ndigits ||
9433  accum_weight != old_weight)
9434  {
9435  int32 *new_pos_digits;
9436  int32 *new_neg_digits;
9437  int weightdiff;
9438 
9439  weightdiff = accum_weight - old_weight;
9440 
9441  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
9442  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
9443 
9444  if (accum->pos_digits)
9445  {
9446  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
9447  old_ndigits * sizeof(int32));
9448  pfree(accum->pos_digits);
9449 
9450  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
9451  old_ndigits * sizeof(int32));
9452  pfree(accum->neg_digits);
9453  }
9454 
9455  accum->pos_digits = new_pos_digits;
9456  accum->neg_digits = new_neg_digits;
9457 
9458  accum->weight = accum_weight;
9459  accum->ndigits = accum_ndigits;
9460 
9461  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
9462  accum->have_carry_space = true;
9463  }
9464 
9465  if (val->dscale > accum->dscale)
9466  accum->dscale = val->dscale;
9467 }
int weight
Definition: numeric.c:275
int32 * neg_digits
Definition: numeric.c:347
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
signed int int32
Definition: c.h:347
void pfree(void *pointer)
Definition: mcxt.c:1056
bool have_carry_space
Definition: numeric.c:345
void * palloc0(Size size)
Definition: mcxt.c:980
#define Assert(condition)
Definition: c.h:739
int32 * pos_digits
Definition: numeric.c:346

◆ accum_sum_reset()

static void accum_sum_reset ( NumericSumAccum accum)
static

Definition at line 9251 of file numeric.c.

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

Referenced by do_numeric_discard().

9252 {
9253  int i;
9254 
9255  accum->dscale = 0;
9256  for (i = 0; i < accum->ndigits; i++)
9257  {
9258  accum->pos_digits[i] = 0;
9259  accum->neg_digits[i] = 0;
9260  }
9261 }
int32 * neg_digits
Definition: numeric.c:347
int i
int32 * pos_digits
Definition: numeric.c:346

◆ add_abs()

static void add_abs ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

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

8876 {
8877  NumericDigit *res_buf;
8878  NumericDigit *res_digits;
8879  int res_ndigits;
8880  int res_weight;
8881  int res_rscale,
8882  rscale1,
8883  rscale2;
8884  int res_dscale;
8885  int i,
8886  i1,
8887  i2;
8888  int carry = 0;
8889 
8890  /* copy these values into local vars for speed in inner loop */
8891  int var1ndigits = var1->ndigits;
8892  int var2ndigits = var2->ndigits;
8893  NumericDigit *var1digits = var1->digits;
8894  NumericDigit *var2digits = var2->digits;
8895 
8896  res_weight = Max(var1->weight, var2->weight) + 1;
8897 
8898  res_dscale = Max(var1->dscale, var2->dscale);
8899 
8900  /* Note: here we are figuring rscale in base-NBASE digits */
8901  rscale1 = var1->ndigits - var1->weight - 1;
8902  rscale2 = var2->ndigits - var2->weight - 1;
8903  res_rscale = Max(rscale1, rscale2);
8904 
8905  res_ndigits = res_rscale + res_weight + 1;
8906  if (res_ndigits <= 0)
8907  res_ndigits = 1;
8908 
8909  res_buf = digitbuf_alloc(res_ndigits + 1);
8910  res_buf[0] = 0; /* spare digit for later rounding */
8911  res_digits = res_buf + 1;
8912 
8913  i1 = res_rscale + var1->weight + 1;
8914  i2 = res_rscale + var2->weight + 1;
8915  for (i = res_ndigits - 1; i >= 0; i--)
8916  {
8917  i1--;
8918  i2--;
8919  if (i1 >= 0 && i1 < var1ndigits)
8920  carry += var1digits[i1];
8921  if (i2 >= 0 && i2 < var2ndigits)
8922  carry += var2digits[i2];
8923 
8924  if (carry >= NBASE)
8925  {
8926  res_digits[i] = carry - NBASE;
8927  carry = 1;
8928  }
8929  else
8930  {
8931  res_digits[i] = carry;
8932  carry = 0;
8933  }
8934  }
8935 
8936  Assert(carry == 0); /* else we failed to allow for carry out */
8937 
8938  digitbuf_free(result->buf);
8939  result->ndigits = res_ndigits;
8940  result->buf = res_buf;
8941  result->digits = res_digits;
8942  result->weight = res_weight;
8943  result->dscale = res_dscale;
8944 
8945  /* Remove leading/trailing zeroes */
8946  strip_var(result);
8947 }
int weight
Definition: numeric.c:275
static void strip_var(NumericVar *var)
Definition: numeric.c:9210
#define digitbuf_alloc(ndigits)
Definition: numeric.c:446
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
#define digitbuf_free(buf)
Definition: numeric.c:448
int16 NumericDigit
Definition: numeric.c:102
NumericDigit * buf
Definition: numeric.c:278
#define NBASE
Definition: numeric.c:96
#define Assert(condition)
Definition: c.h:739
NumericDigit * digits
Definition: numeric.c:279
int i
#define Max(x, y)
Definition: numeric.c:13

◆ add_var()

static void add_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

Definition at line 6888 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(), in_range_numeric_numeric(), ln_var(), numeric_add_opt_error(), numeric_inc(), sqrt_var(), and width_bucket_numeric().

6889 {
6890  /*
6891  * Decide on the signs of the two variables what to do
6892  */
6893  if (var1->sign == NUMERIC_POS)
6894  {
6895  if (var2->sign == NUMERIC_POS)
6896  {
6897  /*
6898  * Both are positive result = +(ABS(var1) + ABS(var2))
6899  */
6900  add_abs(var1, var2, result);
6901  result->sign = NUMERIC_POS;
6902  }
6903  else
6904  {
6905  /*
6906  * var1 is positive, var2 is negative Must compare absolute values
6907  */
6908  switch (cmp_abs(var1, var2))
6909  {
6910  case 0:
6911  /* ----------
6912  * ABS(var1) == ABS(var2)
6913  * result = ZERO
6914  * ----------
6915  */
6916  zero_var(result);
6917  result->dscale = Max(var1->dscale, var2->dscale);
6918  break;
6919 
6920  case 1:
6921  /* ----------
6922  * ABS(var1) > ABS(var2)
6923  * result = +(ABS(var1) - ABS(var2))
6924  * ----------
6925  */
6926  sub_abs(var1, var2, result);
6927  result->sign = NUMERIC_POS;
6928  break;
6929 
6930  case -1:
6931  /* ----------
6932  * ABS(var1) < ABS(var2)
6933  * result = -(ABS(var2) - ABS(var1))
6934  * ----------
6935  */
6936  sub_abs(var2, var1, result);
6937  result->sign = NUMERIC_NEG;
6938  break;
6939  }
6940  }
6941  }
6942  else
6943  {
6944  if (var2->sign == NUMERIC_POS)
6945  {
6946  /* ----------
6947  * var1 is negative, var2 is positive
6948  * Must compare absolute values
6949  * ----------
6950  */
6951  switch (cmp_abs(var1, var2))
6952  {
6953  case 0:
6954  /* ----------
6955  * ABS(var1) == ABS(var2)
6956  * result = ZERO
6957  * ----------
6958  */
6959  zero_var(result);
6960  result->dscale = Max(var1->dscale, var2->dscale);
6961  break;
6962 
6963  case 1:
6964  /* ----------
6965  * ABS(var1) > ABS(var2)
6966  * result = -(ABS(var1) - ABS(var2))
6967  * ----------
6968  */
6969  sub_abs(var1, var2, result);
6970  result->sign = NUMERIC_NEG;
6971  break;
6972 
6973  case -1:
6974  /* ----------
6975  * ABS(var1) < ABS(var2)
6976  * result = +(ABS(var2) - ABS(var1))
6977  * ----------
6978  */
6979  sub_abs(var2, var1, result);
6980  result->sign = NUMERIC_POS;
6981  break;
6982  }
6983  }
6984  else
6985  {
6986  /* ----------
6987  * Both are negative
6988  * result = -(ABS(var1) + ABS(var2))
6989  * ----------
6990  */
6991  add_abs(var1, var2, result);
6992  result->sign = NUMERIC_NEG;
6993  }
6994  }
6995 }
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8960
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8875
#define NUMERIC_POS
Definition: numeric.c:167
int dscale
Definition: numeric.c:277
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
static void zero_var(NumericVar *var)
Definition: numeric.c:5842
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:8797
#define Max(x, y)
Definition: numeric.c:13

◆ alloc_var()

static void alloc_var ( NumericVar var,
int  ndigits 
)
static

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

5811 {
5812  digitbuf_free(var->buf);
5813  var->buf = digitbuf_alloc(ndigits + 1);
5814  var->buf[0] = 0; /* spare digit for rounding */
5815  var->digits = var->buf + 1;
5816  var->ndigits = ndigits;
5817 }
#define digitbuf_alloc(ndigits)
Definition: numeric.c:446
int ndigits
Definition: numeric.c:274
#define digitbuf_free(buf)
Definition: numeric.c:448
NumericDigit * buf
Definition: numeric.c:278
NumericDigit * digits
Definition: numeric.c:279

◆ apply_typmod()

static void apply_typmod ( NumericVar var,
int32  typmod 
)
static

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

6456 {
6457  int precision;
6458  int scale;
6459  int maxdigits;
6460  int ddigits;
6461  int i;
6462 
6463  /* Do nothing if we have a default typmod (-1) */
6464  if (typmod < (int32) (VARHDRSZ))
6465  return;
6466 
6467  typmod -= VARHDRSZ;
6468  precision = (typmod >> 16) & 0xffff;
6469  scale = typmod & 0xffff;
6470  maxdigits = precision - scale;
6471 
6472  /* Round to target scale (and set var->dscale) */
6473  round_var(var, scale);
6474 
6475  /*
6476  * Check for overflow - note we can't do this before rounding, because
6477  * rounding could raise the weight. Also note that the var's weight could
6478  * be inflated by leading zeroes, which will be stripped before storage
6479  * but perhaps might not have been yet. In any case, we must recognize a
6480  * true zero, whose weight doesn't mean anything.
6481  */
6482  ddigits = (var->weight + 1) * DEC_DIGITS;
6483  if (ddigits > maxdigits)
6484  {
6485  /* Determine true weight; and check for all-zero result */
6486  for (i = 0; i < var->ndigits; i++)
6487  {
6488  NumericDigit dig = var->digits[i];
6489 
6490  if (dig)
6491  {
6492  /* Adjust for any high-order decimal zero digits */
6493 #if DEC_DIGITS == 4
6494  if (dig < 10)
6495  ddigits -= 3;
6496  else if (dig < 100)
6497  ddigits -= 2;
6498  else if (dig < 1000)
6499  ddigits -= 1;
6500 #elif DEC_DIGITS == 2
6501  if (dig < 10)
6502  ddigits -= 1;
6503 #elif DEC_DIGITS == 1
6504  /* no adjustment */
6505 #else
6506 #error unsupported NBASE
6507 #endif
6508  if (ddigits > maxdigits)
6509  ereport(ERROR,
6510  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6511  errmsg("numeric field overflow"),
6512  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
6513  precision, scale,
6514  /* Display 10^0 as 1 */
6515  maxdigits ? "10^" : "",
6516  maxdigits ? maxdigits : 1
6517  )));
6518  break;
6519  }
6520  ddigits -= DEC_DIGITS;
6521  }
6522  }
6523 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:9042
int weight
Definition: numeric.c:275
#define VARHDRSZ
Definition: c.h:562
int errcode(int sqlerrcode)
Definition: elog.c:608
int scale
Definition: pgbench.c:153
int ndigits
Definition: numeric.c:274
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:102
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define ereport(elevel, rest)
Definition: elog.h:141
int maxdigits
Definition: informix.c:665
NumericDigit * digits
Definition: numeric.c:279
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define DEC_DIGITS
Definition: numeric.c:98

◆ ceil_var()

static void ceil_var ( const NumericVar var,
NumericVar result 
)
static

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

8002 {
8003  NumericVar tmp;
8004 
8005  init_var(&tmp);
8006  set_var_from_var(var, &tmp);
8007 
8008  trunc_var(&tmp, 0);
8009 
8010  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
8011  add_var(&tmp, &const_one, &tmp);
8012 
8013  set_var_from_var(&tmp, result);
8014  free_var(&tmp);
8015 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:9148
static const NumericVar const_one
Definition: numeric.c:378
#define NUMERIC_POS
Definition: numeric.c:167
int sign
Definition: numeric.c:276
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6830
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6888
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6076
#define init_var(v)
Definition: numeric.c:454

◆ cmp_abs()

static int cmp_abs ( const NumericVar var1,
const NumericVar var2 
)
static

Definition at line 8797 of file numeric.c.

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

Referenced by add_var(), and sub_var().

8798 {
8799  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
8800  var2->digits, var2->ndigits, var2->weight);
8801 }
int weight
Definition: numeric.c:275
int ndigits
Definition: numeric.c:274
NumericDigit * digits
Definition: numeric.c:279
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:8811

◆ cmp_abs_common()

static int cmp_abs_common ( const NumericDigit var1digits,
int  var1ndigits,
int  var1weight,
const NumericDigit var2digits,
int  var2ndigits,
int  var2weight 
)
static

Definition at line 8811 of file numeric.c.

References stat.

Referenced by cmp_abs(), and cmp_var_common().

8813 {
8814  int i1 = 0;
8815  int i2 = 0;
8816 
8817  /* Check any digits before the first common digit */
8818 
8819  while (var1weight > var2weight && i1 < var1ndigits)
8820  {
8821  if (var1digits[i1++] != 0)
8822  return 1;
8823  var1weight--;
8824  }
8825  while (var2weight > var1weight && i2 < var2ndigits)
8826  {
8827  if (var2digits[i2++] != 0)
8828  return -1;
8829  var2weight--;
8830  }
8831 
8832  /* At this point, either w1 == w2 or we've run out of digits */
8833 
8834  if (var1weight == var2weight)
8835  {
8836  while (i1 < var1ndigits && i2 < var2ndigits)
8837  {
8838  int stat = var1digits[i1++] - var2digits[i2++];
8839 
8840  if (stat)
8841  {
8842  if (stat > 0)
8843  return 1;
8844  return -1;
8845  }
8846  }
8847  }
8848 
8849  /*
8850  * At this point, we've run out of digits on one side or the other; so any
8851  * remaining nonzero digits imply that side is larger
8852  */
8853  while (i1 < var1ndigits)
8854  {
8855  if (var1digits[i1++] != 0)
8856  return 1;
8857  }
8858  while (i2 < var2ndigits)
8859  {
8860  if (var2digits[i2++] != 0)
8861  return -1;
8862  }
8863 
8864  return 0;
8865 }
#define stat(a, b)
Definition: win32_port.h:255

◆ cmp_numerics()

static int cmp_numerics ( Numeric  num1,
Numeric  num2 
)
static

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

2153 {
2154  int result;
2155 
2156  /*
2157  * We consider all NANs to be equal and larger than any non-NAN. This is
2158  * somewhat arbitrary; the important thing is to have a consistent sort
2159  * order.
2160  */
2161  if (NUMERIC_IS_NAN(num1))
2162  {
2163  if (NUMERIC_IS_NAN(num2))
2164  result = 0; /* NAN = NAN */
2165  else
2166  result = 1; /* NAN > non-NAN */
2167  }
2168  else if (NUMERIC_IS_NAN(num2))
2169  {
2170  result = -1; /* non-NAN < NAN */
2171  }
2172  else
2173  {
2174  result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
2175  NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
2176  NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
2177  NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
2178  }
2179 
2180  return result;
2181 }
#define NUMERIC_DIGITS(num)
Definition: numeric.c:456
#define NUMERIC_SIGN(n)
Definition: numeric.c:209
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:458
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:6845
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:217
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173

◆ cmp_var()

static int cmp_var ( const NumericVar var1,
const NumericVar var2 
)
static

Definition at line 6830 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(), in_range_numeric_numeric(), ln_var(), numeric_power(), numeric_stddev_internal(), power_var(), and sqrt_var().

6831 {
6832  return cmp_var_common(var1->digits, var1->ndigits,
6833  var1->weight, var1->sign,
6834  var2->digits, var2->ndigits,
6835  var2->weight, var2->sign);
6836 }
int weight
Definition: numeric.c:275
int ndigits
Definition: numeric.c:274
int sign
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)
Definition: numeric.c:6845
NumericDigit * digits
Definition: numeric.c:279

◆ cmp_var_common()

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 6845 of file numeric.c.

References cmp_abs_common(), NUMERIC_NEG, and NUMERIC_POS.

Referenced by cmp_numerics(), and cmp_var().

6849 {
6850  if (var1ndigits == 0)
6851  {
6852  if (var2ndigits == 0)
6853  return 0;
6854  if (var2sign == NUMERIC_NEG)
6855  return 1;
6856  return -1;
6857  }
6858  if (var2ndigits == 0)
6859  {
6860  if (var1sign == NUMERIC_POS)
6861  return 1;
6862  return -1;
6863  }
6864 
6865  if (var1sign == NUMERIC_POS)
6866  {
6867  if (var2sign == NUMERIC_NEG)
6868  return 1;
6869  return cmp_abs_common(var1digits, var1ndigits, var1weight,
6870  var2digits, var2ndigits, var2weight);
6871  }
6872 
6873  if (var2sign == NUMERIC_POS)
6874  return -1;
6875 
6876  return cmp_abs_common(var2digits, var2ndigits, var2weight,
6877  var1digits, var1ndigits, var1weight);
6878 }
#define NUMERIC_POS
Definition: numeric.c:167
#define NUMERIC_NEG
Definition: numeric.c:168
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:8811

◆ compute_bucket()

static void compute_bucket ( Numeric  operand,
Numeric  bound1,
Numeric  bound2,
const NumericVar count_var,
NumericVar result_var 
)
static

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

1581 {
1582  NumericVar bound1_var;
1583  NumericVar bound2_var;
1584  NumericVar operand_var;
1585 
1586  init_var_from_num(bound1, &bound1_var);
1587  init_var_from_num(bound2, &bound2_var);
1588  init_var_from_num(operand, &operand_var);
1589 
1590  if (cmp_var(&bound1_var, &bound2_var) < 0)
1591  {
1592  sub_var(&operand_var, &bound1_var, &operand_var);
1593  sub_var(&bound2_var, &bound1_var, &bound2_var);
1594  div_var(&operand_var, &bound2_var, result_var,
1595  select_div_scale(&operand_var, &bound2_var), true);
1596  }
1597  else
1598  {
1599  sub_var(&bound1_var, &operand_var, &operand_var);
1600  sub_var(&bound1_var, &bound2_var, &bound1_var);
1601  div_var(&operand_var, &bound1_var, result_var,
1602  select_div_scale(&operand_var, &bound1_var), true);
1603  }
1604 
1605  mul_var(result_var, count_var, result_var,
1606  result_var->dscale + count_var->dscale);
1607  add_var(result_var, &const_one, result_var);
1608  floor_var(result_var, result_var);
1609 
1610  free_var(&bound1_var);
1611  free_var(&bound2_var);
1612  free_var(&operand_var);
1613 }
static const NumericVar const_one
Definition: numeric.c:378
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:7323
int dscale
Definition: numeric.c:277
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7903
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6059
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6830
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:7126
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6888
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7005
static void floor_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:8025

◆ div_var()

static void div_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 7323 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_opt_error(), numeric_div_trunc(), numeric_stddev_internal(), and power_var_int().

7325 {
7326  int div_ndigits;
7327  int res_ndigits;
7328  int res_sign;
7329  int res_weight;
7330  int carry;
7331  int borrow;
7332  int divisor1;
7333  int divisor2;
7334  NumericDigit *dividend;
7335  NumericDigit *divisor;
7336  NumericDigit *res_digits;
7337  int i;
7338  int j;
7339 
7340  /* copy these values into local vars for speed in inner loop */
7341  int var1ndigits = var1->ndigits;
7342  int var2ndigits = var2->ndigits;
7343 
7344  /*
7345  * First of all division by zero check; we must not be handed an
7346  * unnormalized divisor.
7347  */
7348  if (var2ndigits == 0 || var2->digits[0] == 0)
7349  ereport(ERROR,
7350  (errcode(ERRCODE_DIVISION_BY_ZERO),
7351  errmsg("division by zero")));
7352 
7353  /*
7354  * Now result zero check
7355  */
7356  if (var1ndigits == 0)
7357  {
7358  zero_var(result);
7359  result->dscale = rscale;
7360  return;
7361  }
7362 
7363  /*
7364  * Determine the result sign, weight and number of digits to calculate.
7365  * The weight figured here is correct if the emitted quotient has no
7366  * leading zero digits; otherwise strip_var() will fix things up.
7367  */
7368  if (var1->sign == var2->sign)
7369  res_sign = NUMERIC_POS;
7370  else
7371  res_sign = NUMERIC_NEG;
7372  res_weight = var1->weight - var2->weight;
7373  /* The number of accurate result digits we need to produce: */
7374  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
7375  /* ... but always at least 1 */
7376  res_ndigits = Max(res_ndigits, 1);
7377  /* If rounding needed, figure one more digit to ensure correct result */
7378  if (round)
7379  res_ndigits++;
7380 
7381  /*
7382  * The working dividend normally requires res_ndigits + var2ndigits
7383  * digits, but make it at least var1ndigits so we can load all of var1
7384  * into it. (There will be an additional digit dividend[0] in the
7385  * dividend space, but for consistency with Knuth's notation we don't
7386  * count that in div_ndigits.)
7387  */
7388  div_ndigits = res_ndigits + var2ndigits;
7389  div_ndigits = Max(div_ndigits, var1ndigits);
7390 
7391  /*
7392  * We need a workspace with room for the working dividend (div_ndigits+1
7393  * digits) plus room for the possibly-normalized divisor (var2ndigits
7394  * digits). It is convenient also to have a zero at divisor[0] with the
7395  * actual divisor data in divisor[1 .. var2ndigits]. Transferring the
7396  * digits into the workspace also allows us to realloc the result (which
7397  * might be the same as either input var) before we begin the main loop.
7398  * Note that we use palloc0 to ensure that divisor[0], dividend[0], and
7399  * any additional dividend positions beyond var1ndigits, start out 0.
7400  */
7401  dividend = (NumericDigit *)
7402  palloc0((div_ndigits + var2ndigits + 2) * sizeof(NumericDigit));
7403  divisor = dividend + (div_ndigits + 1);
7404  memcpy(dividend + 1, var1->digits, var1ndigits * sizeof(NumericDigit));
7405  memcpy(divisor + 1, var2->digits, var2ndigits * sizeof(NumericDigit));
7406 
7407  /*
7408  * Now we can realloc the result to hold the generated quotient digits.
7409  */
7410  alloc_var(result, res_ndigits);
7411  res_digits = result->digits;
7412 
7413  if (var2ndigits == 1)
7414  {
7415  /*
7416  * If there's only a single divisor digit, we can use a fast path (cf.
7417  * Knuth section 4.3.1 exercise 16).
7418  */
7419  divisor1 = divisor[1];
7420  carry = 0;
7421  for (i = 0; i < res_ndigits; i++)
7422  {
7423  carry = carry * NBASE + dividend[i + 1];
7424  res_digits[i] = carry / divisor1;
7425  carry = carry % divisor1;
7426  }
7427  }
7428  else
7429  {
7430  /*
7431  * The full multiple-place algorithm is taken from Knuth volume 2,
7432  * Algorithm 4.3.1D.
7433  *
7434  * We need the first divisor digit to be >= NBASE/2. If it isn't,
7435  * make it so by scaling up both the divisor and dividend by the
7436  * factor "d". (The reason for allocating dividend[0] above is to
7437  * leave room for possible carry here.)
7438  */
7439  if (divisor[1] < HALF_NBASE)
7440  {
7441  int d = NBASE / (divisor[1] + 1);
7442 
7443  carry = 0;
7444  for (i = var2ndigits; i > 0; i--)
7445  {
7446  carry += divisor[i] * d;
7447  divisor[i] = carry % NBASE;
7448  carry = carry / NBASE;
7449  }
7450  Assert(carry == 0);
7451  carry = 0;
7452  /* at this point only var1ndigits of dividend can be nonzero */
7453  for (i = var1ndigits; i >= 0; i--)
7454  {
7455  carry += dividend[i] * d;
7456  dividend[i] = carry % NBASE;
7457  carry = carry / NBASE;
7458  }
7459  Assert(carry == 0);
7460  Assert(divisor[1] >= HALF_NBASE);
7461  }
7462  /* First 2 divisor digits are used repeatedly in main loop */
7463  divisor1 = divisor[1];
7464  divisor2 = divisor[2];
7465 
7466  /*
7467  * Begin the main loop. Each iteration of this loop produces the j'th
7468  * quotient digit by dividing dividend[j .. j + var2ndigits] by the
7469  * divisor; this is essentially the same as the common manual
7470  * procedure for long division.
7471  */
7472  for (j = 0; j < res_ndigits; j++)
7473  {
7474  /* Estimate quotient digit from the first two dividend digits */
7475  int next2digits = dividend[j] * NBASE + dividend[j + 1];
7476  int qhat;
7477 
7478  /*
7479  * If next2digits are 0, then quotient digit must be 0 and there's
7480  * no need to adjust the working dividend. It's worth testing
7481  * here to fall out ASAP when processing trailing zeroes in a
7482  * dividend.
7483  */
7484  if (next2digits == 0)
7485  {
7486  res_digits[j] = 0;
7487  continue;
7488  }
7489 
7490  if (dividend[j] == divisor1)
7491  qhat = NBASE - 1;
7492  else
7493  qhat = next2digits / divisor1;
7494 
7495  /*
7496  * Adjust quotient digit if it's too large. Knuth proves that
7497  * after this step, the quotient digit will be either correct or
7498  * just one too large. (Note: it's OK to use dividend[j+2] here
7499  * because we know the divisor length is at least 2.)
7500  */
7501  while (divisor2 * qhat >
7502  (next2digits - qhat * divisor1) * NBASE + dividend[j + 2])
7503  qhat--;
7504 
7505  /* As above, need do nothing more when quotient digit is 0 */
7506  if (qhat > 0)
7507  {
7508  /*
7509  * Multiply the divisor by qhat, and subtract that from the
7510  * working dividend. "carry" tracks the multiplication,
7511  * "borrow" the subtraction (could we fold these together?)
7512  */
7513  carry = 0;
7514  borrow = 0;
7515  for (i = var2ndigits; i >= 0; i--)
7516  {
7517  carry += divisor[i] * qhat;
7518  borrow -= carry % NBASE;
7519  carry = carry / NBASE;
7520  borrow += dividend[j + i];
7521  if (borrow < 0)
7522  {
7523  dividend[j + i] = borrow + NBASE;
7524  borrow = -1;
7525  }
7526  else
7527  {
7528  dividend[j + i] = borrow;
7529  borrow = 0;
7530  }
7531  }
7532  Assert(carry == 0);
7533 
7534  /*
7535  * If we got a borrow out of the top dividend digit, then
7536  * indeed qhat was one too large. Fix it, and add back the
7537  * divisor to correct the working dividend. (Knuth proves
7538  * that this will occur only about 3/NBASE of the time; hence,
7539  * it's a good idea to test this code with small NBASE to be
7540  * sure this section gets exercised.)
7541  */
7542  if (borrow)
7543  {
7544  qhat--;
7545  carry = 0;
7546  for (i = var2ndigits; i >= 0; i--)
7547  {
7548  carry += dividend[j + i] + divisor[i];
7549  if (carry >= NBASE)
7550  {
7551  dividend[j + i] = carry - NBASE;
7552  carry = 1;
7553  }
7554  else
7555  {
7556  dividend[j + i] = carry;
7557  carry = 0;
7558  }
7559  }
7560  /* A carry should occur here to cancel the borrow above */
7561  Assert(carry == 1);
7562  }
7563  }
7564 
7565  /* And we're done with this quotient digit */
7566  res_digits[j] = qhat;
7567  }
7568  }
7569 
7570  pfree(dividend);
7571 
7572  /*
7573  * Finally, round or truncate the result to the requested precision.
7574  */
7575  result->weight = res_weight;
7576  result->sign = res_sign;
7577 
7578  /* Round or truncate to target rscale (and set result->dscale) */
7579  if (round)
7580  round_var(result, rscale);
7581  else
7582  trunc_var(result, rscale);
7583 
7584  /* Strip leading and trailing zeroes */
7585  strip_var(result);
7586 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:9042
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:9148
int weight
Definition: numeric.c:275
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:9210
int errcode(int sqlerrcode)
Definition: elog.c:608
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
static void zero_var(NumericVar *var)
Definition: numeric.c:5842
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:102
#define HALF_NBASE
Definition: numeric.c:97
#define ereport(elevel, rest)
Definition: elog.h:141
#define NBASE
Definition: numeric.c:96
void * palloc0(Size size)
Definition: mcxt.c:980
#define Assert(condition)
Definition: c.h:739
NumericDigit * digits
Definition: numeric.c:279
int errmsg(const char *fmt,...)
Definition: elog.c:822
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:5810
int i
#define Max(x, y)
Definition: numeric.c:13
#define DEC_DIGITS
Definition: numeric.c:98

◆ div_var_fast()

static void div_var_fast ( const NumericVar var1,
const NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

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

7610 {
7611  int div_ndigits;
7612  int res_sign;
7613  int res_weight;
7614  int *div;
7615  int qdigit;
7616  int carry;
7617  int maxdiv;
7618  int newdig;
7619  NumericDigit *res_digits;
7620  double fdividend,
7621  fdivisor,
7622  fdivisorinverse,
7623  fquotient;
7624  int qi;
7625  int i;
7626 
7627  /* copy these values into local vars for speed in inner loop */
7628  int var1ndigits = var1->ndigits;
7629  int var2ndigits = var2->ndigits;
7630  NumericDigit *var1digits = var1->digits;
7631  NumericDigit *var2digits = var2->digits;
7632 
7633  /*
7634  * First of all division by zero check; we must not be handed an
7635  * unnormalized divisor.
7636  */
7637  if (var2ndigits == 0 || var2digits[0] == 0)
7638  ereport(ERROR,
7639  (errcode(ERRCODE_DIVISION_BY_ZERO),
7640  errmsg("division by zero")));
7641 
7642  /*
7643  * Now result zero check
7644  */
7645  if (var1ndigits == 0)
7646  {
7647  zero_var(result);
7648  result->dscale = rscale;
7649  return;
7650  }
7651 
7652  /*
7653  * Determine the result sign, weight and number of digits to calculate
7654  */
7655  if (var1->sign == var2->sign)
7656  res_sign = NUMERIC_POS;
7657  else
7658  res_sign = NUMERIC_NEG;
7659  res_weight = var1->weight - var2->weight + 1;
7660  /* The number of accurate result digits we need to produce: */
7661  div_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
7662  /* Add guard digits for roundoff error */
7663  div_ndigits += DIV_GUARD_DIGITS;
7664  if (div_ndigits < DIV_GUARD_DIGITS)
7665  div_ndigits = DIV_GUARD_DIGITS;
7666  /* Must be at least var1ndigits, too, to simplify data-loading loop */
7667  if (div_ndigits < var1ndigits)
7668  div_ndigits = var1ndigits;
7669 
7670  /*
7671  * We do the arithmetic in an array "div[]" of signed int's. Since
7672  * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
7673  * to avoid normalizing carries immediately.
7674  *
7675  * We start with div[] containing one zero digit followed by the
7676  * dividend's digits (plus appended zeroes to reach the desired precision
7677  * including guard digits). Each step of the main loop computes an
7678  * (approximate) quotient digit and stores it into div[], removing one
7679  * position of dividend space. A final pass of carry propagation takes
7680  * care of any mistaken quotient digits.
7681  */
7682  div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
7683  for (i = 0; i < var1ndigits; i++)
7684  div[i + 1] = var1digits[i];
7685 
7686  /*
7687  * We estimate each quotient digit using floating-point arithmetic, taking
7688  * the first four digits of the (current) dividend and divisor. This must
7689  * be float to avoid overflow. The quotient digits will generally be off
7690  * by no more than one from the exact answer.
7691  */
7692  fdivisor = (double) var2digits[0];
7693  for (i = 1; i < 4; i++)
7694  {
7695  fdivisor *= NBASE;
7696  if (i < var2ndigits)
7697  fdivisor += (double) var2digits[i];
7698  }
7699  fdivisorinverse = 1.0 / fdivisor;
7700 
7701  /*
7702  * maxdiv tracks the maximum possible absolute value of any div[] entry;
7703  * when this threatens to exceed INT_MAX, we take the time to propagate
7704  * carries. Furthermore, we need to ensure that overflow doesn't occur
7705  * during the carry propagation passes either. The carry values may have
7706  * an absolute value as high as INT_MAX/NBASE + 1, so really we must
7707  * normalize when digits threaten to exceed INT_MAX - INT_MAX/NBASE - 1.
7708  *
7709  * To avoid overflow in maxdiv itself, it represents the max absolute
7710  * value divided by NBASE-1, ie, at the top of the loop it is known that
7711  * no div[] entry has an absolute value exceeding maxdiv * (NBASE-1).
7712  *
7713  * Actually, though, that holds good only for div[] entries after div[qi];
7714  * the adjustment done at the bottom of the loop may cause div[qi + 1] to
7715  * exceed the maxdiv limit, so that div[qi] in the next iteration is
7716  * beyond the limit. This does not cause problems, as explained below.
7717  */
7718  maxdiv = 1;
7719 
7720  /*
7721  * Outer loop computes next quotient digit, which will go into div[qi]
7722  */
7723  for (qi = 0; qi < div_ndigits; qi++)
7724  {
7725  /* Approximate the current dividend value */
7726  fdividend = (double) div[qi];
7727  for (i = 1; i < 4; i++)
7728  {
7729  fdividend *= NBASE;
7730  if (qi + i <= div_ndigits)
7731  fdividend += (double) div[qi + i];
7732  }
7733  /* Compute the (approximate) quotient digit */
7734  fquotient = fdividend * fdivisorinverse;
7735  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7736  (((int) fquotient) - 1); /* truncate towards -infinity */
7737 
7738  if (qdigit != 0)
7739  {
7740  /* Do we need to normalize now? */
7741  maxdiv += Abs(qdigit);
7742  if (maxdiv > (INT_MAX - INT_MAX / NBASE - 1) / (NBASE - 1))
7743  {
7744  /* Yes, do it */
7745  carry = 0;
7746  for (i = div_ndigits; i > qi; i--)
7747  {
7748  newdig = div[i] + carry;
7749  if (newdig < 0)
7750  {
7751  carry = -((-newdig - 1) / NBASE) - 1;
7752  newdig -= carry * NBASE;
7753  }
7754  else if (newdig >= NBASE)
7755  {
7756  carry = newdig / NBASE;
7757  newdig -= carry * NBASE;
7758  }
7759  else
7760  carry = 0;
7761  div[i] = newdig;
7762  }
7763  newdig = div[qi] + carry;
7764  div[qi] = newdig;
7765 
7766  /*
7767  * All the div[] digits except possibly div[qi] are now in the
7768  * range 0..NBASE-1. We do not need to consider div[qi] in
7769  * the maxdiv value anymore, so we can reset maxdiv to 1.
7770  */
7771  maxdiv = 1;
7772 
7773  /*
7774  * Recompute the quotient digit since new info may have
7775  * propagated into the top four dividend digits
7776  */
7777  fdividend = (double) div[qi];
7778  for (i = 1; i < 4; i++)
7779  {
7780  fdividend *= NBASE;
7781  if (qi + i <= div_ndigits)
7782  fdividend += (double) div[qi + i];
7783  }
7784  /* Compute the (approximate) quotient digit */
7785  fquotient = fdividend * fdivisorinverse;
7786  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7787  (((int) fquotient) - 1); /* truncate towards -infinity */
7788  maxdiv += Abs(qdigit);
7789  }
7790 
7791  /*
7792  * Subtract off the appropriate multiple of the divisor.
7793  *
7794  * The digits beyond div[qi] cannot overflow, because we know they
7795  * will fall within the maxdiv limit. As for div[qi] itself, note
7796  * that qdigit is approximately trunc(div[qi] / vardigits[0]),
7797  * which would make the new value simply div[qi] mod vardigits[0].
7798  * The lower-order terms in qdigit can change this result by not
7799  * more than about twice INT_MAX/NBASE, so overflow is impossible.
7800  */
7801  if (qdigit != 0)
7802  {
7803  int istop = Min(var2ndigits, div_ndigits - qi + 1);
7804 
7805  for (i = 0; i < istop; i++)
7806  div[qi + i] -= qdigit * var2digits[i];
7807  }
7808  }
7809 
7810  /*
7811  * The dividend digit we are about to replace might still be nonzero.
7812  * Fold it into the next digit position.
7813  *
7814  * There is no risk of overflow here, although proving that requires
7815  * some care. Much as with the argument for div[qi] not overflowing,
7816  * if we consider the first two terms in the numerator and denominator
7817  * of qdigit, we can see that the final value of div[qi + 1] will be
7818  * approximately a remainder mod (vardigits[0]*NBASE + vardigits[1]).
7819  * Accounting for the lower-order terms is a bit complicated but ends
7820  * up adding not much more than INT_MAX/NBASE to the possible range.
7821  * Thus, div[qi + 1] cannot overflow here, and in its role as div[qi]
7822  * in the next loop iteration, it can't be large enough to cause
7823  * overflow in the carry propagation step (if any), either.
7824  *
7825  * But having said that: div[qi] can be more than INT_MAX/NBASE, as
7826  * noted above, which means that the product div[qi] * NBASE *can*
7827  * overflow. When that happens, adding it to div[qi + 1] will always
7828  * cause a canceling overflow so that the end result is correct. We
7829  * could avoid the intermediate overflow by doing the multiplication
7830  * and addition in int64 arithmetic, but so far there appears no need.
7831  */
7832  div[qi + 1] += div[qi] * NBASE;
7833 
7834  div[qi] = qdigit;
7835  }
7836 
7837  /*
7838  * Approximate and store the last quotient digit (div[div_ndigits])
7839  */
7840  fdividend = (double) div[qi];
7841  for (i = 1; i < 4; i++)
7842  fdividend *= NBASE;
7843  fquotient = fdividend * fdivisorinverse;
7844  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
7845  (((int) fquotient) - 1); /* truncate towards -infinity */
7846  div[qi] = qdigit;
7847 
7848  /*
7849  * Because the quotient digits might be off by one, some of them might be
7850  * -1 or NBASE at this point. The represented value is correct in a
7851  * mathematical sense, but it doesn't look right. We do a final carry
7852  * propagation pass to normalize the digits, which we combine with storing
7853  * the result digits into the output. Note that this is still done at
7854  * full precision w/guard digits.
7855  */
7856  alloc_var(result, div_ndigits + 1);
7857  res_digits = result->digits;
7858  carry = 0;
7859  for (i = div_ndigits; i >= 0; i--)
7860  {
7861  newdig = div[i] + carry;
7862  if (newdig < 0)
7863  {
7864  carry = -((-newdig - 1) / NBASE) - 1;
7865  newdig -= carry * NBASE;
7866  }
7867  else if (newdig >= NBASE)
7868  {
7869  carry = newdig / NBASE;
7870  newdig -= carry * NBASE;
7871  }
7872  else
7873  carry = 0;
7874  res_digits[i] = newdig;
7875  }
7876  Assert(carry == 0);
7877 
7878  pfree(div);
7879 
7880  /*
7881  * Finally, round the result to the requested precision.
7882  */
7883  result->weight = res_weight;
7884  result->sign = res_sign;
7885 
7886  /* Round to target rscale (and set result->dscale) */
7887  if (round)
7888  round_var(result, rscale);
7889  else
7890  trunc_var(result, rscale);
7891 
7892  /* Strip leading and trailing zeroes */
7893  strip_var(result);
7894 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:9042
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:9148
int weight
Definition: numeric.c:275
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:9210
int errcode(int sqlerrcode)
Definition: elog.c:608
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
#define Min(x, y)
Definition: numeric.c:14
#define Abs(x)
Definition: c.h:917
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
static void zero_var(NumericVar *var)
Definition: numeric.c:5842
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:102
#define ereport(elevel, rest)
Definition: elog.h:141
#define NBASE
Definition: numeric.c:96
void * palloc0(Size size)
Definition: mcxt.c:980
#define Assert(condition)
Definition: c.h:739
NumericDigit * digits
Definition: numeric.c:279
#define DIV_GUARD_DIGITS
Definition: numeric.c:100
int errmsg(const char *fmt,...)
Definition: elog.c:822
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:5810
int i
#define DEC_DIGITS
Definition: numeric.c:98

◆ do_numeric_accum()

static void do_numeric_accum ( NumericAggState state,
Numeric  newval 
)
static

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

3668 {
3669  NumericVar X;
3670  NumericVar X2;
3671  MemoryContext old_context;
3672 
3673  /* Count NaN inputs separately from all else */
3674  if (NUMERIC_IS_NAN(newval))
3675  {
3676  state->NaNcount++;
3677  return;
3678  }
3679 
3680  /* load processed number in short-lived context */
3681  init_var_from_num(newval, &X);
3682 
3683  /*
3684  * Track the highest input dscale that we've seen, to support inverse
3685  * transitions (see do_numeric_discard).
3686  */
3687  if (X.dscale > state->maxScale)
3688  {
3689  state->maxScale = X.dscale;
3690  state->maxScaleCount = 1;
3691  }
3692  else if (X.dscale == state->maxScale)
3693  state->maxScaleCount++;
3694 
3695  /* if we need X^2, calculate that in short-lived context */
3696  if (state->calcSumX2)
3697  {
3698  init_var(&X2);
3699  mul_var(&X, &X, &X2, X.dscale * 2);
3700  }
3701 
3702  /* The rest of this needs to work in the aggregate context */
3703  old_context = MemoryContextSwitchTo(state->agg_context);
3704 
3705  state->N++;
3706 
3707  /* Accumulate sums */
3708  accum_sum_add(&(state->sumX), &X);
3709 
3710  if (state->calcSumX2)
3711  accum_sum_add(&(state->sumX2), &X2);
3712 
3713  MemoryContextSwitchTo(old_context);
3714 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:3613
int dscale
Definition: numeric.c:277
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:9267
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6059
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:7126
NumericSumAccum sumX2
Definition: numeric.c:3616
int64 NaNcount
Definition: numeric.c:3619
int64 maxScaleCount
Definition: numeric.c:3618
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173
NumericSumAccum sumX
Definition: numeric.c:3615
#define init_var(v)
Definition: numeric.c:454

◆ do_numeric_discard()

static bool do_numeric_discard ( NumericAggState state,
Numeric  newval 
)
static

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

3733 {
3734  NumericVar X;
3735  NumericVar X2;
3736  MemoryContext old_context;
3737 
3738  /* Count NaN inputs separately from all else */
3739  if (NUMERIC_IS_NAN(newval))
3740  {
3741  state->NaNcount--;
3742  return true;
3743  }
3744 
3745  /* load processed number in short-lived context */
3746  init_var_from_num(newval, &X);
3747 
3748  /*
3749  * state->sumX's dscale is the maximum dscale of any of the inputs.
3750  * Removing the last input with that dscale would require us to recompute
3751  * the maximum dscale of the *remaining* inputs, which we cannot do unless
3752  * no more non-NaN inputs remain at all. So we report a failure instead,
3753  * and force the aggregation to be redone from scratch.
3754  */
3755  if (X.dscale == state->maxScale)
3756  {
3757  if (state->maxScaleCount > 1 || state->maxScale == 0)
3758  {
3759  /*
3760  * Some remaining inputs have same dscale, or dscale hasn't gotten
3761  * above zero anyway
3762  */
3763  state->maxScaleCount--;
3764  }
3765  else if (state->N == 1)
3766  {
3767  /* No remaining non-NaN inputs at all, so reset maxScale */
3768  state->maxScale = 0;
3769  state->maxScaleCount = 0;
3770  }
3771  else
3772  {
3773  /* Correct new maxScale is uncertain, must fail */
3774  return false;
3775  }
3776  }
3777 
3778  /* if we need X^2, calculate that in short-lived context */
3779  if (state->calcSumX2)
3780  {
3781  init_var(&X2);
3782  mul_var(&X, &X, &X2, X.dscale * 2);
3783  }
3784 
3785  /* The rest of this needs to work in the aggregate context */
3786  old_context = MemoryContextSwitchTo(state->agg_context);
3787 
3788  if (state->N-- > 1)
3789  {
3790  /* Negate X, to subtract it from the sum */
3791  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
3792  accum_sum_add(&(state->sumX), &X);
3793 
3794  if (state->calcSumX2)
3795  {
3796  /* Negate X^2. X^2 is always positive */
3797  X2.sign = NUMERIC_NEG;
3798  accum_sum_add(&(state->sumX2), &X2);
3799  }
3800  }
3801  else
3802  {
3803  /* Zero the sums */
3804  Assert(state->N == 0);
3805 
3806  accum_sum_reset(&state->sumX);
3807  if (state->calcSumX2)
3808  accum_sum_reset(&state->sumX2);
3809  }
3810 
3811  MemoryContextSwitchTo(old_context);
3812 
3813  return true;
3814 }
#define NUMERIC_POS
Definition: numeric.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:3613
int dscale
Definition: numeric.c:277
#define NUMERIC_NEG
Definition: numeric.c:168
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:9267
int sign
Definition: numeric.c:276
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6059
#define Assert(condition)
Definition: c.h:739
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:7126
NumericSumAccum sumX2
Definition: numeric.c:3616
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:9251
int64 NaNcount
Definition: numeric.c:3619
int64 maxScaleCount
Definition: numeric.c:3618
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173
NumericSumAccum sumX
Definition: numeric.c:3615
#define init_var(v)
Definition: numeric.c:454

◆ estimate_ln_dweight()

static int estimate_ln_dweight ( const NumericVar var)
static

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

8248 {
8249  int ln_dweight;
8250 
8251  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
8252  cmp_var(var, &const_one_point_one) <= 0)
8253  {
8254  /*
8255  * 0.9 <= var <= 1.1
8256  *
8257  * ln(var) has a negative weight (possibly very large). To get a
8258  * reasonably accurate result, estimate it using ln(1+x) ~= x.
8259  */
8260  NumericVar x;
8261 
8262  init_var(&x);
8263  sub_var(var, &const_one, &x);
8264 
8265  if (x.ndigits > 0)
8266  {
8267  /* Use weight of most significant decimal digit of x */
8268  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
8269  }
8270  else
8271  {
8272  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
8273  ln_dweight = 0;
8274  }
8275 
8276  free_var(&x);
8277  }
8278  else
8279  {
8280  /*
8281  * Estimate the logarithm using the first couple of digits from the
8282  * input number. This will give an accurate result whenever the input
8283  * is not too close to 1.
8284  */
8285  if (var->ndigits > 0)
8286  {
8287  int digits;
8288  int dweight;
8289  double ln_var;
8290 
8291  digits = var->digits[0];
8292  dweight = var->weight * DEC_DIGITS;
8293 
8294  if (var->ndigits > 1)
8295  {
8296  digits = digits * NBASE + var->digits[1];
8297  dweight -= DEC_DIGITS;
8298  }
8299 
8300  /*----------
8301  * We have var ~= digits * 10^dweight
8302  * so ln(var) ~= ln(digits) + dweight * ln(10)
8303  *----------
8304  */
8305  ln_var = log((double) digits) + dweight * 2.302585092994046;
8306  ln_dweight = (int) log10(Abs(ln_var));
8307  }
8308  else
8309  {
8310  /* Caller should fail on ln(0), but for the moment return zero */
8311  ln_dweight = 0;
8312  }
8313  }
8314 
8315  return ln_dweight;
8316 }
int weight
Definition: numeric.c:275
static const NumericVar const_zero_point_nine
Definition: numeric.c:412
static const NumericVar const_one
Definition: numeric.c:378
static void ln_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:8325
int ndigits
Definition: numeric.c:274
#define Abs(x)
Definition: c.h:917
#define NBASE
Definition: numeric.c:96
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6830
static void free_var(NumericVar *var)
Definition: numeric.c:5826
NumericDigit * digits
Definition: numeric.c:279
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7005
#define DEC_DIGITS
Definition: numeric.c:98
static const NumericVar const_one_point_one
Definition: numeric.c:422
#define init_var(v)
Definition: numeric.c:454
int digits
Definition: informix.c:666

◆ exp_var()

static void exp_var ( const NumericVar arg,
NumericVar result,
int  rscale 
)
static

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

8122 {
8123  NumericVar x;
8124  NumericVar elem;
8125  NumericVar ni;
8126  double val;
8127  int dweight;
8128  int ndiv2;
8129  int sig_digits;
8130  int local_rscale;
8131 
8132  init_var(&x);
8133  init_var(&elem);
8134  init_var(&ni);
8135 
8136  set_var_from_var(arg, &x);
8137 
8138  /*
8139  * Estimate the dweight of the result using floating point arithmetic, so
8140  * that we can choose an appropriate local rscale for the calculation.
8141  */
8143 
8144  /* Guard against overflow */
8145  /* If you change this limit, see also power_var()'s limit */
8146  if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
8147  ereport(ERROR,
8148  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8149  errmsg("value overflows numeric format")));
8150 
8151  /* decimal weight = log10(e^x) = x * log10(e) */
8152  dweight = (int) (val * 0.434294481903252);
8153 
8154  /*
8155  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
8156  * 2^n, to improve the convergence rate of the Taylor series.
8157  */
8158  if (Abs(val) > 0.01)
8159  {
8160  NumericVar tmp;
8161 
8162  init_var(&tmp);
8163  set_var_from_var(&const_two, &tmp);
8164 
8165  ndiv2 = 1;
8166  val /= 2;
8167 
8168  while (Abs(val) > 0.01)
8169  {
8170  ndiv2++;
8171  val /= 2;
8172  add_var(&tmp, &tmp, &tmp);
8173  }
8174 
8175  local_rscale = x.dscale + ndiv2;
8176  div_var_fast(&x, &tmp, &x, local_rscale, true);
8177 
8178  free_var(&tmp);
8179  }
8180  else
8181  ndiv2 = 0;
8182 
8183  /*
8184  * Set the scale for the Taylor series expansion. The final result has
8185  * (dweight + rscale + 1) significant digits. In addition, we have to
8186  * raise the Taylor series result to the power 2^ndiv2, which introduces
8187  * an error of up to around log10(2^ndiv2) digits, so work with this many
8188  * extra digits of precision (plus a few more for good measure).
8189  */
8190  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
8191  sig_digits = Max(sig_digits, 0) + 8;
8192 
8193  local_rscale = sig_digits - 1;
8194 
8195  /*
8196  * Use the Taylor series
8197  *
8198  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
8199  *
8200  * Given the limited range of x, this should converge reasonably quickly.
8201  * We run the series until the terms fall below the local_rscale limit.
8202  */
8203  add_var(&const_one, &x, result);
8204 
8205  mul_var(&x, &x, &elem, local_rscale);
8206  set_var_from_var(&const_two, &ni);
8207  div_var_fast(&elem, &ni, &elem, local_rscale, true);
8208 
8209  while (elem.ndigits != 0)
8210  {
8211  add_var(result, &elem, result);
8212 
8213  mul_var(&elem, &x, &elem, local_rscale);
8214  add_var(&ni, &const_one, &ni);
8215  div_var_fast(&elem, &ni, &elem, local_rscale, true);
8216  }
8217 
8218  /*
8219  * Compensate for the argument range reduction. Since the weight of the
8220  * result doubles with each multiplication, we can reduce the local rscale
8221  * as we proceed.
8222  */
8223  while (ndiv2-- > 0)
8224  {
8225  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
8226  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
8227  mul_var(result, result, result, local_rscale);
8228  }
8229 
8230  /* Round to requested rscale */
8231  round_var(result, rscale);
8232 
8233  free_var(&x);
8234  free_var(&elem);
8235  free_var(&ni);
8236 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:9042
int weight
Definition: numeric.c:275
static const NumericVar const_one
Definition: numeric.c:378
int errcode(int sqlerrcode)
Definition: elog.c:608
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
#define Abs(x)
Definition: c.h:917
#define ERROR
Definition: elog.h:43
static double numericvar_to_double_no_overflow(const NumericVar *var)
Definition: numeric.c:6798
#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:7608
#define ereport(elevel, rest)
Definition: elog.h:141
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static const NumericVar const_two
Definition: numeric.c:382
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:7126
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6888
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define Max(x, y)
Definition: numeric.c:13
#define DEC_DIGITS
Definition: numeric.c:98
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6076
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:454

◆ float4_numeric()

Datum float4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3545 of file numeric.c.

References buf, ereport, errcode(), errmsg(), ERROR, free_var(), init_var, isinf(), make_result(), PG_GETARG_FLOAT4, PG_RETURN_NUMERIC, set_var_from_str(), snprintf, and val.

3546 {
3548  Numeric res;
3549  NumericVar result;
3550  char buf[FLT_DIG + 100];
3551 
3552  if (isnan(val))
3554 
3555  if (isinf(val))
3556  ereport(ERROR,
3557  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3558  errmsg("cannot convert infinity to numeric")));
3559 
3560  snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
3561 
3562  init_var(&result);
3563 
3564  /* Assume we need not worry about leading/trailing spaces */
3565  (void) set_var_from_str(buf, buf, &result);
3566 
3567  res = make_result(&result);
3568 
3569  free_var(&result);
3570 
3571  PG_RETURN_NUMERIC(res);
3572 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
int isinf(double x)
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:275
#define ereport(elevel, rest)
Definition: elog.h:141
float float4
Definition: c.h:491
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:5866
static const NumericVar const_nan
Definition: numeric.c:425
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:6442
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define snprintf
Definition: port.h:192
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:454

◆ float8_numeric()

Datum float8_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3474 of file numeric.c.

References buf, ereport, errcode(), errmsg(), ERROR, free_var(), init_var, isinf(), make_result(), PG_GETARG_FLOAT8, PG_RETURN_NUMERIC, set_var_from_str(), snprintf, and val.

Referenced by executeItemOptUnwrapTarget(), and SV_to_JsonbValue().

3475 {
3477  Numeric res;
3478  NumericVar result;
3479  char buf[DBL_DIG + 100];
3480 
3481  if (isnan(val))
3483 
3484  if (isinf(val))
3485  ereport(ERROR,
3486  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3487  errmsg("cannot convert infinity to numeric")));
3488 
3489  snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
3490 
3491  init_var(&result);
3492 
3493  /* Assume we need not worry about leading/trailing spaces */
3494  (void) set_var_from_str(buf, buf, &result);
3495 
3496  res = make_result(&result);
3497 
3498  free_var(&result);
3499 
3500  PG_RETURN_NUMERIC(res);
3501 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:492
int isinf(double x)
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:141
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:5866
static const NumericVar const_nan
Definition: numeric.c:425
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:6442
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define snprintf
Definition: port.h:192
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:454

◆ floor_var()

static void floor_var ( const NumericVar var,
NumericVar result 
)
static

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

8026 {
8027  NumericVar tmp;
8028 
8029  init_var(&tmp);
8030  set_var_from_var(var, &tmp);
8031 
8032  trunc_var(&tmp, 0);
8033 
8034  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
8035  sub_var(&tmp, &const_one, &tmp);
8036 
8037  set_var_from_var(&tmp, result);
8038  free_var(&tmp);
8039 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:9148
static const NumericVar const_one
Definition: numeric.c:378
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6830
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7005
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6076
#define init_var(v)
Definition: numeric.c:454

◆ free_var()

static void free_var ( NumericVar var)
static

Definition at line 5826 of file numeric.c.

References NumericVar::buf, digitbuf_free, NumericVar::digits, NUMERIC_NAN, and NumericVar::sign.

Referenced by accum_sum_combine(), ceil_var(), compute_bucket(), estimate_ln_dweight(), exp_var(), float4_numeric(), float8_numeric(), floor_var(), get_str_from_var_sci(), in_range_numeric_numeric(), int2_numeric(), int4_numeric(), int64_to_numericvar(), int8_avg_serialize(), int8_numeric(), ln_var(), log_var(), mod_var(), numeric(), numeric_add_opt_error(), numeric_avg(), numeric_avg_serialize(), numeric_ceil(), numeric_div_opt_error(), numeric_div_trunc(), numeric_exp(), numeric_fac(), numeric_floor(), numeric_in(), numeric_inc(), numeric_ln(), numeric_log(), numeric_min_scale(), numeric_mod_opt_error(), numeric_mul_opt_error(), numeric_poly_avg(), numeric_poly_serialize(), numeric_poly_sum(), numeric_power(), numeric_recv(), numeric_round(), numeric_serialize(), numeric_sign(), numeric_sqrt(), numeric_stddev_internal(), numeric_stddev_pop(), numeric_sub_opt_error(), numeric_sum(), numeric_trim_scale(), numeric_trunc(), numericvar_to_int64(), power_var(), power_var_int(), sqrt_var(), and width_bucket_numeric().

5827 {
5828  digitbuf_free(var->buf);
5829  var->buf = NULL;
5830  var->digits = NULL;
5831  var->sign = NUMERIC_NAN;
5832 }
int sign
Definition: numeric.c:276
#define digitbuf_free(buf)
Definition: numeric.c:448
#define NUMERIC_NAN
Definition: numeric.c:170
NumericDigit * buf
Definition: numeric.c:278
NumericDigit * digits
Definition: numeric.c:279

◆ generate_series_numeric()

Datum generate_series_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1379 of file numeric.c.

References generate_series_step_numeric().

1380 {
1381  return generate_series_step_numeric(fcinfo);
1382 }
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1385

◆ generate_series_step_numeric()

Datum generate_series_step_numeric ( PG_FUNCTION_ARGS  )

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

1386 {
1388  FuncCallContext *funcctx;
1389  MemoryContext oldcontext;
1390 
1391  if (SRF_IS_FIRSTCALL())
1392  {
1393  Numeric start_num = PG_GETARG_NUMERIC(0);
1394  Numeric stop_num = PG_GETARG_NUMERIC(1);
1395  NumericVar steploc = const_one;
1396 
1397  /* handle NaN in start and stop values */
1398  if (NUMERIC_IS_NAN(start_num))
1399  ereport(ERROR,
1400  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1401  errmsg("start value cannot be NaN")));
1402 
1403  if (NUMERIC_IS_NAN(stop_num))
1404  ereport(ERROR,
1405  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1406  errmsg("stop value cannot be NaN")));
1407 
1408  /* see if we were given an explicit step size */
1409  if (PG_NARGS() == 3)
1410  {
1411  Numeric step_num = PG_GETARG_NUMERIC(2);
1412 
1413  if (NUMERIC_IS_NAN(step_num))
1414  ereport(ERROR,
1415  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1416  errmsg("step size cannot be NaN")));
1417 
1418  init_var_from_num(step_num, &steploc);
1419 
1420  if (cmp_var(&steploc, &const_zero) == 0)
1421  ereport(ERROR,
1422  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1423  errmsg("step size cannot equal zero")));
1424  }
1425 
1426  /* create a function context for cross-call persistence */
1427  funcctx = SRF_FIRSTCALL_INIT();
1428 
1429  /*
1430  * Switch to memory context appropriate for multiple function calls.
1431  */
1432  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1433 
1434  /* allocate memory for user context */
1435  fctx = (generate_series_numeric_fctx *)
1437 
1438  /*
1439  * Use fctx to keep state from call to call. Seed current with the
1440  * original start value. We must copy the start_num and stop_num
1441  * values rather than pointing to them, since we may have detoasted
1442  * them in the per-call context.
1443  */
1444  init_var(&fctx->current);
1445  init_var(&fctx->stop);
1446  init_var(&fctx->step);
1447 
1448  set_var_from_num(start_num, &fctx->current);
1449  set_var_from_num(stop_num, &fctx->stop);
1450  set_var_from_var(&steploc, &fctx->step);
1451 
1452  funcctx->user_fctx = fctx;
1453  MemoryContextSwitchTo(oldcontext);
1454  }
1455 
1456  /* stuff done on every call of the function */
1457  funcctx = SRF_PERCALL_SETUP();
1458 
1459  /*
1460  * Get the saved state and use current state as the result of this
1461  * iteration.
1462  */
1463  fctx = funcctx->user_fctx;
1464 
1465  if ((fctx->step.sign == NUMERIC_POS &&
1466  cmp_var(&fctx->current, &fctx->stop) <= 0) ||
1467  (fctx->step.sign == NUMERIC_NEG &&
1468  cmp_var(&fctx->current, &fctx->stop) >= 0))
1469  {
1470  Numeric result = make_result(&fctx->current);
1471 
1472  /* switch to memory context appropriate for iteration calculation */
1473  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1474 
1475  /* increment current in preparation for next iteration */
1476  add_var(&fctx->current, &fctx->step, &fctx->current);
1477  MemoryContextSwitchTo(oldcontext);
1478 
1479  /* do when there is more left to send */
1480  SRF_RETURN_NEXT(funcctx, NumericGetDatum(result));
1481  }
1482  else
1483  /* do when there is no more left */
1484  SRF_RETURN_DONE(funcctx);
1485 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
static const NumericVar const_one
Definition: numeric.c:378
#define NumericGetDatum(X)
Definition: numeric.h:51
#define NUMERIC_POS
Definition: numeric.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:286
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:288
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
#define ERROR
Definition: elog.h:43
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6059
#define ereport(elevel, rest)
Definition: elog.h:141
static const NumericVar const_zero
Definition: numeric.c:374
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6830
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
static void set_var_from_num(Numeric value, NumericVar *dest)
Definition: numeric.c:6028
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6888
#define PG_NARGS()
Definition: fmgr.h:198
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:6442
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6076
#define init_var(v)
Definition: numeric.c:454
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:284

◆ get_min_scale()

static int get_min_scale ( NumericVar var)
static

Definition at line 3186 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, NumericVar::ndigits, and NumericVar::weight.

Referenced by numeric_min_scale(), and numeric_trim_scale().

3187 {
3188  int min_scale;
3189  int last_digit_pos;
3190 
3191  /*
3192  * Ordinarily, the input value will be "stripped" so that the last
3193  * NumericDigit is nonzero. But we don't want to get into an infinite
3194  * loop if it isn't, so explicitly find the last nonzero digit.
3195  */
3196  last_digit_pos = var->ndigits - 1;
3197  while (last_digit_pos >= 0 &&
3198  var->digits[last_digit_pos] == 0)
3199  last_digit_pos--;
3200 
3201  if (last_digit_pos >= 0)
3202  {
3203  /* compute min_scale assuming that last ndigit has no zeroes */
3204  min_scale = (last_digit_pos - var->weight) * DEC_DIGITS;
3205 
3206  /*
3207  * We could get a negative result if there are no digits after the
3208  * decimal point. In this case the min_scale must be zero.
3209  */
3210  if (min_scale > 0)
3211  {
3212  /*
3213  * Reduce min_scale if trailing digit(s) in last NumericDigit are
3214  * zero.
3215  */
3216  NumericDigit last_digit = var->digits[last_digit_pos];
3217 
3218  while (last_digit % 10 == 0)
3219  {
3220  min_scale--;
3221  last_digit /= 10;
3222  }
3223  }
3224  else
3225  min_scale = 0;
3226  }
3227  else
3228  min_scale = 0; /* result if input is zero */
3229 
3230  return min_scale;
3231 }
int weight
Definition: numeric.c:275
int ndigits
Definition: numeric.c:274
int16 NumericDigit
Definition: numeric.c:102
NumericDigit * digits
Definition: numeric.c:279
#define DEC_DIGITS
Definition: numeric.c:98

◆ get_str_from_var()

static char * get_str_from_var ( const NumericVar var)
static

Definition at line 6102 of file numeric.c.

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

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

6103 {
6104  int dscale;
6105  char *str;
6106  char *cp;
6107  char *endcp;
6108  int i;
6109  int d;
6110  NumericDigit dig;
6111 
6112 #if DEC_DIGITS > 1
6113  NumericDigit d1;
6114 #endif
6115 
6116  dscale = var->dscale;
6117 
6118  /*
6119  * Allocate space for the result.
6120  *
6121  * i is set to the # of decimal digits before decimal point. dscale is the
6122  * # of decimal digits we will print after decimal point. We may generate
6123  * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
6124  * need room for sign, decimal point, null terminator.
6125  */
6126  i = (var->weight + 1) * DEC_DIGITS;
6127  if (i <= 0)
6128  i = 1;
6129 
6130  str = palloc(i + dscale + DEC_DIGITS + 2);
6131  cp = str;
6132 
6133  /*
6134  * Output a dash for negative values
6135  */
6136  if (var->sign == NUMERIC_NEG)
6137  *cp++ = '-';
6138 
6139  /*
6140  * Output all digits before the decimal point
6141  */
6142  if (var->weight < 0)
6143  {
6144  d = var->weight + 1;
6145  *cp++ = '0';
6146  }
6147  else
6148  {
6149  for (d = 0; d <= var->weight; d++)
6150  {
6151  dig = (d < var->ndigits) ? var->digits[d] : 0;
6152  /* In the first digit, suppress extra leading decimal zeroes */
6153 #if DEC_DIGITS == 4
6154  {
6155  bool putit = (d > 0);
6156 
6157  d1 = dig / 1000;
6158  dig -= d1 * 1000;
6159  putit |= (d1 > 0);
6160  if (putit)
6161  *cp++ = d1 + '0';
6162  d1 = dig / 100;
6163  dig -= d1 * 100;
6164  putit |= (d1 > 0);
6165  if (putit)
6166  *cp++ = d1 + '0';
6167  d1 = dig / 10;
6168  dig -= d1 * 10;
6169  putit |= (d1 > 0);
6170  if (putit)
6171  *cp++ = d1 + '0';
6172  *cp++ = dig + '0';
6173  }
6174 #elif DEC_DIGITS == 2
6175  d1 = dig / 10;
6176  dig -= d1 * 10;
6177  if (d1 > 0 || d > 0)
6178  *cp++ = d1 + '0';
6179  *cp++ = dig + '0';
6180 #elif DEC_DIGITS == 1
6181  *cp++ = dig + '0';
6182 #else
6183 #error unsupported NBASE
6184 #endif
6185  }
6186  }
6187 
6188  /*
6189  * If requested, output a decimal point and all the digits that follow it.
6190  * We initially put out a multiple of DEC_DIGITS digits, then truncate if
6191  * needed.
6192  */
6193  if (dscale > 0)
6194  {
6195  *cp++ = '.';
6196  endcp = cp + dscale;
6197  for (i = 0; i < dscale; d++, i += DEC_DIGITS)
6198  {
6199  dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
6200 #if DEC_DIGITS == 4
6201  d1 = dig / 1000;
6202  dig -= d1 * 1000;
6203  *cp++ = d1 + '0';
6204  d1 = dig / 100;
6205  dig -= d1 * 100;
6206  *cp++ = d1 + '0';
6207  d1 = dig / 10;
6208  dig -= d1 * 10;
6209  *cp++ = d1 + '0';
6210  *cp++ = dig + '0';
6211 #elif DEC_DIGITS == 2
6212  d1 = dig / 10;
6213  dig -= d1 * 10;
6214  *cp++ = d1 + '0';
6215  *cp++ = dig + '0';
6216 #elif DEC_DIGITS == 1
6217  *cp++ = dig + '0';
6218 #else
6219 #error unsupported NBASE
6220 #endif
6221  }
6222  cp = endcp;
6223  }
6224 
6225  /*
6226  * terminate the string and return it
6227  */
6228  *cp = '\0';
6229  return str;
6230 }
int weight
Definition: numeric.c:275
static void error(void)
Definition: sql-dyntest.c:147
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
int16 NumericDigit
Definition: numeric.c:102
#define NBASE
Definition: numeric.c:96
NumericDigit * digits
Definition: numeric.c:279
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define DEC_DIGITS
Definition: numeric.c:98

◆ get_str_from_var_sci()

static char * get_str_from_var_sci ( const NumericVar var,
int  rscale 
)
static

Definition at line 6255 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, generate_unaccent_rules::str, and NumericVar::weight.

Referenced by numeric_out_sci().

6256 {
6257  int32 exponent;
6258  NumericVar denominator;
6259  NumericVar significand;
6260  int denom_scale;
6261  size_t len;
6262  char *str;
6263  char *sig_out;
6264 
6265  if (rscale < 0)
6266  rscale = 0;
6267 
6268  /*
6269  * Determine the exponent of this number in normalised form.
6270  *
6271  * This is the exponent required to represent the number with only one
6272  * significant digit before the decimal place.
6273  */
6274  if (var->ndigits > 0)
6275  {
6276  exponent = (var->weight + 1) * DEC_DIGITS;
6277 
6278  /*
6279  * Compensate for leading decimal zeroes in the first numeric digit by
6280  * decrementing the exponent.
6281  */
6282  exponent -= DEC_DIGITS - (int) log10(var->digits[0]);
6283  }
6284  else
6285  {
6286  /*
6287  * If var has no digits, then it must be zero.
6288  *
6289  * Zero doesn't technically have a meaningful exponent in normalised
6290  * notation, but we just display the exponent as zero for consistency
6291  * of output.
6292  */
6293  exponent = 0;
6294  }
6295 
6296  /*
6297  * The denominator is set to 10 raised to the power of the exponent.
6298  *
6299  * We then divide var by the denominator to get the significand, rounding
6300  * to rscale decimal digits in the process.
6301  */
6302  if (exponent < 0)
6303  denom_scale = -exponent;
6304  else
6305  denom_scale = 0;
6306 
6307  init_var(&denominator);
6308  init_var(&significand);
6309 
6310  power_var_int(&const_ten, exponent, &denominator, denom_scale);
6311  div_var(var, &denominator, &significand, rscale, true);
6312  sig_out = get_str_from_var(&significand);
6313 
6314  free_var(&denominator);
6315  free_var(&significand);
6316 
6317  /*
6318  * Allocate space for the result.
6319  *
6320  * In addition to the significand, we need room for the exponent
6321  * decoration ("e"), the sign of the exponent, up to 10 digits for the
6322  * exponent itself, and of course the null terminator.
6323  */
6324  len = strlen(sig_out) + 13;
6325  str = palloc(len);
6326  snprintf(str, len, "%se%+03d", sig_out, exponent);
6327 
6328  pfree(sig_out);
6329 
6330  return str;
6331 }
int weight
Definition: numeric.c:275
int ndigits
Definition: numeric.c:274
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:7323
signed int int32
Definition: c.h:347
void pfree(void *pointer)
Definition: mcxt.c:1056
static void power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
Definition: numeric.c:8607
static const NumericVar const_ten
Definition: numeric.c:387
static void free_var(NumericVar *var)
Definition: numeric.c:5826
NumericDigit * digits
Definition: numeric.c:279
void * palloc(Size size)
Definition: mcxt.c:949
static char * get_str_from_var(const NumericVar *var)
Definition: numeric.c:6102
#define DEC_DIGITS
Definition: numeric.c:98
#define snprintf
Definition: port.h:192
#define init_var(v)
Definition: numeric.c:454

◆ hash_numeric()

Datum hash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2259 of file numeric.c.

References Assert, digits, hash_any(), i, sort-test::key, NUMERIC_DIGITS, NUMERIC_IS_NAN, NUMERIC_NDIGITS, NUMERIC_WEIGHT, PG_GETARG_NUMERIC, PG_RETURN_DATUM, and PG_RETURN_UINT32.

Referenced by JsonbHashScalarValue().

2260 {
2262  Datum digit_hash;
2263  Datum result;
2264  int weight;
2265  int start_offset;
2266  int end_offset;
2267  int i;
2268  int hash_len;
2270 
2271  /* If it's NaN, don't try to hash the rest of the fields */
2272  if (NUMERIC_IS_NAN(key))
2273  PG_RETURN_UINT32(0);
2274 
2275  weight = NUMERIC_WEIGHT(key);
2276  start_offset = 0;
2277  end_offset = 0;
2278 
2279  /*
2280  * Omit any leading or trailing zeros from the input to the hash. The
2281  * numeric implementation *should* guarantee that leading and trailing
2282  * zeros are suppressed, but we're paranoid. Note that we measure the
2283  * starting and ending offsets in units of NumericDigits, not bytes.
2284  */
2285  digits = NUMERIC_DIGITS(key);
2286  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2287  {
2288  if (digits[i] != (NumericDigit) 0)
2289  break;
2290 
2291  start_offset++;
2292 
2293  /*
2294  * The weight is effectively the # of digits before the decimal point,
2295  * so decrement it for each leading zero we skip.
2296  */
2297  weight--;
2298  }
2299 
2300  /*
2301  * If there are no non-zero digits, then the value of the number is zero,
2302  * regardless of any other fields.
2303  */
2304  if (NUMERIC_NDIGITS(key) == start_offset)
2305  PG_RETURN_UINT32(-1);
2306 
2307  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2308  {
2309  if (digits[i] != (NumericDigit) 0)
2310  break;
2311 
2312  end_offset++;
2313  }
2314 
2315  /* If we get here, there should be at least one non-zero digit */
2316  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2317 
2318  /*
2319  * Note that we don't hash on the Numeric's scale, since two numerics can
2320  * compare equal but have different scales. We also don't hash on the
2321  * sign, although we could: since a sign difference implies inequality,
2322  * this shouldn't affect correctness.
2323  */
2324  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2325  digit_hash = hash_any((unsigned char *) (NUMERIC_DIGITS(key) + start_offset),
2326  hash_len * sizeof(NumericDigit));
2327 
2328  /* Mix in the weight, via XOR */
2329  result = digit_hash ^ weight;
2330 
2331  PG_RETURN_DATUM(result);
2332 }
Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.c:148
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:345
#define NUMERIC_DIGITS(num)
Definition: numeric.c:456
int16 NumericDigit
Definition: numeric.c:102
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:458
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:217
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#define Assert(condition)
Definition: c.h:739
int i
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173
int digits
Definition: informix.c:666

◆ hash_numeric_extended()

Datum hash_numeric_extended ( PG_FUNCTION_ARGS  )

Definition at line 2339 of file numeric.c.

References Assert, DatumGetUInt64, digits, hash_any_extended(), i, sort-test::key, 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().

2340 {
2342  uint64 seed = PG_GETARG_INT64(1);
2343  Datum digit_hash;
2344  Datum result;
2345  int weight;
2346  int start_offset;
2347  int end_offset;
2348  int i;
2349  int hash_len;
2351 
2352  if (NUMERIC_IS_NAN(key))
2353  PG_RETURN_UINT64(seed);
2354 
2355  weight = NUMERIC_WEIGHT(key);
2356  start_offset = 0;
2357  end_offset = 0;
2358 
2359  digits = NUMERIC_DIGITS(key);
2360  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2361  {
2362  if (digits[i] != (NumericDigit) 0)
2363  break;
2364 
2365  start_offset++;
2366 
2367  weight--;
2368  }
2369 
2370  if (NUMERIC_NDIGITS(key) == start_offset)
2371  PG_RETURN_UINT64(seed - 1);
2372 
2373  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2374  {
2375  if (digits[i] != (NumericDigit) 0)
2376  break;
2377 
2378  end_offset++;
2379  }
2380 
2381  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2382 
2383  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2384  digit_hash = hash_any_extended((unsigned char *) (NUMERIC_DIGITS(key)
2385  + start_offset),
2386  hash_len * sizeof(NumericDigit),
2387  seed);
2388 
2389  result = UInt64GetDatum(DatumGetUInt64(digit_hash) ^ weight);
2390 
2391  PG_RETURN_DATUM(result);
2392 }
#define UInt64GetDatum(X)
Definition: postgres.h:648
Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.c:374
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:358
#define NUMERIC_DIGITS(num)
Definition: numeric.c:456
int16 NumericDigit
Definition: numeric.c:102
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:458
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define DatumGetUInt64(X)
Definition: postgres.h:634
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:217
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#define Assert(condition)
Definition: c.h:739
int i
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277
int digits
Definition: informix.c:666

◆ in_range_numeric_numeric()

Datum in_range_numeric_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2187 of file numeric.c.

References add_var(), cmp_var(), ereport, errcode(), errmsg(), ERROR, free_var(), init_var, init_var_from_num(), NUMERIC_IS_NAN, NUMERIC_NEG, NUMERIC_SIGN, PG_FREE_IF_COPY, PG_GETARG_BOOL, PG_GETARG_NUMERIC, PG_RETURN_BOOL, sub_var(), and val.

2188 {
2190  Numeric base = PG_GETARG_NUMERIC(1);
2191  Numeric offset = PG_GETARG_NUMERIC(2);
2192  bool sub = PG_GETARG_BOOL(3);
2193  bool less = PG_GETARG_BOOL(4);
2194  bool result;
2195 
2196  /*
2197  * Reject negative or NaN offset. Negative is per spec, and NaN is
2198  * because appropriate semantics for that seem non-obvious.
2199  */
2200  if (NUMERIC_IS_NAN(offset) || NUMERIC_SIGN(offset) == NUMERIC_NEG)
2201  ereport(ERROR,
2202  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2203  errmsg("invalid preceding or following size in window function")));
2204 
2205  /*
2206  * Deal with cases where val and/or base is NaN, following the rule that
2207  * NaN sorts after non-NaN (cf cmp_numerics). The offset cannot affect
2208  * the conclusion.
2209  */
2210  if (NUMERIC_IS_NAN(val))
2211  {
2212  if (NUMERIC_IS_NAN(base))
2213  result = true; /* NAN = NAN */
2214  else
2215  result = !less; /* NAN > non-NAN */
2216  }
2217  else if (NUMERIC_IS_NAN(base))
2218  {
2219  result = less; /* non-NAN < NAN */
2220  }
2221  else
2222  {
2223  /*
2224  * Otherwise go ahead and compute base +/- offset. While it's
2225  * possible for this to overflow the numeric format, it's unlikely
2226  * enough that we don't take measures to prevent it.
2227  */
2228  NumericVar valv;
2229  NumericVar basev;
2230  NumericVar offsetv;
2231  NumericVar sum;
2232 
2233  init_var_from_num(val, &valv);
2234  init_var_from_num(base, &basev);
2235  init_var_from_num(offset, &offsetv);
2236  init_var(&sum);
2237 
2238  if (sub)
2239  sub_var(&basev, &offsetv, &sum);
2240  else
2241  add_var(&basev, &offsetv, &sum);
2242 
2243  if (less)
2244  result = (cmp_var(&valv, &sum) <= 0);
2245  else
2246  result = (cmp_var(&valv, &sum) >= 0);
2247 
2248  free_var(&sum);
2249  }
2250 
2251  PG_FREE_IF_COPY(val, 0);
2252  PG_FREE_IF_COPY(base, 1);
2253  PG_FREE_IF_COPY(offset, 2);
2254 
2255  PG_RETURN_BOOL(result);
2256 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define NUMERIC_NEG
Definition: numeric.c:168
#define ERROR
Definition: elog.h:43
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6059
#define NUMERIC_SIGN(n)
Definition: numeric.c:209
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:6830
static void free_var(NumericVar *var)
Definition: numeric.c:5826
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:6888
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:173
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7005
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:454

◆ init_var_from_num()

static void init_var_from_num ( Numeric  num,
NumericVar dest 
)
static

Definition at line 6059 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(), in_range_numeric_numeric(), int8_avg_deserialize(), numeric_abbrev_convert(), numeric_add_opt_error(), numeric_avg_deserialize(), numeric_ceil(), numeric_deserialize(), numeric_div_opt_error(), numeric_div_trunc(), numeric_exp(), numeric_floor(), numeric_inc(), numeric_int2(), numeric_int4_opt_error(), numeric_int8(), numeric_ln(), numeric_log(), numeric_min_scale(), numeric_mod_opt_error(), numeric_mul_opt_error(), numeric_normalize(), numeric_out(), numeric_out_sci(), numeric_poly_deserialize(), numeric_power(), numeric_send(), numeric_sqrt(), numeric_sub_opt_error(), and numeric_trim_scale().

6060 {
6061  dest->ndigits = NUMERIC_NDIGITS(num);
6062  dest->weight = NUMERIC_WEIGHT(num);
6063  dest->sign = NUMERIC_SIGN(num);
6064  dest->dscale = NUMERIC_DSCALE(num);
6065  dest->digits = NUMERIC_DIGITS(num);
6066  dest->buf = NULL; /* digits array is not palloc'd */
6067 }
#define NUMERIC_DSCALE(n)
Definition: numeric.c:213
int weight
Definition: numeric.c:275
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
int sign
Definition: numeric.c:276
#define NUMERIC_DIGITS(num)
Definition: numeric.c:456
#define NUMERIC_SIGN(n)
Definition: numeric.c:209
NumericDigit * buf
Definition: numeric.c:278
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:458
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:217
NumericDigit * digits
Definition: numeric.c:279

◆ int2_accum()

Datum int2_accum ( PG_FUNCTION_ARGS  )

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

4358 {
4360 
4361  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4362 
4363  /* Create the state data on the first call */
4364  if (state == NULL)
4365  state = makePolyNumAggState(fcinfo, true);
4366 
4367  if (!PG_ARGISNULL(1))
4368  {
4369 #ifdef HAVE_INT128
4370  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
4371 #else
4372  Numeric newval;
4373 
4375  PG_GETARG_DATUM(1)));
4376  do_numeric_accum(state, newval);
4377 #endif
4378  }
4379 
4380  PG_RETURN_POINTER(state);
4381 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Datum int2_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3420
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3667
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define makePolyNumAggState
Definition: numeric.c:4352

◆ int2_accum_inv()

Datum int2_accum_inv ( PG_FUNCTION_ARGS  )

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

4835 {
4837 
4838  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4839 
4840  /* Should not get here with no state */
4841  if (state == NULL)
4842  elog(ERROR, "int2_accum_inv called with NULL state");
4843 
4844  if (!PG_ARGISNULL(1))
4845  {
4846 #ifdef HAVE_INT128
4847  do_int128_discard(state, (int128) PG_GETARG_INT16(1));
4848 #else
4849  Numeric newval;
4850 
4852  PG_GETARG_DATUM(1)));
4853 
4854  /* Should never fail, all inputs have dscale 0 */
4855  if (!do_numeric_discard(state, newval))
4856  elog(ERROR, "do_numeric_discard failed unexpectedly");
4857 #endif
4858  }
4859 
4860  PG_RETURN_POINTER(state);
4861 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Datum int2_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3420
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3732
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define elog(elevel,...)
Definition: elog.h:228

◆ int2_avg_accum()

Datum int2_avg_accum ( PG_FUNCTION_ARGS  )

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

5526 {
5527  ArrayType *transarray;
5529  Int8TransTypeData *transdata;
5530 
5531  /*
5532  * If we're invoked as an aggregate, we can cheat and modify our first
5533  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5534  * a copy of it before scribbling on it.
5535  */
5536  if (AggCheckCallContext(fcinfo, NULL))
5537  transarray = PG_GETARG_ARRAYTYPE_P(0);
5538  else
5539  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5540 
5541  if (ARR_HASNULL(transarray) ||
5542  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5543  elog(ERROR, "expected 2-element int8 array");
5544 
5545  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5546  transdata->count++;
5547  transdata->sum += newval;
5548 
5549  PG_RETURN_ARRAYTYPE_P(transarray);
5550 }
signed short int16
Definition: c.h:346
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define elog(elevel,...)
Definition: elog.h:228

◆ int2_avg_accum_inv()

Datum int2_avg_accum_inv ( PG_FUNCTION_ARGS  )

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

5613 {
5614  ArrayType *transarray;
5616  Int8TransTypeData *transdata;
5617 
5618  /*
5619  * If we're invoked as an aggregate, we can cheat and modify our first
5620  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5621  * a copy of it before scribbling on it.
5622  */
5623  if (AggCheckCallContext(fcinfo, NULL))
5624  transarray = PG_GETARG_ARRAYTYPE_P(0);
5625  else
5626  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5627 
5628  if (ARR_HASNULL(transarray) ||
5629  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5630  elog(ERROR, "expected 2-element int8 array");
5631 
5632  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5633  transdata->count--;
5634  transdata->sum -= newval;
5635 
5636  PG_RETURN_ARRAYTYPE_P(transarray);
5637 }
signed short int16
Definition: c.h:346
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define elog(elevel,...)
Definition: elog.h:228

◆ int2_numeric()

Datum int2_numeric ( PG_FUNCTION_ARGS  )

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

3421 {
3422  int16 val = PG_GETARG_INT16(0);
3423  Numeric res;
3424  NumericVar result;
3425 
3426  init_var(&result);
3427 
3428  int64_to_numericvar((int64) val, &result);
3429 
3430  res = make_result(&result);
3431 
3432  free_var(&result);
3433 
3434  PG_RETURN_NUMERIC(res);
3435 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
signed short int16
Definition: c.h:346
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:6442
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:6606
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:454

◆ int2_sum()

Datum int2_sum ( PG_FUNCTION_ARGS  )

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

5374 {
5375  int64 newval;
5376 
5377  if (PG_ARGISNULL(0))
5378  {
5379  /* No non-null input seen so far... */
5380  if (PG_ARGISNULL(1))
5381  PG_RETURN_NULL(); /* still no non-null */
5382  /* This is the first non-null input. */
5383  newval = (int64) PG_GETARG_INT16(1);
5384  PG_RETURN_INT64(newval);
5385  }
5386 
5387  /*
5388  * If we're invoked as an aggregate, we can cheat and modify our first
5389  * parameter in-place to avoid palloc overhead. If not, we need to return
5390  * the new value of the transition variable. (If int8 is pass-by-value,
5391  * then of course this is useless as well as incorrect, so just ifdef it
5392  * out.)
5393  */
5394 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
5395  if (AggCheckCallContext(fcinfo, NULL))
5396  {
5397  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
5398 
5399  /* Leave the running sum unchanged in the new input is null */
5400  if (!PG_ARGISNULL(1))
5401  *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
5402 
5403  PG_RETURN_POINTER(oldsum);
5404  }
5405  else
5406 #endif
5407  {
5408  int64 oldsum = PG_GETARG_INT64(0);
5409 
5410  /* Leave sum unchanged if new input is null. */
5411  if (PG_ARGISNULL(1))
5412  PG_RETURN_INT64(oldsum);
5413 
5414  /* OK to do the addition. */
5415  newval = oldsum + (int64) PG_GETARG_INT16(1);
5416 
5417  PG_RETURN_INT64(newval);
5418  }
5419 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ int2int4_sum()

Datum int2int4_sum ( PG_FUNCTION_ARGS  )

Definition at line 5697 of file numeric.c.

References ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, DEC_DIGITS, NumericVar::digits, digits, NumericVar::dscale, dump_numeric, dump_var, elog, ERROR, i, Int64GetDatumFast, NumericVar::ndigits, NUMERIC_DIGITS, NUMERIC_DSCALE, NUMERIC_NAN, NUMERIC_NDIGITS, NUMERIC_NEG, NUMERIC_POS, NUMERIC_SIGN, NUMERIC_WEIGHT, PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_NULL, printf, NumericVar::sign, generate_unaccent_rules::str, Int8TransTypeData::sum, and NumericVar::weight.

5698 {
5699  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
5700  Int8TransTypeData *transdata;
5701 
5702  if (ARR_HASNULL(transarray) ||
5703  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5704  elog(ERROR, "expected 2-element int8 array");
5705  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5706 
5707  /* SQL defines SUM of no values to be NULL */
5708  if (transdata->count == 0)
5709  PG_RETURN_NULL();
5710 
5711  PG_RETURN_DATUM(Int64GetDatumFast(transdata->sum));
5712 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define Int64GetDatumFast(X)
Definition: postgres.h:760
#define elog(elevel,...)
Definition: elog.h:228
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ int4_accum()

Datum int4_accum ( PG_FUNCTION_ARGS  )

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

4385 {
4387 
4388  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4389 
4390  /* Create the state data on the first call */
4391  if (state == NULL)
4392  state = makePolyNumAggState(fcinfo, true);
4393 
4394  if (!PG_ARGISNULL(1))
4395  {
4396 #ifdef HAVE_INT128
4397  do_int128_accum(state, (int128) PG_GETARG_INT32(1));
4398 #else
4399  Numeric newval;
4400 
4402  PG_GETARG_DATUM(1)));
4403  do_numeric_accum(state, newval);
4404 #endif
4405  }
4406 
4407  PG_RETURN_POINTER(state);
4408 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3667
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3284
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define makePolyNumAggState
Definition: numeric.c:4352

◆ int4_accum_inv()

Datum int4_accum_inv ( PG_FUNCTION_ARGS  )

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

4865 {
4867 
4868  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
4869 
4870  /* Should not get here with no state */
4871  if (state == NULL)
4872  elog(ERROR, "int4_accum_inv called with NULL state");
4873 
4874  if (!PG_ARGISNULL(1))
4875  {
4876 #ifdef HAVE_INT128
4877  do_int128_discard(state, (int128) PG_GETARG_INT32(1));
4878 #else
4879  Numeric newval;
4880 
4882  PG_GETARG_DATUM(1)));
4883 
4884  /* Should never fail, all inputs have dscale 0 */
4885  if (!do_numeric_discard(state, newval))
4886  elog(ERROR, "do_numeric_discard failed unexpectedly");
4887 #endif
4888  }
4889 
4890  PG_RETURN_POINTER(state);
4891 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:3732
#define ERROR
Definition: elog.h:43
Datum int4_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3284
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval
#define elog(elevel,...)
Definition: elog.h:228

◆ int4_avg_accum()

Datum int4_avg_accum ( PG_FUNCTION_ARGS  )

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

5554 {
5555  ArrayType *transarray;
5557  Int8TransTypeData *transdata;
5558 
5559  /*
5560  * If we're invoked as an aggregate, we can cheat and modify our first
5561  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5562  * a copy of it before scribbling on it.
5563  */
5564  if (AggCheckCallContext(fcinfo, NULL))
5565  transarray = PG_GETARG_ARRAYTYPE_P(0);
5566  else
5567  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5568 
5569  if (ARR_HASNULL(transarray) ||
5570  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5571  elog(ERROR, "expected 2-element int8 array");
5572 
5573  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5574  transdata->count++;
5575  transdata->sum += newval;
5576 
5577  PG_RETURN_ARRAYTYPE_P(transarray);
5578 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
signed int int32
Definition: c.h:347
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define elog(elevel,...)
Definition: elog.h:228

◆ int4_avg_accum_inv()

Datum int4_avg_accum_inv ( PG_FUNCTION_ARGS  )

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

5641 {
5642  ArrayType *transarray;
5644  Int8TransTypeData *transdata;
5645 
5646  /*
5647  * If we're invoked as an aggregate, we can cheat and modify our first
5648  * parameter in-place to reduce palloc overhead. Otherwise we need to make
5649  * a copy of it before scribbling on it.
5650  */
5651  if (AggCheckCallContext(fcinfo, NULL))
5652  transarray = PG_GETARG_ARRAYTYPE_P(0);
5653  else
5654  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
5655 
5656  if (ARR_HASNULL(transarray) ||
5657  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5658  elog(ERROR, "expected 2-element int8 array");
5659 
5660  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
5661  transdata->count--;
5662  transdata->sum -= newval;
5663 
5664  PG_RETURN_ARRAYTYPE_P(transarray);
5665 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
signed int int32
Definition: c.h:347
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define elog(elevel,...)
Definition: elog.h:228

◆ int4_avg_combine()

Datum int4_avg_combine ( PG_FUNCTION_ARGS  )

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

5582 {
5583  ArrayType *transarray1;
5584  ArrayType *transarray2;
5585  Int8TransTypeData *state1;
5586  Int8TransTypeData *state2;
5587 
5588  if (!AggCheckCallContext(fcinfo, NULL))
5589  elog(ERROR, "aggregate function called in non-aggregate context");
5590 
5591  transarray1 = PG_GETARG_ARRAYTYPE_P(0);
5592  transarray2 = PG_GETARG_ARRAYTYPE_P(1);
5593 
5594  if (ARR_HASNULL(transarray1) ||
5595  ARR_SIZE(transarray1) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5596  elog(ERROR, "expected 2-element int8 array");
5597 
5598  if (ARR_HASNULL(transarray2) ||
5599  ARR_SIZE(transarray2) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
5600  elog(ERROR, "expected 2-element int8 array");
5601 
5602  state1 = (Int8TransTypeData *) ARR_DATA_PTR(transarray1);
5603  state2 = (Int8TransTypeData *) ARR_DATA_PTR(transarray2);
5604 
5605  state1->count += state2->count;
5606  state1->sum += state2->sum;
5607 
5608  PG_RETURN_ARRAYTYPE_P(transarray1);
5609 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define elog(elevel,...)
Definition: elog.h:228

◆ int4_numeric()

Datum int4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 3284 of file numeric.c.

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

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

3285 {
3286  int32 val = PG_GETARG_INT32(0);
3287  Numeric res;
3288  NumericVar result;
3289 
3290  init_var(&result);
3291 
3292  int64_to_numericvar((int64) val, &result);
3293 
3294  res = make_result(&result);
3295 
3296  free_var(&result);
3297 
3298  PG_RETURN_NUMERIC(res);
3299 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
signed int int32
Definition: c.h:347
static void free_var(NumericVar *var)
Definition: numeric.c:5826
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:6442
static void int64_to_numericvar(int64 val, NumericVar *var)
Definition: numeric.c:6606
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:454

◆ int4_sum()

Datum int4_sum ( PG_FUNCTION_ARGS  )

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

5423 {
5424  int64 newval;
5425 
5426  if (PG_ARGISNULL(0))
5427  {
5428  /* No non-null input seen so far... */
5429  if (PG_ARGISNULL(1))
5430  PG_RETURN_NULL(); /* still no non-null */
5431  /* This is the first non-null input. */
5432  newval = (int64) PG_GETARG_INT32(1);
5433  PG_RETURN_INT64(newval);
5434  }
5435 
5436  /*
5437  * If we're invoked as an aggregate, we can cheat and modify our first
5438  * parameter in-place to avoid palloc overhead. If not, we need to return
5439  * the new value of the transition variable. (If int8 is pass-by-value,
5440  * then of course this is useless as well as incorrect, so just ifdef it
5441  * out.)
5442  */
5443 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
5444  if (AggCheckCallContext(fcinfo, NULL))
5445  {
5446  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
5447 
5448  /* Leave the running sum unchanged in the new input is null */
5449  if (!PG_ARGISNULL(1))
5450  *oldsum = *oldsum + (int64) PG_GETARG_INT32(1);
5451 
5452  PG_RETURN_POINTER(oldsum);
5453  }
5454  else
5455 #endif
5456  {
5457  int64 oldsum = PG_GETARG_INT64(0);
5458 
5459  /* Leave sum unchanged if new input is null. */
5460  if (PG_ARGISNULL(1))
5461  PG_RETURN_INT64(oldsum);
5462 
5463  /* OK to do the addition. */
5464  newval = oldsum + (int64) PG_GETARG_INT32(1);
5465 
5466  PG_RETURN_INT64(newval);
5467  }
5468 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3571
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ int64_to_numericvar()

static void int64_to_numericvar ( int64  val,
NumericVar var 
)
static

Definition at line 6606 of file numeric.c.

References alloc_var(), Assert, DEC_DIGITS, NumericVar::digits, digits, NumericVar::dscale, free_var(), i, init_var, NBASE, NumericVar::ndigits, NUMERIC_NEG, NUMERIC_POS, round_var(), set_var_from_var(), NumericVar::sign, strip_var(), val, and NumericVar::weight.

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

6607 {
6608  uint64 uval,
6609  newuval;
6610  NumericDigit *ptr;
6611  int ndigits;
6612 
6613  /* int64 can require at most 19 decimal digits; add one for safety */
6614  alloc_var(var, 20 / DEC_DIGITS);
6615  if (val < 0)
6616  {
6617  var->sign = NUMERIC_NEG;
6618  uval = -val;
6619  }
6620  else
6621  {
6622  var->sign = NUMERIC_POS;
6623  uval = val;
6624  }
6625  var->dscale = 0;
6626  if (val == 0)
6627  {
6628  var->ndigits = 0;
6629  var->weight = 0;
6630  return;
6631  }
6632  ptr = var->digits + var->ndigits;
6633  ndigits = 0;
6634  do
6635  {
6636  ptr--;
6637  ndigits++;
6638  newuval = uval / NBASE;
6639  *ptr = uval - newuval * NBASE;
6640  uval = newuval;
6641  } while (uval);
6642  var->digits = ptr;
6643  var->ndigits = ndigits;
6644  var->weight = ndigits - 1;
6645 }
int weight
Definition: numeric.c:275
#define NUMERIC_POS
Definition: numeric.c:167
int ndigits
Definition: numeric.c:274
int dscale
Definition: numeric.c:277
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:276
int16 NumericDigit
Definition: numeric.c:102
#define NBASE
Definition: numeric.c:96
NumericDigit * digits
Definition: numeric.c:279
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:5810
#define DEC_DIGITS
Definition: numeric.c:98
long val
Definition: informix.c:664

◆ int8_accum()

Datum int8_accum ( PG_FUNCTION_ARGS  )

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

4412 {
4414 
4415  state = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
4416 
4417  /* Create the state data on the first call */
4418  if (state == NULL)
4419  state = makeNumericAggState(fcinfo, true);
4420 
4421  if (!PG_ARGISNULL(1))
4422  {
4423  Numeric newval;
4424 
4426  PG_GETARG_DATUM(1)));
4427  do_numeric_accum(state, newval);
4428  }
4429 
4430  PG_RETURN_POINTER(state);
4431 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3376
static NumericAggState * makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
Definition: numeric.c:3627
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:3667
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
#define newval

◆ int8_accum_inv()