diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java index 4ada5e5..7f4a807 100644 --- ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUtils.java @@ -452,7 +452,14 @@ public static String getOrdinal(int i) { */ public static int findText(Text text, Text subtext, int start) { // src.position(start) can't accept negative numbers. - if (start < 0) { + int length = text.getLength() - start; + if (start < 0 || length < 0 || length < subtext.getLength()) { + return -1; + } + if (subtext.getLength() == 0) { + return 0; + } + if (length == 0) { return -1; } diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java new file mode 100644 index 0000000..d9338a5 --- /dev/null +++ ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFUtils.java @@ -0,0 +1,48 @@ +/** + * 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 junit.framework.Assert; +import junit.framework.TestCase; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils; +import org.apache.hadoop.io.Text; +import org.junit.Test; + +public class TestGenericUDFUtils extends TestCase { + + @Test + public void testFindText() throws Exception { + + // http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_locate + Assert.assertEquals(3, GenericUDFUtils.findText(new Text("foobarbar"), new Text("bar"), 0)); + Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobarbar"), new Text("xbar"), 0)); + Assert.assertEquals(6, GenericUDFUtils.findText(new Text("foobarbar"), new Text("bar"), 5)); + + Assert.assertEquals(6, GenericUDFUtils.findText(new Text("foobarbar"), new Text("bar"), 5)); + Assert.assertEquals(6, GenericUDFUtils.findText(new Text("foobarbar"), new Text("bar"), 6)); + Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobarbar"), new Text("bar"), 7)); + Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobarbar"), new Text("bar"), 10)); + + Assert.assertEquals(-1, GenericUDFUtils.findText(new Text(""), new Text("bar"), 0)); + Assert.assertEquals(0, GenericUDFUtils.findText(new Text(""), new Text(""), 0)); + Assert.assertEquals(0, GenericUDFUtils.findText(new Text("foobar"), new Text(""), 0)); + Assert.assertEquals(0, GenericUDFUtils.findText(new Text("foobar"), new Text(""), 6)); + Assert.assertEquals(-1, GenericUDFUtils.findText(new Text("foobar"), new Text(""), 7)); + } +}