You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by jd...@apache.org on 2014/12/01 23:54:47 UTC

svn commit: r1642778 - in /hive/trunk/ql/src: java/org/apache/hadoop/hive/ql/udf/generic/ test/org/apache/hadoop/hive/ql/udf/generic/ test/results/clientpositive/ test/results/clientpositive/tez/

Author: jdere
Date: Mon Dec  1 22:54:46 2014
New Revision: 1642778

URL: http://svn.apache.org/r1642778
Log:
HIVE-6421: abs() should preserve precision/scale of decimal input (Jason Dere, reviewed by Ashutosh Chauhan)

Modified:
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java
    hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java
    hive/trunk/ql/src/test/results/clientpositive/decimal_udf.q.out
    hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_math_funcs.q.out
    hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_udf.q.out
    hive/trunk/ql/src/test/results/clientpositive/vector_decimal_math_funcs.q.out
    hive/trunk/ql/src/test/results/clientpositive/vector_decimal_udf.q.out

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java Mon Dec  1 22:54:46 2014
@@ -28,12 +28,14 @@ import org.apache.hadoop.hive.ql.exec.ve
 import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncAbsLongToLong;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
 import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
 import org.apache.hadoop.io.IntWritable;
@@ -55,6 +57,7 @@ public class GenericUDFAbs extends Gener
   private final DoubleWritable resultDouble = new DoubleWritable();
   private final LongWritable resultLong = new LongWritable();
   private final IntWritable resultInt = new IntWritable();
+  private final HiveDecimalWritable resultDecimal = new HiveDecimalWritable();
   private transient PrimitiveObjectInspector argumentOI;
   private transient Converter inputConverter;
 
@@ -94,9 +97,10 @@ public class GenericUDFAbs extends Gener
       outputOI = PrimitiveObjectInspectorFactory.writableDoubleObjectInspector;
       break;
     case DECIMAL:
+      outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(
+          ((PrimitiveObjectInspector) arguments[0]).getTypeInfo());
       inputConverter = ObjectInspectorConverters.getConverter(arguments[0],
-          PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector);
-      outputOI = PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector;
+          outputOI);
       break;
     default:
       throw new UDFArgumentException(
@@ -129,11 +133,15 @@ public class GenericUDFAbs extends Gener
       resultDouble.set(Math.abs(((DoubleWritable) valObject).get()));
       return resultDouble;
     case DECIMAL:
-      return PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector.set(
-          PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector
-              .create(HiveDecimal.ZERO),
-          PrimitiveObjectInspectorUtils.getHiveDecimal(valObject,
-              argumentOI).abs());
+      HiveDecimalObjectInspector decimalOI =
+          (HiveDecimalObjectInspector) argumentOI;
+      HiveDecimalWritable val = decimalOI.getPrimitiveWritableObject(valObject);
+
+      if (val != null) {
+        resultDecimal.set(val.getHiveDecimal().abs());
+        val = resultDecimal;
+      }
+      return val;
     default:
       throw new UDFArgumentException(
           "ABS only takes SHORT/BYTE/INT/LONG/DOUBLE/FLOAT/STRING/DECIMAL types, got " + inputType);

Modified: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java (original)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java Mon Dec  1 22:54:46 2014
@@ -28,7 +28,9 @@ import org.apache.hadoop.hive.ql.udf.gen
 import org.apache.hadoop.hive.serde2.io.DoubleWritable;
 import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
 import org.apache.hadoop.io.FloatWritable;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.LongWritable;
@@ -135,10 +137,17 @@ public class TestGenericUDFAbs extends T
 
   public void testHiveDecimal() throws HiveException {
     GenericUDFAbs udf = new GenericUDFAbs();
-    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector;
+    int prec = 12;
+    int scale = 9;
+    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(
+        TypeInfoFactory.getDecimalTypeInfo(prec, scale));
     ObjectInspector[] arguments = {valueOI};
 
-    udf.initialize(arguments);
+    PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(arguments);
+    // Make sure result precision/scale matches the input prec/scale
+    assertEquals("result precision for abs()", prec, outputOI.precision());
+    assertEquals("result scale for abs()", scale, outputOI.scale());
+
     DeferredObject valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create(
         "107.123456789")));
     DeferredObject[] args = {valueObj};
@@ -153,5 +162,15 @@ public class TestGenericUDFAbs extends T
 
     assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal()
         .doubleValue());
+
+    // null input
+    args[0] = new DeferredJavaObject(null);
+    output = (HiveDecimalWritable) udf.evaluate(args);
+    assertEquals("abs(null)", null, output);
+
+    // if value too large, should also be null
+    args[0] = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create("-1000.123456")));
+    output = (HiveDecimalWritable) udf.evaluate(args);
+    assertEquals("abs() of too large decimal value", null, output);
   }
 }

