Merge branch 'master' into jimahn/master_issue536
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 42a8695..6f92d3a 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
@@ -81,7 +81,6 @@
 import edu.uci.ics.asterix.aql.expression.UpdateStatement;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
@@ -258,11 +257,6 @@
             ILogicalOperator leafOperator = null;
 
             switch (stmt.getKind()) {
-                case WRITE_FROM_QUERY_RESULT: {
-                    leafOperator = new WriteResultOperator(targetDatasource, varRef, varRefsForLoading);
-                    leafOperator.getInputs().add(new MutableObject<ILogicalOperator>(assign));
-                    break;
-                }
                 case INSERT: {
                     ILogicalOperator insertOp = new InsertDeleteOperator(targetDatasource, varRef, varRefsForLoading,
                             InsertDeleteOperator.Kind.INSERT);
@@ -1268,13 +1262,6 @@
     }
 
     @Override
-    public Pair<ILogicalOperator, LogicalVariable> visitWriteFromQueryResultStatement(
-            WriteFromQueryResultStatement stmtLoad, Mutable<ILogicalOperator> arg) throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Pair<ILogicalOperator, LogicalVariable> visitDropStatement(DropStatement del, Mutable<ILogicalOperator> arg)
             throws AsterixException {
         // TODO Auto-generated method stub
@@ -1389,13 +1376,6 @@
     }
 
     @Override
-    public Pair<ILogicalOperator, LogicalVariable> visitLoadFromQueryResultStatement(
-            WriteFromQueryResultStatement stmtLoad, Mutable<ILogicalOperator> arg) throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Pair<ILogicalOperator, LogicalVariable> visitCreateDataverseStatement(CreateDataverseStatement del,
             Mutable<ILogicalOperator> arg) throws AsterixException {
         // TODO Auto-generated method stub
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java
index 866ad49..d1f8053 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java
@@ -85,7 +85,6 @@
 import edu.uci.ics.asterix.aql.expression.UpdateStatement;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlPlusExpressionVisitor;
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
@@ -1214,13 +1213,6 @@
     }
 
     @Override
-    public Pair<ILogicalOperator, LogicalVariable> visitWriteFromQueryResultStatement(
-            WriteFromQueryResultStatement stmtLoad, Mutable<ILogicalOperator> arg) throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Pair<ILogicalOperator, LogicalVariable> visitDropStatement(DropStatement del, Mutable<ILogicalOperator> arg)
             throws AsterixException {
         // TODO Auto-generated method stub
@@ -1395,13 +1387,6 @@
     }
 
     @Override
-    public Pair<ILogicalOperator, LogicalVariable> visitLoadFromQueryResultStatement(
-            WriteFromQueryResultStatement stmtLoad, Mutable<ILogicalOperator> arg) throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Pair<ILogicalOperator, LogicalVariable> visitCreateDataverseStatement(CreateDataverseStatement del,
             Mutable<ILogicalOperator> arg) throws AsterixException {
         // TODO Auto-generated method stub
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/CompiledStatements.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/CompiledStatements.java
index e40a163..36f0824 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/CompiledStatements.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/CompiledStatements.java
@@ -55,44 +55,6 @@
         public Kind getKind();
     }
 
-    public static class CompiledWriteFromQueryResultStatement implements ICompiledDmlStatement {
-
-        private String dataverseName;
-        private String datasetName;
-        private Query query;
-        private int varCounter;
-
-        public CompiledWriteFromQueryResultStatement(String dataverseName, String datasetName, Query query,
-                int varCounter) {
-            this.dataverseName = dataverseName;
-            this.datasetName = datasetName;
-            this.query = query;
-            this.varCounter = varCounter;
-        }
-
-        public String getDataverseName() {
-            return dataverseName;
-        }
-
-        public String getDatasetName() {
-            return datasetName;
-        }
-
-        public int getVarCounter() {
-            return varCounter;
-        }
-
-        public Query getQuery() {
-            return query;
-        }
-
-        @Override
-        public Kind getKind() {
-            return Kind.WRITE_FROM_QUERY_RESULT;
-        }
-
-    }
-
     public static class CompiledDatasetDropStatement implements ICompiledStatement {
         private final String dataverseName;
         private final String datasetName;
diff --git a/asterix-algebra/src/main/javacc/AQLPlus.jj b/asterix-algebra/src/main/javacc/AQLPlus.jj
index 5b97d04..75179b1 100644
--- a/asterix-algebra/src/main/javacc/AQLPlus.jj
+++ b/asterix-algebra/src/main/javacc/AQLPlus.jj
@@ -168,21 +168,13 @@
   Query query;
 }
 {
-  (( "output" "to" 
+  ( "output" "to" 
     <IDENTIFIER> { nodeName = new Identifier(token.image); } 
     ":" <STRING_LITERAL> { 
              fileName = removeQuotesAndEscapes(token.image);
              stmt = new WriteStatement(nodeName, fileName, null);         
-         } )
-    |
-   ( "into" 
-     <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
-     <LEFTPAREN> query = Query() <RIGHTPAREN>
-     {
-        stmt = new WriteFromQueryResultStatement(null, datasetName, query, getVarCounter());
-     } ))  
-        
-    ";"
+         }
+  ) ";"
     {
       return stmt;
     }
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java
index fed1238..cb1e9ad 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/UpdateAPIServlet.java
@@ -31,7 +31,7 @@
 
     protected List<Statement.Kind> getAllowedStatements() {
         Kind[] statementsArray = { Kind.DATAVERSE_DECL, Kind.DELETE, Kind.INSERT, Kind.UPDATE,
-                Kind.DML_CMD_LIST, Kind.LOAD_FROM_FILE, Kind.WRITE_FROM_QUERY_RESULT, Kind.BEGIN_FEED,
+                Kind.DML_CMD_LIST, Kind.LOAD_FROM_FILE, Kind.BEGIN_FEED,
                 Kind.CONTROL_FEED };
         return Arrays.asList(statementsArray);
     }
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java b/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java
index 655d249..3f752cb 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java
@@ -57,7 +57,6 @@
 import edu.uci.ics.asterix.aql.expression.SetStatement;
 import edu.uci.ics.asterix.aql.expression.TypeDecl;
 import edu.uci.ics.asterix.aql.expression.TypeDropStatement;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
 import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
@@ -101,7 +100,6 @@
 import edu.uci.ics.asterix.translator.CompiledStatements.CompiledIndexDropStatement;
 import edu.uci.ics.asterix.translator.CompiledStatements.CompiledInsertStatement;
 import edu.uci.ics.asterix.translator.CompiledStatements.CompiledLoadFromFileStatement;
-import edu.uci.ics.asterix.translator.CompiledStatements.CompiledWriteFromQueryResultStatement;
 import edu.uci.ics.asterix.translator.CompiledStatements.ICompiledDmlStatement;
 import edu.uci.ics.asterix.translator.TypeTranslator;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -250,10 +248,6 @@
                     handleLoadFromFileStatement(metadataProvider, stmt, hcc);
                     break;
                 }
-                case WRITE_FROM_QUERY_RESULT: {
-                    handleWriteFromQueryResultStatement(metadataProvider, stmt, hcc);
-                    break;
-                }
                 case INSERT: {
                     handleInsertStatement(metadataProvider, stmt, hcc);
                     break;
@@ -1183,36 +1177,6 @@
         }
     }
 
