diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRound.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRound.java index 03ee334..02db90a 100644 --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRound.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRound.java @@ -18,24 +18,27 @@ package org.apache.hadoop.hive.ql.udf; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import java.math.BigDecimal; +import java.math.RoundingMode; + import org.apache.hadoop.hive.ql.exec.UDF; public class UDFRound implements UDF { - private static Log LOG = LogFactory.getLog(UDFRound.class.getName()); - public UDFRound() { } - public Long evaluate(Double i) { - if (i == null) { + public Long evaluate(Double n) { + if (n == null) + return null; + return evaluate(n, 0).longValue(); + } + + public Double evaluate(Double n, Integer i) { + if ((n == null) || (i == null)) return null; - } else { - return Long.valueOf(Math.round(i)); - } + return BigDecimal.valueOf(n).setScale(i, RoundingMode.HALF_UP).doubleValue(); } } diff --git ql/src/test/queries/clientpositive/udf_round.q ql/src/test/queries/clientpositive/udf_round.q new file mode 100644 index 0000000..b699d72 --- /dev/null +++ ql/src/test/queries/clientpositive/udf_round.q @@ -0,0 +1,40 @@ +SELECT round(null), round(null, 0), round(125, null) +FROM src LIMIT 1; + +SELECT + round(55555), round(55555, 0), round(55555, 1), round(55555, 2), round(55555, 3), + round(55555, -1), round(55555, -2), round(55555, -3), round(55555, -4), + round(55555, -5), round(55555, -6), round(55555, -7), round(55555, -8) +FROM src LIMIT 1; + +SELECT + round(125.315), round(125.315, 0), + round(125.315, 1), round(125.315, 2), round(125.315, 3), round(125.315, 4), + round(125.315, -1), round(125.315, -2), round(125.315, -3), round(125.315, -4), + round(-125.315), round(-125.315, 0), + round(-125.315, 1), round(-125.315, 2), round(-125.315, 3), round(-125.315, 4), + round(-125.315, -1), round(-125.315, -2), round(-125.315, -3), round(-125.315, -4) +FROM src LIMIT 1; + +SELECT + round(3.141592653589793, -15), round(3.141592653589793, -16), + round(3.141592653589793, -13), round(3.141592653589793, -14), + round(3.141592653589793, -11), round(3.141592653589793, -12), + round(3.141592653589793, -9), round(3.141592653589793, -10), + round(3.141592653589793, -7), round(3.141592653589793, -8), + round(3.141592653589793, -5), round(3.141592653589793, -6), + round(3.141592653589793, -3), round(3.141592653589793, -4), + round(3.141592653589793, -1), round(3.141592653589793, -2), + round(3.141592653589793, 0), round(3.141592653589793, 1), + round(3.141592653589793, 2), round(3.141592653589793, 3), + round(3.141592653589793, 4), round(3.141592653589793, 5), + round(3.141592653589793, 6), round(3.141592653589793, 7), + round(3.141592653589793, 8), round(3.141592653589793, 9), + round(3.141592653589793, 10), round(3.141592653589793, 11), + round(3.141592653589793, 12), round(3.141592653589793, 13), + round(3.141592653589793, 13), round(3.141592653589793, 14), + round(3.141592653589793, 15), round(3.141592653589793, 16) +FROM src LIMIT 1; + +SELECT round(1809242.3151111344, 9), round(-1809242.3151111344, 9) +FROM src LIMIT 1; diff --git ql/src/test/results/clientpositive/udf4.q.out ql/src/test/results/clientpositive/udf4.q.out index b39cec4..960c0df 100644 --- ql/src/test/results/clientpositive/udf4.q.out +++ ql/src/test/results/clientpositive/udf4.q.out @@ -56,4 +56,4 @@ STAGE PLANS: limit: -1 -1 2 -1 1 1 -2 1 2 -1 1 0.731057369148862 3 -3 3 -1 -2 +1 2 -2 1 1 -2 1 2 -1 1 0.731057369148862 3 -3 3 -1 -2 diff --git ql/src/test/results/clientpositive/udf_round.q.out ql/src/test/results/clientpositive/udf_round.q.out new file mode 100644 index 0000000..f2b1627 --- /dev/null +++ ql/src/test/results/clientpositive/udf_round.q.out @@ -0,0 +1,5 @@ +NULL NULL NULL +55555 55555.0 55555.0 55555.0 55555.0 55560.0 55600.0 56000.0 60000.0 100000.0 0.0 0.0 0.0 +125 125.0 125.3 125.32 125.315 125.315 130.0 100.0 0.0 0.0 -125 -125.0 -125.3 -125.32 -125.315 -125.315 -130.0 -100.0 0.0 0.0 +0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 3.1 3.14 3.142 3.1416 3.14159 3.141593 3.1415927 3.14159265 3.141592654 3.1415926536 3.14159265359 3.14159265359 3.1415926535898 3.1415926535898 3.14159265358979 3.141592653589793 3.141592653589793 +1809242.315111134 -1809242.315111134