PHOENIX-1846 Add MINUTE built-in function (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/5f9445d0 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/5f9445d0 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/5f9445d0 Branch: refs/heads/4.x-HBase-0.98 Commit: 5f9445d06cf3f4de7d7e670fd70effb541fc598e Parents: d74526f Author: James Taylor <jtay...@salesforce.com> Authored: Mon Apr 13 16:32:31 2015 -0700 Committer: Cody Marcel <codymar...@apache.org> Committed: Tue Apr 14 09:40:14 2015 -0700 ---------------------------------------------------------------------- .../end2end/YearMonthSecondFunctionIT.java | 26 +++++++ .../phoenix/expression/ExpressionType.java | 4 +- .../expression/function/MinuteFunction.java | 81 ++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f9445d0/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 cc51bdd..1206ee4 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 @@ -258,4 +258,30 @@ public class YearMonthSecondFunctionIT extends BaseHBaseManagedTimeIT { assertEquals(new Date(date.getTime()-500), rs.getDate(2)); assertFalse(rs.next()); } + + @Test + public void testMinuteFuncAgainstColumns() throws Exception { + String ddl = + "CREATE TABLE IF NOT EXISTS T1 (k1 INTEGER NOT NULL, dates DATE, timestamps TIMESTAMP, times TIME, " + + "unsignedDates UNSIGNED_DATE, unsignedTimestamps UNSIGNED_TIMESTAMP, unsignedTimes UNSIGNED_TIME CONSTRAINT pk PRIMARY KEY (k1))"; + conn.createStatement().execute(ddl); + String dml = "UPSERT INTO T1 VALUES (1, TO_DATE('2004-03-01 00:10:10'), TO_TIMESTAMP('2006-04-12 00:20:20'), TO_TIME('2008-05-16 10:30:30'), " + + "TO_DATE('2010-06-20 00:40:40:789', 'yyyy-MM-dd HH:mm:ss:SSS'), TO_TIMESTAMP('2012-07-28'), TO_TIME('2015-12-25 00:50:50'))"; + conn.createStatement().execute(dml); + dml = "UPSERT INTO T1 VALUES (2, TO_DATE('2004-03-01 00:10:10'), TO_TIMESTAMP('2006-04-12 00:50:20'), TO_TIME('2008-05-16 10:30:30'), " + + "TO_DATE('2010-06-20 00:40:40:789', 'yyyy-MM-dd HH:mm:ss:SSS'), TO_TIMESTAMP('2012-07-28'), TO_TIME('2015-12-25 00:50:50'))"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs = conn.createStatement().executeQuery("SELECT k1, MINUTE(dates), MINUTE(times), MINUTE(unsignedDates), MINUTE(unsignedTimestamps), " + + "MINUTE(unsignedTimes) FROM T1 where MINUTE(timestamps)=20"); + assertTrue(rs.next()); + assertEquals(1, rs.getInt(1)); + assertEquals(10, rs.getInt(2)); + assertEquals(30, rs.getInt(3)); + assertEquals(40, rs.getInt(4)); + assertEquals(0, rs.getInt(5)); + assertEquals(50, rs.getInt(6)); + assertFalse(rs.next()); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f9445d0/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 fcb9dd1..ed15a28 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.LpadFunction; 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.MinuteFunction; import org.apache.phoenix.expression.function.MonthFunction; import org.apache.phoenix.expression.function.NowFunction; import org.apache.phoenix.expression.function.NthValueFunction; @@ -206,7 +207,8 @@ public enum ExpressionType { WeekFunction(WeekFunction.class), HourFunction(HourFunction.class), NowFunction(NowFunction.class), - InstrFunction(InstrFunction.class) + InstrFunction(InstrFunction.class), + MinuteFunction(MinuteFunction.class) ; ExpressionType(Class<? extends Expression> clazz) { http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f9445d0/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MinuteFunction.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MinuteFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MinuteFunction.java new file mode 100644 index 0000000..fc721fc --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MinuteFunction.java @@ -0,0 +1,81 @@ +/* + * 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.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.parse.FunctionParseNode.Argument; +import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.schema.types.PInteger; +import org.apache.phoenix.schema.types.PTimestamp; + +/** + * + * Implementation of the Minute() buildin. Input Date/Timestamp/Time. + * Returns an integer from 0 to 59 representing the minute component of time + * + */ +@BuiltInFunction(name=MinuteFunction.NAME, +args={@Argument(allowedTypes={PTimestamp.class})}) +public class MinuteFunction extends ScalarFunction { + public static final String NAME = "MINUTE"; + + public MinuteFunction() { + } + + public MinuteFunction(List<Expression> children) throws SQLException { + super(children); + } + + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + Expression expression = getChildExpression(); + if (!expression.evaluate(tuple, ptr)) { + return false; + } + if ( ptr.getLength() == 0) { + return true; //means null + } + long dateTime = expression.getDataType().getCodec().decodeLong(ptr, expression.getSortOrder()); + int minute = (int)(((dateTime/1000) % 3600)/60); + PDataType returnType = getDataType(); + byte[] byteValue = new byte[returnType.getByteSize()]; + returnType.getCodec().encodeInt(minute, byteValue, 0); + ptr.set(byteValue); + return true; + } + + @Override + public PDataType getDataType() { + return PInteger.INSTANCE; + } + + @Override + public String getName() { + return NAME; + } + + private Expression getChildExpression() { + return children.get(0); + } +}