You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by de...@apache.org on 2016/02/03 13:10:44 UTC

[24/51] [abbrv] lens git commit: LENS-735 : Remove accepting TableReferences for ReferenceDimAttribute

http://git-wip-us.apache.org/repos/asf/lens/blob/908530f5/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 61fb73c..4810559 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
@@ -135,7 +135,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
   @Test
   public void testCubeQuery() throws Exception {
     CubeQueryContext rewrittenQuery =
-      rewriteCtx("cube select" + " SUM(msr2) from testCube where " + TWO_DAYS_RANGE, getConfWithStorages("C2"));
+      rewriteCtx("cube select SUM(msr2) from testCube where " + TWO_DAYS_RANGE, getConfWithStorages("C2"));
     String expected =
       getExpectedQuery(TEST_CUBE_NAME, "select sum(testcube.msr2) FROM ", null, null,
         getWhereForDailyAndHourly2days(TEST_CUBE_NAME, "C2_testfact"));
@@ -151,7 +151,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
     conf.setBoolean(CubeQueryConfUtil.FAIL_QUERY_ON_PARTIAL_DATA, false);
     conf.set(DRIVER_SUPPORTED_STORAGES, "C1,C2,C4");
     CubeQueryContext cubeQueryContext =
-      rewriteCtx("cube select" + " SUM(msr2) from testCube where " + THIS_YEAR_RANGE, conf);
+      rewriteCtx("cube select SUM(msr2) from testCube where " + THIS_YEAR_RANGE, conf);
     PruneCauses<CubeFactTable> pruneCause = cubeQueryContext.getFactPruningMsgs();
     int lessDataCauses = 0;
     for (Map.Entry<CubeFactTable, List<CandidateTablePruneCause>> entry : pruneCause.entrySet()) {
@@ -167,7 +167,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
   @Test
   public void testLightestFactFirst() throws Exception {
     // testFact is lighter than testFact2.
-    String hqlQuery = rewrite("cube select" + " SUM(msr2) from testCube where " + TWO_DAYS_RANGE, getConfWithStorages(
+    String hqlQuery = rewrite("cube select SUM(msr2) from testCube where " + TWO_DAYS_RANGE, getConfWithStorages(
       "C2"));
     String expected =
       getExpectedQuery(TEST_CUBE_NAME, "select sum(testcube.msr2) FROM ", null, null,
@@ -202,7 +202,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
   @Test
   public void testDerivedCube() throws ParseException, LensException, HiveException, ClassNotFoundException {
     CubeQueryContext rewrittenQuery =
-      rewriteCtx("cube select" + " SUM(msr2) from derivedCube where " + TWO_DAYS_RANGE, getConfWithStorages("C2"));
+      rewriteCtx("cube select SUM(msr2) from derivedCube where " + TWO_DAYS_RANGE, getConfWithStorages("C2"));
     String expected =
       getExpectedQuery(DERIVED_CUBE_NAME, "select sum(derivedCube.msr2) FROM ", null, null,
         getWhereForDailyAndHourly2days(DERIVED_CUBE_NAME, "C2_testfact"));
@@ -211,7 +211,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
     assertNotNull(rewrittenQuery.getNonExistingParts());
 
     LensException th = getLensExceptionInRewrite(
-      "select SUM(msr4) from derivedCube" + " where " + TWO_DAYS_RANGE, getConf());
+      "select SUM(msr4) from derivedCube where " + TWO_DAYS_RANGE, getConf());
     assertEquals(th.getErrorCode(), LensCubeErrorCode.COLUMN_NOT_FOUND.getLensErrorInfo().getErrorCode());
 
     // test join
@@ -219,27 +219,28 @@ public class TestCubeRewriter extends TestQueryRewrite {
     conf.setBoolean(DISABLE_AUTO_JOINS, false);
     String hqlQuery;
 
-    hqlQuery = rewrite("cube select" + " testdim2.name, SUM(msr2) from derivedCube where " + TWO_DAYS_RANGE, conf);
+    /*
+    Accessing join chains from derived cubes are not supported yet.
+    hqlQuery = rewrite("cube select dim2chain.name, SUM(msr2) from derivedCube where " + TWO_DAYS_RANGE, conf);
     expected =
-      getExpectedQuery(DERIVED_CUBE_NAME, "select testdim2.name, sum(derivedCube.msr2) FROM ", " JOIN "
-          + getDbName() + "c1_testdim2tbl testdim2 ON derivedCube.dim2 = "
-          + " testdim2.id and (testdim2.dt = 'latest') ", null, "group by (testdim2.name)", null,
+      getExpectedQuery(DERIVED_CUBE_NAME, "select dim2chain.name, sum(derivedCube.msr2) FROM ", " JOIN "
+          + getDbName() + "c1_testdim2tbl dim2chain ON derivedCube.dim2 = "
+          + " dim2chain.id and (dim2chain.dt = 'latest') ", null, "group by (dim2chain.name)", null,
         getWhereForDailyAndHourly2days(DERIVED_CUBE_NAME, "c1_summary2"));
     compareQueries(hqlQuery, expected);
 
     // Test that explicit join query passes with join resolver disabled
     conf.setBoolean(DISABLE_AUTO_JOINS, true);
-    List<String> joinWhereConds = new ArrayList<String>();
-    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "testdim2", StorageConstants.getPartitionsForLatest()));
     hqlQuery =
-      rewrite("cube select" + " testdim2.name, SUM(msr2) from derivedCube "
-        + " inner join testdim2 on derivedCube.dim2 = testdim2.id " + "where " + TWO_DAYS_RANGE, conf);
+      rewrite("cube select citydim.name, SUM(msr2) from derivedCube "
+        + " inner join citydim on derivedCube.cityid = citydim.id where " + TWO_DAYS_RANGE, conf);
     expected =
-      getExpectedQuery(DERIVED_CUBE_NAME, "select testdim2.name, sum(derivedCube.msr2) FROM ",
-        " inner JOIN " + getDbName() + "c1_testdim2tbl testdim2 ON derivedCube.dim2 = " + " testdim2.id ", null,
-        "group by (testdim2.name)", joinWhereConds,
+      getExpectedQuery(DERIVED_CUBE_NAME, "select citydim.name, sum(derivedCube.msr2) FROM ",
+        " inner JOIN " + getDbName()
+        + "c1_citytable citydim ON derivedCube.cityid = citydim.id  and (citydim.dt = 'latest')", null,
+        "group by (citydim.name)", null,
         getWhereForDailyAndHourly2days(DERIVED_CUBE_NAME, "c1_summary2"));
-    compareQueries(hqlQuery, expected);
+    compareQueries(hqlQuery, expected);*/
   }
 
   @Test
@@ -619,24 +620,24 @@ public class TestCubeRewriter extends TestQueryRewrite {
     conf.setBoolean(DISABLE_AUTO_JOINS, false);
     String hql, expected;
     hql = rewrite(
-      "select countrydim.name, msr2 from" + " testCube" + " where countrydim.region = 'asia' and "
+      "select cubecountry.name, msr2 from" + " testCube" + " where cubecountry.region = 'asia' and "
         + TWO_DAYS_RANGE, conf);
     expected =
-      getExpectedQuery(TEST_CUBE_NAME, "select countrydim.name, sum(testcube.msr2)" + " FROM ", " JOIN " + getDbName()
-          + "c3_countrytable_partitioned countrydim on testcube.countryid=countrydim.id and countrydim.dt='latest'",
-        "countrydim.region='asia'",
-        " group by countrydim.name ", null,
+      getExpectedQuery(TEST_CUBE_NAME, "select cubecountry.name, sum(testcube.msr2)" + " FROM ", " JOIN " + getDbName()
+          + "c3_countrytable_partitioned cubecountry on testcube.countryid=cubecountry.id and cubecountry.dt='latest'",
+        "cubecountry.region='asia'",
+        " group by cubecountry.name ", null,
         getWhereForHourly2days(TEST_CUBE_NAME, "C3_testfact2_raw"));
     compareQueries(hql, expected);
     hql = rewrite(
-      "select statedim.name, statedim.countryid, msr2 from" + " testCube" + " where statedim.countryid = 5 and "
+      "select cubestate.name, cubestate.countryid, msr2 from" + " testCube" + " where cubestate.countryid = 5 and "
         + TWO_DAYS_RANGE, conf);
     expected =
-      getExpectedQuery(TEST_CUBE_NAME, "select statedim.name, statedim.countryid, sum(testcube.msr2)" + " FROM ",
+      getExpectedQuery(TEST_CUBE_NAME, "select cubestate.name, cubestate.countryid, sum(testcube.msr2)" + " FROM ",
         " JOIN " + getDbName()
-          + "c3_statetable_partitioned statedim ON" + " testCube.stateid = statedim.id and statedim.dt = 'latest'",
-        "statedim.countryid=5",
-        " group by statedim.name, statedim.countryid", null,
+          + "c3_statetable_partitioned cubestate ON" + " testCube.stateid = cubestate.id and cubestate.dt = 'latest'",
+        "cubestate.countryid=5",
+        " group by cubestate.name, cubestate.countryid", null,
         getWhereForHourly2days(TEST_CUBE_NAME, "C3_testfact2_raw"));
     compareQueries(hql, expected);
   }
@@ -649,12 +650,9 @@ public class TestCubeRewriter extends TestQueryRewrite {
     String hqlQuery =
       rewrite("select SUM(msr2) from testCube" + " join citydim on testCube.cityid = citydim.id" + " where "
         + TWO_DAYS_RANGE, conf);
-    List<String> joinWhereConds = new ArrayList<String>();
-    //    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "citydim", StorageConstants.getPartitionsForLatest
-    // ()));
     String expected =
       getExpectedQuery(TEST_CUBE_NAME, "select sum(testcube.msr2)" + " FROM ", " INNER JOIN " + getDbName()
-          + "c2_citytable citydim ON" + " testCube.cityid = citydim.id", null, null, joinWhereConds,
+          + "c2_citytable citydim ON" + " testCube.cityid = citydim.id", null, null, null,
         getWhereForDailyAndHourly2days(TEST_CUBE_NAME, "C2_testfact"));
     compareQueries(hqlQuery, expected);
 
@@ -673,16 +671,15 @@ public class TestCubeRewriter extends TestQueryRewrite {
       rewrite("select statedim.name, SUM(msr2) from" + " testCube" + " join citydim on testCube.cityid = citydim.id"
         + " left outer join statedim on statedim.id = citydim.stateid"
         + " right outer join zipdim on citydim.zipcode = zipdim.code" + " where " + TWO_DAYS_RANGE, getConf());
-    joinWhereConds = new ArrayList<>();
-    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "citydim", StorageConstants.getPartitionsForLatest()));
-    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "zipdim", StorageConstants.getPartitionsForLatest()));
     expected =
       getExpectedQuery(TEST_CUBE_NAME,
         "select statedim.name," + " sum(testcube.msr2) FROM ", "INNER JOIN " + getDbName()
-          + "c1_citytable citydim ON" + " testCube.cityid = citydim.id LEFT OUTER JOIN " + getDbName()
+          + "c1_citytable citydim ON testCube.cityid = citydim.id and citydim.dt='latest' LEFT OUTER JOIN "
+          + getDbName()
           + "c1_statetable statedim" + " ON statedim.id = citydim.stateid AND "
           + "(statedim.dt = 'latest') RIGHT OUTER JOIN " + getDbName() + "c1_ziptable"
-          + " zipdim ON citydim.zipcode = zipdim.code", null, " group by" + " statedim.name ", joinWhereConds,
+          + " zipdim ON citydim.zipcode = zipdim.code and zipdim.dt='latest'", null, " group by" + " statedim.name ",
+        null,
         getWhereForHourly2days(TEST_CUBE_NAME, "C1_testfact2"));
     compareQueries(hqlQuery, expected);
 
@@ -691,14 +688,12 @@ public class TestCubeRewriter extends TestQueryRewrite {
       rewrite("select st.name, SUM(msr2) from" + " testCube TC" + " join citydim CT on TC.cityid = CT.id"
         + " left outer join statedim ST on ST.id = CT.stateid"
         + " right outer join zipdim ZT on CT.zipcode = ZT.code" + " where " + TWO_DAYS_RANGE, getConf());
-    joinWhereConds = new ArrayList<String>();
-    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "ct", StorageConstants.getPartitionsForLatest()));
-    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "zt", StorageConstants.getPartitionsForLatest()));
     expected =
       getExpectedQuery("tc", "select st.name," + " sum(tc.msr2) FROM ", " INNER JOIN " + getDbName()
-          + "c1_citytable ct ON" + " tc.cityid = ct.id LEFT OUTER JOIN " + getDbName() + "c1_statetable st"
+          + "c1_citytable ct ON" + " tc.cityid = ct.id and ct.dt='latest' LEFT OUTER JOIN "
+          + getDbName() + "c1_statetable st"
           + " ON st.id = ct.stateid and (st.dt = 'latest') " + "RIGHT OUTER JOIN " + getDbName() + "c1_ziptable"
-          + " zt ON ct.zipcode = zt.code", null, " group by" + " st.name ", joinWhereConds,
+          + " zt ON ct.zipcode = zt.code and zt.dt='latest'", null, " group by" + " st.name ", null,
         getWhereForHourly2days("tc", "C1_testfact2"));
     compareQueries(hqlQuery, expected);
 
@@ -727,7 +722,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
     LensException th = getLensExceptionInRewrite(
       "select name, SUM(msr2) from testCube" + " join citydim" + " where " + TWO_DAYS_RANGE
         + " group by name", getConf());
-    assertEquals(th.getErrorCode(), LensCubeErrorCode.NO_JOIN_CONDITION_AVAIABLE.getLensErrorInfo().getErrorCode());
+    assertEquals(th.getErrorCode(), LensCubeErrorCode.NO_JOIN_CONDITION_AVAILABLE.getLensErrorInfo().getErrorCode());
   }
 
   @Test
@@ -779,13 +774,10 @@ public class TestCubeRewriter extends TestQueryRewrite {
     String hqlQuery =
       rewrite("select name, SUM(msr2) from" + " testCube join citydim on testCube.cityid = citydim.id where "
         + TWO_DAYS_RANGE, conf);
-    List<String> joinWhereConds = new ArrayList<String>();
-    //    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "citydim", StorageConstants.getPartitionsForLatest
-    // ()));
     String expected =
       getExpectedQuery(TEST_CUBE_NAME, "select citydim.name," + " sum(testcube.msr2) FROM ", "INNER JOIN " + getDbName()
           + "c2_citytable citydim ON" + " testCube.cityid = citydim.id", null, " group by citydim.name ",
-        joinWhereConds, getWhereForDailyAndHourly2days(TEST_CUBE_NAME, "C2_testfact"));
+        null, getWhereForDailyAndHourly2days(TEST_CUBE_NAME, "C2_testfact"));
     compareQueries(hqlQuery, expected);
 
     hqlQuery =
@@ -860,21 +852,21 @@ public class TestCubeRewriter extends TestQueryRewrite {
     conf.setBoolean(DISABLE_AUTO_JOINS, false);
     conf.set(DRIVER_SUPPORTED_STORAGES, "C1, C2");
     hqlQuery =
-      rewrite("SELECT citydim.name AS g1," + " CASE  WHEN citydim.name=='NULL'  THEN 'NULL' "
-        + " WHEN citydim.name=='X'  THEN 'X-NAME' " + " WHEN citydim.name=='Y'  THEN 'Y-NAME' "
-        + " ELSE 'DEFAULT'   END  AS g2, " + " statedim.name AS g3," + " statedim.id AS g4, "
-        + " zipdim.code!=1  AND " + " ((zipdim.f1==\"xyz\"  AND  (zipdim.f2 >= \"3\"  AND "
-        + "  zipdim.f2 !=\"NULL\"  AND  zipdim.f2 != \"uk\")) "
-        + "  OR (zipdim.f2==\"adc\"  AND  zipdim.f1==\"js\" "
-        + "  AND  ( citydim.name == \"X\"  OR  citydim.name == \"Y\" )) "
-        + " OR ((zipdim.f1==\"api\"  OR  zipdim.f1==\"uk\"  OR  (zipdim.f1==\"adc\"  AND  zipdim.f1!=\"js\"))"
-        + "  AND  citydim.id==12) ) AS g5," + " zipdim.code==1  AND "
-        + " ((zipdim.f1==\"xyz\"  AND  (zipdim.f2 >= \"3\"  AND "
-        + "  zipdim.f2 !=\"NULL\"  AND  zipdim.f2 != \"uk\")) "
-        + " OR (zipdim.f2==\"adc\"  AND  zipdim.f1==\"js\" "
-        + " AND  ( citydim.name == \"X\"  OR  citydim.name == \"Y\" )) "
-        + "  OR ((zipdim.f1==\"api\"  OR  zipdim.f1==\"uk\"  OR  (zipdim.f1==\"adc\"  AND  zipdim.f1!=\"js\"))"
-        + "    AND  citydim.id==12) ) AS g6, " + "  zipdim.f1 AS g7, "
+      rewrite("SELECT cubecity.name AS g1," + " CASE  WHEN cubecity.name=='NULL'  THEN 'NULL' "
+        + " WHEN cubecity.name=='X'  THEN 'X-NAME' " + " WHEN cubecity.name=='Y'  THEN 'Y-NAME' "
+        + " ELSE 'DEFAULT'   END  AS g2, " + " cubestate.name AS g3," + " cubestate.id AS g4, "
+        + " cubezip.code!=1  AND " + " ((cubezip.f1==\"xyz\"  AND  (cubezip.f2 >= \"3\"  AND "
+        + "  cubezip.f2 !=\"NULL\"  AND  cubezip.f2 != \"uk\")) "
+        + "  OR (cubezip.f2==\"adc\"  AND  cubezip.f1==\"js\" "
+        + "  AND  ( cubecity.name == \"X\"  OR  cubecity.name == \"Y\" )) "
+        + " OR ((cubezip.f1==\"api\"  OR  cubezip.f1==\"uk\"  OR  (cubezip.f1==\"adc\"  AND  cubezip.f1!=\"js\"))"
+        + "  AND  cubecity.id==12) ) AS g5," + " cubezip.code==1  AND "
+        + " ((cubezip.f1==\"xyz\"  AND  (cubezip.f2 >= \"3\"  AND "
+        + "  cubezip.f2 !=\"NULL\"  AND  cubezip.f2 != \"uk\")) "
+        + " OR (cubezip.f2==\"adc\"  AND  cubezip.f1==\"js\" "
+        + " AND  ( cubecity.name == \"X\"  OR  cubecity.name == \"Y\" )) "
+        + "  OR ((cubezip.f1==\"api\"  OR  cubezip.f1==\"uk\"  OR  (cubezip.f1==\"adc\"  AND  cubezip.f1!=\"js\"))"
+        + "    AND  cubecity.id==12) ) AS g6, " + "  cubezip.f1 AS g7, "
         + "  format_number(SUM(msr1),\"##################.###\") AS a1,"
         + "  format_number(SUM(msr2),\"##################.###\") AS a2, "
         + "  format_number(SUM(msr3),\"##################.###\") AS a3, "
@@ -884,35 +876,38 @@ public class TestCubeRewriter extends TestQueryRewrite {
         + "  FROM testCube where " + TWO_DAYS_RANGE + " HAVING (SUM(msr1) >=1000)  AND (SUM(msr2)>=0.01)", conf);
     String actualExpr =
       ""
-        + " join " + getDbName() + "c1_statetable statedim on testcube.stateid=statedim.id and (statedim.dt='latest')"
-        + " join " + getDbName() + "c1_ziptable zipdim on testcube.zipcode = zipdim.code and (zipdim.dt = 'latest')  "
-        + " join " + getDbName() + "c1_citytable citydim on testcube.cityid = citydim.id and (citydim.dt = 'latest')"
+        + " join " + getDbName() + "c1_statetable cubestate on testcube.stateid=cubestate.id and "
+        + "(cubestate.dt='latest')"
+        + " join " + getDbName()
+        + "c1_ziptable cubezip on testcube.zipcode = cubezip.code and (cubezip.dt = 'latest')  "
+        + " join " + getDbName()
+        + "c1_citytable cubecity on testcube.cityid = cubecity.id and (cubecity.dt = 'latest')"
         + "";
     expected =
       getExpectedQuery(
         TEST_CUBE_NAME,
-        "SELECT ( citydim.name ) as `g1` ,"
-          + "  case  when (( citydim.name ) ==  'NULL' ) then  'NULL'  when (( citydim.name ) ==  'X' )"
-          + " then  'X-NAME'  when (( citydim.name ) ==  'Y' ) then  'Y-NAME'"
-          + "  else  'DEFAULT'  end  as `g2` , ( statedim.name ) as `g3` , ( statedim.id ) as `g4` ,"
-          + " ((( zipdim.code ) !=  1 ) and ((((( zipdim.f1 ) ==  \"xyz\" )"
-          + " and (((( zipdim.f2 ) >=  \"3\" ) and (( zipdim.f2 ) !=  \"NULL\" ))"
-          + " and (( zipdim.f2 ) !=  \"uk\" ))) or (((( zipdim.f2 ) ==  \"adc\" )"
-          + " and (( zipdim.f1 ) ==  \"js\" ))"
-          + " and ((( citydim.name ) ==  \"X\" ) or (( citydim.name ) ==  \"Y\" ))))"
-          + " or ((((( zipdim.f1 ) ==  \"api\" )"
-          + " or (( zipdim.f1 ) ==  \"uk\" )) or ((( zipdim.f1 ) ==  \"adc\" )"
-          + " and (( zipdim.f1 ) !=  \"js\" )))"
-          + " and (( citydim.id ) ==  12 )))) as `g5` , ((( zipdim.code ) ==  1 )"
-          + " and ((((( zipdim.f1 ) ==  \"xyz\" ) and (((( zipdim.f2 ) >=  \"3\" )"
-          + " and (( zipdim.f2 ) !=  \"NULL\" ))"
-          + " and (( zipdim.f2 ) !=  \"uk\" ))) or (((( zipdim.f2 ) ==  \"adc\" )"
-          + " and (( zipdim.f1 ) ==  \"js\" ))"
-          + " and ((( citydim.name ) ==  \"X\" ) or (( citydim.name ) ==  \"Y\" ))))"
-          + " or ((((( zipdim.f1 ) ==  \"api\" )"
-          + " or (( zipdim.f1 ) ==  \"uk\" )) or ((( zipdim.f1 ) ==  \"adc\" )"
-          + " and (( zipdim.f1 ) !=  \"js\" )))"
-          + " and (( citydim.id ) ==  12 )))) as `g6` , ( zipdim.f1 ) as `g7` ,"
+        "SELECT ( cubecity.name ) as `g1` ,"
+          + "  case  when (( cubecity.name ) ==  'NULL' ) then  'NULL'  when (( cubecity.name ) ==  'X' )"
+          + " then  'X-NAME'  when (( cubecity.name ) ==  'Y' ) then  'Y-NAME'"
+          + "  else  'DEFAULT'  end  as `g2` , ( cubestate.name ) as `g3` , ( cubestate.id ) as `g4` ,"
+          + " ((( cubezip.code ) !=  1 ) and ((((( cubezip.f1 ) ==  \"xyz\" )"
+          + " and (((( cubezip.f2 ) >=  \"3\" ) and (( cubezip.f2 ) !=  \"NULL\" ))"
+          + " and (( cubezip.f2 ) !=  \"uk\" ))) or (((( cubezip.f2 ) ==  \"adc\" )"
+          + " and (( cubezip.f1 ) ==  \"js\" ))"
+          + " and ((( cubecity.name ) ==  \"X\" ) or (( cubecity.name ) ==  \"Y\" ))))"
+          + " or ((((( cubezip.f1 ) ==  \"api\" )"
+          + " or (( cubezip.f1 ) ==  \"uk\" )) or ((( cubezip.f1 ) ==  \"adc\" )"
+          + " and (( cubezip.f1 ) !=  \"js\" )))"
+          + " and (( cubecity.id ) ==  12 )))) as `g5` , ((( cubezip.code ) ==  1 )"
+          + " and ((((( cubezip.f1 ) ==  \"xyz\" ) and (((( cubezip.f2 ) >=  \"3\" )"
+          + " and (( cubezip.f2 ) !=  \"NULL\" ))"
+          + " and (( cubezip.f2 ) !=  \"uk\" ))) or (((( cubezip.f2 ) ==  \"adc\" )"
+          + " and (( cubezip.f1 ) ==  \"js\" ))"
+          + " and ((( cubecity.name ) ==  \"X\" ) or (( cubecity.name ) ==  \"Y\" ))))"
+          + " or ((((( cubezip.f1 ) ==  \"api\" )"
+          + " or (( cubezip.f1 ) ==  \"uk\" )) or ((( cubezip.f1 ) ==  \"adc\" )"
+          + " and (( cubezip.f1 ) !=  \"js\" )))"
+          + " and (( cubecity.id ) ==  12 )))) as `g6` , ( cubezip.f1 ) as `g7` ,"
           + " format_number(sum(( testcube.msr1 )),  \"##################.###\" ) as `a1` ,"
           + " format_number(sum(( testcube.msr2 )),  \"##################.###\" ) as `a2` ,"
           + " format_number(sum(( testcube.msr3 )),  \"##################.###\" ) as `a3`, "
@@ -923,47 +918,48 @@ public class TestCubeRewriter extends TestQueryRewrite {
           + "  FROM ",
         actualExpr,
         null,
-        " GROUP BY ( citydim.name ), case  when (( citydim.name ) ==  'NULL' ) "
-          + "then  'NULL'  when (( citydim.name ) ==  'X' ) then  'X-NAME'  when (( citydim.name ) ==  'Y' )"
-          + " then  'Y-NAME'  else  'DEFAULT'  end, ( statedim.name ), ( statedim.id ),"
-          + " ((( zipdim.code ) !=  1 ) and ((((( zipdim.f1 ) ==  \"xyz\" ) and (((( zipdim.f2 ) >=  \"3\" )"
-          + " and (( zipdim.f2 ) !=  \"NULL\" )) and (( zipdim.f2 ) !=  \"uk\" ))) or (((( zipdim.f2 ) ==  \"adc\" )"
-          + " and (( zipdim.f1 ) ==  \"js\" )) and ((( citydim.name ) ==  \"X\" ) or (( citydim.name ) ==  \"Y\" ))))"
-          + " or ((((( zipdim.f1 ) ==  \"api\" ) or (( zipdim.f1 ) ==  \"uk\" )) or ((( zipdim.f1 ) ==  \"adc\" )"
-          + " and (( zipdim.f1 ) !=  \"js\" ))) and (( citydim.id ) ==  12 )))), ((( zipdim.code ) ==  1 ) and"
-          + " ((((( zipdim.f1 ) ==  \"xyz\" ) and (((( zipdim.f2 ) >=  \"3\" ) and (( zipdim.f2 ) !=  \"NULL\" ))"
-          + " and (( zipdim.f2 ) !=  \"uk\" ))) or (((( zipdim.f2 ) ==  \"adc\" ) and (( zipdim.f1 ) ==  \"js\" ))"
-          + " and ((( citydim.name ) ==  \"X\" ) or (( citydim.name ) ==  \"Y\" )))) or ((((( zipdim.f1 ) ==  \"api\" )"
-          + " or (( zipdim.f1 ) ==  \"uk\" )) or ((( zipdim.f1 ) ==  \"adc\" ) and (( zipdim.f1 ) !=  \"js\" )))"
-          + " and (( citydim.id ) ==  12 )))), ( zipdim.f1 ) HAVING ((sum(( testcube.msr1 )) >=  1000 ) "
+        " GROUP BY ( cubecity.name ), case  when (( cubecity.name ) ==  'NULL' ) "
+          + "then  'NULL'  when (( cubecity.name ) ==  'X' ) then  'X-NAME'  when (( cubecity.name ) ==  'Y' )"
+          + " then  'Y-NAME'  else  'DEFAULT'  end, ( cubestate.name ), ( cubestate.id ),"
+          + " ((( cubezip.code ) !=  1 ) and ((((( cubezip.f1 ) ==  \"xyz\" ) and (((( cubezip.f2 ) >=  \"3\" )"
+          + " and (( cubezip.f2 ) !=  \"NULL\" )) and (( cubezip.f2 ) !=  \"uk\" ))) or (((( cubezip.f2 ) ==  \"adc\" )"
+          + " and (( cubezip.f1 ) ==  \"js\" )) and ((( cubecity.name ) ==  \"X\" ) or (( cubecity.name ) ==  \"Y\""
+          + " ))))"
+          + " or ((((( cubezip.f1 ) ==  \"api\" ) or (( cubezip.f1 ) ==  \"uk\" )) or ((( cubezip.f1 ) ==  \"adc\" )"
+          + " and (( cubezip.f1 ) !=  \"js\" ))) and (( cubecity.id ) ==  12 )))), ((( cubezip.code ) ==  1 ) and"
+          + " ((((( cubezip.f1 ) ==  \"xyz\" ) and (((( cubezip.f2 ) >=  \"3\" ) and (( cubezip.f2 ) !=  \"NULL\" ))"
+          + " and (( cubezip.f2 ) !=  \"uk\" ))) or (((( cubezip.f2 ) ==  \"adc\" ) and (( cubezip.f1 ) ==  \"js\" ))"
+          + " and ((( cubecity.name ) ==  \"X\" ) or (( cubecity.name ) ==  \"Y\" )))) or ((((( cubezip.f1 )==\"api\" )"
+          + " or (( cubezip.f1 ) ==  \"uk\" )) or ((( cubezip.f1 ) ==  \"adc\" ) and (( cubezip.f1 ) !=  \"js\" )))"
+          + " and (( cubecity.id ) ==  12 )))), ( cubezip.f1 ) HAVING ((sum(( testcube.msr1 )) >=  1000 ) "
           + "and (sum(( testcube.msr2 )) >=  0.01 ))",
         null, getWhereForHourly2days("c1_testfact2_raw"));
     compareQueries(hqlQuery, expected);
 
     hqlQuery =
       rewrite(
-        "SELECT citydim.name AS g1,"
-          + " CASE  WHEN citydim.name=='NULL'  THEN 'NULL' "
-          + " WHEN citydim.name=='X'  THEN 'X-NAME' "
-          + " WHEN citydim.name=='Y'  THEN 'Y-NAME' "
+        "SELECT cubecity.name AS g1,"
+          + " CASE  WHEN cubecity.name=='NULL'  THEN 'NULL' "
+          + " WHEN cubecity.name=='X'  THEN 'X-NAME' "
+          + " WHEN cubecity.name=='Y'  THEN 'Y-NAME' "
           + " ELSE 'DEFAULT'   END  AS g2, "
-          + " statedim.name AS g3,"
-          + " statedim.id AS g4, "
-          + " zipdim.code!=1  AND "
-          + " ((zipdim.f1==\"xyz\"  AND  (zipdim.f2 >= \"3\"  AND "
-          + "  zipdim.f2 !=\"NULL\"  AND  zipdim.f2 != \"uk\")) "
-          + "  OR (zipdim.f2==\"adc\"  AND  zipdim.f1==\"js\" "
-          + "  AND  ( citydim.name == \"X\"  OR  citydim.name == \"Y\" )) "
-          + " OR ((zipdim.f1==\"api\"  OR  zipdim.f1==\"uk\"  OR  (zipdim.f1==\"adc\"  AND  zipdim.f1!=\"js\"))"
-          + "  AND  citydim.id==12) ) AS g5,"
-          + " zipdim.code==1  AND "
-          + " ((zipdim.f1==\"xyz\"  AND  (zipdim.f2 >= \"3\"  AND "
-          + "  zipdim.f2 !=\"NULL\"  AND  zipdim.f2 != \"uk\")) "
-          + " OR (zipdim.f2==\"adc\"  AND  zipdim.f1==\"js\" "
-          + " AND  ( citydim.name == \"X\"  OR  citydim.name == \"Y\" )) "
-          + "  OR ((zipdim.f1==\"api\"  OR  zipdim.f1==\"uk\"  OR  (zipdim.f1==\"adc\"  AND  zipdim.f1!=\"js\"))"
-          + "    AND  citydim.id==12) ) AS g6, "
-          + "  zipdim.f1 AS g7, "
+          + " cubestate.name AS g3,"
+          + " cubestate.id AS g4, "
+          + " cubezip.code!=1  AND "
+          + " ((cubezip.f1==\"xyz\"  AND  (cubezip.f2 >= \"3\"  AND "
+          + "  cubezip.f2 !=\"NULL\"  AND  cubezip.f2 != \"uk\")) "
+          + "  OR (cubezip.f2==\"adc\"  AND  cubezip.f1==\"js\" "
+          + "  AND  ( cubecity.name == \"X\"  OR  cubecity.name == \"Y\" )) "
+          + " OR ((cubezip.f1==\"api\"  OR  cubezip.f1==\"uk\"  OR  (cubezip.f1==\"adc\"  AND  cubezip.f1!=\"js\"))"
+          + "  AND  cubecity.id==12) ) AS g5,"
+          + " cubezip.code==1  AND "
+          + " ((cubezip.f1==\"xyz\"  AND  (cubezip.f2 >= \"3\"  AND "
+          + "  cubezip.f2 !=\"NULL\"  AND  cubezip.f2 != \"uk\")) "
+          + " OR (cubezip.f2==\"adc\"  AND  cubezip.f1==\"js\" "
+          + " AND  ( cubecity.name == \"X\"  OR  cubecity.name == \"Y\" )) "
+          + "  OR ((cubezip.f1==\"api\"  OR  cubezip.f1==\"uk\"  OR  (cubezip.f1==\"adc\"  AND  cubezip.f1!=\"js\"))"
+          + "    AND  cubecity.id==12) ) AS g6, "
+          + "  cubezip.f1 AS g7, "
           + "  format_number(SUM(msr1),\"##################.###\") AS a1,"
           + "  format_number(SUM(msr2),\"##################.###\") AS a2, "
           + "  format_number(SUM(msr3),\"##################.###\") AS a3, "
@@ -972,20 +968,20 @@ public class TestCubeRewriter extends TestQueryRewrite {
           + " format_number(SUM(msr1)-(SUM(msr2)+SUM(msr3)),\"##################.###\") AS a6"
           + "  FROM testCube where "
           + TWO_DAYS_RANGE
-          + " group by citydim.name, CASE WHEN citydim.name=='NULL' THEN 'NULL'"
-          + " WHEN citydim.name=='X' THEN 'X-NAME' WHEN citydim.name=='Y' THEN 'Y-NAME'"
-          + " ELSE 'DEFAULT'   END, statedim.name, statedim.id,  zipdim.code!=1  AND"
-          + " ((zipdim.f1==\"xyz\"  AND  (zipdim.f2 >= \"3\"  AND zipdim.f2 !=\"NULL\"  AND  zipdim.f2 != \"uk\"))"
-          + " OR (zipdim.f2==\"adc\"  AND  zipdim.f1==\"js\""
-          + " AND ( citydim.name == \"X\"  OR  citydim.name == \"Y\" ))"
-          + " OR ((zipdim.f1==\"api\"  OR  zipdim.f1==\"uk\"  OR  (zipdim.f1==\"adc\"  AND  zipdim.f1!=\"js\"))"
-          + " AND  citydim.id==12) ),"
-          + " zipdim.code==1  AND  ((zipdim.f1==\"xyz\" AND ( zipdim.f2 >= \"3\"  AND zipdim.f2 !=\"NULL\""
-          + " AND  zipdim.f2 != \"uk\"))"
-          + " OR (zipdim.f2==\"adc\"  AND  zipdim.f1==\"js\""
-          + " AND  ( citydim.name == \"X\"  OR  citydim.name == \"Y\" ))"
-          + " OR ((zipdim.f1=\"api\"  OR  zipdim.f1==\"uk\" OR (zipdim.f1==\"adc\"  AND  zipdim.f1!=\"js\")) AND"
-          + " citydim.id==12))," + " zipdim.f1 " + "HAVING (SUM(msr1) >=1000)  AND (SUM(msr2)>=0.01)", conf);
+          + " group by cubecity.name, CASE WHEN cubecity.name=='NULL' THEN 'NULL'"
+          + " WHEN cubecity.name=='X' THEN 'X-NAME' WHEN cubecity.name=='Y' THEN 'Y-NAME'"
+          + " ELSE 'DEFAULT'   END, cubestate.name, cubestate.id,  cubezip.code!=1  AND"
+          + " ((cubezip.f1==\"xyz\"  AND  (cubezip.f2 >= \"3\"  AND cubezip.f2 !=\"NULL\"  AND  cubezip.f2 != \"uk\"))"
+          + " OR (cubezip.f2==\"adc\"  AND  cubezip.f1==\"js\""
+          + " AND ( cubecity.name == \"X\"  OR  cubecity.name == \"Y\" ))"
+          + " OR ((cubezip.f1==\"api\"  OR  cubezip.f1==\"uk\"  OR  (cubezip.f1==\"adc\"  AND  cubezip.f1!=\"js\"))"
+          + " AND  cubecity.id==12) ),"
+          + " cubezip.code==1  AND  ((cubezip.f1==\"xyz\" AND ( cubezip.f2 >= \"3\"  AND cubezip.f2 !=\"NULL\""
+          + " AND  cubezip.f2 != \"uk\"))"
+          + " OR (cubezip.f2==\"adc\"  AND  cubezip.f1==\"js\""
+          + " AND  ( cubecity.name == \"X\"  OR  cubecity.name == \"Y\" ))"
+          + " OR ((cubezip.f1=\"api\"  OR  cubezip.f1==\"uk\" OR (cubezip.f1==\"adc\"  AND  cubezip.f1!=\"js\")) AND"
+          + " cubecity.id==12))," + " cubezip.f1 " + "HAVING (SUM(msr1) >=1000)  AND (SUM(msr2)>=0.01)", conf);
     compareQueries(hqlQuery, expected);
   }
 
@@ -1570,18 +1566,16 @@ public class TestCubeRewriter extends TestQueryRewrite {
   public void testJoinPathColumnLifeValidation() throws Exception {
     HiveConf testConf = new HiveConf(new HiveConf(getConf(), HiveConf.class));
     testConf.setBoolean(DISABLE_AUTO_JOINS, false);
-    System.out.println("@@ Joins disabled? " + testConf.get(DISABLE_AUTO_JOINS));
     // Set column life of dim2 column in testCube
     CubeMetastoreClient client = CubeMetastoreClient.getInstance(testConf);
     Cube cube = (Cube) client.getCube(TEST_CUBE_NAME);
 
-    ReferencedDimAtrribute col = (ReferencedDimAtrribute) cube.getColumnByName("cdim2");
+    BaseDimAttribute col = (BaseDimAttribute) cube.getColumnByName("cdim2");
     assertNotNull(col);
 
-    final String query = "SELECT cycledim1.name, msr2 FROM testCube where " + TWO_DAYS_RANGE;
+    final String query = "SELECT cdimChain.name, msr2 FROM testCube where " + TWO_DAYS_RANGE;
     try {
       CubeQueryContext context = rewriteCtx(query, testConf);
-      System.out.println("TestJoinPathTimeRange: " + context.toHQL());
       fail("Expected query to fail because of invalid column life");
     } catch (LensException exc) {
       assertEquals(exc.getErrorCode(), LensCubeErrorCode.NO_JOIN_PATH.getLensErrorInfo().getErrorCode());
@@ -1595,10 +1589,9 @@ public class TestCubeRewriter extends TestQueryRewrite {
     Date oneWeekBack = DateUtils.addDays(TWODAYS_BACK, -7);
 
     // Alter cube.dim2 with an invalid column life
-    ReferencedDimAtrribute newDim2 =
-      new ReferencedDimAtrribute(new FieldSchema(col.getName(), "string", "invalid col"), col.getDisplayString(),
-        col.getReferences(), oneWeekBack, null,
-        col.getCost());
+    BaseDimAttribute newDim2 =
+      new BaseDimAttribute(new FieldSchema(col.getName(), "string", "invalid col"), col.getDisplayString(),
+        oneWeekBack, null, col.getCost(), null);
     cube.alterDimension(newDim2);
     client.alterCube(TEST_CUBE_NAME, cube);
     String hql = rewrite(query, testConf);
@@ -1678,7 +1671,7 @@ public class TestCubeRewriter extends TestQueryRewrite {
 
   @Test
   public void testSelectDimonlyJoinOnCube() throws Exception {
-    String query = "SELECT count (distinct citydim.name) from testCube where " + TWO_DAYS_RANGE;
+    String query = "SELECT count (distinct cubecity.name) from testCube where " + TWO_DAYS_RANGE;
     Configuration conf = new Configuration(getConf());
     conf.setBoolean(DISABLE_AUTO_JOINS, false);
     String hql = rewrite(query, conf);

http://git-wip-us.apache.org/repos/asf/lens/blob/908530f5/lens-cube/src/test/java/org/apache/lens/cube/parse/TestDenormalizationResolver.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestDenormalizationResolver.java b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestDenormalizationResolver.java
index af9daad..a8390ef 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestDenormalizationResolver.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestDenormalizationResolver.java
@@ -65,12 +65,12 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
         null);
     TestCubeRewriter.compareQueries(hqlQuery, expecteddim2big1);
     // with another table
-    hqlQuery = rewrite("select dim2big1, citydim.name, max(msr3)," + " msr2 from testCube" + " where "
+    hqlQuery = rewrite("select dim2big1, cubecity.name, max(msr3)," + " msr2 from testCube" + " where "
       + TWO_DAYS_RANGE_IT, conf);
     String expecteddim2big1WithAnotherTable = getExpectedQuery(cubeName,
-      "select testcube.dim2big1, citydim.name, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
-        + getDbName() + "c1_citytable citydim " + "on testcube.cityid = citydim.id and citydim.dt = 'latest' ", null,
-      " group by testcube.dim2big1, citydim.name", null,
+      "select testcube.dim2big1, cubecity.name, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
+        + getDbName() + "c1_citytable cubecity " + "on testcube.cityid = cubecity.id and cubecity.dt = 'latest' ", null,
+      " group by testcube.dim2big1, cubecity.name", null,
       getWhereForDailyAndHourly2daysWithTimeDim(cubeName, "it", "C2_summary4"),
       null);
     TestCubeRewriter.compareQueries(hqlQuery, expecteddim2big1WithAnotherTable);
@@ -84,70 +84,102 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
 
     Configuration conf2 = new Configuration(conf);
     conf2.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C2");
-    hqlQuery = rewrite("select testdim3.name, dim2big1, max(msr3)," + " msr2 from testCube" + " where "
+    hqlQuery = rewrite("select dim3chain.name, dim2big1, max(msr3), msr2 from testCube where "
       + TWO_DAYS_RANGE_IT, conf2);
     String expected =
       getExpectedQuery(cubeName,
-        "select testdim3.name, testcube.dim2big1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
-          + getDbName() + "c2_testdim2tbl3 testdim2 " + "on testcube.dim2big1 = testdim2.bigid1" + " join "
-          + getDbName() + "c2_testdim3tbl testdim3 on " + "testdim2.testdim3id = testdim3.id", null,
-        " group by testdim3.name, (testcube.dim2big1)", null,
+        "select dim3chain.name, testcube.dim2big1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
+          + getDbName() + "c2_testdim2tbl3 dim2chain " + "on testcube.dim2big1 = dim2chain.bigid1" + " join "
+          + getDbName() + "c2_testdim3tbl dim3chain on " + "dim2chain.testdim3id = dim3chain.id", null,
+        " group by dim3chain.name, (testcube.dim2big1)", null,
         getWhereForDailyAndHourly2daysWithTimeDim(cubeName, "it", "C2_summary4"),
         null);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
 
-    hqlQuery = rewrite("select dim2big1, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE_IT, conf2);
+    hqlQuery = rewrite("select dim2big1, max(msr3), msr2 from testCube where " + TWO_DAYS_RANGE_IT, conf2);
     TestCubeRewriter.compareQueries(hqlQuery, expecteddim2big1);
-    hqlQuery = rewrite("select dim2big2, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE_IT, conf2);
+    hqlQuery = rewrite("select dim2big2, max(msr3), msr2 from testCube where " + TWO_DAYS_RANGE_IT, conf2);
     TestCubeRewriter.compareQueries(hqlQuery, expecteddim2big2);
   }
 
   @Test
-  public void testDenormsWithJoins() throws ParseException, LensException, HiveException, ClassNotFoundException {
+  public void testDenormsWithJoins() throws Exception {
     // all following queries use joins to get denorm fields
     Configuration tconf = new Configuration(this.conf);
     tconf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C1");
-    String hqlQuery = rewrite("select dim2big1, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE,
+    String hqlQuery = rewrite("select dim2big1, max(msr3), msr2 from testCube where " + TWO_DAYS_RANGE,
       tconf);
     String expected =
-      getExpectedQuery(cubeName, "select testdim2.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
-          + getDbName() + "c1_testdim2tbl2 testdim2 ON testcube.dim2 = "
-          + " testdim2.id and (testdim2.dt = 'latest') ", null, "group by (testdim2.bigid1)", null,
+      getExpectedQuery(cubeName, "select dim2chain.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
+          + getDbName() + "c1_testdim2tbl2 dim2chain ON testcube.dim2 = "
+          + " dim2chain.id and (dim2chain.dt = 'latest') ", null, "group by (dim2chain.bigid1)", null,
         getWhereForDailyAndHourly2days(cubeName, "c1_summary2"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
+  }
 
-    hqlQuery =
-      rewrite("select testdim2.name, dim2big1, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE, tconf);
-    expected =
+  @Test
+  public void testDenormsWithJoinsWithChainFieldSelected() throws Exception {
+    Configuration tconf = new Configuration(this.conf);
+    tconf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C1");
+    String hqlQuery = rewrite("select dim2chain.name, dim2big1, max(msr3), msr2 from testCube where " + TWO_DAYS_RANGE,
+      tconf);
+    String expected =
       getExpectedQuery(cubeName,
-        "select testdim2.name, testdim2.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
-          + getDbName() + "c1_testdim2tbl2 testdim2 ON testcube.dim2 = "
-          + " testdim2.id and (testdim2.dt = 'latest') ", null, "group by testdim2.name, testdim2.bigid1", null,
+        "select dim2chain.name, dim2chain.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
+          + getDbName() + "c1_testdim2tbl2 dim2chain ON testcube.dim2 = "
+          + " dim2chain.id and (dim2chain.dt = 'latest') ", null, "group by dim2chain.name, dim2chain.bigid1", null,
         getWhereForDailyAndHourly2days(cubeName, "c1_summary2"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
+  }
 
-    hqlQuery =
-      rewrite("select testdim2.name, dim2big1, max(msr3)," + " msr2 from testCube left outer join testdim2"
-        + " where " + TWO_DAYS_RANGE, tconf);
-    expected =
+  @Test
+  public void testDenormsWithJoinsWithChainFieldSelectedAndJoinTypeSpecified() throws Exception {
+    Configuration tconf = new Configuration(this.conf);
+    tconf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C1");
+    tconf.set(CubeQueryConfUtil.JOIN_TYPE_KEY, "LEFTOUTER");
+    String hqlQuery = rewrite("select dim2chain.name, dim2big1, max(msr3), msr2 from testCube where " + TWO_DAYS_RANGE,
+      tconf);
+    String expected =
       getExpectedQuery(cubeName,
-        "select testdim2.name, testdim2.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " left outer JOIN "
-          + getDbName() + "c1_testdim2tbl2 testdim2 ON testcube.dim2 = "
-          + " testdim2.id and (testdim2.dt = 'latest') ", null, "group by testdim2.name, testdim2.bigid1", null,
+        "select dim2chain.name, dim2chain.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " LEFT OUTER JOIN "
+          + getDbName() + "c1_testdim2tbl2 dim2chain ON testcube.dim2 = "
+          + " dim2chain.id and (dim2chain.dt = 'latest') ", null, "group by dim2chain.name, dim2chain.bigid1", null,
         getWhereForDailyAndHourly2days(cubeName, "c1_summary2"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
+  }
 
-    hqlQuery =
-      rewrite("select testdim3.name, dim2big1, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE, tconf);
-    expected =
+  @Test
+  public void testDenormsWithJoinsWithExplicitJoinSpecified() throws Exception {
+    Configuration tconf = new Configuration(this.conf);
+    tconf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C1");
+    // With explicit join specified, automatic join resolver is disabled.
+    // thus querying denorm variables will fail
+    getLensExceptionInRewrite("select testdim2.name, dim2big1, max(msr3), msr2 from testCube left outer join testdim2"
+      + " on testcube.dim2 = testdim2.id where " + TWO_DAYS_RANGE, tconf);
+  }
+
+  @Test
+  public void testDenormsWithJoinsWithMergableChains() throws Exception {
+    Configuration tconf = new Configuration(this.conf);
+    tconf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C1");
+    String hqlQuery =
+      rewrite("select dim3chain.name, dim2big1, max(msr3), msr2 from testCube where " + TWO_DAYS_RANGE,
+        tconf);
+    String expected =
       getExpectedQuery(cubeName,
-        "select testdim3.name, testdim2.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
-          + getDbName() + "c1_testdim2tbl3 testdim2 "
-          + "on testcube.dim2 = testdim2.id AND (testdim2.dt = 'latest')" + " join " + getDbName()
-          + "c1_testdim3tbl testdim3 on " + "testdim2.testdim3id = testdim3.id AND (testdim3.dt = 'latest')",
-        null, " group by testdim3.name, (testdim2.bigid1)", null,
+        "select dim3chain.name, dim2chain.bigid1, max(testcube.msr3), sum(testcube.msr2) FROM ", " JOIN "
+          + getDbName() + "c1_testdim2tbl3 dim2chain "
+          + "on testcube.dim2 = dim2chain.id AND (dim2chain.dt = 'latest')" + " join " + getDbName()
+          + "c1_testdim3tbl dim3chain on " + "dim2chain.testdim3id = dim3chain.id AND (dim3chain.dt = 'latest')",
+        null, " group by dim3chain.name, (dim2chain.bigid1)", null,
         getWhereForDailyAndHourly2days(cubeName, "c1_summary2"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
+  }
+
+  @Test
+  public void testDenormsWithJoinsWithNoCandidateStorages() throws Exception {
+    Configuration tconf = new Configuration(this.conf);
+    tconf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "C1");
     LensException e = getLensExceptionInRewrite(
       "select dim2big2, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE, tconf);
     NoCandidateFactAvailableException ne = (NoCandidateFactAvailableException) e;
@@ -208,9 +240,9 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
     String hqlQuery = rewrite("select substrdim2big1, max(msr3)," + " msr2 from testCube" + " where " + TWO_DAYS_RANGE,
       tconf);
     String expected =
-      getExpectedQuery(cubeName, "select substr(testdim2.bigid1, 5), max(testcube.msr3), sum(testcube.msr2) FROM ",
-        " JOIN " + getDbName() + "c1_testdim2tbl2 testdim2 ON testcube.dim2 = "
-          + " testdim2.id and (testdim2.dt = 'latest') ", null, "group by substr(testdim2.bigid1, 5)", null,
+      getExpectedQuery(cubeName, "select substr(dim2chain.bigid1, 5), max(testcube.msr3), sum(testcube.msr2) FROM ",
+        " JOIN " + getDbName() + "c1_testdim2tbl2 dim2chain ON testcube.dim2 = "
+          + " dim2chain.id and (dim2chain.dt = 'latest') ", null, "group by substr(dim2chain.bigid1, 5)", null,
         getWhereForDailyAndHourly2days(cubeName, "c1_summary2"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
@@ -219,15 +251,15 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
   public void testDimensionQuery() throws Exception {
     String hqlQuery = rewrite("select citydim.name, citydim.statename from" + " citydim", conf);
 
-    String joinExpr = " join " + getDbName() + "c1_statetable statedim on"
-        + " citydim.stateid = statedim.id and (statedim.dt = 'latest')";
-    String expected = getExpectedQuery("citydim", "SELECT citydim.name, statedim.name FROM ", joinExpr, null, null,
+    String joinExpr = " join " + getDbName() + "c1_statetable citystate on"
+        + " citydim.stateid = citystate.id and (citystate.dt = 'latest')";
+    String expected = getExpectedQuery("citydim", "SELECT citydim.name, citystate.name FROM ", joinExpr, null, null,
         "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
 
     hqlQuery = rewrite("select citydim.statename, citydim.name  from" + " citydim", conf);
 
-    expected = getExpectedQuery("citydim", "SELECT statedim.name, citydim.name FROM ", joinExpr, null, null,
+    expected = getExpectedQuery("citydim", "SELECT citystate.name, citydim.name FROM ", joinExpr, null, null,
         "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
 
@@ -261,9 +293,9 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
     tConf.set(CubeQueryConfUtil.getValidStorageTablesKey("testFact2"), "C1_testFact2");
     String hqlQuery = rewrite("select test_time_dim2, msr2 from testcube where " + TWO_DAYS_RANGE, tConf);
     String expected =
-      getExpectedQuery(cubeName, "select timehourchain.full_hour, sum(testcube.msr2) FROM ", " join " + getDbName()
-        + "c4_hourDimTbl timehourchain on testcube.test_time_dim_hour_id2  = timehourchain.id", null,
-        " group by timehourchain . full_hour ", null,
+      getExpectedQuery(cubeName, "select timehourchain2.full_hour, sum(testcube.msr2) FROM ", " join " + getDbName()
+        + "c4_hourDimTbl timehourchain2 on testcube.test_time_dim_hour_id2  = timehourchain2.id", null,
+        " group by timehourchain2 . full_hour ", null,
         getWhereForHourly2days("c1_testfact2"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
@@ -276,9 +308,9 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
     tConf.set(CubeQueryConfUtil.getValidStorageTablesKey("testFact"), "C1_testFact");
     String hqlQuery = rewrite("select test_time_dim2, msr2 from testcube where " + TWO_DAYS_RANGE, tConf);
     String expected =
-      getExpectedQuery(cubeName, "select timedatechain.full_date, sum(testcube.msr2) FROM ", " join " + getDbName()
-        + "c4_dayDimTbl timedatechain on testcube.test_time_dim_day_id2  = timedatechain.id", null,
-        " group by timedatechain . full_date ", null,
+      getExpectedQuery(cubeName, "select timedatechain2.full_date, sum(testcube.msr2) FROM ", " join " + getDbName()
+        + "c4_dayDimTbl timedatechain2 on testcube.test_time_dim_day_id2  = timedatechain2.id", null,
+        " group by timedatechain2 . full_date ", null,
         getWhereForDailyAndHourly2days(cubeName, "c1_testfact"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
@@ -302,7 +334,7 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
     Configuration tConf = new Configuration(conf);
     tConf.set(CubeQueryConfUtil.DRIVER_SUPPORTED_STORAGES, "");
     CubeQueryContext cubeql = rewriteCtx("select citydim.zipcode, citydim.statename from" + " citydim", tConf);
-    Set<String> candidateDims = new HashSet<String>();
+    Set<String> candidateDims = new HashSet<>();
     for (CandidateDim cdim : cubeql.getCandidateDims().get(cubeql.getMetastoreClient().getDimension("citydim"))) {
       candidateDims.add(cdim.getName());
     }
@@ -314,10 +346,10 @@ public class TestDenormalizationResolver extends TestQueryRewrite {
   public void testDimensionQueryWithExpressionHavingDenormColumn() throws Exception {
     String hqlQuery = rewrite("select citydim.name, citydim.citystate from" + " citydim", conf);
     String joinExpr =
-      " join " + getDbName() + "c1_statetable statedim on"
-        + " citydim.stateid = statedim.id and (statedim.dt = 'latest')";
+      " join " + getDbName() + "c1_statetable citystate on"
+        + " citydim.stateid = citystate.id and (citystate.dt = 'latest')";
     String expected =
-      getExpectedQuery("citydim", "SELECT citydim.name, concat(citydim.name, \":\", statedim.name) FROM ",
+      getExpectedQuery("citydim", "SELECT citydim.name, concat(citydim.name, \":\", citystate.name) FROM ",
         joinExpr, null, null, "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }

http://git-wip-us.apache.org/repos/asf/lens/blob/908530f5/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionContext.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionContext.java b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionContext.java
index f48e1b7..669a8e9 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionContext.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionContext.java
@@ -50,7 +50,7 @@ public class TestExpressionContext extends TestQueryRewrite {
   public void testNestedExpressions() throws Exception {
     CubeQueryContext nestedExprQL = rewriteCtx("select nestedexpr from testCube where " + TWO_DAYS_RANGE, conf);
     Assert.assertNotNull(nestedExprQL.getExprCtx());
-    List<String> expectedExpressions = new ArrayList<String>();
+    List<String> expectedExpressions = new ArrayList<>();
     expectedExpressions.add("avg(( testcube . roundedmsr2 ))");
     expectedExpressions.add("avg(( testcube . equalsums ))");
     expectedExpressions.add(" case  when (( testcube . substrexpr ) =  'xyz' ) then avg(( testcube . msr5 ))"
@@ -60,14 +60,14 @@ public class TestExpressionContext extends TestQueryRewrite {
     expectedExpressions.add("avg(((( testcube . msr3 ) + ( testcube . msr2 )) /  100 ))");
     expectedExpressions.add(" case  when (substr(( testcube . dim1 ),  3 ) =  'xyz' ) then avg(( testcube . msr5 ))"
       + " when (substr(( testcube . dim1 ),  3 ) =  'abc' ) then (avg(( testcube . msr4 )) /  100 ) end ");
-    expectedExpressions.add(" case  when (substr(ascii(( testdim2 . name )),  3 ) =  'xyz' ) then"
-      + " avg(( testcube . msr5 )) when (substr(ascii(( testdim2 . name )),  3 ) =  'abc' ) then"
+    expectedExpressions.add(" case  when (substr(ascii(( dim2chain . name )),  3 ) =  'xyz' ) then"
+      + " avg(( testcube . msr5 )) when (substr(ascii(( dim2chain . name )),  3 ) =  'abc' ) then"
       + " (avg(( testcube . msr4 )) /  100 ) end ");
     expectedExpressions.add(" case  when (substr(( testcube . dim1 ),  3 ) =  'xyz' ) then avg((( testcube . msr2 )"
       + " + ( testcube . msr3 ))) when (substr(( testcube . dim1 ),  3 ) =  'abc' ) then"
       + " (avg(( testcube . msr4 )) /  100 ) end ");
-    expectedExpressions.add(" case  when (substr(ascii(( testdim2 . name )),  3 ) =  'xyz' ) then"
-      + " avg((( testcube . msr2 ) + ( testcube . msr3 ))) when (substr(ascii(( testdim2 . name )),  3 ) =  'abc' )"
+    expectedExpressions.add(" case  when (substr(ascii(( dim2chain . name )),  3 ) =  'xyz' ) then"
+      + " avg((( testcube . msr2 ) + ( testcube . msr3 ))) when (substr(ascii(( dim2chain . name )),  3 ) =  'abc' )"
       + " then (avg(( testcube . msr4 )) /  100 ) end ");
     expectedExpressions.add(" case  when (( testcube . substrexpr ) =  'xyz' ) then avg((( testcube . msr2 )"
       + " + ( testcube . msr3 ))) when (( testcube . substrexpr ) =  'abc' ) then (avg(( testcube . msr4 )) /  100 )"
@@ -75,11 +75,11 @@ public class TestExpressionContext extends TestQueryRewrite {
     expectedExpressions.add(" case  when (substr(( testcube . dim1 ),  3 ) =  'xyz' ) then avg((( testcube . msr2 )"
       + " + ( testcube . msr3 ))) when (substr(( testcube . dim1 ),  3 ) =  'abc' ) then"
       + " (avg(( testcube . msr4 )) /  100 ) end ");
-    expectedExpressions.add(" case  when (substr(ascii(( testdim2 . name )),  3 ) =  'xyz' ) then"
-      + " avg((( testcube . msr2 ) + ( testcube . msr3 ))) when (substr(ascii(( testdim2 . name )),  3 ) =  'abc' )"
+    expectedExpressions.add(" case  when (substr(ascii(( dim2chain . name )),  3 ) =  'xyz' ) then"
+      + " avg((( testcube . msr2 ) + ( testcube . msr3 ))) when (substr(ascii(( dim2chain . name )),  3 ) =  'abc' )"
       + " then (avg(( testcube . msr4 )) /  100 ) end ");
 
-    List<String> actualExpressions = new ArrayList<String>();
+    List<String> actualExpressions = new ArrayList<>();
     for (ExprSpecContext esc : nestedExprQL.getExprCtx().getExpressionContext("nestedexpr", "testcube").getAllExprs()) {
       actualExpressions.add(HQLParser.getString(esc.getFinalAST()));
     }
@@ -92,14 +92,14 @@ public class TestExpressionContext extends TestQueryRewrite {
     CubeQueryContext nestedExprQL = rewriteCtx("select nestedExprWithTimes from testCube where " + TWO_DAYS_RANGE,
       conf);
     Assert.assertNotNull(nestedExprQL.getExprCtx());
-    List<String> expectedExpressions = new ArrayList<String>();
+    List<String> expectedExpressions = new ArrayList<>();
     expectedExpressions.add("avg(( testcube . roundedmsr2 ))");
     expectedExpressions.add("avg(( testcube . equalsums ))");
     expectedExpressions.add("avg(round((( testcube . msr2 ) /  1000 )))");
     expectedExpressions.add("avg((( testcube . msr3 ) + ( testcube . msr4 )))");
     expectedExpressions.add("avg(((( testcube . msr3 ) + ( testcube . msr2 )) /  100 ))");
 
-    List<String> actualExpressions = new ArrayList<String>();
+    List<String> actualExpressions = new ArrayList<>();
     for (ExprSpecContext esc : nestedExprQL.getExprCtx()
       .getExpressionContext("nestedexprwithtimes", "testcube").getAllExprs()) {
       actualExpressions.add(HQLParser.getString(esc.getFinalAST()));

http://git-wip-us.apache.org/repos/asf/lens/blob/908530f5/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionResolver.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionResolver.java b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionResolver.java
index e77f919..6816fac 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionResolver.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestExpressionResolver.java
@@ -56,10 +56,10 @@ public class TestExpressionResolver extends TestQueryRewrite {
     Assert.assertEquals(th.getErrorCode(), LensCubeErrorCode.COLUMN_NOT_FOUND.getLensErrorInfo().getErrorCode());
 
     Assert.assertTrue(getLensExceptionErrorMessageInRewrite(
-        "select nocolexpr, SUM(msr2) from testCube" + " where " + TWO_DAYS_RANGE, conf).contains("nonexist"));
+      "select nocolexpr, SUM(msr2) from testCube" + " where " + TWO_DAYS_RANGE, conf).contains("nonexist"));
 
     Assert.assertTrue(getLensExceptionErrorMessageInRewrite(
-        "select invalidexpr, SUM(msr2) from testCube" + " where " + TWO_DAYS_RANGE, conf).contains("invalidexpr"));
+      "select invalidexpr, SUM(msr2) from testCube" + " where " + TWO_DAYS_RANGE, conf).contains("invalidexpr"));
 
     th = getLensExceptionInRewrite("select invalidexpr, " + "SUM(msr2) from testCube" + " where " + TWO_DAYS_RANGE,
         conf);
@@ -194,56 +194,30 @@ public class TestExpressionResolver extends TestQueryRewrite {
         conf);
 
     String join1 =
-      " join " + getDbName() + "c1_citytable citydim"
-        + " on testcube.cityid = citydim.id and (citydim.dt = 'latest') ";
+      " join " + getDbName() + "c1_citytable cubecity"
+        + " on testcube.cityid = cubecity.id and (cubecity.dt = 'latest') ";
     String join2 = " join" + getDbName()
-      + "c1_statetable statedim on" + " testcube.stateid = statedim.id and (statedim.dt = 'latest')";
-    String joinExpr;
+      + "c1_statetable cubestate on" + " testcube.stateid = cubestate.id and (cubestate.dt = 'latest')";
 
     String expected =
-      getExpectedQuery(cubeName, "select concat(citydim.name, \":\", statedim.name),"
+      getExpectedQuery(cubeName, "select concat(cubecity.name, \":\", cubestate.name),"
         + " avg(testcube.msr1 + testcube.msr2) FROM ", join2 + join1, null, " and substr(testcube.dim1, 3) != 'XYZ'"
-          + " group by concat(citydim.name, \":\", statedim.name)", null, getWhereForHourly2days("C1_testfact2_raw"));
+          + " group by concat(cubecity.name, \":\", cubestate.name)", null, getWhereForHourly2days("C1_testfact2_raw"));
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
   @Test
   public void testExpressionInWhereWithJoinClausePassed() throws Exception {
-
-    String hqlQuery =
-      rewrite("select cityAndState, avgmsr from testCube tc" + " join citydim cd join statedim sd " + " where "
-        + TWO_DAYS_RANGE + " and substrexpr != 'XYZ'", conf);
-
-    String join1 = " inner join " + getDbName() + "c1_citytable cd" + " on tc.cityid = cd.id and (cd.dt = 'latest')";
-    String join2 = " inner join" + getDbName() + "c1_statetable sd on" + " tc.stateid = sd.id and (sd.dt = 'latest')";
-    String expected =
-      getExpectedQuery("tc", "select concat(cd.name, \":\", sd.name)," + " avg(tc.msr1 + tc.msr2) FROM ",
-        join2 + join1, null, " and substr(tc.dim1, 3) != 'XYZ'" + " group by concat(cd.name, \":\", sd.name)", null,
-        getWhereForHourly2days("tc", "C1_testfact2_raw"));
-    TestCubeRewriter.compareQueries(hqlQuery, expected);
+    assertLensExceptionInRewrite("select cityAndState, avgmsr from testCube tc join citydim cd join statedim sd where "
+      + TWO_DAYS_RANGE + " and substrexpr != 'XYZ'", conf, LensCubeErrorCode.NO_JOIN_CONDITION_AVAILABLE);
   }
 
-  //@Test
+  @Test
   public void testExpressionInJoinClause() throws Exception {
     // expression in join clause
-    /*
-     * This is broken right now as partial join conditions
-     *  List<String> joinWhereConds = new ArrayList<String>();
-    joinWhereConds.add(StorageUtil.getWherePartClause("dt", "statedim", StorageConstants.getPartitionsForLatest()));
-    String hqlQuery =
-      rewrite("select cityAndState, avgmsr from testCube " + " join citydim on substrexpr != 'XYZ' where "
-        + TWO_DAYS_RANGE, conf);
-
-    String joinExpr =
-      "join" + getDbName() + "c1_statetable statedim on" + " testcube.stateid = statedim.id"
-        + " inner join " + getDbName() + "c1_citytable citydim" + " on testcube.cityid = citydim.id "
-        + " and substr(testcube.dim1, 3) != 'XYZ' and (citydim.dt = 'latest') ";
-    String expected =
-      getExpectedQuery(cubeName, "select concat(citydim.name, \":\", statedim.name),"
-          + " avg(testcube.msr1 + testcube.msr2) FROM ", joinExpr, null,
-        " group by concat(citydim.name, \":\", statedim.name)", joinWhereConds,
-        getWhereForHourly2days("C1_testfact2_raw"));
-    TestCubeRewriter.compareQueries(hqlQuery, expected);*/
+    assertLensExceptionInRewrite("select cityAndState, avgmsr from testCube join citydim on substrexpr != 'XYZ' where "
+      + TWO_DAYS_RANGE, conf, LensCubeErrorCode.NO_CANDIDATE_FACT_AVAILABLE);
   }
+
   @Test
   public void testExpressionInHaving() throws Exception {
     // expression with having clause
@@ -318,8 +292,9 @@ public class TestExpressionResolver extends TestQueryRewrite {
 
   @Test
   public void testExpressionFieldWithOtherFields() throws Exception {
-    // select with expression which requires dimension tables. And there is a candidate, which is removed because
-    // the other fields which require the dimension tables as expression ones, are not reachable and
+    // select with expression which requires dimension tables.
+    // And there is a candidate, which is removed because
+    // required the dimension tables in the expression are not reachable and
     // the expression is not evaluable on the candidate.
     LensException th =
       getLensExceptionInRewrite("select cityStateName, msr2expr, msr5, msr15 from testCube where "
@@ -360,15 +335,15 @@ public class TestExpressionResolver extends TestQueryRewrite {
 
     String joinExpr;
     String join1 =
-      " join " + getDbName() + "c1_ziptable zipdim on" + " citydim.zipcode = zipdim.code and (zipdim.dt = 'latest')";
-    String join2 = " join " + getDbName() + "c1_statetable statedim on"
-      + " citydim.stateid = statedim.id and (statedim.dt = 'latest')";
+      " join " + getDbName() + "c1_ziptable cityzip on" + " citydim.zipcode = cityzip.code and (cityzip.dt = 'latest')";
+    String join2 = " join " + getDbName() + "c1_statetable citystate on"
+      + " citydim.stateid = citystate.id and (citystate.dt = 'latest')";
     String join3 = " join " + getDbName()
-      + "c1_countrytable countrydim on" + " statedim.countryid = countrydim.id";
+      + "c1_countrytable citycountry on" + " citystate.countryid = citycountry.id";
     joinExpr = join2 + join3 + join1;
     String expected =
-      getExpectedQuery("citydim", "SELECT citydim.name, concat((citydim.name), \":\", (statedim.name ),"
-        + " \":\",(countrydim.name),  \":\" , ( zipdim . code )) FROM ", joinExpr, null, null, "c1_citytable", true);
+      getExpectedQuery("citydim", "SELECT citydim.name, concat((citydim.name), \":\", (citystate.name ),"
+        + " \":\",(citycountry.name),  \":\" , ( cityzip . code )) FROM ", joinExpr, null, null, "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
 
@@ -399,15 +374,15 @@ public class TestExpressionResolver extends TestQueryRewrite {
 
     String joinExpr;
     String join1 =
-      " join " + getDbName() + "c1_ziptable zipdim on" + " citydim.zipcode = zipdim.code and (zipdim.dt = 'latest')";
-    String join2 = " join " + getDbName() + "c1_statetable statedim on"
-      + " citydim.stateid = statedim.id and (statedim.dt = 'latest')";
+      " join " + getDbName() + "c1_ziptable cityzip on" + " citydim.zipcode = cityzip.code and (cityzip.dt = 'latest')";
+    String join2 = " join " + getDbName() + "c1_statetable citystate on"
+      + " citydim.stateid = citystate.id and (citystate.dt = 'latest')";
     String join3 = " join " + getDbName()
-      + "c1_countrytable countrydim on" + " statedim.countryid = countrydim.id";
+      + "c1_countrytable citycountry on" + " citystate.countryid = citycountry.id";
     joinExpr = join2 + join3 + join1;
     String expected =
-      getExpectedQuery("citydim", "SELECT citydim.name as `cname`, concat((citydim.name), \":\", (statedim.name ),"
-        + " \":\",(countrydim.name),  \":\" , ( zipdim . code )) as `caddr` FROM ", joinExpr, null, null,
+      getExpectedQuery("citydim", "SELECT citydim.name as `cname`, concat((citydim.name), \":\", (citystate.name ),"
+        + " \":\",(citycountry.name),  \":\" , ( cityzip . code )) as `caddr` FROM ", joinExpr, null, null,
         "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
@@ -418,14 +393,14 @@ public class TestExpressionResolver extends TestQueryRewrite {
 
     String joinExpr =
       ""
-        + " join " + getDbName() + "c1_statetable statedim on ct.stateid = statedim.id and (statedim.dt = 'latest')"
-        + " join " + getDbName() + "c1_countrytable countrydim on statedim.countryid = countrydim.id"
-        + " join " + getDbName() + "c1_ziptable zipdim on ct.zipcode = zipdim.code and (zipdim.dt = 'latest')"
+        + " join " + getDbName() + "c1_statetable citystate on ct.stateid = citystate.id and (citystate.dt = 'latest')"
+        + " join " + getDbName() + "c1_countrytable citycountry on citystate.countryid = citycountry.id"
+        + " join " + getDbName() + "c1_ziptable cityzip on ct.zipcode = cityzip.code and (cityzip.dt = 'latest')"
         + "";
 
     String expected =
-      getExpectedQuery("ct", "SELECT ct.name, concat((ct.name), \":\", (statedim.name ),"
-        + " \":\",(countrydim.name),  \":\" , ( zipdim . code )) FROM ", joinExpr, null, null, "c1_citytable", true);
+      getExpectedQuery("ct", "SELECT ct.name, concat((ct.name), \":\", (citystate.name ),"
+        + " \":\",(citycountry.name),  \":\" , ( cityzip . code )) FROM ", joinExpr, null, null, "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }
 
@@ -444,14 +419,13 @@ public class TestExpressionResolver extends TestQueryRewrite {
 
     String joinExpr =
       ""
-        + " join " + getDbName() + "c1_statetable statedim on ct.stateid = statedim.id and (statedim.dt = 'latest')"
-        + " join " + getDbName() + "c1_countrytable countrydim on statedim.countryid = countrydim.id"
-        + " join " + getDbName() + "c1_ziptable zipdim on ct.zipcode = zipdim.code and (zipdim.dt = 'latest')"
-        + "";
+        + " join " + getDbName() + "c1_statetable citystate on ct.stateid = citystate.id and (citystate.dt = 'latest')"
+        + " join " + getDbName() + "c1_countrytable citycountry on citystate.countryid = citycountry.id"
+        + " join " + getDbName() + "c1_ziptable cityzip on ct.zipcode = cityzip.code and (cityzip.dt = 'latest')";
 
     String expected =
-      getExpectedQuery("ct", "SELECT ct.name as `cname`, concat((ct.name), \":\", (statedim.name ),"
-        + " \":\",(countrydim.name),  \":\" , ( zipdim . code )) as `caddr` FROM ", joinExpr, null, null,
+      getExpectedQuery("ct", "SELECT ct.name as `cname`, concat((ct.name), \":\", (citystate.name ),"
+        + " \":\",(citycountry.name),  \":\" , ( cityzip . code )) as `caddr` FROM ", joinExpr, null, null,
         "c1_citytable", true);
     TestCubeRewriter.compareQueries(hqlQuery, expected);
   }