diff --git a/shell/impala_shell.py b/shell/impala_shell.py index 3aea2ea..61c8399 100755 --- a/shell/impala_shell.py +++ b/shell/impala_shell.py @@ -210,6 +210,61 @@ class ImpalaShell(cmd.Cmd): args = ' '.join(tokens).rstrip(ImpalaShell.CMD_DELIM) return args + def __check_delimiter(self, cmd): + """Check for a delimiter to verify whether the input is incomplete or not + If the input is incomplete: + - return False + If the input is complete: + - return True + """ + init_state = 0 + sq_state = 1 + dq_state =2 + semicolon_state = 3 + current_state = init_state + if not cmd: + return False + if self.partial_cmd: + cmd = "%s\n%s" % (self.partial_cmd, cmd) + i = 0 + while i < len(cmd): + ch = cmd[i] + i += 1 + if current_state == init_state: + if ch == '\'': + current_state = sq_state + elif ch == '"': + current_state = dq_state + elif ch == ';': + current_state = semicolon_state + else: + continue + elif current_state == sq_state: + if ch == '\\': + i += 1 + elif ch == '\'': + current_state = init_state + else: + continue + elif current_state == dq_state: + if ch == '\\': + i += 1 + elif ch == '"': + current_state = init_state + else: + continue + elif current_state == semicolon_state: + if ch == ' ' or ch == '\t' or ch == '\n': + continue + else: + current_state = init_state + #else: + # wrong state + if current_state == semicolon_state: + return True + else: + return False + def __check_for_command_completion(self, cmd): """Check for a delimiter at the end of user input. @@ -228,8 +283,11 @@ class ImpalaShell(cmd.Cmd): """ if self.readline: current_history_len = self.readline.get_current_history_length() + iscompleted = False + if cmd.endswith(ImpalaShell.CMD_DELIM): + iscompleted = self.__check_delimiter(cmd) # Input is incomplete, store the contents and do nothing. - if not cmd.endswith(ImpalaShell.CMD_DELIM): + if not iscompleted: # The user input is incomplete, change the prompt to reflect this. if not self.partial_cmd and cmd: self.cached_prompt = self.prompt