Merge branch 'diego/hotfix/issue610'
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
index 14adbcc..0a1b429 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
@@ -42,7 +42,6 @@
 import edu.uci.ics.asterix.optimizer.rules.LoadRecordFieldsRule;
 import edu.uci.ics.asterix.optimizer.rules.NestGroupByRule;
 import edu.uci.ics.asterix.optimizer.rules.NestedSubplanToJoinRule;
-import edu.uci.ics.asterix.optimizer.rules.PullPositionalVariableFromUnnestRule;
 import edu.uci.ics.asterix.optimizer.rules.PushAggFuncIntoStandaloneAggregateRule;
 import edu.uci.ics.asterix.optimizer.rules.PushAggregateIntoGroupbyRule;
 import edu.uci.ics.asterix.optimizer.rules.PushFieldAccessRule;
@@ -124,7 +123,6 @@
         normalization.add(new ExtractGbyExpressionsRule());
         normalization.add(new ExtractDistinctByExpressionsRule());
         normalization.add(new ExtractOrderExpressionsRule());
-        normalization.add(new ExtractCommonExpressionsRule());
 
         // IntroduceStaticTypeCastRule should go before
         // IntroduceDynamicTypeCastRule to
@@ -132,6 +130,7 @@
         normalization.add(new IntroduceStaticTypeCastForInsertRule());
         normalization.add(new IntroduceDynamicTypeCastRule());
         normalization.add(new IntroduceEnforcedListTypeRule());
+        normalization.add(new ExtractCommonExpressionsRule());
         normalization.add(new ConstantFoldingRule());
         normalization.add(new UnnestToDataScanRule());
         normalization.add(new IfElseToSwitchCaseFunctionRule());
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java
index c67a4e5..1be4f40 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java
@@ -223,7 +223,8 @@
         boolean changed = false;
         for (int j = 0; j < args.size(); j++) {
             ILogicalExpression arg = args.get(j).getValue();
-            IAType currentItemType = (inputItemType == null || inputItemType == BuiltinType.ANY) ? (IAType) env.getType(arg) : inputItemType;
+            IAType currentItemType = (inputItemType == null || inputItemType == BuiltinType.ANY) ? (IAType) env
+                    .getType(arg) : inputItemType;
             switch (arg.getExpressionTag()) {
                 case FUNCTION_CALL:
                     ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) arg;
@@ -271,8 +272,10 @@
             String fieldName = inputFieldNames[i];
             IAType fieldType = inputFieldTypes[i];
 
-            if (2 * i + 1 > func.getArguments().size())
-                throw new AlgebricksException("expression index out of bound");
+            if (2 * i + 1 > func.getArguments().size()) {
+                // it is not a record constructor function
+                return false;
+            }
 
             // 2*i+1 is the index of field value expression
             ILogicalExpression arg = func.getArguments().get(2 * i + 1).getValue();
@@ -463,12 +466,9 @@
                     reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
                     fi = AsterixBuiltinFunctions.CAST_LIST;
             }
-            if (fi != null
-                    && ! inputFieldType.equals(reqFieldType)
-                    && parameterVars.size() > 0) {
+            if (fi != null && !inputFieldType.equals(reqFieldType) && parameterVars.size() > 0) {
                 //inject dynamic type casting
-                injectCastFunction(FunctionUtils.getFunctionInfo(fi),
-                        reqFieldType, inputFieldType, expRef, argExpr);
+                injectCastFunction(FunctionUtils.getFunctionInfo(fi), reqFieldType, inputFieldType, expRef, argExpr);
                 castInjected = true;
             }
             if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
index 9f6b7eb..486f16c 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
@@ -35,6 +35,7 @@
 import edu.uci.ics.asterix.aql.parser.TokenMgrError;
 import edu.uci.ics.asterix.aql.translator.AqlTranslator;
 import edu.uci.ics.asterix.common.config.GlobalConfig;
+import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.metadata.MetadataManager;
 import edu.uci.ics.asterix.result.ResultReader;
 import edu.uci.ics.asterix.result.ResultUtils;
@@ -86,16 +87,15 @@
 
             AQLParser parser = new AQLParser(query);
             List<Statement> aqlStatements = parser.Statement();
-            if (checkForbiddenStatements(aqlStatements, out)) {
-                return;
+            if (!containsForbiddenStatements(aqlStatements)) {
+                SessionConfig sessionConfig = new SessionConfig(true, false, false, false, false, false, true, true, false);
+    
+                MetadataManager.INSTANCE.init();
+    
+                AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, format);
+    
+                aqlTranslator.compileAndExecute(hcc, hds, asyncResults);
             }
-            SessionConfig sessionConfig = new SessionConfig(true, false, false, false, false, false, true, true, false);
-
-            MetadataManager.INSTANCE.init();
-
-            AqlTranslator aqlTranslator = new AqlTranslator(aqlStatements, out, sessionConfig, format);
-
-            aqlTranslator.compileAndExecute(hcc, hds, asyncResults);
         } catch (ParseException | TokenMgrError | edu.uci.ics.asterix.aqlplus.parser.TokenMgrError pe) {
             GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, pe.getMessage(), pe);
             String errorMessage = ResultUtils.buildParseExceptionMessage(pe, query);
@@ -109,13 +109,10 @@
         }
     }
 
