79 #define MUL_GUARD_DIGITS 4
80 #define DIV_GUARD_DIGITS 8
89 #define MUL_GUARD_DIGITS 3
90 #define DIV_GUARD_DIGITS 6
97 #define HALF_NBASE 5000
99 #define MUL_GUARD_DIGITS 2
100 #define DIV_GUARD_DIGITS 4
165 #define NUMERIC_SIGN_MASK 0xC000
166 #define NUMERIC_POS 0x0000
167 #define NUMERIC_NEG 0x4000
168 #define NUMERIC_SHORT 0x8000
169 #define NUMERIC_SPECIAL 0xC000
171 #define NUMERIC_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_SIGN_MASK)
172 #define NUMERIC_IS_SHORT(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
173 #define NUMERIC_IS_SPECIAL(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SPECIAL)
175 #define NUMERIC_HDRSZ (VARHDRSZ + sizeof(uint16) + sizeof(int16))
176 #define NUMERIC_HDRSZ_SHORT (VARHDRSZ + sizeof(uint16))
183 #define NUMERIC_HEADER_IS_SHORT(n) (((n)->choice.n_header & 0x8000) != 0)
184 #define NUMERIC_HEADER_SIZE(n) \
185 (VARHDRSZ + sizeof(uint16) + \
186 (NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
197 #define NUMERIC_EXT_SIGN_MASK 0xF000
198 #define NUMERIC_NAN 0xC000
199 #define NUMERIC_PINF 0xD000
200 #define NUMERIC_NINF 0xF000
201 #define NUMERIC_INF_SIGN_MASK 0x2000
203 #define NUMERIC_EXT_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_EXT_SIGN_MASK)
204 #define NUMERIC_IS_NAN(n) ((n)->choice.n_header == NUMERIC_NAN)
205 #define NUMERIC_IS_PINF(n) ((n)->choice.n_header == NUMERIC_PINF)
206 #define NUMERIC_IS_NINF(n) ((n)->choice.n_header == NUMERIC_NINF)
207 #define NUMERIC_IS_INF(n) \
208 (((n)->choice.n_header & ~NUMERIC_INF_SIGN_MASK) == NUMERIC_PINF)
214 #define NUMERIC_SHORT_SIGN_MASK 0x2000
215 #define NUMERIC_SHORT_DSCALE_MASK 0x1F80
216 #define NUMERIC_SHORT_DSCALE_SHIFT 7
217 #define NUMERIC_SHORT_DSCALE_MAX \
218 (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)
219 #define NUMERIC_SHORT_WEIGHT_SIGN_MASK 0x0040
220 #define NUMERIC_SHORT_WEIGHT_MASK 0x003F
221 #define NUMERIC_SHORT_WEIGHT_MAX NUMERIC_SHORT_WEIGHT_MASK
222 #define NUMERIC_SHORT_WEIGHT_MIN (-(NUMERIC_SHORT_WEIGHT_MASK+1))
234 #define NUMERIC_DSCALE_MASK 0x3FFF
235 #define NUMERIC_DSCALE_MAX NUMERIC_DSCALE_MASK
237 #define NUMERIC_SIGN(n) \
238 (NUMERIC_IS_SHORT(n) ? \
239 (((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
240 NUMERIC_NEG : NUMERIC_POS) : \
241 (NUMERIC_IS_SPECIAL(n) ? \
242 NUMERIC_EXT_FLAGBITS(n) : NUMERIC_FLAGBITS(n)))
243 #define NUMERIC_DSCALE(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
244 ((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
245 >> NUMERIC_SHORT_DSCALE_SHIFT \
246 : ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
247 #define NUMERIC_WEIGHT(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
248 (((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
249 ~NUMERIC_SHORT_WEIGHT_MASK : 0) \
250 | ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
251 : ((n)->choice.n_long.n_weight))
395 #define NUMERIC_ABBREV_BITS (SIZEOF_DATUM * BITS_PER_BYTE)
396 #if SIZEOF_DATUM == 8
397 #define NumericAbbrevGetDatum(X) ((Datum) (X))
398 #define DatumGetNumericAbbrev(X) ((int64) (X))
399 #define NUMERIC_ABBREV_NAN NumericAbbrevGetDatum(PG_INT64_MIN)
400 #define NUMERIC_ABBREV_PINF NumericAbbrevGetDatum(-PG_INT64_MAX)
401 #define NUMERIC_ABBREV_NINF NumericAbbrevGetDatum(PG_INT64_MAX)
403 #define NumericAbbrevGetDatum(X) ((Datum) (X))
404 #define DatumGetNumericAbbrev(X) ((int32) (X))
405 #define NUMERIC_ABBREV_NAN NumericAbbrevGetDatum(PG_INT32_MIN)
406 #define NUMERIC_ABBREV_PINF NumericAbbrevGetDatum(-PG_INT32_MAX)
407 #define NUMERIC_ABBREV_NINF NumericAbbrevGetDatum(PG_INT32_MAX)
432 #elif DEC_DIGITS == 2
434 #elif DEC_DIGITS == 1
442 #elif DEC_DIGITS == 2
444 #elif DEC_DIGITS == 1
473 #define dump_numeric(s,n)
474 #define dump_var(s,v)
477 #define digitbuf_alloc(ndigits) \
478 ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
479 #define digitbuf_free(buf) \
485 #define init_var(v) memset(v, 0, sizeof(NumericVar))
487 #define NUMERIC_DIGITS(num) (NUMERIC_HEADER_IS_SHORT(num) ? \
488 (num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
489 #define NUMERIC_NDIGITS(num) \
490 ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
491 #define NUMERIC_CAN_BE_SHORT(scale,weight) \
492 ((scale) <= NUMERIC_SHORT_DSCALE_MAX && \
493 (weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
494 (weight) >= NUMERIC_SHORT_WEIGHT_MIN)
504 const char *cp,
int sign,
529 static bool numericvar_to_int128(
const NumericVar *var, int128 *result);
530 static void int128_to_numericvar(int128
val,
NumericVar *var);
545 int var1weight,
int var1sign,
547 int var2weight,
int var2sign);
557 int rscale,
bool round);
563 static void div_var_int64(
const NumericVar *var, int64 ival,
int ival_weight,
601 const NumericVar *count_var,
bool reversed_bounds,
634 Node *escontext = fcinfo->context;
637 const char *numstart;
644 if (!isspace((
unsigned char) *cp))
672 if (!isdigit((
unsigned char) *cp) && *cp !=
'.')
705 if (!isspace((
unsigned char) *cp))
762 &
value, &cp, escontext))
772 if (!isspace((
unsigned char) *cp))
784 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
785 errmsg(
"value overflows numeric format")));
794 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
795 errmsg(
"invalid input syntax for type %s: \"%s\"",
878 return (
arg.ndigits == 0 ||
arg.ndigits <=
arg.weight + 1);
919 return ((typmod -
VARHDRSZ) >> 16) & 0xffff;
934 return (((typmod -
VARHDRSZ) & 0x7ff) ^ 1024) - 1024;
1040 if (strchr(
str,
'.') != NULL)
1046 last = strlen(
str) - 1;
1047 while (
str[last] ==
'0')
1051 if (
str[last] ==
'.')
1055 str[last + 1] =
'\0';
1097 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1098 errmsg(
"invalid sign in external \"numeric\" value")));
1103 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1104 errmsg(
"invalid scale in external \"numeric\" value")));
1106 for (
i = 0;
i <
len;
i++)
1110 if (d < 0 || d >=
NBASE)
1112 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1113 errmsg(
"invalid digit in external \"numeric\" value")));
1168 for (
i = 0;
i <
x.ndigits;
i++)
1220 new_scale == old_scale && new_precision >= old_precision))
1289 new->choice.n_short.n_header =
1327 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1328 errmsg(
"NUMERIC precision %d must be between 1 and %d",
1332 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1333 errmsg(
"NUMERIC scale %d must be between %d and %d",
1341 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1342 errmsg(
"NUMERIC precision %d must be between 1 and %d",
1350 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1351 errmsg(
"invalid NUMERIC type modifier")));
1394 res->choice.n_short.n_header =
1399 res->choice.n_short.n_header =
1424 res->choice.n_short.n_header =
1437 res->choice.n_short.n_header =
1440 res->choice.n_long.n_sign_dscale =
1443 res->choice.n_long.n_sign_dscale =
1708 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1709 errmsg(
"start value cannot be NaN")));
1712 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1713 errmsg(
"start value cannot be infinity")));
1719 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1720 errmsg(
"stop value cannot be NaN")));
1723 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1724 errmsg(
"stop value cannot be infinity")));
1736 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1737 errmsg(
"step size cannot be NaN")));
1740 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1741 errmsg(
"step size cannot be infinity")));
1748 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1749 errmsg(
"step size cannot equal zero")));
1840 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1841 errmsg(
"count must be greater than zero")));
1851 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1852 errmsg(
"operand, lower bound, and upper bound cannot be NaN")));
1856 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1857 errmsg(
"lower and upper bounds must be finite")));
1870 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1871 errmsg(
"lower bound cannot equal upper bound")));
1900 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1901 errmsg(
"integer out of range")));
1917 const NumericVar *count_var,
bool reversed_bounds,
1928 if (!reversed_bounds)
1930 sub_var(&operand_var, &bound1_var, &operand_var);
1931 sub_var(&bound2_var, &bound1_var, &bound2_var);
1935 sub_var(&bound1_var, &operand_var, &operand_var);
1936 sub_var(&bound1_var, &bound2_var, &bound2_var);
1939 mul_var(&operand_var, count_var, &operand_var,
1941 div_var(&operand_var, &bound2_var, result_var,
2082 pfree(original_varatt);
2101 if (memtupcount < 10000 || nss->input_count < 10000 || !nss->estimating)
2112 if (abbr_card > 100000.0)
2117 "numeric_abbrev: estimation ends at cardinality %f"
2134 if (abbr_card < nss->input_count / 10000.0 + 0.5)
2139 "numeric_abbrev: aborting abbreviation at cardinality %f"
2140 " below threshold %f after " INT64_FORMAT " values (%d rows)",
2150 "numeric_abbrev: cardinality %f"
2250 #if NUMERIC_ABBREV_BITS == 64
2256 int weight = var->
weight;
2259 if (ndigits == 0 || weight < -44)
2263 else if (weight > 83)
2269 result = ((int64) (weight + 44) << 56);
2274 result |= ((int64) var->
digits[3]);
2277 result |= ((int64) var->
digits[2]) << 14;
2280 result |= ((int64) var->
digits[1]) << 28;
2283 result |= ((int64) var->
digits[0]) << 42;
2295 ^ (
uint32) ((uint64) result >> 32));
2305 #if NUMERIC_ABBREV_BITS == 32
2311 int weight = var->
weight;
2314 if (ndigits == 0 || weight < -11)
2318 else if (weight > 20)
2326 weight = (weight + 11) * 4;
2338 result = (result * 1000) + (nxt1 / 10);
2341 else if (result > 99)
2344 result = (result * 10000) + nxt1;
2347 else if (result > 9)
2352 result = (result * 100000) + (nxt1 * 10) + (nxt2 / 1000);
2360 result = (result * 1000000) + (nxt1 * 100) + (nxt2 / 100);
2363 result = result | (weight << 24);
2567 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2568 errmsg(
"invalid preceding or following size in window function")));
2665 sub_var(&basev, &offsetv, &sum);
2667 add_var(&basev, &offsetv, &sum);
2670 result = (
cmp_var(&valv, &sum) <= 0);
2672 result = (
cmp_var(&valv, &sum) >= 0);
2755 result = digit_hash ^ weight;
2897 add_var(&arg1, &arg2, &result);
2975 sub_var(&arg1, &arg2, &result);
3141 *have_error =
false;
3163 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3164 errmsg(
"division by zero")));
3186 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3187 errmsg(
"division by zero")));
3222 if (have_error && (arg2.
ndigits == 0 || arg2.
digits[0] == 0))
3231 div_var(&arg1, &arg2, &result, rscale,
true);
3271 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3272 errmsg(
"division by zero")));
3289 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3290 errmsg(
"division by zero")));
3320 div_var(&arg1, &arg2, &result, 0,
false);
3364 *have_error =
false;
3385 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3386 errmsg(
"division by zero")));
3403 if (have_error && (arg2.
ndigits == 0 || arg2.
digits[0] == 0))
3409 mod_var(&arg1, &arg2, &result);
3536 gcd_var(&arg1, &arg2, &result);
3590 gcd_var(&arg1, &arg2, &result);
3591 div_var(&arg1, &result, &result, 0,
false);
3621 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3622 errmsg(
"factorial of a negative number is undefined")));
3631 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3632 errmsg(
"value overflows numeric format")));
3639 for (num = num - 1; num > 1; num--)
3646 mul_var(&result, &fact, &result, 0);
3681 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3682 errmsg(
"cannot take square root of a negative number")));
3703 #if DEC_DIGITS == ((DEC_DIGITS / 2) * 2)
3706 if (
arg.weight >= 0)
3713 rscale =
Max(rscale,
arg.dscale);
3773 val *= 0.434294481903252;
3780 rscale =
Max(rscale,
arg.dscale);
3819 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3820 errmsg(
"cannot take logarithm of a negative number")));
3832 rscale =
Max(rscale,
arg.dscale);
3874 if (sign1 < 0 || sign2 < 0)
3876 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3877 errmsg(
"cannot take logarithm of a negative number")));
3879 if (sign1 == 0 || sign2 == 0)
3881 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3882 errmsg(
"cannot take logarithm of zero")));
3907 log_var(&arg1, &arg2, &result);
3967 if (sign1 == 0 && sign2 < 0)
3969 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3970 errmsg(
"zero raised to a negative power is undefined")));
3973 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3974 errmsg(
"a negative number raised to a non-integer power yields a complex result")));
4000 if (sign1 == 0 && sign2 > 0)
4019 abs_x_gt_one =
true;
4028 if (abs_x_gt_one == (sign2 > 0))
4079 if (sign1 == 0 && sign2 < 0)
4081 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
4082 errmsg(
"zero raised to a negative power is undefined")));
4134 last_digit_pos = var->
ndigits - 1;
4135 while (last_digit_pos >= 0 &&
4136 var->
digits[last_digit_pos] == 0)
4139 if (last_digit_pos >= 0)
4156 while (last_digit % 10 == 0)
4253 rscale = log10val2 < 0 ? 0 : log10val2;
4273 static const int pow10[] = {1, 10, 100, 1000};
4274 #elif DEC_DIGITS == 2
4275 static const int pow10[] = {1, 10};
4276 #elif DEC_DIGITS == 1
4277 static const int pow10[] = {1};
4279 #error unsupported NBASE
4292 tmp = (int128) val1 * (int128) factor;
4294 int128_to_numericvar(tmp, &result);
4303 mul_var(&result, &tmp, &result, 0);
4341 *have_error =
false;
4354 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4355 errmsg(
"cannot convert NaN to %s",
"integer")));
4358 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4359 errmsg(
"cannot convert infinity to %s",
"integer")));
4376 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4377 errmsg(
"integer out of range")));
4434 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4435 errmsg(
"cannot convert NaN to %s",
"bigint")));
4438 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4439 errmsg(
"cannot convert infinity to %s",
"bigint")));
4447 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4448 errmsg(
"bigint out of range")));
4475 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4476 errmsg(
"cannot convert NaN to %s",
"smallint")));
4479 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4480 errmsg(
"cannot convert infinity to %s",
"smallint")));
4488 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4489 errmsg(
"smallint out of range")));
4493 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4494 errmsg(
"smallint out of range")));
4509 char buf[DBL_DIG + 100];
4603 char buf[FLT_DIG + 100];
4671 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4672 errmsg(
"cannot convert NaN to %s",
"pg_lsn")));
4675 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4676 errmsg(
"cannot convert infinity to %s",
"pg_lsn")));
4684 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4685 errmsg(
"pg_lsn out of range")));
4720 #define NA_TOTAL_COUNT(na) \
4721 ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)
4735 elog(
ERROR,
"aggregate function called in non-aggregate context");
4740 state->calcSumX2 = calcSumX2;
4741 state->agg_context = agg_context;
4758 state->calcSumX2 = calcSumX2;
4796 state->maxScaleCount = 1;
4799 state->maxScaleCount++;
4802 if (
state->calcSumX2)
4816 if (
state->calcSumX2)
4868 if (
state->maxScaleCount > 1 ||
state->maxScale == 0)
4874 state->maxScaleCount--;
4876 else if (
state->N == 1)
4879 state->maxScale = 0;
4880 state->maxScaleCount = 0;
4890 if (
state->calcSumX2)
4905 if (
state->calcSumX2)
4918 if (
state->calcSumX2)
4959 elog(
ERROR,
"aggregate function called in non-aggregate context");
4973 state1->
N = state2->
N;
4988 state1->
N += state2->
N;
5051 elog(
ERROR,
"aggregate function called in non-aggregate context");
5065 state1->
N = state2->
N;
5079 state1->
N += state2->
N;
5124 elog(
ERROR,
"aggregate function called in non-aggregate context");
5175 elog(
ERROR,
"aggregate function called in non-aggregate context");
5236 elog(
ERROR,
"aggregate function called in non-aggregate context");
5291 elog(
ERROR,
"aggregate function called in non-aggregate context");
5354 elog(
ERROR,
"numeric_accum_inv called with NULL state");
5382 typedef struct Int128AggState
5394 static Int128AggState *
5397 Int128AggState *
state;
5402 elog(
ERROR,
"aggregate function called in non-aggregate context");
5406 state = (Int128AggState *)
palloc0(
sizeof(Int128AggState));
5407 state->calcSumX2 = calcSumX2;
5418 static Int128AggState *
5419 makeInt128AggStateCurrentContext(
bool calcSumX2)
5421 Int128AggState *
state;
5423 state = (Int128AggState *)
palloc0(
sizeof(Int128AggState));
5424 state->calcSumX2 = calcSumX2;
5433 do_int128_accum(Int128AggState *
state, int128
newval)
5435 if (
state->calcSumX2)
5446 do_int128_discard(Int128AggState *
state, int128
newval)
5448 if (
state->calcSumX2)
5456 #define makePolyNumAggState makeInt128AggState
5457 #define makePolyNumAggStateCurrentContext makeInt128AggStateCurrentContext
5460 #define makePolyNumAggState makeNumericAggState
5461 #define makePolyNumAggStateCurrentContext makeNumericAggStateCurrentContext
5539 elog(
ERROR,
"aggregate function called in non-aggregate context");
5553 state1->
N = state2->
N;
5570 state1->
N += state2->
N;
5605 elog(
ERROR,
"aggregate function called in non-aggregate context");
5627 int128_to_numericvar(
state->sumX, &tmp_var);
5635 int128_to_numericvar(
state->sumX2, &tmp_var);