Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rounding negative decimal places #1689

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
*/
package io.prestosql.operator.scalar;

import com.google.common.math.LongMath;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Shorts;
import com.google.common.primitives.SignedBytes;
import io.airlift.slice.Slice;
import io.prestosql.metadata.Signature;
import io.prestosql.metadata.SqlScalarFunction;
Expand All @@ -34,6 +37,7 @@
import org.apache.commons.math3.special.Erf;

import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.concurrent.ThreadLocalRandom;

import static io.airlift.slice.Slices.utf8Slice;
Expand All @@ -60,6 +64,7 @@
import static java.lang.Character.MIN_RADIX;
import static java.lang.Float.floatToRawIntBits;
import static java.lang.Float.intBitsToFloat;
import static java.lang.Math.toIntExact;
import static java.lang.String.format;

public final class MathFunctions
Expand Down Expand Up @@ -690,35 +695,64 @@ public static long round(@SqlType(StandardTypes.BIGINT) long num)
@SqlType(StandardTypes.TINYINT)
public static long roundTinyint(@SqlType(StandardTypes.TINYINT) long num, @SqlType(StandardTypes.INTEGER) long decimals)
{
// TODO implement support for `decimals < 0`
return num;
long rounded = roundLong(num, decimals);
try {
return SignedBytes.checkedCast(rounded);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + rounded, e);
}
}

@Description("round to nearest integer")
@ScalarFunction("round")
@SqlType(StandardTypes.SMALLINT)
public static long roundSmallint(@SqlType(StandardTypes.SMALLINT) long num, @SqlType(StandardTypes.INTEGER) long decimals)
{
// TODO implement support for `decimals < 0`
return num;
long rounded = roundLong(num, decimals);
try {
return Shorts.checkedCast(rounded);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for smallint: " + rounded, e);
}
}

@Description("round to nearest integer")
@ScalarFunction("round")
@SqlType(StandardTypes.INTEGER)
public static long roundInteger(@SqlType(StandardTypes.INTEGER) long num, @SqlType(StandardTypes.INTEGER) long decimals)
{
// TODO implement support for `decimals < 0`
return num;
long rounded = roundLong(num, decimals);
try {
return toIntExact(rounded);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for integer: " + rounded, e);
}
}

@Description("round to nearest integer")
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static long round(@SqlType(StandardTypes.BIGINT) long num, @SqlType(StandardTypes.INTEGER) long decimals)
{
// TODO implement support for `decimals < 0`
return num;
return roundLong(num, decimals);
}

private static long roundLong(long num, long decimals)
{
if (decimals >= 0) {
return num;
}

try {
long factor = LongMath.checkedPow(10, toIntExact(-decimals));
return Math.multiplyExact(LongMath.divide(num, factor, RoundingMode.HALF_UP), factor);
}
catch (ArithmeticException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "numerical overflow: " + num, e);
}
}

@Description("round to nearest integer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,54 @@ public void testRound()
assertFunction("round(REAL '-3.5001', 1)", REAL, -3.5f);
assertFunction("round(REAL '-3.99', 1)", REAL, -4.0f);

// ROUND negative DECIMAL
assertFunction("round(TINYINT '9', -1)", TINYINT, (byte) 10);
assertFunction("round(TINYINT '-9', -1)", TINYINT, (byte) -10);
assertFunction("round(TINYINT '5', -1)", TINYINT, (byte) 10);
assertFunction("round(TINYINT '-5', -1)", TINYINT, (byte) -10);
assertFunction("round(TINYINT '-14', -1)", TINYINT, (byte) -10);
assertFunction("round(TINYINT '12', -1)", TINYINT, (byte) 10);
assertFunction("round(TINYINT '18', -1)", TINYINT, (byte) 20);
assertFunction("round(TINYINT '18', -2)", TINYINT, (byte) 0);
assertFunction("round(TINYINT '18', -3)", TINYINT, (byte) 0);
jlabarbera11 marked this conversation as resolved.
Show resolved Hide resolved
assertFunction("round(TINYINT '127', -3)", TINYINT, (byte) 0);
assertFunction("round(TINYINT '-128', -3)", TINYINT, (byte) 0);
assertFunction("round(SMALLINT '99', -1)", SMALLINT, (short) 100);
assertFunction("round(SMALLINT '99', -2)", SMALLINT, (short) 100);
assertFunction("round(SMALLINT '99', -3)", SMALLINT, (short) 0);
assertFunction("round(SMALLINT '-99', -1)", SMALLINT, (short) -100);
assertFunction("round(SMALLINT '-99', -2)", SMALLINT, (short) -100);
assertFunction("round(SMALLINT '-99', -3)", SMALLINT, (short) 0);
jlabarbera11 marked this conversation as resolved.
Show resolved Hide resolved
assertFunction("round(SMALLINT '32767', -5)", SMALLINT, (short) 0);
assertFunction("round(SMALLINT '-32768', -5)", SMALLINT, (short) 0);
assertFunction("round(99, -1)", INTEGER, 100);
assertFunction("round(-99, -1)", INTEGER, -100);
assertFunction("round(99, INTEGER '-1')", INTEGER, 100);
assertFunction("round(-99, INTEGER '-1')", INTEGER, -100);
assertFunction("round(12355, -2)", INTEGER, 12400);
assertFunction("round(12345, -2)", INTEGER, 12300);
assertFunction("round(2147483647, -9)", INTEGER, 2000000000);
assertFunction("round(2147483647, -10)", INTEGER, 0);
assertFunction("round( 3999999999, -1)", BIGINT, 4000000000L);
assertFunction("round(-3999999999, -1)", BIGINT, -4000000000L);
assertFunction("round(9223372036854775807, -2)", BIGINT, 9223372036854775800L);
assertFunction("round(9223372036854775807, -18)", BIGINT, 9000000000000000000L);
jlabarbera11 marked this conversation as resolved.
Show resolved Hide resolved
assertFunction("round(-9223372036854775807, -18)", BIGINT, -9000000000000000000L);

assertInvalidFunction("round(TINYINT '127', -1)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(TINYINT '-128', -1)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(SMALLINT '32767', -1)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(SMALLINT '32767', -3)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(SMALLINT '-32768', -1)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(SMALLINT '-32768', -3)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(2147483647, -100)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(2147483647, -2147483648)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(9223372036854775807, -1)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(9223372036854775807, -3)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(9223372036854775807, -19)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(-9223372036854775807, -20)", NUMERIC_VALUE_OUT_OF_RANGE);
assertInvalidFunction("round(-9223372036854775807, -2147483648)", NUMERIC_VALUE_OUT_OF_RANGE);

// ROUND short DECIMAL -> short DECIMAL
assertFunction("round(DECIMAL '0')", createDecimalType(1, 0), SqlDecimal.of("0"));
assertFunction("round(DECIMAL '0.1')", createDecimalType(1, 0), SqlDecimal.of("0"));
Expand Down