.../java/org/apache/hive/beeline/BufferedRows.java | 48 ++++++++++------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/beeline/src/java/org/apache/hive/beeline/BufferedRows.java b/beeline/src/java/org/apache/hive/beeline/BufferedRows.java index 5369b08..f7e10a4 100644 --- a/beeline/src/java/org/apache/hive/beeline/BufferedRows.java +++ b/beeline/src/java/org/apache/hive/beeline/BufferedRows.java @@ -24,18 +24,20 @@ import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; +import java.util.List; import com.google.common.base.Optional; /** - * Rows implementation which buffers all rows in a linked list. + * Rows implementation which buffers all rows */ class BufferedRows extends Rows { - private final LinkedList list; + private final List list; private final Iterator iterator; + private int columnCount; private int maxColumnWidth; BufferedRows(BeeLine beeLine, ResultSet rs) throws SQLException { @@ -44,21 +46,17 @@ BufferedRows(BeeLine beeLine, ResultSet rs, Optional limit) throws SQLException { super(beeLine, rs); - list = new LinkedList(); - int count = rsMeta.getColumnCount(); - list.add(new Row(count)); + list = new ArrayList(); + columnCount = rsMeta.getColumnCount(); + list.add(new Row(columnCount)); int numRowsBuffered = 0; - if (limit.isPresent()) { - while (limit.get() > numRowsBuffered && rs.next()) { - this.list.add(new Row(count, rs)); - numRowsBuffered++; - } - } else { - while (rs.next()) { - this.list.add(new Row(count, rs)); - } + int maxRowsBuffered = limit.or(Integer.MAX_VALUE); + + while (numRowsBuffered++ < maxRowsBuffered && rs.next()) { + this.list.add(new Row(columnCount, rs)); } + iterator = list.iterator(); maxColumnWidth = beeLine.getOpts().getMaxColumnWidth(); } @@ -80,18 +78,16 @@ public String toString(){ @Override void normalizeWidths() { - int[] max = null; - for (Row row : list) { - if (max == null) { - max = new int[row.values.length]; + if (!list.isEmpty()) + { + int[] max = new int[columnCount]; + for (Row row : list) { + for (int j = 0; j < columnCount; j++) { + // if the max column width is too large, reset it to max allowed Column width + max[j] = Math.min(Math.max(max[j], row.sizes[j] + 1), maxColumnWidth); + } + row.sizes = max; } - for (int j = 0; j < max.length; j++) { - // if the max column width is too large, reset it to max allowed Column width - max[j] = Math.min(Math.max(max[j], row.sizes[j] + 1), maxColumnWidth); - } - } - for (Row row : list) { - row.sizes = max; } } }