PageRenderTime 37ms CodeModel.GetById 25ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 173 lines | 81 code | 12 blank | 80 comment | 21 complexity | 15a43467e7428a57e6dee8f1c6794277 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.io.ByteWritable;
 23import org.apache.hadoop.hive.serde2.io.DoubleWritable;
 24import org.apache.hadoop.hive.serde2.io.ShortWritable;
 25import org.apache.hadoop.hive.serde2.lazy.LazyShort;
 26import org.apache.hadoop.io.BooleanWritable;
 27import org.apache.hadoop.io.FloatWritable;
 28import org.apache.hadoop.io.IntWritable;
 29import org.apache.hadoop.io.LongWritable;
 30import org.apache.hadoop.io.NullWritable;
 31import org.apache.hadoop.io.Text;
 32
 33/**
 34 * UDFToShort.
 35 *
 36 */
 37public class UDFToShort extends UDF {
 38  ShortWritable shortWritable = new ShortWritable();
 39
 40  public UDFToShort() {
 41  }
 42
 43  /**
 44   * Convert from void to a short. This is called for CAST(... AS SMALLINT)
 45   * 
 46   * @param i
 47   *          The void value to convert
 48   * @return ShortWritable
 49   */
 50  public ShortWritable evaluate(NullWritable i) {
 51    return null;
 52  }
 53
 54  /**
 55   * Convert from boolean to a short. This is called for CAST(... AS SMALLINT)
 56   * 
 57   * @param i
 58   *          The boolean value to convert
 59   * @return ShortWritable
 60   */
 61  public ShortWritable evaluate(BooleanWritable i) {
 62    if (i == null) {
 63      return null;
 64    } else {
 65      shortWritable.set(i.get() ? (short) 1 : (short) 0);
 66      return shortWritable;
 67    }
 68  }
 69
 70  /**
 71   * Convert from byte to a short. This is called for CAST(... AS SMALLINT)
 72   * 
 73   * @param i
 74   *          The byte value to convert
 75   * @return ShortWritable
 76   */
 77  public ShortWritable evaluate(ByteWritable i) {
 78    if (i == null) {
 79      return null;
 80    } else {
 81      shortWritable.set(i.get());
 82      return shortWritable;
 83    }
 84  }
 85
 86  /**
 87   * Convert from integer to a short. This is called for CAST(... AS SMALLINT)
 88   * 
 89   * @param i
 90   *          The integer value to convert
 91   * @return ShortWritable
 92   */
 93  public ShortWritable evaluate(IntWritable i) {
 94    if (i == null) {
 95      return null;
 96    } else {
 97      shortWritable.set((short) i.get());
 98      return shortWritable;
 99    }
100  }
101
102  /**
103   * Convert from long to a short. This is called for CAST(... AS SMALLINT)
104   * 
105   * @param i
106   *          The long value to convert
107   * @return ShortWritable
108   */
109  public ShortWritable evaluate(LongWritable i) {
110    if (i == null) {
111      return null;
112    } else {
113      shortWritable.set((short) i.get());
114      return shortWritable;
115    }
116  }
117
118  /**
119   * Convert from float to a short. This is called for CAST(... AS SMALLINT)
120   * 
121   * @param i
122   *          The float value to convert
123   * @return ShortWritable
124   */
125  public ShortWritable evaluate(FloatWritable i) {
126    if (i == null) {
127      return null;
128    } else {
129      shortWritable.set((short) i.get());
130      return shortWritable;
131    }
132  }
133
134  /**
135   * Convert from double to a short. This is called for CAST(... AS SMALLINT)
136   * 
137   * @param i
138   *          The double value to convert
139   * @return ShortWritable
140   */
141  public ShortWritable evaluate(DoubleWritable i) {
142    if (i == null) {
143      return null;
144    } else {
145      shortWritable.set((short) i.get());
146      return shortWritable;
147    }
148  }
149
150  /**
151   * Convert from string to a short. This is called for CAST(... AS SMALLINT)
152   * 
153   * @param i
154   *          The string value to convert
155   * @return ShortWritable
156   */
157  public ShortWritable evaluate(Text i) {
158    if (i == null) {
159      return null;
160    } else {
161      try {
162        shortWritable.set(LazyShort.parseShort(i.getBytes(), 0, i.getLength(),
163            10));
164        return shortWritable;
165      } catch (NumberFormatException e) {
166        // MySQL returns 0 if the string is not a well-formed numeric value.
167        // return Byte.valueOf(0);
168        // But we decided to return NULL instead, which is more conservative.
169        return null;
170      }
171    }
172  }
173}