Skip to content

Commit cbd1a11

Browse files
hantangwangdtdcmeehan
authored andcommitted
Fix round for float value when input out of long range
1 parent 9fa4aee commit cbd1a11

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

presto-main/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ public static long roundFloat(@SqlType(StandardTypes.REAL) long num, @SqlType(St
11361136
catch (ArithmeticException e) {
11371137
// Use BigDecimal if the value is out of the range of long.
11381138
BigDecimal bigDecimal = new BigDecimal(numInFloat);
1139-
return floatToRawIntBits(bigDecimal.setScale((int) decimals, HALF_UP).longValue());
1139+
return floatToRawIntBits(bigDecimal.setScale((int) decimals, HALF_UP).floatValue());
11401140
}
11411141
}
11421142

presto-main/src/test/java/com/facebook/presto/operator/scalar/TestMathFunctions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,19 @@ public void testSecureRandom()
709709
assertInvalidFunction("secure_random(DECIMAL '5.0', DECIMAL '-5.0')", "upper bound must be greater than lower bound");
710710
}
711711

712+
@Test
713+
public void testRoundForUnderlyingValueOutOfRange()
714+
{
715+
// Round data of `REAL` type with underlying value out of long range should work well.
716+
// See issue https://github.com/prestodb/presto/issues/23763
717+
assertFunction("round(REAL '1.0E19', 1)", REAL, 1.0E19f);
718+
assertFunction("round(REAL '1.0E19', 10)", REAL, 1.0E19f);
719+
assertFunction("round(REAL '1.0E19', 100)", REAL, 1.0E19f);
720+
assertFunction("round(REAL '9999999999999999999.9', 1)", REAL, 9999999999999999999.9f);
721+
assertFunction("round(REAL '9999999999999999999.99', 10)", REAL, 9999999999999999999.99f);
722+
assertFunction("round(REAL '9999999999999999999.999', 100)", REAL, 9999999999999999999.999f);
723+
}
724+
712725
@Test
713726
public void testRound()
714727
{

0 commit comments

Comments
 (0)