Index: ql/src/test/results/clientpositive/udf_substring_index.q.out =================================================================== --- ql/src/test/results/clientpositive/udf_substring_index.q.out (revision 0) +++ ql/src/test/results/clientpositive/udf_substring_index.q.out (revision 0) @@ -0,0 +1,131 @@ +PREHOOK: query: DESCRIBE FUNCTION substring_index +PREHOOK: type: DESCFUNCTION +POSTHOOK: query: DESCRIBE FUNCTION substring_index +POSTHOOK: type: DESCFUNCTION +_FUNC(str, delim, count) - returns the substring of str before count occurrences of delim. +PREHOOK: query: DESCRIBE FUNCTION EXTENDED substring_index +PREHOOK: type: DESCFUNCTION +POSTHOOK: query: DESCRIBE FUNCTION EXTENDED substring_index +POSTHOOK: type: DESCFUNCTION +_FUNC(str, delim, count) - returns the substring of str before count occurrences of delim. +If count is positive, counting is done from the left, and everything to the left of the final delimiter is returned. If count is negative, counting is done from the right and everything to the right of the final delimted is returned. If delim can't be found count times, the input string is returned +PREHOOK: query: SELECT + substring_index("www.mysql.com", ".", 0), + substring_index("www.mysql.com", "", 1), + substring_index("www.mysql.com", ".", 1), + substring_index("www.mysql.com", ".", 2), + substring_index("www.mysql.", ".", 2), + substring_index("www.mysql.", ".", 3), + substring_index("www.mysql.co.uk", ".", 3) +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/1554181931/10000 +POSTHOOK: query: SELECT + substring_index("www.mysql.com", ".", 0), + substring_index("www.mysql.com", "", 1), + substring_index("www.mysql.com", ".", 1), + substring_index("www.mysql.com", ".", 2), + substring_index("www.mysql.", ".", 2), + substring_index("www.mysql.", ".", 3), + substring_index("www.mysql.co.uk", ".", 3) +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/1554181931/10000 + www www.mysql www.mysql www.mysql. www.mysql.co +PREHOOK: query: SELECT + substring_index("www.mysql.com", ".", -2), + substring_index("www.mysql.com", ".", -1), + substring_index("www.mysql.co.uk", ".", -2), + substring_index("www.mysql.co.uk", ".", -3), + substring_index("www.mysql.co.uk", ".", -1) +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/756648396/10000 +POSTHOOK: query: SELECT + substring_index("www.mysql.com", ".", -2), + substring_index("www.mysql.com", ".", -1), + substring_index("www.mysql.co.uk", ".", -2), + substring_index("www.mysql.co.uk", ".", -3), + substring_index("www.mysql.co.uk", ".", -1) +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/756648396/10000 +mysql.com com co.uk mysql.co.uk uk +PREHOOK: query: SELECT + substring_index(".www.", ".", 1), + substring_index(".www.", ".", 2), + substring_index(".www.", ".", 3), + substring_index(".www.", ".", -1), + substring_index(".www.", ".", -2), + substring_index(".www.", ".", -3) +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/832443732/10000 +POSTHOOK: query: SELECT + substring_index(".www.", ".", 1), + substring_index(".www.", ".", 2), + substring_index(".www.", ".", 3), + substring_index(".www.", ".", -1), + substring_index(".www.", ".", -2), + substring_index(".www.", ".", -3) +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/832443732/10000 + .www .www. www. .www. +PREHOOK: query: SELECT + substring_index("....", ".", 3), + substring_index("....", ".", 2), + substring_index("....", ".", -2) +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/2059359624/10000 +POSTHOOK: query: SELECT + substring_index("....", ".", 3), + substring_index("....", ".", 2), + substring_index("....", ".", -2) +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/2059359624/10000 +.. . . +PREHOOK: query: SELECT + substring_index("www.mysql.com", "-", 2), + substring_index("www.mysql.com", "Q", 1), + substring_index("www.mysql.com", "q", 1) +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/225828497/10000 +POSTHOOK: query: SELECT + substring_index("www.mysql.com", "-", 2), + substring_index("www.mysql.com", "Q", 1), + substring_index("www.mysql.com", "q", 1) +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/225828497/10000 +www.mysql.com www.mysql.com www.mys +PREHOOK: query: SELECT + substring_index(NULL, ".", 1), + substring_index("www.mysql.com", NULL, 1), + substring_index("www.mysql.com", ".", NULL) +FROM src LIMIT 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/1083306338/10000 +POSTHOOK: query: SELECT + substring_index(NULL, ".", 1), + substring_index("www.mysql.com", NULL, 1), + substring_index("www.mysql.com", ".", NULL) +FROM src LIMIT 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: file:/opt/h3/hive-trunk/build/ql/tmp/1083306338/10000 +NULL NULL NULL Index: ql/src/test/results/clientpositive/show_functions.q.out =================================================================== --- ql/src/test/results/clientpositive/show_functions.q.out (revision 896759) +++ ql/src/test/results/clientpositive/show_functions.q.out (working copy) @@ -109,6 +109,7 @@ string substr substring +substring_index sum tinyint to_date Index: ql/src/test/queries/clientpositive/udf_substring_index.q =================================================================== --- ql/src/test/queries/clientpositive/udf_substring_index.q (revision 0) +++ ql/src/test/queries/clientpositive/udf_substring_index.q (revision 0) @@ -0,0 +1,47 @@ +DESCRIBE FUNCTION substring_index; +DESCRIBE FUNCTION EXTENDED substring_index; + +SELECT + substring_index("www.mysql.com", ".", 0), + substring_index("www.mysql.com", "", 1), + substring_index("www.mysql.com", ".", 1), + substring_index("www.mysql.com", ".", 2), + substring_index("www.mysql.", ".", 2), + substring_index("www.mysql.", ".", 3), + substring_index("www.mysql.co.uk", ".", 3) +FROM src LIMIT 1; + +SELECT + substring_index("www.mysql.com", ".", -2), + substring_index("www.mysql.com", ".", -1), + substring_index("www.mysql.co.uk", ".", -2), + substring_index("www.mysql.co.uk", ".", -3), + substring_index("www.mysql.co.uk", ".", -1) +FROM src LIMIT 1; + +SELECT + substring_index(".www.", ".", 1), + substring_index(".www.", ".", 2), + substring_index(".www.", ".", 3), + substring_index(".www.", ".", -1), + substring_index(".www.", ".", -2), + substring_index(".www.", ".", -3) +FROM src LIMIT 1; + +SELECT + substring_index("....", ".", 3), + substring_index("....", ".", 2), + substring_index("....", ".", -2) +FROM src LIMIT 1; + +SELECT + substring_index("www.mysql.com", "-", 2), + substring_index("www.mysql.com", "Q", 1), + substring_index("www.mysql.com", "q", 1) +FROM src LIMIT 1; + +SELECT + substring_index(NULL, ".", 1), + substring_index("www.mysql.com", NULL, 1), + substring_index("www.mysql.com", ".", NULL) +FROM src LIMIT 1; Index: ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (revision 896759) +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java (working copy) @@ -61,6 +61,7 @@ registerUDF("concat", UDFConcat.class, false); registerUDF("substr", UDFSubstr.class, false); registerUDF("substring", UDFSubstr.class, false); + registerUDF("substring_index", UDFSubstringIndex.class, false); registerUDF("space", UDFSpace.class, false); registerUDF("repeat", UDFRepeat.class, false); registerUDF("ascii", UDFAscii.class, false); Index: ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstringIndex.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstringIndex.java (revision 0) +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstringIndex.java (revision 0) @@ -0,0 +1,83 @@ +/** + * 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.hive.ql.exec.description; +import org.apache.hadoop.io.IntWritable; +import org.apache.hadoop.io.Text; + +@description( + name = "substring_index", + value = "_FUNC(str, delim, count) - returns the substring of str before count" + + " occurrences of delim.", + extended = "If count is positive, counting is done from the left, and everything" + + " to the left of the final delimiter is returned. If count is negative, counting is" + + " done from the right and everything to the right of the final delimted is returned." + + " If delim can't be found count times, the input string is returned" + ) +public class UDFSubstringIndex extends UDF { + final Text r = new Text(); + + public Text evaluate(final Text _input, final Text _delim, final IntWritable _pos) { + if ((_input == null) || (_delim == null) || (_pos == null)) { + return null; + } + + final int pos = _pos.get(); + final String input = _input.toString(); + final String delim = _delim.toString(); + + r.clear(); + + if (pos == 0 || delim.equals("")) { + return r; + } + + int count = 0; + + if (pos > 0) { + int k = -1; + + while (count++ < pos) { + k = input.indexOf(delim, k + 1); + if (k < 0) { + r.set(input); + return r; + } + } + + r.set(input.substring(0, k)); + return r; + } + + int k = input.length() + 1; + + while (count-- > pos) { + k = input.lastIndexOf(delim, k - 1); + if (k < 0) { + r.set(input); + return r; + } + } + + r.set(input.substring(k + 1)); + return r; + } +}