Index: ql/src/test/results/clientpositive/udf_length.q.out =================================================================== --- ql/src/test/results/clientpositive/udf_length.q.out (revision 792751) +++ ql/src/test/results/clientpositive/udf_length.q.out (working copy) @@ -33,10 +33,10 @@ Move Operator files: hdfs directory: true - destination: file:/data/users/njain/hive4/hive4/build/ql/tmp/683676336/10000 + destination: file:/data/users/emil/hive1/hive1/build/ql/tmp/809193894/10000 Map Reduce Alias -> Map Operator Tree: - file:/data/users/njain/hive4/hive4/build/ql/tmp/1465726405/10002 + file:/data/users/emil/hive1/hive1/build/ql/tmp/353629462/10002 Reduce Output Operator sort order: Map-reduce partition columns: @@ -73,7 +73,7 @@ Output: default/dest1 query: SELECT dest1.* FROM dest1 Input: default/dest1 -Output: file:/data/users/njain/hive4/hive4/build/ql/tmp/1272744999/10000 +Output: file:/data/users/emil/hive1/hive1/build/ql/tmp/1493924198/10000 7 6 7 @@ -574,3 +574,4 @@ 7 7 6 +query: DROP TABLE dest1 Index: ql/src/test/results/clientpositive/udf_reverse.q.out =================================================================== --- ql/src/test/results/clientpositive/udf_reverse.q.out (revision 0) +++ ql/src/test/results/clientpositive/udf_reverse.q.out (revision 0) @@ -0,0 +1,102 @@ +query: CREATE TABLE dest1(len STRING) +query: EXPLAIN +FROM src1 INSERT OVERWRITE TABLE dest1 SELECT reverse(src1.value) +ABSTRACT SYNTAX TREE: + (TOK_QUERY (TOK_FROM (TOK_TABREF src1)) (TOK_INSERT (TOK_DESTINATION (TOK_TAB dest1)) (TOK_SELECT (TOK_SELEXPR (TOK_FUNCTION reverse (. (TOK_TABLE_OR_COL src1) value)))))) + +STAGE DEPENDENCIES: + Stage-1 is a root stage + Stage-4 depends on stages: Stage-1 + Stage-0 depends on stages: Stage-4 + +STAGE PLANS: + Stage: Stage-1 + Map Reduce + Alias -> Map Operator Tree: + src1 + Select Operator + expressions: + expr: reverse(value) + type: string + File Output Operator + compressed: false + GlobalTableId: 1 + table: + input format: org.apache.hadoop.mapred.TextInputFormat + output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat + serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe + name: dest1 + + Stage: Stage-4 + Conditional Operator + list of dependent Tasks: + Move Operator + files: + hdfs directory: true + destination: file:/data/users/emil/hive1/hive1/build/ql/tmp/533825523/10000 + Map Reduce + Alias -> Map Operator Tree: + file:/data/users/emil/hive1/hive1/build/ql/tmp/620216694/10002 + Reduce Output Operator + sort order: + Map-reduce partition columns: + expr: rand() + type: double + tag: -1 + value expressions: + expr: len + type: string + Reduce Operator Tree: + Extract + File Output Operator + compressed: false + GlobalTableId: 0 + table: + input format: org.apache.hadoop.mapred.TextInputFormat + output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat + serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe + name: dest1 + + Stage: Stage-0 + Move Operator + tables: + replace: true + table: + input format: org.apache.hadoop.mapred.TextInputFormat + output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat + serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe + name: dest1 + + +query: FROM src1 INSERT OVERWRITE TABLE dest1 SELECT reverse(src1.value) +Input: default/src1 +Output: default/dest1 +query: SELECT dest1.* FROM dest1 +Input: default/dest1 +Output: file:/data/users/emil/hive1/hive1/build/ql/tmp/1181513979/10000 +832_lav + +113_lav +72_lav +561_lav +904_lav +552_lav +872_lav +89_lav +484_lav +562_lav +391_lav +104_lav +051_lav +372_lav + + +66_lav + +312_lav +641_lav +604_lav + + + +query: DROP TABLE dest1 Index: ql/src/test/queries/clientpositive/udf_length.q =================================================================== --- ql/src/test/queries/clientpositive/udf_length.q (revision 792751) +++ ql/src/test/queries/clientpositive/udf_length.q (working copy) @@ -6,3 +6,5 @@ FROM src INSERT OVERWRITE TABLE dest1 SELECT length(src.value); SELECT dest1.* FROM dest1; + +DROP TABLE dest1; Index: ql/src/test/queries/clientpositive/udf_reverse.q =================================================================== --- ql/src/test/queries/clientpositive/udf_reverse.q (revision 0) +++ ql/src/test/queries/clientpositive/udf_reverse.q (revision 0) @@ -0,0 +1,10 @@ +CREATE TABLE dest1(len STRING); + +EXPLAIN +FROM src1 INSERT OVERWRITE TABLE dest1 SELECT reverse(src1.value); + +FROM src1 INSERT OVERWRITE TABLE dest1 SELECT reverse(src1.value); + +SELECT dest1.* FROM dest1; + +DROP TABLE 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 792751) +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (working copy) @@ -77,6 +77,7 @@ registerUDF("ltrim", UDFLTrim.class, OperatorType.PREFIX, false); registerUDF("rtrim", UDFRTrim.class, OperatorType.PREFIX, false); registerUDF("length", UDFLength.class, OperatorType.PREFIX, false); + registerUDF("reverse", UDFReverse.class, OperatorType.PREFIX, false); registerUDF("like", UDFLike.class, OperatorType.INFIX, true); registerUDF("rlike", UDFRegExp.class, OperatorType.INFIX, true); Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFReverse.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFReverse.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFReverse.java (revision 0) @@ -0,0 +1,46 @@ +/** + * 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.hive.ql.exec.UDF; +import org.apache.hadoop.io.Text; + +public class UDFReverse extends UDF { + private Text result = new Text(); + + public Text evaluate(Text s) { + if (s == null) { + return null; + } + + // Use a string because Text.getLength() returns the number of bytes. + // This can be optimized by walking over the utf8 characters and not + // creating a string at all. + String text = s.toString(); + + // Append the text to a StringBuffer in reverse order. + StringBuffer revBuff = new StringBuffer(); + for (int i = text.length() - 1; i >= 0; i--) { + revBuff.append(text.charAt(i)); + } + + result.set(revBuff.toString()); + return result; + } +}