PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 116 lines | 61 code | 17 blank | 38 comment | 24 complexity | c7bcf55a3e1b8ca2c666cef3b60fb489 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.serde2.io.ByteWritable;
 23import org.apache.hadoop.hive.serde2.io.DoubleWritable;
 24import org.apache.hadoop.hive.serde2.io.ShortWritable;
 25import org.apache.hadoop.io.FloatWritable;
 26import org.apache.hadoop.io.IntWritable;
 27import org.apache.hadoop.io.LongWritable;
 28
 29/**
 30 * The reason that we list evaluate methods with all numeric types is for both
 31 * better performance and type checking (so we know int + int is still an int
 32 * instead of a double); otherwise a single method that takes (Number a, Number
 33 * b) and use a.doubleValue() == b.doubleValue() is enough.
 34 * 
 35 * The case of int + double will be handled by implicit type casting using
 36 * UDFRegistry.implicitConvertable method.
 37 */
 38@Description(name = "+", value = "a _FUNC_ b - Returns a+b")
 39public class UDFOPPlus extends UDFBaseNumericOp {
 40
 41  public UDFOPPlus() {
 42  }
 43
 44  @Override
 45  public ByteWritable evaluate(ByteWritable a, ByteWritable b) {
 46    // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
 47    // + b);
 48    if ((a == null) || (b == null)) {
 49      return null;
 50    }
 51
 52    byteWritable.set((byte) (a.get() + b.get()));
 53    return byteWritable;
 54  }
 55
 56  @Override
 57  public ShortWritable evaluate(ShortWritable a, ShortWritable b) {
 58    // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
 59    // + b);
 60    if ((a == null) || (b == null)) {
 61      return null;
 62    }
 63
 64    shortWritable.set((short) (a.get() + b.get()));
 65    return shortWritable;
 66  }
 67
 68  @Override
 69  public IntWritable evaluate(IntWritable a, IntWritable b) {
 70    // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
 71    // + b);
 72    if ((a == null) || (b == null)) {
 73      return null;
 74    }
 75
 76    intWritable.set((a.get() + b.get()));
 77    return intWritable;
 78  }
 79
 80  @Override
 81  public LongWritable evaluate(LongWritable a, LongWritable b) {
 82    // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
 83    // + b);
 84    if ((a == null) || (b == null)) {
 85      return null;
 86    }
 87
 88    longWritable.set(a.get() + b.get());
 89    return longWritable;
 90  }
 91
 92  @Override
 93  public FloatWritable evaluate(FloatWritable a, FloatWritable b) {
 94    // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
 95    // + b);
 96    if ((a == null) || (b == null)) {
 97      return null;
 98    }
 99
100    floatWritable.set(a.get() + b.get());
101    return floatWritable;
102  }
103
104  @Override
105  public DoubleWritable evaluate(DoubleWritable a, DoubleWritable b) {
106    // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
107    // + b);
108    if ((a == null) || (b == null)) {
109      return null;
110    }
111
112    doubleWritable.set(a.get() + b.get());
113    return doubleWritable;
114  }
115
116}