fix issue218
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IfElseToSwitchCaseFunctionRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IfElseToSwitchCaseFunctionRule.java
index f4f36f5..94711d1 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IfElseToSwitchCaseFunctionRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IfElseToSwitchCaseFunctionRule.java
@@ -17,6 +17,7 @@
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalVariable;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
@@ -33,7 +34,8 @@
     }
 
     @Override
-    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
+    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
+            throws AlgebricksException {
         AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
         if (op1.getOperatorTag() != LogicalOperatorTag.ASSIGN)
             return false;
@@ -64,8 +66,7 @@
                 return false;
             SelectOperator selectOp = (SelectOperator) nestedRoot;
 
-            AbstractLogicalOperator nestedNextOp = (AbstractLogicalOperator) nestedRoot.getInputs().get(0)
-                    .getValue();
+            AbstractLogicalOperator nestedNextOp = (AbstractLogicalOperator) nestedRoot.getInputs().get(0).getValue();
             if (nestedNextOp.getOperatorTag() != LogicalOperatorTag.ASSIGN)
                 return false;
             AssignOperator assignRoot = (AssignOperator) nestedNextOp;
@@ -73,8 +74,7 @@
 
             arguments.add(selectOp.getCondition());
             arguments.add(actionExprRef);
-            AbstractLogicalOperator nestedBottomOp = (AbstractLogicalOperator) assignRoot.getInputs().get(0)
-                    .getValue();
+            AbstractLogicalOperator nestedBottomOp = (AbstractLogicalOperator) assignRoot.getInputs().get(0).getValue();
 
             if (nestedBottomOp.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE)
                 return false;
@@ -86,12 +86,21 @@
 
         AssignOperator bottomAssign = (AssignOperator) op3;
         LogicalVariable conditionVar = bottomAssign.getVariables().get(0);
-        Mutable<ILogicalExpression> switchCondition = new MutableObject<ILogicalExpression>(new VariableReferenceExpression(
-                conditionVar));
+        Mutable<ILogicalExpression> switchCondition = new MutableObject<ILogicalExpression>(
+                new VariableReferenceExpression(conditionVar));
         List<Mutable<ILogicalExpression>> argumentRefs = new ArrayList<Mutable<ILogicalExpression>>();
         argumentRefs.add(switchCondition);
         argumentRefs.addAll(arguments);
 
+        /** replace the branch conditions */
+        for (int i = 0; i < arguments.size(); i += 2) {
+            if (arguments.get(i).getValue().equals(switchCondition.getValue())) {
+                arguments.get(i).setValue(ConstantExpression.TRUE);
+            } else {
+                arguments.get(i).setValue(ConstantExpression.FALSE);
+            }
+        }
+
         ILogicalExpression callExpr = new ScalarFunctionCallExpression(
                 FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.SWITCH_CASE), argumentRefs);
 
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
index fc4bb03d7..7944bc3 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
@@ -1118,7 +1118,7 @@
                     Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, src);
 
                     if (((AbstractLogicalOperator) p.first).getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
-                        // src.setOperator(topOp.getOperator());
+                        src.setValue(topOp.getValue());
                         Mutable<ILogicalOperator> top2 = new MutableObject<ILogicalOperator>(p.first);
                         return new Pair<ILogicalExpression, Mutable<ILogicalOperator>>(new VariableReferenceExpression(
                                 p.second), top2);
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.1.ddl.aql
new file mode 100644
index 0000000..257cbcf
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.1.ddl.aql
@@ -0,0 +1,17 @@
+/*
+ * Description  : This test case is to verify the fix for issue218
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=218
+ * Expected Res : Success
+ * Date         : 5th June 2013
+ */
+ 
+drop dataverse test if exists;
+create dataverse test;
+
+create function test.computeBonus($pbcRating,$salary)
+{
+   if ($pbcRating = 1) then
+        $salary * 0.25
+   else
+        $salary * 0.10
+}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.2.update.aql
new file mode 100644
index 0000000..c5732be
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.2.update.aql
@@ -0,0 +1,6 @@
+/*
+ * Description  : This test case is to verify the fix for issue218
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=218
+ * Expected Res : Success
+ * Date         : 5th June 2013
+ */
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.3.query.aql
new file mode 100644
index 0000000..ee6a31e
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218-2/query-issue218-2.3.query.aql
@@ -0,0 +1,8 @@
+/*
+ * Description  : This test case is to verify the fix for issue218
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=218
+ * Expected Res : Success
+ * Date         : 5th June 2013
+ */
+
+test.computeBonus(-1,-1);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.1.ddl.aql
new file mode 100644
index 0000000..c5732be
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.1.ddl.aql
@@ -0,0 +1,6 @@
+/*
+ * Description  : This test case is to verify the fix for issue218
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=218
+ * Expected Res : Success
+ * Date         : 5th June 2013
+ */
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.2.update.aql
new file mode 100644
index 0000000..c5732be
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.2.update.aql
@@ -0,0 +1,6 @@
+/*
+ * Description  : This test case is to verify the fix for issue218
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=218
+ * Expected Res : Success
+ * Date         : 5th June 2013
+ */
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.3.query.aql
new file mode 100644
index 0000000..3938b75
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue218/query-issue218.3.query.aql
@@ -0,0 +1,13 @@
+/*
+ * Description  : This test case is to verify the fix for issue218
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=218
+ * Expected Res : Success
+ * Date         : 5th June 2013
+ */
+
+let $a:= let $temp:=1
+               return (
+               let $z:=$temp
+               return $z
+               )
+return $a
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue218-2/query-issue218-2.1.adm b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue218-2/query-issue218-2.1.adm
new file mode 100644
index 0000000..b5135a2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue218-2/query-issue218-2.1.adm
@@ -0,0 +1 @@
+-0.1
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue218/query-issue218.1.adm b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue218/query-issue218.1.adm
new file mode 100644
index 0000000..56a6051
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue218/query-issue218.1.adm
@@ -0,0 +1 @@
+1
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index d6c6733..01301a5 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -4033,6 +4033,16 @@
     </test-case>
   </test-group>
   <test-group name="user-defined-functions">
+  	<test-case FilePath="user-defined-functions">
+      <compilation-unit name="query-issue218-2">
+        <output-dir compare="Text">query-issue218-2</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="user-defined-functions">
+      <compilation-unit name="query-issue218">
+        <output-dir compare="Text">query-issue218</output-dir>
+      </compilation-unit>
+    </test-case>
     <test-case FilePath="user-defined-functions">
       <compilation-unit name="query-issue201">
         <output-dir compare="Text">query-issue201</output-dir>