fix issue 134

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_printerfix@923 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java
index c1d0fea..7b311d3 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java
@@ -37,7 +37,10 @@
 import edu.uci.ics.asterix.om.base.IAObject;
 import edu.uci.ics.asterix.om.constants.AsterixConstantValue;
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.ARecordType;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.AbstractCollectionType;
 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;
@@ -168,6 +171,13 @@
                     || expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.CAST_RECORD)) {
                 return new Pair<Boolean, ILogicalExpression>(false, null);
             }
+            if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR)
+                    || expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.ORDERED_LIST_CONSTRUCTOR)) {
+                AbstractCollectionType listType = (AbstractCollectionType) TypeComputerUtilities.getRequiredType(expr);
+                if (listType != null && listType.getItemType().getTypeTag() == ATypeTag.ANY) {
+                    return new Pair<Boolean, ILogicalExpression>(false, null);
+                }
+            }
             if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
                 ARecordType rt = (ARecordType) _emptyTypeEnv.getType(expr.getArguments().get(0).getValue());
                 String str = ((AString) ((AsterixConstantValue) ((ConstantExpression) expr.getArguments().get(1)
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/IntroduceStaticTypeCastRule.java
index a3edd9a..17944bb 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/IntroduceStaticTypeCastRule.java
@@ -47,9 +47,11 @@
 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.expressions.UnnestingFunctionCallExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
 import edu.uci.ics.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 
@@ -140,6 +142,23 @@
             LogicalVariable inputVar = producedVariables.get(0);
             IVariableTypeEnvironment env = assignOp.computeOutputTypeEnvironment(context);
             inputRecordType = (IAType) env.getVarType(inputVar);
+        } else if (op1.getOperatorTag() == LogicalOperatorTag.WRITE) {
+            AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
+            if (op2.getOperatorTag() != LogicalOperatorTag.PROJECT)
+                return false;
+            assignOp = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
+            if (assignOp.getOperatorTag() != LogicalOperatorTag.UNNEST)
+                return false;
+            IVariableTypeEnvironment env = assignOp.computeOutputTypeEnvironment(context);
+            UnnestOperator unnestOp = (UnnestOperator) assignOp;
+            ILogicalExpression unnestExpr = unnestOp.getExpressionRef().getValue();
+            UnnestingFunctionCallExpression unnestExpression = (UnnestingFunctionCallExpression) unnestExpr;
+            AbstractFunctionCallExpression unnestArgExpr = (AbstractFunctionCallExpression) unnestExpression
+                    .getArguments().get(0).getValue();
+            inputRecordType = (IAType) env.getType(unnestArgExpr);
+            rewriteFuncExpr(unnestArgExpr, inputRecordType, inputRecordType, env);
+            context.computeAndSetTypeEnvironmentForOperator(unnestOp);
+            return true;
         } else {
             return false;
         }
@@ -191,7 +210,7 @@
         return true;
     }
 
-    private void rewriteFuncExpr(ScalarFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
+    private void rewriteFuncExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
             IVariableTypeEnvironment env) throws AlgebricksException {
         if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR) {
             if (reqType.equals(BuiltinType.ANY)) {
@@ -223,7 +242,7 @@
      *            type environment
      * @throws AlgebricksException
      */
-    private void rewriteRecordFuncExpr(ScalarFunctionCallExpression funcExpr, ARecordType requiredRecordType,
+    private void rewriteRecordFuncExpr(AbstractFunctionCallExpression funcExpr, ARecordType requiredRecordType,
             ARecordType inputRecordType, IVariableTypeEnvironment env) throws AlgebricksException {
         // if already rewritten, the required type is not null
         if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
@@ -244,7 +263,7 @@
      *            type environment
      * @throws AlgebricksException
      */
-    private void rewriteListFuncExpr(ScalarFunctionCallExpression funcExpr, AbstractCollectionType requiredListType,
+    private void rewriteListFuncExpr(AbstractFunctionCallExpression funcExpr, AbstractCollectionType requiredListType,
             AbstractCollectionType inputListType, IVariableTypeEnvironment env) throws AlgebricksException {
         if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
             return;
@@ -269,7 +288,7 @@
         }
     }
 
-    private void staticRecordTypeCast(ScalarFunctionCallExpression func, ARecordType reqType, ARecordType inputType,
+    private void staticRecordTypeCast(AbstractFunctionCallExpression func, ARecordType reqType, ARecordType inputType,
             IVariableTypeEnvironment env) throws AlgebricksException {
         IAType[] reqFieldTypes = reqType.getFieldTypes();
         String[] reqFieldNames = reqType.getFieldNames();
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue134.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue134.aql
new file mode 100644
index 0000000..77dd8b5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue134.aql
@@ -0,0 +1,4 @@
+write output to nc1:"rttest/open-closed_query-issue134.adm";
+
+let $a:=true
+return {{[1,2,3,4,5],[6,5,3,8,9],[44,22,66,-1,0,99.9]}}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue134.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue134.adm
new file mode 100644
index 0000000..0d06066
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue134.adm
@@ -0,0 +1 @@
+{{ [ 1, 2, 3, 4, 5 ], [ 6, 5, 3, 8, 9 ], [ 44, 22, 66, -1, 0, 99.9d ] }}
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index af49e84..8fe6856 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -2299,6 +2299,11 @@
         <output-file compare="Text">open-closed-14.adm</output-file>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue134">
+        <output-file compare="Text">query-issue134.adm</output-file>
+      </compilation-unit>
+    </test-case>
     <!--
     <test-case FilePath="open-closed">
       <compilation-unit name="open-closed-15">