← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/gen-flwor-opt into lp:zorba

 

Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/gen-flwor-opt into lp:zorba.

Commit message:
1. Implemented flwor-merge optimization for general FLWOR.
2. Removed gflwor_expr_kind


Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/gen-flwor-opt/+merge/165694

1. Implemented flwor-merge optimization for general FLWOR.
2. Removed gflwor_expr_kind
-- 
https://code.launchpad.net/~zorba-coders/zorba/gen-flwor-opt/+merge/165694
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'ChangeLog'
--- ChangeLog	2013-05-24 00:34:25 +0000
+++ ChangeLog	2013-05-24 20:18:27 +0000
@@ -7,7 +7,9 @@
 
 Optimizations:
   * Implemented hoisting optimization for general FLWOR.
+  * Implemented flwor-merge optimization for general FLWOR.
   * Implemented document-ordering elimination for general FLWOR.
+  * Applied count optimization to the return clause of general FLWORs.
   * Optimized implementation of fn:deep-equal
   * No need to apply document ordering on the domain expression of a FOR clause
     if the FOR clause is followed by an orderby or groupby clause.

=== modified file 'src/compiler/expression/expr_base.cpp'
--- src/compiler/expression/expr_base.cpp	2013-03-15 08:22:41 +0000
+++ src/compiler/expression/expr_base.cpp	2013-05-24 20:18:27 +0000
@@ -898,7 +898,6 @@
   }
 
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     const flwor_expr* flworExpr = static_cast<const flwor_expr *>(this);
     bool haveOrderBy = false;

=== modified file 'src/compiler/expression/expr_base.h'
--- src/compiler/expression/expr_base.h	2013-03-15 08:22:41 +0000
+++ src/compiler/expression/expr_base.h	2013-05-24 20:18:27 +0000
@@ -29,8 +29,6 @@
 
 #include "functions/function_consts.h"
 
-//#include "types/typeimpl.h"
-
 #include "context/static_context_consts.h"
 
 
@@ -65,7 +63,6 @@
   match_expr_kind,
 
   flwor_expr_kind,
-  gflwor_expr_kind,
   if_expr_kind,
   trycatch_expr_kind,
 

=== modified file 'src/compiler/expression/expr_clone.cpp'
--- src/compiler/expression/expr_clone.cpp	2013-04-24 01:35:58 +0000
+++ src/compiler/expression/expr_clone.cpp	2013-05-24 20:18:27 +0000
@@ -261,12 +261,10 @@
   }
 
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     const flwor_expr* e = static_cast<const flwor_expr*>(this);
 
-    flwor_expr* cloneExpr = theCCB->theEM->
-    create_flwor_expr(theSctx, udf, theLoc, e->is_general());
+    flwor_expr* cloneExpr = theCCB->theEM->create_flwor_expr(theSctx, udf, theLoc);
 
     csize numClauses = e->num_clauses();
 

=== modified file 'src/compiler/expression/expr_iter.cpp'
--- src/compiler/expression/expr_iter.cpp	2013-04-24 01:35:58 +0000
+++ src/compiler/expression/expr_iter.cpp	2013-05-24 20:18:27 +0000
@@ -97,7 +97,6 @@
   switch (theExpr->get_expr_kind())
   {
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     flwor_expr* flworExpr = static_cast<flwor_expr*>(theExpr);
 

=== modified file 'src/compiler/expression/expr_manager.cpp'
--- src/compiler/expression/expr_manager.cpp	2013-05-08 20:14:47 +0000
+++ src/compiler/expression/expr_manager.cpp	2013-05-24 20:18:27 +0000
@@ -1017,10 +1017,9 @@
 flwor_expr* ExprManager::create_flwor_expr(
     static_context* sctx,
     user_function* udf,
-    const QueryLoc& loc,
-    bool general)
+    const QueryLoc& loc)
 {
-  CREATE_AND_RETURN_EXPR(flwor_expr, sctx, udf, loc, general);
+  CREATE_AND_RETURN_EXPR(flwor_expr, sctx, udf, loc);
 }
 
 

=== modified file 'src/compiler/expression/expr_manager.h'
--- src/compiler/expression/expr_manager.h	2013-04-16 20:06:08 +0000
+++ src/compiler/expression/expr_manager.h	2013-05-24 20:18:27 +0000
@@ -341,8 +341,6 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-#ifdef ZORBA_WITH_JSON
-
   json_array_expr* create_json_array_expr(
       static_context* sctx,
       user_function* udf,
@@ -363,8 +361,6 @@
       std::vector<expr*>& names,
       std::vector<expr*>& values);
 
-#endif
-
 ////////////////////////////////////////////////////////////////////////////////
 
   insert_expr* create_insert_expr(
@@ -614,8 +610,7 @@
   flwor_expr* create_flwor_expr(
       static_context* sctx,
       user_function* udf,
-      const QueryLoc& loc,
-      bool general);
+      const QueryLoc& loc);
 
   pragma* create_pragma(
       const store::Item_t&,
@@ -624,8 +619,6 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-#ifdef ZORBA_WITH_JSON
-
 json_array_expr* create_json_array_expr(
       static_context* sctx,
       user_function* udf,
@@ -646,8 +639,6 @@
       std::vector<expr*>& names,
       std::vector<expr*>& values);
 
-#endif
-
 } // namespace zorba
 
 #endif // ZORBA_COMPILER_EXPRMANAGER_H

=== modified file 'src/compiler/expression/expr_type.cpp'
--- src/compiler/expression/expr_type.cpp	2013-05-06 08:47:57 +0000
+++ src/compiler/expression/expr_type.cpp	2013-05-24 20:18:27 +0000
@@ -116,7 +116,6 @@
   }
 
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     flwor_expr* e = static_cast<flwor_expr*>(this);
 

=== modified file 'src/compiler/expression/flwor_expr.cpp'
--- src/compiler/expression/flwor_expr.cpp	2013-05-13 13:58:24 +0000
+++ src/compiler/expression/flwor_expr.cpp	2013-05-24 20:18:27 +0000
@@ -756,12 +756,11 @@
     CompilerCB* ccb,
     static_context* sctx,
     user_function* udf,
-    const QueryLoc& loc,
-    bool general)
+    const QueryLoc& loc)
   :
-  expr(ccb, sctx, udf, loc, (general ? gflwor_expr_kind : flwor_expr_kind)),
-  theHasSequentialClauses(false),
-  theReturnExpr(NULL)
+  expr(ccb, sctx, udf, loc, flwor_expr_kind),
+  theReturnExpr(NULL),
+  theFlworFlags(0)
 {
   theScriptingKind = SIMPLE_EXPR;
 }
@@ -770,6 +769,30 @@
 /*******************************************************************************
 
 ********************************************************************************/