-    private void handleWriteFromQueryResultStatement(AqlMetadataProvider metadataProvider, Statement stmt,
-            IHyracksClientConnection hcc) throws Exception {
-        MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
-        boolean bActiveTxn = true;
-        metadataProvider.setMetadataTxnContext(mdTxnCtx);
-        acquireReadLatch();
-
-        try {
-            metadataProvider.setWriteTransaction(true);
-            WriteFromQueryResultStatement st1 = (WriteFromQueryResultStatement) stmt;
-            String dataverseName = getActiveDataverseName(st1.getDataverseName());
-            CompiledWriteFromQueryResultStatement clfrqs = new CompiledWriteFromQueryResultStatement(dataverseName, st1
-                    .getDatasetName().getValue(), st1.getQuery(), st1.getVarCounter());
-
-            JobSpecification compiled = rewriteCompileQuery(metadataProvider, clfrqs.getQuery(), clfrqs);
-            MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
-            bActiveTxn = false;
-            if (compiled != null) {
-                runJob(hcc, compiled, true);
-            }
-        } catch (Exception e) {
-            if (bActiveTxn) {
-                abort(e, e, mdTxnCtx);
-            }
-            throw e;
-        } finally {
-            releaseReadLatch();
-        }
-    }
-
     private void handleInsertStatement(AqlMetadataProvider metadataProvider, Statement stmt,
             IHyracksClientConnection hcc) throws Exception {
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_01/denorm-cust-order_01.3.update.aql b/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_01/denorm-cust-order_01.3.update.aql
index 6dfff7a..63f040f 100644
--- a/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_01/denorm-cust-order_01.3.update.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_01/denorm-cust-order_01.3.update.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
-write into dataset CustomerOrders1 (
+insert into dataset CustomerOrders1 (
 
 for $c in dataset('Customers1')
 for $o in dataset('Orders1')
diff --git a/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_02/denorm-cust-order_02.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_02/denorm-cust-order_02.2.update.aql
index fdbfa62..8faaa98 100644
--- a/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_02/denorm-cust-order_02.2.update.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_02/denorm-cust-order_02.2.update.aql
@@ -8,7 +8,7 @@
 using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
 (("path"="nc1://data/custord-tiny/order-tiny.adm"),("format"="adm")) pre-sorted;
 
-write into dataset CustomerOrders2 (
+insert into dataset CustomerOrders2 (
 
 for $c in dataset('Customers2')
 let $orders := 
diff --git a/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_03/denorm-cust-order_03.3.update.aql b/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_03/denorm-cust-order_03.3.update.aql
index 6ddccc4..701119b 100644
--- a/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_03/denorm-cust-order_03.3.update.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/custord/denorm-cust-order_03/denorm-cust-order_03.3.update.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
-write into dataset CustomerOrders3 (
+insert into dataset CustomerOrders3 (
 
 for $c in dataset('Customers3')
 let $orders := 
diff --git a/asterix-app/src/test/resources/runtimets/queries/custord/load-test/load-test.3.update.aql b/asterix-app/src/test/resources/runtimets/queries/custord/load-test/load-test.3.update.aql
index 11761d6..0cf11bb 100644
--- a/asterix-app/src/test/resources/runtimets/queries/custord/load-test/load-test.3.update.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/custord/load-test/load-test.3.update.aql
@@ -1,10 +1,9 @@
 use dataverse test;
 
-write into dataset c2 (
+insert into dataset c2 (
 
   for $c in dataset('c1')
   return $c 
 
 );
 
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item/q10_returned_item.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item/q10_returned_item.3.query.aql
index c867b31..19a2b44 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item/q10_returned_item.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item/q10_returned_item.3.query.aql
@@ -1,55 +1,55 @@
 use dataverse tpch;
 
 for $locn in (
-for $l in dataset('LineItem')
-for $ocn in (
-	for $o in dataset('Orders')
-	for $c in dataset('Customer')
-		where $c.c_custkey = $o.o_custkey and $o.o_orderdate >= '1993-10-01' and $o.o_orderdate < '1994-01-01'
-	for $n in dataset('Nation')
-		where $c.c_nationkey = $n.n_nationkey
-	return {
-		"c_custkey": $c.c_custkey, 
-		"c_name": $c.c_name,
-  		"c_acctbal": $c.c_acctbal, 
-  		"n_name": $n.n_name, 
-  		"c_address": $c.c_address, 
-  		"c_phone": $c.c_phone, 
-  		"c_comment": $c.c_comment,
-  		"o_orderkey": $o.o_orderkey
-	}
-)
-where
-   $l.l_orderkey = $ocn.o_orderkey and $l.l_returnflag = 'R'
-   return {
-		"c_custkey": $ocn.c_custkey, 
-		"c_name": $ocn.c_name,
-  		"c_acctbal": $ocn.c_acctbal, 
-  		"n_name": $ocn.n_name, 
-  		"c_address": $ocn.c_address, 
-  		"c_phone": $ocn.c_phone, 
-  		"c_comment": $ocn.c_comment,
-  		"l_extendedprice": $l.l_extendedprice,
-  		"l_discount": $l.l_discount
-	}	
+  for $l in dataset('LineItem')
+  for $ocn in (
+    for $o in dataset('Orders')
+    for $c in dataset('Customer')
+    where $c.c_custkey = $o.o_custkey and $o.o_orderdate >= '1993-10-01'
+      and $o.o_orderdate < '1994-01-01'
+    for $n in dataset('Nation')
+    where $c.c_nationkey = $n.n_nationkey
+    return {
+      "c_custkey": $c.c_custkey, 
+      "c_name": $c.c_name,
+      "c_acctbal": $c.c_acctbal, 
+      "n_name": $n.n_name, 
+      "c_address": $c.c_address, 
+      "c_phone": $c.c_phone, 
+      "c_comment": $c.c_comment,
+      "o_orderkey": $o.o_orderkey
+    }
+  )
+  where $l.l_orderkey = $ocn.o_orderkey and $l.l_returnflag = 'R'
+  return {
+    "c_custkey": $ocn.c_custkey, 
+    "c_name": $ocn.c_name,
+    "c_acctbal": $ocn.c_acctbal, 
+    "n_name": $ocn.n_name, 
+    "c_address": $ocn.c_address, 
+    "c_phone": $ocn.c_phone, 
+    "c_comment": $ocn.c_comment,
+    "l_extendedprice": $l.l_extendedprice,
+    "l_discount": $l.l_discount
+  }
 )
 group by $c_custkey:=$locn.c_custkey, 
-		$c_name:=$locn.c_name, 
-		$c_acctbal:=$locn.c_acctbal, $c_phone:=$locn.c_phone, 
-		$n_name:=$locn.n_name, $c_address:=$locn.c_address, $c_comment:=$locn.c_comment
-		with $locn
+    $c_name:=$locn.c_name, 
+    $c_acctbal:=$locn.c_acctbal, $c_phone:=$locn.c_phone, 
+    $n_name:=$locn.n_name, $c_address:=$locn.c_address, $c_comment:=$locn.c_comment
+    with $locn
 let $revenue := sum(for $i in $locn return $i.l_extendedprice * (1 - $i.l_discount))
 order by $revenue desc
 limit 20
 return {
-		"c_custkey": $c_custkey, 
-		"c_name": $c_name,
-		"revenue": $revenue,
-  		"c_acctbal": $c_acctbal, 
-  		"n_name": $n_name, 
-  		"c_address": $c_address, 
-  		"c_phone": $c_phone, 
-  		"c_comment": $c_comment
+  "c_custkey": $c_custkey, 
+  "c_name": $c_name,
+  "revenue": $revenue,
+  "c_acctbal": $c_acctbal, 
+  "n_name": $n_name, 
+  "c_address": $c_address, 
+  "c_phone": $c_phone, 
+  "c_comment": $c_comment
 }
-		
-		
+    
+    
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.aql
index c867b31..19a2b44 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.aql
@@ -1,55 +1,55 @@
 use dataverse tpch;
 
 for $locn in (
-for $l in dataset('LineItem')
-for $ocn in (
-	for $o in dataset('Orders')
-	for $c in dataset('Customer')
-		where $c.c_custkey = $o.o_custkey and $o.o_orderdate >= '1993-10-01' and $o.o_orderdate < '1994-01-01'
-	for $n in dataset('Nation')
-		where $c.c_nationkey = $n.n_nationkey
-	return {
-		"c_custkey": $c.c_custkey, 
-		"c_name": $c.c_name,
-  		"c_acctbal": $c.c_acctbal, 
-  		"n_name": $n.n_name, 
-  		"c_address": $c.c_address, 
-  		"c_phone": $c.c_phone, 
-  		"c_comment": $c.c_comment,
-  		"o_orderkey": $o.o_orderkey
-	}
-)
-where
-   $l.l_orderkey = $ocn.o_orderkey and $l.l_returnflag = 'R'
-   return {
-		"c_custkey": $ocn.c_custkey, 
-		"c_name": $ocn.c_name,
-  		"c_acctbal": $ocn.c_acctbal, 
-  		"n_name": $ocn.n_name, 
-  		"c_address": $ocn.c_address, 
-  		"c_phone": $ocn.c_phone, 
-  		"c_comment": $ocn.c_comment,
-  		"l_extendedprice": $l.l_extendedprice,
-  		"l_discount": $l.l_discount
-	}	
+  for $l in dataset('LineItem')
+  for $ocn in (
+    for $o in dataset('Orders')
+    for $c in dataset('Customer')
+    where $c.c_custkey = $o.o_custkey and $o.o_orderdate >= '1993-10-01'
+      and $o.o_orderdate < '1994-01-01'
+    for $n in dataset('Nation')
+    where $c.c_nationkey = $n.n_nationkey
+    return {
+      "c_custkey": $c.c_custkey, 
+      "c_name": $c.c_name,
+      "c_acctbal": $c.c_acctbal, 
+      "n_name": $n.n_name, 
+      "c_address": $c.c_address, 
+      "c_phone": $c.c_phone, 
+      "c_comment": $c.c_comment,
+      "o_orderkey": $o.o_orderkey
+    }
+  )
+  where $l.l_orderkey = $ocn.o_orderkey and $l.l_returnflag = 'R'
+  return {
+    "c_custkey": $ocn.c_custkey, 
+    "c_name": $ocn.c_name,
+    "c_acctbal": $ocn.c_acctbal, 
+    "n_name": $ocn.n_name, 
+    "c_address": $ocn.c_address, 
+    "c_phone": $ocn.c_phone, 
+    "c_comment": $ocn.c_comment,
+    "l_extendedprice": $l.l_extendedprice,
+    "l_discount": $l.l_discount
+  }
 )
 group by $c_custkey:=$locn.c_custkey, 
-		$c_name:=$locn.c_name, 
-		$c_acctbal:=$locn.c_acctbal, $c_phone:=$locn.c_phone, 
-		$n_name:=$locn.n_name, $c_address:=$locn.c_address, $c_comment:=$locn.c_comment
-		with $locn
+    $c_name:=$locn.c_name, 
+    $c_acctbal:=$locn.c_acctbal, $c_phone:=$locn.c_phone, 
+    $n_name:=$locn.n_name, $c_address:=$locn.c_address, $c_comment:=$locn.c_comment
+    with $locn
 let $revenue := sum(for $i in $locn return $i.l_extendedprice * (1 - $i.l_discount))
 order by $revenue desc
 limit 20
 return {
-		"c_custkey": $c_custkey, 
-		"c_name": $c_name,
-		"revenue": $revenue,
-  		"c_acctbal": $c_acctbal, 
-  		"n_name": $n_name, 
-  		"c_address": $c_address, 
-  		"c_phone": $c_phone, 
-  		"c_comment": $c_comment
+  "c_custkey": $c_custkey, 
+  "c_name": $c_name,
+  "revenue": $revenue,
+  "c_acctbal": $c_acctbal, 
+  "n_name": $n_name, 
+  "c_address": $c_address, 
+  "c_phone": $c_phone, 
+  "c_comment": $c_comment
 }
-		
-		
+    
+    
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q11_important_stock/q11_important_stock.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q11_important_stock/q11_important_stock.3.query.aql
index 42a86d1..fdb01ca 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q11_important_stock/q11_important_stock.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q11_important_stock/q11_important_stock.3.query.aql
@@ -1,31 +1,34 @@
 use dataverse tpch;
 
 let $sum := sum (
-for $ps in dataset('Partsupp')
-for $sn in (
-	for $s in dataset('Supplier')
-	for $n in dataset('Nation')
-		where $s.s_nationkey = $n.n_nationkey
-	return {"s_suppkey": $s.s_suppkey}
-	)
-where $ps.ps_suppkey = $sn.s_suppkey
-return $ps.ps_supplycost * $ps.ps_availqty)
-
-for $t1 in 
-(
-for $ps in dataset('Partsupp')
-for $sn in (
-	for $s in dataset('Supplier')
-	for $n in dataset('Nation')
-		where $s.s_nationkey = $n.n_nationkey
-	return {"s_suppkey": $s.s_suppkey}
+  for $ps in dataset('Partsupp')
+  for $sn in (
+    for $s in dataset('Supplier')
+    for $n in dataset('Nation')
+    where $s.s_nationkey = $n.n_nationkey
+    return { "s_suppkey": $s.s_suppkey }
+  )
+  where $ps.ps_suppkey = $sn.s_suppkey
+  return $ps.ps_supplycost * $ps.ps_availqty
 )
-where $ps.ps_suppkey = $sn.s_suppkey
-group by $ps_partkey := $ps.ps_partkey with $ps
-return {"ps_partkey": $ps_partkey, 
-"part_value": sum(for $i in $ps return $i.ps_supplycost * $i.ps_availqty)}
+for $t1 in (
+  for $ps in dataset('Partsupp')
+  for $sn in (
+    for $s in dataset('Supplier')
+    for $n in dataset('Nation')
+    where $s.s_nationkey = $n.n_nationkey
+    return { "s_suppkey": $s.s_suppkey }
+  )
+  where $ps.ps_suppkey = $sn.s_suppkey
+  group by $ps_partkey := $ps.ps_partkey with $ps
+  return {
+    "ps_partkey": $ps_partkey, 
+    "part_value": sum(for $i in $ps return $i.ps_supplycost * $i.ps_availqty)
+  }
 )
-
 where $t1.part_value > $sum * 0.00001
 order by $t1.part_value desc
-return {"partkey": $t1.ps_partkey, "part_value":$t1.part_value}
+return {
+  "partkey": $t1.ps_partkey,
+  "part_value": $t1.part_value
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q12_shipping/q12_shipping.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q12_shipping/q12_shipping.3.query.aql
index b6d4ab0..c836a48 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q12_shipping/q12_shipping.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q12_shipping/q12_shipping.3.query.aql
@@ -2,27 +2,26 @@
 
 for $l in dataset('LineItem')
 for $o in dataset('Orders')
-
-where
-$o.o_orderkey = $l.l_orderkey 
-and $l.l_commitdate < $l.l_receiptdate
-and $l.l_shipdate < $l.l_commitdate 
-and $l.l_receiptdate >= '1994-01-01' 
-and $l.l_receiptdate < '1995-01-01'
-and ($l.l_shipmode = 'MAIL' or $l.l_shipmode = 'SHIP')
-
+where $o.o_orderkey = $l.l_orderkey 
+  and $l.l_commitdate < $l.l_receiptdate
+  and $l.l_shipdate < $l.l_commitdate 
+  and $l.l_receiptdate >= '1994-01-01' 
+  and $l.l_receiptdate < '1995-01-01'
+  and ($l.l_shipmode = 'MAIL' or $l.l_shipmode = 'SHIP')
 group by $l_shipmode := $l.l_shipmode with $o
 order by $l_shipmode
 return {
-"l_shipmode": $l_shipmode,
-"high_line_count": sum(for $i in $o 
-        return switch-case( $i.o_orderpriority ='1-URGENT' or $i.o_orderpriority ='2-HIGH',
-		true, 1,
-        false, 0)),
-"low_line_count": sum(for $i in $o 
-		return switch-case( $i.o_orderpriority ='1-URGENT' or $i.o_orderpriority ='2-HIGH',
-		true, 0,
-        false, 1))
+  "l_shipmode": $l_shipmode,
+  "high_line_count": sum(
+    for $i in $o 
+    return
+      switch-case($i.o_orderpriority ='1-URGENT' or $i.o_orderpriority ='2-HIGH',
+                  true, 1, false, 0)
+  ),
+  "low_line_count": sum(
+    for $i in $o 
+    return switch-case($i.o_orderpriority ='1-URGENT' or $i.o_orderpriority ='2-HIGH',
+                       true, 0, false, 1)
+  )
 }
 
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q13_customer_distribution/q13_customer_distribution.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q13_customer_distribution/q13_customer_distribution.3.query.aql
index dd65443..a7b7b62 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q13_customer_distribution/q13_customer_distribution.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q13_customer_distribution/q13_customer_distribution.3.query.aql
@@ -2,26 +2,28 @@
 set import-private-functions 'true';
 
 for $gco in (
-	for $co in
-		(
-			for $c in dataset('Customer')
-			return 
-			{
-				"c_custkey": $c.c_custkey, 
-				 "o_orderkey_count": count(
-						for $o in dataset('Orders')
-   							where  $c.c_custkey = $o.o_custkey and not(like($o.o_comment,'%special%requests%'))
-						return $o.o_orderkey)
-			}
-		)
-	group by $c_custkey := $co.c_custkey with $co
-		return{
-			"c_custkey": $c_custkey, "c_count": sum(for $i in $co return $i.o_orderkey_count)
-		}
+  for $co in (
+    for $c in dataset('Customer')
+    return {
+      "c_custkey": $c.c_custkey, 
+      "o_orderkey_count": count(
+        for $o in dataset('Orders')
+        where  $c.c_custkey = $o.o_custkey and not(like($o.o_comment,'%special%requests%'))
+        return $o.o_orderkey
+      )
+    }
+  )
+  group by $c_custkey := $co.c_custkey with $co
+  return {
+    "c_custkey": $c_custkey,
+    "c_count": sum(for $i in $co return $i.o_orderkey_count)
+  }
 )
-
 group by $c_count := $gco.c_count with $gco
 let $custdist := count($gco)
 order by $custdist desc, $c_count desc
-return {"c_count": $c_count, "custdist": $custdist}
+return {
+  "c_count": $c_count,
+  "custdist": $custdist
+}
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q14_promotion_effect/q14_promotion_effect.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q14_promotion_effect/q14_promotion_effect.3.query.aql
index 040e4ec..786637d 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q14_promotion_effect/q14_promotion_effect.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q14_promotion_effect/q14_promotion_effect.3.query.aql
@@ -2,10 +2,14 @@
 
 for $l in dataset('LineItem')
 for $p in dataset('Part')
-	where $l.l_partkey = $p.p_partkey and $l.l_shipdate >= '1995-09-01' and $l.l_shipdate < '1995-10-01'
+where $l.l_partkey = $p.p_partkey
+  and $l.l_shipdate >= '1995-09-01'
+  and $l.l_shipdate < '1995-10-01'
 group by $t:=1 with $l, $p
-return 100.00 * sum( for $i in $l
-			   return switch-case(like($i.p_type, 'PROMO%'),
-               true, $i.l_extendedprice*(1-$i.l_discount),
-               false, 0.0)
-  ) / sum( for $i in $l return $i.l_extendedprice * (1 - $i.l_discount))
+return 100.00 * sum(
+  for $i in $l
+  return switch-case(like($i.p_type, 'PROMO%'),
+                     true, $i.l_extendedprice*(1-$i.l_discount),
+                     false, 0.0)
+  ) / sum(for $i in $l return $i.l_extendedprice * (1 - $i.l_discount)
+)
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q15_top_supplier/q15_top_supplier.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q15_top_supplier/q15_top_supplier.3.query.aql
index 9f1080d..45b0fad 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q15_top_supplier/q15_top_supplier.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q15_top_supplier/q15_top_supplier.3.query.aql
@@ -1,27 +1,27 @@
 use dataverse tpch;
 
-declare function revenue(){
-for $l in dataset('LineItem')
-where $l.l_shipdate >= '1996-01-01' and $l.l_shipdate < '1996-04-01'
-group by $l_suppkey := $l.l_suppkey with $l
-return {
-"supplier_no": $l_suppkey, 
-"total_revenue": sum(for $i in $l return $i.l_extendedprice * (1 - $i.l_discount))
-}
+declare function revenue() {
+  for $l in dataset('LineItem')
+  where $l.l_shipdate >= '1996-01-01' and $l.l_shipdate < '1996-04-01'
+  group by $l_suppkey := $l.l_suppkey with $l
+  return {
+    "supplier_no": $l_suppkey, 
+    "total_revenue": sum(for $i in $l return $i.l_extendedprice * (1 - $i.l_discount))
+  }
 }
 
 let $m := max(
-	for $r2 in revenue()
-	return $r2.total_revenue
+  for $r2 in revenue()
+  return $r2.total_revenue
 )
 
 for $s in dataset('Supplier')
 for $r in revenue()
-	where $s.s_suppkey = $r.supplier_no and $r.total_revenue<$m+0.000000001 and $r.total_revenue>$m-0.000000001
+where $s.s_suppkey = $r.supplier_no and $r.total_revenue<$m+0.000000001 and $r.total_revenue>$m-0.000000001
 return {
-"s_suppkey": $s.s_suppkey, 
-"s_name": $s.s_name, 
-"s_address": $s.s_address, 
-"s_phone": $s.s_phone, 
-"total_revenue": $r.total_revenue
+  "s_suppkey": $s.s_suppkey, 
+  "s_name": $s.s_name, 
+  "s_address": $s.s_address, 
+  "s_phone": $s.s_phone, 
+  "total_revenue": $r.total_revenue
 }
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.aql
index e5c928e..014ceff 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.aql
@@ -1,54 +1,48 @@
 use dataverse tpch;
 
 declare function tmp(){
-for $psp in (
-	for $ps in dataset('Partsupp')
-	for $p in dataset('Part')
-		where $p.p_partkey = $ps.ps_partkey and $p.p_brand != 'Brand#45' 
-    	and not(like($p.p_type, 'MEDIUM POLISHED%'))
-	return 
-	{
- 	"p_brand": $p.p_brand, 
- 	"p_type": $p.p_type, 
- 	"p_size": $p.p_size,
- 	"ps_suppkey": $ps.ps_suppkey
-	}
-)
-for $s in dataset('Supplier')
-	where $psp.ps_suppkey = $s.s_suppkey and not(like($s.s_comment, '%Customer%Complaints%'))
-return 
-{
- "p_brand": $psp.p_brand, 
- "p_type": $psp.p_type, 
- "p_size": $psp.p_size, 
- "ps_suppkey": $psp.ps_suppkey
-}
+  for $psp in (
+    for $ps in dataset('Partsupp')
+    for $p in dataset('Part')
+    where $p.p_partkey = $ps.ps_partkey and $p.p_brand != 'Brand#45' 
+      and not(like($p.p_type, 'MEDIUM POLISHED%'))
+    return {
+      "p_brand": $p.p_brand, 
+      "p_type": $p.p_type, 
+      "p_size": $p.p_size,
+      "ps_suppkey": $ps.ps_suppkey
+    }
+  )
+  for $s in dataset('Supplier')
+  where $psp.ps_suppkey = $s.s_suppkey and not(like($s.s_comment, '%Customer%Complaints%'))
+  return {
+   "p_brand": $psp.p_brand, 
+   "p_type": $psp.p_type, 
+   "p_size": $psp.p_size, 
+   "ps_suppkey": $psp.ps_suppkey
+  }
 }
 
-for $t2 in 
-(
-for $t in tmp()   
-	where $t.p_size = 49 or $t.p_size = 14 or $t.p_size = 23 or
-         $t.p_size = 45 or $t.p_size = 19 or $t.p_size = 3 or
-         $t.p_size = 36 or $t.p_size = 9
-group by $p_brand1:= $t.p_brand, $p_type1 := $t.p_type, 
-	  $p_size1:= $t.p_size, $ps_suppkey1:=$t.ps_suppkey with $t
-return 
-{
-"p_brand": $p_brand1, 
- "p_type": $p_type1, 
- "p_size": $p_size1, 
- "ps_suppkey": $ps_suppkey1
-} )
+for $t2 in (
+  for $t in tmp()   
+  where $t.p_size = 49 or $t.p_size = 14 or $t.p_size = 23
+    or $t.p_size = 45 or $t.p_size = 19 or $t.p_size = 3
+    or $t.p_size = 36 or $t.p_size = 9
+  group by $p_brand1:= $t.p_brand, $p_type1 := $t.p_type, 
+    $p_size1:= $t.p_size, $ps_suppkey1:=$t.ps_suppkey with $t
+  return {
+    "p_brand": $p_brand1, 
+    "p_type": $p_type1, 
+    "p_size": $p_size1, 
+    "ps_suppkey": $ps_suppkey1
+  }
+)
 group by $p_brand := $t2.p_brand, $p_type := $t2.p_type, $p_size := $t2.p_size with $t2
 let $supplier_cnt := count(for $i in $t2 return $i.ps_suppkey)
 order by $supplier_cnt desc, $p_brand, $p_type, $p_size
 return {
-"p_brand": $p_brand, 
-"p_type": $p_type, 
-"p_size": $p_size,
-"supplier_cnt": $supplier_cnt
+  "p_brand": $p_brand, 
+  "p_type": $p_type, 
+  "p_size": $p_size,
+  "supplier_cnt": $supplier_cnt
 }
-
-
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.aql
index 67644d9..99f32c6 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.aql
@@ -1,21 +1,21 @@
 use dataverse tpch;
 
 declare function tmp(){
-for $l in dataset('LineItem')
-group by $l_partkey := $l.l_partkey with $l
-return {
-"t_partkey": $l_partkey, 
-"t_avg_quantity": 0.2 * avg(for $i in $l return $i.l_quantity)
-}
+  for $l in dataset('LineItem')
+  group by $l_partkey := $l.l_partkey with $l
+  return {
+    "t_partkey": $l_partkey, 
+    "t_avg_quantity": 0.2 * avg(for $i in $l return $i.l_quantity)
+  }
 }
 
 sum(
-for $l in dataset('LineItem')
-for $p in dataset('Part')
-where  $p.p_partkey = $l.l_partkey and $p.p_container = 'MED BOX'
-
-for $t in tmp()
-where $l.l_partkey = $t.t_partkey and $l.l_quantity < $t.t_avg_quantity
-return $l.l_extendedprice
+  for $l in dataset('LineItem')
+  for $p in dataset('Part')
+  where  $p.p_partkey = $l.l_partkey and $p.p_container = 'MED BOX'
+  for $t in tmp()
+  where $l.l_partkey = $t.t_partkey
+    and $l.l_quantity < $t.t_avg_quantity
+  return $l.l_extendedprice
 )/7.0
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.aql
index df6d9f0..289d900 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.aql
@@ -2,21 +2,18 @@
 
 for $c in dataset('Customer')
 for $o in dataset('Orders')
-where
-  $c.c_custkey = $o.o_custkey
-for $t in 
-(
+where $c.c_custkey = $o.o_custkey
+for $t in (
   for $l in dataset('LineItem')
   group by $l_orderkey := $l.l_orderkey with $l
-  return 
-    { "l_orderkey": $l_orderkey, 
-      "t_sum_quantity": sum(for $i in $l return $i.l_quantity) }
+  return {
+    "l_orderkey": $l_orderkey, 
+    "t_sum_quantity": sum(for $i in $l return $i.l_quantity)
+  }
 ) 
-where 
-  $o.o_orderkey = $t.l_orderkey and $t.t_sum_quantity > 30
+where $o.o_orderkey = $t.l_orderkey and $t.t_sum_quantity > 30
 for $l in dataset('LineItem')
-where
-  $l.l_orderkey = $o.o_orderkey 
+where $l.l_orderkey = $o.o_orderkey 
 group by $c_name := $c.c_name, $c_custkey := $c.c_custkey, $o_orderkey := $o.o_orderkey, 
          $o_orderdate := $o.o_orderdate, $o_totalprice := $o.o_totalprice with $l
 order by $o_totalprice desc, $o_orderdate 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q19_discounted_revenue/q19_discounted_revenue.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q19_discounted_revenue/q19_discounted_revenue.3.query.aql
index ec10939..7e26d94 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q19_discounted_revenue/q19_discounted_revenue.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q19_discounted_revenue/q19_discounted_revenue.3.query.aql
@@ -1,37 +1,33 @@
 use dataverse tpch;
+
 set import-private-functions 'true';
 
 sum( 
-for $l in dataset('LineItem')
-for $p in dataset('Part')
-where $p.p_partkey = $l.l_partkey
-and (
- (
-    $p.p_brand = 'Brand#12'
-	and reg-exp($p.p_container,'SM CASE||SM BOX||SM PACK||SM PKG')
-	and $l.l_quantity >= 1 and $l.l_quantity <= 11
-	and $p.p_size >= 1 and $p.p_size <= 5
-	and reg-exp($l.l_shipmode, 'AIR||AIR REG')
-	and $l.l_shipinstruct = 'DELIVER IN PERSON'
-  ) 
-  or 
-  (
-    $p.p_brand = 'Brand#23'
-	and reg-exp($p.p_container, 'MED BAG||MED BOX||MED PKG||MED PACK')
-	and $l.l_quantity >= 10 and $l.l_quantity <= 20
-	and $p.p_size >= 1 and $p.p_size <= 10
-	and reg-exp($l.l_shipmode, 'AIR||AIR REG')
-	and $l.l_shipinstruct = 'DELIVER IN PERSON'
-  )
-  or
-  (
-	$p.p_brand = 'Brand#34'
-	and reg-exp($p.p_container, 'LG CASE||LG BOX||LG PACK||LG PKG')
-	and $l.l_quantity >= 20 and $l.l_quantity <= 30
-	and $p.p_size >= 1 and $p.p_size <= 15
-	and reg-exp($l.l_shipmode, 'AIR||AIR REG')
-	and $l.l_shipinstruct = 'DELIVER IN PERSON'
-  )
- )
-return $l.l_extendedprice * (1 - $l.l_discount)
+  for $l in dataset('LineItem')
+  for $p in dataset('Part')
+  where $p.p_partkey = $l.l_partkey
+    and ( (
+        $p.p_brand = 'Brand#12'
+        and reg-exp($p.p_container,'SM CASE||SM BOX||SM PACK||SM PKG')
+        and $l.l_quantity >= 1 and $l.l_quantity <= 11
+        and $p.p_size >= 1 and $p.p_size <= 5
+        and reg-exp($l.l_shipmode, 'AIR||AIR REG')
+        and $l.l_shipinstruct = 'DELIVER IN PERSON'
+      ) or (
+        $p.p_brand = 'Brand#23'
+        and reg-exp($p.p_container, 'MED BAG||MED BOX||MED PKG||MED PACK')
+        and $l.l_quantity >= 10 and $l.l_quantity <= 20
+        and $p.p_size >= 1 and $p.p_size <= 10
+        and reg-exp($l.l_shipmode, 'AIR||AIR REG')
+        and $l.l_shipinstruct = 'DELIVER IN PERSON'
+      ) or (
+        $p.p_brand = 'Brand#34'
+        and reg-exp($p.p_container, 'LG CASE||LG BOX||LG PACK||LG PKG')
+        and $l.l_quantity >= 20 and $l.l_quantity <= 30
+        and $p.p_size >= 1 and $p.p_size <= 15
+        and reg-exp($l.l_shipmode, 'AIR||AIR REG')
+        and $l.l_shipinstruct = 'DELIVER IN PERSON'
+      )
+    )
+  return $l.l_extendedprice * (1 - $l.l_discount)
 )
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q1_pricing_summary_report_nt/q1_pricing_summary_report_nt.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q1_pricing_summary_report_nt/q1_pricing_summary_report_nt.3.query.aql
index c27be0a..599f51e 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q1_pricing_summary_report_nt/q1_pricing_summary_report_nt.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q1_pricing_summary_report_nt/q1_pricing_summary_report_nt.3.query.aql
@@ -4,7 +4,8 @@
 for $l in dataset('LineItem')
 where $l.l_shipdate <= '1998-09-02'
 /*+ hash*/
-group by $l_returnflag := $l.l_returnflag, $l_linestatus := $l.l_linestatus  
+group by $l_returnflag := $l.l_returnflag,
+         $l_linestatus := $l.l_linestatus  
   with $l
 order by $l_returnflag, $l_linestatus
 return {
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.aql
index a5a5bbe..b023966 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.aql
@@ -1,39 +1,48 @@
 use dataverse tpch;
 
 for $t3 in (
-for $t2 in (
-	for $l in dataset('LineItem')
-		group by $l_partkey:=$l.l_partkey, $l_suppkey:=$l.l_suppkey with $l
-	return {
-	"l_partkey": $l_partkey, 
-	"l_suppkey": $l_suppkey, 
-	"sum_quantity": 0.5 * sum(for $i in $l return $i.l_quantity)
-})
-for $pst1 in (
-	for $ps in dataset('Partsupp')
-	for $t1 in 
-	(
-		for $p in dataset('Part')
-			distinct by $p.p_partkey
-		return {"p_partkey": $p.p_partkey}
-	)
-	where $ps.ps_partkey = $t1.p_partkey
-	return {"ps_suppkey": $ps.ps_suppkey, "ps_partkey": $ps.ps_partkey, "ps_availqty": $ps.ps_availqty}
+  for $t2 in (
+    for $l in dataset('LineItem')
+    group by $l_partkey:=$l.l_partkey, $l_suppkey:=$l.l_suppkey with $l
+    return {
+      "l_partkey": $l_partkey, 
+      "l_suppkey": $l_suppkey, 
+      "sum_quantity": 0.5 * sum(for $i in $l return $i.l_quantity)
+    }
+  )
+  for $pst1 in (
+    for $ps in dataset('Partsupp')
+    for $t1 in (
+      for $p in dataset('Part')
+      distinct by $p.p_partkey
+      return { "p_partkey": $p.p_partkey }
+    )
+    where $ps.ps_partkey = $t1.p_partkey
+    return {
+      "ps_suppkey": $ps.ps_suppkey,
+      "ps_partkey": $ps.ps_partkey,
+      "ps_availqty": $ps.ps_availqty
+    }
+  )
+  where $pst1.ps_partkey = $t2.l_partkey and $pst1.ps_suppkey = $t2.l_suppkey
+    and $pst1.ps_availqty > $t2.sum_quantity
+  distinct by $pst1.ps_suppkey
+  return { "ps_suppkey": $pst1.ps_suppkey }
 )
-where $pst1.ps_partkey = $t2.l_partkey and $pst1.ps_suppkey = $t2.l_suppkey
-		and $pst1.ps_availqty > $t2.sum_quantity
-distinct by $pst1.ps_suppkey
-return {"ps_suppkey": $pst1.ps_suppkey}
-)
-
 for $t4 in (
-	for $n in dataset('Nation')
-	for $s in dataset('Supplier')
-		where  $s.s_nationkey = $n.n_nationkey
-	return {"s_name": $s.s_name, "s_address": $s.s_address, "s_suppkey": $s.s_suppkey}
+  for $n in dataset('Nation')
+  for $s in dataset('Supplier')
+  where  $s.s_nationkey = $n.n_nationkey
+  return {
+    "s_name": $s.s_name,
+    "s_address": $s.s_address,
+    "s_suppkey": $s.s_suppkey
+  }
 )
-
 where $t3.ps_suppkey = $t4.s_suppkey
 order by $t4.s_name
-return {"s_name": $t4.s_name, "s_address": $t4.s_address}
+return {
+  "s_name": $t4.s_name,
+  "s_address": $t4.s_address
+}
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.aql
index eb94678..e99dea2 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.aql
@@ -1,78 +1,76 @@
 use dataverse tpch;
 
-declare function tmp1()
-{
-for $l2 in 
-(
-	for $l in dataset('LineItem')
-	group by $l_orderkey1 := $l.l_orderkey, $l_suppkey1 := $l.l_suppkey with $l
-	return {
-		"l_orderkey": $l_orderkey1,
-		"l_suppkey": $l_suppkey1
-		} 
-)
-group by $l_orderkey := $l2.l_orderkey with $l2
-return {
-	"l_orderkey": $l_orderkey, 
-	"count_suppkey": count(for $i in $l2 return $i.l_suppkey), 
-	"max_suppkey": max(for $i in $l2 return $i.l_suppkey)
-}
+declare function tmp1() {
+  for $l2 in (
+    for $l in dataset('LineItem')
+    group by $l_orderkey1 := $l.l_orderkey, $l_suppkey1 := $l.l_suppkey with $l
+    return {
+      "l_orderkey": $l_orderkey1,
+      "l_suppkey": $l_suppkey1
+    } 
+  )
+  group by $l_orderkey := $l2.l_orderkey with $l2
+  return {
+    "l_orderkey": $l_orderkey, 
+    "count_suppkey": count(for $i in $l2 return $i.l_suppkey), 
+    "max_suppkey": max(for $i in $l2 return $i.l_suppkey)
+  }
 }
 
-declare function tmp2()
-{
-for $l2 in 
-(
-	for $l in dataset('LineItem')
-	where $l.l_receiptdate > $l.l_commitdate
-	group by $l_orderkey1 := $l.l_orderkey, $l_suppkey1 := $l.l_suppkey with $l
-	return {
-		"l_orderkey": $l_orderkey1,
-		"l_suppkey": $l_suppkey1
-		} 
-)
-group by $l_orderkey := $l2.l_orderkey with $l2
-return {
-"l_orderkey": $l_orderkey, 
-"count_suppkey": count(for $i in $l2 return $i.l_suppkey), 
-"max_suppkey": max(for $i in $l2 return $i.l_suppkey)
-}
+declare function tmp2() {
+  for $l2 in (
+    for $l in dataset('LineItem')
+    where $l.l_receiptdate > $l.l_commitdate
+    group by $l_orderkey1 := $l.l_orderkey, $l_suppkey1 := $l.l_suppkey with $l
+    return {
+      "l_orderkey": $l_orderkey1,
+      "l_suppkey": $l_suppkey1
+    } 
+  )
+  group by $l_orderkey := $l2.l_orderkey with $l2
+  return {
+    "l_orderkey": $l_orderkey, 
+    "count_suppkey": count(for $i in $l2 return $i.l_suppkey), 
+    "max_suppkey": max(for $i in $l2 return $i.l_suppkey)
+  }
 }
 
 for $t4 in (
-for $t3 in (
-	for $l in dataset('LineItem')
-	for $ns in (
-		for $n in dataset('Nation')
-		for $s in dataset('Supplier')
-			where $s.s_nationkey = $n.n_nationkey
-		return {"s_name": $s.s_name, "s_suppkey": $s.s_suppkey}
-	)
-	where $ns.s_suppkey = $l.l_suppkey and $l.l_receiptdate > $l.l_commitdate
-
-	for $o in dataset('Orders')
-	where $o.o_orderkey = $l.l_orderkey
-
-	for $t1 in tmp1()
-	where $l.l_orderkey = $t1.l_orderkey
-	
-	return {"s_name": $ns.s_name, "l_orderkey": $t1.l_orderkey, "l_suppkey": $l.l_suppkey}
+  for $t3 in (
+    for $l in dataset('LineItem')
+    for $ns in (
+      for $n in dataset('Nation')
+      for $s in dataset('Supplier')
+      where $s.s_nationkey = $n.n_nationkey
+      return {
+        "s_name": $s.s_name,
+        "s_suppkey": $s.s_suppkey
+      }
+    )
+    where $ns.s_suppkey = $l.l_suppkey and $l.l_receiptdate > $l.l_commitdate
+    for $o in dataset('Orders')
+    where $o.o_orderkey = $l.l_orderkey
+    for $t1 in tmp1()
+    where $l.l_orderkey = $t1.l_orderkey
+    return {
+      "s_name": $ns.s_name,
+      "l_orderkey": $t1.l_orderkey,
+      "l_suppkey": $l.l_suppkey}
+  )
+  for $t2 in tmp2() 
+  where $t2.count_suppkey >= 0 and $t3.l_orderkey = $t2.l_orderkey
+  return {
+    "s_name": $t3.s_name,
+    "l_suppkey": $t3.l_suppkey,
+    "l_orderkey": $t2.l_orderkey,
+    "count_suppkey": $t2.count_suppkey, 
+    "max_suppkey": $t2.max_suppkey
+  }
 )
-
-for $t2 in tmp2() 
-			where $t2.count_suppkey >= 0 and $t3.l_orderkey = $t2.l_orderkey
-			
-return {
-"s_name": $t3.s_name,
-"l_suppkey": $t3.l_suppkey,
-"l_orderkey": $t2.l_orderkey,
-"count_suppkey": $t2.count_suppkey, 
-"max_suppkey": $t2.max_suppkey
-} )
 group by $s_name := $t4.s_name with $t4
 let $numwait := count($t4)
 order by $numwait desc, $s_name
 return {
-"s_name": $s_name,
-"numwait": $numwait
+  "s_name": $s_name,
+  "numwait": $numwait
 }
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.aql
index a07e4ad..8913818 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.aql
@@ -1,30 +1,25 @@
 use dataverse tpch;
 
-declare function q22_customer_tmp(){
-for $c in dataset('Customer')
-return {
-"c_acctbal": $c.c_acctbal,
-"c_custkey": $c.c_custkey,
-"cntrycode": substring($c.c_phone, 1, 2)
-}
+declare function q22_customer_tmp() {
+  for $c in dataset('Customer')
+  return {
+    "c_acctbal": $c.c_acctbal,
+    "c_custkey": $c.c_custkey,
+    "cntrycode": substring($c.c_phone, 1, 2)
+  }
 }
 
-
-let $avg := avg( for $c in dataset('Customer')
-					where $c.c_acctbal > 0.00
-				 return $c.c_acctbal
-				)
+let $avg := avg(
+  for $c in dataset('Customer')
+  where $c.c_acctbal > 0.00
+  return $c.c_acctbal
+)
 for $ct in q22_customer_tmp()
 where $ct.c_acctbal > $avg
-
 group by $cntrycode := $ct.cntrycode with $ct
 order by $cntrycode
-
 return {
-"cntrycode": $cntrycode, 
-"numcust": count($ct), 
-"totacctbal": sum(for $i in $ct return $i.c_acctbal)
+  "cntrycode": $cntrycode, 
+  "numcust": count($ct), 
+  "totacctbal": sum(for $i in $ct return $i.c_acctbal)
 }
-
-
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q2_minimum_cost_supplier/q2_minimum_cost_supplier.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q2_minimum_cost_supplier/q2_minimum_cost_supplier.3.query.aql
index c13dbb9..16cf8b2 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q2_minimum_cost_supplier/q2_minimum_cost_supplier.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q2_minimum_cost_supplier/q2_minimum_cost_supplier.3.query.aql
@@ -1,88 +1,102 @@
 use dataverse tpch;
 
-declare function tmp1(){
-for $p in dataset('Part')
-for $pssrn in 
-(
-	for $ps in dataset('Partsupp')
-	for $srn in 
-		(
-		for $s in dataset('Supplier')
-		 for $rn in
-			(
-			for $r in dataset('Region')
-			for $n in dataset('Nation')
-			where $n.n_regionkey = $r.r_regionkey and $r.r_name = 'EUROPE' 
-			return {"n_nationkey": $n.n_nationkey, "n_name": $n.n_name}
-			)
-		 where $s.s_nationkey = $rn.n_nationkey
-		 return {"s_suppkey": $s.s_suppkey, "n_name": $rn.n_name,
-		 		"s_name": $s.s_name,
-		 		"s_acctbal": $s.s_acctbal,
-		 		"s_address": $s.s_address, 
-				"s_phone": $s.s_phone, 
-				"s_comment": $s.s_comment}
-		)
-	where $srn.s_suppkey = $ps.ps_suppkey
-	return {"n_name": $srn.n_name, "p_partkey": $ps.ps_partkey, "ps_supplycost": $ps.ps_supplycost,
-			"s_name": $srn.s_name,
-			"s_acctbal": $srn.s_acctbal,
-		 	"s_address":  $srn.s_address, 
-			"s_phone":  $srn.s_phone, 
-			"s_comment":  $srn.s_comment}
-) 
-where $p.p_partkey = $pssrn.p_partkey and like($p.p_type, '%BRASS') 
-return 
-{"s_acctbal": $pssrn.s_acctbal, 
-"s_name": $pssrn.s_name, 
-"n_name": $pssrn.n_name, 
-"p_partkey": $p.p_partkey, 
-"ps_supplycost": $pssrn.ps_supplycost, 
-"p_mfgr": $p.p_mfgr, 
-"s_address":  $pssrn.s_address, 
-"s_phone":  $pssrn.s_phone, 
-"s_comment":  $pssrn.s_comment
-}
+declare function tmp1() {
+  for $p in dataset('Part')
+  for $pssrn in (
+    for $ps in dataset('Partsupp')
+    for $srn in (
+      for $s in dataset('Supplier')
+      for $rn in (
+        for $r in dataset('Region')
+        for $n in dataset('Nation')
+        where $n.n_regionkey = $r.r_regionkey and $r.r_name = 'EUROPE' 
+        return {
+          "n_nationkey": $n.n_nationkey,
+          "n_name": $n.n_name
+        }
+      )
+      where $s.s_nationkey = $rn.n_nationkey
+      return {
+        "s_suppkey": $s.s_suppkey,
+        "n_name": $rn.n_name,
+        "s_name": $s.s_name,
+        "s_acctbal": $s.s_acctbal,
+        "s_address": $s.s_address, 
+        "s_phone": $s.s_phone, 
+        "s_comment": $s.s_comment
+      }
+    )
+    where $srn.s_suppkey = $ps.ps_suppkey
+    return {
+      "n_name": $srn.n_name,
+      "p_partkey": $ps.ps_partkey,
+      "ps_supplycost": $ps.ps_supplycost,
+      "s_name": $srn.s_name,
+      "s_acctbal": $srn.s_acctbal,
+      "s_address":  $srn.s_address, 
+      "s_phone":  $srn.s_phone, 
+      "s_comment":  $srn.s_comment
+    }
+  ) 
+  where $p.p_partkey = $pssrn.p_partkey and like($p.p_type, '%BRASS') 
+  return {
+    "s_acctbal": $pssrn.s_acctbal,
+    "s_name": $pssrn.s_name, 
+    "n_name": $pssrn.n_name, 
+    "p_partkey": $p.p_partkey, 
+    "ps_supplycost": $pssrn.ps_supplycost, 
+    "p_mfgr": $p.p_mfgr, 
+    "s_address":  $pssrn.s_address, 
+    "s_phone":  $pssrn.s_phone, 
+    "s_comment":  $pssrn.s_comment
+  }
 }
 
 declare function tmp2(){
-for $p in dataset('Part')
-for $pssrn in 
-(
-	for $ps in dataset('Partsupp')
-	for $srn in 
-		(
-		for $s in dataset('Supplier')
-		 for $rn in
-			(
-			for $r in dataset('Region')
-			for $n in dataset('Nation')
-			where $n.n_regionkey = $r.r_regionkey and $r.r_name = 'EUROPE' 
-			return {"n_nationkey": $n.n_nationkey, "n_name": $n.n_name}
-			)
-		 where $s.s_nationkey = $rn.n_nationkey
-		 return {"s_suppkey": $s.s_suppkey, "n_name": $rn.n_name,
-		 		"s_name": $s.s_name,
-		 		"s_acctbal": $s.s_acctbal,
-		 		"s_address": $s.s_address, 
-				"s_phone": $s.s_phone, 
-				"s_comment": $s.s_comment}
-		)
-	where $srn.s_suppkey = $ps.ps_suppkey
-	return {"n_name": $srn.n_name, "p_partkey": $ps.ps_partkey, "ps_supplycost": $ps.ps_supplycost,
-			"s_name": $srn.s_name,
-			"s_acctbal": $srn.s_acctbal,
-		 	"s_address":  $srn.s_address, 
-			"s_phone":  $srn.s_phone, 
-			"s_comment":  $srn.s_comment}
-)
-where $p.p_partkey = $pssrn.p_partkey and like($p.p_type, '%BRASS') 
-/*+ hash*/
-group by $p_partkey := $pssrn.p_partkey
-with $pssrn
-return {"p_partkey": $p_partkey, 
-"ps_min_supplycost": min(for $i in $pssrn return $i.ps_supplycost)
-}
+  for $p in dataset('Part')
+  for $pssrn in (
+    for $ps in dataset('Partsupp')
+    for $srn in (
+      for $s in dataset('Supplier')
+      for $rn in (
+        for $r in dataset('Region')
+        for $n in dataset('Nation')
+        where $n.n_regionkey = $r.r_regionkey and $r.r_name = 'EUROPE' 
+        return {
+          "n_nationkey": $n.n_nationkey,
+          "n_name": $n.n_name
+        }
+      )
+      where $s.s_nationkey = $rn.n_nationkey
+      return {
+        "s_suppkey": $s.s_suppkey,
+        "n_name": $rn.n_name,
+        "s_name": $s.s_name,
+        "s_acctbal": $s.s_acctbal,
+        "s_address": $s.s_address, 
+        "s_phone": $s.s_phone, 
+        "s_comment": $s.s_comment
+      }
+    )
+    where $srn.s_suppkey = $ps.ps_suppkey
+    return {
+      "n_name": $srn.n_name,
+      "p_partkey": $ps.ps_partkey,
+      "ps_supplycost": $ps.ps_supplycost,
+      "s_name": $srn.s_name,
+      "s_acctbal": $srn.s_acctbal,
+      "s_address":  $srn.s_address, 
+      "s_phone":  $srn.s_phone, 
+      "s_comment":  $srn.s_comment
+    }
+  )
+  where $p.p_partkey = $pssrn.p_partkey and like($p.p_type, '%BRASS') 
+  /*+ hash*/
+  group by $p_partkey := $pssrn.p_partkey with $pssrn
+  return {
+    "p_partkey": $p_partkey, 
+    "ps_min_supplycost": min(for $i in $pssrn return $i.ps_supplycost)
+  }
 }
 
 for $t2 in tmp2()
@@ -92,12 +106,12 @@
 limit 100
 return 
 {
-"s_acctbal":$t1.s_acctbal, 
-"s_name":$t1.s_name, 
-"n_name":$t1.n_name, 
-"p_partkey":$t1.p_partkey, 
-"p_mfgr":$t1.p_mfgr, 
-"s_address":$t1.s_address, 
-"s_phone":$t1.s_phone, 
-"s_comment":$t1.s_comment
+  "s_acctbal": $t1.s_acctbal, 
+  "s_name": $t1.s_name, 
+  "n_name": $t1.n_name, 
+  "p_partkey": $t1.p_partkey, 
+  "p_mfgr": $t1.p_mfgr, 
+  "s_address": $t1.s_address, 
+  "s_phone": $t1.s_phone, 
+  "s_comment": $t1.s_comment
 } 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q4_order_priority/q4_order_priority.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q4_order_priority/q4_order_priority.3.query.aql
index 4079a25..9912c85 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q4_order_priority/q4_order_priority.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q4_order_priority/q4_order_priority.3.query.aql
@@ -2,23 +2,20 @@
 
 declare function tmp()
 {
-for $l in dataset('LineItem')
-where $l.l_commitdate<$l.l_receiptdate
-distinct by $l.l_orderkey
-return {"o_orderkey": $l.l_orderkey}
+  for $l in dataset('LineItem')
+  where $l.l_commitdate < $l.l_receiptdate
+  distinct by $l.l_orderkey
+  return { "o_orderkey": $l.l_orderkey }
 }
 
 for $o in dataset('Orders')
 for $t in tmp()
-where
-$o.o_orderkey = $t.o_orderkey and 
-$o.o_orderdate >= '1993-07-01' and $o.o_orderdate < '1993-10-01' 
-group by $o_orderpriority := $o.o_orderpriority 
-with $o
+where $o.o_orderkey = $t.o_orderkey and 
+  $o.o_orderdate >= '1993-07-01' and $o.o_orderdate < '1993-10-01' 
+group by $o_orderpriority := $o.o_orderpriority with $o
 order by $o_orderpriority
-return 
-{
-"order_priority": $o_orderpriority,
-"count": count($o)
+return {
+  "order_priority": $o_orderpriority,
+  "count": count($o)
 }
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q5_local_supplier_volume/q5_local_supplier_volume.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q5_local_supplier_volume/q5_local_supplier_volume.3.query.aql
index d654ce1..e0e0563 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q5_local_supplier_volume/q5_local_supplier_volume.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q5_local_supplier_volume/q5_local_supplier_volume.3.query.aql
@@ -1,27 +1,46 @@
 use dataverse tpch;
 
 for $c in dataset('Customer')
-for $o1 in 
-(  for $o in dataset('Orders')
-   for $l1 in (
-     for $l in dataset('LineItem')
-     for $s1 in 
-     (  for $s in dataset('Supplier')
-        for $n1 in 
-        (  for $n in dataset('Nation')
-           for $r in dataset('Region')
-             where $n.n_regionkey = $r.r_regionkey
-           return 
-              {"n_name": $n.n_name, "n_nationkey": $n.n_nationkey}  )
-        where $s.s_nationkey = $n1.n_nationkey
-        return 
-           { "n_name": $n1.n_name, "s_suppkey": $s.s_suppkey, "s_nationkey": $s.s_nationkey }  )
-     where $l.l_suppkey = $s1.s_suppkey      
-     return 
-        { "n_name": $s1.n_name, "l_extendedprice": $l.l_extendedprice, "l_discount": $l.l_discount, "l_orderkey": $l.l_orderkey, "s_nationkey": $s1.s_nationkey }  )
-   where $l1.l_orderkey = $o.o_orderkey and $o.o_orderdate >= '1990-01-01' and $o.o_orderdate < '1995-01-01'
-   return 
-     { "n_name": $l1.n_name, "l_extendedprice": $l1.l_extendedprice, "l_discount": $l1.l_discount, "s_nationkey": $l1.s_nationkey, "o_custkey": $o.o_custkey }  )
+for $o1 in (
+  for $o in dataset('Orders')
+  for $l1 in (
+    for $l in dataset('LineItem')
+    for $s1 in (
+      for $s in dataset('Supplier')
+      for $n1 in (
+        for $n in dataset('Nation')
+        for $r in dataset('Region')
+        where $n.n_regionkey = $r.r_regionkey
+        return {
+          "n_name": $n.n_name,
+          "n_nationkey": $n.n_nationkey
+        }
+      )
+      where $s.s_nationkey = $n1.n_nationkey
+      return {
+        "n_name": $n1.n_name,
+        "s_suppkey": $s.s_suppkey,
+        "s_nationkey": $s.s_nationkey
+      }
+    )
+    where $l.l_suppkey = $s1.s_suppkey      
+    return {
+      "n_name": $s1.n_name, 
+      "l_extendedprice": $l.l_extendedprice,
+      "l_discount": $l.l_discount,
+      "l_orderkey": $l.l_orderkey,
+      "s_nationkey": $s1.s_nationkey
+    }
+  )
+  where $l1.l_orderkey = $o.o_orderkey and $o.o_orderdate >= '1990-01-01' and $o.o_orderdate < '1995-01-01'
+  return {
+    "n_name": $l1.n_name,
+    "l_extendedprice": $l1.l_extendedprice,
+    "l_discount": $l1.l_discount,
+    "s_nationkey": $l1.s_nationkey,
+    "o_custkey": $o.o_custkey
+  }
+)
 where $c.c_nationkey = $o1.s_nationkey and $c.c_custkey = $o1.o_custkey
 /*+ hash*/     
 group by $n_name := $o1.n_name with $o1
@@ -31,5 +50,7 @@
     $i.l_extendedprice * (1 - $i.l_discount)
 )
 order by $revenue desc
-return 
-  { "n_name": $n_name, "revenue": $revenue }
+return {
+  "n_name": $n_name,
+  "revenue": $revenue
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q6_forecast_revenue_change/q6_forecast_revenue_change.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q6_forecast_revenue_change/q6_forecast_revenue_change.3.query.aql
index 8ac1613..213b41d 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q6_forecast_revenue_change/q6_forecast_revenue_change.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q6_forecast_revenue_change/q6_forecast_revenue_change.3.query.aql
@@ -1,14 +1,13 @@
 use dataverse tpch;
 
 let $revenue := sum(
-for $l in dataset('LineItem')
-where 
-  $l.l_shipdate >= '1994-01-01'
-  and $l.l_shipdate < '1995-01-01'
-  and $l.l_discount >= 0.05 and $l.l_discount <= 0.07
-  and $l.l_quantity < 24
-return $l.l_extendedprice * $l.l_discount
+  for $l in dataset('LineItem')
+  where $l.l_shipdate >= '1994-01-01'
+    and $l.l_shipdate < '1995-01-01'
+    and $l.l_discount >= 0.05 and $l.l_discount <= 0.07
+    and $l.l_quantity < 24
+  return $l.l_extendedprice * $l.l_discount
 )
 return {
-"revenue": $revenue
+  "revenue": $revenue
 }
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q7_volume_shipping/q7_volume_shipping.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q7_volume_shipping/q7_volume_shipping.3.query.aql
index 843204a..7b9e2b9 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q7_volume_shipping/q7_volume_shipping.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q7_volume_shipping/q7_volume_shipping.3.query.aql
@@ -1,70 +1,63 @@
 use dataverse tpch;
 
-declare function q7_volume_shipping_tmp(){
-for $n1 in dataset('Nation') 
-for $n2 in dataset('Nation')
-   where $n2.n_name='GERMANY' or $n1.n_name='GERMANY' 
-return 
-{
- 	"supp_nation": $n1.n_name, 
- 	"cust_nation": $n2.n_name, 
- 	"s_nationkey": $n1.n_nationkey,      
+declare function q7_volume_shipping_tmp() {
+  for $n1 in dataset('Nation') 
+  for $n2 in dataset('Nation')
+  where $n2.n_name='GERMANY' or $n1.n_name='GERMANY' 
+  return {
+    "supp_nation": $n1.n_name, 
+    "cust_nation": $n2.n_name, 
+    "s_nationkey": $n1.n_nationkey,      
     "c_nationkey": $n2.n_nationkey
-}
+  }
 }
 
 for $locs in (
-	for $loc in (
-		for $lo in 
-		(
-			for $l in dataset('LineItem')
-			for $o in dataset('Orders')
-			where 
-				$o.o_orderkey = $l.l_orderkey and $l.l_shipdate >= '1992-01-01' 
-				and $l.l_shipdate <= '1996-12-31'
-			return
-			{
-			"l_shipdate": $l.l_shipdate, 
-			"l_extendedprice": $l.l_extendedprice, 
-			"l_discount": $l.l_discount, 
-			"l_suppkey": $l.l_suppkey, 
-			"o_custkey": $o.o_custkey 
-			}
-		)
-		for $c in dataset('Customer')
-		where
-			$c.c_custkey = $lo.o_custkey
-		return 
-		{
-		"l_shipdate": $lo.l_shipdate, 
-		"l_extendedprice": $lo.l_extendedprice, 
-		"l_discount": $lo.l_discount, 
-		"l_suppkey": $lo.l_suppkey, 
-		"c_nationkey": $c.c_nationkey 
-		}
-	)
-	for $s in dataset('Supplier')
-	where
-		$s.s_suppkey = $loc.l_suppkey
-	return {
-	"l_shipdate": $loc.l_shipdate, 
-	"l_extendedprice": $loc.l_extendedprice, 
-	"l_discount": $loc.l_discount, 
-	"c_nationkey": $loc.c_nationkey, 
-	"s_nationkey": $s.s_nationkey
-	}
+  for $loc in (
+    for $lo in (
+      for $l in dataset('LineItem')
+      for $o in dataset('Orders')
+      where $o.o_orderkey = $l.l_orderkey and $l.l_shipdate >= '1992-01-01' 
+        and $l.l_shipdate <= '1996-12-31'
+      return {
+        "l_shipdate": $l.l_shipdate, 
+        "l_extendedprice": $l.l_extendedprice, 
+        "l_discount": $l.l_discount, 
+        "l_suppkey": $l.l_suppkey, 
+        "o_custkey": $o.o_custkey 
+      }
+    )
+    for $c in dataset('Customer')
+    where $c.c_custkey = $lo.o_custkey
+    return {
+      "l_shipdate": $lo.l_shipdate, 
+      "l_extendedprice": $lo.l_extendedprice, 
+      "l_discount": $lo.l_discount, 
+      "l_suppkey": $lo.l_suppkey, 
+      "c_nationkey": $c.c_nationkey 
+    }
+  )
+  for $s in dataset('Supplier')
+  where $s.s_suppkey = $loc.l_suppkey
+  return {
+    "l_shipdate": $loc.l_shipdate, 
+    "l_extendedprice": $loc.l_extendedprice, 
+    "l_discount": $loc.l_discount, 
+    "c_nationkey": $loc.c_nationkey, 
+    "s_nationkey": $s.s_nationkey
+  }
 )
 for $t in q7_volume_shipping_tmp()
-where
-	$locs.c_nationkey = $t.c_nationkey and $locs.s_nationkey = $t.s_nationkey
+where $locs.c_nationkey = $t.c_nationkey
+  and $locs.s_nationkey = $t.s_nationkey
 let $l_year0 := get-year($locs.l_shipdate)
 group by $supp_nation := $t.supp_nation, $cust_nation := $t.cust_nation, $l_year := $l_year0
 with $locs
 let $revenue := sum(for $i in $locs return $i.l_extendedprice * (1 - $i.l_discount))
 order by $supp_nation, $cust_nation, $l_year
 return {
-	"supp_nation": $supp_nation, 
-	"cust_nation": $cust_nation, 
-	"l_year": $l_year,
-	"revenue": $revenue
+  "supp_nation": $supp_nation, 
+  "cust_nation": $cust_nation, 
+  "l_year": $l_year,
+  "revenue": $revenue
 }
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q8_national_market_share/q8_national_market_share.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q8_national_market_share/q8_national_market_share.3.query.aql
index cac1248..3b28d11 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q8_national_market_share/q8_national_market_share.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q8_national_market_share/q8_national_market_share.3.query.aql
@@ -1,72 +1,73 @@
 use dataverse tpch;
 
 for $t in (
-for $slnrcop in 
-(for $s in dataset("Supplier")
-for $lnrcop in
-(
-for $lnrco in
-(
-for $l in dataset('LineItem')
-for $nrco in (
-	for $o in dataset('Orders')
-	for $nrc in (
-		for $c in dataset('Customer')
-		for $nr in (
-			for $n1 in dataset('Nation')
-			for $r1 in dataset('Region')
-				where $n1.n_regionkey = $r1.r_regionkey and $r1.r_name = 'AMERICA'
-			return {"n_nationkey": $n1.n_nationkey}
-		)
-		where $c.c_nationkey = $nr.n_nationkey
-		return {"c_custkey": $c.c_custkey}
-	)
-	where $nrc.c_custkey = $o.o_custkey
-	return {
-		"o_orderdate" : $o.o_orderdate, 
-		"o_orderkey": $o.o_orderkey 
-	}
+  for $slnrcop in (
+    for $s in dataset("Supplier")
+    for $lnrcop in (
+      for $lnrco in (
+        for $l in dataset('LineItem')
+        for $nrco in (
+          for $o in dataset('Orders')
+          for $nrc in (
+            for $c in dataset('Customer')
+            for $nr in (
+              for $n1 in dataset('Nation')
+              for $r1 in dataset('Region')
+              where $n1.n_regionkey = $r1.r_regionkey and $r1.r_name = 'AMERICA'
+              return { "n_nationkey": $n1.n_nationkey }
+            )
+            where $c.c_nationkey = $nr.n_nationkey
+            return { "c_custkey": $c.c_custkey }
+          )
+          where $nrc.c_custkey = $o.o_custkey
+          return {
+            "o_orderdate" : $o.o_orderdate, 
+            "o_orderkey": $o.o_orderkey 
+          }
+        )
+        where $l.l_orderkey = $nrco.o_orderkey
+          and $nrco.o_orderdate >= '1995-01-01' 
+          and $nrco.o_orderdate < '1996-12-31'
+        return {
+          "o_orderdate": $nrco.o_orderdate, 
+          "l_partkey": $l.l_partkey, 
+          "l_discount": $l.l_discount, 
+          "l_extendedprice": $l.l_extendedprice, 
+          "l_suppkey": $l.l_suppkey
+        }
+      )
+      for $p in dataset("Part")
+      where $p.p_partkey = $lnrco.l_partkey and $p.p_type = 'ECONOMY ANODIZED STEEL'
+      return {
+        "o_orderdate": $lnrco.o_orderdate, 
+        "l_discount": $lnrco.l_discount, 
+        "l_extendedprice": $lnrco.l_extendedprice, 
+        "l_suppkey": $lnrco.l_suppkey 
+      }
+    )
+    where $s.s_suppkey = $lnrcop.l_suppkey
+    return {
+      "o_orderdate": $lnrcop.o_orderdate, 
+      "l_discount": $lnrcop.l_discount, 
+      "l_extendedprice": $lnrcop.l_extendedprice, 
+      "l_suppkey": $lnrcop.l_suppkey, 
+      "s_nationkey": $s.s_nationkey
+    }
+  )
+  for $n2 in dataset('Nation')
+  where $slnrcop.s_nationkey = $n2.n_nationkey
+  let $o_year := get-year($slnrcop.o_orderdate)
+  return {
+    "year": $o_year,
+    "revenue": $slnrcop.l_extendedprice *(1-$slnrcop.l_discount),
+    "s_name": $n2.n_name
+  }
 )
-where $l.l_orderkey = $nrco.o_orderkey and $nrco.o_orderdate >= '1995-01-01' 
-                         and $nrco.o_orderdate < '1996-12-31'
-return {
-  "o_orderdate": $nrco.o_orderdate, 
-  "l_partkey": $l.l_partkey, 
-  "l_discount": $l.l_discount, 
-  "l_extendedprice": $l.l_extendedprice, 
-  "l_suppkey": $l.l_suppkey
-})
-for $p in dataset("Part")
-where $p.p_partkey = $lnrco.l_partkey and $p.p_type = 'ECONOMY ANODIZED STEEL'
-return {
-	"o_orderdate": $lnrco.o_orderdate, 
-	"l_discount": $lnrco.l_discount, 
-	"l_extendedprice": $lnrco.l_extendedprice, 
-	"l_suppkey": $lnrco.l_suppkey 
-}
-)
-where $s.s_suppkey = $lnrcop.l_suppkey
-return {
-	"o_orderdate": $lnrcop.o_orderdate, 
-	"l_discount": $lnrcop.l_discount, 
-	"l_extendedprice": $lnrcop.l_extendedprice, 
-	"l_suppkey": $lnrcop.l_suppkey, 
-	"s_nationkey": $s.s_nationkey
-})
-
-for $n2 in dataset('Nation')
-where $slnrcop.s_nationkey = $n2.n_nationkey
-let $o_year := get-year($slnrcop.o_orderdate)
-return {
-  "year": $o_year,
-  "revenue": $slnrcop.l_extendedprice *(1-$slnrcop.l_discount),
-  "s_name": $n2.n_name
-})
 group by $year := $t.year with $t
 order by $year
 return {
-	"year": $year,
-	"mkt_share":sum(for $i in $t return switch-case($i.s_name='BRAZIL', true, $i.revenue, false, 0.0))/ 
-				sum(for $i in $t return $i.revenue)
+  "year": $year,
+  "mkt_share": sum(for $i in $t return switch-case($i.s_name='BRAZIL', true, $i.revenue, false, 0.0))/ 
+        sum(for $i in $t return $i.revenue)
 }
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.1.ddl.aql
index c7fbbc6..8af3065 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.1.ddl.aql
@@ -100,7 +100,7 @@
   primary key n_nationkey;
 create dataset Part(PartType)
   primary key p_partkey;
-create dataset PartSupp(PartSuppType)
+create dataset Partsupp(PartSuppType)
   primary key ps_partkey, ps_suppkey;  
 create dataset Customer(CustomerType) 
   primary key c_custkey;
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.2.update.aql
index c68e0af..0adc8e1 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.2.update.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.2.update.aql
@@ -24,7 +24,7 @@
 using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
 (("path"="nc1://data/tpch0.001/part.tbl"),("format"="delimited-text"),("delimiter"="|")) pre-sorted;
 
-load dataset PartSupp 
+load dataset Partsupp 
 using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
 (("path"="nc1://data/tpch0.001/partsupp.tbl"),("format"="delimited-text"),("delimiter"="|")) pre-sorted;
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.3.query.aql
index 2e54083..11191d3 100644
--- a/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/q9_product_type_profit_nt/q9_product_type_profit_nt.3.query.aql
@@ -1,40 +1,67 @@
 use dataverse tpch;
-                
-for $profit in 
-(  for $o in dataset('Orders')
-   for $l3 in 
-   (  for $p in dataset('Part')
-      for $l2 in 
-      (  for $ps in dataset('PartSupp')
-         for $l1 in 
-        (  for $s1 in
-            (  for $s in dataset('Supplier')
-               for $n in dataset('Nation')
-               where $n.n_nationkey = $s.s_nationkey
-               return 
-                 { "s_suppkey": $s.s_suppkey, "n_name": $n.n_name}  )
-            for $l in dataset('LineItem')
-            where $s1.s_suppkey = $l.l_suppkey       
-            return 
-              { "l_suppkey": $l.l_suppkey, "l_extendedprice": $l.l_extendedprice, "l_discount": $l.l_discount,
-                "l_quantity": $l.l_quantity, "l_partkey": $l.l_partkey, "l_orderkey": $l.l_orderkey, "n_name": $s1.n_name } )
-         where $ps.ps_suppkey = $l1.l_suppkey and $ps.ps_partkey = $l1.l_partkey       
-         return 
-           { "l_extendedprice": $l1.l_extendedprice, "l_discount": $l1.l_discount, "l_quantity": $l1.l_quantity, 
-             "l_partkey": $l1.l_partkey, "l_orderkey": $l1.l_orderkey, "n_name": $l1.n_name, "ps_supplycost": $ps.ps_supplycost }  )
-      where contains($p.p_name, 'green') and $p.p_partkey = $l2.l_partkey    
-      return 
-        { "l_extendedprice": $l2.l_extendedprice, "l_discount": $l2.l_discount, "l_quantity": $l2.l_quantity, 
-          "l_orderkey": $l2.l_orderkey, "n_name": $l2.n_name, "ps_supplycost": $l2.ps_supplycost  }  )
-   where $o.o_orderkey = $l3.l_orderkey
-   let $amount := $l3.l_extendedprice * (1 - $l3.l_discount) -  $l3.ps_supplycost * $l3.l_quantity 
-   let $o_year := get-year($o.o_orderdate)
-   return 
-     { "nation": $l3.n_name, "o_year": $o_year, "amount": $amount }  )
- group by $nation := $profit.nation, $o_year := $profit.o_year with $profit
- order by $nation, $o_year desc
- return 
-   { "nation": $nation, 
-     "o_year": $o_year, 
-      "sum_profit": sum( for $pr in $profit return $pr.amount )  }    
-   
+
+for $profit in (
+  for $o in dataset('Orders')
+  for $l3 in (
+    for $p in dataset('Part')
+    for $l2 in (
+      for $ps in dataset('Partsupp')
+      for $l1 in (
+        for $s1 in (
+          for $s in dataset('Supplier')
+          for $n in dataset('Nation')
+          where $n.n_nationkey = $s.s_nationkey
+          return {
+            "s_suppkey": $s.s_suppkey,
+            "n_name": $n.n_name
+          }
+        )
+        for $l in dataset('LineItem')
+        where $s1.s_suppkey = $l.l_suppkey       
+        return  {
+          "l_suppkey": $l.l_suppkey,
+          "l_extendedprice": $l.l_extendedprice,
+          "l_discount": $l.l_discount,
+          "l_quantity": $l.l_quantity,
+          "l_partkey": $l.l_partkey,
+          "l_orderkey": $l.l_orderkey,
+          "n_name": $s1.n_name
+        }
+      )
+      where $ps.ps_suppkey = $l1.l_suppkey and $ps.ps_partkey = $l1.l_partkey       
+      return {
+        "l_extendedprice": $l1.l_extendedprice,
+        "l_discount": $l1.l_discount,
+        "l_quantity": $l1.l_quantity,
+        "l_partkey": $l1.l_partkey,
+        "l_orderkey": $l1.l_orderkey,
+        "n_name": $l1.n_name,
+        "ps_supplycost": $ps.ps_supplycost
+      }
+    )
+    where contains($p.p_name, 'green') and $p.p_partkey = $l2.l_partkey    
+    return {
+      "l_extendedprice": $l2.l_extendedprice,
+      "l_discount": $l2.l_discount,
+      "l_quantity": $l2.l_quantity,
+      "l_orderkey": $l2.l_orderkey,
+      "n_name": $l2.n_name,
+      "ps_supplycost": $l2.ps_supplycost
+    }
+  )
+  where $o.o_orderkey = $l3.l_orderkey
+  let $amount := $l3.l_extendedprice * (1 - $l3.l_discount) -  $l3.ps_supplycost * $l3.l_quantity 
+  let $o_year := get-year($o.o_orderdate)
+  return {
+    "nation": $l3.n_name,
+    "o_year": $o_year,
+    "amount": $amount
+  }
+)
+group by $nation := $profit.nation, $o_year := $profit.o_year with $profit
+order by $nation, $o_year desc
+return {
+  "nation": $nation, 
+  "o_year": $o_year, 
+  "sum_profit": sum( for $pr in $profit return $pr.amount )
+}
diff --git a/asterix-app/src/test/resources/tpch/queries/local/write-custorder.aql b/asterix-app/src/test/resources/tpch/queries/local/write-custorder.aql
index b767d57..78375e6 100644
--- a/asterix-app/src/test/resources/tpch/queries/local/write-custorder.aql
+++ b/asterix-app/src/test/resources/tpch/queries/local/write-custorder.aql
@@ -58,7 +58,7 @@
 declare dataset CustOrder(CustOrderType)
   primary key co_custkey on group1;
 
-write into dataset CustOrder 
+insert into dataset CustOrder 
 ( for $c in dataset('Customers')
   for $o in dataset('Orders')
   where $c.c_custkey = $o.o_custkey
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/Statement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/Statement.java
index 2064d47..98f8387 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/Statement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/Statement.java
@@ -26,7 +26,6 @@
         DML_CMD_LIST,
         FUNCTION_DECL,
         LOAD_FROM_FILE,
-        WRITE_FROM_QUERY_RESULT,
         NODEGROUP_DECL,
         NODEGROUP_DROP,
         QUERY,
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java
deleted file mode 100644
index 06e452d..0000000
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright 2009-2013 by The Regents of the University of California
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * you may obtain a copy of the License from
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package edu.uci.ics.asterix.aql.expression;
-
-import edu.uci.ics.asterix.aql.base.Statement;
-import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
-import edu.uci.ics.asterix.aql.expression.visitor.IAqlVisitorWithVoidReturn;
-import edu.uci.ics.asterix.common.exceptions.AsterixException;
-
-public class WriteFromQueryResultStatement implements Statement {
-
-    private Identifier dataverseName;
-    private Identifier datasetName;
-
-    private Query query;
-    private int varCounter;
-
-    public WriteFromQueryResultStatement(Identifier dataverseName, Identifier datasetName, Query query, int varCounter) {
-        this.dataverseName = dataverseName;
-        this.datasetName = datasetName;
-        this.query = query;
-        this.varCounter = varCounter;
-    }
-
-    @Override
-    public Kind getKind() {
-        return Kind.WRITE_FROM_QUERY_RESULT;
-    }
-
-    public Identifier getDataverseName() {
-        return dataverseName;
-    }
-
-    public Identifier getDatasetName() {
-        return datasetName;
-    }
-
-    public Query getQuery() {
-        return query;
-    }
-
-    public int getVarCounter() {
-        return varCounter;
-    }
-
-    @Override
-    public <R, T> R accept(IAqlExpressionVisitor<R, T> visitor, T arg) throws AsterixException {
-        return visitor.visitLoadFromQueryResultStatement(this, arg);
-    }
-
-    @Override
-    public <T> void accept(IAqlVisitorWithVoidReturn<T> visitor, T arg) throws AsterixException {
-        visitor.visit(this, arg);
-    }
-
-}
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java
index 652aff5..cdbd86f 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java
@@ -76,7 +76,6 @@
 import edu.uci.ics.asterix.aql.expression.UpdateStatement;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
@@ -421,12 +420,6 @@
     }
 
     @Override
-    public void visit(WriteFromQueryResultStatement stmtLoad, Integer arg) throws AsterixException {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
     public void visit(DropStatement stmtDel, Integer arg) throws AsterixException {
         // TODO Auto-generated method stub
 
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlExpressionVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlExpressionVisitor.java
index f6b05d8..7fbf342 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlExpressionVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlExpressionVisitor.java
@@ -61,7 +61,6 @@
 import edu.uci.ics.asterix.aql.expression.UpdateStatement;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 
@@ -79,8 +78,6 @@
 
     R visitLoadFromFileStatement(LoadFromFileStatement stmtLoad, T arg) throws AsterixException;
 
-    R visitLoadFromQueryResultStatement(WriteFromQueryResultStatement stmtLoad, T arg) throws AsterixException;
-
     R visitDropStatement(DropStatement del, T arg) throws AsterixException;
 
     R visitInsertStatement(InsertStatement insert, T arg) throws AsterixException;
@@ -161,8 +158,6 @@
 
     R visitDataverseDecl(DataverseDecl dv, T arg) throws AsterixException;
 
-    R visitWriteFromQueryResultStatement(WriteFromQueryResultStatement stmtLoad, T arg) throws AsterixException;
-
     R visit(CreateFunctionStatement cfs, T arg) throws AsterixException;
 
     R visitFunctionDropStatement(FunctionDropStatement del, T arg) throws AsterixException;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlVisitorWithVoidReturn.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlVisitorWithVoidReturn.java
index fe85851..b936c63 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlVisitorWithVoidReturn.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/IAqlVisitorWithVoidReturn.java
@@ -61,7 +61,6 @@
 import edu.uci.ics.asterix.aql.expression.UpdateStatement;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 
@@ -87,8 +86,6 @@
 
     void visit(UpdateClause updateClause, T arg) throws AsterixException;
 
-    void visit(WriteFromQueryResultStatement stmtLoad, T arg) throws AsterixException;
-
     void visit(DatasetDecl dd, T arg) throws AsterixException;
 
     void visit(LiteralExpr l, T arg) throws AsterixException;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java
index cf6020e..a9d8585 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java
@@ -75,7 +75,6 @@
 import edu.uci.ics.asterix.aql.expression.VarIdentifier;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
@@ -485,13 +484,6 @@
         }
 
         @Override
-        public Void visitWriteFromQueryResultStatement(WriteFromQueryResultStatement stmtLoad, Void arg)
-                throws AsterixException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
         public Void visitWriteStatement(WriteStatement ws, Void arg) throws AsterixException {
             // TODO Auto-generated method stub
             return null;
@@ -502,13 +494,6 @@
         }
 
         @Override
-        public Void visitLoadFromQueryResultStatement(WriteFromQueryResultStatement stmtLoad, Void arg)
-                throws AsterixException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
         public Void visitCreateDataverseStatement(CreateDataverseStatement del, Void arg) throws AsterixException {
             // TODO Auto-generated method stub
             return null;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java
index 4f2f796..dcb4a66 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java
@@ -72,7 +72,6 @@
 import edu.uci.ics.asterix.aql.expression.VarIdentifier;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
@@ -436,13 +435,6 @@
     }
 
     @Override
-    public Pair<IAqlExpression, List<VariableSubstitution>> visitWriteFromQueryResultStatement(
-            WriteFromQueryResultStatement stmtLoad, List<VariableSubstitution> arg) throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Pair<IAqlExpression, List<VariableSubstitution>> visitCreateIndexStatement(CreateIndexStatement cis,
             List<VariableSubstitution> arg) throws AsterixException {
         // TODO Auto-generated method stub
@@ -536,13 +528,6 @@
     }
 
     @Override
-    public Pair<IAqlExpression, List<VariableSubstitution>> visitLoadFromQueryResultStatement(
-            WriteFromQueryResultStatement stmtLoad, List<VariableSubstitution> arg) throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Pair<IAqlExpression, List<VariableSubstitution>> visitCreateDataverseStatement(CreateDataverseStatement del,
             List<VariableSubstitution> arg) throws AsterixException {
         // TODO Auto-generated method stub
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java
index 4858789..ae5d7d9 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java
@@ -73,7 +73,6 @@
 import edu.uci.ics.asterix.aql.expression.VarIdentifier;
 import edu.uci.ics.asterix.aql.expression.VariableExpr;
 import edu.uci.ics.asterix.aql.expression.WhereClause;
-import edu.uci.ics.asterix.aql.expression.WriteFromQueryResultStatement;
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
@@ -461,26 +460,12 @@
     }
 
     @Override
-    public Boolean visitWriteFromQueryResultStatement(WriteFromQueryResultStatement stmtLoad, List<FunctionDecl> arg)
-            throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Boolean visitWriteStatement(WriteStatement ws, List<FunctionDecl> arg) throws AsterixException {
         // TODO Auto-generated method stub
         return null;
     }
 
     @Override
-    public Boolean visitLoadFromQueryResultStatement(WriteFromQueryResultStatement stmtLoad, List<FunctionDecl> arg)
-            throws AsterixException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public Boolean visitCreateDataverseStatement(CreateDataverseStatement del, List<FunctionDecl> arg)
             throws AsterixException {
         // TODO Auto-generated method stub
diff --git a/asterix-aql/src/main/javacc/AQL.jj b/asterix-aql/src/main/javacc/AQL.jj
index 49122e6..84d46f5 100644
--- a/asterix-aql/src/main/javacc/AQL.jj
+++ b/asterix-aql/src/main/javacc/AQL.jj
@@ -678,30 +678,15 @@
 {
   String nodeName = null;
   String fileName = null;
-  Statement stmt = null;
   Query query;
   String writerClass = null;
   Pair<Identifier,Identifier> nameComponents = null;
 }
 {
-  "write" ((
-    "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
+  "write" "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
     ( "using" writerClass = StringLiteral() )?
-      {                  
-        stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);         
-      }
-  ) | (
-    "into" <DATASET> 
-      {
-        nameComponents = QualifiedName();
-      }
-    <LEFTPAREN> query = Query() <RIGHTPAREN>
-      {
-        stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
-      }
-  ))
-    {
-      return stmt;
+    {                  
+      return new WriteStatement(new Identifier(nodeName), fileName, writerClass);         
     }
 }