PageRenderTime 43ms CodeModel.GetById 16ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.udf;
  19. import org.apache.hadoop.hive.ql.exec.Description;
  20. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  21. import org.apache.hadoop.hive.serde2.io.DoubleWritable;
  22. import org.apache.hadoop.hive.serde2.io.ShortWritable;
  23. import org.apache.hadoop.io.FloatWritable;
  24. import org.apache.hadoop.io.IntWritable;
  25. import org.apache.hadoop.io.LongWritable;
  26. /**
  27. * The reason that we list evaluate methods with all numeric types is for both
  28. * better performance and type checking (so we know int + int is still an int
  29. * instead of a double); otherwise a single method that takes (Number a, Number
  30. * b) and use a.doubleValue() == b.doubleValue() is enough.
  31. *
  32. * The case of int + double will be handled by implicit type casting using
  33. * UDFRegistry.implicitConvertable method.
  34. */
  35. @Description(name = "+", value = "a _FUNC_ b - Returns a+b")
  36. public class UDFOPPlus extends UDFBaseNumericOp {
  37. public UDFOPPlus() {
  38. }
  39. @Override
  40. public ByteWritable evaluate(ByteWritable a, ByteWritable b) {
  41. // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
  42. // + b);
  43. if ((a == null) || (b == null)) {
  44. return null;
  45. }
  46. byteWritable.set((byte) (a.get() + b.get()));
  47. return byteWritable;
  48. }
  49. @Override
  50. public ShortWritable evaluate(ShortWritable a, ShortWritable b) {
  51. // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
  52. // + b);
  53. if ((a == null) || (b == null)) {
  54. return null;
  55. }
  56. shortWritable.set((short) (a.get() + b.get()));
  57. return shortWritable;
  58. }
  59. @Override
  60. public IntWritable evaluate(IntWritable a, IntWritable b) {
  61. // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
  62. // + b);
  63. if ((a == null) || (b == null)) {
  64. return null;
  65. }
  66. intWritable.set((a.get() + b.get()));
  67. return intWritable;
  68. }
  69. @Override
  70. public LongWritable evaluate(LongWritable a, LongWritable b) {
  71. // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
  72. // + b);
  73. if ((a == null) || (b == null)) {
  74. return null;
  75. }
  76. longWritable.set(a.get() + b.get());
  77. return longWritable;
  78. }
  79. @Override
  80. public FloatWritable evaluate(FloatWritable a, FloatWritable b) {
  81. // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
  82. // + b);
  83. if ((a == null) || (b == null)) {
  84. return null;
  85. }
  86. floatWritable.set(a.get() + b.get());
  87. return floatWritable;
  88. }
  89. @Override
  90. public DoubleWritable evaluate(DoubleWritable a, DoubleWritable b) {
  91. // LOG.info("Get input " + a.getClass() + ":" + a + " " + b.getClass() + ":"
  92. // + b);
  93. if ((a == null) || (b == null)) {
  94. return null;
  95. }
  96. doubleWritable.set(a.get() + b.get());
  97. return doubleWritable;
  98. }
  99. }