+void flwor_expr::set_general(bool v) 
+{
+  if (v)
+    theFlworFlags |= IS_GENERAL;
+  else
+    theFlworFlags &= ~IS_GENERAL;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void flwor_expr::set_sequential_clauses(bool v) 
+{
+  if (v)
+    theFlworFlags |= HAS_SEQUENTIAL_CLAUSES;
+  else
+    theFlworFlags &= ~HAS_SEQUENTIAL_CLAUSES;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
 flwor_clause* flwor_expr::get_clause(csize i) const
 {
   assert(i < theClauses.size());
@@ -1094,7 +1117,7 @@
       theScriptingKind |= c2->get_expr()->get_scripting_detail();
 
       if (c2->get_expr()->is_sequential())
-        theHasSequentialClauses = true;
+        set_sequential_clauses(true);
     }
   }
 

=== modified file 'src/compiler/expression/flwor_expr.h'
--- src/compiler/expression/flwor_expr.h	2013-05-13 13:58:24 +0000
+++ src/compiler/expression/flwor_expr.h	2013-05-24 20:18:27 +0000
@@ -661,22 +661,34 @@
   typedef std::vector<flwor_clause*> clause_list_t;
 
 protected:
-  bool            theHasSequentialClauses;
+  enum FlworFlags
+  {
+    HAS_SEQUENTIAL_CLAUSES = 0x1,
+    IS_GENERAL             = 0x2
+  };
+
+protected:
   clause_list_t   theClauses;
   expr          * theReturnExpr;
+  ulong           theFlworFlags;
 
 protected:
   flwor_expr(
       CompilerCB* ccb,
       static_context* sctx,
       user_function* udf,
-      const QueryLoc& loc,
-      bool general);
+      const QueryLoc& loc);
 
 public:
-  bool is_general() const { return get_expr_kind() == gflwor_expr_kind; }
-
-  void set_general(bool v) { theKind = (v ? gflwor_expr_kind : flwor_expr_kind); }
+  void compute_scripting_kind();
+
+  bool has_sequential_clauses() const { return theFlworFlags & HAS_SEQUENTIAL_CLAUSES; }
+
+  void set_sequential_clauses(bool v);
+
+  bool is_general() const { return theFlworFlags & IS_GENERAL; }
+
+  void set_general(bool v);
 
   bool compute_is_general();
 
@@ -690,10 +702,6 @@
     compute_scripting_kind();
   }
 
-  bool has_sequential_clauses() const { return theHasSequentialClauses; }
-
-  void compute_scripting_kind();
-
   csize num_clauses() const { return theClauses.size(); }
 
   void add_clause(flwor_clause* c, bool computeScriptingKind = true);

=== modified file 'src/compiler/rewriter/rules/flwor_rules.cpp'
--- src/compiler/rewriter/rules/flwor_rules.cpp	2013-04-29 14:39:04 +0000
+++ src/compiler/rewriter/rules/flwor_rules.cpp	2013-05-24 20:18:27 +0000
@@ -189,8 +189,7 @@
 *******************************************************************************/
 RULE_REWRITE_PRE(EliminateUnusedLetVars)
 {
-  if (node->get_expr_kind() != flwor_expr_kind &&
-      node->get_expr_kind() != gflwor_expr_kind)
+  if (node->get_expr_kind() != flwor_expr_kind)
     return NULL;
 
   const QueryLoc& loc = node->get_loc();
@@ -585,7 +584,6 @@
   switch (node->get_expr_kind())
   {
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     bool unsafe1 = unsafe || !isSafeVar;
 
@@ -1003,11 +1001,8 @@
 ******************************************************************************/
 RULE_REWRITE_PRE(RefactorPredFLWOR)
 {
-  if (node->get_expr_kind() != flwor_expr_kind &&
-      node->get_expr_kind() != gflwor_expr_kind)
-  {
+  if (node->get_expr_kind() != flwor_expr_kind)
     return NULL;
-  }
 
   bool modified = false;
   flwor_expr* flwor = static_cast<flwor_expr*>(node);
@@ -1577,8 +1572,7 @@
 *******************************************************************************/
 expr* MergeFLWOR::apply(RewriterContext& rCtx, expr* node, bool& modified)
 {
-  if (node->get_expr_kind() == flwor_expr_kind ||
-      node->get_expr_kind() == gflwor_expr_kind)
+  if (node->get_expr_kind() == flwor_expr_kind)
   {
     flwor_expr* flwor = static_cast<flwor_expr *>(node);
 
@@ -1592,37 +1586,23 @@
 
       flwor_expr* returnFlwor = static_cast<flwor_expr*>(flwor->get_return_expr());
 
-      // If the outer flwor is not general, and it contains where, groupby, or
-      // orderby clauses, we cannot merge because for/let clauses cannot appear
-      // after where, groupby, or orderby clauses,
-      if (!flwor->is_general())
-      {
-        csize numClauses = flwor->num_clauses();
-        
-        for (csize i = 0; i < numClauses; ++i)
-        {
-          const flwor_clause* c = flwor->get_clause(i);
-          
-          if (c->get_kind() == flwor_clause::where_clause ||
-              c->get_kind() == flwor_clause::groupby_clause ||
-              c->get_kind() == flwor_clause::orderby_clause)
-          {
-            goto next1;
-          }
-        }
-      }
-      
       csize numClauses = returnFlwor->num_clauses();
 
       for (csize i = 0; i < numClauses; ++i)
       {
         const flwor_clause* c = returnFlwor->get_clause(i);
         
-        if (c->get_kind() == flwor_clause::groupby_clause ||
-            c->get_kind() == flwor_clause::orderby_clause)
+        switch (c->get_kind())
+        {
+        case flwor_clause::groupby_clause:
+        case flwor_clause::orderby_clause:
+        case flwor_clause::count_clause:
         {
           goto next1;
         }
+        default:
+          break;
+        }
       }
       
       for (csize i = 0; i < numClauses; ++i)
@@ -1700,30 +1680,31 @@
           numNestedClauses = nestedFlwor->num_clauses();
           merge = true;
 
-          for (csize j = 0; j < numNestedClauses; ++j)
+          for (csize j = 0; j < numNestedClauses && merge; ++j)
           {
             flwor_clause* nestedClause = nestedFlwor->get_clause(j);
-            flwor_clause::ClauseKind nestedClauseKind = nestedClause->get_kind();
-            
-            if (nestedClauseKind == flwor_clause::let_clause)
-              continue;
 
-            if (nestedClauseKind == flwor_clause::for_clause ||
-                nestedClauseKind == flwor_clause::where_clause)
+            switch (nestedClause->get_kind())
+            {
+            case flwor_clause::let_clause:
+            {
+              break;
+            }
+            case flwor_clause::for_clause:
+            case flwor_clause::window_clause:
+            case flwor_clause::where_clause:
             {
               if (isOuter || hasPosVar)
-              {
                 merge = false;
-                break;
-              }
-              else
-              {
-                continue;
-              }
-            }
 
-            merge = false;
-            break;
+              break;
+            }
+            default:
+            {
+              merge = false;
+              break;
+            }
+            }              
           }
         }
       }
@@ -1734,13 +1715,6 @@
         {
           flwor_clause* nestedClause = nestedFlwor->get_clause(j);
           flwor->add_clause(i+j, nestedClause);
-
-          if (!flwor->is_general() &&
-              nestedClause->get_kind() == flwor_clause::where_clause &&
-              i != numClauses - 1)
-          {
-            flwor->set_general(true);
-          }
         }
         
         c->set_expr(nestedFlwor->get_return_expr());

=== modified file 'src/compiler/rewriter/rules/fold_rules.cpp'
--- src/compiler/rewriter/rules/fold_rules.cpp	2013-05-22 21:03:15 +0000
+++ src/compiler/rewriter/rules/fold_rules.cpp	2013-05-24 20:18:27 +0000
@@ -332,7 +332,6 @@
   // the flwor expr itself
 
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     flwor_expr* flwor = static_cast<flwor_expr *> (node);
 
@@ -746,8 +745,7 @@
       }
     }
 
