Merge branch 'master' into zheilbron/asterix_msr_demo

Conflicts:
	asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveUnusedOneToOneEquiJoinRule.java
	asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
	asterix-app/src/test/resources/runtimets/testsuite.xml
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 4ec388e..8e1b32a 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
@@ -43,7 +43,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;
@@ -133,7 +132,6 @@
         normalization.add(new ExtractGbyExpressionsRule());
         normalization.add(new ExtractDistinctByExpressionsRule());
         normalization.add(new ExtractOrderExpressionsRule());
-        normalization.add(new ExtractCommonExpressionsRule());
 
         // IntroduceStaticTypeCastRule should go before
         // IntroduceDynamicTypeCastRule to
@@ -141,6 +139,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/RemoveUnusedOneToOneEquiJoinRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveUnusedOneToOneEquiJoinRule.java
index 09715f5..a6416ba 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveUnusedOneToOneEquiJoinRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveUnusedOneToOneEquiJoinRule.java
@@ -22,8 +22,8 @@
 
 import org.apache.commons.lang3.mutable.Mutable;
 
-import edu.uci.ics.asterix.metadata.declared.AqlDataSource;
 import edu.uci.ics.asterix.metadata.declared.DatasetDataSource;
+import edu.uci.ics.asterix.metadata.entities.InternalDatasetDetails;
 import edu.uci.ics.asterix.metadata.utils.DatasetUtils;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
@@ -190,10 +190,12 @@
     private void fillPKVars(DataSourceScanOperator dataScan, List<LogicalVariable> pkVars) {
         pkVars.clear();
         DatasetDataSource datasetDataSource = (DatasetDataSource) dataScan.getDataSource();
-        int numPKs = DatasetUtils.getPartitioningKeys(datasetDataSource.getDataset()).size();
         pkVars.clear();
-        for (int i = 0; i < numPKs; i++) {
-            pkVars.add(dataScan.getVariables().get(i));
+        if (datasetDataSource.getDataset().getDatasetDetails() instanceof InternalDatasetDetails) {
+            int numPKs = DatasetUtils.getPartitioningKeys(datasetDataSource.getDataset()).size();
+            for (int i = 0; i < numPKs; i++) {
+                pkVars.add(dataScan.getVariables().get(i));
+            }
         }
     }
 
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 067df9c..47b4b7a 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
@@ -275,8 +275,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();
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 40201a5..49d08f6 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
@@ -39,6 +39,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;
@@ -100,14 +101,13 @@
 
             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);
             }
-
-            MetadataManager.INSTANCE.init();
-            SessionConfig sessionConfig = new SessionConfig(true, false, false, false, false, false, true, true, false);
-            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);
@@ -121,13 +121,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/main/java/edu/uci/ics/asterix/file/DatasetOperations.java b/asterix-app/src/main/java/edu/uci/ics/asterix/file/DatasetOperations.java
index 9a48c21..30736c0 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
@@ -309,7 +309,7 @@
             spec.connect(new OneToOneConnectorDescriptor(spec), scanner, 0, asterixOp, 0);
 
             IConnectorDescriptor sortMergeConn = new MToNPartitioningMergingConnectorDescriptor(spec,
-                    new FieldHashPartitionComputerFactory(keys, hashFactories), keys, comparatorFactories);
+                    new FieldHashPartitionComputerFactory(keys, hashFactories), keys, comparatorFactories, null);
             spec.connect(sortMergeConn, asterixOp, 0, btreeBulkLoad, 0);
         }
         spec.addRoot(btreeBulkLoad);
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/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/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/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index d7ab03f..096c141 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -14,12 +14,13 @@
  !-->
 <test-suite xmlns="urn:xml.testframework.asterix.ics.uci.edu" ResultOffsetPath="results" QueryOffsetPath="queries" QueryFileExtension=".aql">
   <test-group name="flwor">
-   <!-- commented due to issue 643
+    <!-- commented due to issue 643 (build fails) 
     <test-case FilePath="flwor">
       <compilation-unit name="at00">
         <output-dir compare="Text">at00</output-dir>
       </compilation-unit>
     </test-case>
+    -->
     <test-case FilePath="flwor">
       <compilation-unit name="at01">
         <output-dir compare="Text">at01</output-dir>
@@ -2901,6 +2902,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">
@@ -4340,6 +4351,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>
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 f77756b..b40fcb4 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
@@ -337,7 +337,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");
@@ -421,10 +421,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),
@@ -448,7 +468,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-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;
+        }
     }
 
 }