added materialization step before an insert and a scan/join of the same dataset.
diff --git a/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-dataset-with-index.aql b/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-dataset-with-index.aql
new file mode 100644
index 0000000..4643eb6
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-dataset-with-index.aql
@@ -0,0 +1,33 @@
+/* 
+ * Test case Name  : insert-and-scan-dataset-with-index.aql
+ * Description     : This test is intended to test inserting into a dataset that has a secondary index and scan
+ * the data at the same time where we insert a materializing to prevent the possibility of deadlatch.
+ * Expected Result : Success
+ * Date            : July 11 2013
+ */
+ 
+drop dataverse test if exists;
+create dataverse test;
+
+create type test.Emp as closed {
+id:int32,
+fname:string,
+lname:string,
+age:int32,
+dept:string
+}
+
+create dataset test.employee(Emp) primary key id;
+
+create index idx_employee_first_name on test.employee(fname);
+
+insert into dataset test.employee (
+for $x in dataset test.employee
+return {
+	"id": $x.id + 10000,
+	"fname": $x.fname,
+	"lname": $x.lname,
+	"age": $x.age,
+	"dept": $x.dept
+}
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-dataset.aql b/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-dataset.aql
new file mode 100644
index 0000000..52c4384
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-dataset.aql
@@ -0,0 +1,26 @@
+/* 
+ * Test case Name  : insert-and-scan-dataset.aql
+ * Description     : This test is intended to test inserting into a dataset and scan it at the same time
+ * where we insert a materializing to prevent the possibility of deadlatch.
+ * Expected Result : Success
+ * Date            : July 11 2013
+ */
+ 
+drop dataverse test if exists;
+create dataverse test;
+
+use dataverse test;
+
+create type myDataType as open {
+  id: int32
+}
+
+create dataset myData(myDataType)
+  primary key id;
+
+insert into dataset myData (
+for $x in dataset myData
+return {
+	"id": $x.id + 1
+}
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-joined-datasets.aql b/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-joined-datasets.aql
new file mode 100644
index 0000000..a8a27ca
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/insert-and-scan-joined-datasets.aql
@@ -0,0 +1,32 @@
+/* 
+ * Test case Name  : insert-and-scan-joined-datasets.aql
+ * Description     : This test is intended to test inserting into a dataset where the incoming stream
+  is involve a join operation that has the same dataset. We insert a materializing to prevent the 
+  possibility of deadlatch.
+ * Expected Result : Success
+ * Date            : July 11 2013
+ */
+ 
+drop dataverse test if exists;
+create dataverse test;
+
+use dataverse test;
+
+create type myDataType as open {
+  id: int32
+}
+
+create dataset myData(myDataType)
+  primary key id;
+  
+create dataset myData2(myDataType)
+  primary key id;
+
+insert into dataset myData (
+for $x in dataset myData2
+for $y in dataset myData
+where $x.id = $y.id
+return {
+	"id": $x.id + 1
+}
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-dataset-with-index.plan b/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-dataset-with-index.plan
new file mode 100644
index 0000000..8bc296b
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-dataset-with-index.plan
@@ -0,0 +1,20 @@
+-- COMMIT  |PARTITIONED|
+  -- STREAM_PROJECT  |PARTITIONED|
+    -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+      -- INDEX_INSERT_DELETE  |PARTITIONED|
+        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+          -- STREAM_PROJECT  |PARTITIONED|
+            -- ASSIGN  |PARTITIONED|
+              -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                -- INSERT_DELETE  |PARTITIONED|
+                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                    -- MATERIALIZE  |PARTITIONED|
+                      -- HASH_PARTITION_EXCHANGE [$$10]  |PARTITIONED|
+                        -- ASSIGN  |PARTITIONED|
+                          -- STREAM_PROJECT  |PARTITIONED|
+                            -- ASSIGN  |PARTITIONED|
+                              -- ASSIGN  |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/optimizerts/results/insert-and-scan-dataset.plan b/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-dataset.plan
new file mode 100644
index 0000000..e11e2a8
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-dataset.plan
@@ -0,0 +1,15 @@
+-- COMMIT  |PARTITIONED|
+  -- STREAM_PROJECT  |PARTITIONED|
+    -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+      -- INSERT_DELETE  |PARTITIONED|
+        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+          -- MATERIALIZE  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$6]  |PARTITIONED|
+              -- ASSIGN  |PARTITIONED|
+                -- STREAM_PROJECT  |PARTITIONED|
+                  -- ASSIGN  |PARTITIONED|
+                    -- STREAM_PROJECT  |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/optimizerts/results/insert-and-scan-joined-datasets.plan b/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-joined-datasets.plan
new file mode 100644
index 0000000..88f256e
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/insert-and-scan-joined-datasets.plan
@@ -0,0 +1,25 @@
+-- COMMIT  |PARTITIONED|
+  -- STREAM_PROJECT  |PARTITIONED|
+    -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+      -- INSERT_DELETE  |PARTITIONED|
+        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+          -- MATERIALIZE  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$11]  |PARTITIONED|
+              -- ASSIGN  |PARTITIONED|
+                -- STREAM_PROJECT  |PARTITIONED|
+                  -- ASSIGN  |PARTITIONED|
+                    -- STREAM_PROJECT  |PARTITIONED|
+                      -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                        -- HYBRID_HASH_JOIN [$$14][$$15]  |PARTITIONED|
+                          -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                            -- STREAM_PROJECT  |PARTITIONED|
+                              -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                -- DATASOURCE_SCAN  |PARTITIONED|
+                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                    -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
+                          -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                            -- STREAM_PROJECT  |PARTITIONED|
+                              -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                -- DATASOURCE_SCAN  |PARTITIONED|
+                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                    -- EMPTY_TUPLE_SOURCE  |PARTITIONED|