diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java index dbb8eaf..08eb05c 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -78,6 +78,7 @@ import org.apache.hadoop.hive.ql.udf.UDFLog2; import org.apache.hadoop.hive.ql.udf.UDFMinute; import org.apache.hadoop.hive.ql.udf.UDFMonth; +import org.apache.hadoop.hive.ql.udf.UDFNow; import org.apache.hadoop.hive.ql.udf.UDFOPBitAnd; import org.apache.hadoop.hive.ql.udf.UDFOPBitNot; import org.apache.hadoop.hive.ql.udf.UDFOPBitOr; @@ -265,6 +266,7 @@ registerUDF("from_unixtime", UDFFromUnixTime.class, false); registerGenericUDF("to_date", GenericUDFDate.class); registerUDF("weekofyear", UDFWeekOfYear.class, false); + registerUDF("now", UDFNow.class, false); registerGenericUDF("date_add", GenericUDFDateAdd.class); registerGenericUDF("date_sub", GenericUDFDateSub.class); diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFNow.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFNow.java new file mode 100644 index 0000000..58e009d --- /dev/null +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFNow.java @@ -0,0 +1,45 @@ +/** + * 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.hadoop.hive.ql.udf; + +import java.sql.Timestamp; +import java.util.Date; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDF; +import org.apache.hadoop.hive.serde2.io.TimestampWritable; + +/** + * Returns the date and time as of the beginning of the query as a TIMESTAMP. + * It mimics now() in mysql. + */ +@Description(name = "now", + value = "_FUNC_() - returns the TIMESTAMP of when this UDF instance was created.", + extended = "It mimics MySQL's 'now()' function as closely as possible but it can't guarantee " + + "that it will always return the same timestamp even within a single query as " + + "processes may be started at different times.") +public class UDFNow extends UDF { + + private final TimestampWritable timestamp = + new TimestampWritable(new Timestamp(new Date().getTime())); + + public TimestampWritable evaluate() { + return timestamp; + } +}