You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by ta...@apache.org on 2018/04/11 00:06:49 UTC

[4/4] impala git commit: IMPALA-6805: Show current database in Impala shell prompt

IMPALA-6805: Show current database in Impala shell prompt

Prompt format:
[host:port] db_name>

Testing:
- Added new shell tests
- Ran end-to-end shell tests

Change-Id: Ifb0ae58507321e426e5f0f16518671420974a3fc
Reviewed-on: http://gerrit.cloudera.org:8080/9927
Reviewed-by: Fredy Wijaya <fw...@cloudera.com>
Reviewed-by: Michael Brown <mi...@cloudera.com>
Tested-by: Impala Public Jenkins <im...@cloudera.com>


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

Branch: refs/heads/master
Commit: 830e3346f186aebc879e4ef2927e08db97143100
Parents: 6dc13d9
Author: Fredy wijaya <fw...@cloudera.com>
Authored: Wed Apr 4 13:05:31 2018 -0700
Committer: Impala Public Jenkins <im...@cloudera.com>
Committed: Tue Apr 10 20:52:48 2018 +0000

----------------------------------------------------------------------
 shell/impala_shell.py                 | 15 +++++++++--
 tests/shell/test_shell_interactive.py | 42 ++++++++++++++++++++++++++----
 2 files changed, 50 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/impala/blob/830e3346/shell/impala_shell.py
----------------------------------------------------------------------
diff --git a/shell/impala_shell.py b/shell/impala_shell.py
index f13899d..93bdafb 100755
--- a/shell/impala_shell.py
+++ b/shell/impala_shell.py
@@ -105,6 +105,7 @@ class ImpalaShell(object, cmd.Cmd):
 
   # If not connected to an impalad, the server version is unknown.
   UNKNOWN_SERVER_VERSION = "Not Connected"
+  PROMPT_FORMAT = "[{host}:{port}] {db}> "
   DISCONNECTED_PROMPT = "[Not connected] > "
   UNKNOWN_WEBSERVER = "0.0.0.0"
   # Message to display in shell when cancelling a query
@@ -774,7 +775,8 @@ class ImpalaShell(object, cmd.Cmd):
     if self.imp_client.connected:
       self._print_if_verbose('Connected to %s:%s' % self.impalad)
       self._print_if_verbose('Server version: %s' % self.server_version)
-      self.prompt = "[%s:%s] > " % self.impalad
+      self.prompt = ImpalaShell.PROMPT_FORMAT.format(
+        host=self.impalad[0], port=self.impalad[1], db=ImpalaShell.DEFAULT_DB)
       self._validate_database()
     try:
       self.imp_client.build_default_query_options_dict()
@@ -1141,7 +1143,16 @@ class ImpalaShell(object, cmd.Cmd):
     """Executes a USE... query"""
     query = self._create_beeswax_query(args)
     if self._execute_stmt(query) is CmdStatus.SUCCESS:
-      self.current_db = args
+      self.current_db = args.strip('`').strip()
+      self.prompt = ImpalaShell.PROMPT_FORMAT.format(host=self.impalad[0],
+                                                     port=self.impalad[1],
+                                                     db=self.current_db)
+    elif args.strip('`') == self.current_db:
+      # args == current_db means -d option was passed but the "use [db]" operation failed.
+      # We need to set the current_db to None so that it does not show a database, which
+      # may not exist.
+      self.current_db = None
+      return CmdStatus.ERROR
     else:
       return CmdStatus.ERROR
 

http://git-wip-us.apache.org/repos/asf/impala/blob/830e3346/tests/shell/test_shell_interactive.py
----------------------------------------------------------------------
diff --git a/tests/shell/test_shell_interactive.py b/tests/shell/test_shell_interactive.py
index e9049fa..4065f9a 100755
--- a/tests/shell/test_shell_interactive.py
+++ b/tests/shell/test_shell_interactive.py
@@ -58,11 +58,11 @@ class TestImpalaShellInteractive(object):
   def teardown_class(cls):
     restore_shell_history(cls.tempfile_name)
 
-  def _expect_with_cmd(self, proc, cmd, expectations=()):
+  def _expect_with_cmd(self, proc, cmd, expectations=(), db="default"):
     """Executes a command on the expect process instance and verifies a set of
     assertions defined by the expections."""
     proc.sendline(cmd + ";")
-    proc.expect(":21000] >")
+    proc.expect(":21000] {db}>".format(db=db))
     if not expectations: return
     for e in expectations:
       assert e in proc.before