Modified: hive/trunk/ql/src/test/results/clientpositive/decimal_udf.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientpositive/decimal_udf.q.out?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientpositive/decimal_udf.q.out (original)
+++ hive/trunk/ql/src/test/results/clientpositive/decimal_udf.q.out Mon Dec  1 22:54:46 2014
@@ -1220,7 +1220,7 @@ STAGE PLANS:
           alias: decimal_udf
           Statistics: Num rows: 3 Data size: 359 Basic stats: COMPLETE Column stats: NONE
           Select Operator
-            expressions: abs(key) (type: decimal(38,18))
+            expressions: abs(key) (type: decimal(20,10))
             outputColumnNames: _col0
             Statistics: Num rows: 3 Data size: 359 Basic stats: COMPLETE Column stats: NONE
             ListSink

Modified: hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_math_funcs.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_math_funcs.q.out?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_math_funcs.q.out (original)
+++ hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_math_funcs.q.out Mon Dec  1 22:54:46 2014
@@ -99,7 +99,7 @@ STAGE PLANS:
           Filter Operator
             predicate: (((cbigint % 500) = 0) and (sin(cdecimal1) >= -1.0)) (type: boolean)
             Select Operator
-              expressions: cdecimal1 (type: decimal(20,10)), round(cdecimal1, 2) (type: decimal(13,2)), round(cdecimal1) (type: decimal(11,0)), floor(cdecimal1) (type: decimal(11,0)), ceil(cdecimal1) (type: decimal(11,0)), round(exp(cdecimal1), 58) (type: double), ln(cdecimal1) (type: double), log10(cdecimal1) (type: double), log2(cdecimal1) (type: double), log2((cdecimal1 - 15601.0)) (type: double), log(2.0, cdecimal1) (type: double), power(log2(cdecimal1), 2.0) (type: double), power(log2(cdecimal1), 2.0) (type: double), sqrt(cdecimal1) (type: double), abs(cdecimal1) (type: decimal(38,18)), sin(cdecimal1) (type: double), asin(cdecimal1) (type: double), cos(cdecimal1) (type: double), acos(cdecimal1) (type: double), atan(cdecimal1) (type: double), degrees(cdecimal1) (type: double), radians(cdecimal1) (type: double), cdecimal1 (type: decimal(20,10)), (- cdecimal1) (type: decimal(20,10)), sign(cdecimal1) (type: int), cos(((- sin(log(cdecimal1))) + 3.14159)) (type: double)
+              expressions: cdecimal1 (type: decimal(20,10)), round(cdecimal1, 2) (type: decimal(13,2)), round(cdecimal1) (type: decimal(11,0)), floor(cdecimal1) (type: decimal(11,0)), ceil(cdecimal1) (type: decimal(11,0)), round(exp(cdecimal1), 58) (type: double), ln(cdecimal1) (type: double), log10(cdecimal1) (type: double), log2(cdecimal1) (type: double), log2((cdecimal1 - 15601.0)) (type: double), log(2.0, cdecimal1) (type: double), power(log2(cdecimal1), 2.0) (type: double), power(log2(cdecimal1), 2.0) (type: double), sqrt(cdecimal1) (type: double), abs(cdecimal1) (type: decimal(20,10)), sin(cdecimal1) (type: double), asin(cdecimal1) (type: double), cos(cdecimal1) (type: double), acos(cdecimal1) (type: double), atan(cdecimal1) (type: double), degrees(cdecimal1) (type: double), radians(cdecimal1) (type: double), cdecimal1 (type: decimal(20,10)), (- cdecimal1) (type: decimal(20,10)), sign(cdecimal1) (type: int), cos(((- sin(log(cdecimal1))) + 3.14159)) (type: double)
               outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6, _col7, _col8, _col9, _col10, _col11, _col12, _col13, _col14, _col15, _col16, _col17, _col18, _col19, _col20, _col21, _col22, _col23, _col24, _col25
               ListSink
 

Modified: hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_udf.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_udf.q.out?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_udf.q.out (original)
+++ hive/trunk/ql/src/test/results/clientpositive/tez/vector_decimal_udf.q.out Mon Dec  1 22:54:46 2014
@@ -1546,7 +1546,7 @@ STAGE PLANS:
                   alias: decimal_udf
                   Statistics: Num rows: 38 Data size: 4296 Basic stats: COMPLETE Column stats: NONE
                   Select Operator
