Dmitry Lychagin has uploaded this change for review. ( 
https://asterix-gerrit.ics.uci.edu/3352


Change subject: [NO ISSUE][COMP] Allow GROUP BY x.a, y.a
......................................................................

[NO ISSUE][COMP] Allow GROUP BY x.a, y.a

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

Details:
- When generating variable names for GROUP BY key expressions
  avoid names that would conflict with existing variables
  in the same GROUP BY clause. Instead generate internal
  names for those variables.

Change-Id: I7a3720eae211bc5c8c642db7cbe20ead9c40ddae
---
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.1.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.2.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.1.adm
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.2.adm
M asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
M 
asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/GenerateColumnNameVisitor.java
M asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
7 files changed, 125 insertions(+), 10 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/52/3352/1

diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.1.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.1.query.sqlpp
new file mode 100644
index 0000000..f1466e5
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.1.query.sqlpp
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * Description: If two group by expressions would produce the
+ *            : same variable name then the first one wins and
+ *            : the second one gets an internal name.
+ *            : Test case when the first group by expression
+ *            : variable name is automatically generated
+ */
+
+from (
+  from range(1,2) as r1
+  select r1 as x
+) as i, (
+  from range(3,4) as r2
+  select r2 as x
+) as j
+group by i.x, j.x
+select
+  i.x as i_x,
+  j.x as j_x,
+  (i.x = x) as x_is_i_x,
+  count(*) as cnt
+order by x, j.x
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.2.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.2.query.sqlpp
new file mode 100644
index 0000000..9d6e08d
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/core-06/core-06.2.query.sqlpp
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * Description: If two group by expressions would produce the
+ *            : same variable name then the first one wins and
+ *            : the second one gets an internal name.
+ *            : Test case when the first group by expression
+ *            : variable name is specified in the query
+ */
+
+from (
+  from range(1,2) as r1
+  select r1 as x
+) as i, (
+  from range(3,4) as r2
+  select r2 as x
+) as j
+group by i.x * 2 - i.x as x, j.x
+select
+  i.x * 2 - i.x as i_x,
+  j.x as j_x,
+  (i.x * 2 - i.x = x) as x_is_i_x,
+  count(*) as cnt
+order by x, j.x
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.1.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.1.adm
new file mode 100644
index 0000000..4c081d5
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.1.adm
@@ -0,0 +1,4 @@
+{ "i_x": 1, "j_x": 3, "x_is_i_x": true, "cnt": 1 }
+{ "i_x": 1, "j_x": 4, "x_is_i_x": true, "cnt": 1 }
+{ "i_x": 2, "j_x": 3, "x_is_i_x": true, "cnt": 1 }
+{ "i_x": 2, "j_x": 4, "x_is_i_x": true, "cnt": 1 }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.2.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.2.adm
new file mode 100644
index 0000000..4c081d5
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-06/core-06.2.adm
@@ -0,0 +1,4 @@
+{ "i_x": 1, "j_x": 3, "x_is_i_x": true, "cnt": 1 }
+{ "i_x": 1, "j_x": 4, "x_is_i_x": true, "cnt": 1 }
+{ "i_x": 2, "j_x": 3, "x_is_i_x": true, "cnt": 1 }
+{ "i_x": 2, "j_x": 4, "x_is_i_x": true, "cnt": 1 }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml 
b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 5bb25f8..febde3f 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -4765,6 +4765,11 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="core-06">
+        <output-dir compare="Text">core-06</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="sugar-01">
         <output-dir compare="Text">core-01</output-dir>
       </compilation-unit>
diff --git 
a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/GenerateColumnNameVisitor.java
 
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/GenerateColumnNameVisitor.java
index 14a7ec0..6d2a0eb 100644
--- 
a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/GenerateColumnNameVisitor.java
+++ 
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/GenerateColumnNameVisitor.java
@@ -19,20 +19,35 @@

 package org.apache.asterix.lang.sqlpp.rewrites.visitor;

+import java.util.HashSet;
+import java.util.Set;
+
 import org.apache.asterix.common.exceptions.CompilationException;
+import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.base.ILangExpression;
 import org.apache.asterix.lang.common.clause.GroupbyClause;
 import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
+import org.apache.asterix.lang.common.struct.VarIdentifier;
 import org.apache.asterix.lang.sqlpp.clause.Projection;
 import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
+import org.apache.asterix.lang.sqlpp.parser.ParseException;
+import org.apache.asterix.lang.sqlpp.util.ExpressionToVariableUtil;
 import org.apache.asterix.lang.sqlpp.util.SqlppVariableUtil;
 import 
org.apache.asterix.lang.sqlpp.visitor.base.AbstractSqlppExpressionScopingVisitor;
+import org.apache.hyracks.api.exceptions.SourceLocation;

-// Generates implicit column names for projections in a SELECT clause and 
group-by keys.
+/**
+ * <pre>
+ * 1. Generates implicit column names for projections in a SELECT clause,
+ * 2. Generates group by key variables if they were not specified in the query
+ * </pre>
+ */
 public class GenerateColumnNameVisitor extends 
AbstractSqlppExpressionScopingVisitor {
+
+    private final Set<VarIdentifier> gbyKeyVars = new HashSet<>();

     public GenerateColumnNameVisitor(LangRewritingContext context) {
         super(context);
@@ -55,14 +70,25 @@

     @Override
     public Expression visit(GroupbyClause groupbyClause, ILangExpression arg) 
throws CompilationException {
+        gbyKeyVars.clear();
         for (GbyVariableExpressionPair gbyKeyPair : 
groupbyClause.getGbyPairList()) {
             if (gbyKeyPair.getVar() == null) {
-                VariableExpr varExpr = new VariableExpr(context.newVariable());
-                
varExpr.setSourceLocation(gbyKeyPair.getExpr().getSourceLocation());
+                Expression gbyKeyExpr = gbyKeyPair.getExpr();
+                SourceLocation sourceLoc = gbyKeyExpr.getSourceLocation();
+                VariableExpr varExpr;
+                try {
+                    varExpr = 
ExpressionToVariableUtil.getGeneratedVariable(gbyKeyExpr, false);
+                } catch (ParseException e) {
+                    throw new CompilationException(ErrorCode.PARSE_ERROR, e, 
sourceLoc);
+                }
+                if (varExpr == null || gbyKeyVars.contains(varExpr.getVar())) {
+                    varExpr = new VariableExpr(context.newVariable());
+                }
+                varExpr.setSourceLocation(sourceLoc);
                 gbyKeyPair.setVar(varExpr);
             }
+            gbyKeyVars.add(gbyKeyPair.getVar().getVar());
         }
         return super.visit(groupbyClause, arg);
     }
-
 }
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj 
b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
index 765e97b..8f2a839 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
+++ b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
@@ -3416,9 +3416,6 @@
         var = Variable()
         )?
         {
-            if(var==null){
-                var = ExpressionToVariableUtil.getGeneratedVariable(expr, 
false);
-            }
             GbyVariableExpressionPair pair1 = new 
GbyVariableExpressionPair(var, expr);
             vePairList.add(pair1);
         }
@@ -3431,9 +3428,6 @@
          var = Variable()
          )?
          {
-             if(var==null){
-                var = ExpressionToVariableUtil.getGeneratedVariable(expr, 
false);
-             }
              GbyVariableExpressionPair pair2 = new 
GbyVariableExpressionPair(var, expr);
              vePairList.add(pair2);
          }

--
To view, visit https://asterix-gerrit.ics.uci.edu/3352
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I7a3720eae211bc5c8c642db7cbe20ead9c40ddae
Gerrit-Change-Number: 3352
Gerrit-PatchSet: 1
Gerrit-Owner: Dmitry Lychagin <[email protected]>

Reply via email to