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,
1949 if (
cmp_var(result_var, count_var) >= 0)
2093 pfree(original_varatt);
2112 if (memtupcount < 10000 || nss->input_count < 10000 || !nss->estimating)
2123 if (abbr_card > 100000.0)
2128 "numeric_abbrev: estimation ends at cardinality %f"
2145 if (abbr_card < nss->input_count / 10000.0 + 0.5)
2150 "numeric_abbrev: aborting abbreviation at cardinality %f"
2151 " below threshold %f after " INT64_FORMAT " values (%d rows)",
2161 "numeric_abbrev: cardinality %f"
2261 #if NUMERIC_ABBREV_BITS == 64
2267 int weight = var->
weight;
2270 if (ndigits == 0 || weight < -44)
2274 else if (weight > 83)
2280 result = ((int64) (weight + 44) << 56);
2285 result |= ((int64) var->
digits[3]);
2288 result |= ((int64) var->
digits[2]) << 14;
2291 result |= ((int64) var->
digits[1]) << 28;
2294 result |= ((int64) var->
digits[0]) << 42;
2306 ^ (
uint32) ((uint64) result >> 32));
2316 #if NUMERIC_ABBREV_BITS == 32
2322 int weight = var->
weight;
2325 if (ndigits == 0 || weight < -11)
2329 else if (weight > 20)
2337 weight = (weight + 11) * 4;
2349 result = (result * 1000) + (nxt1 / 10);
2352 else if (result > 99)
2355 result = (result * 10000) + nxt1;
2358 else if (result > 9)
2363 result = (result * 100000) + (nxt1 * 10) + (nxt2 / 1000);
2371 result = (result * 1000000) + (nxt1 * 100) + (nxt2 / 100);
2374 result = result | (weight << 24);
2578 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2579 errmsg(
"invalid preceding or following size in window function")));
2676 sub_var(&basev, &offsetv, &sum);
2678 add_var(&basev, &offsetv, &sum);
2681 result = (
cmp_var(&valv, &sum) <= 0);
2683 result = (
cmp_var(&valv, &sum) >= 0);
2766 result = digit_hash ^ weight;
2908 add_var(&arg1, &arg2, &result);
2986 sub_var(&arg1, &arg2, &result);
3152 *have_error =
false;
3174 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3175 errmsg(
"division by zero")));
3197 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3198 errmsg(
"division by zero")));
3233 if (have_error && (arg2.
ndigits == 0 || arg2.
digits[0] == 0))
3242 div_var(&arg1, &arg2, &result, rscale,
true);
3282 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3283 errmsg(
"division by zero")));
3300 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3301 errmsg(
"division by zero")));
3331 div_var(&arg1, &arg2, &result, 0,
false);
3375 *have_error =
false;
3396 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3397 errmsg(
"division by zero")));
3414 if (have_error && (arg2.
ndigits == 0 || arg2.
digits[0] == 0))
3420 mod_var(&arg1, &arg2, &result);
3547 gcd_var(&arg1, &arg2, &result);
3601 gcd_var(&arg1, &arg2, &result);
3602 div_var(&arg1, &result, &result, 0,
false);
3632 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3633 errmsg(
"factorial of a negative number is undefined")));
3642 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3643 errmsg(
"value overflows numeric format")));
3650 for (num = num - 1; num > 1; num--)
3657 mul_var(&result, &fact, &result, 0);
3692 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3693 errmsg(
"cannot take square root of a negative number")));
3714 #if DEC_DIGITS == ((DEC_DIGITS / 2) * 2)
3717 if (
arg.weight >= 0)
3724 rscale =
Max(rscale,
arg.dscale);
3784 val *= 0.434294481903252;
3791 rscale =
Max(rscale,
arg.dscale);
3830 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3831 errmsg(
"cannot take logarithm of a negative number")));
3843 rscale =
Max(rscale,
arg.dscale);
3885 if (sign1 < 0 || sign2 < 0)
3887 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3888 errmsg(
"cannot take logarithm of a negative number")));
3890 if (sign1 == 0 || sign2 == 0)
3892 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3893 errmsg(
"cannot take logarithm of zero")));
3918 log_var(&arg1, &arg2, &result);
3978 if (sign1 == 0 && sign2 < 0)
3980 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3981 errmsg(
"zero raised to a negative power is undefined")));
3984 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3985 errmsg(
"a negative number raised to a non-integer power yields a complex result")));
4011 if (sign1 == 0 && sign2 > 0)
4030 abs_x_gt_one =
true;
4039 if (abs_x_gt_one == (sign2 > 0))
4090 if (sign1 == 0 && sign2 < 0)
4092 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
4093 errmsg(
"zero raised to a negative power is undefined")));
4145 last_digit_pos = var->
ndigits - 1;
4146 while (last_digit_pos >= 0 &&
4147 var->
digits[last_digit_pos] == 0)
4150 if (last_digit_pos >= 0)
4167 while (last_digit % 10 == 0)
4264 rscale = log10val2 < 0 ? 0 : log10val2;
4284 static const int pow10[] = {1, 10, 100, 1000};
4285 #elif DEC_DIGITS == 2
4286 static const int pow10[] = {1, 10};
4287 #elif DEC_DIGITS == 1
4288 static const int pow10[] = {1};
4290 #error unsupported NBASE
4303 tmp = (int128) val1 * (int128) factor;
4305 int128_to_numericvar(tmp, &result);
4314 mul_var(&result, &tmp, &result, 0);
4352 *have_error =
false;
4365 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4366 errmsg(
"cannot convert NaN to %s",
"integer")));
4369 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4370 errmsg(
"cannot convert infinity to %s",
"integer")));
4387 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4388 errmsg(
"integer out of range")));
4445 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4446 errmsg(
"cannot convert NaN to %s",
"bigint")));
4449 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4450 errmsg(
"cannot convert infinity to %s",
"bigint")));
4458 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4459 errmsg(
"bigint out of range")));
4486 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4487 errmsg(
"cannot convert NaN to %s",
"smallint")));
4490 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4491 errmsg(
"cannot convert infinity to %s",
"smallint")));
4499 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4500 errmsg(
"smallint out of range")));
4504 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4505 errmsg(
"smallint out of range")));
4520 char buf[DBL_DIG + 100];
4614 char buf[FLT_DIG + 100];
4682 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4683 errmsg(
"cannot convert NaN to %s",
"pg_lsn")));
4686 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4687 errmsg(
"cannot convert infinity to %s",
"pg_lsn")));
4695 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4696 errmsg(
"pg_lsn out of range")));
4731 #define NA_TOTAL_COUNT(na) \
4732 ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)
4746 elog(
ERROR,
"aggregate function called in non-aggregate context");
4751 state->calcSumX2 = calcSumX2;
4752 state->agg_context = agg_context;
4769 state->calcSumX2 = calcSumX2;
4807 state->maxScaleCount = 1;
4810 state->maxScaleCount++;
4813 if (
state->calcSumX2)
4827 if (
state->calcSumX2)
4879 if (
state->maxScaleCount > 1 ||
state->maxScale == 0)
4885 state->maxScaleCount--;
4887 else if (
state->N == 1)
4890 state->maxScale = 0;
4891 state->maxScaleCount = 0;
4901 if (
state->calcSumX2)
4916 if (
state->calcSumX2)
4929 if (
state->calcSumX2)
4970 elog(
ERROR,
"aggregate function called in non-aggregate context");
4984 state1->
N = state2->
N;
4999 state1->
N += state2->
N;
5062 elog(
ERROR,
"aggregate function called in non-aggregate context");
5076 state1->
N = state2->
N;
5090 state1->
N += state2->
N;
5135 elog(
ERROR,
"aggregate function called in non-aggregate context");
5186 elog(
ERROR,
"aggregate function called in non-aggregate context");
5247 elog(
ERROR,
"aggregate function called in non-aggregate context");
5302 elog(
ERROR,
"aggregate function called in non-aggregate context");
5365 elog(
ERROR,
"numeric_accum_inv called with NULL state");
5393 typedef struct Int128AggState
5405 static Int128AggState *
5408 Int128AggState *
state;
5413 elog(
ERROR,
"aggregate function called in non-aggregate context");
5417 state = (Int128AggState *)
palloc0(
sizeof(Int128AggState));
5418 state->calcSumX2 = calcSumX2;
5429 static Int128AggState *
5430 makeInt128AggStateCurrentContext(
bool calcSumX2)
5432 Int128AggState *
state;
5434 state = (Int128AggState *)
palloc0(
sizeof(Int128AggState));
5435 state->calcSumX2 = calcSumX2;
5444 do_int128_accum(Int128AggState *
state, int128
newval)
5446 if (
state->calcSumX2)
5457 do_int128_discard(Int128AggState *
state, int128
newval)
5459 if (
state->calcSumX2)
5467 #define makePolyNumAggState makeInt128AggState
5468 #define makePolyNumAggStateCurrentContext makeInt128AggStateCurrentContext
5471 #define makePolyNumAggState makeNumericAggState
5472 #define makePolyNumAggStateCurrentContext makeNumericAggStateCurrentContext
5550 elog(
ERROR,
"aggregate function called in non-aggregate context");
5564 state1->
N = state2->
N;
5581 state1->
N += state2->
N;
5616 elog(
ERROR,
"aggregate function called in non-aggregate context");
5638 int128_to_numericvar(
state->sumX, &tmp_var);
5646 int128_to_numericvar(
state->sumX2, &tmp_var);