DRILL-841: Using BigDecimal to round and truncate.
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/a1a61446 Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/a1a61446 Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/a1a61446 Branch: refs/heads/master Commit: a1a6144690d922deec0fb3138fa8814a00b0be44 Parents: 37eb656 Author: Sudheesh Katkam <skat...@maprtech.com> Authored: Mon Jun 16 12:52:47 2014 -0700 Committer: Jacques Nadeau <jacq...@apache.org> Committed: Fri Jun 27 10:54:45 2014 -0700 ---------------------------------------------------------------------- .../src/main/codegen/data/MathFunc.tdd | 28 +++++++++ .../main/codegen/templates/MathFunctions.java | 21 +++++++ .../drill/jdbc/test/TestFunctionsQuery.java | 62 ++++++++++++++++++++ 3 files changed, 111 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a1a61446/exec/java-exec/src/main/codegen/data/MathFunc.tdd ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/data/MathFunc.tdd b/exec/java-exec/src/main/codegen/data/MathFunc.tdd index 8180141..2720e82 100644 --- a/exec/java-exec/src/main/codegen/data/MathFunc.tdd +++ b/exec/java-exec/src/main/codegen/data/MathFunc.tdd @@ -137,6 +137,34 @@ unaryMathFunctions : [ ] } ], + otherMathFunctions : [ + {className: "Truncate", funcName: "trunc", mode: "DOWN", types: [ + {dataType: "Int" }, + {dataType: "BigInt" }, + {dataType: "Float4" }, + {dataType: "Float8" }, + {dataType: "SmallInt" }, + {dataType: "TinyInt" }, + {dataType: "UInt1" }, + {dataType: "UInt2" }, + {dataType: "UInt4" }, + {dataType: "UInt8" } + ] + }, + {className: "Roundd", funcName: "round", mode: "HALF_UP", types: [ + {dataType: "Int" }, + {dataType: "BigInt" }, + {dataType: "Float4" }, + {dataType: "Float8" }, + {dataType: "SmallInt" }, + {dataType: "TinyInt" }, + {dataType: "UInt1" }, + {dataType: "UInt2" }, + {dataType: "UInt4" }, + {dataType: "UInt8" } + ] + } + ], logBaseMathFunction : [ {className: "LogBase", funcName: "log", javaFunc : "java.lang.Math.log", outputType: "Float8", types: [ http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a1a61446/exec/java-exec/src/main/codegen/templates/MathFunctions.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/templates/MathFunctions.java b/exec/java-exec/src/main/codegen/templates/MathFunctions.java index d1ba6e8..c4298fb 100644 --- a/exec/java-exec/src/main/codegen/templates/MathFunctions.java +++ b/exec/java-exec/src/main/codegen/templates/MathFunctions.java @@ -98,6 +98,27 @@ public class GMathFunctions{ } </#list> </#list> + + <#list mathFunc.otherMathFunctions as func> + <#list func.types as type> + + @FunctionTemplate(name = "${func.funcName}", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL) + public static class ${func.className}${type.dataType} implements DrillSimpleFunc { + + @Param ${type.dataType}Holder input1; + @Param IntHolder input2; + @Output Float8Holder out; + + public void setup(RecordBatch b) { + } + + public void eval() { + java.math.BigDecimal temp = new java.math.BigDecimal(input1.value); + out.value = temp.setScale(input2.value, java.math.RoundingMode.${func.mode}).doubleValue(); + } + } + </#list> + </#list> } http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a1a61446/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java index 62efd37..1e5052a 100644 --- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java @@ -251,6 +251,37 @@ public class TestFunctionsQuery { } @Test + public void testTruncateWithParamFunction() throws Exception { + String query = String.format("SELECT " + + "trunc(1234.4567, 2) as T_1, " + + "trunc(-1234.4567, 2) as T_2, " + + "trunc(1234.4567, -2) as T_3, " + + "trunc(-1234.4567, -2) as T_4, " + + "trunc(1234, 4) as T_5, " + + "trunc(-1234, 4) as T_6, " + + "trunc(1234, -4) as T_7, " + + "trunc(-1234, -4) as T_8, " + + "trunc(8124674407369523212, 0) as T_9, " + + "trunc(81246744073695.395, 1) as T_10 " + + "FROM dfs.`%s/../../sample-data/region.parquet` limit 1", WORKING_PATH); + + JdbcAssert.withNoDefaultSchema() + .sql(query) + .returns( + "T_1=1234.45; " + + "T_2=-1234.45; " + + "T_3=1200.0; " + + "T_4=-1200.0; " + + "T_5=1234.0; " + + "T_6=-1234.0; " + + "T_7=0.0; " + + "T_8=0.0; " + + "T_9=8.1246744073695232E18; " + + "T_10=8.12467440736953E13\n" + ); + } + + @Test public void testRoundDecimalFunction() throws Exception { String query = String.format("SELECT " + "round(cast('1234.5567' as decimal(9, 5))) as DEC9_1, " + @@ -353,6 +384,37 @@ public class TestFunctionsQuery { } @Test + public void testRoundWithParamFunction() throws Exception { + String query = String.format("SELECT " + + "round(1234.4567, 2) as T_1, " + + "round(-1234.4567, 2) as T_2, " + + "round(1234.4567, -2) as T_3, " + + "round(-1234.4567, -2) as T_4, " + + "round(1234, 4) as T_5, " + + "round(-1234, 4) as T_6, " + + "round(1234, -4) as T_7, " + + "round(-1234, -4) as T_8, " + + "round(8124674407369523212, -4) as T_9, " + + "round(81246744073695.395, 1) as T_10 " + + "FROM dfs.`%s/../../sample-data/region.parquet` limit 1", WORKING_PATH); + + JdbcAssert.withNoDefaultSchema() + .sql(query) + .returns( + "T_1=1234.46; " + + "T_2=-1234.46; " + + "T_3=1200.0; " + + "T_4=-1200.0; " + + "T_5=1234.0; " + + "T_6=-1234.0; " + + "T_7=0.0; " + + "T_8=0.0; " + + "T_9=8.1246744073695201E18; " + + "T_10=8.12467440736954E13\n" + ); + } + + @Test public void testToCharFunction() throws Exception { String query = String.format("SELECT " + "to_char(1234.5567, '#,###.##') as FLOAT8_1, " +