PostgreSQL Source Code: src/include/utils/numeric.h File Reference (original) (raw)
#include "[common/pg_prng.h](pg%5F%5Fprng%5F8h%5Fsource.html)"
#include "[fmgr.h](fmgr%5F8h%5Fsource.html)"
Go to the source code of this file.
Macros | |
---|---|
#define | NUMERIC_MAX_PRECISION 1000 |
#define | NUMERIC_MIN_SCALE (-1000) |
#define | NUMERIC_MAX_SCALE 1000 |
#define | NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION |
#define | NUMERIC_MIN_DISPLAY_SCALE 0 |
#define | NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2) |
#define | NUMERIC_MIN_SIG_DIGITS 16 |
#define | PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n)) |
#define | PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n)) |
#define | PG_RETURN_NUMERIC(x) return NumericGetDatum(x) |
◆ NUMERIC_MAX_DISPLAY_SCALE
◆ NUMERIC_MAX_PRECISION
#define NUMERIC_MAX_PRECISION 1000
◆ NUMERIC_MAX_RESULT_SCALE
◆ NUMERIC_MAX_SCALE
#define NUMERIC_MAX_SCALE 1000
◆ NUMERIC_MIN_DISPLAY_SCALE
#define NUMERIC_MIN_DISPLAY_SCALE 0
◆ NUMERIC_MIN_SCALE
#define NUMERIC_MIN_SCALE (-1000)
◆ NUMERIC_MIN_SIG_DIGITS
#define NUMERIC_MIN_SIG_DIGITS 16
◆ PG_GETARG_NUMERIC
◆ PG_GETARG_NUMERIC_COPY
◆ PG_RETURN_NUMERIC
◆ DatumGetNumeric()
Definition at line 61 of file numeric.h.
62{
64}
#define PG_DETOAST_DATUM(datum)
References PG_DETOAST_DATUM.
Referenced by datum_to_jsonb_internal(), executeItemOptUnwrapTarget(), executeNumericItemMethod(), executeUnaryArithmExpr(), extract_date(), gbt_numeric_consistent(), gbt_numeric_penalty(), generate_series_numeric_support(), getArrayIndex(), hstore_to_jsonb_loose(), jsonb_agg_transfn_worker(), jsonb_in_scalar(), jsonb_object_agg_transfn_worker(), JsonbValueInitNumericDatum(), numeric_absolute(), numeric_fast_cmp(), numeric_half_rounded(), numeric_to_char(), numeric_to_number(), numeric_truncated_divide(), pg_size_bytes(), PLyNumber_ToJsonbValue(), SV_to_JsonbValue(), timestamp_part_common(), and timestamptz_part_common().
◆ DatumGetNumericCopy()
◆ int64_div_fast_to_numeric()
Numeric int64_div_fast_to_numeric | ( | int64 | val1, |
---|---|---|---|
int | log10val2 | ||
) |
Definition at line 4422 of file numeric.c.
4423{
4426 int rscale;
4427 int w;
4428 int m;
4429
4431
4432
4433 rscale = log10val2 < 0 ? 0 : log10val2;
4434
4435
4437
4439 if (m < 0)
4440 {
4442 w--;
4443 }
4444
4445
4446
4447
4448
4449
4450 if (m > 0)
4451 {
4452#if DEC_DIGITS == 4
4453 static const int pow10[] = {1, 10, 100, 1000};
4454#elif DEC_DIGITS == 2
4455 static const int pow10[] = {1, 10};
4456#elif DEC_DIGITS == 1
4457 static const int pow10[] = {1};
4458#else
4459#error unsupported NBASE
4460#endif
4463
4465
4467 {
4468#ifdef HAVE_INT128
4469
4470 int128 tmp;
4471
4472 tmp = (int128) val1 * (int128) factor;
4473
4474 int128_to_numericvar(tmp, &result);
4475#else
4476
4478
4480
4483 mul_var(&result, &tmp, &result, 0);
4484
4486#endif
4487 }
4488 else
4490
4491 w++;
4492 }
4493 else
4495
4497 result.dscale = rscale;
4498
4500
4502
4503 return res;
4504}
static void free_var(NumericVar *var)
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
static void int64_to_numericvar(int64 val, NumericVar *var)
static Numeric make_result(const NumericVar *var)
#define StaticAssertDecl(condition, errmessage)
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
References DEC_DIGITS, NumericVar::dscale, free_var(), init_var, int64_to_numericvar(), lengthof, make_result(), mul_var(), pg_mul_s64_overflow(), StaticAssertDecl, unlikely, and NumericVar::weight.
Referenced by interval_part_common(), time_part_common(), timestamp_part_common(), timestamptz_part_common(), and timetz_part_common().
◆ int64_to_numeric()
Definition at line 4401 of file numeric.c.
4402{
4405
4407
4409
4411
4413
4414 return res;
4415}
References free_var(), init_var, int64_to_numericvar(), make_result(), and val.
Referenced by cash_numeric(), executeItemOptUnwrapTarget(), executeKeyValueMethod(), extract_date(), gbt_numeric_penalty(), int2_accum(), int2_accum_inv(), int2_numeric(), int4_accum(), int4_accum_inv(), int4_numeric(), int8_accum(), int8_accum_inv(), int8_avg(), int8_avg_accum(), int8_avg_accum_inv(), int8_numeric(), int8_sum(), int8_to_char(), interval_part_common(), numeric_avg(), numeric_cash(), numeric_half_rounded(), numeric_poly_avg(), numeric_to_char(), numeric_to_number(), numeric_truncated_divide(), pg_size_bytes(), pg_size_pretty_numeric(), SV_to_JsonbValue(), time_part_common(), timestamp_part_common(), timestamptz_part_common(), and timetz_part_common().
◆ numeric_add_opt_error()
Definition at line 2985 of file numeric.c.
2986{
2991
2992
2993
2994
2996 {
3000 {
3003 else
3005 }
3007 {
3010 else
3012 }
3013
3018 }
3019
3020
3021
3022
3025
3027 add_var(&arg1, &arg2, &result);
3028
3030
3032
3033 return res;
3034}
static const NumericVar const_pinf
static const NumericVar const_ninf
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
#define NUMERIC_IS_SPECIAL(n)
static void init_var_from_num(Numeric num, NumericVar *dest)
#define NUMERIC_IS_PINF(n)
static Numeric make_result_opt_error(const NumericVar *var, bool *have_error)
#define NUMERIC_IS_NINF(n)
static const NumericVar const_nan
#define NUMERIC_IS_NAN(n)
Assert(PointerIsAligned(start, uint64))
References add_var(), Assert(), const_nan, const_ninf, const_pinf, free_var(), init_var, init_var_from_num(), make_result(), make_result_opt_error(), NUMERIC_IS_NAN, NUMERIC_IS_NINF, NUMERIC_IS_PINF, and NUMERIC_IS_SPECIAL.
Referenced by executeItemOptUnwrapTarget(), interval_part_common(), numeric_add(), timestamp_part_common(), and timestamptz_part_common().
◆ numeric_div_opt_error()
Definition at line 3262 of file numeric.c.
3263{
3268 int rscale;
3269
3270 if (have_error)
3271 *have_error = false;
3272
3273
3274
3275
3277 {
3281 {
3285 {
3286 case 0:
3287 if (have_error)
3288 {
3289 *have_error = true;
3290 return NULL;
3291 }
3293 (errcode(ERRCODE_DIVISION_BY_ZERO),
3294 errmsg("division by zero")));
3295 break;
3296 case 1:
3298 case -1:
3300 }
3302 }
3304 {
3308 {
3309 case 0:
3310 if (have_error)
3311 {
3312 *have_error = true;
3313 return NULL;
3314 }
3316 (errcode(ERRCODE_DIVISION_BY_ZERO),
3317 errmsg("division by zero")));
3318 break;
3319 case 1:
3321 case -1:
3323 }
3325 }
3326
3327
3328
3329
3330
3331
3332
3334 }
3335
3336
3337
3338
3341
3343
3344
3345
3346
3348
3349
3350
3351
3352 if (have_error && (arg2.ndigits == 0 || arg2.digits[0] == 0))
3353 {
3354 *have_error = true;
3355 return NULL;
3356 }
3357
3358
3359
3360
3361 div_var(&arg1, &arg2, &result, rscale, true, true);
3362
3364
3366
3367 return res;
3368}
static int numeric_sign_internal(Numeric num)
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
static const NumericVar const_zero
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round, bool exact)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
References Assert(), const_nan, const_ninf, const_pinf, const_zero, NumericVar::digits, div_var(), ereport, errcode(), errmsg(), ERROR, free_var(), init_var, init_var_from_num(), make_result(), make_result_opt_error(), NumericVar::ndigits, NUMERIC_IS_NAN, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, numeric_sign_internal(), and select_div_scale().
Referenced by executeItemOptUnwrapTarget(), numeric_div(), timestamp_part_common(), and timestamptz_part_common().
◆ numeric_int4_opt_error()
int32 numeric_int4_opt_error | ( | Numeric | num, |
---|---|---|---|
bool * | have_error | ||
) |
Definition at line 4515 of file numeric.c.
4516{
4519
4520 if (have_error)
4521 *have_error = false;
4522
4524 {
4525 if (have_error)
4526 {
4527 *have_error = true;
4528 return 0;
4529 }
4530 else
4531 {
4534 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4535 errmsg("cannot convert NaN to %s", "integer")));
4536 else
4538 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4539 errmsg("cannot convert infinity to %s", "integer")));
4540 }
4541 }
4542
4543
4545
4547 {
4548 if (have_error)
4549 {
4550 *have_error = true;
4551 return 0;
4552 }
4553 else
4554 {
4556 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4557 errmsg("integer out of range")));
4558 }
4559 }
4560
4561 return result;
4562}
static bool numericvar_to_int32(const NumericVar *var, int32 *result)
References ereport, errcode(), errmsg(), ERROR, init_var_from_num(), NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, numericvar_to_int32(), and x.
Referenced by executeDateTimeMethod(), executeItemOptUnwrapTarget(), getArrayIndex(), numeric_int4(), and numeric_to_char().
◆ numeric_int8_opt_error()
int64 numeric_int8_opt_error | ( | Numeric | num, |
---|---|---|---|
bool * | have_error | ||
) |
Definition at line 4603 of file numeric.c.
4604{
4607
4608 if (have_error)
4609 *have_error = false;
4610
4612 {
4613 if (have_error)
4614 {
4615 *have_error = true;
4616 return 0;
4617 }
4618 else
4619 {
4622 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4623 errmsg("cannot convert NaN to %s", "bigint")));
4624 else
4626 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4627 errmsg("cannot convert infinity to %s", "bigint")));
4628 }
4629 }
4630
4631
4633
4635 {
4636 if (have_error)
4637 {
4638 *have_error = true;
4639 return 0;
4640 }
4641 else
4642 {
4644 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4645 errmsg("bigint out of range")));
4646 }
4647 }
4648
4649 return result;
4650}
static bool numericvar_to_int64(const NumericVar *var, int64 *result)
References ereport, errcode(), errmsg(), ERROR, init_var_from_num(), NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, numericvar_to_int64(), and x.
Referenced by executeItemOptUnwrapTarget(), and numeric_int8().
◆ numeric_is_inf()
◆ numeric_is_nan()
◆ numeric_maximum_size()
◆ numeric_mod_opt_error()
Definition at line 3486 of file numeric.c.
3487{
3492
3493 if (have_error)
3494 *have_error = false;
3495
3496
3497
3498
3499
3500
3502 {
3506 {
3508 {
3509 if (have_error)
3510 {
3511 *have_error = true;
3512 return NULL;
3513 }
3515 (errcode(ERRCODE_DIVISION_BY_ZERO),
3516 errmsg("division by zero")));
3517 }
3518
3520 }
3521
3523 }
3524
3527
3529
3530
3531
3532
3533 if (have_error && (arg2.ndigits == 0 || arg2.digits[0] == 0))
3534 {
3535 *have_error = true;
3536 return NULL;
3537 }
3538
3539 mod_var(&arg1, &arg2, &result);
3540
3542
3544
3545 return res;
3546}
static void mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
static Numeric duplicate_numeric(Numeric num)
References const_nan, NumericVar::digits, duplicate_numeric(), ereport, errcode(), errmsg(), ERROR, free_var(), init_var, init_var_from_num(), make_result(), make_result_opt_error(), mod_var(), NumericVar::ndigits, NUMERIC_IS_INF, NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, and numeric_sign_internal().
Referenced by executeItemOptUnwrapTarget(), and numeric_mod().
◆ numeric_mul_opt_error()
Definition at line 3141 of file numeric.c.
3142{
3147
3148
3149
3150
3152 {
3156 {
3158 {
3159 case 0:
3161 case 1:
3163 case -1:
3165 }
3167 }
3169 {
3171 {
3172 case 0:
3174 case 1:
3176 case -1:
3178 }
3180 }
3181
3183 {
3185 {
3186 case 0:
3188 case 1:
3190 case -1:
3192 }
3194 }
3197 {
3198 case 0:
3200 case 1:
3202 case -1:
3204 }
3206 }
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3221
3224
3227
3229
3231
3232 return res;
3233}
#define NUMERIC_DSCALE_MAX
static void round_var(NumericVar *var, int rscale)
References Assert(), const_nan, const_ninf, const_pinf, NumericVar::dscale, free_var(), init_var, init_var_from_num(), make_result(), make_result_opt_error(), mul_var(), NUMERIC_DSCALE_MAX, NUMERIC_IS_NAN, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, numeric_sign_internal(), and round_var().
Referenced by executeItemOptUnwrapTarget(), and numeric_mul().
◆ numeric_normalize()
char * numeric_normalize | ( | Numeric | num | ) |
---|
◆ numeric_out_sci()
char * numeric_out_sci | ( | Numeric | num, |
---|---|---|---|
int | scale | ||
) |
Definition at line 992 of file numeric.c.
993{
995 char *str;
996
997
998
999
1001 {
1003 return pstrdup("Infinity");
1005 return pstrdup("-Infinity");
1006 else
1008 }
1009
1011
1013
1014 return str;
1015}
static char * get_str_from_var_sci(const NumericVar *var, int rscale)
References get_str_from_var_sci(), init_var_from_num(), NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, pstrdup(), scale, str, and x.
Referenced by int8_to_char(), and numeric_to_char().
◆ numeric_sub_opt_error()
Definition at line 3063 of file numeric.c.
3064{
3069
3070
3071
3072
3074 {
3078 {
3081 else
3083 }
3085 {
3088 else
3090 }
3091
3096 }
3097
3098
3099
3100
3103
3105 sub_var(&arg1, &arg2, &result);
3106
3108
3110
3111 return res;
3112}
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
References Assert(), const_nan, const_ninf, const_pinf, free_var(), init_var, init_var_from_num(), make_result(), make_result_opt_error(), NUMERIC_IS_NAN, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, and sub_var().
Referenced by executeItemOptUnwrapTarget(), numeric_sub(), timestamp_part_common(), and timestamptz_part_common().
◆ NumericGetDatum()
Definition at line 73 of file numeric.h.
74{
76}
static Datum PointerGetDatum(const void *X)
References PointerGetDatum().
Referenced by cash_numeric(), compareNumeric(), ExecGetJsonValueItemString(), executeItemOptUnwrapTarget(), executeNumericItemMethod(), executeUnaryArithmExpr(), gbt_numeric_penalty(), generate_series_step_numeric(), getArrayIndex(), gin_numeric_cmp(), int8_avg(), int8_sum(), iterate_jsonb_values(), jsonb_agg_transfn_worker(), jsonb_float4(), jsonb_float8(), jsonb_int2(), jsonb_int4(), jsonb_int8(), jsonb_numeric(), jsonb_object_agg_transfn_worker(), JsonbHashScalarValue(), JsonbHashScalarValueExtended(), JsonbValue_to_SV(), numeric_absolute(), numeric_avg(), numeric_cash(), numeric_float4(), numeric_float8(), numeric_half_rounded(), numeric_is_less(), numeric_poly_avg(), numeric_to_char(), numeric_to_cstring(), numeric_to_number(), numeric_truncated_divide(), pg_lsn_mii(), pg_lsn_pli(), pg_size_bytes(), PLyObject_FromJsonbValue(), printJsonPathItem(), timestamp_part_common(), and timestamptz_part_common().
◆ random_numeric()
Definition at line 4346 of file numeric.c.
4347{
4352
4353
4355 {
4358 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4359 errmsg("lower bound cannot be NaN"));
4360 else
4362 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4363 errmsg("lower bound cannot be infinity"));
4364 }
4366 {
4369 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4370 errmsg("upper bound cannot be NaN"));
4371 else
4373 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4374 errmsg("upper bound cannot be infinity"));
4375 }
4376
4377
4380
4382
4384
4386
4388
4389 return res;
4390}
static void random_var(pg_prng_state *state, const NumericVar *rmin, const NumericVar *rmax, NumericVar *result)
References ereport, errcode(), errmsg(), ERROR, free_var(), init_var, init_var_from_num(), make_result(), NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, and random_var().
Referenced by numeric_random().