-    private boolean checkForbiddenStatements(List<Statement> aqlStatements, PrintWriter out) {
+    private boolean containsForbiddenStatements(List<Statement> aqlStatements) throws AsterixException {
         for (Statement st : aqlStatements) {
             if (!getAllowedStatements().contains(st.getKind())) {
-                JSONObject errorResp = ResultUtils.getErrorResponse(1, String.format(getErrorMessage(), st.getKind()),
-                        "", "");
-                out.write(errorResp.toString());
-                return true;
+                throw new AsterixException(String.format(getErrorMessage(), st.getKind()));
             }
         }
         return false;
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_min_empty/scalar_min_empty.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_min_empty/scalar_min_empty.1.ddl.aql
index d4de7aa..732d98a 100644
--- a/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_min_empty/scalar_min_empty.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_min_empty/scalar_min_empty.1.ddl.aql
@@ -6,6 +6,3 @@
 drop dataverse test if exists;
 create dataverse test;
 
-use dataverse test;
-
-min([ ])
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_sum_empty/scalar_sum_empty.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_sum_empty/scalar_sum_empty.1.ddl.aql
index 3d954c1..b0c16d6 100644
--- a/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_sum_empty/scalar_sum_empty.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/scalar_sum_empty/scalar_sum_empty.1.ddl.aql
@@ -6,6 +6,3 @@
 drop dataverse test if exists;
 create dataverse test;
 
-use dataverse test;
-
-sum([ ])
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int64/sum_int64.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int64/sum_int64.1.ddl.aql
index 247c983..97b4670 100644
--- a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int64/sum_int64.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int64/sum_int64.1.ddl.aql
@@ -2,10 +2,3 @@
 
 create dataverse test;
 
-use dataverse test;
-
-sum( 
- for $x in [int64("1"), int64("2"), int64("3")] 
- return $x
-)
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int8/sum_int8.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int8/sum_int8.1.ddl.aql
index 4c7f01d..d330bc0 100644
--- a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int8/sum_int8.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_int8/sum_int8.1.ddl.aql
@@ -1,11 +1,3 @@
 drop dataverse test if exists;
 
 create dataverse test;
-
-use dataverse test;
-
-sum( 
- for $x in [int8("1"), int8("2"), int8("3")] 
- return $x
-)
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.1.ddl.aql
new file mode 100644
index 0000000..b1910fb
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.1.ddl.aql
@@ -0,0 +1,29 @@
+/*
+ * Description    : Left-outer joins two datasets, DBLP and CSX, based on their authors and titles.
+ * Success        : Yes
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+
+use dataverse test;
+
+create type DBLPType as closed {
+  id: int32, 
+  dblpid: string,
+  title: string,
+  authors: string,
+  misc: string
+}
+
+create type CSXType as closed {
+  id: int32, 
+  csxid: string,
+  title: string,
+  authors: string,
+  misc: string
+}
+
+create dataset DBLP(DBLPType) primary key id;
+create dataset CSX(CSXType) primary key id;
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.2.update.aql
new file mode 100644
index 0000000..4e2123a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.2.update.aql
@@ -0,0 +1,15 @@
+/*
+ * Description    : Left-outer joins two datasets, DBLP and CSX, based on their authors and titles.
+ * Success        : Yes
+ */
+
+use dataverse test;
+
+load dataset DBLP 
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/pub-small/dblp-small-id.txt"),("format"="delimited-text"),("delimiter"=":"));
+
+load dataset CSX
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/pub-small/csx-small-id.txt"),("format"="delimited-text"),("delimiter"=":"));
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.3.query.aql
new file mode 100644
index 0000000..04816a7
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/leftouterjoin/query_issue658/query_issue658.3.query.aql
@@ -0,0 +1,16 @@
+/*
+ * Description    : Left-outer joins two datasets, DBLP and CSX, based on their authors and titles.
+ * Success        : Yes
+ */
+
+use dataverse test;
+
+for $a in dataset('DBLP')
+order by $a.id
+return {
+"aid": $a.id,
+"bids": for $b in dataset('CSX')
+where $a.authors = $b.authors and $a.title != $b.title
+order by $b.id
+return $b.id
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/load/issue650_query/issue650_query.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/load/issue650_query/issue650_query.1.ddl.aql
new file mode 100644
index 0000000..b0fe47f
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/load/issue650_query/issue650_query.1.ddl.aql
@@ -0,0 +1,18 @@
+/*
+ * Description  : Drop and recreate the dataverse between creating and loading a dataset.
+ * Expected Res : Failure
+ * Date         : 17 Oct 2013
+ */
+ 
+drop dataverse fuzzyjoin if exists;
+create dataverse fuzzyjoin;
+use dataverse fuzzyjoin;
+
+create type UserType as open {
+  uid: int32, 
+  name: string,
+  lottery_numbers: [int32],
+  interests: {{string}}
+}
+
+create dataset Users(UserType) primary key uid;
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/load/issue650_query/issue650_query.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/load/issue650_query/issue650_query.2.update.aql
new file mode 100644
index 0000000..b7729d2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/load/issue650_query/issue650_query.2.update.aql
@@ -0,0 +1,13 @@
+/*
+ * Description  : Drop and recreate the dataverse between creating and loading a dataset.
+ * Expected Res : Failure
+ * Date         : 17 Oct 2013
+ */
+ 
+drop dataverse fuzzyjoin if exists;
+create dataverse fuzzyjoin;
+use dataverse fuzzyjoin;
+
+load dataset Users 
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/users-visitors-small/users.json"),("format"="adm"));
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.1.ddl.aql
new file mode 100644
index 0000000..a0231f4
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.1.ddl.aql
@@ -0,0 +1,21 @@
+/*
+ * Description     : Issue592
+ * Expected Result : Success
+ * Date            : 21 October 2013
+ * Notes           : This test was written to verify the fix for issue592.
+ */
+
+drop dataverse fooverse if exists;
+create dataverse fooverse;
+use dataverse fooverse;
+
+create type bartype as open {
+    "baz": int32
+}
+
+create type footype as open {
+    "id": int32,
+    "bars": [ bartype ]?   
+};
+
+create dataset fooset(footype) primary key id;
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.2.update.aql
new file mode 100644
index 0000000..0a08c2a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.2.update.aql
@@ -0,0 +1,18 @@
+/*
+ * Description     : Issue592
+ * Expected Result : Success
+ * Date            : 21 October 2013
+ * Notes           : This test was written to verify the fix for issue592.
+ */
+
+use dataverse fooverse;
+ 
+insert into dataset fooset (
+{
+    "id": 1,
+    "bars": [
+        { "baz": 1 },
+        { "baz": 1 }
+    ]
+}
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.3.query.aql
new file mode 100644
index 0000000..39fd476
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue592/query-issue592.3.query.aql
@@ -0,0 +1,11 @@
+/*
+ * Description     : Issue592
+ * Expected Result : Success
+ * Date            : 21 October 2013
+ * Notes           : This test was written to verify the fix for issue592.
+ */
+ 
+use dataverse fooverse;
+
+for $f in dataset fooset
+return $f
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.1.ddl.aql
new file mode 100644
index 0000000..44d9d32
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.1.ddl.aql
@@ -0,0 +1,17 @@
+/*
+ * Description     : Issue625
+ * Expected Result : Success
+ * Date            : 21 October 2013
+ * Notes           : This test was written to verify the fix for issue625.
+ */
+
+drop dataverse fooverse if exists;
+create dataverse fooverse;
+use dataverse fooverse;
+
+create type FooType as open {
+    id: int32,
+    numbers: [int32]
+}
+
+create dataset Foo(FooType) primary key id;
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.2.update.aql
new file mode 100644
index 0000000..dbe69e6
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.2.update.aql
@@ -0,0 +1,17 @@
+/*
+ * Description     : Issue592
+ * Expected Result : Success
+ * Date            : 21 October 2013
+ * Notes           : This test was written to verify the fix for issue592.
+ */
+
+use dataverse fooverse;
+ 
+insert into dataset Foo(
+let $number_strings := ["1", "2", "3"]
+let $numbers := for $x in $number_strings return int32($x)
+return {
+    "id": 1,
+    "numbers": $numbers
+}
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.3.query.aql
new file mode 100644
index 0000000..1f087a8
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue625/query-issue625.3.query.aql
@@ -0,0 +1,11 @@
+/*
+ * Description     : Issue592
+ * Expected Result : Success
+ * Date            : 21 October 2013
+ * Notes           : This test was written to verify the fix for issue592.
+ */
+ 
+use dataverse fooverse;
+
+for $x in dataset Foo
+return $x
diff --git a/asterix-app/src/test/resources/runtimets/queries/records/expFieldName/expFieldName.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/records/expFieldName/expFieldName.1.ddl.aql
index e3f2bc7..754ea81 100644
--- a/asterix-app/src/test/resources/runtimets/queries/records/expFieldName/expFieldName.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/records/expFieldName/expFieldName.1.ddl.aql
@@ -1,5 +1,3 @@
 drop dataverse test if exists;
 create dataverse test;
 
-for $x in ["field1", "field2"] 
-return {$x: 1}
diff --git a/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.1.ddl.aql
new file mode 100644
index 0000000..c74fd3c
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.1.ddl.aql
@@ -0,0 +1,19 @@
+drop dataverse TestVerse if exists;
+create dataverse TestVerse;
+use dataverse TestVerse;
+
+create type Int64TestType as open {
+        myint64: int64,
+        myoptint64: int64?,
+        myint32: int32,
+        myoptint32: int32?,
+        myint16: int16,
+        myoptint16: int16?,
+        mydouble: double,
+        myoptdouble: double?,
+        myfloat: float,
+        myoptfloat: float?
+};
+
+create dataset Int64Test(Int64TestType)
+   primary key myint64;
diff --git a/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.2.update.aql
new file mode 100644
index 0000000..8505bff
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.2.update.aql
@@ -0,0 +1,13 @@
+use dataverse TestVerse;
+
+/* promotable type for optional field */
+insert into dataset Int64Test (
+       {"myint64": int64("13"), "myoptint64": 13, "myint32": int8("2"), "myoptint32": int16("3"), "myint16": int8("9"), "myoptint16": int8("10"), "mydouble": float("2.12"), "myoptdouble": int64("32"), "myfloat": int8("9"), "myoptfloat": int32("328")}
+);
+/* promotable type for non-optional field */
+insert into dataset Int64Test (
+       {"myint64": 12, "myoptint64": null, "myint32": int8("2"), "myoptint32": date(null), "myint16": int8("9"), "myoptint16": interval-starts(null, null), "mydouble": float("2.12"), "myoptdouble": time(null), "myfloat": int8("9"), "myoptfloat": datetime(null) }
+);
+insert into dataset Int64Test (
+       {"myint64": int16("11"), "myoptint64": int8("3"), "myint32": int8("2"), "myoptint32": int16("3"), "myint16": int8("9"), "myoptint16": int8("10"), "mydouble": int8("2"), "myoptdouble": int16("32"), "myfloat": int16("9"), "myoptfloat": datetime(null) }
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.3.query.aql
new file mode 100644
index 0000000..8948b49
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/types/type_promotion_0/type_promotion_0.3.query.aql
@@ -0,0 +1,4 @@
+use dataverse TestVerse;
+
+for $i in dataset Int64Test
+return $i
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/leftouterjoin/query_issue658/query_issue658.1.adm b/asterix-app/src/test/resources/runtimets/results/leftouterjoin/query_issue658/query_issue658.1.adm
new file mode 100644
index 0000000..62c427a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/leftouterjoin/query_issue658/query_issue658.1.adm
@@ -0,0 +1,100 @@
+{ "aid": 1, "bids": [  ] }
+{ "aid": 2, "bids": [  ] }
+{ "aid": 3, "bids": [  ] }
+{ "aid": 4, "bids": [  ] }
+{ "aid": 5, "bids": [ 98 ] }
+{ "aid": 6, "bids": [  ] }
+{ "aid": 7, "bids": [  ] }
+{ "aid": 8, "bids": [  ] }
+{ "aid": 9, "bids": [  ] }
+{ "aid": 10, "bids": [  ] }
+{ "aid": 11, "bids": [  ] }
+{ "aid": 12, "bids": [  ] }
+{ "aid": 13, "bids": [  ] }
+{ "aid": 14, "bids": [  ] }
+{ "aid": 15, "bids": [  ] }
+{ "aid": 16, "bids": [  ] }
+{ "aid": 17, "bids": [  ] }
+{ "aid": 18, "bids": [  ] }
+{ "aid": 19, "bids": [  ] }
+{ "aid": 20, "bids": [  ] }
+{ "aid": 21, "bids": [  ] }
+{ "aid": 22, "bids": [  ] }
+{ "aid": 23, "bids": [  ] }
+{ "aid": 24, "bids": [  ] }
+{ "aid": 25, "bids": [  ] }
+{ "aid": 26, "bids": [  ] }
+{ "aid": 27, "bids": [  ] }
+{ "aid": 28, "bids": [  ] }
+{ "aid": 29, "bids": [  ] }
+{ "aid": 30, "bids": [  ] }
+{ "aid": 31, "bids": [  ] }
+{ "aid": 32, "bids": [  ] }
+{ "aid": 33, "bids": [  ] }
+{ "aid": 34, "bids": [ 57 ] }
+{ "aid": 35, "bids": [  ] }
+{ "aid": 36, "bids": [  ] }
+{ "aid": 37, "bids": [  ] }
+{ "aid": 38, "bids": [  ] }
+{ "aid": 39, "bids": [  ] }
+{ "aid": 40, "bids": [  ] }
+{ "aid": 41, "bids": [  ] }
+{ "aid": 42, "bids": [  ] }
+{ "aid": 43, "bids": [  ] }
+{ "aid": 44, "bids": [  ] }
+{ "aid": 45, "bids": [  ] }
+{ "aid": 46, "bids": [  ] }
+{ "aid": 47, "bids": [  ] }
+{ "aid": 48, "bids": [  ] }
+{ "aid": 49, "bids": [  ] }
+{ "aid": 50, "bids": [  ] }
+{ "aid": 51, "bids": [  ] }
+{ "aid": 52, "bids": [  ] }
+{ "aid": 53, "bids": [  ] }
+{ "aid": 54, "bids": [ 91 ] }
+{ "aid": 55, "bids": [  ] }
+{ "aid": 56, "bids": [  ] }
+{ "aid": 57, "bids": [  ] }
+{ "aid": 58, "bids": [  ] }
+{ "aid": 59, "bids": [  ] }
+{ "aid": 60, "bids": [  ] }
+{ "aid": 61, "bids": [  ] }
+{ "aid": 62, "bids": [  ] }
+{ "aid": 63, "bids": [  ] }
+{ "aid": 64, "bids": [  ] }
+{ "aid": 65, "bids": [  ] }
+{ "aid": 66, "bids": [  ] }
+{ "aid": 67, "bids": [  ] }
+{ "aid": 68, "bids": [ 57 ] }
+{ "aid": 69, "bids": [ 57 ] }
+{ "aid": 70, "bids": [  ] }
+{ "aid": 71, "bids": [  ] }
+{ "aid": 72, "bids": [  ] }
+{ "aid": 73, "bids": [  ] }
+{ "aid": 74, "bids": [  ] }
+{ "aid": 75, "bids": [  ] }
+{ "aid": 76, "bids": [  ] }
+{ "aid": 77, "bids": [  ] }
+{ "aid": 78, "bids": [  ] }
+{ "aid": 79, "bids": [  ] }
+{ "aid": 80, "bids": [  ] }
+{ "aid": 81, "bids": [  ] }
+{ "aid": 82, "bids": [  ] }
+{ "aid": 83, "bids": [  ] }
+{ "aid": 84, "bids": [  ] }
+{ "aid": 85, "bids": [  ] }
+{ "aid": 86, "bids": [  ] }
+{ "aid": 87, "bids": [  ] }
+{ "aid": 88, "bids": [  ] }
+{ "aid": 89, "bids": [  ] }
+{ "aid": 90, "bids": [  ] }
+{ "aid": 91, "bids": [  ] }
+{ "aid": 92, "bids": [  ] }
+{ "aid": 93, "bids": [  ] }
+{ "aid": 94, "bids": [  ] }
+{ "aid": 95, "bids": [  ] }
+{ "aid": 96, "bids": [  ] }
+{ "aid": 97, "bids": [  ] }
+{ "aid": 98, "bids": [  ] }
+{ "aid": 99, "bids": [  ] }
+{ "aid": 100, "bids": [  ] }
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue592/query-issue592.1.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue592/query-issue592.1.adm
new file mode 100644
index 0000000..cd05949
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue592/query-issue592.1.adm
@@ -0,0 +1 @@
+{ "id": 1, "bars": [ { "baz": 1 }, { "baz": 1 } ] }
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue625/query-issue625.1.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue625/query-issue625.1.adm
new file mode 100644
index 0000000..a7adaa3
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue625/query-issue625.1.adm
@@ -0,0 +1 @@
+{ "id": 1, "numbers": [ 1, 2, 3 ] }
diff --git a/asterix-app/src/test/resources/runtimets/results/types/type_promotion_0/type_promotion_0.1.adm b/asterix-app/src/test/resources/runtimets/results/types/type_promotion_0/type_promotion_0.1.adm
new file mode 100644
index 0000000..3c356ed
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/types/type_promotion_0/type_promotion_0.1.adm
@@ -0,0 +1,3 @@
+{ "myint64": 11i64, "myoptint64": 3i64, "myint32": 2, "myoptint32": 3, "myint16": 9i16, "myoptint16": 10i16, "mydouble": 2.0d, "myoptdouble": 32.0d, "myfloat": 9.0f, "myoptfloat": null }
+{ "myint64": 12i64, "myoptint64": null, "myint32": 2, "myoptint32": null, "myint16": 9i16, "myoptint16": null, "mydouble": 2.119999885559082d, "myoptdouble": null, "myfloat": 9.0f, "myoptfloat": null }
+{ "myint64": 13i64, "myoptint64": 13i64, "myint32": 2, "myoptint32": 3, "myint16": 9i16, "myoptint16": 10i16, "mydouble": 2.119999885559082d, "myoptdouble": 32.0d, "myfloat": 9.0f, "myoptfloat": 328.0f }
\ 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 a409981..1e60319 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -2899,6 +2899,16 @@
         <expected-error>edu.uci.ics.asterix.common.exceptions.AsterixException</expected-error>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue592">
+        <output-dir compare="Text">query-issue592</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue625">
+        <output-dir compare="Text">query-issue625</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="quantifiers">
     <test-case FilePath="quantifiers">
@@ -4338,6 +4348,12 @@
         <output-dir compare="Text">issue289_query</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="load">
+      <compilation-unit name="issue650_query">
+        <output-dir compare="Text">none</output-dir>
+        <expected-error>edu.uci.ics.asterix.common.exceptions.AsterixException</expected-error> 
+      </compilation-unit>
+    </test-case>
      <test-case FilePath="user-defined-functions">
       <compilation-unit name="query-issue244">
         <output-dir compare="Text">query-issue244</output-dir>
@@ -4522,6 +4538,11 @@
   </test-group>
   <test-group name="leftouterjoin">
     <test-case FilePath="leftouterjoin">
+      <compilation-unit name="query_issue658">
+        <output-dir compare="Text">query_issue658</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="leftouterjoin">
       <compilation-unit name="query_issue285">
         <output-dir compare="Text">query_issue285</output-dir>
       </compilation-unit>
@@ -4557,5 +4578,10 @@
         <output-dir compare="Text">record01</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="types">
+      <compilation-unit name="type_promotion_0">
+        <output-dir compare="Text">type_promotion_0</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
 </test-suite>
diff --git a/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java b/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
index 55c9d23..f1fa2a3 100644
--- a/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
+++ b/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
@@ -338,7 +338,7 @@
         StringWriter writerIn = new StringWriter();
         IOUtils.copy(bisIn, writerIn, "UTF-8");
         s.append(writerIn.toString());
-        
+
         BufferedInputStream bisErr = new BufferedInputStream(p.getErrorStream());
         StringWriter writerErr = new StringWriter();
         IOUtils.copy(bisErr, writerErr, "UTF-8");
@@ -422,10 +422,30 @@
                             break;
                         case "txnqar": //qar represents query after recovery
                             try {
-                                InputStream resultStream = executeQuery(statement);
+                                ////////////// <begin of temporary fix> ////////////////////////////
+                                //TODO
+                                //Temporary fix in order not to block the build test(mvn verify)
+                                //A proper fix should not have the while loop here.
+                                int maxRetryCount = 12;
+                                int tryCount = 0;
+                                InputStream resultStream = null;
+                                long sleepTime = 5;
+                                
+                                do {
+                                    //wait until NC starts
+                                    sleepTime *= 2;
+                                    Thread.sleep(sleepTime);
+                                    if (++tryCount > maxRetryCount) {
+                                        LOGGER.info("Metadata node is not running - this test will fail.");
+                                        break;
+                                    }
+                                    resultStream = executeQuery(statement);
+                                } while (resultStream.toString().contains("Connection refused to host"));
+                                ////////////// <end of temporary fix> //////////////////////////////
+
                                 qarFile = new File(actualPath + File.separator
                                         + testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
-                                        + cUnit.getName() + "_qbc.adm");
+                                        + cUnit.getName() + "_qar.adm");
                                 qarFile.getParentFile().mkdirs();
                                 TestsUtils.writeResultsToFile(qarFile, resultStream);
                                 TestsUtils.runScriptAndCompareWithResult(testFile, new PrintWriter(System.err),
@@ -449,7 +469,7 @@
                                         pb,
                                         getScriptPath(testFile.getAbsolutePath(), pb.environment().get("SCRIPT_HOME"),
                                                 statement.trim()));
-                                if(output.contains("ERROR")) {
+                                if (output.contains("ERROR")) {
                                     throw new Exception(output);
                                 }
                             } catch (Exception e) {
diff --git a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/FileSystemBasedAdapter.java b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/FileSystemBasedAdapter.java
index 33ee11f..753f7d1 100644
--- a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/FileSystemBasedAdapter.java
+++ b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/FileSystemBasedAdapter.java
@@ -16,7 +16,6 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.List;
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -29,6 +28,7 @@
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.asterix.runtime.operators.file.AdmSchemafullRecordParserFactory;
 import edu.uci.ics.asterix.runtime.operators.file.NtDelimitedDataTupleParserFactory;
 import edu.uci.ics.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint;
@@ -108,11 +108,11 @@
         for (int i = 0; i < n; i++) {
             ATypeTag tag = null;
             if (recordType.getFieldTypes()[i].getTypeTag() == ATypeTag.UNION) {
-                List<IAType> unionTypes = ((AUnionType) recordType.getFieldTypes()[i]).getUnionList();
-                if (unionTypes.size() != 2 && unionTypes.get(0).getTypeTag() != ATypeTag.NULL) {
+                if (!NonTaggedFormatUtil.isOptionalField(((AUnionType) recordType.getFieldTypes()[i]))) {
                     throw new NotImplementedException("Non-optional UNION type is not supported.");
                 }
-                tag = unionTypes.get(1).getTypeTag();
+                tag = ((AUnionType) recordType.getFieldTypes()[i]).getUnionList()
+                        .get(NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST).getTypeTag();
             } else {
                 tag = recordType.getFieldTypes()[i].getTypeTag();
             }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java
index 4e939ce..a711eb9 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java
@@ -15,6 +15,7 @@
 
 package edu.uci.ics.asterix.om.pointables.cast;
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -28,8 +29,12 @@
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AbstractCollectionType;
+import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.asterix.om.types.hierachy.ATypeHierarchy;
+import edu.uci.ics.asterix.om.types.hierachy.ITypePromoteComputer;
 import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
+import edu.uci.ics.hyracks.data.std.util.ArrayBackedValueStorage;
 
 /**
  * This class is a IVisitablePointableVisitor implementation which recursively
@@ -86,10 +91,48 @@
     }
 
     @Override
-    public Void visit(AFlatValuePointable accessor, Triple<IVisitablePointable, IAType, Boolean> arg) {
+    public Void visit(AFlatValuePointable accessor, Triple<IVisitablePointable, IAType, Boolean> arg)
+            throws AsterixException {
+        if (arg.second == null) {
+            // for open type case
+            arg.first.set(accessor);
+            return null;
+        }
         // set the pointer for result
-        arg.first.set(accessor);
+        ATypeTag reqTypeTag = ((IAType) (arg.second)).getTypeTag();
+        ATypeTag inputTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(accessor.getByteArray()[accessor
+                .getStartOffset()]);
+        if (!needPromote(inputTypeTag, reqTypeTag)) {
+            arg.first.set(accessor);
+        } else {
+            ArrayBackedValueStorage castBuffer = new ArrayBackedValueStorage();
+            ITypePromoteComputer promoteComputer = ATypeHierarchy.getTypePromoteComputer(inputTypeTag, reqTypeTag);
+            if (promoteComputer != null) {
+
+                try {
+                    // do the promotion; note that the type tag field should be skipped
+                    promoteComputer.promote(accessor.getByteArray(), accessor.getStartOffset() + 1,
+                            accessor.getLength() - 1, castBuffer);
+                    arg.first.set(castBuffer);
+                } catch (IOException e) {
+                    throw new AsterixException(e);
+                }
+            } else {
+                throw new AsterixException("Type mismatch: cannot cast type " + inputTypeTag + " to " + reqTypeTag);
+            }
+        }
+
         return null;
     }
 
+    private boolean needPromote(ATypeTag tag0, ATypeTag tag1) {
+        if (tag0 == tag1) {
+            return false;
+        }
+        if (tag0 == ATypeTag.NULL) {
+            return false;
+        }
+        return true;
+    }
+
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java
index 507b845..e7e0ef2 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java
@@ -36,6 +36,7 @@
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.asterix.om.types.hierachy.ATypeHierarchy;
 import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.asterix.om.util.ResettableByteArrayOutputStream;
 import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
@@ -205,6 +206,17 @@
                         optionalFields[reqFnPos] && fieldTypeTag.equals(nullTypeTag))) {
                     fieldPermutation[reqFnPos] = fnPos;
                     openFields[fnPos] = false;
+                } else {
+                    // if mismatch, check whether input type can be promoted to the required type
+                    ATypeTag inputTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(fieldTypeTag
+                            .getByteArray()[fieldTypeTag.getStartOffset()]);
+                    ATypeTag requiredTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(reqFieldTypeTag
+                            .getByteArray()[reqFieldTypeTag.getStartOffset()]);
+
+                    if (ATypeHierarchy.canPromote(inputTypeTag, requiredTypeTag)) {
+                        fieldPermutation[reqFnPos] = fnPos;
+                        openFields[fnPos] = false;
+                    }
                 }
                 fnStart++;
                 reqFnStart++;
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/base/TypeComputerUtilities.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/base/TypeComputerUtilities.java
index 8959f2c..6464c4a 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/base/TypeComputerUtilities.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/base/TypeComputerUtilities.java
@@ -15,8 +15,19 @@
 
 package edu.uci.ics.asterix.om.typecomputer.base;
 
+import java.util.List;
+
+import org.apache.commons.lang3.mutable.Mutable;
+
+import edu.uci.ics.asterix.om.types.ARecordType;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.AbstractCollectionType;
 import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
 
 public class TypeComputerUtilities {
 
@@ -55,4 +66,55 @@
         } else
             return null;
     }
+
+    public static boolean nullableType(ILogicalExpression expression, IVariableTypeEnvironment env)
+            throws AlgebricksException {
+        AbstractFunctionCallExpression func = (AbstractFunctionCallExpression) expression;
+        if (!(func instanceof ScalarFunctionCallExpression)) {
+            return true;
+        }
+        List<Mutable<ILogicalExpression>> args = func.getArguments();
+        for (Mutable<ILogicalExpression> arg : args) {
+            IAType type = (IAType) env.getType(arg.getValue());
+            if (type.getTypeTag() == ATypeTag.UNION || type.getTypeTag() == ATypeTag.NULL
+                    || type.getTypeTag() == ATypeTag.ANY) {
+                return true;
+            }
+            if (type.getTypeTag() == ATypeTag.RECORD || type.getTypeTag() == ATypeTag.UNORDEREDLIST
+                    || type.getTypeTag() == ATypeTag.ORDEREDLIST) {
+                if (nullableCompositeType(type)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    private static boolean nullableCompositeType(IAType type) {
+        if (type.getTypeTag() == ATypeTag.UNION || type.getTypeTag() == ATypeTag.NULL
+                || type.getTypeTag() == ATypeTag.ANY) {
+            return true;
+        } else if (type.getTypeTag() == ATypeTag.RECORD) {
+            ARecordType recordType = (ARecordType) type;
+            IAType[] fieldTypes = recordType.getFieldTypes();
+            for (IAType fieldType : fieldTypes) {
+                boolean nullable = nullableCompositeType(fieldType);
+                if (nullable) {
+                    return true;
+                }
+            }
+            return false;
+        } else if (type.getTypeTag() == ATypeTag.UNORDEREDLIST || type.getTypeTag() == ATypeTag.ORDEREDLIST) {
+            AbstractCollectionType collectionType = (AbstractCollectionType) type;
+            IAType itemType = collectionType.getItemType();
+            boolean nullable = nullableCompositeType(itemType);
+            if (nullable) {
+                return true;
+            } else {
+                return false;
+            }
+        } else {
+            return false;
+        }
+    }
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalABooleanTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalABooleanTypeComputer.java
index 71c69dd..764624f 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalABooleanTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalABooleanTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ABOOLEAN);
-        return new AUnionType(unionList, "OptionalBoolean");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ABOOLEAN);
+            return new AUnionType(unionList, "OptionalBoolean");
+        } else {
+            return BuiltinType.ABOOLEAN;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalACircleTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalACircleTypeComputer.java
index 411e201..92e26e0 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalACircleTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalACircleTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ACIRCLE);
-        return new AUnionType(unionList, "OptionalCircle");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ACIRCLE);
+            return new AUnionType(unionList, "OptionalCircle");
+        } else {
+            return BuiltinType.ACIRCLE;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTimeTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTimeTypeComputer.java
index 8ff0b1f..f0d2caf 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTimeTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTimeTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ADATETIME);
-        return new AUnionType(unionList, "OptionalDatetime");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ADATETIME);
+            return new AUnionType(unionList, "OptionalDatetime");
+        } else {
+            return BuiltinType.ADATETIME;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTypeComputer.java
index 1b17333..093849d 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADateTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ADATE);
-        return new AUnionType(unionList, "OptionalDate");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ADATE);
+            return new AUnionType(unionList, "OptionalDate");
+        } else {
+            return BuiltinType.ADATE;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADayTimeDurationTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADayTimeDurationTypeComputer.java
index 38f76e3..c3c7c25 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADayTimeDurationTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADayTimeDurationTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -40,10 +41,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ADAYTIMEDURATION);
-        return new AUnionType(unionList, "OptionalDayTimeDuration");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ADAYTIMEDURATION);
+            return new AUnionType(unionList, "OptionalDayTimeDuration");
+        } else {
+            return BuiltinType.ADAYTIMEDURATION;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADoubleTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADoubleTypeComputer.java
index 163b6b3..324e078 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADoubleTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADoubleTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,9 +37,13 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ADOUBLE);
-        return new AUnionType(unionList, "OptionalDouble");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ADOUBLE);
+            return new AUnionType(unionList, "OptionalDouble");
+        } else {
+            return BuiltinType.ADOUBLE;
+        }
     }
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADurationTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADurationTypeComputer.java
index 48f0715..85556ed 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADurationTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalADurationTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ADURATION);
-        return new AUnionType(unionList, "OptionalDuration");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ADURATION);
+            return new AUnionType(unionList, "OptionalDuration");
+        } else {
+            return BuiltinType.ADURATION;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAFloatTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAFloatTypeComputer.java
index 6c93522..d9da2b8 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAFloatTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAFloatTypeComputer.java
@@ -18,9 +18,11 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
 import edu.uci.ics.hyracks.algebricks.core.algebra.metadata.IMetadataProvider;
@@ -34,11 +36,15 @@
 
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
-            IMetadataProvider<?, ?> metadataProvider) {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AFLOAT);
-        return new AUnionType(unionList, "OptionalFloat");
+            IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AFLOAT);
+            return new AUnionType(unionList, "OptionalFloat");
+        } else {
+            return BuiltinType.AFLOAT;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt16TypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt16TypeComputer.java
index 575748e..a737ed2 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt16TypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt16TypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AINT16);
-        return new AUnionType(unionList, "OptionalInt16");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AINT16);
+            return new AUnionType(unionList, "OptionalInt16");
+        } else {
+            return BuiltinType.AINT16;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt32TypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt32TypeComputer.java
index 38aa0f3..c666d36 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt32TypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt32TypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AINT32);
-        return new AUnionType(unionList, "OptionalInt32");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AINT32);
+            return new AUnionType(unionList, "OptionalInt32");
+        } else {
+            return BuiltinType.AINT32;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt64TypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt64TypeComputer.java
index 571b266..701b1ae 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt64TypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt64TypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AINT64);
-        return new AUnionType(unionList, "OptionalInt64");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AINT64);
+            return new AUnionType(unionList, "OptionalInt64");
+        } else {
+            return BuiltinType.AINT64;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt8TypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt8TypeComputer.java
index 49c2994..ad6b130 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt8TypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAInt8TypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AINT8);
-        return new AUnionType(unionList, "OptionalInt8");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AINT8);
+            return new AUnionType(unionList, "OptionalInt8");
+        } else {
+            return BuiltinType.AINT8;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAIntervalTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAIntervalTypeComputer.java
index 6929a80..9c5a7d5 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAIntervalTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAIntervalTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
 
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AINTERVAL);
-        return new AUnionType(unionList, "OptionalInterval");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AINTERVAL);
+            return new AUnionType(unionList, "OptionalInterval");
+        } else {
+            return BuiltinType.AINTERVAL;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalALineTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalALineTypeComputer.java
index b7b89a9..e692dbd 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalALineTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalALineTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ALINE);
-        return new AUnionType(unionList, "OptionalLine");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ALINE);
+            return new AUnionType(unionList, "OptionalLine");
+        } else {
+            return BuiltinType.ALINE;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPoint3DTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPoint3DTypeComputer.java
index 7008ac6..8f0bf7a 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPoint3DTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPoint3DTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.APOINT3D);
-        return new AUnionType(unionList, "OptionalPoint3d");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.APOINT3D);
+            return new AUnionType(unionList, "OptionalPoint3d");
+        } else {
+            return BuiltinType.APOINT3D;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPointTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPointTypeComputer.java
index c07d4fb..1a93ee6 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPointTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPointTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.APOINT);
-        return new AUnionType(unionList, "OptionalPoint");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.APOINT);
+            return new AUnionType(unionList, "OptionalPoint");
+        } else {
+            return BuiltinType.APOINT;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPolygonTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPolygonTypeComputer.java
index 398add3..405608c 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPolygonTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAPolygonTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.APOLYGON);
-        return new AUnionType(unionList, "OptionalPolygon");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.APOLYGON);
+            return new AUnionType(unionList, "OptionalPolygon");
+        } else {
+            return BuiltinType.APOLYGON;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalARectangleTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalARectangleTypeComputer.java
index 936668b..c6a8026 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalARectangleTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalARectangleTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ARECTANGLE);
-        return new AUnionType(unionList, "OptionalRectangle");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ARECTANGLE);
+            return new AUnionType(unionList, "OptionalRectangle");
+        } else {
+            return BuiltinType.ARECTANGLE;
+        }
     }
 
 }
\ No newline at end of file
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAStringTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAStringTypeComputer.java
index 3758645..5b0642d 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAStringTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAStringTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ASTRING);
-        return new AUnionType(unionList, "OptionalString");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ASTRING);
+            return new AUnionType(unionList, "OptionalString");
+        } else {
+            return BuiltinType.ASTRING;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalATimeTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalATimeTypeComputer.java
index 19e6e30..4d466e7 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalATimeTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalATimeTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -36,10 +37,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.ATIME);
-        return new AUnionType(unionList, "OptionalTime");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.ATIME);
+            return new AUnionType(unionList, "OptionalTime");
+        } else {
+            return BuiltinType.ATIME;
+        }
     }
 
 }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAYearMonthDurationTypeComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAYearMonthDurationTypeComputer.java
index 4c08142..3350674 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAYearMonthDurationTypeComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/OptionalAYearMonthDurationTypeComputer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
@@ -40,10 +41,14 @@
     @Override
     public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
             IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
-        List<IAType> unionList = new ArrayList<IAType>();
-        unionList.add(BuiltinType.ANULL);
-        unionList.add(BuiltinType.AYEARMONTHDURATION);
-        return new AUnionType(unionList, "OptionalYearMonthDuration");
+        if (TypeComputerUtilities.nullableType(expression, env)) {
+            List<IAType> unionList = new ArrayList<IAType>();
+            unionList.add(BuiltinType.ANULL);
+            unionList.add(BuiltinType.AYEARMONTHDURATION);
+            return new AUnionType(unionList, "OptionalYearMonthDuration");
+        } else {
+            return BuiltinType.AYEARMONTHDURATION;
+        }
     }
 
 }
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 0319c7c..28a41d1 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
@@ -66,6 +66,7 @@
      * @throws AsterixException
      *             if there are duplicate field names or if there is an error serializing the field names
      */
+    @SuppressWarnings("resource")
     public ARecordType(String typeName, String[] fieldNames, IAType[] fieldTypes, boolean isOpen)
             throws AsterixException {
         super(typeName);
@@ -96,6 +97,11 @@
             hashCodeIndexPairs[i] = hashCodeIndexPairs[i] << 32;
             hashCodeIndexPairs[i] = hashCodeIndexPairs[i] | i;
         }
+        try {
+            dos.close();
+        } catch (IOException e) {
+            throw new AsterixException(e);
+        }
         serializedFieldNames = baaos.getByteArray();
 
         Arrays.sort(hashCodeIndexPairs);
@@ -336,7 +342,8 @@
                             break;
                         default:
                             throw new AlgebricksException("The field \"" + fieldName + "\" which is of type "
-                                    + fieldType.getTypeTag() + " cannot be indexed using the Length Partitioned N-Gram index.");
+                                    + fieldType.getTypeTag()
+                                    + " cannot be indexed using the Length Partitioned N-Gram index.");
                     }
                     break;
                 case LENGTH_PARTITIONED_WORD_INVIX:
@@ -348,7 +355,8 @@
                             break;
                         default:
                             throw new AlgebricksException("The field \"" + fieldName + "\" which is of type "
-                                    + fieldType.getTypeTag() + " cannot be indexed using the Length Partitioned Keyword index.");
+                                    + fieldType.getTypeTag()
+                                    + " cannot be indexed using the Length Partitioned Keyword index.");
                     }
                     break;
                 case SINGLE_PARTITION_NGRAM_INVIX:
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/operators/file/DelimitedDataParser.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/operators/file/DelimitedDataParser.java
index 5a639dc..23f7aab 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/operators/file/DelimitedDataParser.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/operators/file/DelimitedDataParser.java
@@ -29,6 +29,8 @@
 import edu.uci.ics.asterix.om.base.ANull;
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.AUnionType;
+import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
 import edu.uci.ics.hyracks.data.std.util.ArrayBackedValueStorage;
 import edu.uci.ics.hyracks.dataflow.common.data.parsers.IValueParser;
@@ -115,8 +117,9 @@
                         && recordType.getFieldTypes()[i].getTypeTag() != ATypeTag.NULL) {
                     // if the field is empty and the type is optional, insert NULL
                     // note that string type can also process empty field as an empty string
-                    if (recordType.getFieldTypes()[i].getTypeTag() != ATypeTag.UNION) {
-                        throw new AsterixException("Field " + i + " cannot be NULL. ");
+                    if (recordType.getFieldTypes()[i].getTypeTag() != ATypeTag.UNION
+                            || !NonTaggedFormatUtil.isOptionalField((AUnionType) recordType.getFieldTypes()[i])) {
+                        throw new AsterixException("Field " + i + " is not an optional type so it cannot accept null value. ");
                     }
                     fieldValueBufferOutput.writeByte(ATypeTag.NULL.serialize());
                     ANullSerializerDeserializer.INSTANCE.serialize(ANull.NULL, out);