ASTERIXDB-1226: implement SQL++ core group-by semantics and syntatic sugars.

-Implmented SQL++ core group-by semantics;
-Implemented SQL++ group-by syntatic sugars for standard SQL;
-Added test cases;
-Fixed column alias rewriter;
-Fixed the variable scoping for joins.

Change-Id: I6e5477d5bf80114cfff49c8ecb163849ee55eba6
Reviewed-on: https://asterix-gerrit.ics.uci.edu/752
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterix-app/src/test/resources/parserts/queries_sqlpp/columnalias.sqlpp b/asterix-app/src/test/resources/parserts/queries_sqlpp/columnalias.sqlpp
index 746660c..72eb1d9 100644
--- a/asterix-app/src/test/resources/parserts/queries_sqlpp/columnalias.sqlpp
+++ b/asterix-app/src/test/resources/parserts/queries_sqlpp/columnalias.sqlpp
@@ -21,4 +21,4 @@
 GROUP BY root
 WITH u AS root
 HAVING root > 0
-ORDER BY u; 
\ No newline at end of file
+ORDER BY u;
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/1.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/1.ast
index 5615495..4cd023d 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/1.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/1.ast
@@ -5,7 +5,7 @@
     LiteralExpr [STRING] [name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
@@ -15,20 +15,20 @@
     LiteralExpr [STRING] [User]
   ]
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ]
 Where
   QuantifiedExpression SOME [
-    [Variable [ Name=i ]
+    [Variable [ Name=$i ]
     In
       FieldAccessor [
-        Variable [ Name=user ]
+        Variable [ Name=$user ]
         Field=interests
       ]
     ]
     Satifies [
       OperatorExpr [
-        Variable [ Name=i ]
+        Variable [ Name=$i ]
         =
         LiteralExpr [STRING] [movies]
       ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/2.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/2.ast
index 7de063f..fbf13fc 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/2.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/2.ast
@@ -4,17 +4,17 @@
   (
     LiteralExpr [STRING] [sig_name]
     :
-    Variable [ Name=sig_name ]
+    Variable [ Name=$sig_name ]
   )
   (
     LiteralExpr [STRING] [total_count]
     :
-    Variable [ Name=sig_sponsorship_count ]
+    Variable [ Name=$sig_sponsorship_count ]
   )
   (
     LiteralExpr [STRING] [chapter_breakdown]
     :
-    Variable [ Name=by_chapter ]
+    Variable [ Name=$by_chapter ]
   )
 ]
 ]
@@ -22,47 +22,64 @@
     LiteralExpr [STRING] [Event]
   ]
   AS
-  Variable [ Name=event ]
+  Variable [ Name=$event ]
 ,
   FieldAccessor [
-    Variable [ Name=event ]
+    Variable [ Name=$event ]
     Field=sponsoring_sigs
   ]
   AS
-  Variable [ Name=sponsor ]
+  Variable [ Name=$sponsor ]
 ]
-LetVariable [ Name=es ]
+Let Variable [ Name=$es ]
   :=
   RecordConstructor [
     (
       LiteralExpr [STRING] [event]
       :
-      Variable [ Name=event ]
+      Variable [ Name=$event ]
     )
     (
       LiteralExpr [STRING] [sponsor]
       :
-      Variable [ Name=sponsor ]
+      Variable [ Name=$sponsor ]
     )
   ]
 Groupby
-  Variable [ Name=sig_name ]
+  Variable [ Name=$sig_name ]
   :=
   FieldAccessor [
-    Variable [ Name=sponsor ]
+    Variable [ Name=$sponsor ]
     Field=sig_name
   ]
-  With
-  Variable [ Name=es ]
-  Variable [ Name=sponsor ]
-  Variable [ Name=event ]
+  GROUP AS
+  Variable [ Name=#1 ]
+  (
+  Variable [ Name=$es ]
+   AS es
+  Variable [ Name=$sponsor ]
+   AS sponsor
+  Variable [ Name=$event ]
+   AS event
+  )
 
-LetVariable [ Name=sig_sponsorship_count ]
+Let Variable [ Name=$sig_sponsorship_count ]
   :=
   FunctionCall null.count@1[
-    Variable [ Name=es ]
+    (
+      SELECT ELEMENT [
+      FieldAccessor [
+        Variable [ Name=#2 ]
+        Field=es
+      ]
+      ]
+      FROM [        Variable [ Name=#1 ]
+        AS
+        Variable [ Name=#2 ]
+      ]
+    )
   ]
-LetVariable [ Name=by_chapter ]
+Let Variable [ Name=$by_chapter ]
   :=
   (
     SELECT ELEMENT [
@@ -70,42 +87,64 @@
       (
         LiteralExpr [STRING] [chapter_name]
         :
-        Variable [ Name=chapter_name ]
+        Variable [ Name=$chapter_name ]
       )
       (
         LiteralExpr [STRING] [escount]
         :
         FunctionCall null.count@1[
-          Variable [ Name=es ]
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              Variable [ Name=#3 ]
+              Field=es
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS
+              Variable [ Name=#3 ]
+            ]
+          )
         ]
       )
     ]
     ]
-    FROM [      Variable [ Name=es ]
+    FROM [      Variable [ Name=$es ]
       AS
-      Variable [ Name=e ]
+      Variable [ Name=$e ]
     ]
     Groupby
-      Variable [ Name=chapter_name ]
+      Variable [ Name=$chapter_name ]
       :=
       FieldAccessor [
         FieldAccessor [
-          Variable [ Name=e ]
+          Variable [ Name=$e ]
           Field=sponsor
         ]
         Field=chapter_name
       ]
-      With
-      Variable [ Name=e ]
-      Variable [ Name=sig_sponsorship_count ]
-      Variable [ Name=sponsor ]
-      Variable [ Name=event ]
-      Variable [ Name=es ]
-      Variable [ Name=sig_name ]
+      GROUP AS
+      Variable [ Name=#4 ]
+      (
+      Variable [ Name=$e ]
+       AS e
+      Variable [ Name=$sig_sponsorship_count ]
+       AS sig_sponsorship_count
+      Variable [ Name=$es ]
+       AS es
+      Variable [ Name=$event ]
+       AS event
+      Variable [ Name=$sponsor ]
+       AS sponsor
+      Variable [ Name=$sig_name ]
+       AS sig_name
+      Variable [ Name=#1 ]
+       AS #1
+      )
 
   )
 Orderby
-  Variable [ Name=sig_sponsorship_count ]
+  Variable [ Name=$sig_sponsorship_count ]
   DESC
 
 Limit
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/4.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/4.ast
index e739c37..1aad304 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/4.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/4.ast
@@ -4,7 +4,7 @@
   (
     LiteralExpr [STRING] [similar_sigs]
     :
-    Variable [ Name=similar_sigs ]
+    Variable [ Name=$similar_sigs ]
   )
 ]
 ]
@@ -12,9 +12,9 @@
     LiteralExpr [STRING] [SIGroup]
   ]
   AS
-  Variable [ Name=sig ]
+  Variable [ Name=$sig ]
 ]
-LetVariable [ Name=similar_sigs ]
+Let Variable [ Name=$similar_sigs ]
   :=
   (
     SELECT ELEMENT [
@@ -23,7 +23,7 @@
         LiteralExpr [STRING] [sig_name]
         :
         FieldAccessor [
-          Variable [ Name=similar_sig ]
+          Variable [ Name=$similar_sig ]
           Field=name
         ]
       )
@@ -33,24 +33,24 @@
         LiteralExpr [STRING] [SIGroup]
       ]
       AS
-      Variable [ Name=similar_sig ]
+      Variable [ Name=$similar_sig ]
     ]
     Where
       OperatorExpr [
         OperatorExpr [
-          Variable [ Name=similar_sig ]
+          Variable [ Name=$similar_sig ]
           !=
-          Variable [ Name=sig ]
+          Variable [ Name=$sig ]
         ]
         and
         OperatorExpr [
           FieldAccessor [
-            Variable [ Name=similar_sig ]
+            Variable [ Name=$similar_sig ]
             Field=interests
           ]
           ~=
           FieldAccessor [
-            Variable [ Name=sig ]
+            Variable [ Name=$sig ]
             Field=interests
           ]
         ]
@@ -59,7 +59,7 @@
 Where
   OperatorExpr [
     FieldAccessor [
-      Variable [ Name=sig ]
+      Variable [ Name=$sig ]
       Field=name
     ]
     =
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/5.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/5.ast
index 73f0cd6..dfb6f09 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/5.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/5.ast
@@ -4,7 +4,7 @@
   (
     LiteralExpr [STRING] [collocated_evnets]
     :
-    Variable [ Name=collocated_events ]
+    Variable [ Name=$collocated_events ]
   )
 ]
 ]
@@ -12,9 +12,9 @@
     LiteralExpr [STRING] [Event]
   ]
   AS
