PHOENIX-1781 Add Now() (Alicia Ying Shu)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/13d6296f Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/13d6296f Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/13d6296f Branch: refs/heads/calcite Commit: 13d6296f7cab70e45a5fa9e579f81b2fa0dc03fd Parents: 0eca5f1 Author: Rajeshbabu Chintaguntla <[email protected]> Authored: Wed Apr 1 22:36:57 2015 +0530 Committer: Rajeshbabu Chintaguntla <[email protected]> Committed: Wed Apr 1 22:36:57 2015 +0530 ---------------------------------------------------------------------- .../end2end/YearMonthSecondFunctionIT.java | 30 ++++++++++-- .../phoenix/expression/ExpressionType.java | 6 ++- .../expression/function/NowFunction.java | 48 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/13d6296f/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java index 3742a17..20a88c0 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java @@ -15,7 +15,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.sql.Connection; +import java.sql.Date; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -58,6 +60,8 @@ public class YearMonthSecondFunctionIT extends BaseHBaseManagedTimeIT { public void testYearFunctionDate() throws SQLException { assertEquals(2015, callYearFunction("YEAR(current_date())")); + + assertEquals(2015, callYearFunction("YEAR(now())")); assertEquals(2008, callYearFunction("YEAR(TO_DATE('2008-01-01', 'yyyy-MM-dd', 'local'))")); @@ -174,18 +178,18 @@ public class YearMonthSecondFunctionIT extends BaseHBaseManagedTimeIT { String ddl = "CREATE TABLE IF NOT EXISTS T1 (k1 INTEGER NOT NULL, dates DATE, timestamps TIMESTAMP, times TIME CONSTRAINT pk PRIMARY KEY (k1))"; conn.createStatement().execute(ddl); - String dml = "UPSERT INTO T1 VALUES (1, TO_DATE('2004-03-01 00:00:10'), TO_TIMESTAMP('2006-04-12 00:00:20'), TO_TIME('2008-05-16 10:00:30'))"; + String dml = "UPSERT INTO T1 VALUES (1, TO_DATE('2004-02-01 00:00:10'), TO_TIMESTAMP('2006-04-12 00:00:20'), TO_TIME('2008-05-16 10:00:30'))"; conn.createStatement().execute(dml); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT k1, WEEK(dates), WEEK(times) FROM T1 where WEEK(timestamps)=15"); assertTrue(rs.next()); assertEquals(1, rs.getInt(1)); - assertEquals(9, rs.getInt(2)); + assertEquals(5, rs.getInt(2)); assertEquals(20, rs.getInt(3)); assertFalse(rs.next()); } - + @Test public void testHourFuncAgainstColumns() throws Exception { String ddl = @@ -204,4 +208,24 @@ public class YearMonthSecondFunctionIT extends BaseHBaseManagedTimeIT { assertEquals(20, rs.getInt(4)); assertFalse(rs.next()); } + + @Test + public void testNowFunction() throws Exception { + Date date = new Date(System.currentTimeMillis()); + String ddl = + "CREATE TABLE IF NOT EXISTS T1 (k1 INTEGER NOT NULL, timestamps TIMESTAMP CONSTRAINT pk PRIMARY KEY (k1))"; + conn.createStatement().execute(ddl); + String dml = "UPSERT INTO T1 VALUES (?, ?)"; + PreparedStatement stmt = conn.prepareStatement(dml); + stmt.setInt(1, 1); + stmt.setDate(2, new Date(date.getTime()-500)); + stmt.execute(); + conn.commit(); + + ResultSet rs = conn.createStatement().executeQuery("SELECT * from T1 where now() > timestamps"); + assertTrue(rs.next()); + assertEquals(1, rs.getInt(1)); + assertEquals(new Date(date.getTime()-500), rs.getDate(2)); + assertFalse(rs.next()); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/phoenix/blob/13d6296f/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java index 8a2f127..a7f8b4f 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java @@ -52,6 +52,7 @@ import org.apache.phoenix.expression.function.MD5Function; import org.apache.phoenix.expression.function.MaxAggregateFunction; import org.apache.phoenix.expression.function.MinAggregateFunction; import org.apache.phoenix.expression.function.MonthFunction; +import org.apache.phoenix.expression.function.NowFunction; import org.apache.phoenix.expression.function.NthValueFunction; import org.apache.phoenix.expression.function.PercentRankAggregateFunction; import org.apache.phoenix.expression.function.PercentileContAggregateFunction; @@ -92,7 +93,7 @@ import com.google.common.collect.Maps; /** * - * Enumeration of all Expression types that may be evaluated on the server-side. + * Enumeration of all Expression types that will be looked up. They may be evaluated on the server-side. * Used during serialization and deserialization to pass Expression between client * and server. * @@ -203,7 +204,8 @@ public enum ExpressionType { MonthFunction(MonthFunction.class), SecondFunction(SecondFunction.class), WeekFunction(WeekFunction.class), - HourFunction(HourFunction.class) + HourFunction(HourFunction.class), + NowFunction(NowFunction.class) ; ExpressionType(Class<? extends Expression> clazz) { http://git-wip-us.apache.org/repos/asf/phoenix/blob/13d6296f/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NowFunction.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NowFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NowFunction.java new file mode 100644 index 0000000..dc90249 --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NowFunction.java @@ -0,0 +1,48 @@ +/* + * 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.phoenix.expression.function; + +import java.sql.SQLException; +import java.util.List; + +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.parse.CurrentDateParseNode; +import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction; + +/** + * + * Function used to represent NOW() + * The function returns a {@link org.apache.phoenix.schema.types.PTimestamp} + * + */ +@BuiltInFunction(name = NowFunction.NAME, +nodeClass=CurrentDateParseNode.class, args= {}) +public abstract class NowFunction extends ScalarFunction { + + public static final String NAME = "NOW"; + + public NowFunction(List<Expression> children) throws SQLException { + super(children); + } + + @Override + public String getName() { + return NAME; + } + +}