-    if (arg->get_expr_kind() == flwor_expr_kind ||
-        arg->get_expr_kind() == gflwor_expr_kind)
+    if (arg->get_expr_kind() == flwor_expr_kind)
     {
       bool modified = false;
       expr* newArg = partial_eval_return_clause(static_cast<flwor_expr*>(arg),
@@ -1022,8 +1020,7 @@
     }
   }
 
-  if (returnExpr->get_expr_kind() == flwor_expr_kind ||
-      returnExpr->get_expr_kind() == gflwor_expr_kind)
+  if (returnExpr->get_expr_kind() == flwor_expr_kind)
   {
     expr* newRet =
     partial_eval_return_clause(static_cast<flwor_expr*>(returnExpr),  modified, rCtx);

=== modified file 'src/compiler/rewriter/rules/hoist_rules.cpp'
--- src/compiler/rewriter/rules/hoist_rules.cpp	2013-05-16 09:13:48 +0000
+++ src/compiler/rewriter/rules/hoist_rules.cpp	2013-05-24 20:18:27 +0000
@@ -115,8 +115,7 @@
 {
   bool status = false;
 
-  if (e->get_expr_kind() == flwor_expr_kind ||
-      e->get_expr_kind() == gflwor_expr_kind)
+  if (e->get_expr_kind() == flwor_expr_kind)
   {
     flwor_expr* flwor = static_cast<flwor_expr *>(e);
 
@@ -484,9 +483,7 @@
   PathHolder* nextstep = NULL;
 
   bool inloop = false;
-  bool foundReferencedFLWORVar = false;
-  bool foundSequentialClause = false;
-  int i = 0;
+  int clauseNo = 0;
 
   // step->prev == NULL means that expr e is not inside any flwor expr, and as a
   // result, there is nothing to hoist.
@@ -518,20 +515,22 @@
     }
     else
     {
-      assert(step->theExpr->get_expr_kind() == flwor_expr_kind ||
-             step->theExpr->get_expr_kind() == gflwor_expr_kind);
+      assert(step->theExpr->get_expr_kind() == flwor_expr_kind);
 
       flwor_expr* flwor = static_cast<flwor_expr*>(step->theExpr);
 
+      bool foundReferencedFLWORVar = false;
+      bool foundSequentialClause = false;
+
       // Check whether expr e references any variables from the current flwor. If
       // not, then e can be hoisted out of the current flwor and we repeat the
       // while-loop to see if e can be hoisted w.r.t. the previous (outer) flwor.
       // If yes, then let V be the inner-most var referenced by e. If there are any
       // FOR vars after V, e can be hoisted out of any such FOR vars. Otherwise, e
       // cannot be hoisted.
-      for (i = step->clauseCount - 1; i >= 0; --i)
+      for (clauseNo = step->clauseCount - 1; clauseNo >= 0; --clauseNo)
       {
-        flwor_clause* c = flwor->get_clause(i);
+        flwor_clause* c = flwor->get_clause(clauseNo);
 
         switch (c->get_kind())
         {
@@ -587,7 +586,7 @@
                 {
                   ZORBA_ASSERT(nextstep);
                   step = nextstep;
-                  i = -1;
+                  clauseNo = -1;
                 }
 
                 break;
@@ -620,7 +619,7 @@
                 {
                   ZORBA_ASSERT(nextstep);
                   step = nextstep;
-                  i = -1;
+                  clauseNo = -1;
                 }
 
                 break;
@@ -712,7 +711,7 @@
   // var: $$temp := op:hoist(e) (b) we place the $$temp declaration right after
   // variable V, and (c) we replace e with op:unhoist($$temp).
 
-  var_expr* letvar(rCtx.createTempVar(sctx, loc, var_expr::let_var));
+  var_expr* letvar = rCtx.createTempVar(sctx, loc, var_expr::let_var);
 
   expr* hoisted = rCtx.theEM->
   create_fo_expr(sctx, udf, loc, BUILTIN_FUNC(OP_HOIST_1), e);
@@ -725,21 +724,20 @@
   letvar->set_flwor_clause(flref);
 
   ZORBA_ASSERT(step->theExpr == NULL ||
-               step->theExpr->get_expr_kind() == flwor_expr_kind ||
-               step->theExpr->get_expr_kind() == gflwor_expr_kind);
+               step->theExpr->get_expr_kind() == flwor_expr_kind);
 
   if (step->prev == NULL)
   {
     if (step->theExpr == NULL)
     {
-      step->theExpr = rCtx.theEM->create_flwor_expr(sctx, udf, loc, false);
+      step->theExpr = rCtx.theEM->create_flwor_expr(sctx, udf, loc);
     }
 
     static_cast<flwor_expr*>(step->theExpr)->add_clause(flref);
   }
   else
   {
-    static_cast<flwor_expr*>(step->theExpr)->add_clause(i + 1, flref);
+    static_cast<flwor_expr*>(step->theExpr)->add_clause(clauseNo + 1, flref);
     ++step->clauseCount;
   }
 

=== modified file 'src/compiler/rewriter/rules/index_join_rule.cpp'
--- src/compiler/rewriter/rules/index_join_rule.cpp	2013-03-16 20:19:39 +0000
+++ src/compiler/rewriter/rules/index_join_rule.cpp	2013-05-24 20:18:27 +0000
@@ -113,7 +113,7 @@
 
   expr_kind_t nodeKind = node->get_expr_kind();
 
-  if (nodeKind == flwor_expr_kind || nodeKind == gflwor_expr_kind)
+  if (nodeKind == flwor_expr_kind)
   {
     flworExpr = static_cast<flwor_expr *>(node);
 
@@ -190,7 +190,7 @@
 
   // No index-join rewrite done, so drill down.
 
-  if (nodeKind == flwor_expr_kind || nodeKind == gflwor_expr_kind)
+  if (nodeKind == flwor_expr_kind)
   {
     csize numClauses = flworExpr->num_clauses();
     csize clausePos = 0;
@@ -730,7 +730,7 @@
     expr* currExpr = theVarDefExprs[i];
     expr_kind_t currKind = currExpr->get_expr_kind();
 
-    if (currKind == flwor_expr_kind || currKind == gflwor_expr_kind)
+    if (currKind == flwor_expr_kind)
     {
       flwor_expr* flwor = static_cast<flwor_expr*>(currExpr);
       csize numClauses = flwor->num_clauses();
@@ -868,7 +868,7 @@
     expr* currExpr = theVarDefExprs[i];
     expr_kind_t currKind = currExpr->get_expr_kind();
 
-    if (currKind == flwor_expr_kind || currKind == gflwor_expr_kind)
+    if (currKind == flwor_expr_kind)
     {
       flwor_expr* flwor = static_cast<flwor_expr*>(currExpr);
       csize numClauses = flwor->num_clauses();
@@ -1050,7 +1050,7 @@
 
     block->add(targetPos, createExpr);
   }
-  else if (targetKind == flwor_expr_kind || targetKind == gflwor_expr_kind)
+  else if (targetKind == flwor_expr_kind)
   {
     flwor_expr* flwor = static_cast<flwor_expr*>(targetExpr);
     csize numClauses = flwor->num_clauses();

=== modified file 'src/compiler/rewriter/rules/index_matching_rule.cpp'
--- src/compiler/rewriter/rules/index_matching_rule.cpp	2013-05-04 20:20:05 +0000
+++ src/compiler/rewriter/rules/index_matching_rule.cpp	2013-05-24 20:18:27 +0000
@@ -129,8 +129,7 @@
   if (theIndexDecl->isGeneral())
     return node;
 
-  if (node->get_expr_kind() == flwor_expr_kind ||
-      node->get_expr_kind() == gflwor_expr_kind)
+  if (node->get_expr_kind() == flwor_expr_kind)
   {
     theQueryExpr = static_cast<flwor_expr*>(node);
 

=== modified file 'src/compiler/rewriter/rules/nodeid_rules.cpp'
--- src/compiler/rewriter/rules/nodeid_rules.cpp	2013-05-23 02:27:38 +0000
+++ src/compiler/rewriter/rules/nodeid_rules.cpp	2013-05-24 20:18:27 +0000
@@ -183,7 +183,6 @@
   }
 
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     flwor_expr* flwor = static_cast<flwor_expr *>(node);
 
@@ -970,7 +969,6 @@
     break;
   }
 
-  case gflwor_expr_kind:
   case flwor_expr_kind:
   case if_expr_kind:
   case trycatch_expr_kind:
@@ -1356,7 +1354,6 @@
     return;
   }
 
-  case gflwor_expr_kind:
   case flwor_expr_kind:
   {
     flwor_expr* e = static_cast<flwor_expr *>(node);

=== modified file 'src/compiler/rewriter/tools/dataflow_annotations.cpp'
--- src/compiler/rewriter/tools/dataflow_annotations.cpp	2013-05-23 02:27:38 +0000
+++ src/compiler/rewriter/tools/dataflow_annotations.cpp	2013-05-24 20:18:27 +0000
@@ -156,7 +156,6 @@
     compute_var_expr(static_cast<var_expr *>(e));
     break;
 
-  case gflwor_expr_kind:
   case flwor_expr_kind:
     compute_flwor_expr(static_cast<flwor_expr *>(e));
     break;
@@ -1005,7 +1004,6 @@
     return;
   }
 
-  case gflwor_expr_kind:
   case flwor_expr_kind:
   {
     flwor_expr* e = static_cast<flwor_expr *>(node);
@@ -1345,7 +1343,6 @@
     return;
   }
 
-  case gflwor_expr_kind:
   case flwor_expr_kind:
   {
     flwor_expr* e = static_cast<flwor_expr *>(node);

=== modified file 'src/compiler/rewriter/tools/expr_tools.cpp'
--- src/compiler/rewriter/tools/expr_tools.cpp	2013-05-09 18:50:15 +0000
+++ src/compiler/rewriter/tools/expr_tools.cpp	2013-05-24 20:18:27 +0000
@@ -476,7 +476,6 @@
   }
 
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     flwor_expr* qe = static_cast<flwor_expr*>(query);
     flwor_expr* ve = static_cast<flwor_expr*>(view);
@@ -962,8 +961,7 @@
     VarIdMap& varidmap,
     IdVarMap* idvarmap)
 {
-  if (e->get_expr_kind() == flwor_expr_kind ||
-      e->get_expr_kind() == gflwor_expr_kind)
+  if (e->get_expr_kind() == flwor_expr_kind)
   {
     const flwor_expr* flwor = static_cast<const flwor_expr *>(e);
 
@@ -1211,8 +1209,7 @@
   // A flwor does not depend on the vars that are defined inside the flwor itself,
   // so remove these vars from the freeset of the flwor, if they have been added
   // there.
-  if (e->get_expr_kind() == flwor_expr_kind ||
-      e->get_expr_kind() == gflwor_expr_kind)
+  if (e->get_expr_kind() == flwor_expr_kind)
   {
     flwor_expr* flwor = static_cast<flwor_expr *>(e);
 

=== modified file 'src/compiler/translator/translator.cpp'
--- src/compiler/translator/translator.cpp	2013-05-15 23:22:01 +0000
+++ src/compiler/translator/translator.cpp	2013-05-24 20:18:27 +0000
@@ -1637,7 +1637,7 @@
   push_scope();
 
   // for $fi in argExpr
-  flwor_expr* coersionFlwor = CREATE(flwor)(theRootSctx, theUDF, loc, false);
+  flwor_expr* coersionFlwor = CREATE(flwor)(theRootSctx, theUDF, loc);
   for_clause* fiClause = wrap_in_forclause(argExpr, NULL);
   var_expr* fiVar = fiClause->get_var();
   coersionFlwor->add_clause(fiClause);
@@ -1910,8 +1910,7 @@
     var_expr* lv,
     expr* retExpr)
 {
-  flwor_expr* fe = theExprManager->
-  create_flwor_expr(theRootSctx, theUDF, lv->get_loc(), false);
+  flwor_expr* fe = CREATE(flwor)(theRootSctx, theUDF, lv->get_loc());
 
   fe->add_clause(wrap_in_letclause(domExpr, lv));
 
@@ -1943,8 +1942,7 @@
 
   push_scope();
 
-  flwor_expr* flworExpr = theExprManager->
-  create_flwor_expr(theRootSctx, theUDF, loc, false);
+  flwor_expr* flworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
 
   if (withContextSize)
   {
@@ -4497,8 +4495,7 @@
 
   if (v.size() > 0)
   {
-    flwor_expr* flwor = theExprManager->
-    create_flwor_expr(theRootSctx, theUDF, loc, false);
+    flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc);
 
     push_nodestack(flwor);
   }
@@ -5569,8 +5566,7 @@
     let_clause* lc = theExprManager->
     create_let_clause(theRootSctx, loc, varExpr, collExpr);
 
-    flwor_expr* flworExpr = theExprManager->
-    create_flwor_expr(theRootSctx, theUDF, loc, false);
+    flwor_expr* flworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
 
     flworExpr->add_clause(lc);
     // flworExpr-> return clause to be set in end_visitor
@@ -5650,8 +5646,7 @@
     // every is implemented as a flowr expr
     push_scope();
 
-    flwor_expr* evFlworExpr = theExprManager->
-    create_flwor_expr(theRootSctx, theUDF, loc, false);
+    flwor_expr* evFlworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
 
     evFlworExpr->set_return_expr(CREATE(const)(theRootSctx, theUDF, loc, true));
 
@@ -5671,7 +5666,7 @@
     let_clause* letClause = theExprManager->
     create_let_clause(theRootSctx, loc, varExpr, collExpr);
 
-    flwor_expr* flworExpr = CREATE(flwor)(theRootSctx, theUDF, loc, false);
+    flwor_expr* flworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
 
     flworExpr->add_clause(letClause);
     // flworExpr->set_return_expr( andExpr ); done in end_visit
@@ -5734,13 +5729,10 @@
     // every $x_ in $x satisfies exists ...
     // every is implemented as a flowr expr
     //push_scope();
-    flwor_expr* evFlworExpr = theExprManager->
-    create_flwor_expr(theRootSctx, theUDF, loc, false);
-
-    evFlworExpr->set_return_expr(theExprManager->create_const_expr(theRootSctx,
-                                                                   theUDF,
-                                                                   loc,
-                                                                   true));
+    flwor_expr* evFlworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
+
+    evFlworExpr->set_return_expr(CREATE(const)(theRootSctx, theUDF, loc, true));
+
     // $x
     const QName* varQName = ic.getCollVarName();
 
@@ -5748,9 +5740,7 @@
     var_expr* evVarExpr = bind_var(loc, varQName, var_expr::for_var, NULL);
 
     // maybe make one more collExpr?
-    evFlworExpr->add_clause(wrap_in_forclause(collExpr,
-                                              evVarExpr,
-                                              NULL));
+    evFlworExpr->add_clause(wrap_in_forclause(collExpr, evVarExpr, NULL));
 
     //pop_scope();
     // end every
@@ -5816,8 +5806,7 @@
     // some $y in dc:collection( xs:QName("org:employees") )
     // satisfies ... eq ...
     // implemented using flowr
-    flwor_expr* someFlworExpr = theExprManager->
-    create_flwor_expr(theRootSctx, theUDF, loc, false);
+    flwor_expr* someFlworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
 
     someFlworExpr->set_return_expr(theExprManager->create_const_expr(theRootSctx,
                                                                      theUDF,
@@ -5869,8 +5858,7 @@
     // every $x in dc:collection( xs:QName("org:transactions") )
     // satisfies ...
     // implemented using flowr
-    flwor_expr* evFlworExpr = theExprManager->
-      create_flwor_expr(theRootSctx, theUDF, loc, false);
+    flwor_expr* evFlworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
 
     evFlworExpr->set_return_expr(theExprManager->create_const_expr(theRootSctx,
                                                                    theUDF,
@@ -6885,8 +6873,7 @@
     ERROR_PARAMS(ZED(XPST0003_XQueryVersionAtLeast30_2), theSctx->xquery_version()));
   }
 
-  flwor_expr* flwor = theExprManager->
-  create_flwor_expr(theRootSctx, theUDF, loc, v.is_general());
+  flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc);
 
   expr* retExpr = pop_nodestack();
 
@@ -8585,9 +8572,9 @@
 {
   TRACE_VISIT();
 
-  flwor_expr* flwor(theExprManager->create_flwor_expr(theRootSctx, theUDF, loc, false));
+  flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc);
 
-  flwor->set_return_expr(theExprManager->create_const_expr(theRootSctx, theUDF, loc, true));
+  flwor->set_return_expr(CREATE(const)(theRootSctx, theUDF, loc, true));
 
   push_nodestack(flwor);
 
@@ -11736,7 +11723,7 @@
     }
 
     // create a flwor with LETs to hold the parameters
-    flwor_expr* flworExpr = CREATE(flwor)(theRootSctx, theUDF, loc, false);
+    flwor_expr* flworExpr = CREATE(flwor)(theRootSctx, theUDF, loc);
     
     // wrap function's QName
     expr* qnameExpr = wrap_in_type_promotion(arguments[0],
@@ -12196,7 +12183,7 @@
     
     arguments[0] = normalize_fo_arg(0, arguments[0], f, loc);
 
-    flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc, false);
+    flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc);
     for_clause* seq_fc = wrap_in_forclause(arguments[1], false);
     flwor->add_clause(seq_fc);
     
@@ -12225,7 +12212,7 @@
     
     arguments[0] = normalize_fo_arg(0, arguments[0], f, loc);
 
-    flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc, false);
+    flwor_expr* flwor = CREATE(flwor)(theRootSctx, theUDF, loc);
     for_clause* seq_fc = wrap_in_forclause(arguments[1], true);
     flwor->add_clause(seq_fc);
     
@@ -12794,7 +12781,7 @@
   }
   else
   {
-    flwor = CREATE(flwor)(theRootSctx, theUDF, loc, false);
+    flwor = CREATE(flwor)(theRootSctx, theUDF, loc);
   }
 
   // Handle inscope variables.

=== modified file 'src/compiler/xqddf/value_index.cpp'
--- src/compiler/xqddf/value_index.cpp	2013-02-07 17:24:36 +0000
+++ src/compiler/xqddf/value_index.cpp	2013-05-24 20:18:27 +0000
@@ -430,7 +430,6 @@
     break;
   }
   case flwor_expr_kind:
-  case gflwor_expr_kind:
   {
     static_cast<const flwor_expr*>(e)->get_vars(varExprs);
 
@@ -522,7 +521,7 @@
 
   expr* returnExpr = theCCB->theEM->create_wrapper_expr(sctx, udf, domloc, newdot);
 
-  flwor_expr* flworExpr = theCCB->theEM->create_flwor_expr(sctx, udf, domloc, false);
+  flwor_expr* flworExpr = theCCB->theEM->create_flwor_expr(sctx, udf, domloc);
   flworExpr->set_return_expr(returnExpr);
   flworExpr->add_clause(fc);
 
@@ -699,7 +698,7 @@
 
   fo_expr* returnExpr =  theCCB->theEM->create_fo_expr(sctx, udf, loc, f, clonedExprs);
 
-  flwor_expr* flworExpr = theCCB->theEM->create_flwor_expr(sctx, udf, loc, false);
+  flwor_expr* flworExpr = theCCB->theEM->create_flwor_expr(sctx, udf, loc);
   flworExpr->set_return_expr(returnExpr);
   flworExpr->add_clause(fc);
 
@@ -849,7 +848,7 @@
 
   fo_expr* returnExpr =  theCCB->theEM->create_fo_expr(sctx, udf, loc, f, clonedExprs);
 
-  flwor_expr* flworExpr = theCCB->theEM->create_flwor_expr(sctx, udf, loc, false);
+  flwor_expr* flworExpr = theCCB->theEM->create_flwor_expr(sctx, udf, loc);
   flworExpr->set_return_expr(returnExpr);
   flworExpr->add_clause(fc);
 

=== modified file 'test/rbkt/ExpCompilerResults/IterPlan/zorba/xray/ppm_10.iter'
--- test/rbkt/ExpCompilerResults/IterPlan/zorba/xray/ppm_10.iter	2013-03-04 21:00:58 +0000
+++ test/rbkt/ExpCompilerResults/IterPlan/zorba/xray/ppm_10.iter	2013-05-24 20:18:27 +0000
@@ -1319,95 +1319,232 @@
 </flwor::FLWORIterator>
 
 Iterator tree for shapes:intersect-plane:
-<flwor::FLWORIterator>
-  <ForVariable name="plane">
-    <LetVarIterator varname="plane"/>
-  </ForVariable>
-  <LetVariable name="normal" materialize="true">
-    <FunctionTraceIterator>
-      <flwor::FLWORIterator>
-        <ForVariable name="a">
-          <TreatIterator quant="">
-            <ChildAxisIterator test kind="match_name_test" qname="xs:QName(,,normal)" typename="*" nill allowed="0">
+<flwor::TupleStreamIterator>
+  <flwor::WhereIterator>
+    <flwor::ForIterator>
+      <ForVariable name="$$context-item"/>
+      <flwor::WhereIterator>
+        <flwor::ForIterator>
+          <ForVariable name="denom"/>
+          <flwor::LetIterator>
+            <LetVariable name="normal" materialize="true"/>
+            <flwor::ForIterator>
+              <ForVariable name="plane"/>
+              <flwor::TupleSourceIterator/>
+              <LetVarIterator varname="plane"/>
+            </flwor::ForIterator>
+            <FunctionTraceIterator>
+              <flwor::FLWORIterator>
+                <ForVariable name="a">
+                  <TreatIterator quant="">
+                    <ChildAxisIterator test kind="match_name_test" qname="xs:QName(,,normal)" typename="*" nill allowed="0">
+                      <ForVarIterator varname="plane"/>
+                    </ChildAxisIterator>
+                  </TreatIterator>
+                </ForVariable>
+                <ReturnClause>
+                  <FnConcatIterator>
+                    <CastIterator type="xs:double">
+                      <FnDataIterator>
+                        <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,x)" typename="*" nill allowed="0">
+                          <ForVarIterator varname="a"/>
+                        </AttributeAxisIterator>
+                      </FnDataIterator>
+                    </CastIterator>
+                    <CastIterator type="xs:double">
+                      <FnDataIterator>
+                        <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,y)" typename="*" nill allowed="0">
+                          <ForVarIterator varname="a"/>
+                        </AttributeAxisIterator>
+                      </FnDataIterator>
+                    </CastIterator>
+                    <CastIterator type="xs:double">
+                      <FnDataIterator>
+                        <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,z)" typename="*" nill allowed="0">
+                          <ForVarIterator varname="a"/>
+                        </AttributeAxisIterator>
+                      </FnDataIterator>
+                    </CastIterator>
+                  </FnConcatIterator>
+                </ReturnClause>
+              </flwor::FLWORIterator>
+            </FunctionTraceIterator>
+          </flwor::LetIterator>
+          <FunctionTraceIterator>
+            <FnSumDoubleIterator>
+              <flwor::FLWORIterator>
+                <ForVariable name="x">
+                  <LetVarIterator varname="direction"/>
+                </ForVariable>
+                <ReturnClause>
+                  <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+                    <ForVarIterator varname="x"/>
+                    <LetVarIterator varname="normal">
+                      <ForVarIterator varname="p"/>
+                    </LetVarIterator>
+                  </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+                </ReturnClause>
+              </flwor::FLWORIterator>
+            </FnSumDoubleIterator>
+          </FunctionTraceIterator>
+        </flwor::ForIterator>
+        <TypedValueCompareIterator_DOUBLE>
+          <ForVarIterator varname="denom"/>
+          <SingletonIterator value="xs:double(0)"/>
+        </TypedValueCompareIterator_DOUBLE>
+      </flwor::WhereIterator>
+      <SpecificNumArithIterator_SubtractOperation_DOUBLE>
+        <CastIterator type="xs:double">
+          <FnDataIterator>
+            <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,offset)" typename="*" nill allowed="0">
               <ForVarIterator varname="plane"/>
-            </ChildAxisIterator>
-          </TreatIterator>
-        </ForVariable>
-        <ReturnClause>
-          <FnConcatIterator>
-            <CastIterator type="xs:double">
-              <FnDataIterator>
-                <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,x)" typename="*" nill allowed="0">
-                  <ForVarIterator varname="a"/>
-                </AttributeAxisIterator>
-              </FnDataIterator>
-            </CastIterator>
-            <CastIterator type="xs:double">
-              <FnDataIterator>
-                <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,y)" typename="*" nill allowed="0">
-                  <ForVarIterator varname="a"/>
-                </AttributeAxisIterator>
-              </FnDataIterator>
-            </CastIterator>
-            <CastIterator type="xs:double">
-              <FnDataIterator>
-                <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,z)" typename="*" nill allowed="0">
-                  <ForVarIterator varname="a"/>
-                </AttributeAxisIterator>
-              </FnDataIterator>
-            </CastIterator>
-          </FnConcatIterator>
-        </ReturnClause>
-      </flwor::FLWORIterator>
-    </FunctionTraceIterator>
-  </LetVariable>
-  <ForVariable name="denom">
-    <FunctionTraceIterator>
-      <FnSumDoubleIterator>
-        <flwor::FLWORIterator>
-          <ForVariable name="x">
-            <LetVarIterator varname="direction"/>
-          </ForVariable>
-          <ReturnClause>
-            <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-              <ForVarIterator varname="x"/>
-              <LetVarIterator varname="normal">
-                <ForVarIterator varname="p"/>
-              </LetVarIterator>
-            </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-          </ReturnClause>
-        </flwor::FLWORIterator>
-      </FnSumDoubleIterator>
-    </FunctionTraceIterator>
-  </ForVariable>
-  <WhereClause>
+            </AttributeAxisIterator>
+          </FnDataIterator>
+        </CastIterator>
+        <SpecificNumArithIterator_DivideOperation_DOUBLE>
+          <FunctionTraceIterator>
+            <FnSumDoubleIterator>
+              <flwor::FLWORIterator>
+                <ForVariable name="x">
+                  <LetVarIterator varname="start"/>
+                </ForVariable>
+                <ReturnClause>
+                  <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+                    <ForVarIterator varname="x"/>
+                    <LetVarIterator varname="normal">
+                      <ForVarIterator varname="p"/>
+                    </LetVarIterator>
+                  </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+                </ReturnClause>
+              </flwor::FLWORIterator>
+            </FnSumDoubleIterator>
+          </FunctionTraceIterator>
+          <ForVarIterator varname="denom"/>
+        </SpecificNumArithIterator_DivideOperation_DOUBLE>
+      </SpecificNumArithIterator_SubtractOperation_DOUBLE>
+    </flwor::ForIterator>
     <TypedValueCompareIterator_DOUBLE>
