tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hyun...@apache.org
Subject git commit: TAJO-61: Implement Time Datum Type. (DaeMyung Kang via hyunsik)
Date Thu, 12 Dec 2013 01:13:30 GMT
Updated Branches:
  refs/heads/master 0ee6b2432 -> 2ff0eeacc


TAJO-61: Implement Time Datum Type. (DaeMyung Kang via hyunsik)


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

Branch: refs/heads/master
Commit: 2ff0eeaccb1ff37c5f54d1ac8aafedda7bdf6855
Parents: 0ee6b24
Author: Hyunsik Choi <hyunsik@apache.org>
Authored: Thu Dec 12 10:13:13 2013 +0900
Committer: Hyunsik Choi <hyunsik@apache.org>
Committed: Thu Dec 12 10:13:13 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |   2 +
 .../org/apache/tajo/datum/DatumFactory.java     |  15 ++-
 .../java/org/apache/tajo/datum/TimeDatum.java   | 135 +++++++++++++++++++
 .../org/apache/tajo/json/CommonGsonHelper.java  |   2 +
 .../org/apache/tajo/json/TimeDatumAdapter.java  |  41 ++++++
 .../org/apache/tajo/datum/TestTimeDatum.java    | 111 +++++++++++++++
 6 files changed, 305 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2ff0eeac/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 1047c9f..3c4fbd0 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-61: Implement Time Datum Type. (DaeMyung Kang via hyunsik)
+
     TAJO-60: Implement Date Datum Type. (hyunsik)
 
     TAJO-400: Implement pow(float8, float8) function. (DaeMyung Kang via jaehwa)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2ff0eeac/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java b/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
index d25c7d8..b4c9f17 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
@@ -23,6 +23,7 @@ import org.apache.tajo.common.TajoDataTypes.DataType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.util.Bytes;
 import org.joda.time.LocalDate;
+import org.joda.time.LocalTime;
 
 import java.io.IOException;
 
