You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by xu...@apache.org on 2015/09/08 05:40:40 UTC

hive git commit: HIVE-11640: Shell command doesn't work for new CLI[Beeline-cli branch](Ferdinand Xu, reviewed by Xuefu Zhang)

Repository: hive
Updated Branches:
  refs/heads/beeline-cli 3d088de02 -> b90a2ae5d


HIVE-11640: Shell command doesn't work for new CLI[Beeline-cli branch](Ferdinand Xu, reviewed by Xuefu Zhang)


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

Branch: refs/heads/beeline-cli
Commit: b90a2ae5dfb3371bcf1a8ff5f683d8fdd5dbe0ee
Parents: 3d088de
Author: Ferdinand Xu <ch...@intel.com>
Authored: Mon Sep 7 23:36:29 2015 -0400
Committer: Ferdinand Xu <ch...@intel.com>
Committed: Mon Sep 7 23:36:29 2015 -0400

----------------------------------------------------------------------
 .../java/org/apache/hive/beeline/BeeLine.java   | 82 ++++++++++++--------
 .../java/org/apache/hive/beeline/Commands.java  | 44 +++++------
 .../apache/hive/beeline/cli/TestHiveCli.java    | 32 ++++++--
 3 files changed, 95 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/b90a2ae5/beeline/src/java/org/apache/hive/beeline/BeeLine.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
