PageRenderTime 37ms CodeModel.GetById 11ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 125 lines | 90 code | 14 blank | 21 comment | 21 complexity | a1d5be28bd44edbf67e108521c8c0d08 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.UDF;
 22import org.apache.hadoop.hive.serde2.ByteStream;
 23import org.apache.hadoop.hive.serde2.io.ByteWritable;
 24import org.apache.hadoop.hive.serde2.io.DoubleWritable;
 25import org.apache.hadoop.hive.serde2.io.ShortWritable;
 26import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
 27import org.apache.hadoop.hive.serde2.lazy.LazyLong;
 28import org.apache.hadoop.io.BooleanWritable;
 29import org.apache.hadoop.io.FloatWritable;
 30import org.apache.hadoop.io.IntWritable;
 31import org.apache.hadoop.io.LongWritable;
 32import org.apache.hadoop.io.NullWritable;
 33import org.apache.hadoop.io.Text;
 34
 35/**
 36 * UDFToString.
 37 *
 38 */
 39public class UDFToString extends UDF {
 40  private Text t = new Text();
 41  private ByteStream.Output out = new ByteStream.Output();
 42
 43  public UDFToString() {
 44  }
 45
 46  public Text evaluate(NullWritable i) {
 47    return null;
 48  }
 49
 50  private byte[] trueBytes = {'T', 'R', 'U', 'E'};
 51  private byte[] falseBytes = {'F', 'A', 'L', 'S', 'E'};
 52
 53  public Text evaluate(BooleanWritable i) {
 54    if (i == null) {
 55      return null;
 56    } else {
 57      t.clear();
 58      t.set(i.get() ? trueBytes : falseBytes);
 59      return t;
 60    }
 61  }
 62
 63  public Text evaluate(ByteWritable i) {
 64    if (i == null) {
 65      return null;
 66    } else {
 67      out.reset();
 68      LazyInteger.writeUTF8NoException(out, i.get());
 69      t.set(out.getData(), 0, out.getCount());
 70      return t;
 71    }
 72  }
 73
 74  public Text evaluate(ShortWritable i) {
 75    if (i == null) {
 76      return null;
 77    } else {
 78      out.reset();
 79      LazyInteger.writeUTF8NoException(out, i.get());
 80      t.set(out.getData(), 0, out.getCount());
 81      return t;
 82    }
 83  }
 84
 85  public Text evaluate(IntWritable i) {
 86    if (i == null) {
 87      return null;
 88    } else {
 89      out.reset();
 90      LazyInteger.writeUTF8NoException(out, i.get());
 91      t.set(out.getData(), 0, out.getCount());
 92      return t;
 93    }
 94  }
 95
 96  public Text evaluate(LongWritable i) {
 97    if (i == null) {
 98      return null;
 99    } else {
100      out.reset();
101      LazyLong.writeUTF8NoException(out, i.get());
102      t.set(out.getData(), 0, out.getCount());
103      return t;
104    }
105  }
106
107  public Text evaluate(FloatWritable i) {
108    if (i == null) {
109      return null;
110    } else {
111      t.set(i.toString());
112      return t;
113    }
114  }
115
116  public Text evaluate(DoubleWritable i) {
117    if (i == null) {
118      return null;
119    } else {
120      t.set(i.toString());
121      return t;
122    }
123  }
124
125}