diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java index cb7fdf73b5..b7998cbec5 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java @@ -2334,14 +2334,98 @@ private int showCreateTable(Hive db, DataOutputStream outStream, String tableNam String tbl_columns = ""; List cols = tbl.getCols(); List columns = new ArrayList(); + + //Constraints + PrimaryKeyInfo pkInfo = db.getPrimaryKeys(tbl.getDbName(), tbl.getTableName()); + ForeignKeyInfo fkInfo = db.getForeignKeys(tbl.getDbName(), tbl.getTableName()); + UniqueConstraint ukInfo = db.getUniqueConstraints(tbl.getDbName(), tbl.getTableName()); + NotNullConstraint nnInfo = db.getNotNullConstraints(tbl.getDbName(), tbl.getTableName()); + DefaultConstraint dInfo = db.getDefaultConstraints(tbl.getDbName(), tbl.getTableName()); + CheckConstraint cInfo = db.getCheckConstraints(tbl.getDbName(), tbl.getTableName()); + + Map defaultValues = dInfo.getColNameToDefaultValueMap(); + Map notNullColumns = nnInfo.getNotNullConstraints(); + Map> uniqueColsByConstraintNames = ukInfo.getUniqueConstraints(); + //Getting the columns which are unique as a String from UniqueConstraintCol + List uniqueCols = new ArrayList<>(); + uniqueColsByConstraintNames.values().forEach(uniqueConstraintCols -> { + List uniqueColNamesPerConstraint = new ArrayList<>(); + uniqueConstraintCols.forEach(uniqueConstraintCol -> + uniqueColNamesPerConstraint.add(uniqueConstraintCol.colName)); + uniqueCols.addAll(uniqueColNamesPerConstraint); + }); + //Getting check constraints by column name: + Map> checkExpressionsByConstraintNames = cInfo.getCheckConstraints(); + Map checkExpressionByColName = new HashMap<>(); + checkExpressionsByConstraintNames.forEach((constraintName, checkConstraintCols) -> { + checkConstraintCols.forEach(checkConstraintCol -> + checkExpressionByColName.put(checkConstraintCol.colName, checkConstraintCol.checkExpression)); + }); + for (FieldSchema col : cols) { String columnDesc = " `" + col.getName() + "` " + col.getType(); + + if(defaultValues.containsKey(col.getName())) { + columnDesc += " DEFAULT " + defaultValues.get(col.getName()); + } + if(uniqueCols.contains(col.getName())) { + columnDesc += " UNIQUE"; + } + if(notNullColumns.containsValue(col.getName())) { + columnDesc += " NOT NULL"; + } + if(checkExpressionByColName.containsKey(col.getName())) { + columnDesc += " CHECK " + checkExpressionByColName.get(col.getName()); + } if (col.getComment() != null) { columnDesc = columnDesc + " COMMENT '" + HiveStringUtils.escapeHiveCommand(col.getComment()) + "'"; } columns.add(columnDesc); } + + StringBuilder constaints_str = new StringBuilder(); + //If the table has primary and foreign keys as well, comma is needed to separate them + boolean needComma = false; + + Map pkInfoColNames = pkInfo.getColNames(); + if(!pkInfoColNames.isEmpty()) { + constaints_str.append(" PRIMARY KEY ("); + for (Map.Entry me : pkInfoColNames.entrySet()) { + constaints_str.append(me.getValue() + ", "); + } + constaints_str.setLength(constaints_str.length()-2); + constaints_str.append(")"); + + //If the table has other constraints, comma is needed + needComma = true; + } + + Map> foreignKeysByConstraintName = fkInfo.getForeignKeys(); + //Getting all the foreign key constraints to iterate through it + if(!foreignKeysByConstraintName.isEmpty()) { + if(needComma) { + constaints_str.append(", \n"); + } + for(Map.Entry> e : foreignKeysByConstraintName.entrySet()) { + StringBuilder childCols = new StringBuilder(); + StringBuilder parentCols = new StringBuilder(); + String parentDb = "", parentTable = ""; + for (ForeignKeyInfo.ForeignKeyCol foreignKeyCol : e.getValue()) { + childCols.append(foreignKeyCol.childColName + ", "); + parentCols.append(foreignKeyCol.parentColName + ", "); + parentDb = foreignKeyCol.parentDatabaseName; + parentTable = foreignKeyCol.parentTableName; + } + childCols.setLength(childCols.length()-2); + parentCols.setLength(parentCols.length()-2); + constaints_str.append(" FOREIGN KEY (" + childCols.toString() + ") REFERENCES " + + parentDb + "." + parentTable + "(" + parentCols.toString() + ")"); + } + } + if (!constaints_str.toString().equals("")) { + columns.add(constaints_str.toString()); + } tbl_columns = StringUtils.join(columns, ", \n"); // Table comment