Index: beeline/src/java/org/apache/hive/beeline/BeeLine.java =================================================================== --- beeline/src/java/org/apache/hive/beeline/BeeLine.java (revision 1597407) +++ beeline/src/java/org/apache/hive/beeline/BeeLine.java (working copy) @@ -1645,21 +1645,28 @@ int print(ResultSet rs) throws SQLException { String format = getOpts().getOutputFormat(); + int width = getOpts().getMaxWidth() - 4; + + Rows rows; + + if (getOpts().getIncremental()) { + rows = new IncrementalRows(this, rs); + } else { + rows = new BufferedRows(this, rs, width); + } + + if (format.equals("table") && rows.isRowLengthBigger()) { + format = "vertical"; + } + OutputFormat f = (OutputFormat) formats.get(format); - + if (f == null) { error(loc("unknown-format", new Object[] { format, formats.keySet()})); f = new TableOutputFormat(this); } - - Rows rows; - - if (getOpts().getIncremental()) { - rows = new IncrementalRows(this, rs); - } else { - rows = new BufferedRows(this, rs); - } + return f.print(rows); } Index: beeline/src/java/org/apache/hive/beeline/BufferedRows.java =================================================================== --- beeline/src/java/org/apache/hive/beeline/BufferedRows.java (revision 1597407) +++ beeline/src/java/org/apache/hive/beeline/BufferedRows.java (working copy) @@ -33,9 +33,11 @@ class BufferedRows extends Rows { private final LinkedList list; private final Iterator iterator; + private final int consoleWidth; - BufferedRows(BeeLine beeLine, ResultSet rs) throws SQLException { + BufferedRows(BeeLine beeLine, ResultSet rs, int consoleWidth) throws SQLException { super(beeLine, rs); + this.consoleWidth = consoleWidth; list = new LinkedList(); int count = rsMeta.getColumnCount(); list.add(new Row(count)); @@ -73,5 +75,18 @@ row.sizes = max; } } + + public boolean isRowLengthBigger() { + for (Row row : list) { + int rowLength = 0; + for (int j = 0; j < row.values.length; j++) { + rowLength = rowLength + row.sizes[j]; + } + if (rowLength > consoleWidth) { + return true; + } + } + return false; + } } Index: beeline/src/java/org/apache/hive/beeline/IncrementalRows.java =================================================================== --- beeline/src/java/org/apache/hive/beeline/IncrementalRows.java (revision 1597407) +++ beeline/src/java/org/apache/hive/beeline/IncrementalRows.java (working copy) @@ -104,4 +104,10 @@ // for each row as it is produced normalizingWidths = true; } + + + @Override + boolean isRowLengthBigger() { + return false; + } } Index: beeline/src/java/org/apache/hive/beeline/Rows.java =================================================================== --- beeline/src/java/org/apache/hive/beeline/Rows.java (revision 1597407) +++ beeline/src/java/org/apache/hive/beeline/Rows.java (working copy) @@ -63,6 +63,14 @@ * maximum length of each column in the Rows. */ abstract void normalizeWidths(); + + /** + * Check whether the row length + * + * @return true if row length is bigger than the width + * false if row length is smaller than the width + */ + abstract boolean isRowLengthBigger(); /** * Return whether the specified column (0-based index) is