FAQ
Author: xuefu
Date: Tue Mar 25 20:26:41 2014
New Revision: 1581496

URL: http://svn.apache.org/r1581496
Log:
HIVE-6510: Clean up math based UDFs (Lars via Xuefu)

Modified:
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAcos.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAsin.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAtan.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFBaseBitOP.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFCos.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDegrees.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFExp.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLn.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog10.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog2.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMath.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRadians.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSin.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSqrt.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFTan.java

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAcos.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAcos.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAcos.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAcos.java Tue Mar 25 20:26:41 2014
@@ -26,29 +26,26 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFAcos.
- *
   */
-@Description(name = "acos", value = "_FUNC_(x) - returns the arc cosine of x if -1<=x<=1 or "
- + "NULL otherwise", extended = "Example:\n"
- + " > SELECT _FUNC_(1) FROM src LIMIT 1;\n" + " 0\n"
- + " > SELECT _FUNC_(2) FROM src LIMIT 1;\n" + " NULL")
+@Description(name = "acos",
+ value = "_FUNC_(x) - returns the arc cosine of x if -1<=x<=1 or " + "NULL otherwise",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(1) FROM src LIMIT 1;\n"
+ + " 0\n"
+ + " > SELECT _FUNC_(2) FROM src LIMIT 1;\n"
+ + " NULL")
  @VectorizedExpressions({FuncACosLongToDouble.class, FuncACosDoubleToDouble.class})
  public class UDFAcos extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFAcos() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Take Arc Cosine of a in radians.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null) {
- return null;
- } else {
- result.set(Math.acos(a.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.acos(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAsin.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAsin.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAsin.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAsin.java Tue Mar 25 20:26:41 2014
@@ -26,31 +26,26 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFAsin.
- *
   */
  @Description(name = "asin",
- value = "_FUNC_(x) - returns the arc sine of x if -1<=x<=1 or NULL otherwise",
- extended = "Example:\n"
- + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
- + " 0\n"
- + " > SELECT _FUNC_(2) FROM src LIMIT 1;\n" + " NULL")
+ value = "_FUNC_(x) - returns the arc sine of x if -1<=x<=1 or NULL otherwise",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
+ + " 0\n"
+ + " > SELECT _FUNC_(2) FROM src LIMIT 1;\n"
+ + " NULL")
  @VectorizedExpressions({FuncASinLongToDouble.class, FuncASinDoubleToDouble.class})
  public class UDFAsin extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFAsin() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Take Arc Sine of a in radians.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null) {
- return null;
- } else {
- result.set(Math.asin(a.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.asin(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAtan.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAtan.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAtan.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFAtan.java Tue Mar 25 20:26:41 2014
@@ -18,39 +18,27 @@

  package org.apache.hadoop.hive.ql.udf;

-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncATanDoubleToDouble;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncATanLongToDouble;
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;

-@Description(
- name = "atan",
- value = "_FUNC_(x) - returns the atan (arctan) of x (x is in radians)",
- extended = "Example:\n " +
- " > SELECT _FUNC_(0) FROM src LIMIT 1;\n" +
- " 0"
- )
+@Description(name = "atan",
+ value = "_FUNC_(x) - returns the atan (arctan) of x (x is in radians)",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
+ + " 0"
+)
  @VectorizedExpressions({FuncATanLongToDouble.class, FuncATanDoubleToDouble.class})
  public class UDFAtan extends UDFMath {

- @SuppressWarnings("unused")
- private static Log LOG = LogFactory.getLog(UDFAtan.class.getName());
+ private final DoubleWritable result = new DoubleWritable();

- DoubleWritable result = new DoubleWritable();
-
- public UDFAtan() {
- }
-
- public DoubleWritable evaluate(DoubleWritable x) {
- if (x == null) {
- return null;
- } else {
- result.set(Math.atan(x.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.atan(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFBaseBitOP.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFBaseBitOP.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFBaseBitOP.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFBaseBitOP.java Tue Mar 25 20:26:41 2014
@@ -30,13 +30,6 @@ import org.apache.hadoop.io.LongWritable
   */
  public abstract class UDFBaseBitOP extends UDF {

- /**
- * Constructor.
- */
- public UDFBaseBitOP() {
- super();
- }
-
    protected ByteWritable byteWritable = new ByteWritable();
    protected ShortWritable shortWritable = new ShortWritable();
    protected IntWritable intWritable = new IntWritable();

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFCos.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFCos.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFCos.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFCos.java Tue Mar 25 20:26:41 2014
@@ -26,29 +26,25 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFCos.
- *
   */
  @Description(name = "cos",
- value = "_FUNC_(x) - returns the cosine of x (x is in radians)",
- extended = "Example:\n "
- + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n" + " 1")
+ value = "_FUNC_(x) - returns the cosine of x (x is in radians)",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
+ + " 1"
+)
  @VectorizedExpressions({FuncCosDoubleToDouble.class, FuncCosLongToDouble.class})
  public class UDFCos extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFCos() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Take Cosine of a.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null) {
- return null;
- } else {
- result.set(Math.cos(a.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.cos(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDegrees.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDegrees.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDegrees.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDegrees.java Tue Mar 25 20:26:41 2014
@@ -17,38 +17,27 @@
   */
  package org.apache.hadoop.hive.ql.udf;

-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncDegreesDoubleToDouble;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncDegreesLongToDouble;
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;

-@Description(
- name = "UDFDegrees",
- value = "_FUNC_(x) - Converts radians to degrees",
- extended = "Example:\n" +
- " > SELECT _FUNC_(30) FROM src LIMIT 1;\n" +
- " -1\n"
- )
+@Description(name = "UDFDegrees",
+ value = "_FUNC_(x) - Converts radians to degrees",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(30) FROM src LIMIT 1;\n"
+ + " -1\n"
+)
  @VectorizedExpressions({FuncDegreesLongToDouble.class, FuncDegreesDoubleToDouble.class})
  public class UDFDegrees extends UDFMath {

- @SuppressWarnings("unused")
- private static Log LOG = LogFactory.getLog(UDFDegrees.class.getName());
- DoubleWritable result = new DoubleWritable();
+ private final DoubleWritable result = new DoubleWritable();

- public UDFDegrees() {
- }
-
- public DoubleWritable evaluate(DoubleWritable i) {
- if (i == null) {
- return null;
- } else {
- result.set(Math.toDegrees(i.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.toDegrees(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFExp.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFExp.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFExp.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFExp.java Tue Mar 25 20:26:41 2014
@@ -26,29 +26,25 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFExp.
- *
   */
  @Description(name = "exp",
- value = "_FUNC_(x) - Returns e to the power of x",
- extended = "Example:\n "
- + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n" + " 1")
+ value = "_FUNC_(x) - Returns e to the power of x",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
+ + " 1"
+)
  @VectorizedExpressions({FuncExpDoubleToDouble.class, FuncExpLongToDouble.class})
  public class UDFExp extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFExp() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Raise e (the base of natural logarithm) to the power of a.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null) {
- return null;
- } else {
- result.set(Math.exp(a.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.exp(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLn.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLn.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLn.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLn.java Tue Mar 25 20:26:41 2014
@@ -19,33 +19,30 @@
  package org.apache.hadoop.hive.ql.udf;

  import org.apache.hadoop.hive.ql.exec.Description;
-import org.apache.hadoop.hive.ql.exec.UDF;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncLnDoubleToDouble;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncLnLongToDouble;
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;
-import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;

  /**
   * UDFLn.
- *
   */
  @Description(name = "ln",
- value = "_FUNC_(x) - Returns the natural logarithm of x",
- extended = "Example:\n"
- + " > SELECT _FUNC_(1) FROM src LIMIT 1;\n" + " 0")
+ value = "_FUNC_(x) - Returns the natural logarithm of x",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(1) FROM src LIMIT 1;\n"
+ + " 0")
  @VectorizedExpressions({FuncLnLongToDouble.class, FuncLnDoubleToDouble.class})
  public class UDFLn extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFLn() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Returns the natural logarithm of "a".
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null || a.get() <= 0.0) {
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ if (a.get() <= 0.0) {
        return null;
      } else {
        result.set(Math.log(a.get()));

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog.java Tue Mar 25 20:26:41 2014
@@ -29,31 +29,17 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFLog.
- *
   */
  @Description(name = "log",
- value = "_FUNC_([b], x) - Returns the logarithm of x with base b",
- extended = "Example:\n"
- + " > SELECT _FUNC_(13, 13) FROM src LIMIT 1;\n" + " 1")
+ value = "_FUNC_([b], x) - Returns the logarithm of x with base b",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(13, 13) FROM src LIMIT 1;\n"
+ + " 1")
  @VectorizedExpressions({FuncLogWithBaseLongToDouble.class, FuncLogWithBaseDoubleToDouble.class,
- FuncLnLongToDouble.class, FuncLnDoubleToDouble.class})
+ FuncLnLongToDouble.class, FuncLnDoubleToDouble.class})
  public class UDFLog extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();
-
- public UDFLog() {
- }

- /**
- * Returns the natural logarithm of "a".
- */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null || a.get() <= 0.0) {
- return null;
- } else {
- result.set(Math.log(a.get()));
- return result;
- }
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Returns the logarithm of "a" with base "base".
@@ -65,14 +51,6 @@ public class UDFLog extends UDFMath {
      return log(base.get(), a.get());
    }

- private DoubleWritable log(double base, double input) {
- if( base <= 1.0 || input <= 0.0) {
- return null;
- }
- result.set(Math.log(input) / Math.log(base));
- return result;
- }
-
    /**
     * Get the logarithm of the given decimal with the given base.
     */
@@ -109,4 +87,25 @@ public class UDFLog extends UDFMath {
      return log(base, d);
    }

+ /**
+ * Returns the natural logarithm of "a".
+ */
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ if (a.get() <= 0.0) {
+ return null;
+ } else {
+ result.set(Math.log(a.get()));
+ return result;
+ }
+ }
+
+ private DoubleWritable log(double base, double input) {
+ if (base <= 1.0 || input <= 0.0) {
+ return null;
+ }
+ result.set(Math.log(input) / Math.log(base));
+ return result;
+ }
+
  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog10.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog10.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog10.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog10.java Tue Mar 25 20:26:41 2014
@@ -26,24 +26,23 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFLog10.
- *
   */
  @Description(name = "log10",
- value = "_FUNC_(x) - Returns the logarithm of x with base 10",
- extended = "Example:\n"
- + " > SELECT _FUNC_(10) FROM src LIMIT 1;\n" + " 1")
+ value = "_FUNC_(x) - Returns the logarithm of x with base 10",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(10) FROM src LIMIT 1;\n"
+ + " 1")
  @VectorizedExpressions({FuncLog10LongToDouble.class, FuncLog10DoubleToDouble.class})
  public class UDFLog10 extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFLog10() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Returns the logarithm of "a" with base 10.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null || a.get() <= 0.0) {
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ if (a.get() <= 0.0) {
        return null;
      } else {
        result.set(Math.log10(a.get()));

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog2.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog2.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog2.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLog2.java Tue Mar 25 20:26:41 2014
@@ -26,29 +26,28 @@ import org.apache.hadoop.hive.serde2.io.

  /**
   * UDFLog2.
- *
   */
  @Description(name = "log2",
- value = "_FUNC_(x) - Returns the logarithm of x with base 2",
- extended = "Example:\n"
- + " > SELECT _FUNC_(2) FROM src LIMIT 1;\n" + " 1")
+ value = "_FUNC_(x) - Returns the logarithm of x with base 2",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(2) FROM src LIMIT 1;\n"
+ + " 1")
  @VectorizedExpressions({FuncLog2LongToDouble.class, FuncLog2DoubleToDouble.class})
  public class UDFLog2 extends UDFMath {
- private static double log2 = Math.log(2.0);

- private final DoubleWritable result = new DoubleWritable();
+ private static final double LOG_2 = Math.log(2.0);

- public UDFLog2() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Returns the logarithm of "a" with base 2.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null || a.get() <= 0.0) {
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ if (a.get() <= 0.0) {
        return null;
      } else {
- result.set(Math.log(a.get()) / log2);
+ result.set(Math.log(a.get()) / LOG_2);
        return result;
      }
    }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMath.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMath.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMath.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMath.java Tue Mar 25 20:26:41 2014
@@ -22,16 +22,29 @@ import org.apache.hadoop.hive.ql.exec.UD
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;
  import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;

+/**
+ * This class can be used for math based UDFs that only have an evaluate method for {@code doubles}. By extending from
+ * this class these UDFs will automatically support decimals as well.
+ */
  public abstract class UDFMath extends UDF {
- private final DoubleWritable doubleWritable = new DoubleWritable();

- public UDFMath() {
- }
+ private final DoubleWritable doubleWritable = new DoubleWritable();

    /**
     * For subclass to implement.
     */
- public abstract DoubleWritable evaluate(DoubleWritable a);
+ protected abstract DoubleWritable doEvaluate(DoubleWritable a);
+
+ /**
+ * Returns {@code null} if the passed in value is {@code} and passes on to {@link #doEvaluate(DoubleWritable)} if not.
+ */
+ public final DoubleWritable evaluate(DoubleWritable a) {
+ if (a == null) {
+ return null;
+ }
+
+ return doEvaluate(a);
+ }

    /**
     * Convert HiveDecimal to a double and call evaluate() on it.
@@ -43,7 +56,7 @@ public abstract class UDFMath extends UD

      double d = writable.getHiveDecimal().bigDecimalValue().doubleValue();
      doubleWritable.set(d);
- return evaluate(doubleWritable);
+ return doEvaluate(doubleWritable);
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRadians.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRadians.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRadians.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRadians.java Tue Mar 25 20:26:41 2014
@@ -17,38 +17,27 @@
   */
  package org.apache.hadoop.hive.ql.udf;

-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncRadiansDoubleToDouble;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncRadiansLongToDouble;
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;

-@Description(
- name = "radians",
- value = "_FUNC_(x) - Converts degrees to radians",
- extended = "Example:\n" +
- " > SELECT _FUNC_(90) FROM src LIMIT 1;\n" +
- " 1.5707963267949mo\n"
- )
+@Description(name = "radians",
+ value = "_FUNC_(x) - Converts degrees to radians",
+ extended = "Example:\n"
+ + " > SELECT _FUNC_(90) FROM src LIMIT 1;\n"
+ + " 1.5707963267949mo\n"
+)
  @VectorizedExpressions({FuncRadiansLongToDouble.class, FuncRadiansDoubleToDouble.class})
  public class UDFRadians extends UDFMath {

- @SuppressWarnings("unused")
- private static Log LOG = LogFactory.getLog(UDFRadians.class.getName());
- DoubleWritable result = new DoubleWritable();
+ private final DoubleWritable result = new DoubleWritable();

- public UDFRadians() {
- }
-
- public DoubleWritable evaluate(DoubleWritable i) {
- if (i == null) {
- return null;
- } else {
- result.set(Math.toRadians(i.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.toRadians(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java Tue Mar 25 20:26:41 2014
@@ -18,8 +18,6 @@

  package org.apache.hadoop.hive.ql.udf;

-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.UDF;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
@@ -30,38 +28,30 @@ import org.apache.hadoop.hive.serde2.io.
  import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
  import org.apache.hadoop.io.IntWritable;

-@Description(
- name = "sign",
- value = "_FUNC_(x) - returns the sign of x )",
- extended = "Example:\n " +
- " > SELECT _FUNC_(40) FROM src LIMIT 1;\n" +
- " 1"
- )
+@Description(name = "sign",
+ value = "_FUNC_(x) - returns the sign of x )",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(40) FROM src LIMIT 1;\n"
+ + " 1"
+)
  @VectorizedExpressions({FuncSignLongToDouble.class, FuncSignDoubleToDouble.class, FuncSignDecimalToLong.class})
  public class UDFSign extends UDF {

- @SuppressWarnings("unused")
- private static Log LOG = LogFactory.getLog(UDFSign.class.getName());
- DoubleWritable result = new DoubleWritable();
- IntWritable intWritable = new IntWritable();
-
- public UDFSign() {
- }
+ private final DoubleWritable result = new DoubleWritable();
+ private final IntWritable intWritable = new IntWritable();

    /**
     * Take sign of a
     */
- public DoubleWritable evaluate(DoubleWritable a) {
+ public DoubleWritable evaluate(DoubleWritable a) {
      if (a == null) {
        return null;
      }
- if (a.get()==0) {
+ if (a.get() == 0) {
        result.set(0);
- }
- else if (a.get()>0) {
+ } else if (a.get() > 0) {
        result.set(1);
- }
- else {
+ } else {
        result.set(-1);
      }
      return result;
@@ -69,10 +59,12 @@ public class UDFSign extends UDF {

    /**
     * Get the sign of the decimal input
+ *
     * @param dec decimal input
+ *
     * @return -1, 0, or 1 representing the sign of the input decimal
     */
- public IntWritable evaluate(HiveDecimalWritable dec) {
+ public IntWritable evaluate(HiveDecimalWritable dec) {
      if (dec == null || dec.getHiveDecimal() == null) {
        return null;
      }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSin.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSin.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSin.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSin.java Tue Mar 25 20:26:41 2014
@@ -19,38 +19,31 @@
  package org.apache.hadoop.hive.ql.udf;

  import org.apache.hadoop.hive.ql.exec.Description;
-import org.apache.hadoop.hive.ql.exec.UDF;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncSinDoubleToDouble;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncSinLongToDouble;
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;
-import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;

  /**
   * UDFSin.
- *
   */
  @Description(name = "sin",
- value = "_FUNC_(x) - returns the sine of x (x is in radians)",
- extended = "Example:\n "
- + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n" + " 0")
+ value = "_FUNC_(x) - returns the sine of x (x is in radians)",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
+ + " 0")
  @VectorizedExpressions({FuncSinLongToDouble.class, FuncSinDoubleToDouble.class})
  public class UDFSin extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFSin() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Take Sine of a.
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null) {
- return null;
- } else {
- result.set(Math.sin(a.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.sin(a.get()));
+ return result;
    }

  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSqrt.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSqrt.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSqrt.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSqrt.java Tue Mar 25 20:26:41 2014
@@ -28,26 +28,24 @@ import org.apache.hadoop.hive.serde2.io.
   * Implementation of the SQRT UDF found in many databases.
   */
  @Description(name = "sqrt",
- value = "_FUNC_(x) - returns the square root of x",
- extended = "Example:\n "
- + " > SELECT _FUNC_(4) FROM src LIMIT 1;\n" + " 2")
+ value = "_FUNC_(x) - returns the square root of x",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(4) FROM src LIMIT 1;\n"
+ + " 2")
  @VectorizedExpressions({FuncSqrtLongToDouble.class, FuncSqrtDoubleToDouble.class})
  public class UDFSqrt extends UDFMath {
- private final DoubleWritable result = new DoubleWritable();

- public UDFSqrt() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
- * Return NULL for NULL or negative inputs; otherwise, return the square root.
+ * Return NULL for negative inputs; otherwise, return the square root.
     */
- public DoubleWritable evaluate(DoubleWritable i) {
- if (i == null) {
- return null;
- } else if (i.get() < 0) {
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ if (a.get() < 0) {
        return null;
      } else {
- result.set(Math.sqrt(i.get()));
+ result.set(Math.sqrt(a.get()));
        return result;
      }
    }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFTan.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFTan.java?rev=1581496&r1=1581495&r2=1581496&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFTan.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFTan.java Tue Mar 25 20:26:41 2014
@@ -18,41 +18,30 @@

  package org.apache.hadoop.hive.ql.udf;

-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncTanDoubleToDouble;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncTanLongToDouble;
  import org.apache.hadoop.hive.serde2.io.DoubleWritable;

-@Description(
- name = "tan",
- value = "_FUNC_(x) - returns the tangent of x (x is in radians)",
- extended = "Example:\n " +
- " > SELECT _FUNC_(0) FROM src LIMIT 1;\n" +
- " 1"
- )
+@Description(name = "tan",
+ value = "_FUNC_(x) - returns the tangent of x (x is in radians)",
+ extended = "Example:\n "
+ + " > SELECT _FUNC_(0) FROM src LIMIT 1;\n"
+ + " 1"
+)
  @VectorizedExpressions({FuncTanLongToDouble.class, FuncTanDoubleToDouble.class})
  public class UDFTan extends UDFMath {

- @SuppressWarnings("unused")
- private static Log LOG = LogFactory.getLog(UDFTan.class.getName());
- DoubleWritable result = new DoubleWritable();
-
- public UDFTan() {
- }
+ private final DoubleWritable result = new DoubleWritable();

    /**
     * Take Tangent of a
     */
- public DoubleWritable evaluate(DoubleWritable a) {
- if (a == null) {
- return null;
- } else {
- result.set(Math.tan(a.get()));
- return result;
- }
+ @Override
+ protected DoubleWritable doEvaluate(DoubleWritable a) {
+ result.set(Math.tan(a.get()));
+ return result;
    }

  }

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcommits @
categorieshive, hadoop
postedMar 25, '14 at 8:27p
activeMar 25, '14 at 8:27p
posts1
users1
websitehive.apache.org

1 user in discussion

Xuefu: 1 post

People

Translate

site design / logo © 2021 Grokbase