diff --git a/beeline/src/java/org/apache/hive/beeline/BufferedRows.java b/beeline/src/java/org/apache/hive/beeline/BufferedRows.java index 962c531..ddf33cc 100644 --- a/beeline/src/java/org/apache/hive/beeline/BufferedRows.java +++ b/beeline/src/java/org/apache/hive/beeline/BufferedRows.java @@ -65,8 +65,26 @@ void normalizeWidths() { if (max == null) { max = new int[row.values.length]; } + String[] value = row.values; + if (row.values.length > 1 && row.values[1] == "NULL") { + value = row.values[0].split("\\s+", 3); + int len = value.length; + if (len == 1) { + String[] values = new String[3]; + values[0] = value[0]; + values[1] = value[0]; + values[2] = value[0]; + value = values; + }else if (len == 2) { + String[] values = new String[3]; + values[0] = value[0]; + values[1] = value[0]; + values[2] = value[1]; + value = values; + } + } for (int j = 0; j < max.length; j++) { - max[j] = Math.max(max[j], row.sizes[j] + 1); + max[j] = Math.max(max[j], value[j].length() + 1); } } for (Row row : list) { diff --git a/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java b/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java index 61b84ef..7e1650d 100644 --- a/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java +++ b/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.StringWriter; +import jline.console.history.History; import org.apache.hadoop.io.IOUtils; import org.supercsv.io.CsvListWriter; import org.supercsv.prefs.CsvPreference; @@ -33,94 +34,112 @@ * OutputFormat for values separated by a delimiter. */ class SeparatedValuesOutputFormat implements OutputFormat { - public final static String DISABLE_QUOTING_FOR_SV = "disable.quoting.for.sv"; - private final BeeLine beeLine; - private CsvPreference quotedCsvPreference; - private CsvPreference unquotedCsvPreference; + public final static String DISABLE_QUOTING_FOR_SV = "disable.quoting.for.sv"; + private final BeeLine beeLine; + private CsvPreference quotedCsvPreference; + private CsvPreference unquotedCsvPreference; - SeparatedValuesOutputFormat(BeeLine beeLine, char separator) { - this.beeLine = beeLine; - unquotedCsvPreference = new CsvPreference.Builder('\0', separator, "").build(); - quotedCsvPreference = new CsvPreference.Builder('"', separator, "").build(); - } + SeparatedValuesOutputFormat(BeeLine beeLine, char separator) { + this.beeLine = beeLine; + unquotedCsvPreference = new CsvPreference.Builder('\0', separator, "").build(); + quotedCsvPreference = new CsvPreference.Builder('"', separator, "").build(); + } - private void updateCsvPreference() { - if (beeLine.getOpts().getOutputFormat().equals("dsv")) { - // check whether delimiter changed by user - char curDel = (char) getCsvPreference().getDelimiterChar(); - char newDel = beeLine.getOpts().getDelimiterForDSV(); - // if delimiter changed, rebuild the csv preference - if (newDel != curDel) { - // "" is passed as the end of line symbol in following function, as - // beeline itself adds newline - if (isQuotingDisabled()) { - unquotedCsvPreference = new CsvPreference.Builder('\0', newDel, "").build(); - } else { - quotedCsvPreference = new CsvPreference.Builder('"', newDel, "").build(); + private void updateCsvPreference() { + if (beeLine.getOpts().getOutputFormat().equals("dsv")) { + // check whether delimiter changed by user + char curDel = (char) getCsvPreference().getDelimiterChar(); + char newDel = beeLine.getOpts().getDelimiterForDSV(); + // if delimiter changed, rebuild the csv preference + if (newDel != curDel) { + // "" is passed as the end of line symbol in following function, as + // beeline itself adds newline + if (isQuotingDisabled()) { + unquotedCsvPreference = new CsvPreference.Builder('\0', newDel, "").build(); + } else { + quotedCsvPreference = new CsvPreference.Builder('"', newDel, "").build(); + } + } } - } } - } - @Override - public int print(Rows rows) { - updateCsvPreference(); + @Override + public int print(Rows rows) { + updateCsvPreference(); - int count = 0; - while (rows.hasNext()) { - if (count == 0 && !beeLine.getOpts().getShowHeader()) { - rows.next(); - count++; - continue; - } - printRow((Rows.Row) rows.next()); - count++; + int count = 0; + while (rows.hasNext()) { + History history = beeLine.getConsoleReader().getHistory(); + CharSequence curCommand = history.get(history.index() - 1); + if (count == 0) { + if (curCommand.toString().contains("describe pretty")) { + if (beeLine.getOpts().getShowHeader()) { + rows.next(); + count++; + continue; + } else { + rows.next(); + count++; + rows.next(); + count++; + continue; + } + } else { + if (!beeLine.getOpts().getShowHeader()) { + rows.next(); + count++; + continue; + } + } + } + printRow((Rows.Row) rows.next()); + count++; + } + return count - 1; // sans header row } - return count - 1; // sans header row - } - private String getFormattedStr(String[] vals) { - StringWriter strWriter = new StringWriter(); - CsvListWriter writer = new CsvListWriter(strWriter, getCsvPreference()); - if (vals.length > 0) { - try { - writer.write(vals); - } catch (IOException e) { - beeLine.error(e); - } finally { - IOUtils.closeStream(writer); - } + private String getFormattedStr(String[] vals) { + StringWriter strWriter = new StringWriter(); + CsvListWriter writer = new CsvListWriter(strWriter, getCsvPreference()); + if (vals.length > 0) { + try { + writer.write(vals); + } catch (IOException e) { + beeLine.error(e); + } finally { + IOUtils.closeStream(writer); + } + } + return strWriter.toString(); } - return strWriter.toString(); - } - - private void printRow(Rows.Row row) { - String[] vals = row.values; - String formattedStr = getFormattedStr(vals); - beeLine.output(formattedStr); - } - private boolean isQuotingDisabled() { - String quotingDisabledStr = System.getProperty(SeparatedValuesOutputFormat.DISABLE_QUOTING_FOR_SV); - if (quotingDisabledStr == null || quotingDisabledStr.isEmpty()) { - // default is disabling the double quoting for separated value - return true; + private void printRow(Rows.Row row) { + String[] vals = row.values; + String formattedStr = getFormattedStr(vals); + beeLine.output(formattedStr); } - String parsedOptionStr = quotingDisabledStr.toLowerCase(); - if (parsedOptionStr.equals("false") || parsedOptionStr.equals("true")) { - return Boolean.valueOf(parsedOptionStr); - } else { - beeLine.error("System Property disable.quoting.for.sv is now " + parsedOptionStr - + " which only accepts boolean value"); - return true; + + private boolean isQuotingDisabled() { + String quotingDisabledStr = System.getProperty(SeparatedValuesOutputFormat.DISABLE_QUOTING_FOR_SV); + if (quotingDisabledStr == null || quotingDisabledStr.isEmpty()) { + // default is disabling the double quoting for separated value + return true; + } + String parsedOptionStr = quotingDisabledStr.toLowerCase(); + if (parsedOptionStr.equals("false") || parsedOptionStr.equals("true")) { + return Boolean.valueOf(parsedOptionStr); + } else { + beeLine.error("System Property disable.quoting.for.sv is now " + parsedOptionStr + + " which only accepts boolean value"); + return true; + } } - } - private CsvPreference getCsvPreference() { - if (isQuotingDisabled()) { - return unquotedCsvPreference; - } else { - return quotedCsvPreference; + private CsvPreference getCsvPreference() { + if (isQuotingDisabled()) { + return unquotedCsvPreference; + } else { + return quotedCsvPreference; + } } - } } diff --git a/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java b/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java index 2753568..5296e4d 100644 --- a/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java +++ b/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java @@ -22,6 +22,8 @@ */ package org.apache.hive.beeline; +import jline.console.history.History; + /** * OutputFormat for a pretty, table-like format. * @@ -44,10 +46,18 @@ public int print(Rows rows) { final int width = beeLine.getOpts().getMaxWidth() - 4; // normalize the columns sizes + int flag =0; rows.normalizeWidths(); - for (; rows.hasNext();) { + History history = beeLine.getConsoleReader().getHistory(); + CharSequence curCommand = history.get(history.index() - 1); + if (curCommand.toString().contains("describe pretty") && flag == 1) { + rows.next(); + flag++; + continue; + } Rows.Row row = (Rows.Row) rows.next(); + flag++; ColorBuffer cbuf = getOutputString(rows, row); if (beeLine.getOpts().getTruncateTable()) { cbuf = cbuf.truncate(width); @@ -106,11 +116,27 @@ public ColorBuffer getOutputString(Rows rows, Rows.Row row) { return getOutputString(rows, row, " | "); } - ColorBuffer getOutputString(Rows rows, Rows.Row row, String delim) { ColorBuffer buf = beeLine.getColorBuffer(); - - for (int i = 0; i < row.values.length; i++) { + String[] value = row.values; + if (row.values.length > 1 && row.values[1] == "NULL") { + value = row.values[0].split("\\s+", 3); + int len = value.length; + if (len == 1) { + String[] values = new String[3]; + values[0] = value[0]; + values[1] = value[0]; + values[2] = value[0]; + value = values; + }else if (len == 2) { + String[] values = new String[3]; + values[0] = value[0]; + values[1] = value[0]; + values[2] = value[1]; + value = values; + } + } + for (int i = 0; i < value.length; i++) { if (buf.getVisibleLength() > 0) { buf.green(delim); } @@ -118,14 +144,14 @@ ColorBuffer getOutputString(Rows rows, Rows.Row row, String delim) { ColorBuffer v; if (row.isMeta) { - v = beeLine.getColorBuffer().center(row.values[i], row.sizes[i]); + v = beeLine.getColorBuffer().center(value[i], row.sizes[i]); if (rows.isPrimaryKey(i)) { buf.cyan(v.getMono()); } else { buf.bold(v.getMono()); } } else { - v = beeLine.getColorBuffer().pad(row.values[i], row.sizes[i]); + v = beeLine.getColorBuffer().pad(value[i], row.sizes[i]); if (rows.isPrimaryKey(i)) { buf.cyan(v.getMono()); } else {