Index: ql/src/test/results/clientpositive/udf5.q.out =================================================================== --- ql/src/test/results/clientpositive/udf5.q.out (revision 0) +++ ql/src/test/results/clientpositive/udf5.q.out (revision 0) @@ -0,0 +1,42 @@ +ABSTRACT SYNTAX TREE: + (TOK_QUERY (TOK_FROM (TOK_TABREF dest1)) (TOK_INSERT (TOK_DESTINATION (TOK_DIR TOK_TMP_FILE)) (TOK_SELECT (TOK_SELEXPR (TOK_FUNCTION from_unixtime 1226446340)) (TOK_SELEXPR (TOK_FUNCTION to_date (TOK_FUNCTION from_unixtime 1226446340))) (TOK_SELEXPR (TOK_FUNCTION day '2008-11-01')) (TOK_SELEXPR (TOK_FUNCTION month '2008-11-01')) (TOK_SELEXPR (TOK_FUNCTION year '2008-11-01')) (TOK_SELEXPR (TOK_FUNCTION day '2008-11-01 15:32:20')) (TOK_SELEXPR (TOK_FUNCTION month '2008-11-01 15:32:20')) (TOK_SELEXPR (TOK_FUNCTION year '2008-11-01 15:32:20'))))) + +STAGE DEPENDENCIES: + Stage-1 is a root stage + Stage-0 is a root stage + +STAGE PLANS: + Stage: Stage-1 + Map Reduce + Alias -> Map Operator Tree: + dest1 + Select Operator + Select Operator + expressions: + expr: from_unixtime(1226446340) + type: string + expr: to_date(from_unixtime(1226446340)) + type: string + expr: day('2008-11-01') + type: int + expr: month('2008-11-01') + type: int + expr: year('2008-11-01') + type: int + expr: day('2008-11-01 15:32:20') + type: int + expr: month('2008-11-01 15:32:20') + type: int + expr: year('2008-11-01 15:32:20') + type: int + File Output Operator + table: + input format: org.apache.hadoop.mapred.TextInputFormat + output format: org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat + + Stage: Stage-0 + Fetch Operator + limit: -1 + + +2008-11-11 15:32:20 2008-11-11 1 11 2008 1 11 2008 Index: ql/src/test/queries/clientpositive/udf5.q =================================================================== --- ql/src/test/queries/clientpositive/udf5.q (revision 0) +++ ql/src/test/queries/clientpositive/udf5.q (revision 0) @@ -0,0 +1,8 @@ +CREATE TABLE dest1(c1 STRING) STORED AS TEXTFILE; + +FROM src INSERT OVERWRITE TABLE dest1 SELECT ' abc ' WHERE src.key = 86; + +EXPLAIN +SELECT from_unixtime(1226446340), to_date(from_unixtime(1226446340)), day('2008-11-01'), month('2008-11-01'), year('2008-11-01'), day('2008-11-01 15:32:20'), month('2008-11-01 15:32:20'), year('2008-11-01 15:32:20') FROM dest1; + +SELECT from_unixtime(1226446340), to_date(from_unixtime(1226446340)), day('2008-11-01'), month('2008-11-01'), year('2008-11-01'), day('2008-11-01 15:32:20'), month('2008-11-01 15:32:20'), year('2008-11-01 15:32:20') FROM dest1; Index: ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (revision 723394) +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (working copy) @@ -80,6 +80,13 @@ registerUDF("positive", UDFOPPositive.class, OperatorType.PREFIX, true, "+"); registerUDF("negative", UDFOPNegative.class, OperatorType.PREFIX, true, "-"); + registerUDF("day", UDFDayOfMonth.class, OperatorType.PREFIX, false); + registerUDF("dayofmonth", UDFDayOfMonth.class, OperatorType.PREFIX, false); + registerUDF("month", UDFMonth.class, OperatorType.PREFIX, false); + registerUDF("year", UDFYear.class, OperatorType.PREFIX, false); + registerUDF("from_unixtime", UDFFromUnixTime.class, OperatorType.PREFIX, false); + registerUDF("to_date", UDFDate.class, OperatorType.PREFIX, false); + registerUDF("+", UDFOPPlus.class, OperatorType.INFIX, true); registerUDF("-", UDFOPMinus.class, OperatorType.INFIX, true); registerUDF("*", UDFOPMultiply.class, OperatorType.INFIX, true); Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFYear.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFYear.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFYear.java (revision 0) @@ -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 java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.ql.exec.UDF; + + +public class UDFYear implements UDF { + + private static Log LOG = LogFactory.getLog(UDFYear.class.getName()); + + private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + private Calendar calendar = Calendar.getInstance(); + + public UDFYear() { + } + + /** + * Get the year from a date string. + * + * @param dateString the dateString in the format of "yyyy-MM-dd HH:mm:ss" or "yyyy-MM-dd". + * @return an int from 1 to 12. null if the dateString is not a valid date string. + */ + public Integer evaluate(String dateString) { + + try { + Date date = formatter.parse(dateString); + calendar.setTime(date); + return calendar.get(Calendar.YEAR); + } catch (ParseException e) { + return null; + } + } + +} Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDate.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDate.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDate.java (revision 0) @@ -0,0 +1,57 @@ +/** + * 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.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.ql.exec.UDF; + + +public class UDFDate implements UDF { + + private static Log LOG = LogFactory.getLog(UDFDate.class.getName()); + + private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + private Calendar calendar = Calendar.getInstance(); + + public UDFDate() { + } + + /** + * Get the date part of a date time string. + * + * @param dateString the date string in the format of "yyyy-MM-dd HH:mm:ss" or "yyyy-MM-dd". + * @return the date in the format of "yyyy-MM-dd". + */ + public String evaluate(String dateString) { + + try { + Date date = formatter.parse(dateString); + return formatter.format(date); + } catch (ParseException e) { + return null; + } + } + +} Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFromUnixTime.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFromUnixTime.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFromUnixTime.java (revision 0) @@ -0,0 +1,62 @@ +/** + * 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.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.ql.exec.UDF; + + +public class UDFFromUnixTime implements UDF { + + private static Log LOG = LogFactory.getLog(UDFFromUnixTime.class.getName()); + + private SimpleDateFormat formatter; + + public UDFFromUnixTime() { + } + + public String evaluate(Integer unixtime) { + return evaluate(unixtime, "yyyy-MM-dd HH:mm:ss"); + } + + /** + * Convert UnixTime to a string format. + * @param unixtime The number of seconds from 1970-01-01 00:00:00 + * @param format See http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html + * @return a String in the format specified. + */ + public String evaluate(Integer unixtime, String format) { + if (unixtime == null || format == null) { + return null; + } + + if (formatter == null) { + formatter = new SimpleDateFormat(format); + } + + // convert seconds to milliseconds + Date date = new Date(unixtime * 1000L); + return formatter.format(date); + } + +} Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfMonth.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfMonth.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfMonth.java (revision 0) @@ -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 java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.ql.exec.UDF; + + +public class UDFDayOfMonth implements UDF { + + private static Log LOG = LogFactory.getLog(UDFDayOfMonth.class.getName()); + + private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + private Calendar calendar = Calendar.getInstance(); + + public UDFDayOfMonth() { + } + + /** + * Get the day of month from a date string. + * + * @param dateString the dateString in the format of "yyyy-MM-dd HH:mm:ss" or "yyyy-MM-dd". + * @return an int from 1 to 31. null if the dateString is not a valid date string. + */ + public Integer evaluate(String dateString) { + + try { + Date date = formatter.parse(dateString); + calendar.setTime(date); + return calendar.get(Calendar.DAY_OF_MONTH); + } catch (ParseException e) { + return null; + } + } + +} Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMonth.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMonth.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMonth.java (revision 0) @@ -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 java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.ql.exec.UDF; + + +public class UDFMonth implements UDF { + + private static Log LOG = LogFactory.getLog(UDFMonth.class.getName()); + + private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + private Calendar calendar = Calendar.getInstance(); + + public UDFMonth() { + } + + /** + * Get the month from a date string. + * + * @param dateString the dateString in the format of "yyyy-MM-dd HH:mm:ss" or "yyyy-MM-dd". + * @return an int from 1 to 12. null if the dateString is not a valid date string. + */ + public Integer evaluate(String dateString) { + + try { + Date date = formatter.parse(dateString); + calendar.setTime(date); + return 1 + calendar.get(Calendar.MONTH); + } catch (ParseException e) { + return null; + } + } + +}