You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by hy...@apache.org on 2014/05/20 20:46:18 UTC

[20/48] git commit: TAJO-790: Implements ADD_MONTHS() function. (Hyoungjun Kim via hyunsik)

TAJO-790: Implements ADD_MONTHS() function. (Hyoungjun Kim via hyunsik)


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

Branch: refs/heads/window_function
Commit: 1a430fae5a73f8b5df7fb225e001263379a695fb
Parents: 5f1b8e2
Author: Hyunsik Choi <hy...@apache.org>
Authored: Wed Apr 30 14:56:40 2014 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Wed Apr 30 14:56:40 2014 +0900

----------------------------------------------------------------------
 CHANGES                                         |  2 +
 .../engine/function/datetime/AddMonths.java     | 64 ++++++++++++++++++++
 .../engine/function/TestDateTimeFunctions.java  | 19 ++++++
 3 files changed, 85 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/1a430fae/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 4d4e101..19e6c91 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,8 @@ Release 0.9.0 - unreleased
 
     TAJO-761: Implements INTERVAL type. (Hyoungjun Kim via hyunsik)
 
+    TAJO-790: Implements ADD_MONTHS() function. (Hyoungjun Kim via hyunsik)
+
   IMPROVEMENT
 
     TAJO-425: RAWFILE_SYNC_INTERVAL has not default value. (jinho)

http://git-wip-us.apache.org/repos/asf/tajo/blob/1a430fae/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddMonths.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddMonths.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddMonths.java
new file mode 100644
index 0000000..4af4ff9
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddMonths.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.engine.function.datetime;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.IntervalDatum;
+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;
+
+@Description(
+    functionName = "add_months",
+    description = "Return date value which is added with given parameter.",
+    example = "> SELECT add_months(date '2013-12-17', 2);\n"
+        + "2014-02-17 00:00:00",
+    returnType = Type.TIMESTAMP,
+    paramTypes = {
+        @ParamTypes(paramTypes = {Type.DATE, Type.INT2}),
+        @ParamTypes(paramTypes = {Type.DATE, Type.INT4}),
+        @ParamTypes(paramTypes = {Type.DATE, Type.INT8}),
+        @ParamTypes(paramTypes = {Type.TIMESTAMP, Type.INT2}),
+        @ParamTypes(paramTypes = {Type.TIMESTAMP, Type.INT4}),
+        @ParamTypes(paramTypes = {Type.TIMESTAMP, Type.INT8})
+    }
+)
+public class AddMonths extends GeneralFunction {
+  public AddMonths() {
+    super(new Column[]{
+        new Column("date", TajoDataTypes.Type.DATE),
+        new Column("month", TajoDataTypes.Type.INT4)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum dateDatum = params.get(0);
+    int val = params.get(1).asInt4();
+    if (val >= 0) {
+      return dateDatum.plus(new IntervalDatum(val, 0));
+    } else {
+      return dateDatum.minus(new IntervalDatum(0- val, 0));
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/1a430fae/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
index 8641548..dea847f 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
@@ -256,4 +256,23 @@ public class TestDateTimeFunctions extends ExprTestBase {
     testSimpleEval("select to_date('2014-01-04', 'yyyy-MM-dd')", new String[]{"2014-01-04"});
     testSimpleEval("select to_date('2014-01-04', 'yyyy-MM-dd') + interval '1 day'", new String[]{"2014-01-05 00:00:00"});
   }
+
+  @Test
+  public void testAddMonths() throws Exception {
+    testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT2);", new String[]{"2014-02-17 00:00:00"});
+    testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT4);", new String[]{"2014-02-17 00:00:00"});
+    testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT8);", new String[]{"2014-02-17 00:00:00"});
+
+    testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT2);", new String[]{"2014-02-17 12:10:20"});
+    testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT4);", new String[]{"2014-02-17 12:10:20"});
+    testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT8);", new String[]{"2014-02-17 12:10:20"});
+
+    testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT2);", new String[]{"2013-11-05 00:00:00"});
+    testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT4);", new String[]{"2013-11-05 00:00:00"});
+    testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT8);", new String[]{"2013-11-05 00:00:00"});
+
+    testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT2);", new String[]{"2013-11-05 12:10:20"});
+    testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT4);", new String[]{"2013-11-05 12:10:20"});
+    testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT8);", new String[]{"2013-11-05 12:10:20"});
+  }
 }