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 2019/03/23 23:38:54 UTC

[impala] 03/10: IMPALA-6223: Gracefully handle malformed 'with' queries in impala-shell

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

tarmstrong pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 1aae0c6b4e7d835a461dd0fcc2a418a8269b645b
Author: poojanilangekar <po...@cloudera.com>
AuthorDate: Thu Jul 5 15:52:23 2018 -0700

    IMPALA-6223: Gracefully handle malformed 'with' queries in impala-shell
    
    The change handles the exception thrown by shlex while parsing a
    malformed query.
    
    This patch was tested by adding both commandline and interactive
    shell tests.
    
    Change-Id: Ibb1e9238ac67b8ad3b2caa1748a18b04f384802d
    Reviewed-on: http://gerrit.cloudera.org:8080/10876
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
    Reviewed-on: http://gerrit.cloudera.org:8080/12800
    Reviewed-by: Fredy Wijaya <fw...@cloudera.com>
---
 shell/impala_shell.py                 | 15 +++++++++------
 tests/shell/test_shell_commandline.py |  9 +++++++++
 tests/shell/test_shell_interactive.py |  7 +++++++
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/shell/impala_shell.py b/shell/impala_shell.py
index 5f83144..a6ab277 100755
--- a/shell/impala_shell.py
+++ b/shell/impala_shell.py
@@ -1145,12 +1145,15 @@ class ImpalaShell(object, cmd.Cmd):
     lexer = shlex.shlex(sqlparse.format(query.query.lstrip(), strip_comments=True)
                         .encode('utf-8'), posix=True)
     lexer.escapedquotes += "'"
-    # Because the WITH clause may precede DML or SELECT queries,
-    # just checking the first token is insufficient.
-    is_dml = False
-    tokens = list(lexer)
-    if filter(self.DML_REGEX.match, tokens): is_dml = True
-    return self._execute_stmt(query, is_dml=is_dml, print_web_link=True)
+    try:
+      # Because the WITH clause may precede DML or SELECT queries,
+      # just checking the first token is insufficient.
+      is_dml = False
+      tokens = list(lexer)
+      if filter(self.DML_REGEX.match, tokens): is_dml = True
+      return self._execute_stmt(query, is_dml=is_dml, print_web_link=True)
+    except ValueError as e:
+      return self._execute_stmt(query, print_web_link=True)
 
   def do_use(self, args):
     """Executes a USE... query"""
diff --git a/tests/shell/test_shell_commandline.py b/tests/shell/test_shell_commandline.py
index 7fca23b..858e46b 100644
--- a/tests/shell/test_shell_commandline.py
+++ b/tests/shell/test_shell_commandline.py
@@ -658,3 +658,12 @@ class TestImpalaShell(ImpalaTestSuite):
       self._validate_expected_socket_connected(args2, s)
     finally:
       s.close()
+
+  def test_malformed_query(self):
+    """Test that malformed queries are handled by the commandline shell"""
+    args = " -q \"with foo as (select bar from temp where temp.a='\""
+    result = run_impala_shell_cmd(args, expect_success=False)
+    assert "Encountered: EOF" in result.stderr
+    args = "-q \"with v as (select 1) \;\""
+    result = run_impala_shell_cmd(args, expect_success=False)
+    assert "Encountered: Unexpected character" in result.stderr
diff --git a/tests/shell/test_shell_interactive.py b/tests/shell/test_shell_interactive.py
index d0308d8..c957b2b 100755
--- a/tests/shell/test_shell_interactive.py
+++ b/tests/shell/test_shell_interactive.py
@@ -652,6 +652,13 @@ class TestImpalaShellInteractive(object):
     assert (None, 'select 1') == \
         ImpalaShellClass.strip_leading_comment('select 1')
 
+  def test_malformed_query(self):
+    """Test the handling of malformed query without closing quotation"""
+    shell = ImpalaShell()
+    query = "with v as (select 1) \nselect foo('\\\\'), ('bar \n;"
+    shell.send_cmd(query)
+    result = shell.get_result()
+    assert "ERROR: AnalysisException: Unmatched string literal" in result.stderr
 
 def run_impala_shell_interactive(input_lines, shell_args=None):
   """Runs a command in the Impala shell interactively."""