You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ha...@apache.org on 2013/06/05 01:25:39 UTC

svn commit: r1489672 - in /hive/trunk/beeline/src: java/org/apache/hive/beeline/Commands.java test/org/apache/hive/beeline/src/test/TestBeeLineWithArgs.java

Author: hashutosh
Date: Tue Jun  4 23:24:01 2013
New Revision: 1489672

URL: http://svn.apache.org/r1489672
Log:
HIVE-4566 : NullPointerException if typeinfo and nativesql commands are executed at beeline before a DB connection is established (Xuefu Zhang via Ashutosh Chauhan)

Modified:
    hive/trunk/beeline/src/java/org/apache/hive/beeline/Commands.java
    hive/trunk/beeline/src/test/org/apache/hive/beeline/src/test/TestBeeLineWithArgs.java

Modified: hive/trunk/beeline/src/java/org/apache/hive/beeline/Commands.java
URL: http://svn.apache.org/viewvc/hive/trunk/beeline/src/java/org/apache/hive/beeline/Commands.java?rev=1489672&r1=1489671&r2=1489672&view=diff
==============================================================================
--- hive/trunk/beeline/src/java/org/apache/hive/beeline/Commands.java (original)
+++ hive/trunk/beeline/src/java/org/apache/hive/beeline/Commands.java Tue Jun  4 23:24:01 2013
@@ -96,8 +96,12 @@ public class Commands {
 
 
   public boolean metadata(String cmd, String[] args) {
+    if (!(beeLine.assertConnection())) {
+      return false;
+    }
+
     try {
-      Method[] m = beeLine.getDatabaseConnection().getDatabaseMetaData().getClass().getMethods();
+      Method[] m = beeLine.getDatabaseMetaData().getClass().getMethods();
       Set<String> methodNames = new TreeSet<String>();
       Set<String> methodNamesUpper = new TreeSet<String>();
       for (int i = 0; i < m.length; i++) {
@@ -114,7 +118,7 @@ public class Commands {
         return false;
       }
 
-      Object res = beeLine.getReflector().invoke(beeLine.getDatabaseConnection().getDatabaseMetaData(),
+      Object res = beeLine.getReflector().invoke(beeLine.getDatabaseMetaData(),
           DatabaseMetaData.class, cmd, Arrays.asList(args));
 
       if (res instanceof ResultSet) {
@@ -224,7 +228,7 @@ public class Commands {
     if (sql.startsWith("native")) {
       sql = sql.substring("native".length() + 1);
     }
-    String nat = beeLine.getDatabaseConnection().getConnection().nativeSQL(sql);
+    String nat = beeLine.getConnection().nativeSQL(sql);
     beeLine.output(nat);
     return true;
   }
@@ -568,7 +572,7 @@ public class Commands {
     for (int i = 0; i < m.length; i++) {
       try {
         beeLine.output(beeLine.getColorBuffer().pad(m[i], padlen).append(
-            "" + beeLine.getReflector().invoke(beeLine.getDatabaseConnection().getDatabaseMetaData(),
+            "" + beeLine.getReflector().invoke(beeLine.getDatabaseMetaData(),
                 m[i], new Object[0])));
       } catch (Exception e) {
         beeLine.handleException(e);

Modified: hive/trunk/beeline/src/test/org/apache/hive/beeline/src/test/TestBeeLineWithArgs.java
URL: http://svn.apache.org/viewvc/hive/trunk/beeline/src/test/org/apache/hive/beeline/src/test/TestBeeLineWithArgs.java?rev=1489672&r1=1489671&r2=1489672&view=diff
==============================================================================
--- hive/trunk/beeline/src/test/org/apache/hive/beeline/src/test/TestBeeLineWithArgs.java (original)
+++ hive/trunk/beeline/src/test/org/apache/hive/beeline/src/test/TestBeeLineWithArgs.java Tue Jun  4 23:24:01 2013
@@ -18,23 +18,19 @@
 
 package org.apache.hive.beeline.src.test;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.FileOutputStream;
-
-import junit.framework.TestCase;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.Assert;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
 
 import org.apache.hadoop.hive.conf.HiveConf;
-import org.apache.hadoop.hive.ql.parse.SemanticException;
 import org.apache.hive.beeline.BeeLine;
 import org.apache.hive.service.server.HiveServer2;
-import org.apache.hive.service.cli.HiveSQLException;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 /**
  * TestBeeLineWithArgs - executes tests of the command-line arguments to BeeLine
@@ -216,4 +212,31 @@ public class TestBeeLineWithArgs {
       throw e;
     }
   }
+
+  /**
+   * HIVE-4566
+   * @throws UnsupportedEncodingException
+   */
+  @Test
+  public void testNPE() throws UnsupportedEncodingException {
+    BeeLine beeLine = new BeeLine();
+
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    PrintStream beelineOutputStream = new PrintStream(os);
+    beeLine.setOutputStream(beelineOutputStream);
+    beeLine.setErrorStream(beelineOutputStream);
+
+    beeLine.runCommands( new String[] {"!typeinfo"} );
+    String output = os.toString("UTF8");
+    Assert.assertFalse( output.contains("java.lang.NullPointerException") );
+    Assert.assertTrue( output.contains("No current connection") );
+
+    beeLine.runCommands( new String[] {"!nativesql"} );
+    output = os.toString("UTF8");
+    Assert.assertFalse( output.contains("java.lang.NullPointerException") );
+    Assert.assertTrue( output.contains("No current connection") );
+
+    System.out.println(">>> PASSED " + "testNPE" );
+  }
+
 }