@@ -50,6 +51,8 @@ public class DatumFactory {
         return TimestampDatum.class;
       case DATE:
         return DateDatum.class;
+      case TIME:
+        return TimeDatum.class;
       case BIT:
         return BitDatum.class;
       case BLOB:
@@ -86,6 +89,8 @@ public class DatumFactory {
         return createText(bytes);
       case DATE:
         return new DateDatum(bytes);
+      case TIME:
+        return new TimeDatum(bytes);
       case TIMESTAMP:
         return new TimestampDatum(bytes);
       case BIT:
@@ -216,6 +221,14 @@ public class DatumFactory {
     return new DateDatum(LocalDate.parse(dateStr));
   }
 
+  public static TimeDatum createTime(long instance) {
+    return new TimeDatum(instance);
+  }
+
+  public static TimeDatum createTime(String dateStr) {
+    return new TimeDatum(LocalTime.parse(dateStr));
+  }
+
   public static TimestampDatum createTimeStamp(long instance) {
     return new TimestampDatum(instance);
   }
@@ -251,4 +264,4 @@ public class DatumFactory {
   public static Inet4Datum createInet4(String val) {
     return new Inet4Datum(val);
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2ff0eeac/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
new file mode 100644
index 0000000..7ea0d47
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
@@ -0,0 +1,135 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.datum;
+
+import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.datum.exception.InvalidCastException;
+import org.apache.tajo.datum.exception.InvalidOperationException;
+import org.apache.tajo.util.Bytes;
+import org.joda.time.LocalTime;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+public class TimeDatum extends Datum {
+  public static final int SIZE = 8;
+  /** ISO 8601/SQL standard format - ex) 07:37:16-08 */
+  public static final String DEFAULT_FORMAT_STRING = "HH:mm:ss";
+  private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormat.forPattern(DEFAULT_FORMAT_STRING);
+  private LocalTime time;
+
+  public TimeDatum(long value) {
+    super(TajoDataTypes.Type.TIME);
+    time = new LocalTime(value);
+  }
+
+  public TimeDatum(LocalTime time) {
+    super(TajoDataTypes.Type.TIME);
+    this.time = time;
+  }
+
+  public TimeDatum(byte [] bytes) {
+    this(Bytes.toLong(bytes));
+  }
+
+  public int getHourOfDay() {
+    return time.getHourOfDay();
+  }
+
+  public int getMinuteOfHour() {
+    return time.getMinuteOfHour();
+  }
+
+  public int getSecondOfMinute() {
+    return time.getSecondOfMinute();
+  }
+
+  public int getMillisOfDay() {
+    return time.getMillisOfDay();
+  }
+
+  public int getMillisOfSecond() {
+    return time.getMillisOfSecond();
+  }
+
+  public String toString() {
+    return asChars();
+  }
+
+  @Override
+  public int asInt4() {
+    throw new InvalidCastException();
+  }
+
+  @Override
+  public long asInt8() {
+    return time.toDateTimeToday().getMillis();
+  }
+
+  @Override
+  public float asFloat4() {
+    throw new InvalidCastException();
+  }
+
+  @Override
+  public double asFloat8() {
+    throw new InvalidCastException();
+  }
+
+  @Override
+  public String asChars() {
+    return time.toString(DEFAULT_FORMATTER);
+  }
+
+  public String toChars(String format) {
+    return time.toString(format);
+  }
+
+  @Override
+  public int size() {
+    return SIZE;
+  }
+
+  @Override
+  public byte [] asByteArray() {
+    return Bytes.toBytes(asInt8());
+  }
+
+  @Override
+  public int compareTo(Datum datum) {
+    if (datum.type() == TajoDataTypes.Type.TIME) {
+      return time.compareTo(((TimeDatum)datum).time);
+    } else {
+      throw new InvalidOperationException();
+    }
+  }
+
+  public boolean equals(Object obj) {
+    if (obj instanceof TimeDatum) {
+      TimeDatum another = (TimeDatum) obj;
+      return time.isEqual(another.time);
+    } else {
+      throw new InvalidOperationException();
+    }
+  }
+
+  @Override
+  public int hashCode() {
+    return time.hashCode();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2ff0eeac/tajo-common/src/main/java/org/apache/tajo/json/CommonGsonHelper.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/json/CommonGsonHelper.java b/tajo-common/src/main/java/org/apache/tajo/json/CommonGsonHelper.java
index fb32959..947c5a3 100644
--- a/tajo-common/src/main/java/org/apache/tajo/json/CommonGsonHelper.java
+++ b/tajo-common/src/main/java/org/apache/tajo/json/CommonGsonHelper.java
@@ -21,6 +21,7 @@ package org.apache.tajo.json;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import org.apache.tajo.datum.DateDatum;
+import org.apache.tajo.datum.TimeDatum;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.TimestampDatum;
 import org.apache.tajo.util.TUtil;
@@ -40,6 +41,7 @@ public class CommonGsonHelper {
     adapters.put(Datum.class, new DatumAdapter());
     adapters.put(TimestampDatum.class, new TimestampDatumAdapter());
     adapters.put(DateDatum.class, new DateDatumAdapter());
+    adapters.put(TimeDatum.class, new TimeDatumAdapter());
 
     return adapters;
 	}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2ff0eeac/tajo-common/src/main/java/org/apache/tajo/json/TimeDatumAdapter.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/json/TimeDatumAdapter.java b/tajo-common/src/main/java/org/apache/tajo/json/TimeDatumAdapter.java
new file mode 100644
index 0000000..603c2e0
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/json/TimeDatumAdapter.java
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.json;
+
+import com.google.gson.*;
+import org.apache.tajo.datum.TimeDatum;
+
+import java.lang.reflect.Type;
+
+public class TimeDatumAdapter implements GsonSerDerAdapter<TimeDatum> {
+
+	@Override
+	public TimeDatum deserialize(JsonElement json, Type typeOfT,
+                                    JsonDeserializationContext context) throws JsonParseException
{
+		JsonObject jsonObject = json.getAsJsonObject();
+		return new TimeDatum(jsonObject.get("val").getAsLong());
+	}
+
+	@Override
+	public JsonElement serialize(TimeDatum src, Type typeOfSrc, JsonSerializationContext context)
{
+		JsonObject jsonObj = new JsonObject();
+		jsonObj.addProperty("val", src.asInt8());
+		return jsonObj;
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2ff0eeac/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
new file mode 100644
index 0000000..19bab9b
--- /dev/null
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
@@ -0,0 +1,111 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.datum;
+
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.datum.exception.InvalidCastException;
+import org.apache.tajo.json.CommonGsonHelper;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class TestTimeDatum {
+  private static String TIME = "12:34:56";
+
+	@Test
+	public final void testType() {
+		Datum d = DatumFactory.createTime(TIME);
+    assertEquals(Type.TIME, d.type());
+	}
+
+  @Test(expected = InvalidCastException.class)
+	public final void testAsInt4() {
+    Datum d = DatumFactory.createTime(TIME);
+    Datum copy = DatumFactory.createTime(d.asInt4());
+    assertEquals(d, copy);
+	}
+
+	@Test
+	public final void testAsInt8() {
+    Datum d = DatumFactory.createTime(TIME);
+    Datum copy = DatumFactory.createTime(d.asInt8());
+    assertEquals(d, copy);
+	}
+
+  @Test(expected = InvalidCastException.class)
+	public final void testAsFloat4() {
+    Datum d = DatumFactory.createTime(TIME);
+    d.asFloat4();
+	}
+
+  @Test(expected = InvalidCastException.class)
+	public final void testAsFloat8() {
+    Datum d = DatumFactory.createTime(TIME);
+    d.asFloat8();
+	}
+
+	@Test
+	public final void testAsText() {
+    Datum d = DatumFactory.createTime(TIME);
+    Datum copy = DatumFactory.createTime(d.asChars());
+    assertEquals(d, copy);
+	}
+
+  @Test
+  public final void testAsByteArray() {
+    TimeDatum d = DatumFactory.createTime(TIME);
+    TimeDatum copy = new TimeDatum(d.asByteArray());
+    assertEquals(d.asInt8(), copy.asInt8());
+  }
+
+	@Test
+  public final void testSize() {
+    Datum d = DatumFactory.createTime(TIME);
+    assertEquals(TimeDatum.SIZE, d.asByteArray().length);
+  }
+
+  @Test
+  public final void testAsTextBytes() {
+    Datum d = DatumFactory.createTime(TIME);
+    assertArrayEquals(d.toString().getBytes(), d.asTextBytes());
+  }
+
+  @Test
+  public final void testToJson() {
+    Datum d = DatumFactory.createTime(TIME);
+    Datum copy = CommonGsonHelper.fromJson(d.toJson(), Datum.class);
+    assertEquals(d, copy);
+  }
+
+  @Test
+  public final void testInstance() {
+    TimeDatum d = DatumFactory.createTime(TIME);
+    TimeDatum copy = new TimeDatum(d.asInt8());
+    assertEquals(d, copy);
+  }
+
+  @Test
+  public final void testGetFields() {
+    TimeDatum d = DatumFactory.createTime(TIME);
+    assertEquals(12, d.getHourOfDay());
+    assertEquals(34, d.getMinuteOfHour());
+    assertEquals(56, d.getSecondOfMinute());
+  }
+}


Mime
View raw message