You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by am...@apache.org on 2015/01/21 14:11:29 UTC

incubator-lens git commit: LENS-131 : Fixes groupby resolver not to promote when constants are projected ( Sushil Mohanty via amareshwari)

Repository: incubator-lens
Updated Branches:
  refs/heads/master baac165ba -> 930343b91


LENS-131 : Fixes groupby resolver not to promote when constants are projected ( Sushil Mohanty via amareshwari)


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/930343b9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/930343b9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/930343b9

Branch: refs/heads/master
Commit: 930343b912583df898e04fd735f4a217398c02fe
Parents: baac165
Author: Amareshwari Sriramdasu <am...@inmobi.com>
Authored: Wed Jan 21 18:41:11 2015 +0530
Committer: Amareshwari Sriramdasu <am...@inmobi.com>
Committed: Wed Jan 21 18:41:11 2015 +0530

----------------------------------------------------------------------
 .../apache/lens/cube/parse/GroupbyResolver.java | 60 ++++++++++++++++----
 .../lens/cube/parse/TestCubeRewriter.java       | 37 ++++++++++++
 2 files changed, 86 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/930343b9/lens-cube/src/main/java/org/apache/lens/cube/parse/GroupbyResolver.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/GroupbyResolver.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/GroupbyResolver.java
index 2531c49..1021c96 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/GroupbyResolver.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/GroupbyResolver.java
@@ -34,6 +34,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
 import org.apache.hadoop.hive.ql.parse.ParseException;
 import org.apache.hadoop.hive.ql.parse.SemanticException;
 
@@ -55,8 +56,8 @@ class GroupbyResolver implements ContextRewriter {
             CubeQueryConfUtil.DEFAULT_ENABLE_GROUP_BY_TO_SELECT);
   }
 
-  private void promoteSelect(CubeQueryContext cubeql, List<String> nonMsrNonAggSelExprsWithoutAlias, List<String> groupByExprs)
-      throws SemanticException {
+  private void promoteSelect(CubeQueryContext cubeql, List<String> nonMsrNonAggSelExprsWithoutAlias,
+      List<String> groupByExprs) throws SemanticException {
     if (!selectPromotionEnabled) {
       return;
     }
@@ -80,15 +81,17 @@ class GroupbyResolver implements ContextRewriter {
               throw new SemanticException(e);
             }
             ASTNode groupbyAST = cubeql.getGroupByAST();
-            if (groupbyAST != null) {
-              // groupby ast exists, add the expression to AST
-              groupbyAST.addChild(exprAST);
-              exprAST.setParent(groupbyAST);
-            } else {
-              // no group by ast exist, create one
-              ASTNode newAST = new ASTNode(new CommonToken(TOK_GROUPBY));
-              newAST.addChild(exprAST);
-              cubeql.setGroupByAST(newAST);
+            if (!isConstantsUsed(exprAST)) {
+              if (groupbyAST != null) {
+                // groupby ast exists, add the expression to AST
+                groupbyAST.addChild(exprAST);
+                exprAST.setParent(groupbyAST);
+              } else {
+                // no group by ast exist, create one
+                ASTNode newAST = new ASTNode(new CommonToken(TOK_GROUPBY));
+                newAST.addChild(exprAST);
+                cubeql.setGroupByAST(newAST);
+              }
             }
           }
         }
@@ -96,6 +99,41 @@ class GroupbyResolver implements ContextRewriter {
     }
   }
 
