From 78280a8e08f212dd982407e38ec6333606886d4f Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Tue, 28 Jun 2016 16:46:42 +0800 Subject: [PATCH] KYLIN-1800 enlarge the max digits length after decimal point --- .../org/apache/kylin/dict/NumberDictionary.java | 2 +- .../apache/kylin/dict/NumberDictionaryTest.java | 33 ++++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) 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 d1e2fec..12efbd3 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 @@ -41,7 +41,7 @@ public class NumberDictionary extends TrieDictionary { NumberBytesCodec(int maxDigitsBeforeDecimalPoint) { this.maxDigitsBeforeDecimalPoint = maxDigitsBeforeDecimalPoint; - this.buf = new byte[maxDigitsBeforeDecimalPoint * 2]; + this.buf = new byte[maxDigitsBeforeDecimalPoint * 3]; this.bufOffset = 0; this.bufLen = 0; } 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 b359170..5588d2f 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 @@ -30,9 +30,13 @@ import java.util.Set; import org.apache.kylin.common.util.Bytes; import org.apache.kylin.common.util.Dictionary; +import org.apache.kylin.common.util.LocalFileMetadataTestCase; import org.apache.kylin.metadata.datatype.DataType; import org.junit.Test; - +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -40,18 +44,28 @@ import com.google.common.collect.Sets; * @author yangli9 * */ -public class NumberDictionaryTest { +public class NumberDictionaryTest extends LocalFileMetadataTestCase { NumberDictionary.NumberBytesCodec codec = new NumberDictionary.NumberBytesCodec(NumberDictionary.MAX_DIGITS_BEFORE_DECIMAL_POINT); Random rand = new Random(); + @Before + public void setup() throws Exception { + createTestMetadata(); + } + + @After + public void tearDown() { + cleanupTestMetadata(); + } + @Test public void testMinMax() { NumberDictionaryBuilder builder = new NumberDictionaryBuilder(new StringBytesConverter()); builder.addValue("" + Long.MAX_VALUE); builder.addValue("" + Long.MIN_VALUE); NumberDictionary dict = builder.build(0); - + int minId = dict.getIdFromValue("" + Long.MIN_VALUE); int maxId = dict.getIdFromValue("" + Long.MAX_VALUE); assertEquals(0, minId); @@ -60,15 +74,15 @@ public class NumberDictionaryTest { @SuppressWarnings("unchecked") @Test - public void testEmptyInput() throws IOException{ + public void testEmptyInput() throws IOException { String[] ints = new String[] { "", "0", "5", "100", "13" }; - Collection intBytes = Lists.newArrayListWithCapacity(ints.length); + Collection intBytes = Lists.newArrayListWithCapacity(ints.length); for (String s : ints) { intBytes.add((s == null) ? null : Bytes.toBytes(s)); } // check "" is treated as NULL, not a code of dictionary - Dictionary dict = DictionaryGenerator.buildDictionary(DataType.getType("integer"), new IterableDictionaryValueEnumerator(intBytes)); + Dictionary dict = DictionaryGenerator.buildDictionary(DataType.getType("integer"), new IterableDictionaryValueEnumerator(intBytes)); assertEquals(4, dict.getSize()); final int id = ((NumberDictionary) dict).getIdFromValue(""); @@ -83,6 +97,10 @@ public class NumberDictionaryTest { 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"); } private void checkCodec(String number, String code) { @@ -186,8 +204,7 @@ public class NumberDictionaryTest { if (digits2 > 0) { buf.append("."); for (int i = 0; i < digits2; i++) - buf.append("" + rand.nextInt(9) + 1); // BigDecimal thinks 4.5 - // != 4.50, my god! + buf.append("" + rand.nextInt(9) + 1); // BigDecimal thinks 4.5 != 4.50, my god! } return buf.toString(); } -- 2.7.4 (Apple Git-66)