Index: ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (revision 9364fb610f8185bd1b9944cfc9dea59dd323e05d) +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (revision ) @@ -45,67 +45,7 @@ import org.apache.hadoop.hive.ql.plan.ExprNodeDesc; import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc; import org.apache.hadoop.hive.ql.session.SessionState; -import org.apache.hadoop.hive.ql.udf.SettableUDF; -import org.apache.hadoop.hive.ql.udf.UDAFPercentile; -import org.apache.hadoop.hive.ql.udf.UDFAcos; -import org.apache.hadoop.hive.ql.udf.UDFAscii; -import org.apache.hadoop.hive.ql.udf.UDFAsin; -import org.apache.hadoop.hive.ql.udf.UDFAtan; -import org.apache.hadoop.hive.ql.udf.UDFBase64; -import org.apache.hadoop.hive.ql.udf.UDFBin; -import org.apache.hadoop.hive.ql.udf.UDFConv; -import org.apache.hadoop.hive.ql.udf.UDFCos; -import org.apache.hadoop.hive.ql.udf.UDFDayOfMonth; -import org.apache.hadoop.hive.ql.udf.UDFDegrees; -import org.apache.hadoop.hive.ql.udf.UDFE; -import org.apache.hadoop.hive.ql.udf.UDFExp; -import org.apache.hadoop.hive.ql.udf.UDFFindInSet; -import org.apache.hadoop.hive.ql.udf.UDFFromUnixTime; -import org.apache.hadoop.hive.ql.udf.UDFHex; -import org.apache.hadoop.hive.ql.udf.UDFHour; -import org.apache.hadoop.hive.ql.udf.UDFJson; -import org.apache.hadoop.hive.ql.udf.UDFLength; -import org.apache.hadoop.hive.ql.udf.UDFLike; -import org.apache.hadoop.hive.ql.udf.UDFLn; -import org.apache.hadoop.hive.ql.udf.UDFLog; -import org.apache.hadoop.hive.ql.udf.UDFLog10; -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.UDFOPBitAnd; -import org.apache.hadoop.hive.ql.udf.UDFOPBitNot; -import org.apache.hadoop.hive.ql.udf.UDFOPBitOr; -import org.apache.hadoop.hive.ql.udf.UDFOPBitXor; -import org.apache.hadoop.hive.ql.udf.UDFOPLongDivide; -import org.apache.hadoop.hive.ql.udf.UDFPI; -import org.apache.hadoop.hive.ql.udf.UDFParseUrl; -import org.apache.hadoop.hive.ql.udf.UDFRadians; -import org.apache.hadoop.hive.ql.udf.UDFRand; -import org.apache.hadoop.hive.ql.udf.UDFRegExp; -import org.apache.hadoop.hive.ql.udf.UDFRegExpExtract; -import org.apache.hadoop.hive.ql.udf.UDFRegExpReplace; -import org.apache.hadoop.hive.ql.udf.UDFRepeat; -import org.apache.hadoop.hive.ql.udf.UDFReverse; -import org.apache.hadoop.hive.ql.udf.UDFSecond; -import org.apache.hadoop.hive.ql.udf.UDFSign; -import org.apache.hadoop.hive.ql.udf.UDFSin; -import org.apache.hadoop.hive.ql.udf.UDFSpace; -import org.apache.hadoop.hive.ql.udf.UDFSqrt; -import org.apache.hadoop.hive.ql.udf.UDFSubstr; -import org.apache.hadoop.hive.ql.udf.UDFTan; -import org.apache.hadoop.hive.ql.udf.UDFToBoolean; -import org.apache.hadoop.hive.ql.udf.UDFToByte; -import org.apache.hadoop.hive.ql.udf.UDFToDouble; -import org.apache.hadoop.hive.ql.udf.UDFToFloat; -import org.apache.hadoop.hive.ql.udf.UDFToInteger; -import org.apache.hadoop.hive.ql.udf.UDFToLong; -import org.apache.hadoop.hive.ql.udf.UDFToShort; -import org.apache.hadoop.hive.ql.udf.UDFToString; -import org.apache.hadoop.hive.ql.udf.UDFType; -import org.apache.hadoop.hive.ql.udf.UDFUnbase64; -import org.apache.hadoop.hive.ql.udf.UDFUnhex; -import org.apache.hadoop.hive.ql.udf.UDFWeekOfYear; -import org.apache.hadoop.hive.ql.udf.UDFYear; +import org.apache.hadoop.hive.ql.udf.*; import org.apache.hadoop.hive.ql.udf.generic.*; import org.apache.hadoop.hive.ql.udf.ptf.MatchPath.MatchPathResolver; import org.apache.hadoop.hive.ql.udf.ptf.Noop.NoopResolver; @@ -254,6 +194,7 @@ registerUDF("from_unixtime", UDFFromUnixTime.class, false); registerGenericUDF("to_date", GenericUDFDate.class); registerUDF("weekofyear", UDFWeekOfYear.class, false); + registerUDF("format_date", UDFFormatDate.class, false); registerGenericUDF("date_add", GenericUDFDateAdd.class); registerGenericUDF("date_sub", GenericUDFDateSub.class); Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFormatDate.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFormatDate.java (revision ) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFormatDate.java (revision ) @@ -0,0 +1,68 @@ +/** + * 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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDF; +import org.apache.hadoop.io.Text; + +import java.text.SimpleDateFormat; + +@Description(name = "format_date", + value = "_FUNC_(date, [from_format], to_format) - convert datetime presentation from one format to another", + extended = "Example:\n" + + " > SELECT _FUNC_('2013-12-23 20:13:45.0', 'yyyy-MM-dd HH:mm:ss.S', 'yyyy/MM/dd') FROM src LIMIT 1;\n" + + " '2013/12/23'") +public class UDFFormatDate extends UDF { + static final Log LOG = LogFactory.getLog(UDFFormatDate.class); + + private final Text defaultFormat = new Text("yyyy-MM-dd HH:mm:ss.S"); + private final Text result = new Text(); + + + public Text evaluate(Text datetime, Text toFormat) { + if(datetime == null || toFormat == null) { + return null; + } + + return evaluate(datetime, defaultFormat, toFormat); + } + + public Text evaluate(Text datetime, Text fromFormat, Text toFormat) { + if(datetime == null || fromFormat == null || toFormat == null) { + return null; + } + + try { + String converted = new SimpleDateFormat(toFormat.toString()).format( + new SimpleDateFormat(fromFormat.toString()).parse(datetime.toString())); + + result.set(converted); + return result; + } catch (Exception e) { + LOG.info(String.format("Exception during converting %s from format %s to %s ", + datetime, fromFormat, toFormat)); + } + return null; + } + +} Index: ql/src/test/results/clientpositive/show_functions.q.out IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ql/src/test/results/clientpositive/show_functions.q.out (revision 9364fb610f8185bd1b9944cfc9dea59dd323e05d) +++ ql/src/test/results/clientpositive/show_functions.q.out (revision ) @@ -73,6 +73,7 @@ find_in_set first_value floor +format_date format_number from_unixtime from_utc_timestamp @@ -233,6 +234,7 @@ encode explode first_value +format_date from_unixtime in_file inline @@ -272,6 +274,7 @@ date_add date_sub datediff +format_date to_date PREHOOK: query: SHOW FUNCTIONS '***' PREHOOK: type: SHOWFUNCTIONS Index: ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFFormatDate.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFFormatDate.java (revision ) +++ ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFFormatDate.java (revision ) @@ -0,0 +1,58 @@ +/** + * 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 org.apache.hadoop.io.Text; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +public class TestUDFFormatDate { + + private String datetime; + private String fromFormat; + private String toFormat; + private String expectedResult; + + public TestUDFFormatDate(String datetime, String fromFormat, String toFormat, String expectedResult) { + this.datetime = datetime; + this.fromFormat = fromFormat; + this.toFormat = toFormat; + this.expectedResult = expectedResult; + } + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{ + {"2013-12-23", "yyyy-MM-dd", "dd/MM/yyyy", "23/12/2013"}, + {"2012-12-06 12:05", "yyyy-MM-dd HH:mm", "KK:mm aa, dd/MM/yy", "00:05 PM, 06/12/12"} + }); + + } + + @Test + public void test() { + Assert.assertEquals(expectedResult, + new UDFFormatDate().evaluate(new Text(datetime), new Text(fromFormat), new Text(toFormat)).toString()); + } +} Index: ql/src/test/queries/clientpositive/udf_format_date.q IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ql/src/test/queries/clientpositive/udf_format_date.q (revision ) +++ ql/src/test/queries/clientpositive/udf_format_date.q (revision ) @@ -0,0 +1,2 @@ +DESCRIBE FUNCTION format_date; +DESCRIBE FUNCTION EXTENDED format_date; Index: ql/src/test/results/clientpositive/udf_format_date.q.out IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ql/src/test/results/clientpositive/udf_format_date.q.out (revision ) +++ ql/src/test/results/clientpositive/udf_format_date.q.out (revision ) @@ -0,0 +1,13 @@ +PREHOOK: query: DESCRIBE FUNCTION format_date +PREHOOK: type: DESCFUNCTION +POSTHOOK: query: DESCRIBE FUNCTION format_date +POSTHOOK: type: DESCFUNCTION +format_date(date, [from_format], to_format) - convert datetime presentation from one format to another +PREHOOK: query: DESCRIBE FUNCTION EXTENDED format_date +PREHOOK: type: DESCFUNCTION +POSTHOOK: query: DESCRIBE FUNCTION EXTENDED format_date +POSTHOOK: type: DESCFUNCTION +format_date(date, [from_format], to_format) - convert datetime presentation from one format to another +Example: + > SELECT format_date('2013-12-23 20:13:45.0', 'yyyy-MM-dd HH:mm:ss.S', 'yyyy/MM/dd') FROM src LIMIT 1; + '2013/12/23'