You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by ji...@apache.org on 2014/03/26 07:00:15 UTC
git commit: TAJO-684: Add functions about time. (Alvin Henrick via
jihoon)
Repository: tajo
Updated Branches:
refs/heads/master 321bdc07e -> 9ecd9f6e0
TAJO-684: Add functions about time. (Alvin Henrick via jihoon)
Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/9ecd9f6e
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/9ecd9f6e
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/9ecd9f6e
Branch: refs/heads/master
Commit: 9ecd9f6e01ee259a7f6e9af71c020a70f0ffe9bc
Parents: 321bdc0
Author: Jihoon Son <ji...@apache.org>
Authored: Wed Mar 26 14:59:49 2014 +0900
Committer: Jihoon Son <ji...@apache.org>
Committed: Wed Mar 26 14:59:49 2014 +0900
----------------------------------------------------------------------
CHANGES.txt | 2 +
.../org/apache/tajo/util/TimeStampUtil.java | 64 +++++++++
.../datetime/DateTimePartFromUnixTimeStamp.java | 141 +++++++++++++++++++
.../engine/function/TestDateTimeFunctions.java | 13 +-
4 files changed, 217 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tajo/blob/9ecd9f6e/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 0f64d88..ad923bb 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -559,6 +559,8 @@ Release 0.8.0 - unreleased
TASKS
+ TAJO-684: Add functions about time. (Alvin Henrick via jihoon)
+
TAJO-669: Add cluster setup documentation. (hyunsik)
TAJO-681: Embed sphinx rtd theme into tajo-docs. (hyunsik)
http://git-wip-us.apache.org/repos/asf/tajo/blob/9ecd9f6e/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java b/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java
new file mode 100644
index 0000000..31c5b90
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java
@@ -0,0 +1,64 @@
+/**
+ * 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.util;
+
+
+import org.apache.tajo.datum.Int8Datum;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeFieldType;
+import org.joda.time.DateTimeZone;
+
+public class TimeStampUtil {
+
+ public static long getDay(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay());
+ }
+
+ public static long getHour(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTime(dateTime.get(DateTimeFieldType.clockhourOfDay()), 0, 0, 0));
+ }
+
+ public static long getMinute(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTime(dateTime.get(DateTimeFieldType.clockhourOfDay()), dateTime.get(DateTimeFieldType.minuteOfHour()), 0, 0));
+ }
+
+ public static long getSecond(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTime(dateTime.get(DateTimeFieldType.clockhourOfDay()), dateTime.get(DateTimeFieldType.minuteOfHour()), dateTime.get(DateTimeFieldType.secondOfMinute()), 0));
+ }
+
+ public static long getMonth(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDate(dateTime.getYear(),dateTime.getMonthOfYear(),1));
+ }
+
+ public static long getDayOfWeek(DateTime dateTime,int week) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDayOfWeek(week));
+ }
+
+ public static long getYear (DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDate(dateTime.getYear(), 1, 1));
+ }
+
+ public static DateTime getUTCDateTime(Int8Datum int8Datum){
+ return new DateTime(int8Datum.asInt8()/1000, DateTimeZone.UTC);
+ }
+
+ public static long convertToMicroSeconds(DateTime dateTime) {
+ return dateTime.getMillis() * 1000;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/9ecd9f6e/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
new file mode 100644
index 0000000..6aaded0
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
@@ -0,0 +1,141 @@
+/**
+ * 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.engine.function.datetime;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.datum.*;
+import org.apache.tajo.engine.function.GeneralFunction;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.TimeStampUtil;
+import org.joda.time.DateTime;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.*;
+
+
+@Description(
+ functionName = "utc_usec_to",
+ description = "Extract field from time",
+ example = "> SELECT utc_usec_to('day', 1274259481071200);\n"
+ + "1274227200000000",
+ returnType = TajoDataTypes.Type.INT8,
+ paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT8}),
+ @ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT8, TajoDataTypes.Type.INT4})}
+)
+public class DateTimePartFromUnixTimeStamp extends GeneralFunction {
+
+ private DateTimePartExtractorFromUnixTime extractor = null;
+ private WeekPartExtractorFromUnixTime weekExtractor = null;
+
+ public DateTimePartFromUnixTimeStamp() {
+ super(new Column[]{
+ new Column("target", TEXT),
+ new Column("source", INT8),
+ new Column("dayOfWeek", INT4),
+
+ });
+ }
+
+ @Override
+ public Datum eval(Tuple params) {
+
+ Datum target = params.get(0);
+ DateTime dateTime;
+ Int4Datum dayOfWeek = null;
+
+ if (target instanceof NullDatum || params.get(1) instanceof NullDatum) {
+ return NullDatum.get();
+ }
+
+ if (params.get(1) instanceof Int8Datum) {
+ dateTime = TimeStampUtil.getUTCDateTime((Int8Datum) (params.get(1)));
+ } else {
+ return NullDatum.get();
+ }
+
+
+ if ( null == extractor || null == weekExtractor) {
+
+ String extractType = target.asChars().toLowerCase();
+
+ if (extractType.equals("day")) {
+ extractor = new DayExtractorFromTime();
+ } else if (extractType.equals("hour")) {
+ extractor = new HourExtractorFromTime();
+ } else if (extractType.equals("month")) {
+ extractor = new MonthExtractorFromTime();
+ } else if (extractType.equals("year")) {
+ extractor = new YearExtractorFromTime();
+ } else if (extractType.equals("week")) {
+ if (params.get(2) instanceof NullDatum) {
+ return NullDatum.get();
+ }
+ dayOfWeek = (Int4Datum) params.get(2);
+ weekExtractor = new WeekExtractorFromTime();
+ }
+ }
+
+ return null != weekExtractor ? weekExtractor.extract(dateTime, dayOfWeek.asInt4()) : extractor.extract(dateTime);
+ }
+
+ private interface DateTimePartExtractorFromUnixTime {
+ public Datum extract(DateTime dateTime);
+ }
+
+ private interface WeekPartExtractorFromUnixTime {
+ public Datum extract(DateTime dateTime, int week);
+ }
+
+ private class DayExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(TimeStampUtil.getDay(dateTime));
+ }
+ }
+
+ private class HourExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(TimeStampUtil.getHour(dateTime));
+ }
+ }
+
+ private class MonthExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(TimeStampUtil.getMonth(dateTime));
+ }
+ }
+
+ private class YearExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(TimeStampUtil.getYear(dateTime));
+ }
+ }
+
+ private class WeekExtractorFromTime implements WeekPartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime , int week) {
+ return DatumFactory.createInt8(TimeStampUtil.getDayOfWeek(dateTime,week));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/9ecd9f6e/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
index 3f1decb..ac7a2b8 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
@@ -27,9 +27,7 @@ import org.junit.Test;
import java.io.IOException;
-import static org.apache.tajo.common.TajoDataTypes.Type.TIMESTAMP;
-import static org.apache.tajo.common.TajoDataTypes.Type.TIME;
-import static org.apache.tajo.common.TajoDataTypes.Type.DATE;
+import static org.apache.tajo.common.TajoDataTypes.Type.*;
public class TestDateTimeFunctions extends ExprTestBase {
@@ -243,4 +241,13 @@ public class TestDateTimeFunctions extends ExprTestBase {
testSimpleEval("select date_part('week', DATE '1970-01-17');", new String[]{"3.0"});
}
+
+ @Test
+ public void testUtcUsecTo() throws IOException {
+ testSimpleEval("select utc_usec_to('day' ,1274259481071200);", new String[]{1274227200000000L+""});
+ testSimpleEval("select utc_usec_to('hour' ,1274259481071200);", new String[]{1274256000000000L+""});
+ testSimpleEval("select utc_usec_to('month' ,1274259481071200);", new String[]{1272672000000000L+""});
+ testSimpleEval("select utc_usec_to('year' ,1274259481071200);", new String[]{1262304000000000L+""});
+ testSimpleEval("select utc_usec_to('week' ,1207929480000000, 2);", new String[]{1207612800000000L+""});
+ }
}