You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by li...@apache.org on 2015/03/21 01:28:46 UTC

[13/25] incubator-kylin git commit: KYLIN-630, define upper bound for date dictionary "9999-12-31". Make "10000-1-1" invalid.

KYLIN-630, define upper bound for date dictionary "9999-12-31". Make "10000-1-1" invalid.


Project: http://git-wip-us.apache.org/repos/asf/incubator-kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kylin/commit/0bc0f167
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kylin/tree/0bc0f167
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kylin/diff/0bc0f167

Branch: refs/heads/master
Commit: 0bc0f1679e8c3f08ab6588c43cce1e3e425a7991
Parents: 3979766
Author: Li, Yang <ya...@ebay.com>
Authored: Mon Mar 16 13:41:58 2015 +0800
Committer: Li, Yang <ya...@ebay.com>
Committed: Mon Mar 16 13:41:58 2015 +0800

----------------------------------------------------------------------
 .../apache/kylin/dict/DateStrDictionary.java    | 18 ++++++++-----
 .../kylin/dict/DateStrDictionaryTest.java       | 27 ++++++++++++++++++++
 2 files changed, 38 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/0bc0f167/dictionary/src/main/java/org/apache/kylin/dict/DateStrDictionary.java
----------------------------------------------------------------------
diff --git a/dictionary/src/main/java/org/apache/kylin/dict/DateStrDictionary.java b/dictionary/src/main/java/org/apache/kylin/dict/DateStrDictionary.java
index 0081751..7cace15 100644
--- a/dictionary/src/main/java/org/apache/kylin/dict/DateStrDictionary.java
+++ b/dictionary/src/main/java/org/apache/kylin/dict/DateStrDictionary.java
@@ -35,8 +35,8 @@ import org.apache.commons.lang.StringUtils;
 /**
  * A dictionary for date string (date only, no time).
  * 
- * Dates are numbered from 1970-1-1 -- 0 for 1970-1-1, 1 for 1-2, 2 for 1-3 and
- * so on. With 2 bytes, 65536 states, can express dates up to the year of 2149.
+ * Dates are numbered from 0000-1-1 -- 0 for "0000-1-1", 1 for "0000-1-2", 2 for "0000-1-3" and
+ * up to 3652426 for "9999-12-31".
  * 
  * Note the implementation is not thread-safe.
  * 
@@ -48,6 +48,8 @@ public class DateStrDictionary extends Dictionary<String> {
     static final String DEFAULT_DATETIME_PATTERN_WITHOUT_MILLISECONDS = "yyyy-MM-dd HH:mm:ss";
     static final String DEFAULT_DATETIME_PATTERN_WITH_MILLISECONDS = "yyyy-MM-dd HH:mm:ss.SSS";
 
+    static final int ID_9999_12_31 = 3652426; // assume 0 based
+
     static final private Map<String, ThreadLocal<SimpleDateFormat>> threadLocalMap = new ConcurrentHashMap<String, ThreadLocal<SimpleDateFormat>>();
 
     static SimpleDateFormat getDateFormat(String datePattern) {
@@ -102,6 +104,7 @@ public class DateStrDictionary extends Dictionary<String> {
 
     private String pattern;
     private int baseId;
+    private int maxId;
 
     public DateStrDictionary() {
         init(DEFAULT_DATE_PATTERN, 0);
@@ -114,6 +117,7 @@ public class DateStrDictionary extends Dictionary<String> {
     private void init(String datePattern, int baseId) {
         this.pattern = datePattern;
         this.baseId = baseId;
+        this.maxId = baseId + ID_9999_12_31;
     }
 
     @Override
@@ -123,7 +127,7 @@ public class DateStrDictionary extends Dictionary<String> {
 
     @Override
     public int getMaxId() {
-        return Integer.MAX_VALUE;
+        return maxId;
     }
 
     @Override
@@ -145,16 +149,16 @@ public class DateStrDictionary extends Dictionary<String> {
     final protected int getIdFromValueImpl(String value, int roundFlag) {
         Date date = stringToDate(value, pattern);
         int id = calcIdFromSeqNo(getNumOfDaysSince0000(date));
-        if (id < 0 || id >= 16777216)
-            throw new IllegalArgumentException("'" + value + "' encodes to '" + id + "' which is out of range of 3 bytes");
+        if (id < baseId || id > maxId)
+            throw new IllegalArgumentException("'" + value + "' encodes to '" + id + "' which is out of range [" + baseId + "," + maxId + "]");
 
         return id;
     }
 
     @Override
     final protected String getValueFromIdImpl(int id) {
-        if (id < baseId)
-            throw new IllegalArgumentException("ID '" + id + "' must not be less than base ID " + baseId);
+        if (id < baseId || id > maxId)
+            throw new IllegalArgumentException("ID '" + id + "' is out of range [" + baseId + "," + maxId + "]");
         Date d = getDateFromNumOfDaysSince0000(calcSeqNoFromId(id));
         return dateToString(d, pattern);
     }

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/0bc0f167/dictionary/src/test/java/org/apache/kylin/dict/DateStrDictionaryTest.java
----------------------------------------------------------------------
diff --git a/dictionary/src/test/java/org/apache/kylin/dict/DateStrDictionaryTest.java b/dictionary/src/test/java/org/apache/kylin/dict/DateStrDictionaryTest.java
index fa2ef67..f393ead 100644
--- a/dictionary/src/test/java/org/apache/kylin/dict/DateStrDictionaryTest.java
+++ b/dictionary/src/test/java/org/apache/kylin/dict/DateStrDictionaryTest.java
@@ -33,6 +33,33 @@ public class DateStrDictionaryTest {
     }
 
     @Test
+    public void testMinMaxId() {
+        assertEquals(0, dict.getIdFromValue("0000-01-01"));
+        assertEquals(DateStrDictionary.ID_9999_12_31, dict.getIdFromValue("9999-12-31"));
+
+        try {
+            dict.getValueFromId(-2); // -1 is id for NULL
+            fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException e) {
+            // good
+        }
+        
+        try {
+            dict.getValueFromId(DateStrDictionary.ID_9999_12_31 + 1);
+            fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException e) {
+            // good
+        }
+        
+        try {
+            dict.getIdFromValue("10000-1-1");
+            fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException e) {
+            // good
+        }
+    }
+
+    @Test
     public void testNull() {
         int nullId = dict.getIdFromValue(null);
         assertNull(dict.getValueFromId(nullId));