Added basic testing. This fix is ready for review.

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix-fix-issue-9@278 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceSecondaryIndexInsertDeleteRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceSecondaryIndexInsertDeleteRule.java
index c9f9f93..dc5d659 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceSecondaryIndexInsertDeleteRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceSecondaryIndexInsertDeleteRule.java
@@ -25,6 +25,7 @@
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.IOptimizationContext;
@@ -151,7 +152,7 @@
                             secondaryKeyVar)));
                 }
                 Mutable<ILogicalExpression> filterExpression = createFilterExpression(secondaryKeyVars,
-                        context.getOutputTypeEnvironment(assign));
+                        context.getOutputTypeEnvironment(assign), false);
                 AqlIndex dataSourceIndex = new AqlIndex(index, metadata, datasetName);
                 IndexInsertDeleteOperator indexUpdate = new IndexInsertDeleteOperator(dataSourceIndex,
                         insertOp.getPrimaryKeyExpressions(), secondaryExpressions, filterExpression,
@@ -160,10 +161,9 @@
                 currentTop = indexUpdate;
                 context.computeAndSetTypeEnvironmentForOperator(indexUpdate);
             } else if (index.getKind() == IndexKind.RTREE) {
-                IAType spatialType = null;
-                for (String secondaryKey : secondaryKeyFields) {
-                    spatialType = keyFieldType(secondaryKey, recType);
-                }
+                Pair<IAType, Boolean> keyPairType = AqlCompiledIndexDecl.getNonNullableKeyFieldType(
+                        secondaryKeyFields.get(0), recType);
+                IAType spatialType = keyPairType.first;
                 int dimension = NonTaggedFormatUtil.getNumDimensions(spatialType.getTypeTag());
                 int numKeys = dimension * 2;
                 List<LogicalVariable> keyVarList = new ArrayList<LogicalVariable>();
@@ -191,8 +191,10 @@
                 AssignOperator assignCoordinates = new AssignOperator(keyVarList, keyExprList);
                 assignCoordinates.getInputs().add(new MutableObject<ILogicalOperator>(assign));
                 context.computeAndSetTypeEnvironmentForOperator(assignCoordinates);
+                // We must enforce the filter if the originating spatial type is nullable.
+                boolean forceFilter = keyPairType.second;
                 Mutable<ILogicalExpression> filterExpression = createFilterExpression(keyVarList,
-                        context.getOutputTypeEnvironment(assignCoordinates));
+                        context.getOutputTypeEnvironment(assignCoordinates), forceFilter);
                 AqlIndex dataSourceIndex = new AqlIndex(index, metadata, datasetName);
                 IndexInsertDeleteOperator indexUpdate = new IndexInsertDeleteOperator(dataSourceIndex,
                         insertOp.getPrimaryKeyExpressions(), secondaryExpressions, filterExpression,
@@ -210,12 +212,12 @@
 
     @SuppressWarnings("unchecked")
     private Mutable<ILogicalExpression> createFilterExpression(List<LogicalVariable> secondaryKeyVars,
-            IVariableTypeEnvironment typeEnv) throws AlgebricksException {
+            IVariableTypeEnvironment typeEnv, boolean forceFilter) throws AlgebricksException {
         List<Mutable<ILogicalExpression>> filterExpressions = new ArrayList<Mutable<ILogicalExpression>>();
         // Add 'is not null' to all nullable secondary index keys as a filtering condition.
         for (LogicalVariable secondaryKeyVar : secondaryKeyVars) {
             IAType secondaryKeyType = (IAType) typeEnv.getVarType(secondaryKeyVar);
-            if (!isNullableType(secondaryKeyType)) {
+            if (!isNullableType(secondaryKeyType) && !forceFilter) {
                 continue;
             }            
             ScalarFunctionCallExpression isNullFuncExpr = new ScalarFunctionCallExpression(
@@ -247,15 +249,4 @@
         }
         return false;
     }
-    
-    public static IAType keyFieldType(String expr, ARecordType recType) throws AlgebricksException {
-        String[] names = recType.getFieldNames();
-        int n = names.length;
-        for (int i = 0; i < n; i++) {
-            if (names[i].equals(expr)) {
-                return recType.getFieldTypes()[i];
-            }
-        }
-        throw new AlgebricksException("Could not find field " + expr + " in the schema.");
-    }
 }
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-btree-secondary-index-nullable.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-btree-secondary-index-nullable.aql
new file mode 100644
index 0000000..a35c8e5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-btree-secondary-index-nullable.aql
@@ -0,0 +1,42 @@
+/* 
+ * Test case Name  : scan-delete-btree-secondary-index-nullable.aql
+ * Description     : This test is intended to test deletion from secondary btree indexes that are built on nullable fields 
+ * Expected Result : Success
+ * Date            : May 12 2012
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type AddressType as closed {
+  number: int32, 
+  street: string,
+  city: string
+}
+
+create type CustomerType as closed {
+  cid: int32, 
+  name: string,
+  age: int32?,
+  address: AddressType?,
+  interests: {{string}},
+  children: [ { name: string, age: int32? } ]
+}
+
+create dataset Customers(CustomerType) partitioned by key cid;
+
+load dataset Customers 
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/semistructured/co1k/customer.adm"),("format"="adm"));
+
+create index age_index on Customers(age);
+
+delete $c from dataset Customers where $c.cid>=200;
+
+write output to nc1:"rttest/dml_scan-delete-btree-secondary-index-nullable.adm";
+
+for $c in dataset('Customers')
+where $c.age < 20
+order by $c.cid
+return $c
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable.aql
new file mode 100644
index 0000000..fc30529
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable.aql
@@ -0,0 +1,39 @@
+/* 
+ * Test case Name  : scan-delete-rtree-secondary-index-nullable.aql
+ * Description     : This test is intended to test deletion from secondary rtree indexes that are built on nullable fields 
+ * Expected Result : Success
+ * Date            : May 12 2012
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type MyRecord as closed {
+  id: int32,
+  point: point?,
+  kwds: string,
+  line1: line,
+  line2: line,
+  poly1: polygon,
+  poly2: polygon,
+  rec: rectangle
+}
+
+create dataset MyData(MyRecord)
+  partitioned by key id;
+
+load dataset MyData 
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/spatial/spatialDataNulls.json"),("format"="adm")) pre-sorted;
+
+create index rtree_index_point on MyData(point) type rtree;
+
+delete $m from dataset MyData where $m.id>10;
+
+write output to nc1:"rttest/dml_scan-delete-rtree-secondary-index-nullable.adm";
+
+for $o in dataset('MyData')
+where spatial-intersect($o.point, create-polygon(create-point(0.0,1.0), create-point(0.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+order by $o.id
+return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-btree-secondary-index-nullable.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-btree-secondary-index-nullable.aql
new file mode 100644
index 0000000..d5d22d3
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-btree-secondary-index-nullable.aql
@@ -0,0 +1,55 @@
+/* 
+ * Test case Name  : scan-delete-btree-secondary-index-nullable.aql
+ * Description     : This test is intended to test insertion into secondary btree indexes that are built on nullable fields 
+ * Expected Result : Success
+ * Date            : May 12 2012
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type AddressType as closed {
+  number: int32, 
+  street: string,
+  city: string
+}
+
+create type CustomerType as closed {
+  cid: int32, 
+  name: string,
+  age: int32?,
+  address: AddressType?,
+  interests: {{string}},
+  children: [ { name: string, age: int32? } ]
+}
+
+create dataset Customers(CustomerType) partitioned by key cid;
+create dataset CustomersMini(CustomerType) partitioned by key cid;
+
+load dataset Customers 
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/semistructured/co1k/customer.adm"),("format"="adm"));
+
+create index age_index on CustomersMini(age);
+
+insert into dataset CustomersMini
+(
+	for $c in dataset('Customers')
+	where $c.cid < 200	
+	return {
+	  "cid": $c.cid,
+  	  "name": $c.name,
+  	  "age": $c.age,
+  	  "address": $c.address,
+  	  "interests": $c.interests,
+  	  "children": $c.children
+	}	
+);
+
+write output to nc1:"rttest/dml_scan-insert-btree-secondary-index-nullable.adm";
+
+for $c in dataset('CustomersMini')
+where $c.age < 20
+order by $c.cid
+return $c
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable.aql
new file mode 100644
index 0000000..161439a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable.aql
@@ -0,0 +1,54 @@
+/* 
+ * Test case Name  : scan-insert-rtree-secondary-index-nullable.aql
+ * Description     : This test is intended to test insertion into secondary rtree indexes that are built on nullable fields 
+ * Expected Result : Success
+ * Date            : May 12 2012
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type MyRecord as closed {
+  id: int32,
+  point: point?,
+  kwds: string,
+  line1: line,
+  line2: line,
+  poly1: polygon,
+  poly2: polygon,
+  rec: rectangle
+}
+
+create type MyMiniRecord as closed {
+  id: int32,
+  point: point?
+}
+
+create dataset MyData(MyRecord)
+  partitioned by key id;
+
+load dataset MyData 
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/spatial/spatialDataNulls.json"),("format"="adm")) pre-sorted;
+
+create dataset MyMiniData(MyMiniRecord)
+  partitioned by key id;
+
+create index rtree_index_point on MyMiniData(point) type rtree;
+
+insert into dataset MyMiniData
+(
+	for $m in dataset('MyData')
+	return {
+		"id": $m.id,
+		"point": $m.point
+	}
+);
+
+write output to nc1:"rttest/dml_scan-insert-rtree-secondary-index-nullable.adm";
+
+for $o in dataset('MyMiniData')
+where spatial-intersect($o.point, create-polygon(create-point(0.0,1.0), create-point(0.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+order by $o.id
+return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/results/dml/scan-delete-btree-secondary-index-nullable.adm b/asterix-app/src/test/resources/runtimets/results/dml/scan-delete-btree-secondary-index-nullable.adm
new file mode 100644
index 0000000..a12b9b9
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/dml/scan-delete-btree-secondary-index-nullable.adm
@@ -0,0 +1,14 @@
+{ "cid": 3, "name": "Phung Wheetley", "age": 12, "address": { "number": 5549, "street": "Hill St.", "city": "Mountain View" }, "interests": {{ "Wine" }}, "children": [ { "name": "Raelene Wheetley", "age": null }, { "name": "Dudley Wheetley", "age": null } ] }
+{ "cid": 11, "name": "Meta Simek", "age": 13, "address": { "number": 4384, "street": "7th St.", "city": "San Jose" }, "interests": {{ "Wine", "Walking" }}, "children": [ { "name": "Oretha Simek", "age": null }, { "name": "Terence Simek", "age": null } ] }
+{ "cid": 52, "name": "Janna Tish", "age": 12, "address": { "number": 2598, "street": "Washington St.", "city": "San Jose" }, "interests": {{  }}, "children": [ { "name": "Mackenzie Tish", "age": null }, { "name": "Ettie Tish", "age": null }, { "name": "Hortencia Tish", "age": null }, { "name": "Paul Tish", "age": null } ] }
+{ "cid": 55, "name": "Terrence Bryant", "age": 12, "address": { "number": 3188, "street": "Park St.", "city": "Seattle" }, "interests": {{ "Wine", "Cooking" }}, "children": [ { "name": "Dayna Bryant", "age": null } ] }
+{ "cid": 61, "name": "Linsey Mose", "age": 17, "address": { "number": 9198, "street": "Lake St.", "city": "Portland" }, "interests": {{ "Puzzles" }}, "children": [ { "name": "Tilda Mose", "age": null }, { "name": "Lillie Mose", "age": null }, { "name": "Robyn Mose", "age": null } ] }
+{ "cid": 92, "name": "Kenny Laychock", "age": 15, "address": { "number": 4790, "street": "Washington St.", "city": "Portland" }, "interests": {{ "Video Games", "Basketball" }}, "children": [  ] }
+{ "cid": 111, "name": "Eddy Ortea", "age": 16, "address": { "number": 6874, "street": "Main St.", "city": "Los Angeles" }, "interests": {{  }}, "children": [ { "name": "Shera Ortea", "age": null } ] }
+{ "cid": 112, "name": "Dorie Lave", "age": 10, "address": { "number": 2286, "street": "Lake St.", "city": "Los Angeles" }, "interests": {{ "Coffee" }}, "children": [ { "name": "Grady Lave", "age": null }, { "name": "Daysi Lave", "age": null } ] }
+{ "cid": 144, "name": "Celesta Sosebee", "age": 19, "address": { "number": 2683, "street": "7th St.", "city": "Portland" }, "interests": {{ "Databases", "Databases" }}, "children": [ { "name": "Jesse Sosebee", "age": null }, { "name": "Oralee Sosebee", "age": null }, { "name": "Sunday Sosebee", "age": null } ] }
+{ "cid": 146, "name": "Glennis Vanruiten", "age": 14, "address": { "number": 8272, "street": "Park St.", "city": "Los Angeles" }, "interests": {{ "Squash", "Databases" }}, "children": [ { "name": "Joanie Vanruiten", "age": null }, { "name": "Long Vanruiten", "age": null }, { "name": "Abdul Vanruiten", "age": null } ] }
+{ "cid": 153, "name": "Randy Hueso", "age": 11, "address": { "number": 1957, "street": "Oak St.", "city": "San Jose" }, "interests": {{ "Computers", "Wine", "Databases", "Walking" }}, "children": [  ] }
+{ "cid": 186, "name": "Krystle Spangler", "age": 15, "address": { "number": 4697, "street": "Cedar St.", "city": "Seattle" }, "interests": {{ "Cigars", "Squash", "Coffee", "Video Games" }}, "children": [  ] }
+{ "cid": 192, "name": "Shakira Delmonte", "age": 10, "address": { "number": 8838, "street": "Park St.", "city": "Sunnyvale" }, "interests": {{ "Books", "Cigars", "Bass", "Base Jumping" }}, "children": [ { "name": "Sergio Delmonte", "age": null }, { "name": "Aida Delmonte", "age": null }, { "name": "Juliane Delmonte", "age": null } ] }
+{ "cid": 195, "name": "Annetta Demille", "age": 17, "address": { "number": 5722, "street": "Park St.", "city": "Portland" }, "interests": {{ "Bass" }}, "children": [ { "name": "Natacha Demille", "age": null }, { "name": "Giuseppe Demille", "age": null }, { "name": "Kami Demille", "age": null }, { "name": "Jewell Demille", "age": null } ] }
diff --git a/asterix-app/src/test/resources/runtimets/results/dml/scan-delete-rtree-secondary-index-nullable.adm b/asterix-app/src/test/resources/runtimets/results/dml/scan-delete-rtree-secondary-index-nullable.adm
new file mode 100644
index 0000000..0e739c3
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/dml/scan-delete-rtree-secondary-index-nullable.adm
@@ -0,0 +1 @@
+{ "id": 10 }
diff --git a/asterix-app/src/test/resources/runtimets/results/dml/scan-insert-btree-secondary-index-nullable.adm b/asterix-app/src/test/resources/runtimets/results/dml/scan-insert-btree-secondary-index-nullable.adm
new file mode 100644
index 0000000..a12b9b9
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/dml/scan-insert-btree-secondary-index-nullable.adm
@@ -0,0 +1,14 @@
+{ "cid": 3, "name": "Phung Wheetley", "age": 12, "address": { "number": 5549, "street": "Hill St.", "city": "Mountain View" }, "interests": {{ "Wine" }}, "children": [ { "name": "Raelene Wheetley", "age": null }, { "name": "Dudley Wheetley", "age": null } ] }
+{ "cid": 11, "name": "Meta Simek", "age": 13, "address": { "number": 4384, "street": "7th St.", "city": "San Jose" }, "interests": {{ "Wine", "Walking" }}, "children": [ { "name": "Oretha Simek", "age": null }, { "name": "Terence Simek", "age": null } ] }
+{ "cid": 52, "name": "Janna Tish", "age": 12, "address": { "number": 2598, "street": "Washington St.", "city": "San Jose" }, "interests": {{  }}, "children": [ { "name": "Mackenzie Tish", "age": null }, { "name": "Ettie Tish", "age": null }, { "name": "Hortencia Tish", "age": null }, { "name": "Paul Tish", "age": null } ] }
+{ "cid": 55, "name": "Terrence Bryant", "age": 12, "address": { "number": 3188, "street": "Park St.", "city": "Seattle" }, "interests": {{ "Wine", "Cooking" }}, "children": [ { "name": "Dayna Bryant", "age": null } ] }
+{ "cid": 61, "name": "Linsey Mose", "age": 17, "address": { "number": 9198, "street": "Lake St.", "city": "Portland" }, "interests": {{ "Puzzles" }}, "children": [ { "name": "Tilda Mose", "age": null }, { "name": "Lillie Mose", "age": null }, { "name": "Robyn Mose", "age": null } ] }
+{ "cid": 92, "name": "Kenny Laychock", "age": 15, "address": { "number": 4790, "street": "Washington St.", "city": "Portland" }, "interests": {{ "Video Games", "Basketball" }}, "children": [  ] }
+{ "cid": 111, "name": "Eddy Ortea", "age": 16, "address": { "number": 6874, "street": "Main St.", "city": "Los Angeles" }, "interests": {{  }}, "children": [ { "name": "Shera Ortea", "age": null } ] }
+{ "cid": 112, "name": "Dorie Lave", "age": 10, "address": { "number": 2286, "street": "Lake St.", "city": "Los Angeles" }, "interests": {{ "Coffee" }}, "children": [ { "name": "Grady Lave", "age": null }, { "name": "Daysi Lave", "age": null } ] }
+{ "cid": 144, "name": "Celesta Sosebee", "age": 19, "address": { "number": 2683, "street": "7th St.", "city": "Portland" }, "interests": {{ "Databases", "Databases" }}, "children": [ { "name": "Jesse Sosebee", "age": null }, { "name": "Oralee Sosebee", "age": null }, { "name": "Sunday Sosebee", "age": null } ] }
+{ "cid": 146, "name": "Glennis Vanruiten", "age": 14, "address": { "number": 8272, "street": "Park St.", "city": "Los Angeles" }, "interests": {{ "Squash", "Databases" }}, "children": [ { "name": "Joanie Vanruiten", "age": null }, { "name": "Long Vanruiten", "age": null }, { "name": "Abdul Vanruiten", "age": null } ] }
+{ "cid": 153, "name": "Randy Hueso", "age": 11, "address": { "number": 1957, "street": "Oak St.", "city": "San Jose" }, "interests": {{ "Computers", "Wine", "Databases", "Walking" }}, "children": [  ] }
+{ "cid": 186, "name": "Krystle Spangler", "age": 15, "address": { "number": 4697, "street": "Cedar St.", "city": "Seattle" }, "interests": {{ "Cigars", "Squash", "Coffee", "Video Games" }}, "children": [  ] }
+{ "cid": 192, "name": "Shakira Delmonte", "age": 10, "address": { "number": 8838, "street": "Park St.", "city": "Sunnyvale" }, "interests": {{ "Books", "Cigars", "Bass", "Base Jumping" }}, "children": [ { "name": "Sergio Delmonte", "age": null }, { "name": "Aida Delmonte", "age": null }, { "name": "Juliane Delmonte", "age": null } ] }
+{ "cid": 195, "name": "Annetta Demille", "age": 17, "address": { "number": 5722, "street": "Park St.", "city": "Portland" }, "interests": {{ "Bass" }}, "children": [ { "name": "Natacha Demille", "age": null }, { "name": "Giuseppe Demille", "age": null }, { "name": "Kami Demille", "age": null }, { "name": "Jewell Demille", "age": null } ] }
diff --git a/asterix-app/src/test/resources/runtimets/results/dml/scan-insert-rtree-secondary-index-nullable.adm b/asterix-app/src/test/resources/runtimets/results/dml/scan-insert-rtree-secondary-index-nullable.adm
new file mode 100644
index 0000000..4bd8c19
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/dml/scan-insert-rtree-secondary-index-nullable.adm
@@ -0,0 +1,2 @@
+{ "id": 10 }
+{ "id": 12 }
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlMetadataProvider.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlMetadataProvider.java
index 33d36bd..856028c 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlMetadataProvider.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlMetadataProvider.java
@@ -1010,8 +1010,9 @@
 		AqlCompiledIndexDecl cid = DatasetUtils.findSecondaryIndexByName(
 				compiledDatasetDecl, indexName);
 		List<String> secondaryKeyExprs = cid.getFieldExprs();
-		IAType spatialType = AqlCompiledIndexDecl.keyFieldType(
-				secondaryKeyExprs.get(0).toString(), recType);
+		Pair<IAType, Boolean> keyPairType = AqlCompiledIndexDecl.getNonNullableKeyFieldType(
+				secondaryKeyExprs.get(0), recType);
+        IAType spatialType = keyPairType.first;
 		int dimension = NonTaggedFormatUtil.getNumDimensions(spatialType
 				.getTypeTag());
 		int numSecondaryKeys = dimension * 2;
@@ -1068,8 +1069,8 @@
 				appContext.getIndexRegistryProvider(),
 				splitsAndConstraint.first, typeTraits, comparatorFactories,
 				fieldPermutation, indexOp, new RTreeDataflowHelperFactory(
-						valueProviderFactories),
-				null, NoOpOperationCallbackProvider.INSTANCE, txnId);
+						valueProviderFactories), filterFactory,
+				NoOpOperationCallbackProvider.INSTANCE, txnId);
 		return new Pair<IOperatorDescriptor, AlgebricksPartitionConstraint>(
 				rtreeUpdate, splitsAndConstraint.second);
 	}
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/SpatialIntersectDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/SpatialIntersectDescriptor.java
index ea5340c..2d639af 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/SpatialIntersectDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/SpatialIntersectDescriptor.java
@@ -776,6 +776,9 @@
                                         case RECTANGLE:
                                             res = pointInRectangle(outInput0.getBytes(), outInput1.getBytes());
                                             break;
+                                        case NULL:
+                                            res = false;
+                                            break;
                                         default:
                                             throw new NotImplementedException(
                                                     "spatial-intersection does not support the type: "
@@ -826,6 +829,9 @@
                                         case RECTANGLE:
                                             res = lineRectangleIntersection(outInput0.getBytes(), outInput1.getBytes());
                                             break;
+                                        case NULL:
+                                            res = false;
+                                            break;
                                         default:
                                             throw new NotImplementedException(
                                                     "spatial-intersection does not support the type: "
@@ -922,6 +928,9 @@
                                             res = rectanglePolygonIntersection(outInput1.getBytes(),
                                                     outInput0.getBytes());
                                             break;
+                                        case NULL:
+                                            res = false;
+                                            break;
                                         default:
                                             throw new NotImplementedException(
                                                     "spatial-intersection does not support the type: "
@@ -947,6 +956,9 @@
                                             res = rectangleCircleIntersection(outInput1.getBytes(),
                                                     outInput0.getBytes());
                                             break;
+                                        case NULL:
+                                            res = false;
+                                            break;
                                         default:
                                             throw new NotImplementedException(
                                                     "spatial-intersection does not support the type: "
@@ -997,6 +1009,9 @@
                                                 }
                                             }
                                             break;
+                                        case NULL:
+                                            res = false;
+                                            break;
                                         default:
                                             throw new NotImplementedException(
                                                     "spatial-intersection does not support the type: "
@@ -1004,6 +1019,9 @@
                                                             + " It is only implemented for POINT, ALINE, POLYGON, and CIRCLE.");
                                     }
                                     break;
+                                case NULL:
+                                    res = false;
+                                    break;
                                 default:
                                     throw new NotImplementedException(
                                             "spatial-intersection does not support the type: " + tag0