-  Variable [ Name=event ]
+  Variable [ Name=$event ]
 ]
-LetVariable [ Name=collocated_events ]
+Let Variable [ Name=$collocated_events ]
   :=
   (
     SELECT ELEMENT [
@@ -23,7 +23,7 @@
         LiteralExpr [STRING] [event_name]
         :
         FieldAccessor [
-          Variable [ Name=collocated_event ]
+          Variable [ Name=$collocated_event ]
           Field=name
         ]
       )
@@ -33,14 +33,14 @@
         LiteralExpr [STRING] [Events]
       ]
       AS
-      Variable [ Name=collocated_event ]
+      Variable [ Name=$collocated_event ]
     ]
     Where
       OperatorExpr [
         OperatorExpr [
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=collocated_event ]
+              Variable [ Name=$collocated_event ]
               Field=location
             ]
             Field=street
@@ -48,7 +48,7 @@
           ~=
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=event ]
+              Variable [ Name=$event ]
               Field=location
             ]
             Field=street
@@ -58,7 +58,7 @@
         OperatorExpr [
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=collocated_event ]
+              Variable [ Name=$collocated_event ]
               Field=location
             ]
             Field=city
@@ -66,7 +66,7 @@
           =
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=event ]
+              Variable [ Name=$event ]
               Field=location
             ]
             Field=city
@@ -76,7 +76,7 @@
         OperatorExpr [
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=collocated_event ]
+              Variable [ Name=$collocated_event ]
               Field=location
             ]
             Field=state
@@ -84,7 +84,7 @@
           =
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=event ]
+              Variable [ Name=$event ]
               Field=location
             ]
             Field=state
@@ -94,7 +94,7 @@
         OperatorExpr [
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=collocated_event ]
+              Variable [ Name=$collocated_event ]
               Field=location
             ]
             Field=zip
@@ -102,7 +102,7 @@
           =
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=event ]
+              Variable [ Name=$event ]
               Field=location
             ]
             Field=zip
@@ -113,7 +113,7 @@
 Where
   OperatorExpr [
     FieldAccessor [
-      Variable [ Name=event ]
+      Variable [ Name=$event ]
       Field=name
     ]
     =
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/6.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/6.ast
index dcbd485..effebf2 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/6.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/6.ast
@@ -5,14 +5,14 @@
     LiteralExpr [STRING] [user_name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
   (
     LiteralExpr [STRING] [similar_users]
     :
-    Variable [ Name=similar_users ]
+    Variable [ Name=$similar_users ]
   )
 ]
 ]
@@ -20,9 +20,9 @@
     LiteralExpr [STRING] [Users]
   ]
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ]
-LetVariable [ Name=similar_users ]
+Let Variable [ Name=$similar_users ]
   :=
   (
     SELECT ELEMENT [
@@ -31,14 +31,14 @@
         LiteralExpr [STRING] [user_name]
         :
         FieldAccessor [
-          Variable [ Name=similar_user ]
+          Variable [ Name=$similar_user ]
           Field=name
         ]
       )
       (
         LiteralExpr [STRING] [similarity]
         :
-        Variable [ Name=similarity ]
+        Variable [ Name=$similarity ]
       )
     ]
     ]
@@ -46,36 +46,36 @@
         LiteralExpr [STRING] [Users]
       ]
       AS
-      Variable [ Name=similar_user ]
+      Variable [ Name=$similar_user ]
     ]
-    LetVariable [ Name=similarity ]
+    Let Variable [ Name=$similarity ]
       :=
       FunctionCall null.jaccard_similarity@2[
         FieldAccessor [
-          Variable [ Name=user ]
+          Variable [ Name=$user ]
           Field=interests
         ]
         FieldAccessor [
-          Variable [ Name=similar_user ]
+          Variable [ Name=$similar_user ]
           Field=interests
         ]
       ]
     Where
       OperatorExpr [
         OperatorExpr [
-          Variable [ Name=user ]
+          Variable [ Name=$user ]
           !=
-          Variable [ Name=similar_user ]
+          Variable [ Name=$similar_user ]
         ]
         and
         OperatorExpr [
-          Variable [ Name=similarity ]
+          Variable [ Name=$similarity ]
           >=
           LiteralExpr [DOUBLE] [0.75]
         ]
       ]
     Orderby