@@ -71,7 +71,7 @@ class TestImpalaShellInteractive(object):
   def test_local_shell_options(self):
     """Test that setting the local shell options works"""
     proc = pexpect.spawn(SHELL_CMD)
-    proc.expect(":21000] >")
+    proc.expect(":21000] default>")
     self._expect_with_cmd(proc, "set", ("LIVE_PROGRESS: False", "LIVE_SUMMARY: False"))
     self._expect_with_cmd(proc, "set live_progress=true")
     self._expect_with_cmd(proc, "set", ("LIVE_PROGRESS: True", "LIVE_SUMMARY: False"))
@@ -179,11 +179,14 @@ class TestImpalaShellInteractive(object):
     assert get_num_open_sessions(initial_impala_service) == num_sessions_initial + 1, \
         "Not connected to %s:21000" % hostname
     p.send_cmd("connect %s:21001" % hostname)
+
     # Wait for a little while
     sleep(2)
     # The number of sessions on the target impalad should have been incremented.
     assert get_num_open_sessions(target_impala_service) == num_sessions_target + 1, \
         "Not connected to %s:21001" % hostname
+    assert "[%s:21001] default>" % hostname in p.get_result().stdout
+
     # The number of sessions on the initial impalad should have been decremented.
     assert get_num_open_sessions(initial_impala_service) == num_sessions_initial, \
         "Connection to %s:21000 should have been closed" % hostname
@@ -260,7 +263,7 @@ class TestImpalaShellInteractive(object):
       os.remove(SHELL_HISTORY_FILE)
     assert not os.path.exists(SHELL_HISTORY_FILE)
     child_proc = pexpect.spawn(SHELL_CMD)
-    child_proc.expect(":21000] >")
+    child_proc.expect(":21000] default>")
     self._expect_with_cmd(child_proc, "@1", ("Command index out of range"))
     self._expect_with_cmd(child_proc, "rerun -1", ("Command index out of range"))
     self._expect_with_cmd(child_proc, "select 'first_command'", ("first_command"))
@@ -268,7 +271,7 @@ class TestImpalaShellInteractive(object):
     self._expect_with_cmd(child_proc, "@ -1", ("first_command"))
     self._expect_with_cmd(child_proc, "select 'second_command'", ("second_command"))
     child_proc.sendline('history;')
-    child_proc.expect(":21000] >")
+    child_proc.expect(":21000] default>")
     assert '[1]: select \'first_command\';' in child_proc.before;
     assert '[2]: select \'second_command\';' in child_proc.before;
     assert '[3]: history;' in child_proc.before;
@@ -498,6 +501,35 @@ class TestImpalaShellInteractive(object):
     result = run_impala_shell_interactive(query)
     assert '| id   |' in result.stdout
 
+  @pytest.mark.execute_serially
+  def test_shell_prompt(self):
+    proc = pexpect.spawn(SHELL_CMD)
+    proc.expect(":21000] default>")
+    self._expect_with_cmd(proc, "use foo", (), 'default')
+    self._expect_with_cmd(proc, "use functional", (), 'functional')
+    self._expect_with_cmd(proc, "use foo", (), 'functional')
+    self._expect_with_cmd(proc, 'use `tpch`', (), 'tpch')
+    self._expect_with_cmd(proc, 'use ` tpch `', (), 'tpch')
+
+    proc = pexpect.spawn(SHELL_CMD, ['-d', 'functional'])
+    proc.expect(":21000] functional>")
+    self._expect_with_cmd(proc, "use foo", (), 'functional')
+    self._expect_with_cmd(proc, "use tpch", (), 'tpch')
+    self._expect_with_cmd(proc, "use foo", (), 'tpch')
+
+    proc = pexpect.spawn(SHELL_CMD, ['-d', ' functional '])
+    proc.expect(":21000] functional>")
+
+    proc = pexpect.spawn(SHELL_CMD, ['-d', '` functional `'])
+    proc.expect(":21000] functional>")
+
+    # Start an Impala shell with an invalid DB.
+    proc = pexpect.spawn(SHELL_CMD, ['-d', 'foo'])
+    proc.expect(":21000] default>")
+    self._expect_with_cmd(proc, "use foo", (), 'default')
+    self._expect_with_cmd(proc, "use functional", (), 'functional')
+    self._expect_with_cmd(proc, "use foo", (), 'functional')
+
 def run_impala_shell_interactive(input_lines, shell_args=None):
   """Runs a command in the Impala shell interactively."""
   # if argument "input_lines" is a string, makes it into a list