@Test public void testReplaceDBName() throws Exception { File jarDir = new File("target/testjars"); File testJarFile = new File(jarDir, "test.jar"); File serdeJarFile = new File(jarDir, "serde.jar"); URL[] serdeUrls = new URL[2]; serdeUrls[0] = new URL("file:" + testJarFile.getAbsolutePath()); serdeUrls[1] = new URL("file:" + serdeJarFile.getAbsolutePath()); URLClassLoader createTableClassLoader = new URLClassLoader(serdeUrls, hconf.getClassLoader()); hconf.setClassLoader(createTableClassLoader); SessionState.start(hconf); Database database = new Database(); database.setName("mydb"); Hive.get(hconf).createDatabase(database); SessionState.get().setCurrentDatabase("mydb"); createTable(hconf, "mydb", "mytable", "testDB", "testTable_1"); String query = "SELECT * FROM mydb.mytable t1 WHERE A = 100"; DruidSQLRewriter rewriter = new DruidSQLRewriter(); rewriter.init(conf); rewriter.ast = HQLParser.parseHQL(query, hconf); rewriter.query = query; rewriter.analyzeInternal(conf, hconf); String joinTreeBeforeRewrite = HQLParser.getString(rewriter.fromAST); System.out.println(joinTreeBeforeRewrite); rewriter.replaceWithUnderlyingStorage(hconf); String joinTreeAfterRewrite = HQLParser.getString(rewriter.fromAST); System.out.println("joinTreeAfterRewrite:" + joinTreeAfterRewrite); assertTrue(joinTreeBeforeRewrite.contains("mydb")); assertTrue(joinTreeBeforeRewrite.contains("mytable")); assertFalse(joinTreeAfterRewrite.contains("mydb")); assertFalse(joinTreeAfterRewrite.contains("mytable")); assertTrue(joinTreeAfterRewrite.contains("testdb")); assertTrue(joinTreeAfterRewrite.contains("testtable_1")); createTable(hconf, "mydb", "mytable_2", null, null); String query2 = "SELECT * FROM mydb.mytable_2 WHERE a = 100"; rewriter.ast = HQLParser.parseHQL(query2, hconf); rewriter.query = query2; rewriter.analyzeInternal(conf, hconf); joinTreeBeforeRewrite = HQLParser.getString(rewriter.fromAST); System.out.println(joinTreeBeforeRewrite); rewriter.replaceWithUnderlyingStorage(hconf); joinTreeAfterRewrite = HQLParser.getString(rewriter.fromAST); System.out.println(joinTreeAfterRewrite); assertEquals(joinTreeAfterRewrite, joinTreeBeforeRewrite); Hive.get().dropTable("mydb", "mytable"); database = new Database(); database.setName("examples"); Hive.get().createDatabase(database); createTable(hconf, "examples", "mytable", "default", null); String defaultQuery = "SELECT * FROM examples.mytable t1 WHERE A = 100"; rewriter.ast = HQLParser.parseHQL(defaultQuery, hconf); rewriter.query = defaultQuery; rewriter.analyzeInternal(conf, hconf); joinTreeBeforeRewrite = HQLParser.getString(rewriter.fromAST); rewriter.replaceWithUnderlyingStorage(hconf); joinTreeAfterRewrite = HQLParser.getString(rewriter.fromAST); assertTrue(joinTreeBeforeRewrite.contains("examples"), joinTreeBeforeRewrite); assertFalse(joinTreeAfterRewrite.contains("examples"), joinTreeAfterRewrite); System.out.println("default case: " + joinTreeAfterRewrite); Hive.get().dropTable("mydb", "mytable"); Hive.get().dropTable("mydb", "mytable_2"); Hive.get().dropTable("examples", "mytable"); Hive.get().dropDatabase("mydb", true, true, true); Hive.get().dropDatabase("examples", true, true, true); SessionState.get().setCurrentDatabase("default"); }
public void analyzeInternal(Configuration conf, HiveConf hconf) throws SemanticException { CubeSemanticAnalyzer c1 = new CubeSemanticAnalyzer(conf, hconf); QB qb = new QB(null, null, false); if (!c1.doPhase1(ast, qb, c1.initPhase1Ctx(), null)) { return; } if (!qb.getSubqAliases().isEmpty()) { log.warn("Subqueries in from clause is not supported by {} Query : {}", this, this.query); throw new SemanticException("Subqueries in from clause is not supported by " + this + " Query : " + this.query); } TreeSet<String> ks = new TreeSet<String>(qb.getParseInfo().getClauseNames()); String clauseName = ks.first(); if (qb.getParseInfo().getJoinExpr() != null) { log.warn("Join queries not supported by {} Query : {}", this, this.query); throw new SemanticException("Join queries not supported by " + this + " Query : " + this.query); } if (qb.getParseInfo().getWhrForClause(clauseName) != null) { this.whereAST = qb.getParseInfo().getWhrForClause(clauseName); } if (qb.getParseInfo().getGroupByForClause(clauseName) != null) { this.groupByAST = qb.getParseInfo().getGroupByForClause(clauseName); } if (qb.getParseInfo().getSelForClause(clauseName) != null) { this.selectAST = qb.getParseInfo().getSelForClause(clauseName); } if (qb.getParseInfo().getHavingForClause(clauseName) != null) { this.havingAST = qb.getParseInfo().getHavingForClause(clauseName); } if (qb.getParseInfo().getOrderByForClause(clauseName) != null) { this.orderByAST = qb.getParseInfo().getOrderByForClause(clauseName); } this.fromAST = HQLParser.findNodeByPath(ast, TOK_FROM); }
DruidSQLRewriter extends ColumnarSQLRewriter { public void analyzeInternal(Configuration conf, HiveConf hconf) throws SemanticException { CubeSemanticAnalyzer c1 = new CubeSemanticAnalyzer(conf, hconf); QB qb = new QB(null, null, false); if (!c1.doPhase1(ast, qb, c1.initPhase1Ctx(), null)) { return; } if (!qb.getSubqAliases().isEmpty()) { log.warn("Subqueries in from clause is not supported by {} Query : {}", this, this.query); throw new SemanticException("Subqueries in from clause is not supported by " + this + " Query : " + this.query); } TreeSet<String> ks = new TreeSet<String>(qb.getParseInfo().getClauseNames()); String clauseName = ks.first(); if (qb.getParseInfo().getJoinExpr() != null) { log.warn("Join queries not supported by {} Query : {}", this, this.query); throw new SemanticException("Join queries not supported by " + this + " Query : " + this.query); } if (qb.getParseInfo().getWhrForClause(clauseName) != null) { this.whereAST = qb.getParseInfo().getWhrForClause(clauseName); } if (qb.getParseInfo().getGroupByForClause(clauseName) != null) { this.groupByAST = qb.getParseInfo().getGroupByForClause(clauseName); } if (qb.getParseInfo().getSelForClause(clauseName) != null) { this.selectAST = qb.getParseInfo().getSelForClause(clauseName); } if (qb.getParseInfo().getHavingForClause(clauseName) != null) { this.havingAST = qb.getParseInfo().getHavingForClause(clauseName); } if (qb.getParseInfo().getOrderByForClause(clauseName) != null) { this.orderByAST = qb.getParseInfo().getOrderByForClause(clauseName); } this.fromAST = HQLParser.findNodeByPath(ast, TOK_FROM); } }
DruidSQLRewriter extends ColumnarSQLRewriter { public void analyzeInternal(Configuration conf, HiveConf hconf) throws SemanticException { CubeSemanticAnalyzer c1 = new CubeSemanticAnalyzer(conf, hconf); QB qb = new QB(null, null, false); if (!c1.doPhase1(ast, qb, c1.initPhase1Ctx(), null)) { return; } if (!qb.getSubqAliases().isEmpty()) { log.warn("Subqueries in from clause is not supported by {} Query : {}", this, this.query); throw new SemanticException("Subqueries in from clause is not supported by " + this + " Query : " + this.query); } TreeSet<String> ks = new TreeSet<String>(qb.getParseInfo().getClauseNames()); String clauseName = ks.first(); if (qb.getParseInfo().getJoinExpr() != null) { log.warn("Join queries not supported by {} Query : {}", this, this.query); throw new SemanticException("Join queries not supported by " + this + " Query : " + this.query); } if (qb.getParseInfo().getWhrForClause(clauseName) != null) { this.whereAST = qb.getParseInfo().getWhrForClause(clauseName); } if (qb.getParseInfo().getGroupByForClause(clauseName) != null) { this.groupByAST = qb.getParseInfo().getGroupByForClause(clauseName); } if (qb.getParseInfo().getSelForClause(clauseName) != null) { this.selectAST = qb.getParseInfo().getSelForClause(clauseName); } if (qb.getParseInfo().getHavingForClause(clauseName) != null) { this.havingAST = qb.getParseInfo().getHavingForClause(clauseName); } if (qb.getParseInfo().getOrderByForClause(clauseName) != null) { this.orderByAST = qb.getParseInfo().getOrderByForClause(clauseName); } this.fromAST = HQLParser.findNodeByPath(ast, TOK_FROM); }  }
DruidSQLRewriter extends ColumnarSQLRewriter { public void analyzeInternal(Configuration conf, HiveConf hconf) throws SemanticException { CubeSemanticAnalyzer c1 = new CubeSemanticAnalyzer(conf, hconf); QB qb = new QB(null, null, false); if (!c1.doPhase1(ast, qb, c1.initPhase1Ctx(), null)) { return; } if (!qb.getSubqAliases().isEmpty()) { log.warn("Subqueries in from clause is not supported by {} Query : {}", this, this.query); throw new SemanticException("Subqueries in from clause is not supported by " + this + " Query : " + this.query); } TreeSet<String> ks = new TreeSet<String>(qb.getParseInfo().getClauseNames()); String clauseName = ks.first(); if (qb.getParseInfo().getJoinExpr() != null) { log.warn("Join queries not supported by {} Query : {}", this, this.query); throw new SemanticException("Join queries not supported by " + this + " Query : " + this.query); } if (qb.getParseInfo().getWhrForClause(clauseName) != null) { this.whereAST = qb.getParseInfo().getWhrForClause(clauseName); } if (qb.getParseInfo().getGroupByForClause(clauseName) != null) { this.groupByAST = qb.getParseInfo().getGroupByForClause(clauseName); } if (qb.getParseInfo().getSelForClause(clauseName) != null) { this.selectAST = qb.getParseInfo().getSelForClause(clauseName); } if (qb.getParseInfo().getHavingForClause(clauseName) != null) { this.havingAST = qb.getParseInfo().getHavingForClause(clauseName); } if (qb.getParseInfo().getOrderByForClause(clauseName) != null) { this.orderByAST = qb.getParseInfo().getOrderByForClause(clauseName); } this.fromAST = HQLParser.findNodeByPath(ast, TOK_FROM); }  void analyzeInternal(Configuration conf, HiveConf hconf); void buildDruidQuery(Configuration conf, HiveConf hconf); @Override String rewrite(String query, Configuration conf, HiveConf metastoreConf); String rewrite(ASTNode currNode, Configuration conf, HiveConf metastoreConf, boolean resolveNativeTables); }
DruidSQLRewriter extends ColumnarSQLRewriter { public void analyzeInternal(Configuration conf, HiveConf hconf) throws SemanticException { CubeSemanticAnalyzer c1 = new CubeSemanticAnalyzer(conf, hconf); QB qb = new QB(null, null, false); if (!c1.doPhase1(ast, qb, c1.initPhase1Ctx(), null)) { return; } if (!qb.getSubqAliases().isEmpty()) { log.warn("Subqueries in from clause is not supported by {} Query : {}", this, this.query); throw new SemanticException("Subqueries in from clause is not supported by " + this + " Query : " + this.query); } TreeSet<String> ks = new TreeSet<String>(qb.getParseInfo().getClauseNames()); String clauseName = ks.first(); if (qb.getParseInfo().getJoinExpr() != null) { log.warn("Join queries not supported by {} Query : {}", this, this.query); throw new SemanticException("Join queries not supported by " + this + " Query : " + this.query); } if (qb.getParseInfo().getWhrForClause(clauseName) != null) { this.whereAST = qb.getParseInfo().getWhrForClause(clauseName); } if (qb.getParseInfo().getGroupByForClause(clauseName) != null) { this.groupByAST = qb.getParseInfo().getGroupByForClause(clauseName); } if (qb.getParseInfo().getSelForClause(clauseName) != null) { this.selectAST = qb.getParseInfo().getSelForClause(clauseName); } if (qb.getParseInfo().getHavingForClause(clauseName) != null) { this.havingAST = qb.getParseInfo().getHavingForClause(clauseName); } if (qb.getParseInfo().getOrderByForClause(clauseName) != null) { this.orderByAST = qb.getParseInfo().getOrderByForClause(clauseName); } this.fromAST = HQLParser.findNodeByPath(ast, TOK_FROM); }  void analyzeInternal(Configuration conf, HiveConf hconf); void buildDruidQuery(Configuration conf, HiveConf hconf); @Override String rewrite(String query, Configuration conf, HiveConf metastoreConf); String rewrite(ASTNode currNode, Configuration conf, HiveConf metastoreConf, boolean resolveNativeTables);  }