hello-stephen commented on code in PR #71094:
URL: https://github.com/apache/doris/pull/71094#discussion_r4780830585
##########
be/src/exprs/function/round.h:
##########
@@ -476,6 +615,34 @@ struct Dispatcher {
FloatRoundingImpl<T, rounding_mode, scale_mode,
tie_breaking_mode>,
IntegerRoundingImpl<T, rounding_mode, scale_mode,
tie_breaking_mode>>>;
+ template <typename Input, typename Output>
+ static void apply_positive(const Input& in, Int32 precision, Output& out) {
+ if constexpr (is_float_or_double(T) || T == TYPE_TIMEV2) {
+ using Value = typename PrimitiveTypeTraits<T>::CppType;
+ if (precision > std::numeric_limits<Value>::max_exponent10) {
+ // Do not lose the precision by passing an infinite factor to
the scalar path.
+
FunctionRoundingImpl<ScaleMode::Positive>::apply_large_positive_scale(in,
precision,
+
out);
+ return;
+ }
+ }
+ auto scale = scale_factor(precision);
+ FunctionRoundingImpl<ScaleMode::Positive>::apply(in, scale, out);
+ }
+
+ static auto scale_factor(Int32 exponent) {
+ if constexpr (is_float_or_double(T) || T == TYPE_TIMEV2) {
+ // Keep the table lookup for common scales. 10^20 no longer fits
in uint64,
+ // so larger floating factors must not use int_exp10's saturation.
+ if (exponent <= 19) {
+ return static_cast<double>(int_exp10(exponent));
+ }
+ return std::pow(10.0, exponent);
Review Comment:
[P2] Retain the decimal factor through the rounding decision
For a DOUBLE column containing `1.797693134862315e308`, `ceil(x, -293)` now
raises arithmetic overflow. With the exact decimal factor, the ceiling multiple
is 3.548 ULPs below DOUBLE_MAX and converts back to the original input. The
DOUBLE factor from `pow(10, 293)` is slightly smaller than the decimal factor,
placing the input above that multiple. CEIL selects the next multiple, 0.783
ULPs above DOUBLE_MAX, and throws. The base returned the input. The same factor
error also makes `round(5e36, -36)` return `5.0000000000000004e36`. Keep
sufficient precision in the decimal factor to select the correct multiple
before converting the result to DOUBLE.
##########
be/src/exprs/function/round.h:
##########
@@ -349,8 +446,50 @@ struct FloatRoundingImpl {
using ColumnType = ColumnVector<Type>;
using Container = typename ColumnType::Container;
+ static long double decimal_unit(Int32 precision) {
+ // At 324 places for DOUBLE, or 46 for FLOAT, one decimal unit is less
+ // than half the smallest representable value. Rounding keeps the
input.
+ constexpr int unchanged_precision =
+ std::numeric_limits<T>::max_digits10 -
std::numeric_limits<T>::min_exponent10;
+ if (precision >= unchanged_precision) {
+ return 0;
+ }
+ // AArch64's compatibility powl narrows to DOUBLE, making 10^-323 equal
+ // to twice DOUBLE_DENORM_MIN and creating a false halfway case. Divide
+ // a long-double literal by an exact integer power instead: 1e-308L /
10^15.
+ constexpr int base_precision = std::numeric_limits<T>::max_exponent10;
+ constexpr long double base_unit = std::is_same_v<T, float> ? 1e-38L :
1e-308L;
+ DCHECK_GT(precision, base_precision);
+ return base_unit / int_exp10(precision - base_precision);
Review Comment:
[P2] Preserve decimal-unit precision on Apple Silicon
On Apple Silicon macOS, `long double` uses binary64, so this division still
represents `10^-323` as exactly twice the smallest positive DOUBLE. For that
smallest DOUBLE, the quotient becomes exactly `0.5` and `round(x, 323)` returns
twice the minimum. The exact quotient is about `0.494` and the expected result
is zero, as asserted by `FloatingPositiveRoundingScale`. Use decimal-unit
arithmetic with sufficient precision on targets where `long double` is
binary64. [Clang's Darwin arm64 type
definition](https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0/clang/lib/Basic/Targets/AArch64.cpp#L1696-L1707)
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/ComputePrecisionForRound.java:
##########
@@ -29,6 +30,20 @@
/** ComputePrecisionForRound */
public interface ComputePrecisionForRound extends ComputePrecision {
+ @Override
+ default void checkLegalityBeforeTypeCoercion() {
+ if (arity() == 2) {
+ Expression scale =
FoldConstantRuleOnFE.evaluateWithoutContext(getArgument(1));
+ // BE stores scale in int16. Check before folding can remove an
invalid argument:
+ // ROUND(NULL, -32769) becomes NULL, and ROUND(1, 32768) can
become 1.
+ if (scale instanceof IntegerLikeLiteral
Review Comment:
[P2] Validate the coerced scale before constant folding
`ROUND(CAST(1 AS DOUBLE), 32768.5)` passes this check because the scale is a
fractional literal. The function signature then coerces it to INT `32768`, and
FE folds the call to `1`. With a DOUBLE column in the first argument, BE
rejects the same coerced scale as out of range. A typed NULL first argument can
also hide the invalid scale. Check the coerced constant before folding the
function and retain the early check for wide integer literals.
##########
be/src/exprs/function/round.h:
##########
@@ -312,23 +333,99 @@ template <typename T, RoundingMode rounding_mode,
ScaleMode scale_mode,
class FloatRoundingComputation : public BaseFloatRoundingComputation<T,
tie_breaking_mode> {
using Base = BaseFloatRoundingComputation<T, tie_breaking_mode>;
+ static T round_negative(T value, T scale) {
+ if constexpr (rounding_mode != RoundingMode::Round) {
+ // For |value| < scale the directed result is zero or one unit.
+ // Return it directly: subtracting a tiny value from the unit can
+ // lose precision, and ceil(-1e-300, -308) must preserve negative
zero.
+ if (std::abs(value) < scale) {
+ if constexpr (rounding_mode == RoundingMode::Floor) {
+ return value < 0 ? -scale : std::copysign(T(0), value);
+ } else if constexpr (rounding_mode == RoundingMode::Ceil) {
+ return value > 0 ? scale : std::copysign(T(0), value);
+ } else {
+ return std::copysign(T(0), value);
+ }
+ }
+ }
+ // Dividing 3.323069989462296e35 by 1e20 creates a false .5 tie;
+ // even long double loses the side of the tie for
3.3230699894625745e35.
+ // remainder selects the nearest even multiple without rounding a
quotient.
+ auto remainder = std::remainder(value, scale);
+ if constexpr (rounding_mode == RoundingMode::Round) {
+ if constexpr (tie_breaking_mode == TieBreakingMode::Auto) {
+ // A genuine tie such as 2.5e20 at scale -20 rounds away from
zero.
+ if (std::abs(remainder) == scale / 2) {
+ remainder = -std::copysign(remainder, value);
+ }
+ }
+ } else {
+ const bool down = rounding_mode == RoundingMode::Floor ||
+ (rounding_mode == RoundingMode::Trunc && value >
0);
+ const bool up = rounding_mode == RoundingMode::Ceil ||
+ (rounding_mode == RoundingMode::Trunc && value <
0);
+ // Move from the nearest multiple to the directed one when
necessary.
+ // Here |value| >= scale, so the adjusted remainder (smaller than
scale)
+ // is still exact: both inputs are multiples of the scale's binary
ULP.
+ if ((down && remainder < 0) || (up && remainder > 0)) {
+ remainder -= std::copysign(scale, remainder);
+ }
+ }
+ // Subtract once, rounding only the final result. Preserve signed zero.
+ return std::copysign(value - remainder, value);
+ }
+
public:
static inline void compute(const T* __restrict in, const typename
Base::VectorType& scale,
T* __restrict out) {
auto val = Base::load(in);
-
- if (scale_mode == ScaleMode::Positive) {
+ if (!std::isfinite(val)) {
+ Base::store(out, val);
+ return;
+ }
+ if constexpr (scale_mode == ScaleMode::Positive) {
+ // Check the unit before multiplication loses precision. For
example,
+ // 0.00018014398509481986 * 1e20 rounds to exactly 2^54, bypassing
a strict
+ // product cutoff although the unit is below half the input's
inward spacing.
+ const auto half_ulp = std::abs(val - std::nextafter(val, T(0))) /
2;
+ if (!std::isfinite(scale) || T(1) / scale < half_ulp) {
+ Base::store(out, val);
+ return;
+ }
val = Base::multiply(val, scale);
Review Comment:
[P2] Resolve positive-scale boundaries before narrowing
For a DOUBLE column containing `3.051757812500016e-05`, the half-ULP guard
falls through at precision 20. The exact scaled product is
`3051757812500016.263032...`, but the DOUBLE multiplication rounds it to
`3051757812500016.5`. ROUND then returns `3.051757812500017e-05`, one ULP above
the result from exact rounding, FE folding, and the base. Widening the quotient
alone can still lose the side of the boundary. The x86 extended-precision path
makes `ceil(4.8929891602228e-296, 310)` advance a decimal unit even though its
exact quotient is just below an integer. Preserve enough precision to decide
the rounding direction and cover both positive-scale branches with exact
comparisons.
##########
be/src/exprs/function/round.h:
##########
@@ -312,23 +333,99 @@ template <typename T, RoundingMode rounding_mode,
ScaleMode scale_mode,
class FloatRoundingComputation : public BaseFloatRoundingComputation<T,
tie_breaking_mode> {
using Base = BaseFloatRoundingComputation<T, tie_breaking_mode>;
+ static T round_negative(T value, T scale) {
+ if constexpr (rounding_mode != RoundingMode::Round) {
+ // For |value| < scale the directed result is zero or one unit.
+ // Return it directly: subtracting a tiny value from the unit can
+ // lose precision, and ceil(-1e-300, -308) must preserve negative
zero.
+ if (std::abs(value) < scale) {
+ if constexpr (rounding_mode == RoundingMode::Floor) {
+ return value < 0 ? -scale : std::copysign(T(0), value);
+ } else if constexpr (rounding_mode == RoundingMode::Ceil) {
+ return value > 0 ? scale : std::copysign(T(0), value);
+ } else {
+ return std::copysign(T(0), value);
+ }
+ }
+ }
+ // Dividing 3.323069989462296e35 by 1e20 creates a false .5 tie;
+ // even long double loses the side of the tie for
3.3230699894625745e35.
+ // remainder selects the nearest even multiple without rounding a
quotient.
+ auto remainder = std::remainder(value, scale);
Review Comment:
[P2] Keep folded and stored DOUBLE results consistent
With default FE folding, `floor(CAST('1e35' AS DOUBLE), -20)` returns
`1e35`. `NumericArithmetic.floor` treats the DOUBLE's decimal rendering as an
exact multiple of `10^20`. Evaluating the same stored DOUBLE in BE now returns
`9.99999999999999e34`, five ULPs lower, because the exact binary input is
slightly below that decimal multiple. Literal and column results agreed on the
base. This can change which rows match a predicate comparing the two forms.
Make FE folding and BE execution use the same floating rounding semantics and
cover the literal and column forms in the same regression.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]