You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by jo...@apache.org on 2020/02/24 21:22:44 UTC

[impala] 01/02: IMPALA-9036: Fix CTRL+C a multiline query in impala-shell

This is an automated email from the ASF dual-hosted git repository.

joemcdonnell pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 0282c024b45c627695c217d2a714ee9c752fd7c1
Author: Adam Tamas <ta...@cloudera.com>
AuthorDate: Tue Feb 11 16:42:36 2020 +0100

    IMPALA-9036: Fix CTRL+C a multiline query in impala-shell
    
    Modified the '_signal_handler()' in impala-shell.py so when a user
    cancels a multiline query by hitting CTRL+C it will cancel the
    query, instead of just the current line.
    
    Testing:
    -Added 'test_cancellation_mid_command()' to test_shell_interactive.py
    to test if it really cancels the partial commands.
    -Manually tested by giving partial commands then cancelling them.
    
    Change-Id: Id8d8bdaee929e2655eb66e886ae92a02d3fbd83f
    Reviewed-on: http://gerrit.cloudera.org:8080/15233
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 shell/impala_shell.py                 |  5 +++++
 tests/shell/test_shell_interactive.py | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/shell/impala_shell.py b/shell/impala_shell.py
index 1e0c6d6..74a4c3b 100755
--- a/shell/impala_shell.py
+++ b/shell/impala_shell.py
@@ -557,6 +557,11 @@ class ImpalaShell(object, cmd.Cmd):
   def _signal_handler(self, signal, frame):
     """Handles query cancellation on a Ctrl+C event"""
     if self.last_query_handle is None or self.last_query_handle.is_closed:
+      if self.partial_cmd:
+        # Revert the prompt to its earlier state
+        self.prompt = self.cached_prompt
+        # Reset the already given commands
+        self.partial_cmd = str()
       raise KeyboardInterrupt()
     # Create a new connection to the impalad and cancel the query.
     # TODO: this isn't thread-safe with respect to the main thread executing the
diff --git a/tests/shell/test_shell_interactive.py b/tests/shell/test_shell_interactive.py
index 46447e2..109ca47 100755
--- a/tests/shell/test_shell_interactive.py
+++ b/tests/shell/test_shell_interactive.py
@@ -239,6 +239,40 @@ class TestImpalaShellInteractive(ImpalaTestSuite):
     result = p.get_result()
     assert "^C" in result.stderr
 
+  @pytest.mark.execute_serially
+  def test_cancellation_mid_command(self, vector):
+    """The test starts with sending in a multi-line input without a command delimiter.
+       When the impala-shell is waiting for more input, the test sends a SIGINT signal (to
+       simulate pressing Ctrl-C) followed by a final query terminated with semicolon.
+       The expected behavior for the impala shell is to discard everything before the
+       SIGINT signal was sent and execute the final query only."""
+    shell_cmd = get_shell_cmd(vector)
+    queries = [
+        "line 1\n", "line 2\n", "line 3\n\n", "line 4 and", " 5\n",
+        "line 6\n", "line 7\n", "line 8\n", "line 9\n", "line 10"]
+    # Check when the last line before Ctrl-C doesn't end with newline.
+    child_proc = pexpect.spawn(shell_cmd[0], shell_cmd[1:])
+    for query in queries:
+      child_proc.send(query)
+    child_proc.sendintr()
+    child_proc.send('select "test without newline";\n')
+    child_proc.expect("test without newline")
+    child_proc.sendline('quit;')
+    child_proc.wait()
+    # Check when the last line before Ctrl-C ends with newline.
+    child_proc = pexpect.spawn(shell_cmd[0], shell_cmd[1:])
+    for query in queries:
+      child_proc.send(query)
+    # Sending in a newline so it will end with one
+    child_proc.send("\n")
+    # checking if it realy is a new line
+    child_proc.expect(" > ")
+    child_proc.sendintr()
+    child_proc.send('select "test with newline";\n')
+    child_proc.expect("test with newline")
+    child_proc.sendline('quit;')
+    child_proc.wait()
+
   def test_unicode_input(self, vector):
     "Test queries containing non-ascii input"
     # test a unicode query spanning multiple lines