commit d341a4169656fe3acfef1cf67dd98ff899e6d1eb 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/beeline/TestBeeLineWithArgs.java itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java index 065f258abcfb4bf9ba649c8183c76c031e8fdd6f..1f5cd2ebaf0aed7535b13fdf27b845a6d962dbd9 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java @@ -1130,4 +1130,19 @@ public void testCustomDelimiterBeelineCmd() throws Throwable { argList.add("--outputformat=tsv2"); testScriptFile(SCRIPT_TEXT, argList, EXPECTED_PATTERN, true); } + + /** + * Test 'describe extended' on tables that have special white space characters in the row format. + */ + @Test + public void testDescribeExtended() throws Throwable { + String SCRIPT_TEXT = "drop table if exists describeDelim;" + + "create table describeDelim (orderid int, orderdate string, customerid int)" + + " ROW FORMAT DELIMITED FIELDS terminated by '\\t' LINES terminated by '\\n';" + + "describe extended describeDelim;"; + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + testScriptFile(SCRIPT_TEXT, argList, OutStream.OUT, Arrays.asList( + new Tuple<>("Detailed Table Information.*line.delim=\\\\n", true), + new Tuple<>("Detailed Table Information.*field.delim=\\\\t", true))); + } } 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..23ea661c9de6df024ca2099a9f3001d540e297da 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 @@ -208,7 +208,8 @@ 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 = HiveStringUtils.escapeJava(tbl.getTTable().toString()); + outStream.write(tableDesc.getBytes("UTF-8")); outStream.write(separator); outStream.write(terminator); }