Merge branch 'master' of https://code.google.com/p/asterixdb
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 783fbaa..1f3801c 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
@@ -34,7 +34,7 @@
 import edu.uci.ics.asterix.optimizer.rules.IntroduceInstantLockSearchCallbackRule;
 import edu.uci.ics.asterix.optimizer.rules.IntroduceRapidFrameFlushProjectRule;
 import edu.uci.ics.asterix.optimizer.rules.IntroduceSecondaryIndexInsertDeleteRule;
-import edu.uci.ics.asterix.optimizer.rules.IntroduceStaticTypeCastRule;
+import edu.uci.ics.asterix.optimizer.rules.IntroduceStaticTypeCastForInsertRule;
 import edu.uci.ics.asterix.optimizer.rules.LoadRecordFieldsRule;
 import edu.uci.ics.asterix.optimizer.rules.NestGroupByRule;
 import edu.uci.ics.asterix.optimizer.rules.NestedSubplanToJoinRule;
@@ -122,7 +122,7 @@
         // IntroduceStaticTypeCastRule should go before
         // IntroduceDynamicTypeCastRule to
         // avoid unnecessary dynamic casting
-        normalization.add(new IntroduceStaticTypeCastRule());
+        normalization.add(new IntroduceStaticTypeCastForInsertRule());
         normalization.add(new IntroduceDynamicTypeCastRule());
         normalization.add(new IntroduceEnforcedListTypeRule());
         normalization.add(new ConstantFoldingRule());
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java
index 2e8d530..a07583f 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java
@@ -20,6 +20,7 @@
 
 import org.apache.commons.lang3.mutable.Mutable;
 
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.optimizer.rules.typecast.StaticTypeCastUtil;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -27,7 +28,6 @@
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.IOptimizationContext;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
-import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
 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.operators.logical.AbstractAssignOperator;
@@ -53,26 +53,27 @@
             throws AlgebricksException {
         if (context.checkIfInDontApplySet(this, opRef.getValue()))
             return false;
-        AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
+        AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
         context.addToDontApplySet(this, opRef.getValue());
-        boolean changed = false;
 
         /**
          * rewrite list constructor types for list constructor functions
          */
-        if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
-            AbstractAssignOperator assignOp = (AbstractAssignOperator) op1;
-            List<Mutable<ILogicalExpression>> expressions = assignOp.getExpressions();
-            IVariableTypeEnvironment env = assignOp.computeOutputTypeEnvironment(context);
-            changed = rewriteExpressions(expressions, env);
+        List<Mutable<ILogicalExpression>> expressions;
+        switch (op.getOperatorTag()) {
+            case ASSIGN:
+                AbstractAssignOperator assignOp = (AbstractAssignOperator) op;
+                expressions = assignOp.getExpressions();
+                break;
+            case UNNEST:
+                AbstractUnnestOperator unnestOp = (AbstractUnnestOperator) op;
+                expressions = Collections.singletonList(unnestOp.getExpressionRef());
+                break;
+            default:
+                return false;
         }
-        if (op1.getOperatorTag() == LogicalOperatorTag.UNNEST) {
-            AbstractUnnestOperator unnestOp = (AbstractUnnestOperator) op1;
-            List<Mutable<ILogicalExpression>> expressions = Collections.singletonList(unnestOp.getExpressionRef());
-            IVariableTypeEnvironment env = unnestOp.computeOutputTypeEnvironment(context);
-            changed = rewriteExpressions(expressions, env);
-        }
-        return changed;
+        IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context);
+        return rewriteExpressions(expressions, env);
     }
 
     private boolean rewriteExpressions(List<Mutable<ILogicalExpression>> expressions, IVariableTypeEnvironment env)
@@ -83,7 +84,10 @@
             if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                 AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) expr;
                 IAType exprType = (IAType) env.getType(argFuncExpr);
