DRILL-1074: Fix computation of scale for divide function.
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/405abb2e Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/405abb2e Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/405abb2e Branch: refs/heads/master Commit: 405abb2e3cb348c6089e77ff5c1314d5260b42a3 Parents: d5dbe2c Author: Mehant Baid <meha...@gmail.com> Authored: Mon Jun 30 18:39:20 2014 -0700 Committer: Aditya Kishore <adi...@maprtech.com> Committed: Wed Jul 2 23:11:39 2014 -0700 ---------------------------------------------------------------------- .../DecimalScalePrecisionDivideFunction.java | 20 ++++----- .../drill/jdbc/test/TestFunctionsQuery.java | 44 ++++++++++---------- 2 files changed, 32 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/405abb2e/common/src/main/java/org/apache/drill/common/util/DecimalScalePrecisionDivideFunction.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/drill/common/util/DecimalScalePrecisionDivideFunction.java b/common/src/main/java/org/apache/drill/common/util/DecimalScalePrecisionDivideFunction.java index e662371..c4b33e4 100644 --- a/common/src/main/java/org/apache/drill/common/util/DecimalScalePrecisionDivideFunction.java +++ b/common/src/main/java/org/apache/drill/common/util/DecimalScalePrecisionDivideFunction.java @@ -22,22 +22,18 @@ package org.apache.drill.common.util; * Here we compute the scale and precision of the output decimal data type * based on the input scale and precision. Since division operation can be * a multiplication operation we compute the scale to be the sum of the inputs. - * The precision is computed by getting the sum of integer digits of the input - * and adding it with scale. The scale is further expanded to occupy the remaining - * digits in the given precision range - * * Eg: Input1 : precision = 5, scale = 3 ==> max integer digits = 2 * Input2 : precision = 7, scale = 4 ==> max integer digits = 3 * - * Output: max integer digits ==> 2 + 3 = 5 + * Output: max integer digits ==> 2 (left integer digits) + 4 (right scale, when divide results in multiplication) * max scale ==> 3 + 4 = 7 * - * Minimum precision required ==> 5 + 7 = 12 + * Minimum precision required ==> 6 + 7 = 13 * - * Since our minimum precision required is 12, we will use DECIMAL18 as the output type + * Since our minimum precision required is 13, we will use DECIMAL18 as the output type * but since this is divide we will grant the remaining digits in DECIMAL18 to scale * so we have the following - * output scale ==> 7 + (18 - 12) = 13 + * output scale ==> 7 + (18 - 13) = 12 * output precision ==> 18 */ public class DecimalScalePrecisionDivideFunction extends DrillBaseComputeScalePrecision { @@ -51,11 +47,13 @@ public class DecimalScalePrecisionDivideFunction extends DrillBaseComputeScalePr // compute the output scale and precision here outputScale = leftScale + rightScale; - int integerDigits = (leftPrecision - leftScale) + (rightPrecision - rightScale); + int leftIntegerDigits = leftPrecision - leftScale; + int maxResultIntegerDigits = leftIntegerDigits + rightScale; + - outputPrecision = DecimalUtility.getPrecisionRange(outputScale + integerDigits); + outputPrecision = DecimalUtility.getPrecisionRange(outputScale + maxResultIntegerDigits); // Try and increase the scale if we have any room - outputScale = (outputPrecision - integerDigits >= 0) ? (outputPrecision - integerDigits) : 0; + outputScale = (outputPrecision - maxResultIntegerDigits >= 0) ? (outputPrecision - maxResultIntegerDigits) : 0; } } http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/405abb2e/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 1189ac5..9337f61 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 @@ -268,16 +268,16 @@ public class TestFunctionsQuery { 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" + "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" ); } @@ -401,16 +401,16 @@ public class TestFunctionsQuery { 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" + "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" ); } @@ -589,13 +589,15 @@ public class TestFunctionsQuery { @Test public void testCastDecimalDivide() throws Exception { String query = "select (cast('9' as decimal(9, 1)) / cast('2' as decimal(4, 1))) as DEC9_DIV, " + + "cast('999999999' as decimal(9,0)) / cast('0.000000000000000000000000001' as decimal(28,28)) as DEC38_DIV, " + "cast('123456789.123456789' as decimal(18, 9)) * cast('123456789.123456789' as decimal(18, 9)) as DEC18_MUL " + "from cp.`employee.json` where employee_id = 1"; JdbcAssert.withNoDefaultSchema() .sql(query) .returns( - "DEC9_DIV=4.5000000; " + + "DEC9_DIV=4.500000000; " + + "DEC38_DIV=999999999000000000000000000000000000.0; " + "DEC18_MUL=15241578780673678.515622620750190521\n"); }