diff --git a/shell/shell_output.py b/shell/shell_output.py index becc4dd06b..608a8ca92b 100644 --- a/shell/shell_output.py +++ b/shell/shell_output.py @@ -90,7 +90,14 @@ def format(self, rows): row = [val.encode('utf-8', 'replace') if isinstance(val, unicode) else val for val in row] writer.writerow(row) - rows = temp_buffer.getvalue().rstrip() + # The CSV writer produces an extra newline. Strip that extra newline (and + # only that extra newline). csv wraps newlines for data values in quotes, + # so rstrip will be limited to the extra newline. + if sys.version_info.major == 2: + # Python 2 is in encoded Unicode bytes, so this needs to be a bytes \n. + rows = temp_buffer.getvalue().rstrip(b'\n') + else: + rows = temp_buffer.getvalue().rstrip('\n') temp_buffer.close() return rows diff --git a/tests/shell/test_shell_commandline.py b/tests/shell/test_shell_commandline.py index 7b410d3339..6fda37a326 100644 --- a/tests/shell/test_shell_commandline.py +++ b/tests/shell/test_shell_commandline.py @@ -1254,3 +1254,13 @@ def test_http_socket_timeout(self, vector): result = run_impala_shell_cmd(vector, args + ['--http_socket_timeout_s=None']) assert result.stderr == "" assert result.stdout == "0\n" + + def test_trailing_whitespace(self, vector): + """Test CSV output with trailing whitespace""" + + # Ten trailing spaces + query = "select 'Trailing Whitespace '" + # Only one column, no need for output_delimiter + output = run_impala_shell_cmd(vector, ['-q', query, '-B']) + assert "Fetched 1 row(s)" in output.stderr + assert "Trailing Whitespace \n" in output.stdout