DRILL-946: Fix minor bug in casting decimal to double

Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/734f9a8d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/734f9a8d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/734f9a8d

Branch: refs/heads/master
Commit: 734f9a8dc6f24c9121877fa841b4815dc80fe346
Parents: 60a429f
Author: Mehant Baid <[email protected]>
Authored: Wed Jun 11 19:10:06 2014 -0700
Committer: Jacques Nadeau <[email protected]>
Committed: Wed Jun 11 21:13:45 2014 -0700

----------------------------------------------------------------------
 .../main/codegen/templates/Decimal/CastDecimalFloat.java |  2 +-
 .../org/apache/drill/jdbc/test/TestFunctionsQuery.java   | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/734f9a8d/exec/java-exec/src/main/codegen/templates/Decimal/CastDecimalFloat.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/main/codegen/templates/Decimal/CastDecimalFloat.java 
b/exec/java-exec/src/main/codegen/templates/Decimal/CastDecimalFloat.java
index b259cb3..92cd576 100644
--- a/exec/java-exec/src/main/codegen/templates/Decimal/CastDecimalFloat.java
+++ b/exec/java-exec/src/main/codegen/templates/Decimal/CastDecimalFloat.java
@@ -52,7 +52,7 @@ public class Cast${type.from}${type.to} implements 
DrillSimpleFunc {
     public void eval() {
 
         // Divide the decimal with the scale to get the floating point value
-        out.value = (${type.javatype}) 
(org.apache.drill.common.util.DecimalUtility.adjustScaleDivide(in.value, (int) 
in.scale));
+        out.value = ((${type.javatype}) (in.value)) / 
(org.apache.drill.common.util.DecimalUtility.getPowerOfTen((int) in.scale));
     }
 }
 <#elseif type.major == "DecimalComplexFloat" || type.major == 
"DecimalComplexDouble"> <#-- Cast function template for conversion from 
Decimal9, Decimal18 to Float4 -->

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/734f9a8d/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 89224d4..60b8f82 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
@@ -512,4 +512,15 @@ public class TestFunctionsQuery {
             "TS_EXT=20.123; " +
             "TM_EXT=30.303\n");
   }
+
+  @Test
+  public void testCastDecimalDouble() throws Exception {
+    String query = "select cast((cast('1.0001' as decimal(18, 9))) as double) 
DECIMAL_DOUBLE_CAST " +
+        "from cp.`employee.json` where employee_id = 1";
+
+    JdbcAssert.withNoDefaultSchema()
+        .sql(query)
+        .returns(
+            "DECIMAL_DOUBLE_CAST=1.0001\n");
+  }
 }

Reply via email to