PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 2ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.udf;
  19. import org.apache.hadoop.hive.ql.exec.Description;
  20. import org.apache.hadoop.hive.ql.exec.UDF;
  21. import org.apache.hadoop.io.IntWritable;
  22. import org.apache.hadoop.io.Text;
  23. /**
  24. * UDFFindInSet.
  25. *
  26. */
  27. @Description(name = "find_in_set", value = "_FUNC_(str,str_array) - Returns the first occurrence "
  28. + " of str in str_array where str_array is a comma-delimited string."
  29. + " Returns null if either argument is null."
  30. + " Returns 0 if the first argument has any commas.", extended = "Example:\n"
  31. + " > SELECT _FUNC_('ab','abc,b,ab,c,def') FROM src LIMIT 1;\n"
  32. + " 3\n"
  33. + " > SELECT * FROM src1 WHERE NOT _FUNC_(key,'311,128,345,956')=0;\n"
  34. + " 311 val_311\n" + " 128"
  35. )
  36. public class UDFFindInSet extends UDF {
  37. private final IntWritable result = new IntWritable();
  38. public IntWritable evaluate(Text s, Text txtarray) {
  39. if (s == null || txtarray == null) {
  40. return null;
  41. }
  42. byte[] search_bytes = s.getBytes();
  43. for (int i = 0; i < s.getLength(); i++) {
  44. if (search_bytes[i] == ',') {
  45. result.set(0);
  46. return result;
  47. }
  48. }
  49. byte[] data = txtarray.getBytes();
  50. int search_length = s.getLength();
  51. int cur_pos_in_array = 0;
  52. int cur_length = 0;
  53. boolean matching = true;
  54. for (int i = 0; i < txtarray.getLength(); i++) {
  55. if (data[i] == ',') {
  56. cur_pos_in_array++;
  57. if (matching && cur_length == search_length) {
  58. result.set(cur_pos_in_array);
  59. return result;
  60. } else {
  61. matching = true;
  62. cur_length = 0;
  63. }
  64. } else {
  65. if (cur_length + 1 <= search_length) {
  66. if (!matching || search_bytes[cur_length] != data[i]) {
  67. matching = false;
  68. }
  69. } else {
  70. matching = false;
  71. }
  72. cur_length++;
  73. }
  74. }
  75. if (matching && cur_length == search_length) {
  76. cur_pos_in_array++;
  77. result.set(cur_pos_in_array);
  78. return result;
  79. } else {
  80. result.set(0);
  81. return result;
  82. }
  83. }
  84. }