MCPcopy Create free account
hub / github.com/apache/impala / DelimitedOutputFormatter

Class DelimitedOutputFormatter

shell/impala_shell/shell_output.py:83–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81
82
83class DelimitedOutputFormatter(object):
84 def __init__(self, field_delim="\t"):
85 if field_delim:
86 if sys.version_info.major > 2:
87 # strings do not have a 'decode' method in python 3
88 field_delim_bytes = bytearray(field_delim, 'utf-8')
89 self.field_delim = field_delim_bytes.decode('unicode_escape')
90 else:
91 # csv.writer in python2 requires an ascii string delimiter
92 self.field_delim = field_delim.decode('unicode_escape').encode('ascii', 'ignore')
93 # IMPALA-8652, the delimiter should be a 1-character string and verified already
94 assert len(self.field_delim) == 1
95
96 def format(self, rows):
97 """Returns string containing UTF-8-encoded representation of the table data."""
98 # csv.writer expects a file handle to the input.
99 temp_buffer = StringIO()
100 writer = csv.writer(temp_buffer, delimiter=self.field_delim,
101 lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
102 for row in rows:
103 if sys.version_info.major == 2:
104 row = [val.encode('utf-8', 'replace') if isinstance(val, unicode) # noqa: F821
105 else val for val in row]
106 writer.writerow(row)
107 # The CSV writer produces an extra newline. Strip that extra newline (and
108 # only that extra newline). csv wraps newlines for data values in quotes,
109 # so rstrip will be limited to the extra newline.
110 if sys.version_info.major == 2:
111 # Python 2 is in encoded Unicode bytes, so this needs to be a bytes \n.
112 rows = temp_buffer.getvalue().rstrip(b'\n')
113 else:
114 rows = temp_buffer.getvalue().rstrip('\n')
115 temp_buffer.close()
116 return rows
117
118
119class VerticalOutputFormatter(DelimitedOutputFormatter):

Callers 1

_format_outputstreamMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected