PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 77 lines | 43 code | 7 blank | 27 comment | 6 complexity | 7f4586da90e23f6c2cad97b17b7fd496 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.Text;
  22. /**
  23. * UDFUnhex.
  24. *
  25. */
  26. @Description(name = "unhex",
  27. value = "_FUNC_(str) - Converts hexadecimal argument to string",
  28. extended = "Performs the inverse operation of HEX(str). That is, it interprets\n"
  29. + "each pair of hexadecimal digits in the argument as a number and\n"
  30. + "converts it to the character represented by the number. The\n"
  31. + "resulting characters are returned as a binary string.\n\n"
  32. + "Example:\n"
  33. + "> SELECT UNHEX('4D7953514C') from src limit 1;\n"
  34. + "'MySQL'\n"
  35. + "> SELECT UNHEX(HEX('string')) from src limit 1;\n"
  36. + "'string'\n"
  37. + "> SELECT HEX(UNHEX('1267')) from src limit 1;\n"
  38. + "'1267'\n\n"
  39. + "The characters in the argument string must be legal hexadecimal\n"
  40. + "digits: '0' .. '9', 'A' .. 'F', 'a' .. 'f'. If UNHEX() encounters\n"
  41. + "any nonhexadecimal digits in the argument, it returns NULL. Also,\n"
  42. + "if there are an odd number of characters a leading 0 is appended.")
  43. public class UDFUnhex extends UDF {
  44. /**
  45. * Convert every two hex digits in s into.
  46. *
  47. */
  48. public Text evaluate(Text s) {
  49. if (s == null) {
  50. return null;
  51. }
  52. // append a leading 0 if needed
  53. String str;
  54. if (s.getLength() % 2 == 1) {
  55. str = "0" + s.toString();
  56. } else {
  57. str = s.toString();
  58. }
  59. byte[] result = new byte[str.length() / 2];
  60. for (int i = 0; i < str.length(); i += 2) {
  61. try {
  62. result[i / 2] = ((byte) Integer.parseInt(str.substring(i, i + 2), 16));
  63. } catch (NumberFormatException e) {
  64. // invalid character present, return null
  65. return null;
  66. }
  67. }
  68. return new Text(result);
  69. }
  70. }