tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hj...@apache.org
Subject [02/20] tajo git commit: TAJO-1156: Improve the comparison of timestamp and date types. (Jihun Kang via jihoon)
Date Tue, 25 Nov 2014 14:45:43 GMT
TAJO-1156: Improve the comparison of timestamp and date types. (Jihun Kang via jihoon)

Closes #237


Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/13af3d32
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/13af3d32
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/13af3d32

Branch: refs/heads/hbase_storage
Commit: 13af3d32febc1b2ece7a881d25ee650e50e1a64b
Parents: c193cfa
Author: Jihoon Son <jihoonson@apache.org>
Authored: Fri Nov 14 12:21:35 2014 +0900
Committer: Jihoon Son <jihoonson@apache.org>
Committed: Fri Nov 14 12:21:35 2014 +0900

----------------------------------------------------------------------
 .../java/org/apache/tajo/datum/DateDatum.java   | 20 ++++++++++----------
 .../org/apache/tajo/datum/TimestampDatum.java   | 11 ++++++++++-
 .../org/apache/tajo/util/datetime/TimeMeta.java | 14 +++++++++++++-
 .../org/apache/tajo/datum/TestDateDatum.java    | 17 +++++++++++++++--
 .../apache/tajo/datum/TestTimestampDatum.java   | 17 +++++++++++++++--
 5 files changed, 63 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/13af3d32/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
index b26ef84..3296d4f 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
@@ -241,16 +241,16 @@ public class DateDatum extends Datum {
   public int compareTo(Datum datum) {
     if (datum.type() == TajoDataTypes.Type.DATE) {
       DateDatum another = (DateDatum) datum;
-      int compareResult = (year < another.year) ? -1 : ((year == another.year) ? 0 : 1);
-      if (compareResult != 0) {
-        return compareResult;
-      }
-      compareResult = (monthOfYear < another.monthOfYear) ? -1 : ((monthOfYear == another.monthOfYear)
? 0 : 1);
-      if (compareResult != 0) {
-        return compareResult;
-      }
-
-      return (dayOfMonth < another.dayOfMonth) ? -1 : ((dayOfMonth == another.dayOfMonth)
? 0 : 1);
+      TimeMeta myMeta, otherMeta;
+      myMeta = toTimeMeta();
+      otherMeta = another.toTimeMeta();
+      return myMeta.compareTo(otherMeta);
+    } else if (datum.type() == TajoDataTypes.Type.TIMESTAMP) {
+      TimestampDatum another = (TimestampDatum) datum;
+      TimeMeta myMeta, otherMeta;
+      myMeta = toTimeMeta();
+      otherMeta = another.toTimeMeta();
+      return myMeta.compareTo(otherMeta);
     } else if (datum instanceof NullDatum || datum.isNull()) {
       return -1;
     } else {

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af3d32/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
index 62b0861..9ad5f2b 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
@@ -178,7 +178,16 @@ public class TimestampDatum extends Datum {
   public int compareTo(Datum datum) {
     if (datum.type() == TajoDataTypes.Type.TIMESTAMP) {
       TimestampDatum another = (TimestampDatum) datum;
-      return (timestamp < another.timestamp) ? -1 : ((timestamp > another.timestamp)
? 1 : 0);
+      TimeMeta myMeta, otherMeta;
+      myMeta = toTimeMeta();
+      otherMeta = another.toTimeMeta();
+      return myMeta.compareTo(otherMeta);
+    } else if (datum.type() == TajoDataTypes.Type.DATE) {
+      DateDatum another = (DateDatum) datum;
+      TimeMeta myMeta, otherMeta;
+      myMeta = toTimeMeta();
+      otherMeta = another.toTimeMeta();
+      return myMeta.compareTo(otherMeta);
     } else if (datum.isNull()) {
       return -1;
     } else {

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af3d32/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java b/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
index 25fe64a..19bd814 100644
--- a/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
+++ b/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
@@ -20,7 +20,7 @@ package org.apache.tajo.util.datetime;
 
 import org.apache.tajo.util.datetime.DateTimeConstants.DateStyle;
 
-public class TimeMeta {
+public class TimeMeta implements Comparable<TimeMeta> {
   public int      fsecs;    // 1/1,000,000 secs
   public int			secs;
   public int			minutes;
@@ -151,4 +151,16 @@ public class TimeMeta {
   public int getDayOfWeek() {
     return (DateTimeUtil.date2j(years, monthOfYear, dayOfMonth) + 1) % 7;
   }
+
+  @Override
+  public int compareTo(TimeMeta o) {
+    int result = 1;
+    if (o != null) {
+      long leftjulianTimestamp = DateTimeUtil.toJulianTimestamp(this);
+      long rightjulianTimestamp = DateTimeUtil.toJulianTimestamp(o);
+      
+      result = (int) Math.signum(leftjulianTimestamp - rightjulianTimestamp);
+    }
+    return result;
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af3d32/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
index f2ad261..41b4dca 100644
--- a/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
@@ -23,8 +23,8 @@ import org.apache.tajo.exception.InvalidCastException;
 import org.apache.tajo.json.CommonGsonHelper;
 import org.junit.Test;
 
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
 
 public class TestDateDatum {
   private static String DATE = "1980-04-01";
@@ -109,4 +109,17 @@ public class TestDateDatum {
     assertEquals(DatumFactory.createNullDatum(),d.equalsTo(DatumFactory.createNullDatum()));
     assertEquals(-1,d.compareTo(DatumFactory.createNullDatum()));
   }
+  
+  @Test
+  public void testCompareTo() {
+    DateDatum theday = DatumFactory.createDate("2014-11-12");
+    DateDatum thedaybefore = DatumFactory.createDate("2014-11-11");
+    
+    assertThat(theday.compareTo(thedaybefore) > 0, is(true));
+    assertThat(thedaybefore.compareTo(theday) > 0, is(false));
+    
+    TimestampDatum timestamp = DatumFactory.createTimestamp("2014-11-12 15:00:00.68");
+    
+    assertThat(timestamp.compareTo(theday) > 0, is(true));
+  }
 }

http://git-wip-us.apache.org/repos/asf/tajo/blob/13af3d32/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
index 818c296..5f27cfa 100644
--- a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
@@ -29,8 +29,8 @@ import org.junit.Test;
 import java.util.Calendar;
 import java.util.TimeZone;
 
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
 
 public class TestTimestampDatum {
   private static long javatime;
@@ -179,4 +179,17 @@ public class TestTimestampDatum {
    assertEquals(DatumFactory.createNullDatum(),d.equalsTo(DatumFactory.createNullDatum()));
    assertEquals(-1,d.compareTo(DatumFactory.createNullDatum()));
   }
+  
+  @Test
+  public void testCompareTo() {
+    TimestampDatum theday = DatumFactory.createTimestamp("2014-11-12 15:00:00.68");
+    TimestampDatum thedaybefore = DatumFactory.createTimestamp("2014-11-11 15:00:00.56");
+    
+    assertThat(theday.compareTo(thedaybefore) > 0, is(true));
+    assertThat(thedaybefore.compareTo(theday) > 0, is(false));
+    
+    DateDatum date = DatumFactory.createDate("2014-11-12");
+    
+    assertThat(theday.compareTo(date) > 0, is(true));
+  }
 }


Mime
View raw message