-      Variable [ Name=similarity ]
+      Variable [ Name=$similarity ]
       DESC
 
     Limit
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ANYInFieldAccessor.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ANYInFieldAccessor.ast
index 2f60cbc..619350b 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ANYInFieldAccessor.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ANYInFieldAccessor.ast
@@ -5,7 +5,7 @@
     LiteralExpr [STRING] [name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
@@ -13,7 +13,7 @@
     LiteralExpr [STRING] [movie]
     :
     FieldAccessor [
-      Variable [ Name=mv ]
+      Variable [ Name=$mv ]
       Field=movie
     ]
   )
@@ -23,33 +23,33 @@
     LiteralExpr [STRING] [User]
   ]
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ,
   FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [Movie]
   ]
   AS
-  Variable [ Name=mv ]
+  Variable [ Name=$mv ]
 ]
 Where
   QuantifiedExpression SOME [
-    [Variable [ Name=i ]
+    [Variable [ Name=$i ]
     In
       FieldAccessor [
-        Variable [ Name=user ]
+        Variable [ Name=$user ]
         Field=interests
       ]
     ]
     Satifies [
       OperatorExpr [
         FieldAccessor [
-          Variable [ Name=i ]
+          Variable [ Name=$i ]
           Field=movie
         ]
         =
         IndexAccessor [
           FieldAccessor [
-            Variable [ Name=mv ]
+            Variable [ Name=$mv ]
             Field=movie
           ]
           Index: ANY
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/IfInFLOWGR.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/IfInFLOWGR.ast
index 4b9e932..a8b54c1 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/IfInFLOWGR.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/IfInFLOWGR.ast
@@ -3,14 +3,14 @@
 IfExpr [
   Condition:
     OperatorExpr [
-      Variable [ Name=i ]
+      Variable [ Name=$i ]
       >
-      Variable [ Name=j ]
+      Variable [ Name=$j ]
     ]
   Then:
-    Variable [ Name=i ]
+    Variable [ Name=$i ]
   Else:
-    Variable [ Name=j ]
+    Variable [ Name=$j ]
 ]
 ]
 FROM [  OrderedListConstructor [
@@ -20,7 +20,7 @@
     LiteralExpr [LONG] [40]
   ]
   AS
-  Variable [ Name=i ]
+  Variable [ Name=$i ]
 ,
   UnorderedListConstructor [
     LiteralExpr [LONG] [4]
@@ -28,5 +28,5 @@
     LiteralExpr [LONG] [6]
   ]
   AS
-  Variable [ Name=j ]
+  Variable [ Name=$j ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/LetFor.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/LetFor.ast
index b9bf49a..a46d1c9 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/LetFor.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/LetFor.ast
@@ -1,5 +1,5 @@
 Query:
-LetVariable [ Name=users ]
+Let Variable [ Name=$users ]
   :=
   FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [User]
@@ -10,28 +10,28 @@
     LiteralExpr [STRING] [name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
 ]
 ]
-FROM [  Variable [ Name=users ]
+FROM [  Variable [ Name=$users ]
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ]
 Where
   QuantifiedExpression SOME [
-    [Variable [ Name=i ]
+    [Variable [ Name=$i ]
     In
       FieldAccessor [
-        Variable [ Name=user ]
+        Variable [ Name=$user ]
         Field=interests
       ]
     ]
     Satifies [
       OperatorExpr [
-        Variable [ Name=i ]
+        Variable [ Name=$i ]
         =
         LiteralExpr [STRING] [movies]
       ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ListConstructor.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ListConstructor.ast
index 3d3a387..f769a0d 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ListConstructor.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/ListConstructor.ast
@@ -1,9 +1,9 @@
 Query:
 SELECT ELEMENT [
 OperatorExpr [
-  Variable [ Name=i ]
+  Variable [ Name=$i ]
   +
-  Variable [ Name=j ]
+  Variable [ Name=$j ]
 ]
 ]
 FROM [  OrderedListConstructor [
@@ -12,7 +12,7 @@
     LiteralExpr [LONG] [3]
   ]
   AS
-  Variable [ Name=i ]
+  Variable [ Name=$i ]
 ,
   UnorderedListConstructor [
     LiteralExpr [LONG] [4]
@@ -20,5 +20,5 @@
     LiteralExpr [LONG] [6]
   ]
   AS
-  Variable [ Name=j ]
+  Variable [ Name=$j ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias.ast
index 26addaa..cb03fe8 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias.ast
@@ -1,77 +1,47 @@
 Query:
 SELECT [
-FunctionCall null.SQRT@1[
-  OperatorExpr [
-    FieldAccessor [
-      Variable [ Name=t ]
-      Field=a
-    ]
-    *
-    FieldAccessor [
-      Variable [ Name=t ]
-      Field=b
-    ]
-  ]
-]
+Variable [ Name=#1 ]
 root
 ]
 FROM [  FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [tbl_name]
   ]
   AS
-  Variable [ Name=t ]
+  Variable [ Name=$t ]
 ]
 Groupby
-  FunctionCall null.SQRT@1[
-    OperatorExpr [
-      FieldAccessor [
-        Variable [ Name=t ]
-        Field=a
-      ]
-      *
-      FieldAccessor [
-        Variable [ Name=t ]
-        Field=b
-      ]
-    ]
-  ]
-  With
-  Variable [ Name=t ]
-
-LetVariable [ Name=u ]
+  Variable [ Name=#1 ]
   :=
   FunctionCall null.SQRT@1[
     OperatorExpr [
       FieldAccessor [
-        Variable [ Name=t ]
+        Variable [ Name=$t ]
         Field=a
       ]
       *
       FieldAccessor [
-        Variable [ Name=t ]
+        Variable [ Name=$t ]
         Field=b
       ]
     ]
   ]
+  GROUP AS
+  Variable [ Name=#2 ]
+  (
+  Variable [ Name=$t ]
+   AS t
+  )
+
+Let Variable [ Name=$u ]
+  :=
+  Variable [ Name=#1 ]
  HAVING
   OperatorExpr [
-    FunctionCall null.SQRT@1[
-      OperatorExpr [
-        FieldAccessor [
-          Variable [ Name=t ]
-          Field=a
-        ]
-        *
-        FieldAccessor [
-          Variable [ Name=t ]
-          Field=b
-        ]
-      ]
-    ]
+    Variable [ Name=#1 ]
     >
     LiteralExpr [LONG] [0]
   ]
 Orderby
-  Variable [ Name=u ]
+  Variable [ Name=$u ]
   ASC
 
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias2.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias2.ast
index 90d32cc..6142478 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias2.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias2.ast
@@ -23,32 +23,38 @@
     LiteralExpr [STRING] [tbl_name]
   ]
   AS
-  Variable [ Name=root ]
+  Variable [ Name=$root ]
 ]
 Groupby
-  FieldAccessor [
-    Variable [ Name=root ]
-    Field=id
-  ]
-  With
-  Variable [ Name=root ]
-
-LetVariable [ Name=u ]
+  Variable [ Name=#1 ]
   :=
   FieldAccessor [
-    Variable [ Name=root ]
+    Variable [ Name=$root ]
+    Field=id
+  ]
+  GROUP AS
+  Variable [ Name=#2 ]
+  (
+  Variable [ Name=$root ]
+   AS root
+  )
+
+Let Variable [ Name=$u ]
+  :=
+  FieldAccessor [
+    Variable [ Name=$root ]
     Field=time
   ]
  HAVING
   OperatorExpr [
     FieldAccessor [
-      Variable [ Name=root ]
+      Variable [ Name=$root ]
       Field=orders
     ]
     >
     LiteralExpr [LONG] [0]
   ]
 Orderby
-  Variable [ Name=u ]
+  Variable [ Name=$u ]
   ASC
 
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias3.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias3.ast
index 6b5693b..a7ae7ca 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias3.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/columnalias3.ast
@@ -4,19 +4,7 @@
   (
     LiteralExpr [STRING] [root]
     :
-    FunctionCall null.SQRT@1[
-      OperatorExpr [
-        FieldAccessor [
-          Variable [ Name=t ]
-          Field=a
-        ]
-        *
-        FieldAccessor [
-          Variable [ Name=t ]
-          Field=b
-        ]
-      ]
-    ]
+    Variable [ Name=#1 ]
   )
 ]
 ]
@@ -24,59 +12,41 @@
     LiteralExpr [STRING] [tbl_name]
   ]
   AS
-  Variable [ Name=t ]
+  Variable [ Name=$t ]
 ]
 Groupby
-  FunctionCall null.SQRT@1[
-    OperatorExpr [
-      FieldAccessor [
-        Variable [ Name=t ]
-        Field=a
-      ]
-      *
-      FieldAccessor [
-        Variable [ Name=t ]
-        Field=b
-      ]
-    ]
-  ]
-  With
-  Variable [ Name=t ]
-
-LetVariable [ Name=u ]
+  Variable [ Name=#1 ]
   :=
   FunctionCall null.SQRT@1[
     OperatorExpr [
       FieldAccessor [
-        Variable [ Name=t ]
+        Variable [ Name=$t ]
         Field=a
       ]
       *
       FieldAccessor [
-        Variable [ Name=t ]
+        Variable [ Name=$t ]
         Field=b
       ]
     ]
   ]
+  GROUP AS
+  Variable [ Name=#2 ]
+  (
+  Variable [ Name=$t ]
+   AS t
+  )
+
+Let Variable [ Name=$u ]
+  :=
+  Variable [ Name=#1 ]
  HAVING
   OperatorExpr [
-    FunctionCall null.SQRT@1[
-      OperatorExpr [
-        FieldAccessor [
-          Variable [ Name=t ]
-          Field=a
-        ]
-        *
-        FieldAccessor [
-          Variable [ Name=t ]
-          Field=b
-        ]
-      ]
-    ]
+    Variable [ Name=#1 ]
     >
     LiteralExpr [LONG] [0]
   ]
 Orderby
-  Variable [ Name=u ]
+  Variable [ Name=$u ]
   ASC
 
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/fieldAccessor.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/fieldAccessor.ast
index 40bc769..0db2e76 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/fieldAccessor.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/fieldAccessor.ast
@@ -1,5 +1,5 @@
 Query:
-LetVariable [ Name=bla ]
+Let Variable [ Name=$bla ]
   :=
   RecordConstructor [
     (
@@ -11,12 +11,12 @@
 SELECT ELEMENT [
 OperatorExpr [
   FieldAccessor [
-    Variable [ Name=bla ]
+    Variable [ Name=$bla ]
     Field=name
   ]
   =
   FieldAccessor [
-    Variable [ Name=bla ]
+    Variable [ Name=$bla ]
     Field=name
   ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_array.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_array.ast
index 192bf6e..43c56e7 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_array.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_array.ast
@@ -1,6 +1,6 @@
 Query:
 SELECT ELEMENT [
-Variable [ Name=x ]
+Variable [ Name=$x ]
 ]
 FROM [  OrderedListConstructor [
     LiteralExpr [LONG] [10]
@@ -8,5 +8,5 @@
     LiteralExpr [LONG] [30]
   ]
   AS
-  Variable [ Name=x ]
+  Variable [ Name=$x ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_bag.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_bag.ast
index e768f7e..79cb01d 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_bag.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_collection_bag.ast
@@ -1,6 +1,6 @@
 Query:
 SELECT ELEMENT [
-Variable [ Name=x ]
+Variable [ Name=$x ]
 ]
 FROM [  UnorderedListConstructor [
     LiteralExpr [LONG] [10]
@@ -8,5 +8,5 @@
     LiteralExpr [LONG] [30]
   ]
   AS
-  Variable [ Name=x ]
+  Variable [ Name=$x ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_correlate.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_correlate.ast
index b354122..701d997 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_correlate.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_correlate.ast
@@ -1,18 +1,18 @@
 DataverseUse test
 Query:
 SELECT ELEMENT [
-Variable [ Name=r ]
+Variable [ Name=$r ]
 ]
 FROM [  FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [sensors]
   ]
   AS
-  Variable [ Name=s ]
+  Variable [ Name=$s ]
   INNER UNNEST
     FieldAccessor [
-      Variable [ Name=s ]
+      Variable [ Name=$s ]
       Field=readings
     ]
      AS
-    Variable [ Name=r ]
+    Variable [ Name=$r ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_flatten.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_flatten.ast
index 7668550..f3c8a42 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_flatten.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_flatten.ast
@@ -2,23 +2,23 @@
 Query:
 SELECT [
 FieldAccessor [
-  Variable [ Name=s ]
+  Variable [ Name=$s ]
   Field=sensor
 ]
 sensor
-Variable [ Name=r ]
+Variable [ Name=$r ]
 reading
 ]
 FROM [  FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [sensors]
   ]
   AS
-  Variable [ Name=s ]
+  Variable [ Name=$s ]
 ,
   FieldAccessor [
-    Variable [ Name=s ]
+    Variable [ Name=$s ]
     Field=readings
   ]
   AS
-  Variable [ Name=r ]
+  Variable [ Name=$r ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_join.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_join.ast
index 0992966..d82b43d 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_join.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_inner_join.ast
@@ -2,17 +2,17 @@
 Query:
 SELECT [
 FieldAccessor [
-  Variable [ Name=m ]
+  Variable [ Name=$m ]
   Field=sensor
 ]
 sensor
 FieldAccessor [
-  Variable [ Name=s ]
+  Variable [ Name=$s ]
   Field=readings
 ]
 readings
 FieldAccessor [
-  Variable [ Name=m ]
+  Variable [ Name=$m ]
   Field=location
 ]
 location
@@ -21,22 +21,22 @@
     LiteralExpr [STRING] [sensors]
   ]
   AS
-  Variable [ Name=s ]
+  Variable [ Name=$s ]
   INNER JOIN
     FunctionCall Metadata.dataset@1[
       LiteralExpr [STRING] [sensorMeta]
     ]
     AS
-    Variable [ Name=m ]
+    Variable [ Name=$m ]
     ON
     OperatorExpr [
       FieldAccessor [
-        Variable [ Name=s ]
+        Variable [ Name=$s ]
         Field=sensor
       ]
       =
       FieldAccessor [
-        Variable [ Name=m ]
+        Variable [ Name=$m ]
         Field=sensor
       ]
     ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate.ast
index 9fd948a..f11ecaf 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate.ast
@@ -2,17 +2,17 @@
 Query:
 SELECT [
 FieldAccessor [
-  Variable [ Name=sl ]
+  Variable [ Name=$sl ]
   Field=location
 ]
 location
 FieldAccessor [
-  Variable [ Name=sr ]
+  Variable [ Name=$sr ]
   Field=gas
 ]
 gas
 FieldAccessor [
-  Variable [ Name=sr ]
+  Variable [ Name=$sr ]
   Field=readings
 ]
 readings
@@ -62,7 +62,7 @@
     ]
   ]
   AS
-  Variable [ Name=sl ]
+  Variable [ Name=$sl ]
   LEFTOUTER UNNEST
     UnorderedListConstructor [
       RecordConstructor [
@@ -141,17 +141,17 @@
       ]
     ]
      AS
-    Variable [ Name=sr ]
+    Variable [ Name=$sr ]
 ]
 Where
   OperatorExpr [
     FieldAccessor [
-      Variable [ Name=sl ]
+      Variable [ Name=$sl ]
       Field=sensor
     ]
     =
     FieldAccessor [
-      Variable [ Name=sr ]
+      Variable [ Name=$sr ]
       Field=sensor
     ]
   ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate2.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate2.ast
index 9ace3b5..6036b55 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate2.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_correlate2.ast
@@ -1,6 +1,6 @@
 Query:
 SELECT ELEMENT [
-Variable [ Name=r ]
+Variable [ Name=$r ]
 ]
 FROM [  UnorderedListConstructor [
     RecordConstructor [
@@ -48,12 +48,12 @@
     ]
   ]
   AS
-  Variable [ Name=s ]
+  Variable [ Name=$s ]
   LEFTOUTER UNNEST
     FieldAccessor [
-      Variable [ Name=s ]
+      Variable [ Name=$s ]
       Field=readings
     ]
      AS
-    Variable [ Name=r ]
+    Variable [ Name=$r ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_outer_join.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_outer_join.ast
index 0f82980..998fc6f 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_outer_join.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_left_outer_join.ast
@@ -2,17 +2,17 @@
 Query:
 SELECT [
 FieldAccessor [
-  Variable [ Name=sl ]
+  Variable [ Name=$sl ]
   Field=location
 ]
 location
 FieldAccessor [
-  Variable [ Name=sr ]
+  Variable [ Name=$sr ]
   Field=gas
 ]
 gas
 FieldAccessor [
-  Variable [ Name=sr ]
+  Variable [ Name=$sr ]
   Field=readings
 ]
 readings
@@ -62,7 +62,7 @@
     ]
   ]
   AS
-  Variable [ Name=sl ]
+  Variable [ Name=$sl ]
   LEFTOUTER JOIN
     UnorderedListConstructor [
       RecordConstructor [
@@ -141,16 +141,16 @@
       ]
     ]
     AS
-    Variable [ Name=sr ]
+    Variable [ Name=$sr ]
     ON
     OperatorExpr [
       FieldAccessor [
-        Variable [ Name=sl ]
+        Variable [ Name=$sl ]
         Field=sensor
       ]
       =
       FieldAccessor [
-        Variable [ Name=sr ]
+        Variable [ Name=$sr ]
         Field=sensor
       ]
     ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_where_select_clause.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_where_select_clause.ast
index bb0c8b5..b978b56 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_where_select_clause.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/from_where_select_clause.ast
@@ -1,6 +1,6 @@
 Query:
 SELECT ELEMENT [
-Variable [ Name=x ]
+Variable [ Name=$x ]
 ]
 FROM [  OrderedListConstructor [
     LiteralExpr [LONG] [10]
@@ -10,11 +10,11 @@
     LiteralExpr [LONG] [10]
   ]
   AS
-  Variable [ Name=x ]
+  Variable [ Name=$x ]
 ]
 Where
   OperatorExpr [
-    Variable [ Name=x ]
+    Variable [ Name=$x ]
     =
     LiteralExpr [LONG] [10]
   ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl.ast
index e025413..90fa1e7 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl.ast
@@ -1,16 +1,16 @@
-FunctionDecl add([a, b]) {
+FunctionDecl add([$a, $b]) {
   OperatorExpr [
-    Variable [ Name=a ]
+    Variable [ Name=$a ]
     +
-    Variable [ Name=b ]
+    Variable [ Name=$b ]
   ]
 }
 
-FunctionDecl minus([a, b]) {
+FunctionDecl minus([$a, $b]) {
   OperatorExpr [
-    Variable [ Name=a ]
+    Variable [ Name=$a ]
     -
-    Variable [ Name=b ]
+    Variable [ Name=$b ]
   ]
 }
 
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl1.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl1.ast
index 36f35dd..5983067 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl1.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl1.ast
@@ -1,63 +1,63 @@
-FunctionDecl calculate([events]) {
+FunctionDecl calculate([$events]) {
   (
     SELECT ELEMENT [
     RecordConstructor [
       (
         LiteralExpr [STRING] [sig_name]
         :
-        Variable [ Name=sig_name ]
+        Variable [ Name=$sig_name ]
       )
       (
         LiteralExpr [STRING] [total_count]
         :
-        Variable [ Name=sig_sponsorship_count ]
+        Variable [ Name=$sig_sponsorship_count ]
       )
       (
         LiteralExpr [STRING] [chapter_breakdown]
         :
-        Variable [ Name=by_chapter ]
+        Variable [ Name=$by_chapter ]
       )
     ]
     ]
-    FROM [      Variable [ Name=events ]
+    FROM [      Variable [ Name=$events ]
       AS
-      Variable [ Name=event ]
+      Variable [ Name=$event ]
 ,
       FieldAccessor [
-        Variable [ Name=event ]
+        Variable [ Name=$event ]
         Field=sponsoring_sigs
       ]
       AS
-      Variable [ Name=sponsor ]
+      Variable [ Name=$sponsor ]
     ]
-    LetVariable [ Name=es ]
+    Let Variable [ Name=$es ]
       :=
       RecordConstructor [
         (
           LiteralExpr [STRING] [event]
           :
-          Variable [ Name=event ]
+          Variable [ Name=$event ]
         )
         (
           LiteralExpr [STRING] [sponsor]
           :
-          Variable [ Name=sponsor ]
+          Variable [ Name=$sponsor ]
         )
       ]
     Groupby
-      Variable [ Name=sig_name ]
+      Variable [ Name=$sig_name ]
       :=
       FieldAccessor [
-        Variable [ Name=sponsor ]
+        Variable [ Name=$sponsor ]
         Field=sig_name
       ]
 
-    LetVariable [ Name=sig_sponsorship_count ]
+    Let Variable [ Name=$sig_sponsorship_count ]
       :=
       FunctionCall null.count@1[
-        Variable [ Name=es ]
+        Variable [ Name=$es ]
       ]
-    LetVariable [ Name=by_chapter ]
+    Let Variable [ Name=$by_chapter ]
       :=
       (
         SELECT ELEMENT [
@@ -65,27 +65,27 @@
           (
             LiteralExpr [STRING] [chapter_name]
             :
-            Variable [ Name=chapter_name ]
+            Variable [ Name=$chapter_name ]
           )
           (
             LiteralExpr [STRING] [escount]
             :
             FunctionCall null.count@1[
-              Variable [ Name=es ]
+              Variable [ Name=$es ]
             ]
           )
         ]
         ]
-        FROM [          Variable [ Name=es ]
+        FROM [          Variable [ Name=$es ]
           AS
-          Variable [ Name=e ]
+          Variable [ Name=$e ]
         ]
         Groupby
-          Variable [ Name=chapter_name ]
+          Variable [ Name=$chapter_name ]
           :=
           FieldAccessor [
             FieldAccessor [
-              Variable [ Name=e ]
+              Variable [ Name=$e ]
               Field=sponsor
             ]
             Field=chapter_name
@@ -93,7 +93,7 @@
 
       )
     Orderby
-      Variable [ Name=sig_sponsorship_count ]
+      Variable [ Name=$sig_sponsorship_count ]
       DESC
 
     Limit
@@ -102,7 +102,7 @@
 }
 
 Query:
-LetVariable [ Name=result ]
+Let Variable [ Name=$result ]
   :=
   FunctionCall null.calculate@1[
     FunctionCall Metadata.dataset@1[
@@ -110,5 +110,5 @@
     ]
   ]
 SELECT ELEMENT [
-Variable [ Name=result ]
+Variable [ Name=$result ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl2.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl2.ast
index 407d545..de37697 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl2.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl2.ast
@@ -1,10 +1,10 @@
-FunctionDecl GT([a, b]) {
+FunctionDecl GT([$a, $b]) {
   IfExpr [
     Condition:
       OperatorExpr [
-        Variable [ Name=a ]
+        Variable [ Name=$a ]
         >
-        Variable [ Name=b ]
+        Variable [ Name=$b ]
       ]
     Then:
       LiteralExpr [TRUE]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl3.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl3.ast
index 487c466..54032ff 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl3.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/functionDecl3.ast
@@ -1,4 +1,4 @@
-FunctionDecl function with spaces([a, b]) {
+FunctionDecl function with spaces([$a, $b]) {
   LiteralExpr [STRING] [string with spaces]
 }
 
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/groupby_clause_count.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/groupby_clause_count.ast
index b413291..feeed6e 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/groupby_clause_count.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/groupby_clause_count.ast
@@ -1,7 +1,18 @@
 Query:
 SELECT ELEMENT [
 FunctionCall null.count@1[
-  Variable [ Name=r ]
+  (
+    SELECT ELEMENT [
+    FieldAccessor [
+      Variable [ Name=#2 ]
+      Field=r
+    ]
+    ]
+    FROM [      Variable [ Name=#1 ]
+      AS
+      Variable [ Name=#2 ]
+    ]
+  )
 ]
 ]
 FROM [  UnorderedListConstructor [
@@ -79,15 +90,19 @@
     ]
   ]
   AS
-  Variable [ Name=r ]
+  Variable [ Name=$r ]
 ]
 Groupby
-  Variable [ Name=g ]
+  Variable [ Name=$g ]
   :=
   FieldAccessor [
-    Variable [ Name=r ]
+    Variable [ Name=$r ]
     Field=gas
   ]
-  With
-  Variable [ Name=r ]
+  GROUP AS
+  Variable [ Name=#1 ]
+  (
+  Variable [ Name=$r ]
+   AS r
+  )
 
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR.ast
index 6e7af3a..da4a74f 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR.ast
@@ -5,37 +5,37 @@
     LiteralExpr [STRING] [name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
 ]
 ]
 FROM [  (
-    LetVariable [ Name=data ]
+    Let Variable [ Name=$data ]
       :=
       FunctionCall Metadata.dataset@1[
         LiteralExpr [STRING] [User]
       ]
     SELECT ELEMENT [
-    Variable [ Name=data ]
+    Variable [ Name=$data ]
     ]
   )
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ]
 Where
   QuantifiedExpression SOME [
-    [Variable [ Name=i ]
+    [Variable [ Name=$i ]
     In
       FieldAccessor [
-        Variable [ Name=user ]
+        Variable [ Name=$user ]
         Field=interests
       ]
     ]
     Satifies [
       OperatorExpr [
-        Variable [ Name=i ]
+        Variable [ Name=$i ]
         =
         LiteralExpr [STRING] [movies]
       ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR1.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR1.ast
index 46d3fb8..ea08249 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR1.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR1.ast
@@ -2,26 +2,26 @@
 SELECT ELEMENT [
 (
   SELECT ELEMENT [
-  Variable [ Name=k ]
+  Variable [ Name=$k ]
   ]
   FROM [    IfExpr [
       Condition:
         OperatorExpr [
-          Variable [ Name=i ]
+          Variable [ Name=$i ]
           >
-          Variable [ Name=j ]
+          Variable [ Name=$j ]
         ]
       Then:
-        Variable [ Name=i ]
+        Variable [ Name=$i ]
       Else:
-        Variable [ Name=j ]
+        Variable [ Name=$j ]
     ]
     AS
-    Variable [ Name=k ]
+    Variable [ Name=$k ]
   ]
   Where
     OperatorExpr [
-      Variable [ Name=k ]
+      Variable [ Name=$k ]
       <
       LiteralExpr [LONG] [10]
     ]
@@ -34,7 +34,7 @@
     LiteralExpr [LONG] [40]
   ]
   AS
-  Variable [ Name=i ]
+  Variable [ Name=$i ]
 ,
   UnorderedListConstructor [
     LiteralExpr [LONG] [4]
@@ -42,5 +42,5 @@
     LiteralExpr [LONG] [6]
   ]
   AS
-  Variable [ Name=j ]
+  Variable [ Name=$j ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR2.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR2.ast
index 9673c91..84e2550 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR2.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR2.ast
@@ -2,26 +2,26 @@
 SELECT ELEMENT [
 (
   SELECT ELEMENT [
-  Variable [ Name=k ]
+  Variable [ Name=$k ]
   ]
   FROM [    IfExpr [
       Condition:
         OperatorExpr [
-          Variable [ Name=i ]
+          Variable [ Name=$i ]
           >
-          Variable [ Name=j ]
+          Variable [ Name=$j ]
         ]
       Then:
-        Variable [ Name=i ]
+        Variable [ Name=$i ]
       Else:
-        Variable [ Name=j ]
+        Variable [ Name=$j ]
     ]
     AS
-    Variable [ Name=k ]
+    Variable [ Name=$k ]
   ]
   Where
     OperatorExpr [
-      Variable [ Name=k ]
+      Variable [ Name=$k ]
       <
       LiteralExpr [LONG] [10]
     ]
@@ -33,18 +33,18 @@
     LiteralExpr [LONG] [30]
     (
       SELECT ELEMENT [
-      Variable [ Name=tmp ]
+      Variable [ Name=$tmp ]
       ]
       FROM [        FunctionCall Metadata.dataset@1[
           LiteralExpr [STRING] [number]
         ]
         AS
-        Variable [ Name=tmp ]
+        Variable [ Name=$tmp ]
       ]
     )
   ]
   AS
-  Variable [ Name=i ]
+  Variable [ Name=$i ]
 ,
   UnorderedListConstructor [
     LiteralExpr [LONG] [4]
@@ -52,5 +52,5 @@
     LiteralExpr [LONG] [6]
   ]
   AS
-  Variable [ Name=j ]
+  Variable [ Name=$j ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR3.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR3.ast
index 4232303..a636e0d 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR3.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFLWOGR3.ast
@@ -4,17 +4,17 @@
   (
     LiteralExpr [STRING] [sig_name]
     :
-    Variable [ Name=sig_name ]
+    Variable [ Name=$sig_name ]
   )
   (
     LiteralExpr [STRING] [total_count]
     :
-    Variable [ Name=sig_sponsorship_count ]
+    Variable [ Name=$sig_sponsorship_count ]
   )
   (
     LiteralExpr [STRING] [chapter_breakdown]
     :
-    Variable [ Name=by_chapter ]
+    Variable [ Name=$by_chapter ]
   )
 ]
 ]
@@ -22,47 +22,64 @@
     LiteralExpr [STRING] [Event]
   ]
   AS
-  Variable [ Name=event ]
+  Variable [ Name=$event ]
 ,
   FieldAccessor [
-    Variable [ Name=event ]
+    Variable [ Name=$event ]
     Field=sponsoring_sigs
   ]
   AS
-  Variable [ Name=sponsor ]
+  Variable [ Name=$sponsor ]
 ]
-LetVariable [ Name=es ]
+Let Variable [ Name=$es ]
   :=
   RecordConstructor [
     (
       LiteralExpr [STRING] [event]
       :
-      Variable [ Name=event ]
+      Variable [ Name=$event ]
     )
     (
       LiteralExpr [STRING] [sponsor]
       :
-      Variable [ Name=sponsor ]
+      Variable [ Name=$sponsor ]
     )
   ]
 Groupby
-  Variable [ Name=sig_name ]
+  Variable [ Name=$sig_name ]
   :=
   FieldAccessor [
-    Variable [ Name=sponsor ]
+    Variable [ Name=$sponsor ]
     Field=sig_name
   ]
-  With
-  Variable [ Name=es ]
-  Variable [ Name=sponsor ]
-  Variable [ Name=event ]
+  GROUP AS
+  Variable [ Name=#1 ]
+  (
+  Variable [ Name=$es ]
+   AS es
+  Variable [ Name=$sponsor ]
+   AS sponsor
+  Variable [ Name=$event ]
+   AS event
+  )
 
-LetVariable [ Name=sig_sponsorship_count ]
+Let Variable [ Name=$sig_sponsorship_count ]
   :=
   FunctionCall null.count@1[
-    Variable [ Name=es ]
+    (
+      SELECT ELEMENT [
+      FieldAccessor [
+        Variable [ Name=#2 ]
+        Field=es
+      ]
+      ]
+      FROM [        Variable [ Name=#1 ]
+        AS
+        Variable [ Name=#2 ]
+      ]
+    )
   ]
-LetVariable [ Name=by_chapter ]
+Let Variable [ Name=$by_chapter ]
   :=
   (
     SELECT ELEMENT [
@@ -70,42 +87,64 @@
       (
         LiteralExpr [STRING] [chapter_name]
         :
-        Variable [ Name=chapter_name ]
+        Variable [ Name=$chapter_name ]
       )
       (
         LiteralExpr [STRING] [escount]
         :
         FunctionCall null.count@1[
-          Variable [ Name=es ]
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              Variable [ Name=#3 ]
+              Field=es
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS
+              Variable [ Name=#3 ]
+            ]
+          )
         ]
       )
     ]
     ]
-    FROM [      Variable [ Name=es ]
+    FROM [      Variable [ Name=$es ]
       AS
-      Variable [ Name=e ]
+      Variable [ Name=$e ]
     ]
     Groupby
-      Variable [ Name=chapter_name ]
+      Variable [ Name=$chapter_name ]
       :=
       FieldAccessor [
         FieldAccessor [
-          Variable [ Name=e ]
+          Variable [ Name=$e ]
           Field=sponsor
         ]
         Field=chapter_name
       ]
-      With
-      Variable [ Name=e ]
-      Variable [ Name=sig_sponsorship_count ]
-      Variable [ Name=sponsor ]
-      Variable [ Name=event ]
-      Variable [ Name=es ]
-      Variable [ Name=sig_name ]
+      GROUP AS
+      Variable [ Name=#4 ]
+      (
+      Variable [ Name=$e ]
+       AS e
+      Variable [ Name=$sig_sponsorship_count ]
+       AS sig_sponsorship_count
+      Variable [ Name=$es ]
+       AS es
+      Variable [ Name=$event ]
+       AS event
+      Variable [ Name=$sponsor ]
+       AS sponsor
+      Variable [ Name=$sig_name ]
+       AS sig_name
+      Variable [ Name=#1 ]
+       AS #1
+      )
 
   )
 Orderby
-  Variable [ Name=sig_sponsorship_count ]
+  Variable [ Name=$sig_sponsorship_count ]
   DESC
 
 Limit
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFor.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFor.ast
index 518d51b..d94617e 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFor.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/nestedFor.ast
@@ -5,7 +5,7 @@
     LiteralExpr [STRING] [name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
@@ -13,7 +13,7 @@
     LiteralExpr [STRING] [movie]
     :
     FieldAccessor [
-      Variable [ Name=mv ]
+      Variable [ Name=$mv ]
       Field=movie
     ]
   )
@@ -23,32 +23,32 @@
     LiteralExpr [STRING] [User]
   ]
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ,
   FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [Movie]
   ]
   AS
-  Variable [ Name=mv ]
+  Variable [ Name=$mv ]
 ]
 Where
   QuantifiedExpression SOME [
-    [Variable [ Name=i ]
+    [Variable [ Name=$i ]
     In
       FieldAccessor [
-        Variable [ Name=user ]
+        Variable [ Name=$user ]
         Field=interests
       ]
     ]
     Satifies [
       OperatorExpr [
         FieldAccessor [
-          Variable [ Name=i ]
+          Variable [ Name=$i ]
           Field=movie
         ]
         =
         FieldAccessor [
-          Variable [ Name=mv ]
+          Variable [ Name=$mv ]
           Field=movie
         ]
       ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/numberInFieldAccessor.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/numberInFieldAccessor.ast
index 6a681a0..c9ad9d6 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/numberInFieldAccessor.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/numberInFieldAccessor.ast
@@ -5,7 +5,7 @@
     LiteralExpr [STRING] [name]
     :
     FieldAccessor [
-      Variable [ Name=user ]
+      Variable [ Name=$user ]
       Field=name
     ]
   )
@@ -13,7 +13,7 @@
     LiteralExpr [STRING] [movie]
     :
     FieldAccessor [
-      Variable [ Name=mv ]
+      Variable [ Name=$mv ]
       Field=movie
     ]
   )
@@ -23,33 +23,33 @@
     LiteralExpr [STRING] [User]
   ]
   AS
-  Variable [ Name=user ]
+  Variable [ Name=$user ]
 ,
   FunctionCall Metadata.dataset@1[
     LiteralExpr [STRING] [Movie]
   ]
   AS
-  Variable [ Name=mv ]
+  Variable [ Name=$mv ]
 ]
 Where
   QuantifiedExpression SOME [
-    [Variable [ Name=i ]
+    [Variable [ Name=$i ]
     In
       FieldAccessor [
-        Variable [ Name=user ]
+        Variable [ Name=$user ]
         Field=interests
       ]
     ]
     Satifies [
       OperatorExpr [
         FieldAccessor [
-          Variable [ Name=i ]
+          Variable [ Name=$i ]
           Field=movie
         ]
         =
         IndexAccessor [
           FieldAccessor [
-            Variable [ Name=mv ]
+            Variable [ Name=$mv ]
             Field=movie
           ]
           Index:           LiteralExpr [LONG] [2]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_clause_sugar.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_clause_sugar.ast
index 6622fe1..3049374 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_clause_sugar.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_clause_sugar.ast
@@ -1,6 +1,6 @@
 Query:
 SELECT [
-Variable [ Name=x ]
+Variable [ Name=$x ]
 value
 ]
 FROM [  OrderedListConstructor [
@@ -11,11 +11,11 @@
     LiteralExpr [LONG] [10]
   ]
   AS
-  Variable [ Name=x ]
+  Variable [ Name=$x ]
 ]
 Where
   OperatorExpr [
-    Variable [ Name=x ]
+    Variable [ Name=$x ]
     =
     LiteralExpr [LONG] [10]
   ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_from_where_sugar.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_from_where_sugar.ast
index bb0c8b5..b978b56 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_from_where_sugar.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/select_from_where_sugar.ast
@@ -1,6 +1,6 @@
 Query:
 SELECT ELEMENT [
-Variable [ Name=x ]
+Variable [ Name=$x ]
 ]
 FROM [  OrderedListConstructor [
     LiteralExpr [LONG] [10]
@@ -10,11 +10,11 @@
     LiteralExpr [LONG] [10]
   ]
   AS
-  Variable [ Name=x ]
+  Variable [ Name=$x ]
 ]
 Where
   OperatorExpr [
-    Variable [ Name=x ]
+    Variable [ Name=$x ]
     =
     LiteralExpr [LONG] [10]
   ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/union.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/union.ast
index ef69119..fdc8591 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/union.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/union.ast
@@ -1,27 +1,27 @@
 Query:
 SELECT ELEMENT [
-Variable [ Name=l ]
+Variable [ Name=$l ]
 ]
 FROM [  FunctionCall null.foo1@0[
   ]
   AS
-  Variable [ Name=l ]
+  Variable [ Name=$l ]
 ]
 UNION ALL 
   SELECT ELEMENT [
-  Variable [ Name=l ]
+  Variable [ Name=$l ]
   ]
   FROM [    FunctionCall null.foo2@0[
     ]
     AS
-    Variable [ Name=l ]
+    Variable [ Name=$l ]
   ]
 UNION ALL 
   SELECT ELEMENT [
-  Variable [ Name=l ]
+  Variable [ Name=$l ]
   ]
   FROM [    FunctionCall null.foo3@0[
     ]
     AS
-    Variable [ Name=l ]
+    Variable [ Name=$l ]
   ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/variables.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/variables.ast
index 330ebef..698f469 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/variables.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/variables.ast
@@ -1,14 +1,14 @@
 Query:
-LetVariable [ Name=a ]
+Let Variable [ Name=$a ]
   :=
   LiteralExpr [LONG] [1]
-LetVariable [ Name=b ]
+Let Variable [ Name=$b ]
   :=
   LiteralExpr [LONG] [1]
 SELECT ELEMENT [
 OperatorExpr [
-  Variable [ Name=b ]
+  Variable [ Name=$b ]
   -
-  Variable [ Name=a ]
+  Variable [ Name=$a ]
 ]
 ]
diff --git a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/where_clause.ast b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/where_clause.ast
index 57c1c2c..7a32a1f 100644
--- a/asterix-app/src/test/resources/parserts/results_parser_sqlpp/where_clause.ast
+++ b/asterix-app/src/test/resources/parserts/results_parser_sqlpp/where_clause.ast
@@ -2,7 +2,7 @@
 Query:
 SELECT ELEMENT [
 FieldAccessor [
-  Variable [ Name=r ]
+  Variable [ Name=$r ]
   Field=reading
 ]
 ]
@@ -10,12 +10,12 @@
     LiteralExpr [STRING] [sensors]
   ]
   AS
-  Variable [ Name=r ]
+  Variable [ Name=$r ]
 ]
 Where
   OperatorExpr [
     FieldAccessor [
-      Variable [ Name=r ]
+      Variable [ Name=$r ]
       Field=reading
     ]
     =