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

# · Java · 101 lines · 60 code · 11 blank · 30 comment · 10 complexity · 296fe6a8a0c5a217fdc914e1fa17667f 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. 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.LongWritable;
  23. import org.apache.hadoop.io.Text;
  24. /**
  25. * UDFHex.
  26. *
  27. */
  28. @Description(name = "hex",
  29. value = "_FUNC_(n or str) - Convert the argument to hexadecimal ",
  30. extended = "If the argument is a string, returns two hex digits for each "
  31. + "character in the string.\n"
  32. + "If the argument is a number, returns the hexadecimal representation.\n"
  33. + "Example:\n"
  34. + " > SELECT _FUNC_(17) FROM src LIMIT 1;\n"
  35. + " 'H1'\n"
  36. + " > SELECT _FUNC_('Facebook') FROM src LIMIT 1;\n"
  37. + " '46616365626F6F6B'")
  38. public class UDFHex extends UDF {
  39. private final Text result = new Text();
  40. private byte[] value = new byte[16];
  41. /**
  42. * Convert num to hex.
  43. *
  44. */
  45. private Text evaluate(long num) {
  46. // Extract the hex digits of num into value[] from right to left
  47. int len = 0;
  48. do {
  49. len++;
  50. value[value.length - len] = (byte) Character.toUpperCase(Character
  51. .forDigit((int) (num & 0xF), 16));
  52. num >>>= 4;
  53. } while (num != 0);
  54. result.set(value, value.length - len, len);
  55. return result;
  56. }
  57. public Text evaluate(LongWritable n) {
  58. if (n == null) {
  59. return null;
  60. }
  61. return evaluate(n.get());
  62. }
  63. public Text evaluate(IntWritable n) {
  64. if (n == null) {
  65. return null;
  66. }
  67. return evaluate(n.get());
  68. }
  69. /**
  70. * Convert every character in s to two hex digits.
  71. *
  72. */
  73. public Text evaluate(Text s) {
  74. if (s == null) {
  75. return null;
  76. }
  77. if (value.length < s.getLength() * 2) {
  78. value = new byte[s.getLength() * 2];
  79. }
  80. byte[] str = s.getBytes();
  81. for (int i = 0; i < s.getLength(); i++) {
  82. value[i * 2] = (byte) Character.toUpperCase(Character.forDigit(
  83. (str[i] & 0xF0) >>> 4, 16));
  84. value[i * 2 + 1] = (byte) Character.toUpperCase(Character.forDigit(
  85. str[i] & 0x0F, 16));
  86. }
  87. result.set(value, 0, s.getLength() * 2);
  88. return result;
  89. }
  90. }