Repository: tajo Updated Branches: refs/heads/master 76a5e077c -> e7051ef34
TAJO-827: SUM() overflow in the case of INT4. (Hyoungjun Kim via hyunsik) Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/e7051ef3 Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/e7051ef3 Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/e7051ef3 Branch: refs/heads/master Commit: e7051ef343d2a97bd8af4aa6b2f8a345b3567c76 Parents: 76a5e07 Author: Hyunsik Choi <[email protected]> Authored: Wed May 21 12:23:39 2014 +0900 Committer: Hyunsik Choi <[email protected]> Committed: Wed May 21 12:24:15 2014 +0900 ---------------------------------------------------------------------- CHANGES | 2 ++ .../tajo/engine/function/builtin/SumInt.java | 10 ++++----- .../tajo/engine/query/TestSelectQuery.java | 22 ++++++++++++++++++++ .../TestSelectQuery/testSumFloatOverflow.sql | 1 + .../TestSelectQuery/testSumIntOverflow.sql | 1 + .../TestSelectQuery/testSumFloatOverflow.result | 3 +++ .../TestSelectQuery/testSumIntOverflow.result | 3 +++ 7 files changed, 37 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 382bd09..26f3f9a 100644 --- a/CHANGES +++ b/CHANGES @@ -41,6 +41,8 @@ Release 0.9.0 - unreleased BUG FIXES + TAJO-827: SUM() overflow in the case of INT4. (Hyoungjun Kim via hyunsik) + TAJO-832: NPE occurs when Exception's message is null in Task. (Hyoungjun Kim via hyunsik) http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/SumInt.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/SumInt.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/SumInt.java index fff3a23..ce98128 100644 --- a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/SumInt.java +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/SumInt.java @@ -39,7 +39,7 @@ import org.apache.tajo.storage.Tuple; functionName = "sum", description = "the sum of a set of numbers", example = "> SELECT sum(expr);", - returnType = Type.INT4, + returnType = Type.INT8, paramTypes = {@ParamTypes(paramTypes = {Type.INT4})} ) public class SumInt extends AggFunction<Datum> { @@ -63,20 +63,20 @@ public class SumInt extends AggFunction<Datum> { @Override public Datum getPartialResult(FunctionContext ctx) { - return DatumFactory.createInt4(((SumIntContext) ctx).sum); + return DatumFactory.createInt8(((SumIntContext) ctx).sum); } @Override public DataType getPartialResultType() { - return CatalogUtil.newSimpleDataType(Type.INT4); + return CatalogUtil.newSimpleDataType(Type.INT8); } @Override public Datum terminate(FunctionContext ctx) { - return DatumFactory.createInt4(((SumIntContext) ctx).sum); + return DatumFactory.createInt8(((SumIntContext) ctx).sum); } private class SumIntContext implements FunctionContext { - int sum; + long sum; } } http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java index 815994b..96c51fe 100644 --- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java +++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java @@ -323,4 +323,26 @@ public class TestSelectQuery extends QueryTestCaseBase { executeString("DROP DATABASE \"TestSelectQuery\"").close(); } } + + @Test + public final void testSumIntOverflow() throws Exception { + // Test data's min value is 17 and number of rows is 5. + // 25264513 = 2147483647/17/5 + // result is 116,848,374,845 ==> int overflow + // select sum(cast(l_quantity * 25264513 as INT4)) from lineitem where l_quantity > 0; + ResultSet res = executeQuery(); + assertResultSet(res); + cleanupQuery(res); + } + + @Test + public final void testSumFloatOverflow() throws Exception { + // Test data's min value is 21168.23 and number of rows is 5. + // 3.21506374375027E33 = 3.40282346638529E38/21168/ 5 + // result is 6.838452478692677E38 ==> float4 overflow + // select sum(cast(L_EXTENDEDPRICE * 3.21506374375027E33 as FLOAT4)) from lineitem where l_quantity > 0; + ResultSet res = executeQuery(); + assertResultSet(res); + cleanupQuery(res); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/tajo-core/src/test/resources/queries/TestSelectQuery/testSumFloatOverflow.sql ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/queries/TestSelectQuery/testSumFloatOverflow.sql b/tajo-core/src/test/resources/queries/TestSelectQuery/testSumFloatOverflow.sql new file mode 100644 index 0000000..9ec941a --- /dev/null +++ b/tajo-core/src/test/resources/queries/TestSelectQuery/testSumFloatOverflow.sql @@ -0,0 +1 @@ +select sum(cast(L_EXTENDEDPRICE * 3.21506374375027E33 as FLOAT8)) from lineitem where l_quantity > 0 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/tajo-core/src/test/resources/queries/TestSelectQuery/testSumIntOverflow.sql ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/queries/TestSelectQuery/testSumIntOverflow.sql b/tajo-core/src/test/resources/queries/TestSelectQuery/testSumIntOverflow.sql new file mode 100644 index 0000000..96421eb --- /dev/null +++ b/tajo-core/src/test/resources/queries/TestSelectQuery/testSumIntOverflow.sql @@ -0,0 +1 @@ +select sum(cast(l_quantity * 25264513 as INT4)) from lineitem where l_quantity > 0 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/tajo-core/src/test/resources/results/TestSelectQuery/testSumFloatOverflow.result ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/results/TestSelectQuery/testSumFloatOverflow.result b/tajo-core/src/test/resources/results/TestSelectQuery/testSumFloatOverflow.result new file mode 100644 index 0000000..13b9ef4 --- /dev/null +++ b/tajo-core/src/test/resources/results/TestSelectQuery/testSumFloatOverflow.result @@ -0,0 +1,3 @@ +?sum +------------------------------- +6.838452478692677E38 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/e7051ef3/tajo-core/src/test/resources/results/TestSelectQuery/testSumIntOverflow.result ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/results/TestSelectQuery/testSumIntOverflow.result b/tajo-core/src/test/resources/results/TestSelectQuery/testSumIntOverflow.result new file mode 100644 index 0000000..cf2e0a8 --- /dev/null +++ b/tajo-core/src/test/resources/results/TestSelectQuery/testSumIntOverflow.result @@ -0,0 +1,3 @@ +?sum +------------------------------- +4673934905 \ No newline at end of file