-      <ForVarIterator varname="denom"/>
+      <ForVarIterator varname="$$context-item"/>
       <SingletonIterator value="xs:double(0)"/>
     </TypedValueCompareIterator_DOUBLE>
-  </WhereClause>
-  <ReturnClause>
-    <flwor::FLWORIterator>
-      <ForVariable name="$$context-item">
+  </flwor::WhereIterator>
+  <ForVarIterator varname="$$context-item"/>
+</flwor::TupleStreamIterator>
+
+Iterator tree for shapes:intersect-sphere:
+<flwor::TupleStreamIterator>
+  <flwor::LetIterator>
+    <LetVariable name="sqrt" materialize="true"/>
+    <flwor::WhereIterator>
+      <flwor::LetIterator>
+        <LetVariable name="descriminant" materialize="true"/>
+        <flwor::ForIterator>
+          <ForVariable name="beta"/>
+          <flwor::LetIterator>
+            <LetVariable name="y" materialize="true"/>
+            <flwor::LetIterator>
+              <LetVariable name="radius" materialize="true"/>
+              <flwor::ForIterator>
+                <ForVariable name="sphere"/>
+                <flwor::TupleSourceIterator/>
+                <LetVarIterator varname="sphere"/>
+              </flwor::ForIterator>
+              <CastIterator type="xs:double">
+                <FnDataIterator>
+                  <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,radius)" typename="*" nill allowed="0">
+                    <ForVarIterator varname="sphere"/>
+                  </AttributeAxisIterator>
+                </FnDataIterator>
+              </CastIterator>
+            </flwor::LetIterator>
+            <FunctionTraceIterator>
+              <flwor::FLWORIterator>
+                <LetVariable name="v2" materialize="true">
+                  <FunctionTraceIterator>
+                    <flwor::FLWORIterator>
+                      <ForVariable name="a">
+                        <TreatIterator quant="">
+                          <ChildAxisIterator test kind="match_name_test" qname="xs:QName(,,center)" typename="*" nill allowed="0">
+                            <ForVarIterator varname="sphere"/>
+                          </ChildAxisIterator>
+                        </TreatIterator>
+                      </ForVariable>
+                      <ReturnClause>
+                        <FnConcatIterator>
+                          <CastIterator type="xs:double">
+                            <FnDataIterator>
+                              <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,x)" typename="*" nill allowed="0">
+                                <ForVarIterator varname="a"/>
+                              </AttributeAxisIterator>
+                            </FnDataIterator>
+                          </CastIterator>
+                          <CastIterator type="xs:double">
+                            <FnDataIterator>
+                              <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,y)" typename="*" nill allowed="0">
+                                <ForVarIterator varname="a"/>
+                              </AttributeAxisIterator>
+                            </FnDataIterator>
+                          </CastIterator>
+                          <CastIterator type="xs:double">
+                            <FnDataIterator>
+                              <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,z)" typename="*" nill allowed="0">
+                                <ForVarIterator varname="a"/>
+                              </AttributeAxisIterator>
+                            </FnDataIterator>
+                          </CastIterator>
+                        </FnConcatIterator>
+                      </ReturnClause>
+                    </flwor::FLWORIterator>
+                  </FunctionTraceIterator>
+                </LetVariable>
+                <ForVariable name="x">
+                  <LetVarIterator varname="start"/>
+                </ForVariable>
+                <ReturnClause>
+                  <SpecificNumArithIterator_SubtractOperation_DOUBLE>
+                    <ForVarIterator varname="x"/>
+                    <LetVarIterator varname="v2">
+                      <ForVarIterator varname="p"/>
+                    </LetVarIterator>
+                  </SpecificNumArithIterator_SubtractOperation_DOUBLE>
+                </ReturnClause>
+              </flwor::FLWORIterator>
+            </FunctionTraceIterator>
+          </flwor::LetIterator>
+          <FunctionTraceIterator>
+            <FnSumDoubleIterator>
+              <flwor::FLWORIterator>
+                <ForVariable name="x">
+                  <LetVarIterator varname="direction"/>
+                </ForVariable>
+                <ReturnClause>
+                  <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+                    <ForVarIterator varname="x"/>
+                    <LetVarIterator varname="y">
+                      <ForVarIterator varname="p"/>
+                    </LetVarIterator>
+                  </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+                </ReturnClause>
+              </flwor::FLWORIterator>
+            </FnSumDoubleIterator>
+          </FunctionTraceIterator>
+        </flwor::ForIterator>
         <SpecificNumArithIterator_SubtractOperation_DOUBLE>
