From 4899e33200ec116816d9e4a6c97c5e8c17a4ea5d Mon Sep 17 00:00:00 2001 From: Mahesh Kumar Behera Date: Tue, 20 Feb 2018 15:10:07 +0530 Subject: [PATCH] HIVE-18754 : REPL STATUS should support 'with' clause --- .../org/apache/hadoop/hive/ql/parse/HiveParser.g | 5 +-- .../hive/ql/parse/ReplicationSemanticAnalyzer.java | 42 ++++++++++++++++++---- .../ql/parse/TestReplicationSemanticAnalyzer.java | 33 +++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g b/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g index e431271d3a..733ec79ce1 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g @@ -892,11 +892,12 @@ replConfigsList ; replStatusStatement -@init { pushMsg("replication load statement", state); } +@init { pushMsg("replication status statement", state); } @after { popMsg(state); } : KW_REPL KW_STATUS (dbName=identifier) (DOT tblName=identifier)? - -> ^(TOK_REPL_STATUS $dbName $tblName?) + (KW_WITH replConf=replConfigs)? + -> ^(TOK_REPL_STATUS $dbName ^(TOK_TABNAME $tblName)? $replConf?) ; ddlStatement diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/ReplicationSemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/ReplicationSemanticAnalyzer.java index c38f7dc49f..796ab0d5e5 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/ReplicationSemanticAnalyzer.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/ReplicationSemanticAnalyzer.java @@ -48,6 +48,7 @@ import org.apache.hadoop.hive.ql.plan.PlanUtils; import org.apache.hadoop.hive.ql.session.SessionState; import org.apache.hadoop.hive.ql.stats.StatsUtils; +import org.apache.hadoop.hive.ql.metadata.Hive; import java.io.FileNotFoundException; import java.io.Serializable; @@ -81,6 +82,9 @@ // of any other queries running in the session private HiveConf conf; + // This will be set to true only if repl-status is fired with "WITH" keyword + private boolean needNewdb; + private static String testInjectDumpDir = null; // unit tests can overwrite this to affect default dump behaviour private static final String dumpSchema = "dump_dir,last_repl_id#string,string"; @@ -555,11 +559,29 @@ private void analyzeReplLoad(ASTNode ast) throws SemanticException { } // REPL STATUS - private void initReplStatus(ASTNode ast) { - int numChildren = ast.getChildCount(); + private void initReplStatus(ASTNode ast) throws SemanticException{ + needNewdb = false; dbNameOrPattern = PlanUtils.stripQuotes(ast.getChild(0).getText()); - if (numChildren > 1) { - tblNameOrPattern = PlanUtils.stripQuotes(ast.getChild(1).getText()); + int numChildren = ast.getChildCount(); + for (int i = 1; i < numChildren; i++) { + ASTNode childNode = (ASTNode) ast.getChild(i); + switch (childNode.getToken().getType()) { + case TOK_TABNAME: + tblNameOrPattern = PlanUtils.stripQuotes(childNode.getChild(0).getText()); + break; + case TOK_REPL_CONFIG: + Map replConfigs + = DDLSemanticAnalyzer.getProps((ASTNode) childNode.getChild(0)); + if (null != replConfigs) { + for (Map.Entry config : replConfigs.entrySet()) { + conf.set(config.getKey(), config.getValue()); + } + } + needNewdb = true; + break; + default: + throw new SemanticException("Unrecognized token in REPL STATUS statement"); + } } } @@ -570,9 +592,16 @@ private void analyzeReplStatus(ASTNode ast) throws SemanticException { String replLastId = null; try { + Hive newDb; + if (needNewdb) { + newDb = Hive.get(conf, false); + } else { + newDb = db; + } + if (tblNameOrPattern != null) { // Checking for status of table - Table tbl = db.getTable(dbNameOrPattern, tblNameOrPattern); + Table tbl = newDb.getTable(dbNameOrPattern, tblNameOrPattern); if (tbl != null) { inputs.add(new ReadEntity(tbl)); Map params = tbl.getParameters(); @@ -582,7 +611,8 @@ private void analyzeReplStatus(ASTNode ast) throws SemanticException { } } else { // Checking for status of a db - Database database = db.getDatabase(dbNameOrPattern); + + Database database = newDb.getDatabase(dbNameOrPattern); if (database != null) { inputs.add(new ReadEntity(database)); Map params = database.getParameters(); diff --git a/ql/src/test/org/apache/hadoop/hive/ql/parse/TestReplicationSemanticAnalyzer.java b/ql/src/test/org/apache/hadoop/hive/ql/parse/TestReplicationSemanticAnalyzer.java index 96e3fca899..a034723faf 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/parse/TestReplicationSemanticAnalyzer.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/parse/TestReplicationSemanticAnalyzer.java @@ -264,4 +264,37 @@ private void assertTargetDatabaseName(ASTNode root) { assertEquals(0, child.getChildCount()); } } + + public static class ReplStatus { + + @Test + public void parseTargetDbName() throws ParseException { + ASTNode root = parse("repl status targetTestDbName"); + assertTargetDatabaseName(root); + } + + @Test + public void parseWithClause() throws ParseException { + ASTNode root = parse("repl status targetTestDbName with" + + "('hive.metastore.uris'='thrift://localhost:12341')"); + assertTargetDatabaseName(root); + + ASTNode child = (ASTNode) root.getChild(1); + assertEquals("TOK_REPL_CONFIG", child.getText()); + assertEquals(1, child.getChildCount()); + child = (ASTNode) child.getChild(0); + assertEquals("TOK_REPL_CONFIG_LIST", child.getText()); + ASTNode configNode = (ASTNode) child.getChild(0); + assertEquals("TOK_TABLEPROPERTY", configNode.getText()); + assertEquals(2, configNode.getChildCount()); + assertEquals("'hive.metastore.uris'", configNode.getChild(0).getText()); + assertEquals("'thrift://localhost:12341'", configNode.getChild(1).getText()); + } + + private void assertTargetDatabaseName(ASTNode root) { + ASTNode child = (ASTNode) root.getChild(0); + assertEquals("targetTestDbName", child.getText()); + assertEquals(0, child.getChildCount()); + } + } } \ No newline at end of file -- 2.14.3 (Apple Git-98)