From 8975fc1a427700fc3f13d34ddd610404781683ae Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 13 Apr 2015 16:32:31 -0700 Subject: [PATCH] PHOENIX-1846 Add MINUTE built-in function (Alicia Ying Shu) --- .../end2end/YearMonthSecondFunctionIT.java | 26 ++++++ .../phoenix/expression/ExpressionType.java | 4 +- .../expression/function/MinuteFunction.java | 81 +++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 phoenix-core/src/main/java/org/apache/phoenix/expression/function/MinuteFunction.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 cc51bdd9bc6..1206ee44b05 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 void testNowFunction() throws Exception { 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 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 c25b1cc4911..d42c5f2800c 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.MinuteFunction; import org.apache.phoenix.expression.function.MonthFunction; import org.apache.phoenix.expression.function.NowFunction; import org.apache.phoenix.expression.function.NthValueFunction; @@ -207,7 +208,8 @@ public enum ExpressionType { WeekFunction(WeekFunction.class), HourFunction(HourFunction.class), NowFunction(NowFunction.class), - InstrFunction(InstrFunction.class) + InstrFunction(InstrFunction.class), + MinuteFunction(MinuteFunction.class) ; ExpressionType(Class clazz) { 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 00000000000..fc721fc8e5f --- /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 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); + } +}