Author: khorgath
Date: Mon Jun  9 20:28:39 2014
New Revision: 1601499

URL: http://svn.apache.org/r1601499
Log:
JsonSerde raises NullPointerException when object key is not lower case (Yibing 
Shi, Navis Ryu via Sushanth Sowmyan)

Modified:
    
hive/trunk/hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchema.java
    
hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java

Modified: 
hive/trunk/hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchema.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchema.java?rev=1601499&r1=1601498&r2=1601499&view=diff
==============================================================================
--- 
hive/trunk/hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchema.java
 (original)
+++ 
hive/trunk/hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchema.java
 Mon Jun  9 20:28:39 2014
@@ -58,9 +58,9 @@ public class HCatSchema implements Seria
       if (field == null)
         throw new IllegalArgumentException("Field cannot be null");
 
-      String fieldName = field.getName();
+      String fieldName = normalizeName(field.getName());
       if (fieldPositionMap.containsKey(fieldName))
-        throw new IllegalArgumentException("Field named " + fieldName +
+        throw new IllegalArgumentException("Field named " + field.getName() +
           " already exists");
       fieldPositionMap.put(fieldName, idx);
       fieldNames.add(fieldName);
@@ -72,7 +72,7 @@ public class HCatSchema implements Seria
     if (hfs == null)
       throw new HCatException("Attempt to append null HCatFieldSchema in 
HCatSchema.");
 
-    String fieldName = hfs.getName();
+    String fieldName = normalizeName(hfs.getName());
     if (fieldPositionMap.containsKey(fieldName))
       throw new HCatException("Attempt to append HCatFieldSchema with already 
" +
         "existing name: " + fieldName + ".");
@@ -98,7 +98,7 @@ public class HCatSchema implements Seria
    * present, returns null.
    */
   public Integer getPosition(String fieldName) {
-    return fieldPositionMap.get(fieldName);
+    return fieldPositionMap.get(normalizeName(fieldName));
   }
 
   public HCatFieldSchema get(String fieldName) throws HCatException {
@@ -134,9 +134,14 @@ public class HCatSchema implements Seria
     }     
     fieldSchemas.remove(hcatFieldSchema);
     // Re-align the positionMap by -1 for the columns appearing after 
hcatFieldSchema.
-    reAlignPositionMap(fieldPositionMap.get(hcatFieldSchema.getName())+1, -1);
-    fieldPositionMap.remove(hcatFieldSchema.getName());
-    fieldNames.remove(hcatFieldSchema.getName());
+    String fieldName = normalizeName(hcatFieldSchema.getName());
+    reAlignPositionMap(fieldPositionMap.get(fieldName)+1, -1);
+    fieldPositionMap.remove(fieldName);
+    fieldNames.remove(fieldName);
+  }
+
+  private String normalizeName(String name) {
+    return name == null ? null : name.toLowerCase();
   }
 
   @Override

Modified: 
hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java?rev=1601499&r1=1601498&r2=1601499&view=diff
==============================================================================
--- 
hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java
 (original)
+++ 
hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/TestJsonSerDe.java
 Mon Jun  9 20:28:39 2014
@@ -22,6 +22,7 @@ import java.math.BigDecimal;
 import java.sql.Date;
 import java.sql.Timestamp;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -287,4 +288,23 @@ public class TestJsonSerDe extends TestC
 
   }
 
+  public void testUpperCaseKey() throws Exception {
+    Configuration conf = new Configuration();
+    Properties props = new Properties();
+
+    props.put(serdeConstants.LIST_COLUMNS, "empid,name");
+    props.put(serdeConstants.LIST_COLUMN_TYPES, "int,string");
+    JsonSerDe rjsd = new JsonSerDe();
+    SerDeUtils.initializeSerDe(rjsd, conf, props, null);
+
+    Text text1 = new Text("{ \"empId\" : 123, \"name\" : \"John\" } ");
+    Text text2 = new Text("{ \"empId\" : 456, \"name\" : \"Jane\" } ");
+
+    HCatRecord expected1 = new DefaultHCatRecord(Arrays.<Object>asList(123, 
"John"));
+    HCatRecord expected2 = new DefaultHCatRecord(Arrays.<Object>asList(456, 
"Jane"));
+
+    
assertTrue(HCatDataCheckUtil.recordsEqual((HCatRecord)rjsd.deserialize(text1), 
expected1));
+    
assertTrue(HCatDataCheckUtil.recordsEqual((HCatRecord)rjsd.deserialize(text2), 
expected2));
+
+  }
 }


Reply via email to