-                changed = changed || StaticTypeCastUtil.rewriteListExpr(argFuncExpr, exprType, exprType, env);
+                if (StaticTypeCastUtil.rewriteListExpr(argFuncExpr, exprType, exprType, env)) {
+                    TypeComputerUtilities.resetRequiredAndInputTypes(argFuncExpr);
+                    changed = true;
+                }
             }
         }
         return changed;
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastForInsertRule.java
similarity index 98%
rename from asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
rename to asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastForInsertRule.java
index f42782b..850f01d 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastForInsertRule.java
@@ -60,7 +60,7 @@
  * ConstantFoldingRule uses ARecordSerializerDeserializer which seems to have
  * some problem.
  */
-public class IntroduceStaticTypeCastRule implements IAlgebraicRewriteRule {
+public class IntroduceStaticTypeCastForInsertRule implements IAlgebraicRewriteRule {
 
     @Override
     public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
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 282d26b..e5d6bfc 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
@@ -47,6 +47,7 @@
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 import edu.uci.ics.hyracks.algebricks.core.algebra.functions.IFunctionInfo;
 
 /**
@@ -98,7 +99,7 @@
                 if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                     AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) argExpr;
                     IAType exprType = (IAType) env.getType(argFuncExpr);
-                    changed = changed || rewriteListExpr(argFuncExpr, exprType, exprType, env);
+                    changed = rewriteListExpr(argFuncExpr, exprType, exprType, env) || changed;
                 }
             }
             return changed;
@@ -163,7 +164,7 @@
                 }
             }
             if (!compatible(reqType, inputType)) {
-                throw new AlgebricksException("type mistmach, requred: " + reqType.toString() + " actual: "
+                throw new AlgebricksException("type mismatch, required: " + reqType.toString() + " actual: "
                         + inputType.toString());
             }
             return changed;
@@ -219,20 +220,21 @@
 
         IAType itemType = requiredListType.getItemType();
         IAType inputItemType = inputListType.getItemType();
+        boolean changed = false;
         for (int j = 0; j < args.size(); j++) {
             ILogicalExpression arg = args.get(j).getValue();
-            if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) arg;
-                IAType currentItemType = (IAType) env.getType(argFunc);
-                if (inputItemType == null || inputItemType == BuiltinType.ANY) {
-                    currentItemType = (IAType) env.getType(argFunc);
-                    rewriteFuncExpr(argFunc, itemType, currentItemType, env);
-                } else {
-                    rewriteFuncExpr(argFunc, itemType, inputItemType, env);
-                }
+            IAType currentItemType = (inputItemType == null || inputItemType == BuiltinType.ANY) ? (IAType) env.getType(arg) : inputItemType;
+            switch (arg.getExpressionTag()) {
+                case FUNCTION_CALL:
+                    ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) arg;
+                    changed = rewriteFuncExpr(argFunc, itemType, currentItemType, env) || changed;
+                    break;
+                case VARIABLE:
+                    changed = injectCastToRelaxType(args.get(j), currentItemType, env) || changed;
+                    break;
             }
         }
-        return true;
+        return changed;
     }
 
     /**
@@ -426,67 +428,68 @@
             if (openFields[i]) {
                 arguments.add(originalArguments.get(2 * i));
                 Mutable<ILogicalExpression> expRef = originalArguments.get(2 * i + 1);
-                ILogicalExpression argExpr = expRef.getValue();
-                List<LogicalVariable> parameterVars = new ArrayList<LogicalVariable>();
-                argExpr.getUsedVariables(parameterVars);
-                // we need to handle open fields recursively by their default
-                // types
-                // for list, their item type is any
-                // for record, their
-                boolean castInjected = false;
-                if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL
-                        || argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
-                    IAType reqFieldType = inputFieldTypes[i];
-                    // do not enforce nested type in the case of no-used variables
-                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.RECORD) {
-                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
-                        if (!inputFieldTypes[i].equals(reqFieldType) && parameterVars.size() > 0) {
-                            //inject dynamic type casting
-                            injectCastFunction(FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.CAST_RECORD),
-                                    reqFieldType, inputFieldTypes[i], expRef, argExpr);
-                            castInjected = true;
-                        }
-                    }
-                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.ORDEREDLIST) {
-                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
-                        if (!inputFieldTypes[i].equals(reqFieldType) && parameterVars.size() > 0) {
-                            //inject dynamic type casting
-                            injectCastFunction(FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.CAST_LIST),
-                                    reqFieldType, inputFieldTypes[i], expRef, argExpr);
-                            castInjected = true;
-                        }
-                    }
-                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.UNORDEREDLIST) {
-                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
-                        if (!inputFieldTypes[i].equals(reqFieldType) && parameterVars.size() > 0) {
-                            //inject dynamic type casting
-                            injectCastFunction(FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.CAST_LIST),
-                                    reqFieldType, inputFieldTypes[i], expRef, argExpr);
-                            castInjected = true;
-                        }
-                    }
-                    if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                        //recursively rewrite function arguments
-                        if (TypeComputerUtilities.getRequiredType((AbstractFunctionCallExpression) argExpr) == null
-                                && reqFieldType != null) {
-                            if (castInjected) {
-                                //rewrite the arg expression inside the dynamic cast
-                                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
-                                rewriteFuncExpr(argFunc, inputFieldTypes[i], inputFieldTypes[i], env);
-                            } else {
-                                //rewrite arg
-                                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
-                                rewriteFuncExpr(argFunc, reqFieldType, inputFieldTypes[i], env);
-                            }
-                        }
-                    }
-                }
+                injectCastToRelaxType(expRef, inputFieldTypes[i], env);
                 arguments.add(expRef);
             }
         }
         return true;
     }
 
+    private static boolean injectCastToRelaxType(Mutable<ILogicalExpression> expRef, IAType inputFieldType,
+            IVariableTypeEnvironment env) throws AlgebricksException {
+        ILogicalExpression argExpr = expRef.getValue();
+        List<LogicalVariable> parameterVars = new ArrayList<LogicalVariable>();
+        argExpr.getUsedVariables(parameterVars);
+        // we need to handle open fields recursively by their default
+        // types
+        // for list, their item type is any
+        // for record, their
+        boolean castInjected = false;
+        if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL
+                || argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+            IAType reqFieldType = inputFieldType;
+            FunctionIdentifier fi = null;
+            // do not enforce nested type in the case of no-used variables
+            switch (inputFieldType.getTypeTag()) {
+                case RECORD:
+                    reqFieldType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
+                    fi = AsterixBuiltinFunctions.CAST_RECORD;
+                    break;
+                case ORDEREDLIST:
+                    reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
+                    fi = AsterixBuiltinFunctions.CAST_LIST;
+                    break;
+                case UNORDEREDLIST:
+                    reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
+                    fi = AsterixBuiltinFunctions.CAST_LIST;
+            }
+            if (fi != null
+                    && ! inputFieldType.equals(reqFieldType)
+                    && parameterVars.size() > 0) {
+                //inject dynamic type casting
+                injectCastFunction(FunctionUtils.getFunctionInfo(fi),
+                        reqFieldType, inputFieldType, expRef, argExpr);
+                castInjected = true;
+            }
+            if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                //recursively rewrite function arguments
+                if (TypeComputerUtilities.getRequiredType((AbstractFunctionCallExpression) argExpr) == null
+                        && reqFieldType != null) {
+                    if (castInjected) {
+                        //rewrite the arg expression inside the dynamic cast
+                        ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
+                        rewriteFuncExpr(argFunc, inputFieldType, inputFieldType, env);
+                    } else {
+                        //rewrite arg
+                        ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
+                        rewriteFuncExpr(argFunc, reqFieldType, inputFieldType, env);
+                    }
+                }
+            }
+        }
+        return castInjected;
+    }
+
     /**
      * Inject a dynamic cast function wrapping an existing expression
      * 
@@ -516,7 +519,7 @@
      *            the required type
      * @param inputType
      *            the input type
-     * @return true if the two types are compatiable; false otherwise
+     * @return true if the two types are compatible; false otherwise
      */
     public static boolean compatible(IAType reqType, IAType inputType) {
         if (reqType.getTypeTag() == ATypeTag.ANY || inputType.getTypeTag() == ATypeTag.ANY) {
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.1.ddl.aql
new file mode 100644
index 0000000..edd97cd
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.1.ddl.aql
@@ -0,0 +1,20 @@
+/*
+ * Description  : This test case is to verify the fix for issue456: 
+ *                https://code.google.com/p/asterixdb/issues/detail?id=456
+ * Expected Res : SUCCESS
+ * Date         : 3rd June 2013
+ */
+ 
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type TypeOpen as open {
+  id : int32,
+  int_m : int32,
+  int_o : int32?,
+  string_m : string,
+  string_o : string?
+};
+
+create dataset DataOpen(TypeOpen) primary key id;
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.2.update.aql
new file mode 100644
index 0000000..e084b44
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.2.update.aql
@@ -0,0 +1,25 @@
+/*
+ * Description  : This test case is to verify the fix for issue456: 
+ *                https://code.google.com/p/asterixdb/issues/detail?id=456
+ * Expected Res : SUCCESS
+ * Date         : 3rd June 2013
+ */
+
+use dataverse test;
+
+insert into dataset DataOpen({ "id": 0, "int_m": 1, "int_o": 1, "string_m": "a", "string_o": "a" });
+insert into dataset DataOpen({ "id": 1, "int_m": 1, "int_o": 1, "string_m": "a", "string_o": null });
+insert into dataset DataOpen({ "id": 2, "int_m": 1, "int_o": 1, "string_m": "b", "string_o": "a" });
+insert into dataset DataOpen({ "id": 3, "int_m": 1, "int_o": 1, "string_m": "b", "string_o": null });
+insert into dataset DataOpen({ "id": 4, "int_m": 1, "int_o": null, "string_m": "a", "string_o": "a" });
+insert into dataset DataOpen({ "id": 5, "int_m": 1, "int_o": null, "string_m": "a", "string_o": null });
+insert into dataset DataOpen({ "id": 6, "int_m": 1, "int_o": null, "string_m": "b", "string_o": "a" });
+insert into dataset DataOpen({ "id": 7, "int_m": 1, "int_o": null, "string_m": "b", "string_o": null });
+insert into dataset DataOpen({ "id": 8, "int_m": 2, "int_o": 1, "string_m": "a", "string_o": "a" });
+insert into dataset DataOpen({ "id": 9, "int_m": 2, "int_o": 1, "string_m": "a", "string_o": null });
+insert into dataset DataOpen({ "id": 10, "int_m": 2, "int_o": 1, "string_m": "b", "string_o": "a" });
+insert into dataset DataOpen({ "id": 11, "int_m": 2, "int_o": 1, "string_m": "b", "string_o": null });
+insert into dataset DataOpen({ "id": 12, "int_m": 2, "int_o": null, "string_m": "a", "string_o": "a" });
+insert into dataset DataOpen({ "id": 13, "int_m": 2, "int_o": null, "string_m": "a", "string_o": null });
+insert into dataset DataOpen({ "id": 14, "int_m": 2, "int_o": null, "string_m": "b", "string_o": "a" });
+insert into dataset DataOpen({ "id": 15, "int_m": 2, "int_o": null, "string_m": "b", "string_o": null });
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.3.query.aql
new file mode 100644
index 0000000..217fbd1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue456/query-issue456.3.query.aql
@@ -0,0 +1,13 @@
+/*
+ * Description  : This test case is to verify the fix for issue456: 
+ *                https://code.google.com/p/asterixdb/issues/detail?id=456
+ * Expected Res : SUCCESS
+ * Date         : 3rd June 2013
+ */
+
+use dataverse test;
+
+for $x in dataset DataOpen
+let $id := $x.id
+group by $m := $x.int_m with $id
+return [ $m, count($id) ]
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.1.ddl.aql
new file mode 100644
index 0000000..9f5fb23
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.1.ddl.aql
@@ -0,0 +1,7 @@
+/*
+ * Description  : This test case is to verify the fix for issue465: 
+ *                https://code.google.com/p/asterixdb/issues/detail?id=465
+ * Expected Res : SUCCESS
+ * Date         : 3rd June 2013
+ */
+ 
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.2.update.aql
new file mode 100644
index 0000000..9f5fb23
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.2.update.aql
@@ -0,0 +1,7 @@
+/*
+ * Description  : This test case is to verify the fix for issue465: 
+ *                https://code.google.com/p/asterixdb/issues/detail?id=465
+ * Expected Res : SUCCESS
+ * Date         : 3rd June 2013
+ */
+ 
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.3.query.aql
new file mode 100644
index 0000000..244b193
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue465/query-issue465.3.query.aql
@@ -0,0 +1,10 @@
+/*
+ * Description  : This test case is to verify the fix for issue465: 
+ *                https://code.google.com/p/asterixdb/issues/detail?id=465
+ * Expected Res : SUCCESS
+ * Date         : 3rd June 2013
+ */
+
+let $a := {"r1":1234}
+let $b := {"r2":456}
+return [ $a, $b ]
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue456/query-issue456.1.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue456/query-issue456.1.adm
new file mode 100644
index 0000000..0ca7b82
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue456/query-issue456.1.adm
@@ -0,0 +1,2 @@
+[ 1, 8 ]
+[ 2, 8 ]
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue465/query-issue465.1.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue465/query-issue465.1.adm
new file mode 100644
index 0000000..15c3035
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue465/query-issue465.1.adm
@@ -0,0 +1 @@
+[ { "r1": 1234 }, { "r2": 456 } ]
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 0f3e005..6f315cf 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -2718,12 +2718,22 @@
     </test-case>
     <test-case FilePath="open-closed">
       <compilation-unit name="query-issue453">
-	<output-dir compare="Text">query-issue453</output-dir>
+        <output-dir compare="Text">query-issue453</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="open-closed">
       <compilation-unit name="query-issue453-2">
-	<output-dir compare="Text">query-issue453-2</output-dir>
+        <output-dir compare="Text">query-issue453-2</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue456"> 
+        <output-dir compare="Text">query-issue456</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue465"> 
+        <output-dir compare="Text">query-issue465</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="open-closed">
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 2c7ff10..84d177f 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
@@ -23,9 +23,9 @@
     public static boolean setRequiredAndInputTypes(AbstractFunctionCallExpression expr, IAType requiredRecordType,
             IAType inputRecordType) {
         boolean changed = false;
-        Object opaqueParameter = expr.getOpaqueParameters();
-        if (opaqueParameter == null) {
-            Object[] opaqueParameters = new Object[2];
+        Object[] opaqueParameters = expr.getOpaqueParameters();
+        if (opaqueParameters == null) {
+            opaqueParameters = new Object[2];
             opaqueParameters[0] = requiredRecordType;
             opaqueParameters[1] = inputRecordType;
             expr.setOpaqueParameters(opaqueParameters);
@@ -34,6 +34,10 @@
         return changed;
     }
 
+    public static void resetRequiredAndInputTypes(AbstractFunctionCallExpression expr) {
+        expr.setOpaqueParameters(null);
+    }
+
     public static IAType getRequiredType(AbstractFunctionCallExpression expr) {
         Object[] type = expr.getOpaqueParameters();
         if (type != null) {