Merge branch 'asterix_lsm_stabilization' of https://code.google.com/p/asterixdb into yingyi/asterix_beta_fix
diff --git a/asterix-app/src/test/resources/optimizerts/queries/limit-issue353.aql b/asterix-app/src/test/resources/optimizerts/queries/limit-issue353.aql
new file mode 100644
index 0000000..f1b2678
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/limit-issue353.aql
@@ -0,0 +1,44 @@
+/*
+ * Description  : This test case is to verify the fix for issue353
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=353
+ * Expected Res : Success
+ * Date         : 28th April 2013
+ */
+
+drop dataverse tpch if exists;
+create dataverse tpch;
+
+use dataverse tpch;
+
+create type LineItemType as closed {
+  l_orderkey: int32, 
+  l_partkey: int32, 
+  l_suppkey: int32, 
+  l_linenumber: int32, 
+  l_quantity: double, 
+  l_extendedprice: double,
+  l_discount: double, 
+  l_tax: double,
+  l_returnflag: string, 
+  l_linestatus: string, 
+  l_shipdate: string,
+  l_commitdate: string, 
+  l_receiptdate: string, 
+  l_shipinstruct: string, 
+  l_shipmode: string, 
+  l_comment: string
+}
+
+create dataset LineItem(LineItemType)
+  primary key l_orderkey, l_linenumber;
+  
+write output to nc1:"/tmp/push_limit.adm";
+
+for $l in dataset('LineItem')
+limit 2
+return {
+"l_returnflag": $l.l_returnflag,
+"l_linestatus": $l.l_linestatus,
+"l_shipmode": $l.l_shipmode
+}
+
diff --git a/asterix-app/src/test/resources/optimizerts/results/limit-issue353.plan b/asterix-app/src/test/resources/optimizerts/results/limit-issue353.plan
new file mode 100644
index 0000000..829a245
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/limit-issue353.plan
@@ -0,0 +1,12 @@
+-- DISTRIBUTE_RESULT  |UNPARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+    -- STREAM_PROJECT  |UNPARTITIONED|
+      -- ASSIGN  |UNPARTITIONED|
+        -- STREAM_LIMIT  |UNPARTITIONED|
+          -- STREAM_PROJECT  |PARTITIONED|
+            -- SORT_MERGE_EXCHANGE [$$6(ASC), $$7(ASC) ]  |PARTITIONED|
+              -- STREAM_LIMIT  |PARTITIONED|
+                -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                  -- DATASOURCE_SCAN  |PARTITIONED|
+                    -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                      -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.1.ddl.aql
new file mode 100644
index 0000000..85f2160
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.1.ddl.aql
@@ -0,0 +1,17 @@
+/*
+ * Description  : This test case is to verify the fix for issue350
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=350
+ * Expected Res : Success
+ * Date         : 28th April 2013
+ */
+
+drop dataverse TinySocial if exists;
+create dataverse TinySocial;
+use dataverse TinySocial;
+
+create type TweetMessageType as open {
+tweetid: string
+};
+
+create dataset TweetMessages(TweetMessageType)
+primary key tweetid; 
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.2.update.aql
new file mode 100644
index 0000000..372d7ca
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.2.update.aql
@@ -0,0 +1,50 @@
+/*
+ * Description  : This test case is to verify the fix for issue350
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=350
+ * Expected Res : Success
+ * Date         : 28th April 2013
+ */
+
+use dataverse TinySocial;
+
+load dataset TweetMessages
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/twitter/tw_messages.adm"),("format"="adm"));
+
+insert into dataset TweetMessages
+(
+   {"tweetid":"13",
+    "user":
+        {"screen-name":"NathanGiesen@211",
+         "lang":"en",
+         "friends_count":39345,
+         "statuses_count":479,
+         "name":"Nathan Giesen",
+         "followers_count":49420,
+         "hobbies":["basket weaving","mud wrestling"]
+        },
+    "sender-location":point("47.44,80.65"),
+    "send-time":datetime("2008-04-26T10:10:35"),
+    "referred-topics":{{"tweeting"}},
+    "message-text":"tweety tweet, my fellow tweeters!"
+   }
+);
+
+insert into dataset TweetMessages
+(
+   {"tweetid":"15",
+    "user":
+        {"screen-name":"Jason17",
+         "lang":"en",
+         "friends_count":393,
+         "statuses_count":47,
+         "name":"Nathan Giesen",
+         "followers_count":420,
+         "hobbies":["swimming"]
+        },
+    "sender-location":point("49.44,80.65"),
+    "send-time":datetime("2009-04-26T10:10:35"),
+    "referred-topics":{{"nothing"}},
+    "message-text":"Nothing to say!"
+   }
+);
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.3.query.aql
new file mode 100644
index 0000000..79a620b
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue350/query-issue350.3.query.aql
@@ -0,0 +1,12 @@
+/*
+ * Description  : This test case is to verify the fix for issue350
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=350
+ * Expected Res : Success
+ * Date         : 28th April 2013
+ */
+
+use dataverse TinySocial;
+
+for $tm  in dataset TweetMessages
+where (some $h in $tm.user.hobbies satisfies $h = "basket weaving")
+return $tm;
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue350/query-issue350.1.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue350/query-issue350.1.adm
new file mode 100644
index 0000000..e5ac8ab
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue350/query-issue350.1.adm
@@ -0,0 +1 @@
+{ "tweetid": "13", "user": { "screen-name": "NathanGiesen@211", "lang": "en", "friends_count": 39345, "statuses_count": 479, "name": "Nathan Giesen", "followers_count": 49420, "hobbies": [ "basket weaving", "mud wrestling" ] }, "sender-location": point("47.44,80.65"), "send-time": datetime("2008-04-26T10:10:35.000Z"), "referred-topics": {{ "tweeting" }}, "message-text": "tweety tweet, my fellow tweeters!" }
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index dd29a7b..a9b8eef 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -2693,6 +2693,11 @@
         <output-dir compare="Text">query-proposal</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue350">
