78 #define MUL_GUARD_DIGITS 4
79 #define DIV_GUARD_DIGITS 8
88 #define MUL_GUARD_DIGITS 3
89 #define DIV_GUARD_DIGITS 6
96 #define HALF_NBASE 5000
98 #define MUL_GUARD_DIGITS 2
99 #define DIV_GUARD_DIGITS 4
104 #define NBASE_SQR (NBASE * NBASE)
166 #define NUMERIC_SIGN_MASK 0xC000
167 #define NUMERIC_POS 0x0000
168 #define NUMERIC_NEG 0x4000
169 #define NUMERIC_SHORT 0x8000
170 #define NUMERIC_SPECIAL 0xC000
172 #define NUMERIC_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_SIGN_MASK)
173 #define NUMERIC_IS_SHORT(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
174 #define NUMERIC_IS_SPECIAL(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SPECIAL)
176 #define NUMERIC_HDRSZ (VARHDRSZ + sizeof(uint16) + sizeof(int16))
177 #define NUMERIC_HDRSZ_SHORT (VARHDRSZ + sizeof(uint16))
184 #define NUMERIC_HEADER_IS_SHORT(n) (((n)->choice.n_header & 0x8000) != 0)
185 #define NUMERIC_HEADER_SIZE(n) \
186 (VARHDRSZ + sizeof(uint16) + \
187 (NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
198 #define NUMERIC_EXT_SIGN_MASK 0xF000
199 #define NUMERIC_NAN 0xC000
200 #define NUMERIC_PINF 0xD000
201 #define NUMERIC_NINF 0xF000
202 #define NUMERIC_INF_SIGN_MASK 0x2000
204 #define NUMERIC_EXT_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_EXT_SIGN_MASK)
205 #define NUMERIC_IS_NAN(n) ((n)->choice.n_header == NUMERIC_NAN)
206 #define NUMERIC_IS_PINF(n) ((n)->choice.n_header == NUMERIC_PINF)
207 #define NUMERIC_IS_NINF(n) ((n)->choice.n_header == NUMERIC_NINF)
208 #define NUMERIC_IS_INF(n) \
209 (((n)->choice.n_header & ~NUMERIC_INF_SIGN_MASK) == NUMERIC_PINF)
215 #define NUMERIC_SHORT_SIGN_MASK 0x2000
216 #define NUMERIC_SHORT_DSCALE_MASK 0x1F80
217 #define NUMERIC_SHORT_DSCALE_SHIFT 7
218 #define NUMERIC_SHORT_DSCALE_MAX \
219 (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)
220 #define NUMERIC_SHORT_WEIGHT_SIGN_MASK 0x0040
221 #define NUMERIC_SHORT_WEIGHT_MASK 0x003F
222 #define NUMERIC_SHORT_WEIGHT_MAX NUMERIC_SHORT_WEIGHT_MASK
223 #define NUMERIC_SHORT_WEIGHT_MIN (-(NUMERIC_SHORT_WEIGHT_MASK+1))
235 #define NUMERIC_DSCALE_MASK 0x3FFF
236 #define NUMERIC_DSCALE_MAX NUMERIC_DSCALE_MASK
238 #define NUMERIC_SIGN(n) \
239 (NUMERIC_IS_SHORT(n) ? \
240 (((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
241 NUMERIC_NEG : NUMERIC_POS) : \
242 (NUMERIC_IS_SPECIAL(n) ? \
243 NUMERIC_EXT_FLAGBITS(n) : NUMERIC_FLAGBITS(n)))
244 #define NUMERIC_DSCALE(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
245 ((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
246 >> NUMERIC_SHORT_DSCALE_SHIFT \
247 : ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
248 #define NUMERIC_WEIGHT(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
249 (((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
250 ~NUMERIC_SHORT_WEIGHT_MASK : 0) \
251 | ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
252 : ((n)->choice.n_long.n_weight))
259 #define NUMERIC_WEIGHT_MAX PG_INT16_MAX
403 #define NUMERIC_ABBREV_BITS (SIZEOF_DATUM * BITS_PER_BYTE)
404 #if SIZEOF_DATUM == 8
405 #define NumericAbbrevGetDatum(X) ((Datum) (X))
406 #define DatumGetNumericAbbrev(X) ((int64) (X))
407 #define NUMERIC_ABBREV_NAN NumericAbbrevGetDatum(PG_INT64_MIN)
408 #define NUMERIC_ABBREV_PINF NumericAbbrevGetDatum(-PG_INT64_MAX)
409 #define NUMERIC_ABBREV_NINF NumericAbbrevGetDatum(PG_INT64_MAX)
411 #define NumericAbbrevGetDatum(X) ((Datum) (X))
412 #define DatumGetNumericAbbrev(X) ((int32) (X))
413 #define NUMERIC_ABBREV_NAN NumericAbbrevGetDatum(PG_INT32_MIN)
414 #define NUMERIC_ABBREV_PINF NumericAbbrevGetDatum(-PG_INT32_MAX)
415 #define NUMERIC_ABBREV_NINF NumericAbbrevGetDatum(PG_INT32_MAX)
440 #elif DEC_DIGITS == 2
442 #elif DEC_DIGITS == 1
450 #elif DEC_DIGITS == 2
452 #elif DEC_DIGITS == 1
481 #define dump_numeric(s,n)
482 #define dump_var(s,v)
485 #define digitbuf_alloc(ndigits) \
486 ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
487 #define digitbuf_free(buf) \
493 #define init_var(v) memset(v, 0, sizeof(NumericVar))
495 #define NUMERIC_DIGITS(num) (NUMERIC_HEADER_IS_SHORT(num) ? \
496 (num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
497 #define NUMERIC_NDIGITS(num) \
498 ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
499 #define NUMERIC_CAN_BE_SHORT(scale,weight) \
500 ((scale) <= NUMERIC_SHORT_DSCALE_MAX && \
501 (weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
502 (weight) >= NUMERIC_SHORT_WEIGHT_MIN)
512 const char *cp,
int sign,
537 static bool numericvar_to_int128(
const NumericVar *var, int128 *result);
538 static void int128_to_numericvar(int128
val,
NumericVar *var);
553 int var1weight,
int var1sign,
555 int var2weight,
int var2sign);
566 NumericVar *result,
int rscale,
bool round,
bool exact);
570 static void div_var_int64(
const NumericVar *var, int64 ival,
int ival_weight,
643 Node *escontext = fcinfo->context;
646 const char *numstart;
653 if (!isspace((
unsigned char) *cp))
681 if (!isdigit((
unsigned char) *cp) && *cp !=
'.')
714 if (!isspace((
unsigned char) *cp))
771 &
value, &cp, escontext))
781 if (!isspace((
unsigned char) *cp))
793 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
794 errmsg(
"value overflows numeric format")));
803 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
804 errmsg(
"invalid input syntax for type %s: \"%s\"",
887 return (
arg.ndigits == 0 ||
arg.ndigits <=
arg.weight + 1);
928 return ((typmod -
VARHDRSZ) >> 16) & 0xffff;
943 return (((typmod -
VARHDRSZ) & 0x7ff) ^ 1024) - 1024;
1049 if (strchr(
str,
'.') != NULL)
1055 last = strlen(
str) - 1;
1056 while (
str[last] ==
'0')
1060 if (
str[last] ==
'.')
1064 str[last + 1] =
'\0';
1106 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1107 errmsg(
"invalid sign in external \"numeric\" value")));
1112 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1113 errmsg(
"invalid scale in external \"numeric\" value")));
1115 for (
i = 0;
i <
len;
i++)
1119 if (d < 0 || d >=
NBASE)
1121 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1122 errmsg(
"invalid digit in external \"numeric\" value")));
1177 for (
i = 0;
i <
x.ndigits;
i++)
1229 new_scale == old_scale && new_precision >= old_precision))
1298 new->choice.n_short.n_header =
1336 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1337 errmsg(
"NUMERIC precision %d must be between 1 and %d",
1341 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1342 errmsg(
"NUMERIC scale %d must be between %d and %d",
1350 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1351 errmsg(
"NUMERIC precision %d must be between 1 and %d",
1359 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1360 errmsg(
"invalid NUMERIC type modifier")));
1403 res->choice.n_short.n_header =
1408 res->choice.n_short.n_header =
1433 res->choice.n_short.n_header =
1446 res->choice.n_short.n_header =
1449 res->choice.n_long.n_sign_dscale =
1452 res->choice.n_long.n_sign_dscale =
1725 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1726 errmsg(
"start value cannot be NaN")));
1729 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1730 errmsg(
"start value cannot be infinity")));
1736 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1737 errmsg(
"stop value cannot be NaN")));
1740 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1741 errmsg(
"stop value cannot be infinity")));
1753 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1754 errmsg(
"step size cannot be NaN")));
1757 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1758 errmsg(
"step size cannot be infinity")));
1765 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1766 errmsg(
"step size cannot equal zero")));
1857 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1858 errmsg(
"count must be greater than zero")));
1868 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1869 errmsg(
"operand, lower bound, and upper bound cannot be NaN")));
1873 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1874 errmsg(
"lower and upper bounds must be finite")));
1887 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
1888 errmsg(
"lower bound cannot equal upper bound")));
1917 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1918 errmsg(
"integer out of range")));
1953 sub_var(&operand_var, &bound1_var, &operand_var);
1954 sub_var(&bound2_var, &bound1_var, &bound2_var);
1956 mul_var(&operand_var, count_var, &operand_var,
1958 div_var(&operand_var, &bound2_var, result_var, 0,
false,
true);
2097 pfree(original_varatt);
2116 if (memtupcount < 10000 || nss->input_count < 10000 || !nss->estimating)
2127 if (abbr_card > 100000.0)
2131 "numeric_abbrev: estimation ends at cardinality %f"
2147 if (abbr_card < nss->input_count / 10000.0 + 0.5)
2151 "numeric_abbrev: aborting abbreviation at cardinality %f"
2152 " below threshold %f after " INT64_FORMAT " values (%d rows)",
2160 "numeric_abbrev: cardinality %f"
2259 #if NUMERIC_ABBREV_BITS == 64
2265 int weight = var->
weight;
2268 if (ndigits == 0 || weight < -44)
2272 else if (weight > 83)
2278 result = ((int64) (weight + 44) << 56);
2283 result |= ((int64) var->
digits[3]);
2286 result |= ((int64) var->
digits[2]) << 14;
2289 result |= ((int64) var->
digits[1]) << 28;
2292 result |= ((int64) var->
digits[0]) << 42;
2304 ^ (
uint32) ((uint64) result >> 32));
2314 #if NUMERIC_ABBREV_BITS == 32
2320 int weight = var->
weight;
2323 if (ndigits == 0 || weight < -11)
2327 else if (weight > 20)
2335 weight = (weight + 11) * 4;
2347 result = (result * 1000) + (nxt1 / 10);
2350 else if (result > 99)
2353 result = (result * 10000) + nxt1;
2356 else if (result > 9)
2361 result = (result * 100000) + (nxt1 * 10) + (nxt2 / 1000);
2369 result = (result * 1000000) + (nxt1 * 100) + (nxt2 / 100);
2372 result = result | (weight << 24);
2576 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2577 errmsg(
"invalid preceding or following size in window function")));
2674 sub_var(&basev, &offsetv, &sum);
2676 add_var(&basev, &offsetv, &sum);
2679 result = (
cmp_var(&valv, &sum) <= 0);
2681 result = (
cmp_var(&valv, &sum) >= 0);
2764 result = digit_hash ^ weight;
2906 add_var(&arg1, &arg2, &result);
2984 sub_var(&arg1, &arg2, &result);
3150 *have_error =
false;
3172 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3173 errmsg(
"division by zero")));
3195 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3196 errmsg(
"division by zero")));
3231 if (have_error && (arg2.
ndigits == 0 || arg2.
digits[0] == 0))
3240 div_var(&arg1, &arg2, &result, rscale,
true,
true);
3280 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3281 errmsg(
"division by zero")));
3298 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3299 errmsg(
"division by zero")));
3329 div_var(&arg1, &arg2, &result, 0,
false,
true);
3373 *have_error =
false;
3394 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3395 errmsg(
"division by zero")));
3412 if (have_error && (arg2.
ndigits == 0 || arg2.
digits[0] == 0))
3418 mod_var(&arg1, &arg2, &result);
3545 gcd_var(&arg1, &arg2, &result);
3599 gcd_var(&arg1, &arg2, &result);
3600 div_var(&arg1, &result, &result, 0,
false,
true);
3630 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3631 errmsg(
"factorial of a negative number is undefined")));
3640 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3641 errmsg(
"value overflows numeric format")));
3648 for (num = num - 1; num > 1; num--)
3655 mul_var(&result, &fact, &result, 0);
3690 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3691 errmsg(
"cannot take square root of a negative number")));
3712 #if DEC_DIGITS == ((DEC_DIGITS / 2) * 2)
3715 if (
arg.weight >= 0)
3722 rscale =
Max(rscale,
arg.dscale);
3782 val *= 0.434294481903252;
3789 rscale =
Max(rscale,
arg.dscale);
3828 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3829 errmsg(
"cannot take logarithm of a negative number")));
3841 rscale =
Max(rscale,
arg.dscale);
3883 if (sign1 < 0 || sign2 < 0)
3885 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3886 errmsg(
"cannot take logarithm of a negative number")));
3888 if (sign1 == 0 || sign2 == 0)
3890 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
3891 errmsg(
"cannot take logarithm of zero")));
3916 log_var(&arg1, &arg2, &result);
3976 if (sign1 == 0 && sign2 < 0)
3978 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3979 errmsg(
"zero raised to a negative power is undefined")));
3982 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3983 errmsg(
"a negative number raised to a non-integer power yields a complex result")));
4009 if (sign1 == 0 && sign2 > 0)
4028 abs_x_gt_one =
true;
4037 if (abs_x_gt_one == (sign2 > 0))
4088 if (sign1 == 0 && sign2 < 0)
4090 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
4091 errmsg(
"zero raised to a negative power is undefined")));
4143 last_digit_pos = var->
ndigits - 1;
4144 while (last_digit_pos >= 0 &&
4145 var->
digits[last_digit_pos] == 0)
4148 if (last_digit_pos >= 0)
4165 while (last_digit % 10 == 0)
4237 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4238 errmsg(
"lower bound cannot be NaN"));
4241 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4242 errmsg(
"lower bound cannot be infinity"));
4248 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4249 errmsg(
"upper bound cannot be NaN"));
4252 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4253 errmsg(
"upper bound cannot be infinity"));
4312 rscale = log10val2 < 0 ? 0 : log10val2;
4332 static const int pow10[] = {1, 10, 100, 1000};
4333 #elif DEC_DIGITS == 2
4334 static const int pow10[] = {1, 10};
4335 #elif DEC_DIGITS == 1
4336 static const int pow10[] = {1};
4338 #error unsupported NBASE
4351 tmp = (int128) val1 * (int128) factor;
4353 int128_to_numericvar(tmp, &result);
4362 mul_var(&result, &tmp, &result, 0);
4400 *have_error =
false;
4413 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4414 errmsg(
"cannot convert NaN to %s",
"integer")));
4417 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4418 errmsg(
"cannot convert infinity to %s",
"integer")));
4435 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4436 errmsg(
"integer out of range")));
4488 *have_error =
false;
4501 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4502 errmsg(
"cannot convert NaN to %s",
"bigint")));
4505 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4506 errmsg(
"cannot convert infinity to %s",
"bigint")));
4523 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4524 errmsg(
"bigint out of range")));
4561 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4562 errmsg(
"cannot convert NaN to %s",
"smallint")));
4565 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4566 errmsg(
"cannot convert infinity to %s",
"smallint")));
4574 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4575 errmsg(
"smallint out of range")));
4579 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4580 errmsg(
"smallint out of range")));
4595 char buf[DBL_DIG + 100];
4689 char buf[FLT_DIG + 100];
4757 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4758 errmsg(
"cannot convert NaN to %s",
"pg_lsn")));
4761 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4762 errmsg(
"cannot convert infinity to %s",
"pg_lsn")));
4770 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4771 errmsg(
"pg_lsn out of range")));
4806 #define NA_TOTAL_COUNT(na) \
4807 ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)
4821 elog(
ERROR,
"aggregate function called in non-aggregate context");
4826 state->calcSumX2 = calcSumX2;
4827 state->agg_context = agg_context;
4844 state->calcSumX2 = calcSumX2;
4882 state->maxScaleCount = 1;
4885 state->maxScaleCount++;
4888 if (
state->calcSumX2)
4902 if (
state->calcSumX2)
4954 if (
state->maxScaleCount > 1 ||
state->maxScale == 0)
4960 state->maxScaleCount--;
4962 else if (
state->N == 1)
4965 state->maxScale = 0;
4966 state->maxScaleCount = 0;
4976 if (
state->calcSumX2)
4991 if (
state->calcSumX2)
5004 if (
state->calcSumX2)
5045 elog(
ERROR,
"aggregate function called in non-aggregate context");
5059 state1->
N = state2->
N;
5074 state1->
N += state2->
N;
5137 elog(
ERROR,
"aggregate function called in non-aggregate context");
5151 state1->
N = state2->
N;
5165 state1->
N += state2->
N;
5210 elog(
ERROR,
"aggregate function called in non-aggregate context");
5261 elog(
ERROR,
"aggregate function called in non-aggregate context");
5320 elog(
ERROR,
"aggregate function called in non-aggregate context");
5375 elog(
ERROR,
"aggregate function called in non-aggregate context");
5436 elog(
ERROR,
"numeric_accum_inv called with NULL state");
5464 typedef struct Int128AggState
5476 static Int128AggState *
5479 Int128AggState *
state;
5484 elog(
ERROR,
"aggregate function called in non-aggregate context");
5488 state = (Int128AggState *)
palloc0(
sizeof(Int128AggState));
5489 state->calcSumX2 = calcSumX2;
5500 static Int128AggState *
5501 makeInt128AggStateCurrentContext(
bool calcSumX2)
5503 Int128AggState *
state;
5505 state = (Int128AggState *)
palloc0(
sizeof(Int128AggState));
5506 state->calcSumX2 = calcSumX2;
5515 do_int128_accum(Int128AggState *
state, int128
newval)
5517 if (
state->calcSumX2)
5528 do_int128_discard(Int128AggState *
state, int128
newval)
5530 if (
state->calcSumX2)
5538 #define makePolyNumAggState makeInt128AggState
5539 #define makePolyNumAggStateCurrentContext makeInt128AggStateCurrentContext
5542 #define makePolyNumAggState makeNumericAggState
5543 #define makePolyNumAggStateCurrentContext makeNumericAggStateCurrentContext
5621 elog(
ERROR,
"aggregate function called in non-aggregate context");
5635 state1->
N = state2->
N;
5652 state1->
N += state2->
N;
5687 elog(
ERROR,
"aggregate function called in non-aggregate context");
5709 int128_to_numericvar(
state->sumX, &tmp_var);
5717 int128_to_numericvar(
state->sumX2, &tmp_var);
5744 elog(
ERROR,
"aggregate function called in non-aggregate context");
5765 numericvar_to_int128(&tmp_var, &result->
sumX);
5773 numericvar_to_int128(&tmp_var, &result->
sumX2);
5824 elog(
ERROR,
"aggregate function called in non-aggregate context");
5838 state1->
N = state2->
N;
5852 state1->
N += state2->
N;
5885 elog(
ERROR,
"aggregate function called in non-aggregate context");
5907 int128_to_numericvar(
state->sumX, &tmp_var);
5933 elog(
ERROR,
"aggregate function called in non-aggregate context");
5954 numericvar_to_int128(&tmp_var, &result->
sumX);
5979 elog(
ERROR,
"int2_accum_inv called with NULL state");
5988 elog(
ERROR,
"do_numeric_discard failed unexpectedly");
6004 elog(
ERROR,
"int4_accum_inv called with NULL state");
6013 elog(
ERROR,
"do_numeric_discard failed unexpectedly");
6029 elog(
ERROR,
"int8_accum_inv called with NULL state");
6035 elog(
ERROR,
"do_numeric_discard failed unexpectedly");
6050 elog(
ERROR,
"int8_avg_accum_inv called with NULL state");
6059 elog(
ERROR,
"do_numeric_discard failed unexpectedly");
6082 int128_to_numericvar(
state->sumX, &result);
6111 int128_to_numericvar(
state->sumX, &result);
6138 if (
state->NaNcount > 0)
6142 if (
state->pInfcount > 0 &&
state->nInfcount > 0)
6144 if (
state->pInfcount > 0)
6146 if (
state->nInfcount > 0)
6172 if (
state->NaNcount > 0)
6176 if (
state->pInfcount > 0 &&
state->nInfcount > 0)
6178 if (
state->pInfcount > 0)
6180 if (
state->nInfcount > 0)
6204 bool variance,
bool sample,
6226 if (sample && totCount <= 1)
6238 if (
state->NaNcount > 0 ||
state->pInfcount > 0 ||
state->nInfcount > 0)
6254 rscale = vsumX.
dscale * 2;
6256 mul_var(&vsumX, &vsumX, &vsumX, rscale);
6257 mul_var(&vN, &vsumX2, &vsumX2, rscale);
6258 sub_var(&vsumX2, &vsumX, &vsumX2);
6268 mul_var(&vN, &vNminus1, &vNminus1, 0);
6270 mul_var(&vN, &vN, &vNminus1, 0);
6272 div_var(&vsumX2, &vNminus1, &vsumX, rscale,
true,
true);
6356 numeric_poly_stddev_internal(Int128AggState *
state,
6357 bool variance,
bool sample,
6374 int128_to_numericvar(
state->sumX, &tmp_var);
6377 int128_to_numericvar(
state->sumX2, &tmp_var);
6410 res = numeric_poly_stddev_internal(
state,
true,
true, &is_null);
6431 res = numeric_poly_stddev_internal(
state,
false,
true, &is_null);
6452 res = numeric_poly_stddev_internal(
state,
true,
false, &is_null);
6473 res = numeric_poly_stddev_internal(
state,
false,
false, &is_null);
6526 #ifndef USE_FLOAT8_BYVAL
6575 #ifndef USE_FLOAT8_BYVAL
6672 elog(
ERROR,
"expected 2-element int8 array");
6700 elog(
ERROR,
"expected 2-element int8 array");
6718 elog(
ERROR,
"aggregate function called in non-aggregate context");
6725 elog(
ERROR,
"expected 2-element int8 array");
6729 elog(
ERROR,
"expected 2-element int8 array");
6735 state1->
sum += state2->
sum;
6759 elog(
ERROR,
"expected 2-element int8 array");
6787 elog(
ERROR,
"expected 2-element int8 array");
6806 elog(
ERROR,
"expected 2-element int8 array");
6810 if (transdata->
count == 0)
6831 elog(
ERROR,
"expected 2-element int8 array");
6835 if (transdata->
count == 0)
6849 #ifdef NUMERIC_DEBUG
6887 for (
i = 0;
i < ndigits;
i++)
7013 bool have_dp =
false;
7015 unsigned char *decdigits;
7048 if (!isdigit((
unsigned char) *cp))
7049 goto invalid_syntax;
7059 if (isdigit((
unsigned char) *cp))
7061 decdigits[
i++] = *cp++ -
'0';
7067 else if (*cp ==
'.')
7070 goto invalid_syntax;
7075 goto invalid_syntax;
7077 else if (*cp ==
'_')
7081 if (!isdigit((
unsigned char) *cp))
7082 goto invalid_syntax;
7093 if (*cp ==
'e' || *cp ==
'E')
7111 else if (*cp ==
'-')
7118 if (!isdigit((
unsigned char) *cp))
7119 goto invalid_syntax;
7123 if (isdigit((
unsigned char) *cp))
7125 exponent = exponent * 10 + (*cp++ -
'0');
7129 else if (*cp ==
'_')
7133 if (!isdigit((
unsigned char) *cp))
7134 goto invalid_syntax;
7141 exponent = -exponent;
7143 dweight += (int) exponent;
7144 dscale -= (int) exponent;
7159 offset = (weight + 1) *
DEC_DIGITS - (dweight + 1);
7164 dest->weight = weight;
7165 dest->dscale = dscale;
7170 while (ndigits-- > 0)
7173 *
digits++ = ((decdigits[
i] * 10 + decdigits[
i + 1]) * 10 +
7174 decdigits[
i + 2]) * 10 + decdigits[
i + 3];
7175 #elif DEC_DIGITS == 2
7176 *
digits++ = decdigits[
i] * 10 + decdigits[
i + 1];
7177 #elif DEC_DIGITS == 1
7180 #error unsupported NBASE
7197 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7198 errmsg(
"value overflows numeric format")));
7202 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
7203 errmsg(
"invalid input syntax for type %s: \"%s\"",
7214 return dig >=
'0' && dig <=
'9' ? dig -
'0' :
7215 dig >=
'a' && dig <=
'f' ? dig -
'a' + 10 :
7216 dig >=
'A' && dig <=
'F' ? dig -
'A' + 10 : -1;
7241 const char **endptr,
Node *escontext)
7243 const char *firstdigit = cp;
7265 if (isxdigit((
unsigned char) *cp))
7287 else if (*cp ==
'_')
7291 if (!isxdigit((
unsigned char) *cp))
7292 goto invalid_syntax;
7302 if (*cp >=
'0' && *cp <=
'7')
7321 tmp = tmp * 8 + (*cp++ -
'0');
7324 else if (*cp ==
'_')
7328 if (*cp < '0' || *cp >
'7')
7329 goto invalid_syntax;
7339 if (*cp >=
'0' && *cp <=
'1')
7358 tmp = tmp * 2 + (*cp++ -
'0');
7361 else if (*cp ==
'_')
7365 if (*cp < '0' || *cp >
'1')
7366 goto invalid_syntax;
7374 goto invalid_syntax;
7378 goto invalid_syntax;
7400 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7401 errmsg(
"value overflows numeric format")));
7405 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
7406 errmsg(
"invalid input syntax for type %s: \"%s\"",
7471 if (
value->ndigits > 0)
7472 memcpy(newbuf + 1,
value->digits,
7479 dest->digits = newbuf + 1;
7538 for (d = 0; d <= var->
weight; d++)
7544 bool putit = (d > 0);
7563 #elif DEC_DIGITS == 2
7566 if (d1 > 0 || d > 0)
7569 #elif DEC_DIGITS == 1
7572 #error unsupported NBASE
7585 endcp = cp + dscale;
7690 div_var(var, &tmp_var, &tmp_var, rscale,
true,
true);
7702 len = strlen(sig_out) + 13;
7749 for (
i = 0;
i <
len;
i++)
7783 int weight = var->
weight;
7789 *have_error =
false;
7816 while (n > 0 && *
digits == 0)
7823 while (n > 0 &&
digits[n - 1] == 0)
7839 result->choice.n_short.n_header =
7851 result->choice.n_long.n_sign_dscale =
7853 result->choice.n_long.n_weight = weight;
7872 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7873 errmsg(
"value overflows numeric format")));
7950 else if (dig < 1000)
7952 #elif DEC_DIGITS == 2
7955 #elif DEC_DIGITS == 1
7958 #error unsupported NBASE
7962 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7963 errmsg(
"numeric field overflow"),
7964 errdetail(
"A field with precision %d, scale %d must round to an absolute value less than %s%d.",
8013 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8014 errmsg(
"numeric field overflow"),
8015 errdetail(
"A field with precision %d, scale %d cannot hold an infinite value.",
8016 precision,
scale)));
8056 Assert(weight >= 0 && ndigits <= weight + 1);
8066 for (
i = 1;
i <= weight;
i++)
8133 newuval = uval /
NBASE;
8134 *ptr = uval - newuval *
NBASE;
8139 var->
weight = ndigits - 1;
8184 Assert(weight >= 0 && ndigits <= weight + 1);
8189 for (
i = 1;
i <= weight;
i++)
8221 numericvar_to_int128(
const NumericVar *var, int128 *result)
8252 Assert(weight >= 0 && ndigits <= weight + 1);
8258 for (
i = 1;
i <= weight;
i++)
8274 if (!neg || (-
val) !=
val ||
val == 0 || oldval < 0)
8284 *result = neg ? -
val :
val;
8324 newuval = uval /
NBASE;
8325 *ptr = uval - newuval *
NBASE;
8330 var->
weight = ndigits - 1;
8347 val = strtod(tmp, &endptr);
8348 if (*endptr !=
'\0')
8352 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
8353 errmsg(
"invalid input syntax for type %s: \"%s\"",
8354 "double precision", tmp)));
8386 int var1weight,
int var1sign,
8388 int var2weight,
int var2sign)
8390 if (var1ndigits == 0)
8392 if (var2ndigits == 0)
8398 if (var2ndigits == 0)
8410 var2digits, var2ndigits, var2weight);
8417 var1digits, var1ndigits, var1weight);
8670 int res_ndigitpairs;
8683 int var1ndigitpairs;
8684 int var2ndigitpairs;
8713 var1digits = var1->
digits;
8714 var2digits = var2->
digits;
8716 if (var1ndigits == 0)
8728 if (var1ndigits <= 6 && rscale == var1->dscale + var2->
dscale)
8764 var1ndigitpairs = (var1ndigits + 1) / 2;
8765 var2ndigitpairs = (var2ndigits + 1) / 2;
8768 res_ndigits = var1ndigits + var2ndigits;
8771 res_ndigitpairs = res_ndigits / 2 + 1;
8774 pair_offset = res_ndigitpairs - var1ndigitpairs - var2ndigitpairs + 1;
8777 res_weight = var1->
weight + var2->
weight + 1 + 2 * res_ndigitpairs -
8778 res_ndigits - (var1ndigits & 1) - (var2ndigits & 1);
8785 res_ndigitpairs =
Min(res_ndigitpairs, maxdigitpairs);
8786 res_ndigits = 2 * res_ndigitpairs;
8794 if (res_ndigitpairs <= pair_offset)
8801 var1ndigitpairs =
Min(var1ndigitpairs, res_ndigitpairs - pair_offset);
8802 var2ndigitpairs =
Min(var2ndigitpairs, res_ndigitpairs - pair_offset);
8827 dig = (uint64 *)
palloc(res_ndigitpairs *
sizeof(uint64) +
8828 var2ndigitpairs *
sizeof(
uint32));
8831 var2digitpairs = (
uint32 *) (dig + res_ndigitpairs);
8833 for (i2 = 0; i2 < var2ndigitpairs - 1; i2++)
8834 var2digitpairs[i2] = var2digits[2 * i2] *
NBASE + var2digits[2 * i2 + 1];
8836 if (2 * i2 + 1 < var2ndigits)
8837 var2digitpairs[i2] = var2digits[2 * i2] *
NBASE + var2digits[2 * i2 + 1];
8839 var2digitpairs[i2] = var2digits[2 * i2] *
NBASE;
8854 i1 = var1ndigitpairs - 1;
8855 if (2 * i1 + 1 < var1ndigits)
8856 var1digitpair = var1digits[2 * i1] *
NBASE + var1digits[2 * i1 + 1];
8858 var1digitpair = var1digits[2 * i1] *
NBASE;
8859 maxdig = var1digitpair;
8861 i2limit =
Min(var2ndigitpairs, res_ndigitpairs - i1 - pair_offset);
8862 dig_i1_off = &dig[i1 + pair_offset];
8864 memset(dig, 0, (i1 + pair_offset) *
sizeof(uint64));
8865 for (i2 = 0; i2 < i2limit; i2++)
8866 dig_i1_off[i2] = (uint64) var1digitpair * var2digitpairs[i2];
8873 for (i1 = i1 - 1; i1 >= 0; i1--)
8875 var1digitpair = var1digits[2 * i1] *
NBASE + var1digits[2 * i1 + 1];
8876 if (var1digitpair == 0)
8880 maxdig += var1digitpair;
8885 for (
i = res_ndigitpairs - 1;
i >= 0;
i--)
8887 newdig = dig[
i] + carry;
8899 maxdig = 1 + var1digitpair;
8903 i2limit =
Min(var2ndigitpairs, res_ndigitpairs - i1 - pair_offset);
8904 dig_i1_off = &dig[i1 + pair_offset];
8906 for (i2 = 0; i2 < i2limit; i2++)
8907 dig_i1_off[i2] += (uint64) var1digitpair * var2digitpairs[i2];
8916 res_digits = result->
digits;
8918 for (
i = res_ndigitpairs - 1;
i >= 0;
i--)
8920 newdig = dig[
i] + carry;
8938 result->
weight = res_weight;
8939 result->
sign = res_sign;
8960 int var1ndigits = var1->
ndigits;
8961 int var2ndigits = var2->
ndigits;
8973 Assert(var1ndigits >= 1);
8974 Assert(var1ndigits <= 6);
8975 Assert(var2ndigits >= var1ndigits);
8989 res_ndigits = var1ndigits + var2ndigits;
8994 res_digits = res_buf + 1;
9001 #define PRODSUM1(v1,i1,v2,i2) ((v1)[(i1)] * (v2)[(i2)])
9002 #define PRODSUM2(v1,i1,v2,i2) (PRODSUM1(v1,i1,v2,i2) + (v1)[(i1)+1] * (v2)[(i2)-1])
9003 #define PRODSUM3(v1,i1,v2,i2) (PRODSUM2(v1,i1,v2,i2) + (v1)[(i1)+2] * (v2)[(i2)-2])
9004 #define PRODSUM4(v1,i1,v2,i2) (PRODSUM3(v1,i1,v2,i2) + (v1)[(i1)+3] * (v2)[(i2)-3])
9005 #define PRODSUM5(v1,i1,v2,i2) (PRODSUM4(v1,i1,v2,i2) + (v1)[(i1)+4] * (v2)[(i2)-4])
9006 #define PRODSUM6(v1,i1,v2,i2) (PRODSUM5(v1,i1,v2,i2) + (v1)[(i1)+5] * (v2)[(i2)-5])
9008 switch (var1ndigits)
9018 for (
int i = var2ndigits - 1;
i >= 0;
i--)
9020 term =
PRODSUM1(var1digits, 0, var2digits,
i) + carry;
9022 carry = term /
NBASE;
9036 term =
PRODSUM1(var1digits, 1, var2digits, var2ndigits - 1);
9038 carry = term /
NBASE;
9041 for (
int i = var2ndigits - 1;
i >= 1;
i--)
9043 term =
PRODSUM2(var1digits, 0, var2digits,
i) + carry;
9045 carry = term /
NBASE;
9058 term =
PRODSUM1(var1digits, 2, var2digits, var2ndigits - 1);
9060 carry = term /
NBASE;
9062 term =
PRODSUM2(var1digits, 1, var2digits, var2ndigits - 1) + carry;
9064 carry = term /
NBASE;
9067 for (
int i = var2ndigits - 1;
i >= 2;
i--)
9069 term =
PRODSUM3(var1digits, 0, var2digits,
i) + carry;
9071 carry = term /
NBASE;
9084 term =
PRODSUM1(var1digits, 3, var2digits, var2ndigits - 1);
9086 carry = term /
NBASE;
9088 term =
PRODSUM2(var1digits, 2, var2digits, var2ndigits - 1) + carry;
9090 carry = term /
NBASE;
9092 term =
PRODSUM3(var1digits, 1, var2digits, var2ndigits - 1) + carry;
9094 carry = term /
NBASE;
9097 for (
int i = var2ndigits - 1;
i >= 3;
i--)
9099 term =
PRODSUM4(var1digits, 0, var2digits,
i) + carry;
9101 carry = term /
NBASE;
9114 term =
PRODSUM1(var1digits, 4, var2digits, var2ndigits - 1);
9116 carry = term /
NBASE;
9118 term =
PRODSUM2(var1digits, 3, var2digits, var2ndigits - 1) + carry;
9120 carry = term /
NBASE;
9122 term =
PRODSUM3(var1digits, 2, var2digits, var2ndigits - 1) + carry;
9124 carry = term /
NBASE;
9126 term =
PRODSUM4(var1digits, 1, var2digits, var2ndigits - 1) + carry;
9128 carry = term /
NBASE;
9131 for (
int i = var2ndigits - 1;
i >= 4;
i--)
9133 term =
PRODSUM5(var1digits, 0, var2digits,
i) + carry;
9135 carry = term /
NBASE;
9148 term =
PRODSUM1(var1digits, 5, var2digits, var2ndigits - 1);
9150 carry = term /
NBASE;
9152 term =
PRODSUM2(var1digits, 4, var2digits, var2ndigits - 1) + carry;
9154 carry = term /
NBASE;
9156 term =
PRODSUM3(var1digits, 3, var2digits, var2ndigits - 1) + carry;
9158 carry = term /
NBASE;
9160 term =
PRODSUM4(var1digits, 2, var2digits, var2ndigits - 1) + carry;
9162 carry = term /
NBASE;
9164 term =
PRODSUM5(var1digits, 1, var2digits, var2ndigits - 1) + carry;
9166 carry = term /
NBASE;
9169 for (
int i = var2ndigits - 1;
i >= 5;
i--)
9171 term =
PRODSUM6(var1digits, 0, var2digits,
i) + carry;
9173 carry = term /
NBASE;
9182 switch (var1ndigits)
9185 term =
PRODSUM5(var1digits, 0, var2digits, 4) + carry;
9187 carry = term /
NBASE;
9190 term =
PRODSUM4(var1digits, 0, var2digits, 3) + carry;
9192 carry = term /
NBASE;
9195 term =
PRODSUM3(var1digits, 0, var2digits, 2) + carry;
9197 carry = term /
NBASE;
9200 term =
PRODSUM2(var1digits, 0, var2digits, 1) + carry;
9202 carry = term /
NBASE;
9205 term =
PRODSUM1(var1digits, 0, var2digits, 0) + carry;
9213 result->
ndigits = res_ndigits;
9214 result->
buf = res_buf;
9215 result->
digits = res_digits;
9216 result->
weight = res_weight;
9217 result->
sign = res_sign;
9245 int rscale,
bool round,
bool exact)
9247 int var1ndigits = var1->
ndigits;
9248 int var2ndigits = var2->
ndigits;
9252 int var1ndigitpairs;
9253 int var2ndigitpairs;
9254 int res_ndigitpairs;
9255 int div_ndigitpairs;
9275 if (var2ndigits == 0 || var2->
digits[0] == 0)
9277 (
errcode(ERRCODE_DIVISION_BY_ZERO),
9278 errmsg(
"division by zero")));
9287 if (var2ndigits <= 2)
9290 int idivisor_weight;
9292 idivisor = var2->
digits[0];
9293 idivisor_weight = var2->
weight;
9294 if (var2ndigits == 2)
9300 idivisor = -idivisor;
9302 div_var_int(var1, idivisor, idivisor_weight, result, rscale, round);
9306 if (var2ndigits <= 4)
9309 int idivisor_weight;
9311 idivisor = var2->
digits[0];
9312 idivisor_weight = var2->
weight;
9313 for (
i = 1;
i < var2ndigits;
i++)
9319 idivisor = -idivisor;
9321 div_var_int64(var1, idivisor, idivisor_weight, result, rscale, round);
9331 if (var1ndigits == 0)
9367 res_ndigits =
Max(res_ndigits, 1);
9382 var1ndigitpairs = (var1ndigits + 1) / 2;
9383 var2ndigitpairs = (var2ndigits + 1) / 2;
9384 res_ndigitpairs = (res_ndigits + 1) / 2;
9385 res_ndigits = 2 * res_ndigitpairs;
9402 div_ndigitpairs = res_ndigitpairs + var2ndigitpairs;
9403 var1ndigitpairs =
Min(var1ndigitpairs, div_ndigitpairs);
9407 div_ndigitpairs = res_ndigitpairs;
9408 var1ndigitpairs =
Min(var1ndigitpairs, div_ndigitpairs);
9409 var2ndigitpairs =
Min(var2ndigitpairs, div_ndigitpairs);
9420 dividend = (int64 *)
palloc((div_ndigitpairs + 1) *
sizeof(int64) +
9421 var2ndigitpairs *
sizeof(
int32));
9422 divisor = (
int32 *) (dividend + div_ndigitpairs + 1);
9425 for (
i = 0;
i < var1ndigitpairs - 1;
i++)
9428 if (2 *
i + 1 < var1ndigits)
9433 memset(dividend +
i + 1, 0, (div_ndigitpairs -
i) *
sizeof(int64));
9436 for (
i = 0;
i < var2ndigitpairs - 1;
i++)
9439 if (2 *
i + 1 < var2ndigits)
9457 fdivisor = (double) divisor[0] *
NBASE_SQR;
9458 if (var2ndigitpairs > 1)
9459 fdivisor += (double) divisor[1];
9460 fdivisorinverse = 1.0 / fdivisor;
9487 for (qi = 0; qi < res_ndigitpairs; qi++)
9490 fdividend = (double) dividend[qi] *
NBASE_SQR;
9491 fdividend += (double) dividend[qi + 1];
9494 fquotient = fdividend * fdivisorinverse;
9495 qdigit = (fquotient >= 0.0) ? ((
int32) fquotient) :
9496 (((
int32) fquotient) - 1);
9501 maxdiv +=
i64abs(qdigit);
9512 for (
i =
Min(qi + var2ndigitpairs - 2, div_ndigitpairs - 1);
i > qi;
i--)
9514 newdig = dividend[
i] + carry;
9517 carry = -((-newdig - 1) /
NBASE_SQR) - 1;
9527 dividend[
i] = newdig;
9529 dividend[qi] += carry;
9543 fdividend = (double) dividend[qi] *
NBASE_SQR;
9544 fdividend += (double) dividend[qi + 1];
9545 fquotient = fdividend * fdivisorinverse;
9546 qdigit = (fquotient >= 0.0) ? ((
int32) fquotient) :
9547 (((
int32) fquotient) - 1);
9549 maxdiv +=
i64abs(qdigit);
9569 int istop =
Min(var2ndigitpairs, div_ndigitpairs - qi);
9570 int64 *dividend_qi = ÷nd[qi];
9572 for (
i = 0;
i < istop;
i++)
9573 dividend_qi[
i] -= (int64) qdigit * divisor[
i];
9602 dividend[qi + 1] += dividend[qi] *
NBASE_SQR;
9604 dividend[qi] = qdigit;
9622 remainder = ÷nd[qi];
9624 for (
i = var2ndigitpairs - 2;
i >= 0;
i--)
9626 newdig = remainder[
i] + carry;
9629 carry = -((-newdig - 1) /
NBASE_SQR) - 1;
9639 remainder[
i + 1] = newdig;
9641 remainder[0] = carry;
9643 if (remainder[0] < 0)
9657 for (
i = var2ndigitpairs - 1;
i > 0;
i--)
9659 newdig = remainder[
i] + divisor[
i] + carry;
9667 remainder[
i] = newdig;
9671 remainder[0] += divisor[0] + carry;
9676 }
while (remainder[0] < 0);
9691 for (
i = 0;
i < var2ndigitpairs;
i++)
9693 if (remainder[
i] < divisor[
i])
9698 if (remainder[
i] > divisor[
i])
9706 for (
i = var2ndigitpairs - 1;
i > 0;
i--)
9708 newdig = remainder[
i] - divisor[
i] + carry;
9716 remainder[
i] = newdig;
9720 remainder[0] = remainder[0] - divisor[0] + carry;
9737 res_digits = result->
digits;
9739 for (
i = res_ndigitpairs - 1;
i >= 0;
i--)
9741 newdig = dividend[
i] + carry;
9744 carry = -((-newdig - 1) /
NBASE_SQR) - 1;
9764 result->
weight = res_weight;
9765 result->
sign = res_sign;
9789 int var_ndigits = var->
ndigits;
9801 errcode(ERRCODE_DIVISION_BY_ZERO),
9802 errmsg(
"division by zero"));
9805 if (var_ndigits == 0)
9821 res_weight = var->
weight - ival_weight;
9825 res_ndigits =
Max(res_ndigits, 1);
9832 res_digits = res_buf + 1;
9844 divisor = abs(ival);
9846 if (divisor <= UINT_MAX /
NBASE)
9851 for (
i = 0;
i < res_ndigits;
i++)
9853 carry = carry *
NBASE + (
i < var_ndigits ? var_digits[
i] : 0);
9855 carry = carry % divisor;
9863 for (
i = 0;
i < res_ndigits;
i++)
9865 carry = carry *
NBASE + (
i < var_ndigits ? var_digits[
i] : 0);
9867 carry = carry % divisor;
9873 result->
ndigits = res_ndigits;
9874 result->
buf = res_buf;
9875 result->
digits = res_digits;
9876 result->
weight = res_weight;
9877 result->
sign = res_sign;
9901 div_var_int64(
const NumericVar *var, int64 ival,
int ival_weight,
9905 int var_ndigits = var->
ndigits;
9917 errcode(ERRCODE_DIVISION_BY_ZERO),
9918 errmsg(
"division by zero"));
9921 if (var_ndigits == 0)
9937 res_weight = var->
weight - ival_weight;
9941 res_ndigits =
Max(res_ndigits, 1);
9948 res_digits = res_buf + 1;
9967 for (
i = 0;
i < res_ndigits;
i++)
9969 carry = carry *
NBASE + (
i < var_ndigits ? var_digits[
i] : 0);
9971 carry = carry % divisor;
9979 for (
i = 0;
i < res_ndigits;
i++)
9981 carry = carry *
NBASE + (
i < var_ndigits ? var_digits[
i] : 0);
9983 carry = carry % divisor;
9989 result->
ndigits = res_ndigits;
9990 result->
buf = res_buf;
9991 result->
digits = res_digits;
9992 result->
weight = res_weight;
9993 result->
sign = res_sign;
10037 firstdigit1 = var1->
digits[
i];
10038 if (firstdigit1 != 0)
10049 firstdigit2 = var2->
digits[
i];
10050 if (firstdigit2 != 0)
10061 qweight = weight1 - weight2;
10062 if (firstdigit1 <= firstdigit2)
10094 div_var(var1, var2, &tmp, 0,
false,
true);
10125 div_var(var1, var2, &q, 0,
false,
false);
10151 while (
cmp_abs(&r, var2) >= 0)
10260 result->
dscale = res_dscale;
10276 mod_var(&tmp_arg, result, &mod);
10283 result->
dscale = res_dscale;
10322 result->
dscale = rscale;
10332 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
10333 errmsg(
"cannot take square root of a negative number")));
10346 if (
arg->weight >= 0)
10347 res_weight =
arg->weight / 2;
10349 res_weight = -((-
arg->weight - 1) / 2 + 1);
10357 if (rscale + 1 >= 0)
10360 res_ndigits = res_weight + 1 - (-rscale - 1) /
DEC_DIGITS;
10361 res_ndigits =
Max(res_ndigits, 1);
10369 src_ndigits =
arg->weight + 1 + (res_ndigits - res_weight - 1) * 2;
10370 src_ndigits =
Max(src_ndigits, 1);
10440 while ((ndigits[step] = src_ndigits) > 4)
10443 blen = src_ndigits / 4;
10444 if (blen * 4 == src_ndigits &&
arg->digits[0] <
NBASE / 4)
10448 src_ndigits -= 2 * blen;
10460 arg_int64 =
arg->digits[0];
10461 for (src_idx = 1; src_idx < src_ndigits; src_idx++)
10463 arg_int64 *=
NBASE;
10464 if (src_idx < arg->ndigits)
10465 arg_int64 +=
arg->digits[src_idx];
10468 s_int64 = (int64) sqrt((
double) arg_int64);
10469 r_int64 = arg_int64 - s_int64 * s_int64;
10481 while (r_int64 < 0 || r_int64 > 2 * s_int64)
10483 s_int64 = (s_int64 + arg_int64 / s_int64) / 2;
10484 r_int64 = arg_int64 - s_int64 * s_int64;
10507 while (step >= 0 && (src_ndigits = ndigits[step]) <= 8)
10518 blen = (src_ndigits - src_idx) / 2;
10525 for (
i = 0;
i < blen;
i++, src_idx++)
10529 if (src_idx < arg->ndigits)
10530 a1 +=
arg->digits[src_idx];
10533 for (
i = 0;
i < blen;
i++, src_idx++)
10536 if (src_idx < arg->ndigits)
10537 a0 +=
arg->digits[src_idx];
10541 numer = r_int64 *
b +
a1;
10542 denom = 2 * s_int64;
10544 u = numer - q * denom;
10547 s_int64 = s_int64 *
b + q;
10548 r_int64 = u *
b + a0 - q * q;
10553 r_int64 += s_int64;
10555 r_int64 += s_int64;
10558 Assert(src_idx == src_ndigits);
10572 s_int128 = s_int64;
10573 r_int128 = r_int64;
10581 while (step >= 0 && (src_ndigits = ndigits[step]) <= 16)
10592 blen = (src_ndigits - src_idx) / 2;
10599 for (
i = 0;
i < blen;
i++, src_idx++)
10603 if (src_idx < arg->ndigits)
10604 a1 +=
arg->digits[src_idx];
10607 for (
i = 0;
i < blen;
i++, src_idx++)
10610 if (src_idx < arg->ndigits)
10611 a0 +=
arg->digits[src_idx];
10615 numer = r_int128 *
b +
a1;
10616 denom = 2 * s_int128;
10618 u = numer - q * denom;
10621 s_int128 = s_int128 *
b + q;
10622 r_int128 = u *
b + a0 - q * q;
10627 r_int128 += s_int128;
10629 r_int128 += s_int128;
10632 Assert(src_idx == src_ndigits);
10642 int128_to_numericvar(s_int128, &s_var);
10644 int128_to_numericvar(r_int128, &r_var);
10665 src_ndigits = ndigits[step];
10666 blen = (src_ndigits - src_idx) / 2;
10669 if (src_idx < arg->ndigits)
10671 tmp_len =
Min(blen,
arg->ndigits - src_idx);
10673 memcpy(a1_var.
digits,
arg->digits + src_idx,
10675 a1_var.
weight = blen - 1;
10687 if (src_idx < arg->ndigits)
10689 tmp_len =
Min(blen,
arg->ndigits - src_idx);
10691 memcpy(a0_var.
digits,
arg->digits + src_idx,
10693 a0_var.
weight = blen - 1;
10708 add_var(&q_var, &a1_var, &q_var);
10709 add_var(&s_var, &s_var, &u_var);
10714 add_var(&s_var, &q_var, &s_var);
10724 add_var(&u_var, &a0_var, &u_var);
10725 mul_var(&q_var, &q_var, &q_var, 0);
10730 sub_var(&u_var, &q_var, &r_var);
10734 add_var(&r_var, &s_var, &r_var);
10736 add_var(&r_var, &s_var, &r_var);
10742 if (
cmp_var(&u_var, &q_var) < 0)
10746 Assert(src_idx == src_ndigits);
10754 result->
weight = res_weight;
10806 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10807 errmsg(
"value overflows numeric format")));
10809 result->
dscale = rscale;
10814 dweight = (int) (
val * 0.434294481903252);
10823 if (fabs(
val) > 0.01)
10828 while (fabs(
val) > 0.01)
10834 local_rscale =
x.dscale + ndiv2;
10847 sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
10848 sig_digits =
Max(sig_digits, 0) + 8;
10850 local_rscale = sig_digits - 1;
10864 div_var_int(&elem, ni, 0, &elem, local_rscale,
true);
10868 add_var(result, &elem, result);
10870 mul_var(&elem, &
x, &elem, local_rscale);
10872 div_var_int(&elem, ni, 0, &elem, local_rscale,
true);
10880 while (ndiv2-- > 0)
10884 mul_var(result, result, result, local_rscale);
10932 ln_dweight =
x.weight *
DEC_DIGITS + (int) log10(
x.digits[0]);
10969 ln_var = log((
double)
digits) + dweight * 2.302585092994046;
10970 ln_dweight = (int) log10(fabs(
ln_var));
11003 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
11004 errmsg(
"cannot take logarithm of zero")));
11007 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
11008 errmsg(
"cannot take logarithm of a negative number")));
11036 local_rscale = rscale -
x.weight *
DEC_DIGITS / 2 + 8;
11043 local_rscale = rscale -
x.weight *
DEC_DIGITS / 2 + 8;
11064 local_rscale = rscale + (int) ((nsqrt + 1) * 0.301029995663981) + 8;
11068 div_var(result, &elem, result, local_rscale,
true,
false);
11070 mul_var(result, result, &
x, local_rscale);
11077 mul_var(&xx, &
x, &xx, local_rscale);
11078 div_var_int(&xx, ni, 0, &elem, local_rscale,
true);
11083 add_var(result, &elem, result);
11090 mul_var(result, &fact, result, rscale);
11111 int ln_base_dweight;
11112 int ln_num_dweight;
11113 int result_dweight;
11115 int ln_base_rscale;
11124 result_dweight = ln_num_dweight - ln_base_dweight;
11141 ln_base_rscale = rscale + result_dweight - ln_base_dweight + 8;
11144 ln_num_rscale = rscale + result_dweight - ln_num_dweight + 8;
11148 ln_var(base, &ln_base, ln_base_rscale);
11149 ln_var(num, &ln_num, ln_num_rscale);
11152 div_var(&ln_num, &ln_base, result, rscale,
true,
false);
11223 (
errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
11224 errmsg(
"a negative number raised to a non-integer power yields a complex result")));
11267 local_rscale = 8 - ln_dweight;
11270 ln_var(base, &ln_base, local_rscale);
11272 mul_var(&ln_base, exp, &ln_num, local_rscale);
11281 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
11282 errmsg(
"value overflows numeric format")));
11288 val *= 0.434294481903252;
11298 sig_digits = rscale + (int)
val;
11299 sig_digits =
Max(sig_digits, 0);
11302 local_rscale = sig_digits - ln_dweight + 8;
11307 ln_var(base, &ln_base, local_rscale);
11309 mul_var(&ln_base, exp, &ln_num, local_rscale);
11311 exp_var(&ln_num, result, rscale);
11367 f = exp * (log10(f) + p);
11375 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
11376 errmsg(
"value overflows numeric format")));
11391 rscale =
Max(rscale, exp_dscale);
11407 result->
dscale = rscale;
11417 mul_var(base, base, result, rscale);
11428 (
errcode(ERRCODE_DIVISION_BY_ZERO),
11429 errmsg(
"division by zero")));
11431 result->
dscale = rscale;
11449 sig_digits = 1 + rscale + (int) f;
11456 sig_digits += (int) log(fabs((
double) exp)) + 8;
11472 while ((mask >>= 1) > 0)
11480 local_rscale =
Min(local_rscale, 2 * base_prod.
dscale);
11483 mul_var(&base_prod, &base_prod, &base_prod, local_rscale);
11487 local_rscale = sig_digits -
11489 local_rscale =
Min(local_rscale,
11493 mul_var(&base_prod, result, result, local_rscale);
11510 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
11511 errmsg(
"value overflows numeric format")));
11540 result->
dscale = exp < 0 ? -exp : 0;
11552 result->
digits[0] *= 10;
11569 int rlen64_ndigits;
11579 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11580 errmsg(
"lower bound must be less than or equal to upper bound"));
11586 result->
dscale = rscale;
11605 for (
i = 0;
i < n;
i++)
11631 rlen64 = (uint64) rlen.
digits[0];
11632 rlen64_ndigits = 1;
11633 while (rlen64_ndigits < res_ndigits && rlen64_ndigits < 4)
11636 if (rlen64_ndigits < rlen.
ndigits)
11637 rlen64 += rlen.
digits[rlen64_ndigits];
11651 result->
dscale = rscale;
11652 res_digits = result->
digits;
11661 if (rlen64_ndigits == res_ndigits && pow10 != 1)
11666 for (
i = rlen64_ndigits - 1;
i >= 0;
i--)
11669 rand = rand /
NBASE;
11676 whole_ndigits = res_ndigits;
11681 i = rlen64_ndigits;
11682 while (
i < whole_ndigits - 3)
11687 rand = rand /
NBASE;
11689 rand = rand /
NBASE;
11691 rand = rand /
NBASE;
11696 while (
i < whole_ndigits)
11703 if (
i < res_ndigits)
11714 }
while (
cmp_var(result, &rlen) > 0);
11717 add_var(result, rmin, result);
11757 const NumericDigit *var2digits,
int var2ndigits,
int var2weight)
11764 while (var1weight > var2weight && i1 < var1ndigits)
11766 if (var1digits[i1++] != 0)
11770 while (var2weight > var1weight && i2 < var2ndigits)
11772 if (var2digits[i2++] != 0)
11779 if (var1weight == var2weight)
11781 while (i1 < var1ndigits && i2 < var2ndigits)
11783 int stat = var1digits[i1++] - var2digits[i2++];
11798 while (i1 < var1ndigits)
11800 if (var1digits[i1++] != 0)
11803 while (i2 < var2ndigits)
11805 if (var2digits[i2++] != 0)
11836 int var1ndigits = var1->
ndigits;
11837 int var2ndigits = var2->
ndigits;
11848 res_rscale =
Max(rscale1, rscale2);
11850 res_ndigits = res_rscale + res_weight + 1;
11851 if (res_ndigits <= 0)
11856 res_digits = res_buf + 1;
11858 i1 = res_rscale + var1->
weight + 1;
11859 i2 = res_rscale + var2->
weight + 1;
11860 for (
i = res_ndigits - 1;
i >= 0;
i--)
11864 if (i1 >= 0 && i1 < var1ndigits)
11865 carry += var1digits[i1];
11866 if (i2 >= 0 && i2 < var2ndigits)
11867 carry += var2digits[i2];
11869 if (carry >=
NBASE)
11871 res_digits[
i] = carry -
NBASE;
11876 res_digits[
i] = carry;
11884 result->
ndigits = res_ndigits;
11885 result->
buf = res_buf;
11886 result->
digits = res_digits;
11887 result->
weight = res_weight;
11888 result->
dscale = res_dscale;
11921 int var1ndigits = var1->
ndigits;
11922 int var2ndigits = var2->
ndigits;
11926 res_weight = var1->
weight;
11933 res_rscale =
Max(rscale1, rscale2);
11935 res_ndigits = res_rscale + res_weight + 1;
11936 if (res_ndigits <= 0)
11941 res_digits = res_buf + 1;
11943 i1 = res_rscale + var1->
weight + 1;
11944 i2 = res_rscale + var2->
weight + 1;
11945 for (
i = res_ndigits - 1;
i >= 0;
i--)
11949 if (i1 >= 0 && i1 < var1ndigits)
11950 borrow += var1digits[i1];
11951 if (i2 >= 0 && i2 < var2ndigits)
11952 borrow -= var2digits[i2];
11956 res_digits[
i] = borrow +
NBASE;
11961 res_digits[
i] = borrow;
11969 result->
ndigits = res_ndigits;
11970 result->
buf = res_buf;
11971 result->
digits = res_digits;
11972 result->
weight = res_weight;
11973 result->
dscale = res_dscale;
12017 if (ndigits < var->ndigits ||
12018 (ndigits == var->
ndigits && di > 0))
12022 #if DEC_DIGITS == 1
12034 #if DEC_DIGITS == 4
12036 #elif DEC_DIGITS == 2
12039 #error unsupported NBASE
12041 extra =
digits[--ndigits] % pow10;
12042 digits[ndigits] -= extra;
12044 if (extra >= pow10 / 2)
12046 pow10 +=
digits[ndigits];
12047 if (pow10 >=
NBASE)
12052 digits[ndigits] = pow10;
12060 carry +=
digits[--ndigits];
12061 if (carry >=
NBASE)
12068 digits[ndigits] = carry;
12117 if (ndigits <= var->ndigits)
12121 #if DEC_DIGITS == 1
12134 #if DEC_DIGITS == 4
12136 #elif DEC_DIGITS == 2
12139 #error unsupported NBASE
12141 extra =
digits[--ndigits] % pow10;
12142 digits[ndigits] -= extra;
12161 while (ndigits > 0 && *
digits == 0)
12169 while (ndigits > 0 &&
digits[ndigits - 1] == 0)
12214 int32 *accum_digits;
12243 val_ndigits =
val->ndigits;
12244 val_digits =
val->digits;
12247 for (val_i = 0; val_i < val_ndigits; val_i++)
12249 accum_digits[
i] += (
int32) val_digits[val_i];
12289 for (
i = ndigits - 1;
i >= 0;
i--)
12291 newdig = dig[
i] + carry;
12292 if (newdig >=
NBASE)
12294 carry = newdig /
NBASE;
12295 newdig -= carry *
NBASE;
12308 for (
i = ndigits - 1;
i >= 0;
i--)
12310 newdig = dig[
i] + carry;
12311 if (newdig >=
NBASE)
12313 carry = newdig /
NBASE;
12314 newdig -= carry *
NBASE;
12335 int old_weight = accum->
weight;
12336 int old_ndigits = accum->
ndigits;
12342 accum_weight = old_weight;
12343 accum_ndigits = old_ndigits;
12354 if (
val->weight >= accum_weight)
12356 accum_weight =
val->weight + 1;
12357 accum_ndigits = accum_ndigits + (accum_weight - old_weight);
12372 accum_rscale = accum_ndigits - accum_weight - 1;
12373 val_rscale =
val->ndigits -
val->weight - 1;
12374 if (val_rscale > accum_rscale)
12375 accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
12377 if (accum_ndigits != old_ndigits ||
12378 accum_weight != old_weight)
12380 int32 *new_pos_digits;
12381 int32 *new_neg_digits;
12384 weightdiff = accum_weight - old_weight;
12386 new_pos_digits =
palloc0(accum_ndigits *
sizeof(
int32));
12387 new_neg_digits =
palloc0(accum_ndigits *
sizeof(
int32));
12391 memcpy(&new_pos_digits[weightdiff], accum->
pos_digits,
12392 old_ndigits *
sizeof(
int32));
12395 memcpy(&new_neg_digits[weightdiff], accum->
neg_digits,
12396 old_ndigits *
sizeof(
int32));
12403 accum->
weight = accum_weight;
12404 accum->
ndigits = accum_ndigits;
12460 add_var(&pos_var, &neg_var, result);
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
#define PG_GETARG_ARRAYTYPE_P(n)
#define PG_RETURN_ARRAYTYPE_P(x)
#define ARR_OVERHEAD_NONULLS(ndims)
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Datum numeric_stddev_samp(PG_FUNCTION_ARGS)
static const NumericVar const_two
static void mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
static const NumericVar const_pinf
static char * get_str_from_var_sci(const NumericVar *var, int rscale)
#define NUMERIC_INF_SIGN_MASK
#define dump_numeric(s, n)
#define NUMERIC_CAN_BE_SHORT(scale, weight)
static void ceil_var(const NumericVar *var, NumericVar *result)
static void accum_sum_carry(NumericSumAccum *accum)
#define DatumGetNumericAbbrev(X)
#define NUMERIC_HDRSZ_SHORT
#define digitbuf_free(buf)
Datum numeric_poly_var_samp(PG_FUNCTION_ARGS)
static const NumericDigit const_zero_point_nine_data[1]
Datum numeric_accum(PG_FUNCTION_ARGS)
static void exp_var(const NumericVar *arg, NumericVar *result, int rscale)
Datum numeric_poly_serialize(PG_FUNCTION_ARGS)
Datum int2_accum_inv(PG_FUNCTION_ARGS)
static Datum numeric_abbrev_convert_var(const NumericVar *var, NumericSortSupport *nss)
Datum numeric_accum_inv(PG_FUNCTION_ARGS)
#define PRODSUM2(v1, i1, v2, i2)
Datum numeric_div(PG_FUNCTION_ARGS)
Datum numeric_sub(PG_FUNCTION_ARGS)
int32 numeric_maximum_size(int32 typmod)
static bool numeric_abbrev_abort(int memtupcount, SortSupport ssup)
Datum numeric_poly_var_pop(PG_FUNCTION_ARGS)
Datum numeric_avg_serialize(PG_FUNCTION_ARGS)
Datum hash_numeric(PG_FUNCTION_ARGS)
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
static bool numeric_is_integral(Numeric num)
static NumericAggState * makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
Datum numeric_var_pop(PG_FUNCTION_ARGS)
static int get_min_scale(NumericVar *var)
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
int32 numeric_int4_opt_error(Numeric num, bool *have_error)
static void power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
static bool is_valid_numeric_typmod(int32 typmod)
static void div_var_int(const NumericVar *var, int ival, int ival_weight, NumericVar *result, int rscale, bool round)
static int cmp_numerics(Numeric num1, Numeric num2)
Datum numeric_avg(PG_FUNCTION_ARGS)
static void numericvar_serialize(StringInfo buf, const NumericVar *var)
static NumericAggState * makeNumericAggStateCurrentContext(bool calcSumX2)
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
static void power_var_int(const NumericVar *base, int exp, int exp_dscale, NumericVar *result)
static void free_var(NumericVar *var)
#define NUMERIC_DSCALE(n)
Datum int2_sum(PG_FUNCTION_ARGS)
static int numeric_typmod_scale(int32 typmod)
char * numeric_normalize(Numeric num)
Datum numeric_poly_sum(PG_FUNCTION_ARGS)
Datum int2_accum(PG_FUNCTION_ARGS)
Datum float8_numeric(PG_FUNCTION_ARGS)
#define PRODSUM3(v1, i1, v2, i2)
static void trunc_var(NumericVar *var, int rscale)
Datum numeric_round(PG_FUNCTION_ARGS)
static void set_var_from_num(Numeric num, NumericVar *dest)
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *val)
Datum numeric_cmp(PG_FUNCTION_ARGS)
Datum int4_avg_combine(PG_FUNCTION_ARGS)
Datum width_bucket_numeric(PG_FUNCTION_ARGS)
Datum int8_sum(PG_FUNCTION_ARGS)
Datum int8_accum_inv(PG_FUNCTION_ARGS)
#define PRODSUM1(v1, i1, v2, i2)
Datum numerictypmodout(PG_FUNCTION_ARGS)
static Numeric duplicate_numeric(Numeric num)
#define makePolyNumAggState
Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
static int32 make_numeric_typmod(int precision, int scale)
Datum numeric_recv(PG_FUNCTION_ARGS)
Numeric numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Numeric int64_to_numeric(int64 val)
Datum numeric_trim_scale(PG_FUNCTION_ARGS)
Datum int2int4_sum(PG_FUNCTION_ARGS)
static Numeric numeric_stddev_internal(NumericAggState *state, bool variance, bool sample, bool *is_null)
Numeric random_numeric(pg_prng_state *state, Numeric rmin, Numeric rmax)
Datum float4_numeric(PG_FUNCTION_ARGS)
Datum numeric_poly_stddev_samp(PG_FUNCTION_ARGS)
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Datum numeric_int8(PG_FUNCTION_ARGS)
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Datum int4_numeric(PG_FUNCTION_ARGS)
Datum numeric_sum(PG_FUNCTION_ARGS)
Datum numeric_ge(PG_FUNCTION_ARGS)
static const NumericVar const_ninf
Datum numeric_uplus(PG_FUNCTION_ARGS)
Datum numeric_stddev_pop(PG_FUNCTION_ARGS)
Datum numeric_uminus(PG_FUNCTION_ARGS)
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
static Datum numeric_abbrev_convert(Datum original_datum, SortSupport ssup)
#define NUMERIC_IS_SPECIAL(n)
#define NA_TOTAL_COUNT(na)
Datum numeric_le(PG_FUNCTION_ARGS)
Datum numeric_ceil(PG_FUNCTION_ARGS)
Datum numeric_send(PG_FUNCTION_ARGS)
struct Int8TransTypeData Int8TransTypeData
static void gcd_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
static bool set_var_from_non_decimal_integer_str(const char *str, const char *cp, int sign, int base, NumericVar *dest, const char **endptr, Node *escontext)
Datum numerictypmodin(PG_FUNCTION_ARGS)
Datum in_range_numeric_numeric(PG_FUNCTION_ARGS)
Datum numeric_power(PG_FUNCTION_ARGS)
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
#define PRODSUM5(v1, i1, v2, i2)
static const NumericVar const_minus_one
static bool numericvar_to_int32(const NumericVar *var, int32 *result)
static int numeric_fast_cmp(Datum x, Datum y, SortSupport ssup)
static void floor_var(const NumericVar *var, NumericVar *result)
Datum numeric_int4(PG_FUNCTION_ARGS)
Datum numeric_gcd(PG_FUNCTION_ARGS)
static const NumericDigit const_one_data[1]
Datum numeric_poly_combine(PG_FUNCTION_ARGS)
static void init_var_from_num(Numeric num, NumericVar *dest)
static const NumericDigit const_two_data[1]
Datum numeric_fac(PG_FUNCTION_ARGS)
Datum numeric_serialize(PG_FUNCTION_ARGS)
#define NUMERIC_SHORT_WEIGHT_MASK
#define NUMERIC_IS_PINF(n)
static void zero_var(NumericVar *var)
Datum numeric_ne(PG_FUNCTION_ARGS)
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK
Datum numeric(PG_FUNCTION_ARGS)
Datum int4_avg_accum_inv(PG_FUNCTION_ARGS)
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
#define PRODSUM6(v1, i1, v2, i2)
Datum int8_avg_serialize(PG_FUNCTION_ARGS)
Datum numeric_exp(PG_FUNCTION_ARGS)
static int numeric_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
Datum numeric_log(PG_FUNCTION_ARGS)
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Datum numeric_scale(PG_FUNCTION_ARGS)
static Numeric make_result_opt_error(const NumericVar *var, bool *have_error)
Datum numeric_sqrt(PG_FUNCTION_ARGS)
static bool apply_typmod_special(Numeric num, int32 typmod, Node *escontext)
Datum int4_accum_inv(PG_FUNCTION_ARGS)
Datum numeric_poly_deserialize(PG_FUNCTION_ARGS)
#define NUMERIC_IS_INF(n)
Datum numeric_inc(PG_FUNCTION_ARGS)
static void random_var(pg_prng_state *state, const NumericVar *rmin, const NumericVar *rmax, NumericVar *result)
static char * get_str_from_var(const NumericVar *var)
Datum int8_avg_accum_inv(PG_FUNCTION_ARGS)
Datum int4_accum(PG_FUNCTION_ARGS)
Datum generate_series_numeric(PG_FUNCTION_ARGS)
#define NUMERIC_ABBREV_PINF
static void compute_bucket(Numeric operand, Numeric bound1, Numeric bound2, const NumericVar *count_var, NumericVar *result_var)
Datum numeric_out(PG_FUNCTION_ARGS)
Datum numeric_int2(PG_FUNCTION_ARGS)
int64 numeric_int8_opt_error(Numeric num, bool *have_error)
#define digitbuf_alloc(ndigits)
struct NumericAggState NumericAggState
static void sqrt_var(const NumericVar *arg, NumericVar *result, int rscale)
Datum numeric_trunc(PG_FUNCTION_ARGS)
static bool set_var_from_str(const char *str, const char *cp, NumericVar *dest, const char **endptr, Node *escontext)
static int estimate_ln_dweight(const NumericVar *var)
Datum numeric_lcm(PG_FUNCTION_ARGS)
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Datum numeric_div_trunc(PG_FUNCTION_ARGS)
static int numeric_sign_internal(Numeric num)
Datum int8_accum(PG_FUNCTION_ARGS)
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Datum numeric_avg_deserialize(PG_FUNCTION_ARGS)
static void mul_var_short(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
#define NUMERIC_ABBREV_NAN
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Datum numeric_poly_stddev_pop(PG_FUNCTION_ARGS)
static bool apply_typmod(NumericVar *var, int32 typmod, Node *escontext)
#define NUMERIC_IS_NINF(n)
Datum numeric_float8(PG_FUNCTION_ARGS)
Datum int2_avg_accum_inv(PG_FUNCTION_ARGS)
static const NumericVar const_zero_point_nine
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
static const NumericVar const_zero
Datum numeric_in(PG_FUNCTION_ARGS)
#define NUMERIC_ABBREV_NINF
static void accum_sum_combine(NumericSumAccum *accum, NumericSumAccum *accum2)
static void numericvar_deserialize(StringInfo buf, NumericVar *var)
static const int round_powers[4]
Datum numeric_mod(PG_FUNCTION_ARGS)
struct NumericSumAccum NumericSumAccum
bool numeric_is_nan(Numeric num)
#define NUMERIC_SIGN_MASK
Datum numeric_lt(PG_FUNCTION_ARGS)
#define NUMERIC_DSCALE_MASK
Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
Datum numeric_var_samp(PG_FUNCTION_ARGS)
Datum numeric_sortsupport(PG_FUNCTION_ARGS)
Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
struct NumericVar NumericVar
static void int64_to_numericvar(int64 val, NumericVar *var)
Datum int8_avg_deserialize(PG_FUNCTION_ARGS)
static bool numericvar_to_int64(const NumericVar *var, int64 *result)
Datum numeric_float8_no_overflow(PG_FUNCTION_ARGS)
Datum numeric_combine(PG_FUNCTION_ARGS)
Datum int8_avg_combine(PG_FUNCTION_ARGS)
Datum int8_avg_accum(PG_FUNCTION_ARGS)
#define PRODSUM4(v1, i1, v2, i2)
Datum numeric_sign(PG_FUNCTION_ARGS)
Datum int2_numeric(PG_FUNCTION_ARGS)
static int numeric_typmod_precision(int32 typmod)
#define NUMERIC_DSCALE_MAX
Datum int4_sum(PG_FUNCTION_ARGS)
static void alloc_var(NumericVar *var, int ndigits)
static int xdigit_value(char dig)
static Numeric make_result(const NumericVar *var)
static const NumericDigit const_one_point_one_data[2]
#define NUMERIC_SHORT_DSCALE_MASK
#define NumericAbbrevGetDatum(X)
#define NUMERIC_SHORT_DSCALE_SHIFT
static void accum_sum_reset(NumericSumAccum *accum)
#define NUMERIC_WEIGHT(n)
Datum numeric_add(PG_FUNCTION_ARGS)
static void round_var(NumericVar *var, int rscale)
Datum numeric_poly_avg(PG_FUNCTION_ARGS)
Datum numeric_eq(PG_FUNCTION_ARGS)
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round, bool exact)
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
static void log_var(const NumericVar *base, const NumericVar *num, NumericVar *result)
Datum numeric_ln(PG_FUNCTION_ARGS)
#define NUMERIC_SHORT_SIGN_MASK
static bool numericvar_to_uint64(const NumericVar *var, uint64 *result)
Datum numeric_abs(PG_FUNCTION_ARGS)
Datum int8_avg(PG_FUNCTION_ARGS)
Datum numeric_avg_accum(PG_FUNCTION_ARGS)
Datum numeric_larger(PG_FUNCTION_ARGS)
static const NumericVar const_one_point_one
Datum int4_avg_accum(PG_FUNCTION_ARGS)
static void strip_var(NumericVar *var)
static const NumericVar const_nan
#define NUMERIC_IS_SHORT(n)
Datum numeric_smaller(PG_FUNCTION_ARGS)
Datum numeric_gt(PG_FUNCTION_ARGS)
Datum numeric_deserialize(PG_FUNCTION_ARGS)
char * numeric_out_sci(Numeric num, int scale)
#define NUMERIC_DIGITS(num)
Datum int8_numeric(PG_FUNCTION_ARGS)
Datum numeric_mul(PG_FUNCTION_ARGS)
#define NUMERIC_IS_NAN(n)
#define makePolyNumAggStateCurrentContext
static const NumericDigit const_zero_data[1]
static double numericvar_to_double_no_overflow(const NumericVar *var)
Datum numeric_avg_combine(PG_FUNCTION_ARGS)
Datum numeric_min_scale(PG_FUNCTION_ARGS)
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
#define NUMERIC_WEIGHT_MAX
Datum numeric_float4(PG_FUNCTION_ARGS)
Datum numeric_support(PG_FUNCTION_ARGS)
static const NumericVar const_one
#define NUMERIC_NDIGITS(num)
NumericAggState PolyNumAggState
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
static int cmp_var_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, int var1sign, const NumericDigit *var2digits, int var2ndigits, int var2weight, int var2sign)
bool numeric_is_inf(Numeric num)
static void ln_var(const NumericVar *arg, NumericVar *result, int rscale)
Datum numeric_pg_lsn(PG_FUNCTION_ARGS)
static void power_ten_int(int exp, NumericVar *result)
static void accum_sum_copy(NumericSumAccum *dst, NumericSumAccum *src)
Datum numeric_floor(PG_FUNCTION_ARGS)
Datum int2_avg_accum(PG_FUNCTION_ARGS)
static void div_mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *quot, NumericVar *rem)
#define Assert(condition)
#define FLEXIBLE_ARRAY_MEMBER
#define StaticAssertDecl(condition, errmessage)
static void PGresult * res
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
Datum float8in(PG_FUNCTION_ARGS)
Datum float4in(PG_FUNCTION_ARGS)
static float4 get_float4_infinity(void)
static float4 get_float4_nan(void)
static float8 get_float8_infinity(void)
static float8 get_float8_nan(void)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_RETURN_UINT32(x)
#define PG_GETARG_BYTEA_PP(n)
#define PG_RETURN_BYTEA_P(x)
#define DirectFunctionCall2(func, arg1, arg2)
#define PG_GETARG_FLOAT8(n)
#define PG_RETURN_FLOAT8(x)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define PG_RETURN_INT64(x)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_INT64(n)
#define PG_DETOAST_DATUM_PACKED(datum)
#define PG_RETURN_UINT64(x)
#define PG_RETURN_INT16(x)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
#define PG_GETARG_FLOAT4(n)
#define PG_RETURN_POINTER(x)
#define PG_RETURN_FLOAT4(x)
#define PG_RETURN_BOOL(x)
#define PG_GETARG_INT16(n)
#define SRF_IS_FIRSTCALL()
#define SRF_PERCALL_SETUP()
#define SRF_RETURN_NEXT(_funcctx, _result)
#define SRF_FIRSTCALL_INIT()
#define SRF_RETURN_DONE(_funcctx)
static Datum hash_uint32(uint32 k)
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
static Datum hash_any(const unsigned char *k, int keylen)
static const FormData_pg_attribute a1
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
double estimateHyperLogLog(hyperLogLogState *cState)
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
static uint64 pg_abs_s64(int64 a)
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
static bool pg_add_u64_overflow(uint64 a, uint64 b, uint64 *result)
static bool pg_mul_u64_overflow(uint64 a, uint64 b, uint64 *result)
static uint32 pg_abs_s32(int32 a)
if(TABLE==NULL||TABLE_index==NULL)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
#define CHECK_FOR_INTERRUPTS()
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
int32 exprTypmod(const Node *expr)
Node * relabel_to_typmod(Node *expr, int32 typmod)
#define IsA(nodeptr, _type_)
#define NUMERIC_MIN_SCALE
#define NUMERIC_MAX_PRECISION
#define NUMERIC_MAX_RESULT_SCALE
#define NUMERIC_MAX_DISPLAY_SCALE
#define NUMERIC_MIN_SIG_DIGITS
static Numeric DatumGetNumeric(Datum X)
#define PG_GETARG_NUMERIC(n)
struct NumericData * Numeric
#define NUMERIC_MAX_SCALE
#define NUMERIC_MIN_DISPLAY_SCALE
#define PG_RETURN_NUMERIC(x)
static Datum NumericGetDatum(Numeric X)
static int list_length(const List *l)
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
static rewind_source * source
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
static uint32 DatumGetUInt32(Datum X)
static uint64 DatumGetUInt64(Datum X)
#define Int64GetDatumFast(X)
static char * DatumGetCString(Datum X)
static Datum UInt64GetDatum(uint64 X)
static Pointer DatumGetPointer(Datum X)
static Datum CStringGetDatum(const char *X)
static int32 DatumGetInt32(Datum X)
MemoryContextSwitchTo(old_ctx)
static int cmp(const chr *x, const chr *y, size_t len)
struct SortSupportData * SortSupport
StringInfoData * StringInfo
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
MemoryContext multi_call_memory_ctx
MemoryContext agg_context
union NumericChoice choice
NumericDigit n_data[FLEXIBLE_ARRAY_MEMBER]
NumericDigit n_data[FLEXIBLE_ARRAY_MEMBER]
hyperLogLogState abbr_card
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
struct NumericLong n_long
struct NumericShort n_short
#define VARSIZE_SHORT(PTR)
#define VARATT_IS_SHORT(PTR)
#define SET_VARSIZE(PTR, len)
#define VARDATA_SHORT(PTR)
#define VARSIZE_ANY_EXHDR(PTR)