hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amareshw...@apache.org
Subject svn commit: r1475719 - in /hive/branches/HIVE-4115/ql/src: java/org/apache/hadoop/hive/ql/cube/metadata/ java/org/apache/hadoop/hive/ql/cube/parse/ test/org/apache/hadoop/hive/ql/cube/parse/ test/org/apache/hadoop/hive/ql/cube/processors/
Date Thu, 25 Apr 2013 12:12:09 GMT
Author: amareshwari
Date: Thu Apr 25 12:12:08 2013
New Revision: 1475719

URL: http://svn.apache.org/r1475719
Log:
Add alias replacer for columns

Modified:
    hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/metadata/MetastoreUtil.java
    hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/AliasReplacer.java
    hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java
    hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java
    hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/processors/TestCubeDriver.java

Modified: hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/metadata/MetastoreUtil.java
URL: http://svn.apache.org/viewvc/hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/metadata/MetastoreUtil.java?rev=1475719&r1=1475718&r2=1475719&view=diff
==============================================================================
--- hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/metadata/MetastoreUtil.java
(original)
+++ hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/metadata/MetastoreUtil.java
Thu Apr 25 12:12:08 2013
@@ -1,7 +1,11 @@
 package org.apache.hadoop.hive.ql.cube.metadata;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
 
 public class MetastoreUtil implements MetastoreConstants {
 
@@ -201,4 +205,35 @@ public class MetastoreUtil implements Me
     valueStr.append(it.next());
     return valueStr.toString();
   }
+
+  public static List<String> getColumnNames(AbstractCubeTable table) {
+    List<FieldSchema> fields = table.getColumns();
+    List<String> columns = new ArrayList<String>(fields.size());
+    for (FieldSchema f : fields) {
+      columns.add(f.getName().toLowerCase());
+    }
+    return columns;
+  }
+
+  public static List<String> getValidColumnNames(Cube table) {
+    List<String> columns = new ArrayList<String>();
+    for (CubeMeasure f : table.getMeasures()) {
+      columns.add(f.getName().toLowerCase());
+    }
+    for (CubeDimension f : table.getDimensions()) {
+      addColumnNames(f, columns);
+    }
+    return columns;
+  }
+
+  private static void addColumnNames(CubeDimension dim, List<String> cols) {
+    if (dim instanceof HierarchicalDimension) {
+      HierarchicalDimension h = (HierarchicalDimension) dim;
+      for (CubeDimension d : h.getHierarchy()) {
+        addColumnNames(d, cols);
+      }
+    } else {
+      cols.add(dim.getName().toLowerCase());
+    }
+  }
 }

Modified: hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/AliasReplacer.java
URL: http://svn.apache.org/viewvc/hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/AliasReplacer.java?rev=1475719&r1=1475718&r2=1475719&view=diff
==============================================================================
--- hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/AliasReplacer.java
(original)
+++ hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/AliasReplacer.java
Thu Apr 25 12:12:08 2013
@@ -27,5 +27,47 @@ public class AliasReplacer implements Co
     // 3: (TOK_SELECT (TOK_SELEXPR (. (TOK_TABLE_OR_COL src) key) srckey))))
 
     Map<String, List<String>> tblToColumns = cubeql.getTblToColumns();
+    for (Map.Entry<String, List<String>> entry : tblToColumns.entrySet()) {
+      System.out.println("Table: " + entry.getKey() + "Columns: " + entry.getValue());
+    }
+    Map<String, String> colToTableAlias = cubeql.getColumnsToTableAlias();
+    if (colToTableAlias == null) {
+      return;
+    }
+    String selectTree = cubeql.getSelectTree();
+    String whereTree = cubeql.getWhereTree();
+    String havingTree = cubeql.getHavingTree();
+    String orderByTree = cubeql.getOrderByTree();
+    String groupByTree = cubeql.getGroupByTree();
+
+    if (selectTree != null) {
+      cubeql.setSelectTree(replaceCols(selectTree, colToTableAlias));
+    }
+
+    if (whereTree != null) {
+      cubeql.setWhereTree(replaceCols(whereTree, colToTableAlias));
+    }
+
+    if (havingTree != null) {
+      cubeql.setHavingTree(replaceCols(havingTree, colToTableAlias));
+    }
+
+    if (orderByTree != null) {
+      cubeql.setOrderByTree(replaceCols(orderByTree, colToTableAlias));
+    }
+
+    if (groupByTree != null) {
+      cubeql.setGroupByTree(replaceCols(groupByTree, colToTableAlias));
+    }
+
+  }
+
+  private String replaceCols(String tree, Map<String, String> colToTableAlias) {
+    String srcTree = new String(tree);
+    for (Map.Entry<String, String> entry : colToTableAlias.entrySet()) {
+      srcTree = srcTree.replaceAll(entry.getKey(),
+          entry.getValue() + "." + entry.getKey());
+    }
+    return srcTree;
   }
 }