index 1e4759b..11ff3f2 100644
--- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java
+++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
@@ -695,6 +695,7 @@ public class BeeLine implements Closeable {
     if (!commands.isEmpty()) {
       embeddedConnect();
       connectDBInEmbededMode();
+      updateOptsForCli();
       for (Iterator<String> i = commands.iterator(); i.hasNext(); ) {
         String command = i.next().toString();
         debug(loc("executing-command", command));
@@ -811,6 +812,14 @@ public class BeeLine implements Closeable {
     }
   }
 
+  private void updateOptsForCli() {
+    getOpts().updateBeeLineOptsFromConf();
+    getOpts().setShowHeader(false);
+    getOpts().setOutputFormat("dsv");
+    getOpts().setDelimiterForDSV(' ');
+    getOpts().setNullEmptyString(true);
+  }
+
   /**
    * Start accepting input from stdin, and dispatch it
    * to the appropriate {@link CommandHandler} until the
@@ -836,10 +845,7 @@ public class BeeLine implements Closeable {
           return code;
         }
         defaultConnect(false);
-        getOpts().updateBeeLineOptsFromConf();
-        getOpts().setShowHeader(false);
-        getOpts().setOutputFormat("dsv");
-        getOpts().setDelimiterForDSV(' ');
+        updateOptsForCli();
 
         processInitFiles(opts.getInitFiles());
       }
@@ -958,6 +964,7 @@ public class BeeLine implements Closeable {
 
         // trim line
         line = (line == null) ? null : line.trim();
+
         if (!dispatch(line) && exitOnError) {
           return ERRNO_OTHER;
         }
@@ -1059,8 +1066,33 @@ public class BeeLine implements Closeable {
     output(loc("cmd-usage"));
   }
 
-  private String[] tokenizeCmd(String cmd) {
-    return cmd.split("\\s+");
+  /**
+   * This method is used for executing commands beginning with !
+   * @param line
+   * @return
+   */
+  public boolean execCommandWithPrefix(String line) {
+    Map<String, CommandHandler> cmdMap = new TreeMap<String, CommandHandler>();
+    line = line.substring(1);
+    for (int i = 0; i < commandHandlers.length; i++) {
+      String match = commandHandlers[i].matches(line);
+      if (match != null) {
+        cmdMap.put(match, commandHandlers[i]);
+      }
+    }
+
+    if (cmdMap.size() == 0) {
+      return error(loc("unknown-command", line));
+    }
+    if (cmdMap.size() > 1) {
+      // any exact match?
+      CommandHandler handler = cmdMap.get(line);
+      if (handler == null) {
+        return error(loc("multiple-matches", cmdMap.keySet().toString()));
+      }
+      return handler.execute(line);
+    }
+    return cmdMap.values().iterator().next().execute(line);
   }
 
   /**
@@ -1096,35 +1128,19 @@ public class BeeLine implements Closeable {
       line = "!help";
     }
 
-    if (line.startsWith(COMMAND_PREFIX)) {
-      Map<String, CommandHandler> cmdMap = new TreeMap<String, CommandHandler>();
-      line = line.substring(1);
-      for (int i = 0; i < commandHandlers.length; i++) {
-        String match = commandHandlers[i].matches(line);
-        if (match != null) {
-          CommandHandler prev = cmdMap.put(match, commandHandlers[i]);
-          if (prev != null) {
-            return error(loc("multiple-matches",
-                Arrays.asList(prev.getName(), commandHandlers[i].getName())));
-          }
-        }
-      }
-
-      if (cmdMap.size() == 0) {
-        return error(loc("unknown-command", line));
-      }
-      if (cmdMap.size() > 1) {
-        // any exact match?
-        CommandHandler handler = cmdMap.get(line);
-        if (handler == null) {
-          return error(loc("multiple-matches", cmdMap.keySet().toString()));
-        }
-        return handler.execute(line);
+    if (isBeeLine) {
+      if (line.startsWith(COMMAND_PREFIX) && !line.contains(";")) {
+        // handle the case "!cmd" for beeline
+        return execCommandWithPrefix(line);
+      } else {
+        return commands.sql(line, getOpts().getEntireLineAsCommand());
       }
-      return cmdMap.values().iterator().next()
-          .execute(line);
     } else {
-      return commands.sql(line, getOpts().getEntireLineAsCommand());
+      if (line.toLowerCase().startsWith("!connect")) {
+        return execCommandWithPrefix(line);
+      } else {
+        return commands.sql(line, getOpts().getEntireLineAsCommand());
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/hive/blob/b90a2ae5/beeline/src/java/org/apache/hive/beeline/Commands.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java
index 5e5cfec..d16b4ec 100644
--- a/beeline/src/java/org/apache/hive/beeline/Commands.java
+++ b/beeline/src/java/org/apache/hive/beeline/Commands.java
@@ -824,7 +824,7 @@ public class Commands {
     if (cmd == null || cmd.isEmpty())
       return false;
     String[] tokens = tokenizeCmd(cmd);
-    return tokens[0].equalsIgnoreCase("!source");
+    return tokens[0].equalsIgnoreCase("source");
   }
 
   private boolean sourceFile(String cmd) {
@@ -866,6 +866,7 @@ public class Commands {
       }
       String[] cmds = lines.split(";");
       for (String c : cmds) {
+        c = c.trim();
         if (!executeInternal(c, false)) {
           return false;
         }
@@ -878,11 +879,10 @@ public class Commands {
     return true;
   }
 
-  private String cliToBeelineCmd(String cmd) {
+  public String cliToBeelineCmd(String cmd) {
     if (cmd == null)
       return null;
-    String[] tokens = tokenizeCmd(cmd);
-    if (tokens[0].equalsIgnoreCase("source")) {
+    if (cmd.toLowerCase().equals("quit") || cmd.toLowerCase().equals("exit")) {
       return BeeLine.COMMAND_PREFIX + cmd;
     } else if (cmd.startsWith("!")) {
       String shell_cmd = cmd.substring(1);
@@ -896,6 +896,10 @@ public class Commands {
   // Return false only occurred error when execution the sql and the sql should follow the rules
   // of beeline.
   private boolean executeInternal(String sql, boolean call) {
+    if (!beeLine.isBeeLine()) {
+      sql = cliToBeelineCmd(sql);
+    }
+
     if (sql == null || sql.length() == 0) {
       return true;
     }
@@ -905,8 +909,13 @@ public class Commands {
       return true;
     }
 
+    // is source CMD
+    if (isSourceCMD(sql)) {
+      return sourceFile(sql);
+    }
+
     if (sql.startsWith(BeeLine.COMMAND_PREFIX)) {
-      sql = sql.substring(1);
+      return beeLine.execCommandWithPrefix(sql);
     }
 
     String prefix = call ? "call" : "sql";
@@ -921,6 +930,10 @@ public class Commands {
       return true;
     }
 
+    if (!(beeLine.assertConnection())) {
+      return false;
+    }
+
     ClientHook hook = null;
     if (!beeLine.isBeeLine()) {
       hook = ClientCommandHookFactory.get().getHook(sql);
@@ -1096,10 +1109,6 @@ public class Commands {
       beeLine.handleException(e);
     }
 
-    if (!(beeLine.assertConnection())) {
-      return false;
-    }
-
     line = line.trim();
     List<String> cmdList = new ArrayList<String>();
     if (entireLineAsCommand) {
@@ -1120,20 +1129,6 @@ public class Commands {
     for (int i = 0; i < cmdList.size(); i++) {
       String sql = cmdList.get(i).trim();
       if (sql.length() != 0) {
-        if (!beeLine.isBeeLine()) {
-          sql = cliToBeelineCmd(sql);
-          if (sql.equalsIgnoreCase("quit") || sql.equalsIgnoreCase("exit")) {
-            beeLine.setExit(true);
-            return true;
-          }
-        }
-
-        // is source CMD
-        if (isSourceCMD(sql)) {
-          sourceFile(sql);
-          continue;
-        }
-
         if (!executeInternal(sql, call)) {
           return false;
         }
@@ -1205,6 +1200,9 @@ public class Commands {
     return true;
   }
 
+  public boolean exit(String line) {
+    return quit(line);
+  }
 
   /**
    * Close all connections.

http://git-wip-us.apache.org/repos/asf/hive/blob/b90a2ae5/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java
----------------------------------------------------------------------
diff --git a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java
index e06d2ea..953ba5f 100644
--- a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java
+++ b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java
@@ -46,6 +46,7 @@ public class TestHiveCli {
       "create table if not exists test.testSrcTbl2(sc2 string);";
   private final static String SOURCE_CONTEXT3 =
       "create table if not exists test.testSrcTbl3(sc3 string);";
+  private final static String SOURCE_CONTEXT4 = "show tables;!ls;show tables;\nquit;";
   final static String CMD =
       "create database if not exists test;\ncreate table if not exists test.testTbl(a string, b "
           + "string);\n";
@@ -90,19 +91,23 @@ public class TestHiveCli {
     String output = os.toString();
     LOG.debug(output);
     if (contains) {
-      Assert.assertTrue(
-          "The expected keyword \"" + keywords + "\" doesn't occur in the output: " + output,
+      Assert.assertTrue("The expected keyword \"" + keywords + "\" occur in the output: " + output,
           output.contains(keywords));
     } else {
       Assert.assertFalse(
-          "The expected keyword \"" + keywords + "\" doesn't occur in the output: " + output,
-          output.contains(keywords));
+          "The expected keyword \"" + keywords + "\" should be excluded occurred in the output: "
+              + output, output.contains(keywords));
     }
   }
 
   @Test
   public void testInValidCmd() {
-    verifyCMD("!lss\n", "Unknown command: lss", errS, null, ERRNO_OK, true);
+    verifyCMD("!lss\n", "Failed to execute lss", errS, null, ERRNO_OK, true);
+  }
+
+  @Test
+  public void testCmd() {
+    verifyCMD("show tables;!ls;show tables;\n", "src", os, null, ERRNO_OK, true);
   }
 
   @Test
@@ -152,6 +157,14 @@ public class TestHiveCli {
   }
 
   @Test
+  public void testSourceCmd3() {
+    File f = generateTmpFile(SOURCE_CONTEXT4);
+    verifyCMD("source " + f.getPath() + ";" + "desc testSrcTbl4;\nquit;\n", "src", os,
+        new String[] { "--database", "test" }, ERRNO_OK, true);
+    f.delete();
+  }
+
+  @Test
   public void testSqlFromCmd() {
     verifyCMD(null, "", os, new String[] { "-e", "show databases;" }, ERRNO_OK, true);
   }
@@ -192,8 +205,7 @@ public class TestHiveCli {
 
   @Test
   public void testErrOutput() {
-    verifyCMD(
-        "show tables;set system:xxx=5;set system:yyy=${system:xxx};\nlss;",
+    verifyCMD("show tables;set system:xxx=5;set system:yyy=${system:xxx};\nlss;",
         "cannot recognize input near 'lss' '<EOF>' '<EOF>'", errS, null, ERRNO_OK, true);
   }
 
@@ -224,6 +236,12 @@ public class TestHiveCli {
         "hive (invalidDB)>", os, null, ERRNO_OK, false);
   }
 
+  @Test
+  public void testNoErrorDB() {
+    verifyCMD(null, "Error: Method not supported (state=,code=0)", errS, new String[] { "-e", "show tables;" },
+        ERRNO_OK, false);
+  }
+
   private void redirectOutputStream() {
     // Setup output stream to redirect output to
     os = new ByteArrayOutputStream();