Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current database name as suffix in cube rewriter #6

Merged
1 commit merged into from May 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.apache.hadoop.hive.ql.parse.QBParseInfo;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.plan.PlanUtils;
import org.apache.hadoop.hive.ql.session.SessionState;

public class CubeQueryContext {
public static final String TIME_RANGE_FUNC = "time_range_in";
Expand Down Expand Up @@ -857,8 +858,15 @@ private Object[] getQueryTreeStrings(CandidateFact fact)
}

public String getStorageString(AbstractCubeTable tbl) {
return StringUtils.join(storageTableToQuery.get(tbl), ",") + " " +
String database = SessionState.get().getCurrentDatabase();
// Add database name suffix for default database
if (StringUtils.isNotBlank(database) && !"default".equalsIgnoreCase(database)) {
return database + "." + StringUtils.join(storageTableToQuery.get(tbl), ",") + " " +
getAliasForTabName(tbl.getName());
} else {
return StringUtils.join(storageTableToQuery.get(tbl), ",") + " " +
getAliasForTabName(tbl.getName());
}
}

private String getFromString() throws SemanticException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static String getExpectedQuery(String cubeName, String selExpr,
String rangeWhere, String storageTable) {
StringBuilder expected = new StringBuilder();
expected.append(selExpr);
expected.append(storageTable);
expected.append(getDbName() + storageTable);
expected.append(" ");
expected.append(cubeName);
expected.append(" WHERE ");
Expand Down Expand Up @@ -261,6 +261,14 @@ public static Map<String, String> getWhereForDailyAndHourly2days(String cubeName
return getWhereForDailyAndHourly2daysWithTimeDim(cubeName, "dt", storageTables);
}

public static String getDbName() {
String database = SessionState.get().getCurrentDatabase();
if (!"default".equalsIgnoreCase(database) && StringUtils.isNotBlank(database)) {
return database + ".";
}
return "";
}

public static Map<String, String> getWhereForDailyAndHourly2daysWithTimeDim(
String cubeName, String timedDimension, String... storageTables) {
return getWhereForDailyAndHourly2daysWithTimeDim(cubeName, timedDimension,
Expand All @@ -274,7 +282,7 @@ public static Map<String, String> getWhereForDailyAndHourly2daysWithTimeDim(
String whereClause = getWhereForDailyAndHourly2daysWithTimeDim(cubeName,
timedDimension, from,
to);
storageTableToWhereClause.put(StringUtils.join(storageTables, ","),
storageTableToWhereClause.put(getDbName() + StringUtils.join(storageTables, ","),
whereClause);
return storageTableToWhereClause;
}
Expand Down Expand Up @@ -355,7 +363,7 @@ public static Map<String, String> getWhereForMonthlyDailyAndHourly2months(
tables.append(storageTables[0]);
}
Collections.sort(parts);
storageTableToWhereClause.put(tables.toString(),
storageTableToWhereClause.put(getDbName() + tables.toString(),
StorageUtil.getWherePartClause("dt", TEST_CUBE_NAME, parts));
return storageTableToWhereClause;
}
Expand All @@ -367,7 +375,7 @@ public static Map<String, String> getWhereForMonthly2months(String monthlyTable)
addParts(parts, UpdatePeriod.MONTHLY,
twoMonthsBack,
DateUtil.getFloorDate(now, UpdatePeriod.MONTHLY));
storageTableToWhereClause.put(monthlyTable,
storageTableToWhereClause.put(getDbName() + monthlyTable,
StorageUtil.getWherePartClause("dt", TEST_CUBE_NAME, parts));
return storageTableToWhereClause;
}
Expand All @@ -378,7 +386,7 @@ public static Map<String, String> getWhereForHourly2days(String hourlyTable) {
List<String> parts = new ArrayList<String>();
addParts(parts, UpdatePeriod.HOURLY, twodaysBack,
DateUtil.getFloorDate(now, UpdatePeriod.HOURLY));
storageTableToWhereClause.put(hourlyTable,
storageTableToWhereClause.put(getDbName() + hourlyTable,
StorageUtil.getWherePartClause("dt", TEST_CUBE_NAME, parts));
return storageTableToWhereClause;
}
Expand Down Expand Up @@ -417,7 +425,7 @@ public static String getExpectedQuery(String dimName, String selExpr, String whe
dimName, StorageConstants.getPartitionsForLatest());
}
expected.append(selExpr);
expected.append(storageTable);
expected.append(getDbName() + storageTable);
expected.append(" ");
expected.append(dimName);
if (whereExpr != null || hasPart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/


import static org.apache.hadoop.hive.ql.cube.parse.CubeTestSetup.getDbName;
import static org.apache.hadoop.hive.ql.cube.parse.CubeTestSetup.getExpectedQuery;
import static org.apache.hadoop.hive.ql.cube.parse.CubeTestSetup.getWhereForDailyAndHourly2days;
import static org.apache.hadoop.hive.ql.cube.parse.CubeTestSetup.getWhereForDailyAndHourly2daysWithTimeDim;
Expand Down Expand Up @@ -57,11 +57,13 @@ public class TestCubeRewriter {

static CubeTestSetup setup;
static HiveConf hconf = new HiveConf(TestCubeRewriter.class);
static String dbName;
@BeforeClass
public static void setup() throws Exception {
SessionState.start(hconf);
setup = new CubeTestSetup();
setup.createSources(hconf, TestCubeRewriter.class.getSimpleName());
String dbName = TestCubeRewriter.class.getSimpleName();
setup.createSources(hconf, dbName);
}

@AfterClass
Expand Down Expand Up @@ -389,7 +391,7 @@ public void testCubeJoinQuery() throws Exception {
joinWhereConds.add(StorageUtil.getWherePartClause("dt",
"citytable", StorageConstants.getPartitionsForLatest()));
String expected = getExpectedQuery(cubeName, "select sum(testcube.msr2)" +
" FROM ", " INNER JOIN c1_citytable citytable ON" +
" FROM ", " INNER JOIN " + getDbName() + "c1_citytable citytable ON" +
" testCube.cityid = citytable.id", null, null, joinWhereConds,
getWhereForDailyAndHourly2days(cubeName, "C2_testfact"));
compareQueries(expected, hqlQuery);
Expand Down Expand Up @@ -417,10 +419,10 @@ public void testCubeJoinQuery() throws Exception {
joinWhereConds.add(StorageUtil.getWherePartClause("dt",
"citytable", StorageConstants.getPartitionsForLatest()));
expected = getExpectedQuery(cubeName, "select statetable.name," +
" sum(testcube.msr2) FROM ", "INNER JOIN c1_citytable citytable ON" +
" testCube.cityid = citytable.id LEFT OUTER JOIN c1_statetable statetable"
" sum(testcube.msr2) FROM ", "INNER JOIN " + getDbName() + "c1_citytable citytable ON" +
" testCube.cityid = citytable.id LEFT OUTER JOIN " + getDbName() + "c1_statetable statetable"
+ " ON statetable.id = citytable.stateid AND " +
"(statetable.dt = 'latest') RIGHT OUTER JOIN c1_ziptable" +
"(statetable.dt = 'latest') RIGHT OUTER JOIN " + getDbName() + "c1_ziptable" +
" ziptable ON citytable.zipcode = ziptable.code", null, " group by" +
" statetable.name ", joinWhereConds,
getWhereForDailyAndHourly2days(cubeName, "C2_testfact"));
Expand All @@ -439,10 +441,10 @@ public void testCubeJoinQuery() throws Exception {
joinWhereConds.add(StorageUtil.getWherePartClause("dt",
"ct", StorageConstants.getPartitionsForLatest()));
expected = getExpectedQuery("tc", "select st.name," +
" sum(tc.msr2) FROM ", " INNER JOIN c1_citytable ct ON" +
" tc.cityid = ct.id LEFT OUTER JOIN c1_statetable st"
" sum(tc.msr2) FROM ", " INNER JOIN " + getDbName() + "c1_citytable ct ON" +
" tc.cityid = ct.id LEFT OUTER JOIN " + getDbName() + "c1_statetable st"
+ " ON st.id = ct.stateid and (st.dt = 'latest') " +
"RIGHT OUTER JOIN c1_ziptable" +
"RIGHT OUTER JOIN " + getDbName() + "c1_ziptable" +
" zt ON ct.zipcode = zt.code", null, " group by" +
" st.name ", joinWhereConds,
getWhereForDailyAndHourly2days("tc", "C2_testfact"));
Expand All @@ -455,9 +457,9 @@ public void testCubeJoinQuery() throws Exception {
+ " left outer join ziptable on citytable.zipcode = ziptable.code"
+ " where " + twoDaysRange);
expected = getExpectedQuery(cubeName, "select citytable.name," +
" sum(testcube.msr2) FROM ", " LEFT OUTER JOIN c1_citytable citytable ON" +
" sum(testcube.msr2) FROM ", " LEFT OUTER JOIN " + getDbName() + "c1_citytable citytable ON" +
" testCube.cityid = citytable.id and (citytable.dt = 'latest') " +
" LEFT OUTER JOIN c1_ziptable" +
" LEFT OUTER JOIN " + getDbName() + "c1_ziptable" +
" ziptable ON citytable.zipcode = ziptable.code AND " +
"(ziptable.dt = 'latest')", null, " group by" +
" citytable.name ", null,
Expand All @@ -468,7 +470,7 @@ public void testCubeJoinQuery() throws Exception {
+ " join countrytable on testCube.countryid = countrytable.id"
+ " where " + twoMonthsRangeUptoMonth);
expected = getExpectedQuery(cubeName, "select sum(testcube.msr2) FROM ",
" INNER JOIN c1_countrytable countrytable ON testCube.countryid = " +
" INNER JOIN " + getDbName() + "c1_countrytable countrytable ON testCube.countryid = " +
" countrytable.id", null, null, null,
getWhereForMonthly2months("c2_testfactmonthly"));
compareQueries(expected, hqlQuery);
Expand All @@ -492,7 +494,7 @@ public void testCubeGroupbyQuery() throws Exception {
joinWhereConds.add(StorageUtil.getWherePartClause("dt",
"citytable", StorageConstants.getPartitionsForLatest()));
String expected = getExpectedQuery(cubeName, "select citytable.name," +
" sum(testcube.msr2) FROM ", "INNER JOIN c1_citytable citytable ON" +
" sum(testcube.msr2) FROM ", "INNER JOIN " + getDbName() + "c1_citytable citytable ON" +
" testCube.cityid = citytable.id", null, " group by citytable.name ",
joinWhereConds, getWhereForDailyAndHourly2days(cubeName, "C2_testfact"));
compareQueries(expected, hqlQuery);
Expand Down Expand Up @@ -616,9 +618,9 @@ public void testCubeGroupbyQuery() throws Exception {
+ " ON statetable.id = citytable.stateid " +
"INNER JOIN c1_ziptable" +
" ziptable ON citytable.zipcode = ziptable.code";
String actualExpr = "join c1_statetable statetable on testcube.stateid = statetable.id and (statetable.dt = 'latest')" +
" join c1_ziptable ziptable on testcube.zipcode = ziptable.zipcode and (ziptable.dt = 'latest') " +
"join c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')";
String actualExpr = "join " + getDbName() + "c1_statetable statetable on testcube.stateid = statetable.id and (statetable.dt = 'latest')" +
" join " + getDbName() + "c1_ziptable ziptable on testcube.zipcode = ziptable.zipcode and (ziptable.dt = 'latest') " +
"join " + getDbName() + "c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')";
expected = getExpectedQuery(cubeName, "SELECT ( citytable . name ) g1 ," +
" case when (( citytable . name ) == 'NULL' ) then 'NULL' when (( citytable . name ) == 'X' )" +
" then 'X-NAME' when (( citytable . name ) == 'Y' ) then 'Y-NAME'" +
Expand Down Expand Up @@ -1061,7 +1063,7 @@ public void testColumnAmbiguity() throws Exception {
" statetable.countryid = countrytable.id";
try {
String hql = rewrite(driver, q2);
Assert.assertTrue("Should not reach here: " + hql, false);
Assert.fail("Should not reach here: " + hql);
} catch (SemanticException exc) {
Assert.assertNotNull(exc);
exc.printStackTrace();
Expand Down
Loading