Modified: hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java
URL: http://svn.apache.org/viewvc/hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java?rev=1475719&r1=1475718&r2=1475719&view=diff
==============================================================================
--- hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java
(original)
+++ hive/branches/HIVE-4115/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java
Thu Apr 25 12:12:08 2013
@@ -3,7 +3,10 @@ package org.apache.hadoop.hive.ql.cube.p
 import static org.apache.hadoop.hive.ql.parse.HiveParser.DOT;
 import static org.apache.hadoop.hive.ql.parse.HiveParser.Identifier;
 import static org.apache.hadoop.hive.ql.parse.HiveParser.KW_AND;
+import static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_ALLCOLREF;
 import static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_FUNCTION;
+import static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_FUNCTIONSTAR;
+import static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_SELEXPR;
 import static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_TABLE_OR_COL;
 
 import java.util.ArrayList;
@@ -22,6 +25,7 @@ import org.apache.hadoop.hive.ql.cube.me
 import org.apache.hadoop.hive.ql.cube.metadata.CubeDimensionTable;
 import org.apache.hadoop.hive.ql.cube.metadata.CubeFactTable;
 import org.apache.hadoop.hive.ql.cube.metadata.CubeMetastoreClient;
+import org.apache.hadoop.hive.ql.cube.metadata.MetastoreUtil;
 import org.apache.hadoop.hive.ql.cube.metadata.UpdatePeriod;
 import org.apache.hadoop.hive.ql.cube.parse.HQLParser.ASTNodeVisitor;
 import org.apache.hadoop.hive.ql.cube.parse.HQLParser.TreeNode;
