DRILL-1246: Fix rounding when scale is zero while casting from varchar ->decimal28/decimal38
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/abaf5c49 Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/abaf5c49 Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/abaf5c49 Branch: refs/heads/master Commit: abaf5c494eb97643385f93496eac4d18cf285ec6 Parents: 66c055e Author: Mehant Baid <meha...@gmail.com> Authored: Thu Jul 31 18:22:34 2014 -0700 Committer: Jacques Nadeau <jacq...@apache.org> Committed: Wed Aug 6 16:44:21 2014 -0700 ---------------------------------------------------------------------- .../codegen/templates/Decimal/CastVarCharDecimal.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/abaf5c49/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java b/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java index 8a50bb6..54232ea 100644 --- a/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java +++ b/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java @@ -162,7 +162,7 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { } } -<#elseif type.major == "VarCharDecimalComplex"> <#-- Cast function template for conversion from VarChar to Decimal9, Decimal18 --> +<#elseif type.major == "VarCharDecimalComplex"> <#-- Cast function template for conversion from VarChar to Decimal28, Decimal38 --> <@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/gcast/Cast${type.from}${type.to}.java" /> <#include "/@includes/license.ftl" /> @@ -329,10 +329,10 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { } // Traverse and extract the fractional part - decimalBufferIndex = ${type.arraySize} - scaleRoundedUp; + decimalBufferIndex = (scaleRoundedUp > 0) ? (${type.arraySize} - scaleRoundedUp) : (${type.arraySize} - 1); ndigits = 0; - if (scaleIndex != -1 && out.scale > 0) { + if (scaleIndex != -1) { while (scaleIndex < scaleEndIndex) { // check if we have scanned MAX_DIGITS and we need to move to the next index @@ -378,8 +378,10 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { } } // Pad zeroes in the fractional part so that number of digits = MAX_DIGITS - int padding = (int) org.apache.drill.common.util.DecimalUtility.getPowerOfTen((int) (org.apache.drill.common.util.DecimalUtility.MAX_DIGITS - ndigits)); - out.setInteger(decimalBufferIndex, out.getInteger(decimalBufferIndex) * padding); + if (out.scale > 0) { + int padding = (int) org.apache.drill.common.util.DecimalUtility.getPowerOfTen((int) (org.apache.drill.common.util.DecimalUtility.MAX_DIGITS - ndigits)); + out.setInteger(decimalBufferIndex, out.getInteger(decimalBufferIndex) * padding); + } int carry = 0; do {