commit 489568e92072f0133141e8992cf68a97f7f365f0 Author: Andrew Sherman Date: Tue Jan 2 15:55:43 2018 -0800 HIVE-18367: Fix 'describe extended' so that it works on tables that have special white space characters in the row format. diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index ffeee69f801330710481c4b0b3517fcb4e17a986..7858944ff66845416ae4a45466b7edfebbb8efc5 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -1617,4 +1617,39 @@ private void checkVertices() { } return verticesLists; } + + /** + * Test 'describe extended' on tables that have special white space characters in the row format. + */ + @Test + public void testDescribe() throws Exception { + try (Statement stmt = conTestDb.createStatement()) { + String table = "testDescribe"; + stmt.execute("drop table if exists " + table); + stmt.execute("create table " + table + " (orderid int, orderdate string, customerid int)" + + " ROW FORMAT DELIMITED FIELDS terminated by '\\t' LINES terminated by '\\n'"); + String extendedDescription = getDetailedTableDescription(stmt, table); + assertNotNull("could not get Detailed Table Information", extendedDescription); + assertTrue("description appears truncated", extendedDescription.endsWith(")")); + assertTrue("bad line delimiter", extendedDescription.contains("line.delim=\\n")); + assertTrue("bad field delimiter", extendedDescription.contains("field.delim=\\t")); + } + } + + /** + * Get Detailed Table Information via jdbc + */ + private String getDetailedTableDescription(Statement stmt, String table) throws SQLException { + String extendedDescription = null; + try (ResultSet rs = stmt.executeQuery("describe extended " + table)) { + while (rs.next()) { + String out = rs.getString(1); + String tableInfo = rs.getString(2); + if ("Detailed Table Information".equals(out)) { // from TextMetaDataFormatter + extendedDescription = tableInfo; + } + } + } + return extendedDescription; + } } diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/formatting/TextMetaDataFormatter.java ql/src/java/org/apache/hadoop/hive/ql/metadata/formatting/TextMetaDataFormatter.java index f3d878d3f750dc2ea60ef65d8f6a5d51e9e937d3..28fccdc678f97b2b31bf7c56e7340ea7ece298ec 100644 --- ql/src/java/org/apache/hadoop/hive/ql/metadata/formatting/TextMetaDataFormatter.java +++ ql/src/java/org/apache/hadoop/hive/ql/metadata/formatting/TextMetaDataFormatter.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils; import org.apache.hive.common.util.HiveStringUtils; import org.slf4j.Logger; @@ -208,7 +209,11 @@ public void describeTable(DataOutputStream outStream, String colPath, // show table information outStream.write(("Detailed Table Information").getBytes("UTF-8")); outStream.write(separator); - outStream.write(tbl.getTTable().toString().getBytes("UTF-8")); + String tableDesc = tbl.getTTable().toString(); + String tableDescEscaped = StringUtils.replaceEach(tableDesc, + new String[]{"\n", "\t"}, // from strings + new String[]{"\\n", "\\t"}); // to strings + outStream.write(tableDescEscaped.getBytes("UTF-8")); outStream.write(separator); outStream.write(terminator); }