hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sp...@apache.org
Subject hive git commit: HIVE-14135: beeline output not formatted correctly for large column widths (Vihang Karajgaonkar, reviewed by Sergio Pena)
Date Mon, 18 Jul 2016 16:13:32 GMT
Repository: hive
Updated Branches:
  refs/heads/master b53794b83 -> 3e3f01c3a


HIVE-14135: beeline output not formatted correctly for large column widths (Vihang Karajgaonkar,
reviewed by Sergio Pena)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/3e3f01c3
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/3e3f01c3
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/3e3f01c3

Branch: refs/heads/master
Commit: 3e3f01c3a3f84c7777b7e797bc0ad24894df5f67
Parents: b53794b
Author: Vihang Karajgaonkar <vihang@cloudera.com>
Authored: Mon Jul 18 11:12:58 2016 -0500
Committer: Sergio Pena <sergio.pena@cloudera.com>
Committed: Mon Jul 18 11:12:58 2016 -0500

----------------------------------------------------------------------
 beeline/pom.xml                                 |   5 +
 .../org/apache/hive/beeline/BeeLineOpts.java    |   3 +-
 .../org/apache/hive/beeline/BufferedRows.java   |   5 +-
 .../apache/hive/beeline/TestBufferedRows.java   | 121 +++++++++++++++++++
 4 files changed, 132 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/3e3f01c3/beeline/pom.xml
----------------------------------------------------------------------
diff --git a/beeline/pom.xml b/beeline/pom.xml
index a720d08..5503add 100644
--- a/beeline/pom.xml
+++ b/beeline/pom.xml
@@ -119,6 +119,11 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>postgresql</groupId>
       <artifactId>postgresql</artifactId>
       <version>9.1-901.jdbc4</version>

