diff --git a/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java b/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java index 67ca571761..ca4f3872f5 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/cube/parse/CubeQueryContext.java @@ -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"; @@ -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 { diff --git a/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java b/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java index 3f2856ce19..49ca34ca33 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/CubeTestSetup.java @@ -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 "); @@ -261,6 +261,14 @@ public static Map 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 getWhereForDailyAndHourly2daysWithTimeDim( String cubeName, String timedDimension, String... storageTables) { return getWhereForDailyAndHourly2daysWithTimeDim(cubeName, timedDimension, @@ -274,7 +282,7 @@ public static Map getWhereForDailyAndHourly2daysWithTimeDim( String whereClause = getWhereForDailyAndHourly2daysWithTimeDim(cubeName, timedDimension, from, to); - storageTableToWhereClause.put(StringUtils.join(storageTables, ","), + storageTableToWhereClause.put(getDbName() + StringUtils.join(storageTables, ","), whereClause); return storageTableToWhereClause; } @@ -355,7 +363,7 @@ public static Map 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; } @@ -367,7 +375,7 @@ public static Map 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; } @@ -378,7 +386,7 @@ public static Map getWhereForHourly2days(String hourlyTable) { List parts = new ArrayList(); 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; } @@ -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) { diff --git a/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestCubeRewriter.java b/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestCubeRewriter.java index a908ceddc7..134f1e4588 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestCubeRewriter.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestCubeRewriter.java @@ -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; @@ -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 @@ -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); @@ -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")); @@ -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")); @@ -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, @@ -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); @@ -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); @@ -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'" + @@ -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(); diff --git a/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestJoinResolver.java b/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestJoinResolver.java index 03155c1fe5..af3670ad51 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestJoinResolver.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/cube/parse/TestJoinResolver.java @@ -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.getDateUptoHours; import static org.apache.hadoop.hive.ql.cube.parse.CubeTestSetup.now; import static org.apache.hadoop.hive.ql.cube.parse.CubeTestSetup.rewrite; @@ -177,10 +177,10 @@ public void testAutoJoinResolver() throws Exception { System.out.println("testAutoJoinResolverauto join HQL:" + hql); System.out.println("testAutoJoinResolver@@Resolved join chain:[" + rewrittenQuery.getAutoResolvedJoinChain()+ "]"); Set expectedClauses = new HashSet(); - expectedClauses.add("c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')"); - expectedClauses.add("c1_testdim2 testdim2 on testcube.dim2 = testdim2.id and (testdim2.dt = 'latest')"); - expectedClauses.add("c1_testdim3 testdim3 on testdim2.testdim3id = testdim3.id and (testdim3.dt = 'latest')"); - expectedClauses.add("c1_testdim4 testdim4 on testdim3.testdim4id = testdim4.id and (testdim4.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim2 testdim2 on testcube.dim2 = testdim2.id and (testdim2.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim3 testdim3 on testdim2.testdim3id = testdim3.id and (testdim3.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim4 testdim4 on testdim3.testdim4id = testdim4.id and (testdim4.dt = 'latest')"); Set actualClauses = new HashSet(); for (String clause : StringUtils.splitByWholeSeparator(rewrittenQuery.getAutoResolvedJoinChain(), "join")) { @@ -201,8 +201,8 @@ public void testAutoJoinResolver() throws Exception { hql = rewrittenQuery.toHQL(); System.out.println("testAutoJoinResolverauto join HQL:" + hql); System.out.println("testAutoJoinResolver@@Resolved join chain:[" + rewrittenQuery.getAutoResolvedJoinChain()+ "]"); - expectedClauses.add("c1_testdim3 testdim3 on testdim2.testdim3id = testdim3.id and (testdim3.dt = 'latest')"); - expectedClauses.add("c1_testdim4 testdim4 on testdim3.testdim4id = testdim4.id and (testdim4.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim3 testdim3 on testdim2.testdim3id = testdim3.id and (testdim3.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim4 testdim4 on testdim3.testdim4id = testdim4.id and (testdim4.dt = 'latest')"); for (String clause : StringUtils.splitByWholeSeparator(rewrittenQuery.getAutoResolvedJoinChain(), "join")) { if (StringUtils.isNotBlank(clause)) { actualClauses.add(clause.trim()); @@ -233,10 +233,10 @@ public void testPartialJoinResolver() throws Exception { String resolvedClause = rewrittenQuery.getAutoResolvedJoinChain(); System.out.println("@@resolved join chain " + resolvedClause); Set expectedClauses = new HashSet(); - expectedClauses.add("c1_citytable citytable on testcube.cityid = citytable.id and ((( citytable . name ) = 'FOOBAR' )) and (citytable.dt = 'latest')"); - expectedClauses.add("c1_testdim4 testdim4 on testdim3.testdim4id = testdim4.id and ((( testdim4 . name ) = 'TESTDIM4NAME' )) and (testdim4.dt = 'latest')"); - expectedClauses.add("c1_testdim3 testdim3 on testdim2.testdim3id = testdim3.id and (testdim3.dt = 'latest')"); - expectedClauses.add("c1_testdim2 testdim2 on testcube.dim2 = testdim2.id and (testdim2.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_citytable citytable on testcube.cityid = citytable.id and ((( citytable . name ) = 'FOOBAR' )) and (citytable.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim4 testdim4 on testdim3.testdim4id = testdim4.id and ((( testdim4 . name ) = 'TESTDIM4NAME' )) and (testdim4.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim3 testdim3 on testdim2.testdim3id = testdim3.id and (testdim3.dt = 'latest')"); + expectedClauses.add(getDbName() + "c1_testdim2 testdim2 on testcube.dim2 = testdim2.id and (testdim2.dt = 'latest')"); Set actualClauses = new HashSet(); for (String clause : StringUtils.splitByWholeSeparator(rewrittenQuery.getAutoResolvedJoinChain(), "join")) { @@ -266,7 +266,7 @@ public void testJoinWithoutCondition() throws Exception { String hql = ctx.toHQL(); String joinClause = ctx.getAutoResolvedJoinChain(); System.out.println("@Resolved join clause " + joinClause); - assertEquals("join c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')", joinClause.trim()); + assertEquals("join " + getDbName() + "c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')", joinClause.trim()); } @Test @@ -278,7 +278,7 @@ public void testJoinTypeConf() throws Exception { CubeQueryContext ctx = driver.rewrite(query); String hql = ctx.toHQL(); System.out.println("testJoinTypeConf@@Resolved join clause1 - " + ctx.getAutoResolvedJoinChain()); - assertEquals("left outer join c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')", + assertEquals("left outer join " + getDbName() + "c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')", ctx.getAutoResolvedJoinChain().trim()); hconf.set(CubeQueryConfUtil.JOIN_TYPE_KEY, "FULLOUTER"); @@ -287,7 +287,7 @@ public void testJoinTypeConf() throws Exception { ctx = driver.rewrite(query); hql = ctx.toHQL(); System.out.println("testJoinTypeConf@@Resolved join clause2 - "+ ctx.getAutoResolvedJoinChain()); - assertEquals("full outer join c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')", + assertEquals("full outer join " + getDbName() + "c1_citytable citytable on testcube.cityid = citytable.id and (citytable.dt = 'latest')", ctx.getAutoResolvedJoinChain().trim()); } @@ -301,7 +301,7 @@ public void testPreserveTableAlias() throws Exception { System.out.println("testPreserveTableAlias@@HQL:" + hql); System.out.println("testPreserveTableAlias@@Resolved join clause - " + ctx.getAutoResolvedJoinChain()); // Check that aliases are preserved in the join clause - assertEquals("left outer join c1_citytable c on t.cityid = c.id and (c.dt = 'latest')", ctx.getAutoResolvedJoinChain().trim()); + assertEquals("left outer join "+ getDbName() + "c1_citytable c on t.cityid = c.id and (c.dt = 'latest')", ctx.getAutoResolvedJoinChain().trim()); String whereClause = hql.substring(hql.indexOf("WHERE")); // Check that the partition condition is not added again in where clause assertFalse(whereClause.contains("c.dt = 'latest'")); @@ -318,7 +318,7 @@ public void testDimOnlyQuery() throws Exception { assertTrue(hql.contains("WHERE (citytable.dt = 'latest') LIMIT 10")); System.out.println("testDimOnlyQuery@@@HQL:" + hql); System.out.println("testDimOnlyQuery@@@Resolved join clause: " + ctx.getAutoResolvedJoinChain()); - assertEquals("left outer join c1_statetable statetable on citytable.stateid = statetable.id and (statetable.dt = 'latest')", + assertEquals("left outer join "+ getDbName() + "c1_statetable statetable on citytable.stateid = statetable.id and (statetable.dt = 'latest')", ctx.getAutoResolvedJoinChain().trim()); String queryWithJoin = "select citytable.name, statetable.name from citytable join statetable"; @@ -339,7 +339,7 @@ public void testStorageFilterPushdown() throws Exception { String hql = context.toHQL(); System.out.println("##1 hql " + hql); System.out.println("##1 " + context.getAutoResolvedJoinChain()); - assertEquals("left outer join c1_statetable statetable on citytable.stateid = statetable.id" + + assertEquals("left outer join " + getDbName() + "c1_statetable statetable on citytable.stateid = statetable.id" + " and (statetable.dt = 'latest')", context.getAutoResolvedJoinChain().trim()); assertTrue(hql.contains("WHERE (citytable.dt = 'latest')")); @@ -349,7 +349,7 @@ public void testStorageFilterPushdown() throws Exception { hql = context.toHQL(); System.out.println("##2 hql " + hql); System.out.println("##2 " + context.getAutoResolvedJoinChain()); - assertEquals("right outer join c1_statetable statetable on citytable.stateid = statetable.id " + + assertEquals("right outer join " + getDbName() + "c1_statetable statetable on citytable.stateid = statetable.id " + "and (citytable.dt = 'latest')", context.getAutoResolvedJoinChain().trim()); assertTrue(hql.contains("WHERE (statetable.dt = 'latest')")); @@ -359,7 +359,7 @@ public void testStorageFilterPushdown() throws Exception { hql = context.toHQL(); System.out.println("##3 hql " + hql); System.out.println("##3 " + context.getAutoResolvedJoinChain()); - assertEquals("full outer join c1_statetable statetable on citytable.stateid = statetable.id " + + assertEquals("full outer join " + getDbName() + "c1_statetable statetable on citytable.stateid = statetable.id " + "and (citytable.dt = 'latest' and statetable.dt = 'latest')", context.getAutoResolvedJoinChain().trim()); assertTrue(!hql.contains("WHERE")); }