+  /*
+   * Check if constants projected
+   */
+  private boolean isConstantsUsed(ASTNode node) {
+    if (node == null) {
+      return false;
+    }
+    if (node.getToken() != null) {
+      if (hasTableOrColumn(node)) {
+        return false;
+      } else {
+        return true;
+      }
+    } else {
+      return false;
+    }
+  }
+
+
+  /*
+   * Check if table or column used in node
+   */
+  private boolean hasTableOrColumn(ASTNode node) {
+    if (node.getToken() != null) {
+      if (node.getToken().getType() == HiveParser.TOK_TABLE_OR_COL) {
+        return true;
+      }
+    }
+    for (int i = 0; i < node.getChildCount(); i++) {
+      if (hasTableOrColumn((ASTNode) node.getChild(i)))
+        return true;
+    }
+    return false;
+  }
+
   private void promoteGroupby(CubeQueryContext cubeql, List<String> selectExprs, List<String> groupByExprs)
       throws SemanticException {
     if (!groupbyPromotionEnabled) {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/930343b9/lens-cube/src/test/java/org/apache/lens/cube/parse/TestCubeRewriter.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestCubeRewriter.java b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestCubeRewriter.java
index cb70b66..65cbd14 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestCubeRewriter.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestCubeRewriter.java
@@ -533,6 +533,43 @@ public class TestCubeRewriter extends TestQueryRewrite {
   }
 
   @Test
+  public void testCubeGroupbyWithConstantProjected() throws Exception {
+    // check constants
+    String hqlQuery1 = rewrite("select cityid, 99, \"placeHolder\", -1001, SUM(msr2) from testCube" + " where "
+        + twoDaysRange, getConf());
+    String expected1 = getExpectedQuery(cubeName, "select testcube.cityid, 99, \"placeHolder\", -1001,"
+        + " sum(testcube.msr2) FROM ", null, " group by testcube.cityid ",
+        getWhereForDailyAndHourly2days(cubeName, "C2_testfact"));
+    compareQueries(expected1, hqlQuery1);
+
+    // check constants with expression
+    String hqlQuery2 = rewrite(
+        "select cityid, case when stateid = 'za' then \"Not Available\" end, 99, \"placeHolder\", -1001, "
+            + "SUM(msr2) from testCube" + " where " + twoDaysRange, getConf());
+    String expected2 = getExpectedQuery(cubeName,
+        "select testcube.cityid, case when testcube.stateid = 'za' then \"Not Available\" end, 99, \"placeHolder\", -1001,"
+            + " sum(testcube.msr2) FROM ", null,
+        " group by testcube.cityid, case when testcube.stateid = 'za' then \"Not Available\" end ",
+        getWhereForDailyAndHourly2days(cubeName, "C2_testfact"));
+    compareQueries(expected2, hqlQuery2);
+
+    // check expression with boolean and numeric constants
+    String hqlQuery3 = rewrite(
+        "select cityid,stateid + 99, 44 + stateid, stateid - 33, 999 - stateid, TRUE, FALSE, round(123.4567,2), case when stateid='za' then 99 else -1001 end,  "
+            + "SUM(msr2), SUM(msr2 + 39), SUM(msr2) + 567 from testCube" + " where " + twoDaysRange, getConf());
+    String expected3 = getExpectedQuery(
+        cubeName,
+        "select testcube.cityid, testcube.stateid + 99, 44 + testcube.stateid, testcube.stateid - 33, 999 - testcube.stateid, TRUE, FALSE, round(123.4567,2), "
+            + "case when testcube.stateid='za' then 99 else -1001 end,"
+            + " sum(testcube.msr2), sum(testcube.msr2 + 39), sum(testcube.msr2) + 567 FROM ",
+        null,
+        " group by testcube.cityid,testcube.stateid + 99, 44 + testcube.stateid, testcube.stateid - 33, 999 - testcube.stateid, "
+            + " case when testcube.stateid='za' then 99 else -1001 end ",
+        getWhereForDailyAndHourly2days(cubeName, "C2_testfact"));
+    compareQueries(expected3, hqlQuery3);
+  }
+
+  @Test
   public void testCubeGroupbyQuery() throws Exception {
     String hqlQuery =
         rewrite("select name, SUM(msr2) from" + " testCube join citydim on testCube.cityid = citydim.id where "