-                    expressions: abs(key) (type: decimal(38,18))
+                    expressions: abs(key) (type: decimal(20,10))
                     outputColumnNames: _col0
                     Statistics: Num rows: 38 Data size: 4296 Basic stats: COMPLETE Column stats: NONE
                     File Output Operator

Modified: hive/trunk/ql/src/test/results/clientpositive/vector_decimal_math_funcs.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientpositive/vector_decimal_math_funcs.q.out?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientpositive/vector_decimal_math_funcs.q.out (original)
+++ hive/trunk/ql/src/test/results/clientpositive/vector_decimal_math_funcs.q.out Mon Dec  1 22:54:46 2014
@@ -101,7 +101,7 @@ STAGE PLANS:
               predicate: (((cbigint % 500) = 0) and (sin(cdecimal1) >= -1.0)) (type: boolean)
               Statistics: Num rows: 2048 Data size: 366958 Basic stats: COMPLETE Column stats: NONE
               Select Operator
-                expressions: cdecimal1 (type: decimal(20,10)), round(cdecimal1, 2) (type: decimal(13,2)), round(cdecimal1) (type: decimal(11,0)), floor(cdecimal1) (type: decimal(11,0)), ceil(cdecimal1) (type: decimal(11,0)), round(exp(cdecimal1), 58) (type: double), ln(cdecimal1) (type: double), log10(cdecimal1) (type: double), log2(cdecimal1) (type: double), log2((cdecimal1 - 15601.0)) (type: double), log(2.0, cdecimal1) (type: double), power(log2(cdecimal1), 2.0) (type: double), power(log2(cdecimal1), 2.0) (type: double), sqrt(cdecimal1) (type: double), abs(cdecimal1) (type: decimal(38,18)), sin(cdecimal1) (type: double), asin(cdecimal1) (type: double), cos(cdecimal1) (type: double), acos(cdecimal1) (type: double), atan(cdecimal1) (type: double), degrees(cdecimal1) (type: double), radians(cdecimal1) (type: double), cdecimal1 (type: decimal(20,10)), (- cdecimal1) (type: decimal(20,10)), sign(cdecimal1) (type: int), cos(((- sin(log(cdecimal1))) + 3.14159)) (type: double)
+                expressions: cdecimal1 (type: decimal(20,10)), round(cdecimal1, 2) (type: decimal(13,2)), round(cdecimal1) (type: decimal(11,0)), floor(cdecimal1) (type: decimal(11,0)), ceil(cdecimal1) (type: decimal(11,0)), round(exp(cdecimal1), 58) (type: double), ln(cdecimal1) (type: double), log10(cdecimal1) (type: double), log2(cdecimal1) (type: double), log2((cdecimal1 - 15601.0)) (type: double), log(2.0, cdecimal1) (type: double), power(log2(cdecimal1), 2.0) (type: double), power(log2(cdecimal1), 2.0) (type: double), sqrt(cdecimal1) (type: double), abs(cdecimal1) (type: decimal(20,10)), sin(cdecimal1) (type: double), asin(cdecimal1) (type: double), cos(cdecimal1) (type: double), acos(cdecimal1) (type: double), atan(cdecimal1) (type: double), degrees(cdecimal1) (type: double), radians(cdecimal1) (type: double), cdecimal1 (type: decimal(20,10)), (- cdecimal1) (type: decimal(20,10)), sign(cdecimal1) (type: int), cos(((- sin(log(cdecimal1))) + 3.14159)) (type: double)
                 outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6, _col7, _col8, _col9, _col10, _col11, _col12, _col13, _col14, _col15, _col16, _col17, _col18, _col19, _col20, _col21, _col22, _col23, _col24, _col25
                 Statistics: Num rows: 2048 Data size: 366958 Basic stats: COMPLETE Column stats: NONE
                 File Output Operator

Modified: hive/trunk/ql/src/test/results/clientpositive/vector_decimal_udf.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientpositive/vector_decimal_udf.q.out?rev=1642778&r1=1642777&r2=1642778&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientpositive/vector_decimal_udf.q.out (original)
+++ hive/trunk/ql/src/test/results/clientpositive/vector_decimal_udf.q.out Mon Dec  1 22:54:46 2014
@@ -1486,7 +1486,7 @@ STAGE PLANS:
             alias: decimal_udf
             Statistics: Num rows: 38 Data size: 4296 Basic stats: COMPLETE Column stats: NONE
             Select Operator
-              expressions: abs(key) (type: decimal(38,18))
+              expressions: abs(key) (type: decimal(20,10))
               outputColumnNames: _col0
               Statistics: Num rows: 38 Data size: 4296 Basic stats: COMPLETE Column stats: NONE
               File Output Operator