You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by ar...@apache.org on 2018/07/09 19:50:09 UTC
[2/6] impala git commit: IMPALA-6223: Gracefully handle malformed
'with' queries in impala-shell
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>
Project: http://git-wip-us.apache.org/repos/asf/impala/repo
Commit: http://git-wip-us.apache.org/repos/asf/impala/commit/28162117
Tree: http://git-wip-us.apache.org/repos/asf/impala/tree/28162117
Diff: http://git-wip-us.apache.org/repos/asf/impala/diff/28162117
Branch: refs/heads/master
Commit: 28162117ad462dfe5fd608d4c09ba02ad213285b
Parents: 54b3c60
Author: poojanilangekar <po...@cloudera.com>
Authored: Thu Jul 5 15:52:23 2018 -0700
Committer: Impala Public Jenkins <im...@cloudera.com>
Committed: Sat Jul 7 02:58:40 2018 +0000
----------------------------------------------------------------------
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(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/impala/blob/28162117/shell/impala_shell.py
----------------------------------------------------------------------
diff --git a/shell/impala_shell.py b/shell/impala_shell.py
index 480c908..aea8350 100755
--- a/shell/impala_shell.py
+++ b/shell/impala_shell.py
@@ -1141,12 +1141,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"""
http://git-wip-us.apache.org/repos/asf/impala/blob/28162117/tests/shell/test_shell_commandline.py
----------------------------------------------------------------------
diff --git a/tests/shell/test_shell_commandline.py b/tests/shell/test_shell_commandline.py
index 29df602..93387fa 100644
--- a/tests/shell/test_shell_commandline.py
+++ b/tests/shell/test_shell_commandline.py
@@ -650,3 +650,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
http://git-wip-us.apache.org/repos/asf/impala/blob/28162117/tests/shell/test_shell_interactive.py
----------------------------------------------------------------------
diff --git a/tests/shell/test_shell_interactive.py b/tests/shell/test_shell_interactive.py
index eac9d27..bf4923d 100755
--- a/tests/shell/test_shell_interactive.py
+++ b/tests/shell/test_shell_interactive.py
@@ -654,6 +654,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."""