TAJO-791: Implements ADD_DAYS() 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/86e09d7e Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/86e09d7e Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/86e09d7e Branch: refs/heads/window_function Commit: 86e09d7ece7f34e455fefcf9731184f5aa8e41ec Parents: 1a430fa Author: Hyunsik Choi <[email protected]> Authored: Wed Apr 30 14:58:44 2014 +0900 Committer: Hyunsik Choi <[email protected]> Committed: Wed Apr 30 14:58:44 2014 +0900 ---------------------------------------------------------------------- CHANGES | 2 + .../org/apache/tajo/datum/IntervalDatum.java | 8 +-- .../tajo/engine/function/datetime/AddDays.java | 65 ++++++++++++++++++++ .../engine/function/TestDateTimeFunctions.java | 19 ++++++ 4 files changed, 90 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/86e09d7e/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 19e6c91..4844994 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,8 @@ Release 0.9.0 - unreleased TAJO-790: Implements ADD_MONTHS() function. (Hyoungjun Kim via hyunsik) + TAJO-791: Implements ADD_DAYS() 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/86e09d7e/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java ---------------------------------------------------------------------- diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java index 4e04f91..029203e 100644 --- a/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java +++ b/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java @@ -30,10 +30,10 @@ import java.util.HashMap; import java.util.Map; public class IntervalDatum extends Datum { - static final long MINUTE_MILLIS = 60 * 1000; - static final long HOUR_MILLIS = 60 * MINUTE_MILLIS; - static final long DAY_MILLIS = 24 * HOUR_MILLIS; - static final long MONTH_MILLIS = 30 * DAY_MILLIS; + public static final long MINUTE_MILLIS = 60 * 1000; + public static final long HOUR_MILLIS = 60 * MINUTE_MILLIS; + public static final long DAY_MILLIS = 24 * HOUR_MILLIS; + public static final long MONTH_MILLIS = 30 * DAY_MILLIS; static enum DATE_UNIT { CENTURY, DECADE, YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MICROSEC, MILLISEC, TIMEZONE, http://git-wip-us.apache.org/repos/asf/tajo/blob/86e09d7e/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddDays.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddDays.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddDays.java new file mode 100644 index 0000000..2f7a8d7 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/AddDays.java @@ -0,0 +1,65 @@ +/** + * 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_days", + description = "Return date value which is added with given parameter.", + example = "> SELECT add_days(date '2013-12-30', 5);\n" + + "2014-01-04 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 AddDays extends GeneralFunction { + public AddDays() { + super(new Column[]{ + new Column("date", TajoDataTypes.Type.DATE), + new Column("day", TajoDataTypes.Type.INT4) + }); + } + + @Override + public Datum eval(Tuple params) { + Datum dateDatum = params.get(0); + long val = params.get(1).asInt8(); + if (val >= 0) { + return dateDatum.plus(new IntervalDatum(val * IntervalDatum.DAY_MILLIS)); + } else { + return dateDatum.minus(new IntervalDatum(0 - val * IntervalDatum.DAY_MILLIS)); + } + } +} + http://git-wip-us.apache.org/repos/asf/tajo/blob/86e09d7e/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 dea847f..507ef61 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 @@ -275,4 +275,23 @@ public class TestDateTimeFunctions extends ExprTestBase { 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"}); } + + @Test + public void testAddDays() throws IOException { + testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT2);", new String[]{"2014-01-04 00:00:00"}); + testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT4);", new String[]{"2014-01-04 00:00:00"}); + testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT8);", new String[]{"2014-01-04 00:00:00"}); + + testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT2);", new String[]{"2014-01-04 12:10:20"}); + testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT4);", new String[]{"2014-01-04 12:10:20"}); + testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT8);", new String[]{"2014-01-04 12:10:20"}); + + testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT2);", new String[]{"2013-11-28 00:00:00"}); + testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT4);", new String[]{"2013-11-28 00:00:00"}); + testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT8);", new String[]{"2013-11-28 00:00:00"}); + + testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT2);", new String[]{"2013-11-28 12:10:20"}); + testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT4);", new String[]{"2013-11-28 12:10:20"}); + testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT8);", new String[]{"2013-11-28 12:10:20"}); + } }
