rbalamohan commented on a change in pull request #2033:
URL: https://github.com/apache/hive/pull/2033#discussion_r621898378



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java
##########
@@ -422,6 +430,92 @@ private JSONObject getLocks(PrintStream out, ExplainWork 
work) {
     return jsonObject;
   }
 
+  public void addCreateTableStatement(Table table, List<String> 
tableCreateStmt , DDLPlanUtils ddlPlanUtils){
+    tableCreateStmt.add(ddlPlanUtils.getCreateTableCommand(table, false) + 
";");
+  }
+  
+  public void addPKandBasicStats(Table tbl, List<String> basicDef, 
DDLPlanUtils ddlPlanUtils){
+    String primaryKeyStmt = 
ddlPlanUtils.getAlterTableStmtPrimaryKeyConstraint(tbl.getPrimaryKeyInfo());
+    if (primaryKeyStmt != null) {
+      basicDef.add(primaryKeyStmt);
+    }
+    basicDef.add(ddlPlanUtils.getAlterTableStmtTableStatsBasic(tbl));
+  }
+
+  public void addConstraints(Table tbl, List<String> constraints, Set<String> 
allTableNames,
+      DDLPlanUtils ddlPlanUtils){
+    constraints.addAll(ddlPlanUtils.populateConstraints(tbl, allTableNames));
+  }
+
+  public void addStats(Table table,List<String> alterTableStmt ,Map<String, 
List<Partition>> tablePartitionsMap,
+      DDLPlanUtils ddlPlanUtils)
+      throws HiveException, MetaException{
+    PerfLogger perfLogger = PerfLogger.getPerfLogger(conf, false);
+    perfLogger.perfLogBegin(ExplainTask.class.getName(), 
PerfLogger.HIVE_GET_TABLE_COLUMN_STATS);
+    if (table.isPartitioned()) {
+      
alterTableStmt.addAll(ddlPlanUtils.getDDLPlanForPartitionWithStats(table, 
tablePartitionsMap));
+    } else {
+      
alterTableStmt.addAll(ddlPlanUtils.getAlterTableStmtTableStatsColsAll(table));
+    }
+    perfLogger.perfLogEnd(ExplainTask.class.getName(), 
PerfLogger.HIVE_GET_TABLE_COLUMN_STATS);
+  }
+
+  public void addExplain(String sql , List<String> explainStmt, DDLPlanUtils 
ddlPlanUtils){
+    explainStmt.addAll(ddlPlanUtils.addExplainPlans(sql));
+    return;
+  }
+
+  public void getDDLPlan(PrintStream out) throws HiveException, MetaException {
+    DDLPlanUtils ddlPlanUtils = new DDLPlanUtils();
+    Set<String> createDatabase = new TreeSet<String>();
+    List<String> tableCreateStmt = new LinkedList<String>();
+    List<String> tableBasicDef = new LinkedList<String>();
+    List<String> createViewList = new LinkedList<String>();
+    List<String> alterTableStmt = new LinkedList<String>();
+    List<String> explainStmt = new LinkedList<String>();
+    Map<String, Table> tableMap = new HashMap<>();
+    Map<String, List<Partition>> tablePartitionsMap = new HashMap<>();
+    for (ReadEntity ent : work.getInputs()) {
+      switch (ent.getType()) {
+      // Views are also covered in table
+      case TABLE:
+        Table tbl = ent.getTable();
+        createDatabase.add(tbl.getDbName());
+        tableMap.put(tbl.getTableName(), tbl);
+        tablePartitionsMap.putIfAbsent(tbl.getTableName(), new 
ArrayList<Partition>());
+        break;
+      case PARTITION:
+        
tablePartitionsMap.get(ent.getTable().getTableName()).add(ent.getPartition());
+        break;
+      default:
+        break;
+      }
+    }
+    //process the databases
+    List<String> createDatabaseStmt = 
ddlPlanUtils.getCreateDatabaseStmt(createDatabase);
+    //process the tables
+    for (String tableName : tableMap.keySet()) {
+      Table table = tableMap.get(tableName);
+      if (table.isView()) {
+        createViewList.add(ddlPlanUtils.getCreateViewStmt(table));
+        continue;
+      } else {
+        addCreateTableStatement(table, tableCreateStmt, ddlPlanUtils);
+        addPKandBasicStats(table, tableBasicDef, ddlPlanUtils);
+        addConstraints(table, alterTableStmt, tableMap.keySet(), ddlPlanUtils);
+        addStats(table, alterTableStmt, tablePartitionsMap, ddlPlanUtils);
+      }
+    }
+    addExplain(conf.getQueryString(), explainStmt, ddlPlanUtils);

Review comment:
       This may end up adding "explain cbo <query>". But will not print the 
output of 'explain cbo query'. Can you fix it?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to