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 fc0256c..ccb64e6 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -79,6 +79,7 @@ import org.apache.hadoop.hive.ql.udf.UDFLpad; import org.apache.hadoop.hive.ql.udf.UDFMinute; import org.apache.hadoop.hive.ql.udf.UDFMonth; +import org.apache.hadoop.hive.ql.udf.UDFInitCap; import org.apache.hadoop.hive.ql.udf.UDFOPBitAnd; import org.apache.hadoop.hive.ql.udf.UDFOPBitNot; import org.apache.hadoop.hive.ql.udf.UDFOPBitOr; @@ -321,6 +322,7 @@ registerUDF("day", UDFDayOfMonth.class, false); registerUDF("dayofmonth", UDFDayOfMonth.class, false); registerUDF("month", UDFMonth.class, false); + registerUDF("initcap", UDFInitCap.class, false); registerUDF("year", UDFYear.class, false); registerUDF("hour", UDFHour.class, false); registerUDF("minute", UDFMinute.class, false); diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFInitCap.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFInitCap.java new file mode 100644 index 0000000..4c9be35 --- /dev/null +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFInitCap.java @@ -0,0 +1,61 @@ +/** + * 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.util.StringTokenizer; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDF; +import org.apache.hadoop.io.Text; +/** + * + * UDFInitCap + * + */ +@Description(name = "initcap", value = "_FUNC_(string) -" + + " Returns the string with the first letter of each word in uppercase, all other letters in same case ") + +public class UDFInitCap extends UDF { + + public UDFInitCap() { + + } + /** + * Converts the first letter of each word in uppercase, all other letters in same case + * + * @param inputString + * + * @return result + */ + public String evaluate(Text inputString) { + + String inputStr = inputString.toString(); + String result = ""; + StringTokenizer st = new StringTokenizer(inputStr); + + while (st.hasMoreTokens()) { + String word = st.nextToken(); + String wordUpper = word.toUpperCase(); + result = result + " " + wordUpper.charAt(0) + + word.substring(1, word.length()); + } + + return result; + + } +} diff --git ql/src/test/queries/clientpositive/udf_initcap.q ql/src/test/queries/clientpositive/udf_initcap.q new file mode 100644 index 0000000..9af29ee --- /dev/null +++ ql/src/test/queries/clientpositive/udf_initcap.q @@ -0,0 +1,22 @@ +DESCRIBE FUNCTION initcap; +DESCRIBE FUNCTION EXTENDED initcap; +EXPLAIN SELECT 'good morning', + initcap("good morning") FROM src LIMIT 1; +SELECT 'good morning', + initcap("good morning") FROM src LIMIT 1; + +SELECT + '1234', + initcap("1234") FROM src LIMIT 1; + + +SELECT + '$#', + initcap("$#") +FROM src LIMIT 1; + +SELECT + 'random_string', + initcap('random_string') +FROM src LIMIT 1; + diff --git ql/src/test/results/clientpositive/udf_initcap.q.out ql/src/test/results/clientpositive/udf_initcap.q.out new file mode 100644 index 0000000..9e220f5 --- /dev/null +++ ql/src/test/results/clientpositive/udf_initcap.q.out @@ -0,0 +1,104 @@ +PREHOOK: query: DESCRIBE FUNCTION initcap +PREHOOK: type: DESCFUNCTION +POSTHOOK: query: DESCRIBE FUNCTION initcap +POSTHOOK: type: DESCFUNCTION +initcap(string) - Returns the string with the first letter of each word in uppercase, all other letters in same case +PREHOOK: query: DESCRIBE FUNCTION EXTENDED initcap +PREHOOK: type: DESCFUNCTION +POSTHOOK: query: DESCRIBE FUNCTION EXTENDED initcap +POSTHOOK: type: DESCFUNCTION +initcap(string) - Returns the string with the first letter of each word in uppercase, all other letters in same case +PREHOOK: query: EXPLAIN SELECT 'good morning', + initcap("good morning") FROM src LIMIT 1 +PREHOOK: type: QUERY +POSTHOOK: query: EXPLAIN SELECT 'good morning', + initcap("good morning") FROM src LIMIT 1 +POSTHOOK: type: QUERY +ABSTRACT SYNTAX TREE: + (TOK_QUERY (TOK_FROM (TOK_TABREF (TOK_TABNAME src))) (TOK_INSERT (TOK_DESTINATION (TOK_DIR TOK_TMP_FILE)) (TOK_SELECT (TOK_SELEXPR 'good morning') (TOK_SELEXPR (TOK_FUNCTION initcap "good morning"))) (TOK_LIMIT 1))) + +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: + src + TableScan + alias: src + Select Operator + expressions: + expr: 'good morning' + type: string + expr: initcap('good morning') + type: string + outputColumnNames: _col0, _col1 + Limit + File Output Operator + compressed: false + GlobalTableId: 0 + table: + input format: org.apache.hadoop.mapred.TextInputFormat + output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat + + Stage: Stage-0 + Fetch Operator + limit: 1 + + +PREHOOK: query: SELECT 'good morning', + initcap("good morning") FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +#### A masked pattern was here #### +POSTHOOK: query: SELECT 'good morning', + initcap("good morning") FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +#### A masked pattern was here #### +good morning Good Morning +PREHOOK: query: SELECT + '1234', + initcap("1234") FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +#### A masked pattern was here #### +POSTHOOK: query: SELECT + '1234', + initcap("1234") FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +#### A masked pattern was here #### +1234 1234 +PREHOOK: query: SELECT + '$#', + initcap("$#") +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +#### A masked pattern was here #### +POSTHOOK: query: SELECT + '$#', + initcap("$#") +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +#### A masked pattern was here #### +$# $# +PREHOOK: query: SELECT + 'random_string', + initcap('random_string') +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +#### A masked pattern was here #### +POSTHOOK: query: SELECT + 'random_string', + initcap('random_string') +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +#### A masked pattern was here #### +random_string Random_string