+        <output-dir compare="Text">query-issue350</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="quantifiers">
     <test-case FilePath="quantifiers">
@@ -4223,4 +4228,4 @@
       </compilation-unit>
     </test-case>
   </test-group>
-</test-suite>
\ No newline at end of file
+</test-suite>
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/unnestingfunctions/std/ScanCollectionDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/unnestingfunctions/std/ScanCollectionDescriptor.java
index c70cdf8..415ee33 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/unnestingfunctions/std/ScanCollectionDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/unnestingfunctions/std/ScanCollectionDescriptor.java
@@ -22,6 +22,8 @@
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
 import edu.uci.ics.asterix.om.functions.IFunctionDescriptor;
 import edu.uci.ics.asterix.om.functions.IFunctionDescriptorFactory;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.runtime.evaluators.common.AsterixListAccessor;
 import edu.uci.ics.asterix.runtime.unnestingfunctions.base.AbstractUnnestingFunctionDynamicDescriptor;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -73,12 +75,20 @@
                 private ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();
                 private ICopyEvaluator argEval = listEvalFactory.createEvaluator(inputVal);
                 private int itemIndex;
+                private boolean metNull = false;
 
                 @Override
                 public void init(IFrameTupleReference tuple) throws AlgebricksException {
                     try {
+                        metNull = false;
                         inputVal.reset();
                         argEval.evaluate(tuple);
+                        ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER
+                                .deserialize(inputVal.getByteArray()[0]);
+                        if (typeTag == ATypeTag.NULL) {
+                            metNull = true;
+                            return;
+                        }
                         listAccessor.reset(inputVal.getByteArray(), 0);
                         itemIndex = 0;
                     } catch (AsterixException e) {
@@ -89,10 +99,12 @@
                 @Override
                 public boolean step() throws AlgebricksException {
                     try {
-                        if (itemIndex < listAccessor.size()) {
-                            listAccessor.writeItem(itemIndex, out);
-                            ++itemIndex;
-                            return true;
+                        if (!metNull) {
+                            if (itemIndex < listAccessor.size()) {
+                                listAccessor.writeItem(itemIndex, out);
+                                ++itemIndex;
+                                return true;
+                            }
                         }
                     } catch (IOException e) {
                         throw new AlgebricksException(e);