@@ -65,14 +69,17 @@ public class CubeQueryContext {
       new HashMap<String, String>();
   private final Map<AbstractCubeTable, String> storageTableToQuery =
       new HashMap<AbstractCubeTable, String>();
+  private final Map<String, String> columnToTabAlias = new HashMap<String, String>();
+  private final Map<AbstractCubeTable, List<String>> cubeTabToCols =
+      new HashMap<AbstractCubeTable, List<String>>();
   private ASTNode fromTree;
-  private ASTNode whereTree;
-  private ASTNode havingTree;
-  private ASTNode orderByTree;
-  private ASTNode selectTree;
-  private ASTNode groupbyTree;
+  private String whereTree;
+  private String havingTree;
+  private String orderByTree;
+  private String selectTree;
+  private String groupByTree;
   private ASTNode limitTree;
-  private ASTNode joinTree;
+  private final ASTNode joinTree;
 
   public CubeQueryContext(ASTNode ast, QB qb, HiveConf conf)
       throws SemanticException {
@@ -80,34 +87,31 @@ public class CubeQueryContext {
     this.qb = qb;
     this.conf = conf;
     this.clauseName = getClause();
-    this.whereTree = qb.getParseInfo().getWhrForClause(clauseName);
-    this.havingTree = qb.getParseInfo().getHavingForClause(clauseName);
-    this.orderByTree = qb.getParseInfo().getOrderByForClause(clauseName);
-    this.groupbyTree = qb.getParseInfo().getGroupByForClause(clauseName);
-    this.selectTree = qb.getParseInfo().getSelForClause(clauseName);
+    if (qb.getParseInfo().getWhrForClause(clauseName) != null) {
+      this.whereTree = HQLParser.getString(
+          qb.getParseInfo().getWhrForClause(clauseName)).toLowerCase();
+    }
+    if (qb.getParseInfo().getHavingForClause(clauseName) != null) {
+      this.havingTree = HQLParser.getString(qb.getParseInfo().getHavingForClause(
+          clauseName)).toLowerCase();
+    }
+    if (qb.getParseInfo().getOrderByForClause(clauseName) != null) {
+      this.orderByTree = HQLParser.getString(qb.getParseInfo()
+          .getOrderByForClause(clauseName)).toLowerCase();
+    }
+    if (qb.getParseInfo().getGroupByForClause(clauseName) != null) {
+      this.groupByTree = HQLParser.getString(qb.getParseInfo()
+          .getGroupByForClause(clauseName)).toLowerCase();
+    }
+    if (qb.getParseInfo().getSelForClause(clauseName) != null) {
+      this.selectTree = HQLParser.getString(qb.getParseInfo().getSelForClause(
+          clauseName)).toLowerCase();
+    }
     this.joinTree = qb.getParseInfo().getJoinExpr();
     extractMetaTables();
     extractTimeRange();
-  }
-
-  public CubeQueryContext(CubeQueryContext other) {
-    this.ast = other.ast;
-    this.qb = other.cloneqb();
-    this.conf = other.conf;
-    this.fromDateRaw = other.fromDateRaw;
-    this.toDateRaw = other.toDateRaw;
-    this.dimensions = other.dimensions;
-    this.cube = other.cube;
-    this.candidateFactTables = other.candidateFactTables;
-    this.timeFrom = other.timeFrom;
-    this.timeTo = other.timeTo;
-    this.partitionCols = other.partitionCols;
-    this.factPartitionMap = other.factPartitionMap;
-  }
-
-  private QB cloneqb() {
-    //TODO do deep copy of QB
-    return qb;
+    extractColumns();
+    extractTabAliasForCol();
   }
 
   public boolean hasCubeInQuery() {
@@ -120,8 +124,7 @@ public class CubeQueryContext {
 
   private void extractMetaTables() throws SemanticException {
     try {
-      CubeMetastoreClient client;
-        client = CubeMetastoreClient.getInstance(conf);
+      CubeMetastoreClient client = CubeMetastoreClient.getInstance(conf);
       List<String> tabAliases = new ArrayList<String>(qb.getTabAliases());
       for (String alias :  tabAliases) {
         String tblName = qb.getTabNameForAlias(alias);
@@ -132,8 +135,12 @@ public class CubeQueryContext {
             }
           }
           cube = client.getCube(tblName);
+          cubeTabToCols.put(cube, MetastoreUtil.getValidColumnNames(cube));
         } else if (client.isDimensionTable(tblName)) {
-          dimensions.add(client.getDimensionTable(tblName));
+          CubeDimensionTable dim = client.getDimensionTable(tblName);
+          dimensions.add(dim);
+          cubeTabToCols.put(dim, MetastoreUtil.getColumnNames(dim));
+
         }
       }
       if (cube == null && dimensions.size() == 0) {
@@ -203,30 +210,35 @@ public class CubeQueryContext {
     System.out.println("timeTo:" + timeTo);
   }
 
-  private void extractColumns() {
+  private void extractColumns() throws SemanticException {
     //columnAliases = new ArrayList<String>();
 
     // Check if its 'select *  from...'
-   /* if (selectTree.getChildCount() == 1) {
-      ASTNode star = HQLParser.findNodeByPath(selectTree, TOK_SELEXPR,
+    ASTNode selTree = qb.getParseInfo().getSelForClause(clauseName);
+    if (selTree.getChildCount() == 1) {
+      ASTNode star = HQLParser.findNodeByPath(selTree, TOK_SELEXPR,
           TOK_ALLCOLREF);
       if (star == null) {
-        star = HQLParser.findNodeByPath(selectTree, TOK_SELEXPR,
+        star = HQLParser.findNodeByPath(selTree, TOK_SELEXPR,
             TOK_FUNCTIONSTAR);
       }
 
       if (star != null) {
         int starType = star.getToken().getType();
         if (TOK_FUNCTIONSTAR == starType || TOK_ALLCOLREF == starType) {
-          selectAllColumns = true;
+          throw new SemanticException("Selecting allColumns is not yet supported");
         }
       }
-    } */
+    }
 
     // Traverse select, where, groupby, having and orderby trees to get column
     // names
-    ASTNode trees[] = { selectTree, whereTree, groupbyTree,
-        havingTree, orderByTree};
+    ASTNode trees[] = { qb.getParseInfo().getSelForClause(clauseName),
+        qb.getParseInfo().getWhrForClause(clauseName),
+        qb.getParseInfo().getHavingForClause(clauseName),
+        qb.getParseInfo().getGroupByForClause(clauseName),
+        qb.getParseInfo().getOrderByForClause(clauseName),
+        qb.getParseInfo().getJoinExpr()};
 
     for (ASTNode tree : trees) {
       if (tree == null) {
@@ -281,6 +293,27 @@ public class CubeQueryContext {
     }
   }
 
+  private void extractTabAliasForCol() throws SemanticException {
+    List<String> columns = tblToColumns.get(DEFAULT_TABLE);
+    if (columns == null) {
+      return;
+    }
+    for (String col : columns) {
+      for (Map.Entry<AbstractCubeTable, List<String>> entry :
+        cubeTabToCols.entrySet()) {
+        System.out.println("table columns:" + entry.getValue());
+        if (entry.getValue().contains(col.toLowerCase())) {
+          System.out.println("Found table " + entry.getKey());
+          columnToTabAlias.put(col, entry.getKey().getName());
+          break;
+        }
+      }
+      if (columnToTabAlias.get(col) == null) {
+        throw new SemanticException("Could not find the table containing" +
+            " column:" + col);
+      }
+    }
+  }
   public String getFromDateRaw() {
     return fromDateRaw;
   }
@@ -313,6 +346,14 @@ public class CubeQueryContext {
     return dimensions;
   }
 
+  private String getAliasForTabName(String tabName) {
+    for (String alias : qb.getTabAliases()) {
+      if (qb.getTabNameForAlias(alias).equalsIgnoreCase(tabName)) {
+        return alias;
+      }
+    }
+    return tabName;
+  }
   public void print() {
     StringBuilder builder = new StringBuilder();
     builder.append("ASTNode:" + ast.dump() + "\n");
@@ -477,19 +518,19 @@ public class CubeQueryContext {
     }
   }
 
-  public ASTNode getSelectTree() {
+  public String getSelectTree() {
     return selectTree;
   }
 
-  public ASTNode getWhereTree() {
+  public String getWhereTree() {
     return whereTree;
   }
 
-  public ASTNode getGroupbyTree() {
-    return groupbyTree;
+  public String getGroupByTree() {
+    return groupByTree;
   }
 
-  public ASTNode getHavingTree() {
+  public String getHavingTree() {
     return havingTree;
   }
 
@@ -497,11 +538,15 @@ public class CubeQueryContext {
     return joinTree;
   }
 
-  public ASTNode getOrderbyTree() {
+  public String getOrderByTree() {
     return orderByTree;
   }
 
   public ASTNode getFromTree() {
+    if (cube != null) {
+      System.out.println("alias:" + getAliasForTabName(cube.getName()));
+      return qb.getParseInfo().getSrcForAlias(getAliasForTabName(cube.getName()));
+    }
     return qb.getParseInfo().getSrcForAlias(qb.getTabAliases().iterator().next());
   }
 
@@ -532,16 +577,19 @@ public class CubeQueryContext {
   String getQueryFormat() {
     StringBuilder queryFormat = new StringBuilder();
     queryFormat.append(baseQueryFormat);
+    if (joinTree != null) {
+      queryFormat.append(" JOIN %s");
+    }
     if (getWhereTree() != null || hasPartitions()) {
       queryFormat.append(" WHERE %s");
     }
-    if (getGroupbyTree() != null) {
+    if (getGroupByTree() != null) {
       queryFormat.append(" GROUP BY %s");
     }
     if (getHavingTree() != null) {
       queryFormat.append(" HAVING %s");
     }
-    if (getOrderbyTree() != null) {
+    if (getOrderByTree() != null) {
       queryFormat.append(" ORDER BY %s");
     }
     if (getLimitValue() != null) {
@@ -552,27 +600,31 @@ public class CubeQueryContext {
 
   private Object[] getQueryTreeStrings(String factStorageTable) {
     List<String> qstrs = new ArrayList<String>();
-    qstrs.add(HQLParser.getString(getSelectTree()));
+    qstrs.add(getSelectTree());
     String fromString = HQLParser.getString(getFromTree()).toLowerCase();
     String whereString = getWhereTree(factStorageTable);
     for (Map.Entry<AbstractCubeTable, String> entry :
-        storageTableToQuery.entrySet()) {
+      storageTableToQuery.entrySet()) {
       String src = entry.getKey().getName().toLowerCase();
+      String alias = getAliasForTabName(src);
       System.out.println("From string:" + fromString + " src:" + src + " value:" + entry.getValue());
-      fromString = fromString.replaceAll(src, entry.getValue() + " " + src);
+      fromString = fromString.replaceAll(src, entry.getValue() + " " + alias);
     }
     qstrs.add(fromString);
+    if (joinTree != null) {
+      qstrs.add(HQLParser.getString(joinTree));
+    }
     if (whereString != null) {
       qstrs.add(whereString);
     }
-    if (getGroupbyTree() != null) {
-      qstrs.add(HQLParser.getString(getGroupbyTree()));
+    if (getGroupByTree() != null) {
+      qstrs.add(getGroupByTree());
     }
     if (getHavingTree() != null) {
-      qstrs.add(HQLParser.getString(getHavingTree()));
+      qstrs.add(getHavingTree());
     }
-    if (getOrderbyTree() != null) {
-      qstrs.add(HQLParser.getString(getOrderbyTree()));
+    if (getOrderByTree() != null) {
+      qstrs.add(getOrderByTree());
     }
     if (getLimitValue() != null) {
       qstrs.add(String.valueOf(getLimitValue()));
@@ -583,11 +635,12 @@ public class CubeQueryContext {
   private String toHQL(String tableName) {
     String qfmt = getQueryFormat();
     System.out.println("qfmt:" + qfmt);
+    //print();
     return String.format(qfmt, getQueryTreeStrings(tableName));
   }
 
   public String getWhereTree(String factStorageTable) {
-    String originalWhereString = HQLParser.getString(getWhereTree());
+    String originalWhereString = getWhereTree();
     String whereWithoutTimerange;
     if (factStorageTable != null) {
       whereWithoutTimerange = originalWhereString.substring(0,
@@ -595,19 +648,37 @@ public class CubeQueryContext {
     } else {
       whereWithoutTimerange = originalWhereString;
     }
+    boolean dimensionsAdded = false;
     // add where clause for all dimensions
-    for (CubeDimensionTable dim : dimensions) {
+    Iterator<CubeDimensionTable> it = dimensions.iterator();
+    if (it.hasNext()) {
+      dimensionsAdded = true;
+      CubeDimensionTable dim = it.next();
+      while (it.hasNext()) {
+        String storageTable = dimStorageMap.get(dim).get(0);
+        storageTableToQuery.put(dim, storageTable);
+        whereWithoutTimerange += "(";
+        whereWithoutTimerange += storageTableToWhereClause.get(storageTable);
+        whereWithoutTimerange += ") AND";
+        dim = it.next();
+      }
       String storageTable = dimStorageMap.get(dim).get(0);
       storageTableToQuery.put(dim, storageTable);
+      whereWithoutTimerange += "(";
       whereWithoutTimerange += storageTableToWhereClause.get(storageTable);
+      whereWithoutTimerange += ") ";
     }
     if (factStorageTable != null) {
+      if (dimensionsAdded) {
+        whereWithoutTimerange += " AND ";
+      }
       // add where clause for fact;
-      return whereWithoutTimerange + storageTableToWhereClause.get(
+      whereWithoutTimerange += "(";
+      whereWithoutTimerange +=  storageTableToWhereClause.get(
           factStorageTable);
-    } else {
-      return whereWithoutTimerange;
+      whereWithoutTimerange += ") ";
     }
+    return whereWithoutTimerange;
   }
 
   public String toHQL() throws SemanticException {
@@ -684,4 +755,27 @@ public class CubeQueryContext {
     return tblToColumns;
   }
 
+  public Map<String, String> getColumnsToTableAlias() {
+    return columnToTabAlias;
+  }
+
+  public void setSelectTree(String selectTree) {
+    this.selectTree = selectTree;
+  }
+
+  public void setWhereTree(String whereTree) {
+    this.whereTree = whereTree;
+  }
+
+  public void setHavingTree(String havingTree) {
+    this.havingTree = havingTree;
+  }
+
+  public void setGroupByTree(String groupByTree) {
+    this.groupByTree = groupByTree;
+  }
+
+  public void setOrderByTree(String orderByTree) {
+    this.orderByTree = orderByTree;
+  }
 }

Modified: hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java
URL: http://svn.apache.org/viewvc/hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java?rev=1475719&r1=1475718&r2=1475719&view=diff
==============================================================================
--- hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java
(original)
+++ hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java
Thu Apr 25 12:12:08 2013
@@ -89,8 +89,9 @@ public class CubeTestSetup {
       factColumns.add(measure.getColumn());
     }
 
-    // add one dimension of the cube
+    // add dimensions of the cube
     factColumns.add(new FieldSchema("zipcode","int", "zip"));
+    factColumns.add(new FieldSchema("cityid","int", "city id"));
 
     Map<Storage, List<UpdatePeriod>> storageAggregatePeriods =
         new HashMap<Storage, List<UpdatePeriod>>();

Modified: hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/processors/TestCubeDriver.java
URL: http://svn.apache.org/viewvc/hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/processors/TestCubeDriver.java?rev=1475719&r1=1475718&r2=1475719&view=diff
==============================================================================
--- hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/processors/TestCubeDriver.java
(original)
+++ hive/branches/HIVE-4115/ql/src/test/org/apache/hadoop/hive/ql/cube/processors/TestCubeDriver.java
Thu Apr 25 12:12:08 2013
@@ -63,6 +63,7 @@ public class TestCubeDriver {
     Date twodaysBack = cal.getTime();
     System.out.println("Test twodaysBack:" + twodaysBack);
     System.out.println("Test from:" + getDateUptoHours(twodaysBack) + " to:" + getDateUptoHours(now));
+    //String expected = "select SUM(testCube.msr2) from "
     String hqlQuery = driver.compileCubeQuery("select SUM(msr2) from testCube" +
         " where time_range_in('" + getDateUptoHours(twodaysBack)
         + "','" + getDateUptoHours(now) + "')");
@@ -71,6 +72,74 @@ public class TestCubeDriver {
   }
 
   @Test
+  public void testCubeJoinQuery() throws Exception {
+    conf = new Configuration();
+    driver = new CubeDriver(new HiveConf(conf, HiveConf.class));
+    Calendar cal = Calendar.getInstance();
+    Date now = cal.getTime();
+    System.out.println("Test now:" + now);
+    cal.add(Calendar.DAY_OF_MONTH, -2);
+    Date twodaysBack = cal.getTime();
+    System.out.println("Test twodaysBack:" + twodaysBack);
+    System.out.println("Test from:" + getDateUptoHours(twodaysBack) + " to:" + getDateUptoHours(now));
+    //String expected = "select SUM(testCube.msr2) from "
+    String hqlQuery = driver.compileCubeQuery("select SUM(msr2) from testCube"
+        + " join citytable on testCube.cityid = citytable.id"
+        + " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+
+  }
+
+  @Test
+  public void testCubeQueryWithAilas() throws Exception {
+    conf = new Configuration();
+    driver = new CubeDriver(new HiveConf(conf, HiveConf.class));
+    Calendar cal = Calendar.getInstance();
+    Date now = cal.getTime();
+    System.out.println("Test now:" + now);
+    cal.add(Calendar.DAY_OF_MONTH, -2);
+    Date twodaysBack = cal.getTime();
+    System.out.println("Test twodaysBack:" + twodaysBack);
+    System.out.println("Test from:" + getDateUptoHours(twodaysBack) + " to:" + getDateUptoHours(now));
+    String hqlQuery = driver.compileCubeQuery("select SUM(msr2) from testCube" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+    hqlQuery = driver.compileCubeQuery("select SUM(msr2) m2 from testCube" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+    hqlQuery = driver.compileCubeQuery("select name, SUM(msr2) from testCube" +
+    		" join citytable" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')" +
+        " group by name");
+    System.out.println("cube hql:" + hqlQuery);
+
+    hqlQuery = driver.compileCubeQuery("select SUM(mycube.msr2) from testCube mycube" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+    //Assert.assertEquals(queries[1], cubeql.toHQL());
+
+    hqlQuery = driver.compileCubeQuery("select SUM(testCube.msr2) from testCube" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+
+    hqlQuery = driver.compileCubeQuery("select mycube.msr2 m2 from testCube mycube" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+
+    hqlQuery = driver.compileCubeQuery("select testCube.msr2 m2 from testCube" +
+        " where time_range_in('" + getDateUptoHours(twodaysBack)
+        + "','" + getDateUptoHours(now) + "')");
+    System.out.println("cube hql:" + hqlQuery);
+  }
+
+  @Test
   public void testCubeWhereQueryForMonth() throws Exception {
     conf = new Configuration();
     driver = new CubeDriver(new HiveConf(new Configuration(), HiveConf.class));



Mime
View raw message