PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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