[ASTERIXDB-2539][COMP] Exclude LET variables from "Single Variable Resolution" rule

- user model changes: yes
- storage format changes: no
- interface changes: no

Details:
- Variables introduced by LET clauses should not be
  considered by "Single Variable Resolution" rule
- In INSERT/UPSERT statements with RETURNING expression
  the automatically introduced variable should be named
  as the target dataset
- Refactor VariableCheckAndRewriteVisitor and
  other code related to name resolution
- Added testcases and updated documentation

Change-Id: I9b9f70a1671378f5216ec4e30416d4d67c3df089
Reviewed-on: https://asterix-gerrit.ics.uci.edu/3322
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
index 05b88cc..765e97b 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
+++ b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
@@ -1088,8 +1088,9 @@
     query = Query(false)
     ( <RETURNING> returnExpression = Expression())?
     {
-      if (returnExpression != null && var == null) {
-        var = ExpressionToVariableUtil.getGeneratedVariable(query.getBody(), true);
+      if (var == null && returnExpression != null) {
+        var = new VariableExpr(SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue()));
+        addSourceLocation(var, startToken);
       }
       query.setTopLevel(true);
       InsertStatement stmt = new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(),
@@ -1111,8 +1112,9 @@
     query = Query(false)
     ( <RETURNING> returnExpression = Expression())?
     {
-      if (returnExpression != null && var == null) {
-        var = ExpressionToVariableUtil.getGeneratedVariable(query.getBody(), true);
+      if (var == null && returnExpression != null) {
+          var = new VariableExpr(SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue()));
+          addSourceLocation(var, startToken);
       }
       query.setTopLevel(true);
       UpsertStatement stmt = new UpsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(),
@@ -1124,23 +1126,20 @@
 DeleteStatement DeleteStatement() throws ParseException:
 {
   Token startToken = null;
-  VariableExpr varExpr = null;
+  VariableExpr var = null;
   Expression condition = null;
   Pair<Identifier, Identifier> nameComponents;
 }
 {
   <DELETE> { startToken = token; }
-  <FROM> nameComponents  = QualifiedName()
-         ((<AS>)? varExpr = Variable())?
+  <FROM> nameComponents  = QualifiedName() ((<AS>)? var = Variable())?
   (<WHERE> condition = Expression())?
   {
-      if(varExpr == null){
-        varExpr = new VariableExpr();
-        VarIdentifier var = SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue());
-        varExpr.setVar(var);
-        addSourceLocation(varExpr, startToken);
+      if (var == null) {
+        var = new VariableExpr(SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue()));
+        addSourceLocation(var, startToken);
       }
-      DeleteStatement stmt = new DeleteStatement(varExpr, nameComponents.first, nameComponents.second,
+      DeleteStatement stmt = new DeleteStatement(var, nameComponents.first, nameComponents.second,
           condition, getVarCounter());
       return addSourceLocation(stmt, startToken);
   }