DRILL-588: Ignore leading zeroes while determining if digits will fit in a given precision
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/c40735ed Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/c40735ed Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/c40735ed Branch: refs/heads/master Commit: c40735ed0e582aba66d27b00a929bae173e8a6a9 Parents: 492ec59 Author: Mehant Baid <[email protected]> Authored: Mon May 19 11:32:59 2014 -0700 Committer: Mehant Baid <[email protected]> Committed: Mon May 19 11:32:59 2014 -0700 ---------------------------------------------------------------------- .../templates/Decimal/CastVarCharDecimal.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/c40735ed/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 e3eb973..8441298 100644 --- a/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java +++ b/exec/java-exec/src/main/codegen/templates/Decimal/CastVarCharDecimal.java @@ -83,6 +83,7 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { */ int integerStartIndex = readIndex; int integerEndIndex = endIndex; + boolean leadingDigitFound = false; int radix = 10; @@ -108,6 +109,13 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { byte[] buf = new byte[in.end - in.start]; in.buffer.getBytes(in.start, buf, 0, in.end - in.start); throw new org.apache.drill.common.exceptions.DrillRuntimeException(new String(buf, com.google.common.base.Charsets.UTF_8)); + } else if (leadingDigitFound == false) { + if (next == 0) { + // Ignore the leading zeroes while validating if input digits will fit within the given precision + integerStartIndex++; + } else { + leadingDigitFound = true; + } } out.value *= radix; out.value += next; @@ -215,7 +223,8 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { startIndex = readIndex; int radix = 10; - + boolean leadingDigitFound = false; + /* This is the first pass, we get the number of integer digits and based on the provided scale * we compute which index into the ByteBuf we start storing the integer part of the Decimal */ @@ -243,7 +252,13 @@ public class Cast${type.from}${type.to} implements DrillSimpleFunc { throw new NumberFormatException(new String(buf, com.google.common.base.Charsets.UTF_8)); } - integerDigits++; + if (leadingDigitFound == false && next != 0) { + leadingDigitFound = true; + } + + if (leadingDigitFound == true) { + integerDigits++; + } } }
