PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java

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