[Feature] Support function roundBankers (#15154)

This commit is contained in:
HaveAnOrangeCat
2022-12-22 22:53:09 +08:00
committed by GitHub
parent 388df291af
commit df5969ab58
15 changed files with 352 additions and 10 deletions

View File

@ -222,6 +222,26 @@ BigIntVal MathFunctions::round(FunctionContext* ctx, const DoubleVal& v) {
return BigIntVal(static_cast<int64_t>(v.val + ((v.val < 0) ? -0.5 : 0.5)));
}
BigIntVal MathFunctions::round_bankers(FunctionContext* ctx, const DoubleVal& v) {
return BigIntVal(static_cast<int64_t>(round_bankers(ctx, v, IntVal(0)).val));
}
DoubleVal MathFunctions::round_bankers(doris_udf::FunctionContext* ctx, const DoubleVal& v,
const IntVal& d) {
const double TOLERANCE = 1e-10;
double shift = std::pow(10, d.val);
double t = v.val * shift;
double rounded = std::round(t);
if (int64_t(rounded) % 2 == 1) {
if (::abs(rounded - t) - 0.5 < TOLERANCE) {
rounded -= 1;
} else {
rounded += 1;
}
}
return DoubleVal(rounded / shift);
}
DoubleVal MathFunctions::round_up_to(FunctionContext* ctx, const DoubleVal& v,
const IntVal& scale) {
if (v.is_null || scale.is_null) {