-          <CastIterator type="xs:double">
-            <FnDataIterator>
-              <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,offset)" typename="*" nill allowed="0">
-                <ForVarIterator varname="plane"/>
-              </AttributeAxisIterator>
-            </FnDataIterator>
-          </CastIterator>
-          <SpecificNumArithIterator_DivideOperation_DOUBLE>
+          <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+            <ForVarIterator varname="beta"/>
+            <ForVarIterator varname="beta"/>
+          </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+          <SpecificNumArithIterator_SubtractOperation_DOUBLE>
             <FunctionTraceIterator>
               <FnSumDoubleIterator>
                 <flwor::FLWORIterator>
                   <ForVariable name="x">
-                    <LetVarIterator varname="start"/>
+                    <LetVarIterator varname="y"/>
                   </ForVariable>
                   <ReturnClause>
                     <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
                       <ForVarIterator varname="x"/>
-                      <LetVarIterator varname="normal">
+                      <LetVarIterator varname="y">
                         <ForVarIterator varname="p"/>
                       </LetVarIterator>
                     </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
@@ -1415,9 +1552,39 @@
                 </flwor::FLWORIterator>
               </FnSumDoubleIterator>
             </FunctionTraceIterator>
-            <ForVarIterator varname="denom"/>
-          </SpecificNumArithIterator_DivideOperation_DOUBLE>
+            <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+              <LetVarIterator varname="radius"/>
+              <LetVarIterator varname="radius"/>
+            </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
+          </SpecificNumArithIterator_SubtractOperation_DOUBLE>
         </SpecificNumArithIterator_SubtractOperation_DOUBLE>
