KYLIN-2422 NumberDictionary support for decimal with extra 0 after "."
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/24fa338e Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/24fa338e Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/24fa338e Branch: refs/heads/master-hbase0.98 Commit: 24fa338e608030e3762ce5a17340fcf1d82029b1 Parents: 5da5393 Author: shaofengshi <shaofeng...@apache.org> Authored: Sat Feb 4 14:16:11 2017 +0800 Committer: shaofengshi <shaofeng...@apache.org> Committed: Sat Feb 4 19:37:59 2017 +0800 ---------------------------------------------------------------------- .../org/apache/kylin/dict/NumberDictionary.java | 25 ++++++++++++++++++++ .../apache/kylin/dict/NumberDictionaryTest.java | 5 +++- 2 files changed, 29 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/24fa338e/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java ---------------------------------------------------------------------- diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java b/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java index c55937d..de28440 100644 --- a/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java +++ b/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java @@ -53,6 +53,7 @@ public class NumberDictionary<T> extends TrieDictionary<T> { return; } + if (len > buf.length) { throw new IllegalArgumentException("Too many digits for NumberDictionary: " + Bytes.toString(value, offset, len) + ". Internal buffer is only " + buf.length + " bytes"); } @@ -104,6 +105,30 @@ public class NumberDictionary<T> extends TrieDictionary<T> { bufOffset = start; bufLen = buf.length - start; + + // remove 0 in tail after the decimal point + if (decimalPoint != end) { + if (negative == true) { + while (buf[bufOffset + bufLen - 2] == '9' && (bufOffset + bufLen - 2 > decimalPoint)) { + bufLen--; + } + + if (bufOffset + bufLen - 2 == decimalPoint) { + bufLen--; + } + + buf[bufOffset + bufLen - 1] = ';'; + } else { + while (buf[bufOffset + bufLen - 1] == '0' && (bufOffset + bufLen - 1 > decimalPoint)) { + bufLen--; + } + + if (bufOffset + bufLen - 1 == decimalPoint) { + bufLen--; + } + + } + } } int decodeNumber(byte[] returnValue, int offset) { http://git-wip-us.apache.org/repos/asf/kylin/blob/24fa338e/core-dictionary/src/test/java/org/apache/kylin/dict/NumberDictionaryTest.java ---------------------------------------------------------------------- diff --git a/core-dictionary/src/test/java/org/apache/kylin/dict/NumberDictionaryTest.java b/core-dictionary/src/test/java/org/apache/kylin/dict/NumberDictionaryTest.java index 1c04745..36eedf5 100644 --- a/core-dictionary/src/test/java/org/apache/kylin/dict/NumberDictionaryTest.java +++ b/core-dictionary/src/test/java/org/apache/kylin/dict/NumberDictionaryTest.java @@ -91,11 +91,14 @@ public class NumberDictionaryTest extends LocalFileMetadataTestCase { checkCodec("-12345", "-9999999999999987654;"); checkCodec("-12345.123", "-9999999999999987654.876;"); checkCodec("0", "00000000000000000000"); - checkCodec("0.0", "00000000000000000000.0"); //test resolved jira-1800 checkCodec("-0.0045454354354354359999999999877218", "-9999999999999999999.9954545645645645640000000000122781;"); checkCodec("-0.009999999999877218", "-9999999999999999999.990000000000122781;"); checkCodec("12343434372493274.438403840384023840253554345345345345", "00012343434372493274.438403840384023840253554345345345345"); + assertEquals("00000000000000000052.57", encodeNumber("52.5700")); + assertEquals("00000000000000000000", encodeNumber("0.00")); + assertEquals("00000000000000000000", encodeNumber("0.0")); + assertEquals("-9999999999999987654.876;", encodeNumber("-12345.12300")); } private void checkCodec(String number, String code) {