Skip to content

Commit

Permalink
PHOENIX-1846 Add MINUTE built-in function (Alicia Ying Shu)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtaylor-sfdc committed Apr 13, 2015
1 parent 36a7f24 commit 8975fc1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
Expand Up @@ -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());
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends Expression> clazz) {
Expand Down
@@ -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);
}
}

0 comments on commit 8975fc1

Please sign in to comment.