+      </flwor::LetIterator>
+      <CompareIterator>
+        <LetVarIterator varname="descriminant"/>
+        <SingletonIterator value="xs:integer(0)"/>
+      </CompareIterator>
+    </flwor::WhereIterator>
+    <SqrtIterator>
+      <LetVarIterator varname="descriminant"/>
+    </SqrtIterator>
+  </flwor::LetIterator>
+  <SequencePointAccessIterator>
+    <flwor::FLWORIterator>
+      <ForVariable name="$$context-item">
+        <FnConcatIterator>
+          <SpecificNumArithIterator_SubtractOperation_DOUBLE>
+            <OpDoubleUnaryIterator>
+              <ForVarIterator varname="beta"/>
+            </OpDoubleUnaryIterator>
+            <LetVarIterator varname="sqrt"/>
+          </SpecificNumArithIterator_SubtractOperation_DOUBLE>
+          <SpecificNumArithIterator_AddOperation_DOUBLE>
+            <OpDoubleUnaryIterator>
+              <ForVarIterator varname="beta"/>
+            </OpDoubleUnaryIterator>
+            <LetVarIterator varname="sqrt"/>
+          </SpecificNumArithIterator_AddOperation_DOUBLE>
+        </FnConcatIterator>
       </ForVariable>
       <WhereClause>
         <TypedValueCompareIterator_DOUBLE>
