You are viewing a plain text version of this content. The canonical link for it is here.
Posted to hcatalog-commits@incubator.apache.org by ga...@apache.org on 2011/05/04 17:59:12 UTC

svn commit: r1099547 - in /incubator/hcatalog/trunk/src: java/org/apache/hcatalog/data/schema/HCatSchema.java test/org/apache/hcatalog/data/schema/TestHCatSchema.java

Author: gates
Date: Wed May  4 17:59:12 2011
New Revision: 1099547

URL: http://svn.apache.org/viewvc?rev=1099547&view=rev
Log:
HCATALOG-17 Shouldn't be able to add an HCatFieldSchema with the same name as existing

Added:
    incubator/hcatalog/trunk/src/test/org/apache/hcatalog/data/schema/TestHCatSchema.java
Modified:
    incubator/hcatalog/trunk/src/java/org/apache/hcatalog/data/schema/HCatSchema.java

Modified: incubator/hcatalog/trunk/src/java/org/apache/hcatalog/data/schema/HCatSchema.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/src/java/org/apache/hcatalog/data/schema/HCatSchema.java?rev=1099547&r1=1099546&r2=1099547&view=diff
==============================================================================
--- incubator/hcatalog/trunk/src/java/org/apache/hcatalog/data/schema/HCatSchema.java (original)
+++ incubator/hcatalog/trunk/src/java/org/apache/hcatalog/data/schema/HCatSchema.java Wed May  4 17:59:12 2011
@@ -41,7 +41,8 @@ public class HCatSchema implements Seria
     /**
      *
      * @param fieldSchemas is now owned by HCatSchema. Any subsequent modifications
-     * on fieldSchemas won't get reflected in HCatSchema.
+     * on fieldSchemas won't get reflected in HCatSchema.  Each fieldSchema's name
+     * in the list must be unique, otherwise throws IllegalArgumentException.
      */
     public HCatSchema(final List<HCatFieldSchema> fieldSchemas){
         this.fieldSchemas = new ArrayList<HCatFieldSchema>(fieldSchemas);
@@ -49,23 +50,29 @@ public class HCatSchema implements Seria
         fieldPositionMap = new HashMap<String,Integer>();
         fieldNames = new ArrayList<String>();
         for (HCatFieldSchema field : fieldSchemas){
-            fieldPositionMap.put(field.getName(), idx);
-            fieldNames.add(field.getName());
+            if(field == null)
+                throw new IllegalArgumentException("Field cannot be null");
+
+            String fieldName = field.getName();
+            if(fieldPositionMap.containsKey(fieldName))
+                throw new IllegalArgumentException("Field named " + fieldName +
+                                                   " already exists");
+            fieldPositionMap.put(fieldName, idx);
+            fieldNames.add(fieldName);
             idx++;
         }
     }
 
     public void append(final HCatFieldSchema hfs) throws HCatException{
-
-      if(hfs == null || fieldSchemas == null){
+      if(hfs == null)
         throw new HCatException("Attempt to append null HCatFieldSchema in HCatSchema.");
-      }
-      //TODO Addition of existing field should not be allowed in Schema.
-      //Need to enforce that. For that to happen, field schema needs to implement Comparable.
-      // Also, HCatSchema needs to implement Comparable.
 
-      this.fieldSchemas.add(hfs);
       String fieldName = hfs.getName();
+      if(fieldPositionMap.containsKey(fieldName))
+        throw new HCatException("Attempt to append HCatFieldSchema with already " +
+            "existing name: " + fieldName + ".");
+
+      this.fieldSchemas.add(hfs);
       this.fieldNames.add(fieldName);
       this.fieldPositionMap.put(fieldName, this.size()-1);
     }

Added: incubator/hcatalog/trunk/src/test/org/apache/hcatalog/data/schema/TestHCatSchema.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/src/test/org/apache/hcatalog/data/schema/TestHCatSchema.java?rev=1099547&view=auto
==============================================================================
--- incubator/hcatalog/trunk/src/test/org/apache/hcatalog/data/schema/TestHCatSchema.java (added)
+++ incubator/hcatalog/trunk/src/test/org/apache/hcatalog/data/schema/TestHCatSchema.java Wed May  4 17:59:12 2011
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hcatalog.data.schema;
+
+import junit.framework.TestCase;
+import org.apache.hcatalog.common.HCatException;
+import org.apache.hcatalog.data.schema.HCatFieldSchema;
+import org.apache.hcatalog.data.schema.HCatSchema;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestHCatSchema extends TestCase {
+  public void testCannotAddFieldMoreThanOnce() throws HCatException {
+    List<HCatFieldSchema> fieldSchemaList = new ArrayList<HCatFieldSchema>();
+    fieldSchemaList.add(new HCatFieldSchema("name", HCatFieldSchema.Type.STRING, "What's your handle?"));
+    fieldSchemaList.add(new HCatFieldSchema("age", HCatFieldSchema.Type.INT, "So very old"));
+
+    HCatSchema schema = new HCatSchema(fieldSchemaList);
+
+    assertTrue(schema.getFieldNames().contains("age"));
+    assertEquals(2, schema.getFields().size());
+
+    try {
+      schema.append(new HCatFieldSchema("age", HCatFieldSchema.Type.INT, "So very old"));
+      fail("Was able to append field schema with same name");
+    } catch(HCatException he) {
+      assertTrue(he.getMessage().contains("Attempt to append HCatFieldSchema with already existing name: age."));
+    }
+
+    assertTrue(schema.getFieldNames().contains("age"));
+    assertEquals(2, schema.getFields().size());
+
+    // Should also not be able to add fields of different types with same name
+    try {
+      schema.append(new HCatFieldSchema("age", HCatFieldSchema.Type.STRING, "Maybe spelled out?"));
+      fail("Was able to append field schema with same name");
+    } catch(HCatException he) {
+      assertTrue(he.getMessage().contains("Attempt to append HCatFieldSchema with already existing name: age."));
+    }
+
+    assertTrue(schema.getFieldNames().contains("age"));
+    assertEquals(2, schema.getFields().size());
+  }
+
+  public void testCannotInstantiateSchemaWithRepeatedFieldNames() throws HCatException {
+      List<HCatFieldSchema> fieldSchemaList = new ArrayList<HCatFieldSchema>();
+
+      fieldSchemaList.add(new HCatFieldSchema("memberID", HCatFieldSchema.Type.INT, "as a number"));
+      fieldSchemaList.add(new HCatFieldSchema("location", HCatFieldSchema.Type.STRING, "there's Waldo"));
+
+      // No duplicate names.  This should be ok
+      HCatSchema schema = new HCatSchema(fieldSchemaList);
+
+      fieldSchemaList.add(new HCatFieldSchema("memberID", HCatFieldSchema.Type.STRING, "as a String"));
+
+      // Now a duplicated field name.  Should fail
+      try {
+        HCatSchema schema2 = new HCatSchema(fieldSchemaList);
+        fail("Able to add duplicate field name");
+      } catch (IllegalArgumentException iae) {
+        assertTrue(iae.getMessage().contains("Field named memberID already exists"));
+      }
+  }
+}