You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by kh...@apache.org on 2014/04/12 11:11:30 UTC

svn commit: r1586842 - in /hive/trunk/hcatalog/core/src: main/java/org/apache/hive/hcatalog/data/schema/HCatSchema.java test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchema.java

Author: khorgath
Date: Sat Apr 12 09:11:30 2014
New Revision: 1586842

URL: http://svn.apache.org/r1586842
Log:
HIVE-5336 : HCatSchema.remove(HCatFieldSchema hcatFieldSchema) should renumber the fieldPositionMap and the fieldPositionMap should not be cached by the end user (Hari Sankar Sivarama 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/schema/TestHCatSchema.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=1586842&r1=1586841&r2=1586842&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 Sat Apr 12 09:11:30 2014
@@ -91,6 +91,8 @@ public class HCatSchema implements Seria
   }
 
   /**
+   * Note : The position will be re-numbered when one of the preceding columns are removed.
+   * Hence, the client should not cache this value and expect it to be always valid.
    * @param fieldName
    * @return the index of field named fieldName in Schema. If field is not
    * present, returns null.
@@ -115,13 +117,24 @@ public class HCatSchema implements Seria
     return fieldSchemas.size();
   }
 
-  public void remove(final HCatFieldSchema hcatFieldSchema) throws HCatException {
+  private void reAlignPositionMap(int startPosition, int offset) {
+    for (Map.Entry<String, Integer> entry : fieldPositionMap.entrySet()) {
+      // Re-align the columns appearing on or after startPostion(say, column 1) such that
+      // column 2 becomes column (2+offset), column 3 becomes column (3+offset) and so on.
+      Integer entryVal = entry.getValue();
+      if (entryVal >= startPosition) {
+        entry.setValue(entryVal+offset);
+      }
+    }
+  }
 
+  public void remove(final HCatFieldSchema hcatFieldSchema) throws HCatException {
     if (!fieldSchemas.contains(hcatFieldSchema)) {
       throw new HCatException("Attempt to delete a non-existent column from HCat Schema: " + hcatFieldSchema);
-    }
-
+    }     
     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());
   }

Modified: hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchema.java
URL: http://svn.apache.org/viewvc/hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchema.java?rev=1586842&r1=1586841&r2=1586842&view=diff
==============================================================================
--- hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchema.java (original)
+++ hive/trunk/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchema.java Sat Apr 12 09:11:30 2014
@@ -18,12 +18,14 @@
  */
 package org.apache.hive.hcatalog.data.schema;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import junit.framework.TestCase;
+
 import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
 import org.apache.hive.hcatalog.common.HCatException;
 
-import java.util.ArrayList;
-import java.util.List;
 
 public class TestHCatSchema extends TestCase {
   public void testCannotAddFieldMoreThanOnce() throws HCatException {
@@ -109,4 +111,27 @@ public class TestHCatSchema extends Test
       assertFalse(ex.getMessage(), true);
     }
   }
+
+  // HIVE-5336. Re-number the position after remove such that:
+  // (1) getPosition on a column always returns a value between 0..schema.size()-1
+  // (2) getPosition() on 2 different columns should never give the same value.
+  public void testRemoveAddField2() throws HCatException {
+    List<HCatFieldSchema> fieldSchemaList = new ArrayList<HCatFieldSchema>();
+    HCatFieldSchema memberIDField = new HCatFieldSchema("memberID", HCatFieldSchema.Type.INT, "id as number");
+    HCatFieldSchema locationField = new HCatFieldSchema("location", HCatFieldSchema.Type.STRING, "loc as string");
+    HCatFieldSchema memberNameField = new HCatFieldSchema("memberName", HCatFieldSchema.Type.STRING, "name as string");
+    HCatFieldSchema memberSalaryField = new HCatFieldSchema("memberSalary", HCatFieldSchema.Type.INT, "sal as number");
+    fieldSchemaList.add(memberIDField);
+    fieldSchemaList.add(locationField);
+    fieldSchemaList.add(memberNameField);
+    fieldSchemaList.add(memberSalaryField);
+    HCatSchema schema = new HCatSchema(fieldSchemaList);
+    schema.remove(locationField);
+    assertTrue("The position of atleast one of the fields is incorrect" ,
+        schema.getPosition(memberIDField.getName()) == 0 &&
+        schema.getPosition(locationField.getName()) == null &&
+        schema.getPosition(memberNameField.getName()) == 1 &&
+        schema.getPosition(memberSalaryField.getName()) == 2);
+  }
+
 }