added proper error message when trying to partition a dataset by non-existent key; added test case and some documentation

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization@1246 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/file/DatasetOperations.java b/asterix-app/src/main/java/edu/uci/ics/asterix/file/DatasetOperations.java
index 5d3ab0a..075ddd3 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/file/DatasetOperations.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/file/DatasetOperations.java
@@ -174,6 +174,17 @@
             throw new AsterixException("Could not find dataset " + datasetName + " in datavetse " + dataverseName);
         }
         ARecordType itemType = (ARecordType) metadata.findType(dataverseName, dataset.getItemTypeName());
+        for (String keyField : DatasetUtils.getPartitioningKeys(dataset)) {
+            try {
+                if (!itemType.isClosedField(keyField)) {
+                    throw new AsterixException("Cannot partition dataset \"" + dataset.getDatasetName()
+                            + "\" by key \"" + keyField + "\" since it is not a valid field of \""
+                            + itemType.getTypeName() + "\"");
+                }
+            } catch (IOException e) {
+                throw new AsterixException(e);
+            }
+        }
         JobSpecification spec = new JobSpecification();
         IBinaryComparatorFactory[] comparatorFactories = DatasetUtils.computeKeysBinaryComparatorFactories(dataset,
                 itemType, format.getBinaryComparatorFactoryProvider());
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/partition-by-nonexistent-field.aql b/asterix-app/src/test/resources/runtimets/queries/misc/partition-by-nonexistent-field.aql
new file mode 100644
index 0000000..10dbfc1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/partition-by-nonexistent-field.aql
@@ -0,0 +1,20 @@
+/*
+ * Description    : Tries to partition a dataset by a non-existent field
+ * Expected Result: An error reporting that this is not allowed
+ * Author: zheilbron
+ */
+ 
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type TestType as open{
+name1:string
+}
+
+create dataset testds(TestType) primary key id;
+
+insert into dataset testds({"name1":"John","name2":"Smith"});
+
+for $l in dataset('testds')
+return $l
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index de694cd..e8e7d72 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -1963,6 +1963,12 @@
     </test-case>
   </test-group>
   <test-group name="misc">
+  <test-case FilePath="misc">
+      <compilation-unit name="partition-by-nonexistent-field">
+        <output-file compare="Text">partition-by-nonexistent-field.adm</output-file>
+        <expected-error>edu.uci.ics.asterix.common.exceptions.AsterixException</expected-error>
+      </compilation-unit>
+    </test-case>
     <test-case FilePath="misc">
       <compilation-unit name="float_01">
         <output-file compare="Text">float_01.adm</output-file>
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/ARecordType.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/ARecordType.java
index ac1facc..82320e4 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/ARecordType.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/ARecordType.java
@@ -204,8 +204,34 @@
         return findFieldPosition(baaos.getByteArray(), 0, baaos.getByteArray().length);
     }
 
+    /**
+     * Returns the field type of the field name if it exists, otherwise null.
+     * 
+     * @param fieldName
+     *            the fieldName whose type is sought
+     * @return the field type of the field name if it exists, otherwise null
+     * @throws IOException
+     *             if an error occurs while serializing the field name
+     */
     public IAType getFieldType(String fieldName) throws IOException {
-        return fieldTypes[findFieldPosition(fieldName)];
+        int fieldPos = findFieldPosition(fieldName);
+        if (fieldPos < 0 || fieldPos >= fieldTypes.length) {
+            return null;
+        }
+        return fieldTypes[fieldPos];
+    }
+
+    /**
+     * Returns true or false indicating whether or not a field is closed.
+     * 
+     * @param fieldName
+     *            the name of the field to check
+     * @return true if fieldName is a closed field, otherwise false
+     * @throws IOException
+     *             if an error occurs while serializing fieldName
+     */
+    public boolean isClosedField(String fieldName) throws IOException {
+        return findFieldPosition(fieldName) != -1;
     }
 
     @Override