@@ -1429,176 +1596,9 @@
         <ForVarIterator varname="$$context-item"/>
       </ReturnClause>
     </flwor::FLWORIterator>
-  </ReturnClause>
-</flwor::FLWORIterator>
-
-Iterator tree for shapes:intersect-sphere:
-<flwor::FLWORIterator>
-  <ForVariable name="sphere">
-    <LetVarIterator varname="sphere"/>
-  </ForVariable>
-  <LetVariable name="radius" materialize="true">
-    <CastIterator type="xs:double">
-      <FnDataIterator>
-        <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,radius)" typename="*" nill allowed="0">
-          <ForVarIterator varname="sphere"/>
-        </AttributeAxisIterator>
-      </FnDataIterator>
-    </CastIterator>
-  </LetVariable>
-  <LetVariable name="y" materialize="true">
-    <FunctionTraceIterator>
-      <flwor::FLWORIterator>
-        <LetVariable name="v2" materialize="true">
-          <FunctionTraceIterator>
-            <flwor::FLWORIterator>
-              <ForVariable name="a">
-                <TreatIterator quant="">
-                  <ChildAxisIterator test kind="match_name_test" qname="xs:QName(,,center)" typename="*" nill allowed="0">
-                    <ForVarIterator varname="sphere"/>
-                  </ChildAxisIterator>
-                </TreatIterator>
-              </ForVariable>
-              <ReturnClause>
-                <FnConcatIterator>
-                  <CastIterator type="xs:double">
-                    <FnDataIterator>
-                      <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,x)" typename="*" nill allowed="0">
-                        <ForVarIterator varname="a"/>
-                      </AttributeAxisIterator>
-                    </FnDataIterator>
-                  </CastIterator>
-                  <CastIterator type="xs:double">
-                    <FnDataIterator>
-                      <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,y)" typename="*" nill allowed="0">
-                        <ForVarIterator varname="a"/>
-                      </AttributeAxisIterator>
-                    </FnDataIterator>
-                  </CastIterator>
-                  <CastIterator type="xs:double">
-                    <FnDataIterator>
-                      <AttributeAxisIterator test kind="match_name_test" qname="xs:QName(,,z)" typename="*" nill allowed="0">
-                        <ForVarIterator varname="a"/>
-                      </AttributeAxisIterator>
-                    </FnDataIterator>
-                  </CastIterator>
-                </FnConcatIterator>
-              </ReturnClause>
-            </flwor::FLWORIterator>
-          </FunctionTraceIterator>
-        </LetVariable>
-        <ForVariable name="x">
-          <LetVarIterator varname="start"/>
-        </ForVariable>
-        <ReturnClause>
-          <SpecificNumArithIterator_SubtractOperation_DOUBLE>
-            <ForVarIterator varname="x"/>
-            <LetVarIterator varname="v2">
-              <ForVarIterator varname="p"/>
-            </LetVarIterator>
-          </SpecificNumArithIterator_SubtractOperation_DOUBLE>
-        </ReturnClause>
-      </flwor::FLWORIterator>
-    </FunctionTraceIterator>
-  </LetVariable>
-  <ForVariable name="beta">
-    <FunctionTraceIterator>
-      <FnSumDoubleIterator>
-        <flwor::FLWORIterator>
-          <ForVariable name="x">
-            <LetVarIterator varname="direction"/>
-          </ForVariable>
-          <ReturnClause>
-            <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-              <ForVarIterator varname="x"/>
-              <LetVarIterator varname="y">
-                <ForVarIterator varname="p"/>
-              </LetVarIterator>
-            </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-          </ReturnClause>
-        </flwor::FLWORIterator>
-      </FnSumDoubleIterator>
-    </FunctionTraceIterator>
-  </ForVariable>
-  <LetVariable name="descriminant" materialize="true">
-    <SpecificNumArithIterator_SubtractOperation_DOUBLE>
-      <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-        <ForVarIterator varname="beta"/>
-        <ForVarIterator varname="beta"/>
-      </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-      <SpecificNumArithIterator_SubtractOperation_DOUBLE>
-        <FunctionTraceIterator>
-          <FnSumDoubleIterator>
-            <flwor::FLWORIterator>
-              <ForVariable name="x">
-                <LetVarIterator varname="y"/>
-              </ForVariable>
-              <ReturnClause>
-                <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-                  <ForVarIterator varname="x"/>
-                  <LetVarIterator varname="y">
-                    <ForVarIterator varname="p"/>
-                  </LetVarIterator>
-                </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-              </ReturnClause>
-            </flwor::FLWORIterator>
-          </FnSumDoubleIterator>
-        </FunctionTraceIterator>
-        <SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-          <LetVarIterator varname="radius"/>
-          <LetVarIterator varname="radius"/>
-        </SpecificNumArithIterator_MultiplyOperation_DOUBLE>
-      </SpecificNumArithIterator_SubtractOperation_DOUBLE>
-    </SpecificNumArithIterator_SubtractOperation_DOUBLE>
-  </LetVariable>
-  <WhereClause>
-    <CompareIterator>
-      <LetVarIterator varname="descriminant"/>
-      <SingletonIterator value="xs:integer(0)"/>
-    </CompareIterator>
-  </WhereClause>
-  <ReturnClause>
-    <flwor::FLWORIterator>
-      <LetVariable name="sqrt" materialize="true">
-        <SqrtIterator>
-          <LetVarIterator varname="descriminant"/>
-        </SqrtIterator>
-      </LetVariable>
-      <ReturnClause>
-        <SequencePointAccessIterator>
-          <flwor::FLWORIterator>
-            <ForVariable name="$$context-item">
-              <FnConcatIterator>
-                <SpecificNumArithIterator_SubtractOperation_DOUBLE>
-                  <OpDoubleUnaryIterator>
-                    <ForVarIterator varname="beta"/>
-                  </OpDoubleUnaryIterator>
-                  <LetVarIterator varname="sqrt"/>
-                </SpecificNumArithIterator_SubtractOperation_DOUBLE>
-                <SpecificNumArithIterator_AddOperation_DOUBLE>
-                  <OpDoubleUnaryIterator>
-                    <ForVarIterator varname="beta"/>
-                  </OpDoubleUnaryIterator>
-                  <LetVarIterator varname="sqrt"/>
-                </SpecificNumArithIterator_AddOperation_DOUBLE>
-              </FnConcatIterator>
-            </ForVariable>
-            <WhereClause>
-              <TypedValueCompareIterator_DOUBLE>
-                <ForVarIterator varname="$$context-item"/>
-                <SingletonIterator value="xs:double(0)"/>
-              </TypedValueCompareIterator_DOUBLE>
-            </WhereClause>
-            <ReturnClause>
-              <ForVarIterator varname="$$context-item"/>
-            </ReturnClause>
-          </flwor::FLWORIterator>
-          <SingletonIterator value="xs:integer(1)"/>
-        </SequencePointAccessIterator>
-      </ReturnClause>
-    </flwor::FLWORIterator>
-  </ReturnClause>
-</flwor::FLWORIterator>
+    <SingletonIterator value="xs:integer(1)"/>
+  </SequencePointAccessIterator>
+</flwor::TupleStreamIterator>
 
 Iterator tree for shapes:normal:
 <flwor::FLWORIterator>


Follow ups