PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFFindInSet.java

#
Java | 98 lines | 63 code | 14 blank | 21 comment | 24 complexity | 0f1a082cc993d4dfb18fcff91a21027c MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.ql.udf;
20
21import org.apache.hadoop.hive.ql.exec.Description;
22import org.apache.hadoop.hive.ql.exec.UDF;
23import org.apache.hadoop.io.IntWritable;
24import org.apache.hadoop.io.Text;
25
26/**
27 * UDFFindInSet.
28 *
29 */
30@Description(name = "find_in_set", value = "_FUNC_(str,str_array) - Returns the first occurrence "
31    + " of str in str_array where str_array is a comma-delimited string."
32    + " Returns null if either argument is null."
33    + " Returns 0 if the first argument has any commas.", extended = "Example:\n"
34    + "  > SELECT _FUNC_('ab','abc,b,ab,c,def') FROM src LIMIT 1;\n"
35    + "  3\n"
36    + "  > SELECT * FROM src1 WHERE NOT _FUNC_(key,'311,128,345,956')=0;\n"
37    + "  311  val_311\n" + "  128"
38
39)
40public class UDFFindInSet extends UDF {
41  private final IntWritable result = new IntWritable();
42
43  public IntWritable evaluate(Text s, Text txtarray) {
44    if (s == null || txtarray == null) {
45      return null;
46    }
47
48    byte[] search_bytes = s.getBytes();
49
50    for (int i = 0; i < s.getLength(); i++) {
51      if (search_bytes[i] == ',') {
52        result.set(0);
53        return result;
54      }
55
56    }
57
58    byte[] data = txtarray.getBytes();
59    int search_length = s.getLength();
60
61    int cur_pos_in_array = 0;
62    int cur_length = 0;
63    boolean matching = true;
64
65    for (int i = 0; i < txtarray.getLength(); i++) {
66      if (data[i] == ',') {
67        cur_pos_in_array++;
68        if (matching && cur_length == search_length) {
69          result.set(cur_pos_in_array);
70          return result;
71        } else {
72          matching = true;
73          cur_length = 0;
74        }
75      } else {
76        if (cur_length + 1 <= search_length) {
77          if (!matching || search_bytes[cur_length] != data[i]) {
78            matching = false;
79          }
80        } else {
81          matching = false;
82        }
83        cur_length++;
84      }
85
86    }
87
88    if (matching && cur_length == search_length) {
89      cur_pos_in_array++;
90      result.set(cur_pos_in_array);
91      return result;
92    } else {
93      result.set(0);
94      return result;
95    }
96  }
97
98}