80#define MUL_GUARD_DIGITS 4
81#define DIV_GUARD_DIGITS 8
90#define MUL_GUARD_DIGITS 3
91#define DIV_GUARD_DIGITS 6
98#define HALF_NBASE 5000
100#define MUL_GUARD_DIGITS 2
101#define DIV_GUARD_DIGITS 4
106#define NBASE_SQR (NBASE * NBASE)
168#define NUMERIC_SIGN_MASK 0xC000
169#define NUMERIC_POS 0x0000
170#define NUMERIC_NEG 0x4000
171#define NUMERIC_SHORT 0x8000
172#define NUMERIC_SPECIAL 0xC000
174#define NUMERIC_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_SIGN_MASK)
175#define NUMERIC_IS_SHORT(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
176#define NUMERIC_IS_SPECIAL(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SPECIAL)
178#define NUMERIC_HDRSZ (VARHDRSZ + sizeof(uint16) + sizeof(int16))
179#define NUMERIC_HDRSZ_SHORT (VARHDRSZ + sizeof(uint16))
186#define NUMERIC_HEADER_IS_SHORT(n) (((n)->choice.n_header & 0x8000) != 0)
187#define NUMERIC_HEADER_SIZE(n) \
188 (VARHDRSZ + sizeof(uint16) + \
189 (NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
200#define NUMERIC_EXT_SIGN_MASK 0xF000
201#define NUMERIC_NAN 0xC000
202#define NUMERIC_PINF 0xD000
203#define NUMERIC_NINF 0xF000
204#define NUMERIC_INF_SIGN_MASK 0x2000
206#define NUMERIC_EXT_FLAGBITS(n) ((n)->choice.n_header & NUMERIC_EXT_SIGN_MASK)
207#define NUMERIC_IS_NAN(n) ((n)->choice.n_header == NUMERIC_NAN)
208#define NUMERIC_IS_PINF(n) ((n)->choice.n_header == NUMERIC_PINF)
209#define NUMERIC_IS_NINF(n) ((n)->choice.n_header == NUMERIC_NINF)
210#define NUMERIC_IS_INF(n) \
211 (((n)->choice.n_header & ~NUMERIC_INF_SIGN_MASK) == NUMERIC_PINF)
217#define NUMERIC_SHORT_SIGN_MASK 0x2000
218#define NUMERIC_SHORT_DSCALE_MASK 0x1F80
219#define NUMERIC_SHORT_DSCALE_SHIFT 7
220#define NUMERIC_SHORT_DSCALE_MAX \
221 (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)
222#define NUMERIC_SHORT_WEIGHT_SIGN_MASK 0x0040
223#define NUMERIC_SHORT_WEIGHT_MASK 0x003F
224#define NUMERIC_SHORT_WEIGHT_MAX NUMERIC_SHORT_WEIGHT_MASK
225#define NUMERIC_SHORT_WEIGHT_MIN (-(NUMERIC_SHORT_WEIGHT_MASK+1))
237#define NUMERIC_DSCALE_MASK 0x3FFF
238#define NUMERIC_DSCALE_MAX NUMERIC_DSCALE_MASK
240#define NUMERIC_SIGN(n) \
241 (NUMERIC_IS_SHORT(n) ? \
242 (((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
243 NUMERIC_NEG : NUMERIC_POS) : \
244 (NUMERIC_IS_SPECIAL(n) ? \
245 NUMERIC_EXT_FLAGBITS(n) : NUMERIC_FLAGBITS(n)))
246#define NUMERIC_DSCALE(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
247 ((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
248 >> NUMERIC_SHORT_DSCALE_SHIFT \
249 : ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
250#define NUMERIC_WEIGHT(n) (NUMERIC_HEADER_IS_SHORT((n)) ? \
251 (((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
252 ~NUMERIC_SHORT_WEIGHT_MASK : 0) \
253 | ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
254 : ((n)->choice.n_long.n_weight))
261#define NUMERIC_WEIGHT_MAX PG_INT16_MAX
405#define NumericAbbrevGetDatum(X) Int64GetDatum(X)
406#define DatumGetNumericAbbrev(X) DatumGetInt64(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)
474#define dump_numeric(s,n)
478#define digitbuf_alloc(ndigits) \
479 ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
480#define digitbuf_free(buf) \
486#define init_var(v) memset(v, 0, sizeof(NumericVar))
488#define NUMERIC_DIGITS(num) (NUMERIC_HEADER_IS_SHORT(num) ? \
489 (num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
490#define NUMERIC_NDIGITS(num) \
491 ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
492#define NUMERIC_CAN_BE_SHORT(scale,weight) \
493 ((scale) <= NUMERIC_SHORT_DSCALE_MAX && \
494 (weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
495 (weight) >= NUMERIC_SHORT_WEIGHT_MIN)
633 Node *escontext = fcinfo->context;
788 errmsg(
"invalid input syntax for type %s: \"%s\"",
871 return (
arg.ndigits == 0 ||
arg.ndigits <=
arg.weight + 1);
912 return ((typmod -
VARHDRSZ) >> 16) & 0xffff;
927 return (((typmod -
VARHDRSZ) & 0x7ff) ^ 1024) - 1024;
1040 while (
str[last] ==
'0')
1044 if (
str[last] ==
'.')
1048 str[last + 1] =
'\0';
1091 errmsg(
"invalid sign in external \"numeric\" value")));
1097 errmsg(
"invalid scale in external \"numeric\" value")));
1099 for (
i = 0;
i <
len;
i++)
1106 errmsg(
"invalid digit in external \"numeric\" value")));
1161 for (
i = 0;
i <
x.ndigits;
i++)
1283 new->choice.n_short.n_header =
1323 errmsg(
"NUMERIC precision %d must be between 1 and %d",
1328 errmsg(
"NUMERIC scale %d must be between %d and %d",
1337 errmsg(
"NUMERIC precision %d must be between 1 and %d",
1346 errmsg(
"invalid NUMERIC type modifier")));
1357 char *res = (
char *)
palloc(64);
1712 errmsg(
"start value cannot be NaN")));
1716 errmsg(
"start value cannot be infinity")));
1723 errmsg(
"stop value cannot be NaN")));
1727 errmsg(
"stop value cannot be infinity")));
1740 errmsg(
"step size cannot be NaN")));
1744 errmsg(
"step size cannot be infinity")));
1752 errmsg(
"step size cannot equal zero")));
1918 div_var(&res, &step, &res, 0,
false,
false);
1965 errmsg(
"count must be greater than zero")));
1972 errmsg(
"lower and upper bounds cannot be NaN")));
1977 errmsg(
"lower and upper bounds must be finite")));
1991 errmsg(
"lower bound cannot equal upper bound")));
2021 errmsg(
"integer out of range")));
2129 nss->input_count = 0;
2130 nss->estimating =
true;
2158 nss->input_count += 1;
2229 if (abbr_card > 100000.0)
2233 "numeric_abbrev: estimation ends at cardinality %f"
2235 abbr_card,
nss->input_count, memtupcount);
2236 nss->estimating =
false;
2253 "numeric_abbrev: aborting abbreviation at cardinality %f"
2254 " below threshold %f after " INT64_FORMAT " values (%d rows)",
2255 abbr_card,
nss->input_count / 10000.0 + 0.5,
2256 nss->input_count, memtupcount);
2262 "numeric_abbrev: cardinality %f"
2264 abbr_card,
nss->input_count, memtupcount);
2364 int weight = var->
weight;
2367 if (ndigits == 0 || weight < -44)
2371 else if (weight > 83)
2400 if (
nss->estimating)
2598 errmsg(
"invalid preceding or following size in window function")));
3226 if (
arg2.ndigits == 0 ||
arg2.digits[0] == 0)
3243 errmsg(
"division by zero"));
3278 errmsg(
"division by zero")));
3296 errmsg(
"division by zero")));
3394 if (
arg2.ndigits == 0 ||
arg2.digits[0] == 0)
3408 errmsg(
"division by zero"));
3579 if (
arg1.ndigits == 0 ||
arg2.ndigits == 0)
3615 errmsg(
"factorial of a negative number is undefined")));
3625 errmsg(
"value overflows numeric format")));
3632 for (num = num - 1; num > 1; num--)
3675 errmsg(
"cannot take square root of a negative number")));
3696#if DEC_DIGITS == ((DEC_DIGITS / 2) * 2)
3699 if (
arg.weight >= 0)
3706 rscale =
Max(rscale,
arg.dscale);
3766 val *= 0.434294481903252;
3773 rscale =
Max(rscale,
arg.dscale);
3813 errmsg(
"cannot take logarithm of a negative number")));
3825 rscale =
Max(rscale,
arg.dscale);
3870 errmsg(
"cannot take logarithm of a negative number")));
3875 errmsg(
"cannot take logarithm of zero")));
3963 errmsg(
"zero raised to a negative power is undefined")));
3967 errmsg(
"a negative number raised to a non-integer power yields a complex result")));
4055 if (
arg2.ndigits > 0 &&
arg2.ndigits ==
arg2.weight + 1 &&
4056 (
arg2.digits[
arg2.ndigits - 1] & 1))
4075 errmsg(
"zero raised to a negative power is undefined")));
4222 errmsg(
"lower bound cannot be NaN"));
4226 errmsg(
"lower bound cannot be infinity"));
4233 errmsg(
"upper bound cannot be NaN"));
4237 errmsg(
"upper bound cannot be infinity"));
4316 static const int pow10[] = {1, 10, 100, 1000};
4317#elif DEC_DIGITS == 2
4318 static const int pow10[] = {1, 10};
4319#elif DEC_DIGITS == 1
4320 static const int pow10[] = {1};
4322#error unsupported NBASE
4379 errmsg(
"cannot convert NaN to %s",
"integer")));
4383 errmsg(
"cannot convert infinity to %s",
"integer")));
4392 errmsg(
"integer out of range")));
4455 errmsg(
"cannot convert NaN to %s",
"bigint")));
4459 errmsg(
"cannot convert infinity to %s",
"bigint")));
4468 errmsg(
"bigint out of range")));
4510 errmsg(
"cannot convert NaN to %s",
"smallint")));
4514 errmsg(
"cannot convert infinity to %s",
"smallint")));
4523 errmsg(
"smallint out of range")));
4528 errmsg(
"smallint out of range")));
4594 (
Node *) fcinfo->context,
4694 (
Node *) fcinfo->context,
4719 errmsg(
"cannot convert NaN to %s",
"pg_lsn")));
4723 errmsg(
"cannot convert infinity to %s",
"pg_lsn")));
4732 errmsg(
"pg_lsn out of range")));
4767#define NA_TOTAL_COUNT(na) \
4768 ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)
4782 elog(
ERROR,
"aggregate function called in non-aggregate context");
4787 state->calcSumX2 = calcSumX2;
4788 state->agg_context = agg_context;
4805 state->calcSumX2 = calcSumX2;
4840 if (
X.dscale >
state->maxScale)
4842 state->maxScale =
X.dscale;
4843 state->maxScaleCount = 1;
4845 else if (
X.dscale ==
state->maxScale)
4846 state->maxScaleCount++;
4849 if (
state->calcSumX2)
4863 if (
state->calcSumX2)
4913 if (
X.dscale ==
state->maxScale)
4915 if (
state->maxScaleCount > 1 ||
state->maxScale == 0)
4921 state->maxScaleCount--;
4923 else if (
state->N == 1)
4926 state->maxScale = 0;
4927 state->maxScaleCount = 0;
4937 if (
state->calcSumX2)
4952 if (
state->calcSumX2)
4965 if (
state->calcSumX2)
5006 elog(
ERROR,
"aggregate function called in non-aggregate context");
5098 elog(
ERROR,
"aggregate function called in non-aggregate context");
5171 elog(
ERROR,
"aggregate function called in non-aggregate context");
5222 elog(
ERROR,
"aggregate function called in non-aggregate context");
5281 elog(
ERROR,
"aggregate function called in non-aggregate context");
5336 elog(
ERROR,
"aggregate function called in non-aggregate context");
5397 elog(
ERROR,
"numeric_accum_inv called with NULL state");
5446 elog(
ERROR,
"aggregate function called in non-aggregate context");
5451 state->calcSumX2 = calcSumX2;
5468 state->calcSumX2 = calcSumX2;
5479 if (
state->calcSumX2)
5492 if (
state->calcSumX2)
5562 elog(
ERROR,
"aggregate function called in non-aggregate context");
5630 elog(
ERROR,
"aggregate function called in non-aggregate context");
5663 elog(
ERROR,
"aggregate function called in non-aggregate context");
5723 elog(
ERROR,
"aggregate function called in non-aggregate context");
5767 elog(
ERROR,
"aggregate function called in non-aggregate context");
5797 elog(
ERROR,
"aggregate function called in non-aggregate context");
5834 elog(
ERROR,
"int2_accum_inv called with NULL state");
5851 elog(
ERROR,
"int4_accum_inv called with NULL state");
5868 elog(
ERROR,
"int8_accum_inv called with NULL state");
5874 elog(
ERROR,
"do_numeric_discard failed unexpectedly");
5889 elog(
ERROR,
"int8_avg_accum_inv called with NULL state");
5961 if (
state->NaNcount > 0)
5965 if (
state->pInfcount > 0 &&
state->nInfcount > 0)
5967 if (
state->pInfcount > 0)
5969 if (
state->nInfcount > 0)
5995 if (
state->NaNcount > 0)
5999 if (
state->pInfcount > 0 &&
state->nInfcount > 0)
6001 if (
state->pInfcount > 0)
6003 if (
state->nInfcount > 0)
6061 if (
state->NaNcount > 0 ||
state->pInfcount > 0 ||
state->nInfcount > 0)
6077 rscale =
vsumX.dscale * 2;
6435 elog(
ERROR,
"expected 2-element int8 array");
6463 elog(
ERROR,
"expected 2-element int8 array");
6481 elog(
ERROR,
"aggregate function called in non-aggregate context");
6488 elog(
ERROR,
"expected 2-element int8 array");
6492 elog(
ERROR,
"expected 2-element int8 array");
6522 elog(
ERROR,
"expected 2-element int8 array");
6550 elog(
ERROR,
"expected 2-element int8 array");
6569 elog(
ERROR,
"expected 2-element int8 array");
6594 elog(
ERROR,
"expected 2-element int8 array");
6650 for (
i = 0;
i < ndigits;
i++)
6830 else if (*
cp ==
'.')
6840 else if (*
cp ==
'_')
6856 if (*
cp ==
'e' || *
cp ==
'E')
6874 else if (*
cp ==
'-')
6888 exponent = exponent * 10 + (*
cp++ -
'0');
6892 else if (*
cp ==
'_')
6904 exponent = -exponent;
6907 dscale -= (
int) exponent;
6927 dest->weight = weight;
6928 dest->dscale = dscale;
6933 while (ndigits-- > 0)
6938#elif DEC_DIGITS == 2
6940#elif DEC_DIGITS == 1
6943#error unsupported NBASE
6961 errmsg(
"value overflows numeric format")));
6966 errmsg(
"invalid input syntax for type %s: \"%s\"",
6977 return dig >=
'0' &&
dig <=
'9' ?
dig -
'0' :
6978 dig >=
'a' &&
dig <=
'f' ?
dig -
'a' + 10 :
6979 dig >=
'A' &&
dig <=
'F' ?
dig -
'A' + 10 : -1;
7004 const char **endptr,
Node *escontext)
7050 else if (*
cp ==
'_')
7065 if (*
cp >=
'0' && *
cp <=
'7')
7084 tmp = tmp * 8 + (*
cp++ -
'0');
7087 else if (*
cp ==
'_')
7091 if (*
cp <
'0' || *
cp >
'7')
7102 if (*
cp >=
'0' && *
cp <=
'1')
7121 tmp = tmp * 2 + (*
cp++ -
'0');
7124 else if (*
cp ==
'_')
7128 if (*
cp <
'0' || *
cp >
'1')
7164 errmsg(
"value overflows numeric format")));
7169 errmsg(
"invalid input syntax for type %s: \"%s\"",
7234 if (
value->ndigits > 0)
7242 dest->digits =
newbuf + 1;
7301 for (d = 0; d <= var->
weight; d++)
7307 bool putit = (d > 0);
7326#elif DEC_DIGITS == 2
7329 if (d1 > 0 || d > 0)
7332#elif DEC_DIGITS == 1
7335#error unsupported NBASE
7363#elif DEC_DIGITS == 2
7368#elif DEC_DIGITS == 1
7371#error unsupported NBASE
7512 for (
i = 0;
i <
len;
i++)
7543 int weight = var->
weight;
7573 while (n > 0 && *
digits == 0)
7580 while (n > 0 &&
digits[n - 1] == 0)
7596 result->choice.n_short.n_header =
7608 result->choice.n_long.n_sign_dscale =
7610 result->choice.n_long.n_weight = weight;
7622 errmsg(
"value overflows numeric format")));
7697 else if (
dig < 1000)
7699#elif DEC_DIGITS == 2
7702#elif DEC_DIGITS == 1
7705#error unsupported NBASE
7710 errmsg(
"numeric field overflow"),
7711 errdetail(
"A field with precision %d, scale %d must round to an absolute value less than %s%d.",
7761 errmsg(
"numeric field overflow"),
7762 errdetail(
"A field with precision %d, scale %d cannot hold an infinite value.",
7763 precision,
scale)));
7803 Assert(weight >= 0 && ndigits <= weight + 1);
7813 for (
i = 1;
i <= weight;
i++)
7886 var->
weight = ndigits - 1;
7931 Assert(weight >= 0 && ndigits <= weight + 1);
7936 for (
i = 1;
i <= weight;
i++)
7994 var->
weight = ndigits - 1;
8011 if (*endptr !=
'\0')
8016 errmsg(
"invalid input syntax for type %s: \"%s\"",
8017 "double precision", tmp)));
8528 for (i2 = 0; i2 <
i2limit; i2++)
8536 for (i1 = i1 - 1; i1 >= 0; i1--)
8569 for (i2 = 0; i2 <
i2limit; i2++)
8664#define PRODSUM1(v1,i1,v2,i2) ((v1)[(i1)] * (v2)[(i2)])
8665#define PRODSUM2(v1,i1,v2,i2) (PRODSUM1(v1,i1,v2,i2) + (v1)[(i1)+1] * (v2)[(i2)-1])
8666#define PRODSUM3(v1,i1,v2,i2) (PRODSUM2(v1,i1,v2,i2) + (v1)[(i1)+2] * (v2)[(i2)-2])
8667#define PRODSUM4(v1,i1,v2,i2) (PRODSUM3(v1,i1,v2,i2) + (v1)[(i1)+3] * (v2)[(i2)-3])
8668#define PRODSUM5(v1,i1,v2,i2) (PRODSUM4(v1,i1,v2,i2) + (v1)[(i1)+4] * (v2)[(i2)-4])
8669#define PRODSUM6(v1,i1,v2,i2) (PRODSUM5(v1,i1,v2,i2) + (v1)[(i1)+5] * (v2)[(i2)-5])
8908 int rscale,
bool round,
bool exact)
8941 errmsg(
"division by zero")));
9465 errmsg(
"division by zero"));
9581 errmsg(
"division by zero"));
9698 for (
i = 0;
i <
var1->ndigits;
i++)
9710 for (
i = 0;
i <
var2->ndigits;
i++)
9730 rscale =
Max(rscale,
var1->dscale);
9731 rscale =
Max(rscale,
var2->dscale);
9919 if (
cmp == 0 ||
var2->ndigits == 0)
9940 if (
mod.ndigits == 0)
9996 errmsg(
"cannot take square root of a negative number")));
10009 if (
arg->weight >= 0)
10020 if (rscale + 1 >= 0)
10170 while (step >= 0 && (
src_ndigits = ndigits[step]) <= 8)
10244 while (step >= 0 && (
src_ndigits = ndigits[step]) <= 16)
10470 errmsg(
"value overflows numeric format")));
10472 result->dscale = rscale;
10543 while (
ndiv2-- > 0)
10667 errmsg(
"cannot take logarithm of zero")));
10671 errmsg(
"cannot take logarithm of a negative number")));
10843 if (
exp->ndigits == 0 ||
exp->ndigits <=
exp->weight + 1)
10884 if (
exp->ndigits > 0 &&
exp->ndigits >
exp->weight + 1)
10887 errmsg(
"a negative number raised to a non-integer power yields a complex result")));
10890 if (
exp->ndigits > 0 &&
exp->ndigits ==
exp->weight + 1 &&
10891 (
exp->digits[
exp->ndigits - 1] & 1))
10945 errmsg(
"value overflows numeric format")));
10951 val *= 0.434294481903252;
10956 rscale =
Max(rscale,
exp->dscale);
11039 errmsg(
"value overflows numeric format")));
11070 result->dscale = rscale;
11092 errmsg(
"division by zero")));
11094 result->dscale = rscale;
11135 while ((mask >>= 1) > 0)
11174 errmsg(
"value overflows numeric format")));
11215 result->digits[0] *= 10;
11243 errmsg(
"lower bound must be less than or equal to upper bound"));
11246 if (
rlen.ndigits == 0)
11249 result->dscale = rscale;
11268 for (
i = 0;
i < n;
i++)
11314 result->dscale = rscale;
11699#elif DEC_DIGITS == 2
11702#error unsupported NBASE
11705 digits[ndigits] -= extra;
11707 if (extra >=
pow10 / 2)
11799#elif DEC_DIGITS == 2
11802#error unsupported NBASE
11805 digits[ndigits] -= extra;
11824 while (ndigits > 0 && *
digits == 0)
11832 while (ndigits > 0 &&
digits[ndigits - 1] == 0)
11952 for (
i = ndigits - 1;
i >= 0;
i--)
11971 for (
i = ndigits - 1;
i >= 0;
i--)
#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
Datum numeric_poly_var_samp(PG_FUNCTION_ARGS)
static const NumericDigit const_zero_point_nine_data[1]
char * numeric_normalize(Numeric num)
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)
char * numeric_out_sci(Numeric num, int scale)
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)
static Numeric make_result_safe(const NumericVar *var, Node *escontext)
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
static bool numeric_is_integral(Numeric num)
static Int128AggState * makeInt128AggState(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)
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)
static NumericAggState * makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
Datum numeric_avg(PG_FUNCTION_ARGS)
static void numericvar_serialize(StringInfo buf, const NumericVar *var)
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)
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)
Datum generate_series_numeric_support(PG_FUNCTION_ARGS)
static Numeric duplicate_numeric(Numeric num)
static int32 make_numeric_typmod(int precision, int scale)
Datum numeric_recv(PG_FUNCTION_ARGS)
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)
static INT128 int128_deserialize(StringInfo buf)
Numeric numeric_mod_safe(Numeric num1, Numeric num2, Node *escontext)
#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)
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)
int32 numeric_int4_safe(Numeric num, Node *escontext)
Datum numerictypmodin(PG_FUNCTION_ARGS)
Datum in_range_numeric_numeric(PG_FUNCTION_ARGS)
static void do_int128_discard(Int128AggState *state, int64 newval)
static Numeric numeric_poly_stddev_internal(Int128AggState *state, bool variance, bool sample, bool *is_null)
Datum numeric_power(PG_FUNCTION_ARGS)
Numeric numeric_add_safe(Numeric num1, Numeric num2, Node *escontext)
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)
int64 numeric_int8_safe(Numeric num, Node *escontext)
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]
Numeric numeric_div_safe(Numeric num1, Numeric num2, Node *escontext)
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 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)
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)
Numeric numeric_sub_safe(Numeric num1, Numeric num2, Node *escontext)
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)
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 NumericAggState * makeNumericAggStateCurrentContext(bool calcSumX2)
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)
bool numeric_is_nan(Numeric num)
#define NUMERIC_SIGN_MASK
static void int128_serialize(StringInfo buf, INT128 val)
Datum numeric_lt(PG_FUNCTION_ARGS)
#define NUMERIC_DSCALE_MASK
Datum numeric_var_samp(PG_FUNCTION_ARGS)
Datum numeric_sortsupport(PG_FUNCTION_ARGS)
static void int64_to_numericvar(int64 val, NumericVar *var)
Datum int8_avg_deserialize(PG_FUNCTION_ARGS)
static void do_int128_accum(Int128AggState *state, int64 newval)
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 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)
Numeric numeric_mul_safe(Numeric num1, Numeric num2, Node *escontext)
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)
static Int128AggState * makeInt128AggStateCurrentContext(bool calcSumX2)
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)
static void int128_to_numericvar(INT128 val, NumericVar *var)
Datum numeric_smaller(PG_FUNCTION_ARGS)
Datum numeric_gt(PG_FUNCTION_ARGS)
Datum numeric_deserialize(PG_FUNCTION_ARGS)
#define NUMERIC_DIGITS(num)
Datum int8_numeric(PG_FUNCTION_ARGS)
Datum numeric_mul(PG_FUNCTION_ARGS)
#define NUMERIC_IS_NAN(n)
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)
#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)
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)
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
Node * estimate_expression_value(PlannerInfo *root, Node *node)
int errcode(int sqlerrcode)
#define ereturn(context, dummy_value,...)
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
struct SortSupportData * SortSupport
#define palloc_object(type)
#define palloc0_object(type)
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)
bool DirectInputFunctionCallSafe(PGFunction func, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
#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 INT128 make_int128(int64 hi, uint64 lo)
static bool int128_is_zero(INT128 x)
static void int128_sub_int64_mul_int64(INT128 *i128, int64 x, int64 y)
static int int128_sign(INT128 x)
static void int128_add_int128(INT128 *i128, INT128 v)
static void int128_sub_int64(INT128 *i128, int64 v)
static INT128 int64_to_int128(int64 v)
static void int128_add_int64(INT128 *i128, int64 v)
static void int128_div_mod_int32(INT128 *i128, int32 v, int32 *remainder)
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
#define PG_INT128_HI_INT64(i128)
#define PG_INT128_LO_UINT64(i128)
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)
#define digitbuf_free(buf)
#define digitbuf_alloc(size)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
#define CHECK_FOR_INTERRUPTS()
#define SOFT_ERROR_OCCURRED(escontext)
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
int32 exprTypmod(const Node *expr)
Node * relabel_to_typmod(Node *expr, int32 typmod)
static bool is_funcclause(const void *clause)
#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 MemoryContext MemoryContextSwitchTo(MemoryContext context)
static int list_length(const List *l)
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
static rewind_source * source
static char buf[DEFAULT_XLOG_SEG_SIZE]
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 Datum UInt64GetDatum(uint64 X)
static char * DatumGetCString(Datum X)
static Pointer DatumGetPointer(Datum X)
static int32 DatumGetInt32(Datum X)
static int cmp(const chr *x, const chr *y, size_t len)
struct StringInfoData * StringInfo
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
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
static bool VARATT_IS_SHORT(const void *PTR)
static Size VARSIZE_ANY_EXHDR(const void *PTR)
static Size VARSIZE(const void *PTR)
static char * VARDATA(const void *PTR)
static char * VARDATA_ANY(const void *PTR)
static char * VARDATA_SHORT(const void *PTR)
static void SET_VARSIZE(void *PTR, Size len)
static Size VARSIZE_SHORT(const void *PTR)