hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hashut...@apache.org
Subject svn commit: r1580538 - in /hive/trunk/ql/src: java/org/apache/hadoop/hive/ql/udf/generic/ test/org/apache/hadoop/hive/ql/udf/
Date Sun, 23 Mar 2014 17:04:48 GMT
Author: hashutosh
Date: Sun Mar 23 17:04:48 2014
New Revision: 1580538

URL: http://svn.apache.org/r1580538
Log:
HIVE-6704 : date_add()/date_sub()/datediff() fail with NPE with null input (Jason Dere via
Ashutosh Chauhan)

Modified:
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java
    hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateAdd.java
    hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateDiff.java
    hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateSub.java

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java?rev=1580538&r1=1580537&r2=1580538&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java Sun
Mar 23 17:04:48 2014
@@ -96,6 +96,9 @@ public class GenericUDFDateAdd extends G
     ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
     switch (inputType1) {
     case STRING:
+    case VARCHAR:
+    case CHAR:
+      inputType1 = PrimitiveCategory.STRING;
       textConverter = ObjectInspectorConverters.getConverter(
         (PrimitiveObjectInspector) arguments[0],
         PrimitiveObjectInspectorFactory.writableStringObjectInspector);
@@ -129,7 +132,14 @@ public class GenericUDFDateAdd extends G
   @Override
   public Object evaluate(DeferredObject[] arguments) throws HiveException {
 
+    if (arguments[0].get() == null) {
+      return null;
+    }
     IntWritable toBeAdded = (IntWritable) intWritableConverter.convert(arguments[1].get());
+    if (toBeAdded == null) {
+      return null;
+    }
+
     switch (inputType1) {
     case STRING:
       String dateString = textConverter.convert(arguments[0].get()).toString();

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java?rev=1580538&r1=1580537&r2=1580538&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java Sun
Mar 23 17:04:48 2014
@@ -113,9 +113,14 @@ public class GenericUDFDateDiff extends 
     throws HiveException {
     assert(converter != null);
     assert(argument != null);
+    if (argument.get() == null) {
+      return null;
+    }
     Date date = new Date();
     switch (inputType) {
     case STRING:
+    case VARCHAR:
+    case CHAR:
       String dateString = converter.convert(argument.get()).toString();
       try {
         date = formatter.parse(dateString);
@@ -149,6 +154,8 @@ public class GenericUDFDateDiff extends 
     Converter converter;
     switch (inputType) {
     case STRING:
+    case VARCHAR:
+    case CHAR:
       converter = ObjectInspectorConverters.getConverter(
         (PrimitiveObjectInspector) arguments[i],
         PrimitiveObjectInspectorFactory.writableStringObjectInspector);

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java?rev=1580538&r1=1580537&r2=1580538&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java Sun
Mar 23 17:04:48 2014
@@ -96,6 +96,9 @@ public class GenericUDFDateSub extends G
     ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
     switch (inputType1) {
     case STRING:
+    case VARCHAR:
+    case CHAR:
+      inputType1 = PrimitiveCategory.STRING;
       textConverter = ObjectInspectorConverters.getConverter(
         (PrimitiveObjectInspector) arguments[0],
         PrimitiveObjectInspectorFactory.writableStringObjectInspector);
@@ -129,7 +132,14 @@ public class GenericUDFDateSub extends G
   @Override
   public Object evaluate(DeferredObject[] arguments) throws HiveException {
 
+    if (arguments[0].get() == null) {
+      return null;
+    }
     IntWritable toBeSubed = (IntWritable) intWritableConverter.convert(arguments[1].get());
+    if (toBeSubed == null) {
+      return null;
+    }
+
     switch (inputType1) {
     case STRING:
       String dateString = textConverter.convert(arguments[0].get()).toString();

Modified: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateAdd.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateAdd.java?rev=1580538&r1=1580537&r2=1580538&view=diff
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateAdd.java (original)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateAdd.java Sun Mar
23 17:04:48 2014
@@ -47,6 +47,16 @@ public class TestGenericUDFDateAdd exten
     Text output = (Text) udf.evaluate(args);
 
     assertEquals("date_add() test for STRING failed ", "2009-07-22", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
   public void testTimestampToDate() throws HiveException {
@@ -63,6 +73,16 @@ public class TestGenericUDFDateAdd exten
     Text output = (Text) udf.evaluate(args);
 
     assertEquals("date_add() test for TIMESTAMP failed ", "2009-07-23", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
   public void testDateWritablepToDate() throws HiveException {
@@ -79,6 +99,16 @@ public class TestGenericUDFDateAdd exten
     Text output = (Text) udf.evaluate(args);
 
     assertEquals("date_add() test for DATEWRITABLE failed ", "2009-07-24", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
 }

Modified: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateDiff.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateDiff.java?rev=1580538&r1=1580537&r2=1580538&view=diff
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateDiff.java (original)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateDiff.java Sun Mar
23 17:04:48 2014
@@ -48,6 +48,16 @@ public class TestGenericUDFDateDiff exte
     IntWritable output = (IntWritable) udf.evaluate(args);
 
     assertEquals("date_iff() test for STRING failed ", "-2", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
   public void testTimestampToDate() throws HiveException {
@@ -65,6 +75,16 @@ public class TestGenericUDFDateDiff exte
     IntWritable output = (IntWritable) udf.evaluate(args);
 
     assertEquals("datediff() test for TIMESTAMP failed ", "3", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
   public void testDateWritablepToDate() throws HiveException {
@@ -81,6 +101,16 @@ public class TestGenericUDFDateDiff exte
     IntWritable output = (IntWritable) udf.evaluate(args);
 
     assertEquals("datediff() test for DATEWRITABLE failed ", "10", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
 }

Modified: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateSub.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateSub.java?rev=1580538&r1=1580537&r2=1580538&view=diff
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateSub.java (original)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDateSub.java Sun Mar
23 17:04:48 2014
@@ -47,6 +47,16 @@ public class TestGenericUDFDateSub exten
     Text output = (Text) udf.evaluate(args);
 
     assertEquals("date_sub() test for STRING failed ", "2009-07-18", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
   public void testTimestampToDate() throws HiveException {
@@ -63,6 +73,16 @@ public class TestGenericUDFDateSub exten
     Text output = (Text) udf.evaluate(args);
 
     assertEquals("date_sub() test for TIMESTAMP failed ", "2009-07-17", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
   public void testDateWritablepToDate() throws HiveException {
@@ -79,6 +99,16 @@ public class TestGenericUDFDateSub exten
     Text output = (Text) udf.evaluate(args);
 
     assertEquals("date_sub() test for DATEWRITABLE failed ", "2009-07-16", output.toString());
+
+    // Test with null args
+    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
+    assertNull("date_add() 1st arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
+    assertNull("date_add() 2nd arg null", udf.evaluate(args));
+
+    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null)
};
+    assertNull("date_add() both args null", udf.evaluate(args));
   }
 
 }



Mime
View raw message