http://git-wip-us.apache.org/repos/asf/hive/blob/3e3f01c3/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
index 5aaa385..e2bbbea 100644
--- a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
+++ b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
@@ -58,6 +58,7 @@ class BeeLineOpts implements Completer {
       PROPERTY_PREFIX + "system.exit";
   public static final String DEFAULT_NULL_STRING = "NULL";
   public static final char DEFAULT_DELIMITER_FOR_DSV = '|';
+  public static final int DEFAULT_MAX_COLUMN_WIDTH = 50;
 
   public static String URL_ENV_PREFIX = "BEELINE_URL_";
 
@@ -80,7 +81,7 @@ class BeeLineOpts implements Completer {
   private final Terminal terminal = TerminalFactory.get();
   private int maxWidth = DEFAULT_MAX_WIDTH;
   private int maxHeight = DEFAULT_MAX_HEIGHT;
-  private int maxColumnWidth = 15;
+  private int maxColumnWidth = DEFAULT_MAX_COLUMN_WIDTH;
   int timeout = -1;
   private String isolation = DEFAULT_ISOLATION_LEVEL;
   private String outputFormat = "table";

http://git-wip-us.apache.org/repos/asf/hive/blob/3e3f01c3/beeline/src/java/org/apache/hive/beeline/BufferedRows.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/BufferedRows.java b/beeline/src/java/org/apache/hive/beeline/BufferedRows.java
index 962c531..5604742 100644
--- a/beeline/src/java/org/apache/hive/beeline/BufferedRows.java
+++ b/beeline/src/java/org/apache/hive/beeline/BufferedRows.java
@@ -33,6 +33,7 @@ import java.util.LinkedList;
 class BufferedRows extends Rows {
   private final LinkedList<Row> list;
   private final Iterator<Row> iterator;
+  private int maxColumnWidth;
 
   BufferedRows(BeeLine beeLine, ResultSet rs) throws SQLException {
     super(beeLine, rs);
@@ -43,6 +44,7 @@ class BufferedRows extends Rows {
       list.add(new Row(count, rs));
     }
     iterator = list.iterator();
+    maxColumnWidth = beeLine.getOpts().getMaxColumnWidth();
   }
 
   public boolean hasNext() {
@@ -66,7 +68,8 @@ class BufferedRows extends Rows {
         max = new int[row.values.length];
       }
       for (int j = 0; j < max.length; j++) {
-        max[j] = Math.max(max[j], row.sizes[j] + 1);
+        // 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) {

http://git-wip-us.apache.org/repos/asf/hive/blob/3e3f01c3/beeline/src/test/org/apache/hive/beeline/TestBufferedRows.java
----------------------------------------------------------------------
diff --git a/beeline/src/test/org/apache/hive/beeline/TestBufferedRows.java b/beeline/src/test/org/apache/hive/beeline/TestBufferedRows.java
new file mode 100644
index 0000000..f3f3d3a
--- /dev/null
+++ b/beeline/src/test/org/apache/hive/beeline/TestBufferedRows.java
@@ -0,0 +1,121 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hive.beeline;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class TestBufferedRows {
+  private String[][] mockRowData = {
+      { "key1", "aaa" },
+      { "key2", "bbbbb" },
+      { "key3",
+          "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+              + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+              + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+              + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+              + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+              + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+              + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
},
+      { "key4", "ddddddddddddddd" }
+  };
+  private BeeLineOpts mockBeeLineOpts;
+  private BeeLine mockBeeline;
+  private ResultSet mockResultSet;
+  private MockRow mockRow;
+
+  @Test
+  public void testNormalizeWidths() throws SQLException {
+    setupMockData();
+
+    BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
+    bfRows.normalizeWidths();
+    while (bfRows.hasNext()) {
+      Rows.Row row = (Rows.Row) bfRows.next();
+      for (int colSize : row.sizes) {
+        Assert.assertTrue(colSize <= mockBeeLineOpts.getMaxColumnWidth());
+      }
+    }
+  }
+
+  private void setupMockData() throws SQLException {
+    // Mock BeeLine
+    mockBeeline = mock(BeeLine.class);
+    // Mock BeeLineOpts
+    mockBeeLineOpts = mock(BeeLineOpts.class);
+    when(mockBeeLineOpts.getMaxColumnWidth()).thenReturn(BeeLineOpts.DEFAULT_MAX_COLUMN_WIDTH);
+    when(mockBeeLineOpts.getNumberFormat()).thenReturn("default");
+    when(mockBeeLineOpts.getNullString()).thenReturn("NULL");
+    when(mockBeeline.getOpts()).thenReturn(mockBeeLineOpts);
+
+    // MockResultSet
+    mockResultSet = mock(ResultSet.class);
+
+    ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
+    when(mockResultSetMetaData.getColumnCount()).thenReturn(2);
+    when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("Key");
+    when(mockResultSetMetaData.getColumnLabel(2)).thenReturn("Value");
+    when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
+
+    mockRow = new MockRow();
+    // returns true as long as there is more data in mockResultData array
+    when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {
+      private int mockRowDataIndex = 0;
+
+      public Boolean answer(InvocationOnMock invocation) {
+        if (mockRowDataIndex < mockRowData.length) {
+          mockRow.setCurrentRowData(mockRowData[mockRowDataIndex]);
+          mockRowDataIndex++;
+          return true;
+        } else {
+          return false;
+        }
+      }
+    });
+
+    when(mockResultSet.getString(Matchers.anyInt())).thenAnswer(new Answer<String>()
{
+      public String answer(InvocationOnMock invocation) {
+        Object[] args = invocation.getArguments();
+        int index = ((Integer) args[0]).intValue();
+        return mockRow.getColumn(index);
+      }
+    });
+  }
+
+  static class MockRow {
+    String[] rowData;
+
+    public void setCurrentRowData(String[] rowData) {
+      this.rowData = rowData;
+    }
+
+    public String getColumn(int idx) {
+      return rowData[idx - 1];
+    }
+  }
+}
\ No newline at end of file


Mime
View raw message