PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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