Skip to content

Commit

Permalink
Fixed bug harelba#89 - Output formatting attempts to apply to header row
Browse files Browse the repository at this point in the history
  • Loading branch information
harelba committed Jan 26, 2015
1 parent c7e1dea commit 2386a30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 6 additions & 2 deletions bin/q
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#
# Run with --help for command line details
#
q_version = "1.5.0"
q_version = "1.6.0notreleasedyet"

__all__ = [ 'QTextAsData' ]

Expand Down Expand Up @@ -1353,8 +1353,9 @@ class QOutputPrinter(object):
data.insert(0,results.metadata.output_column_name_list)
for rownum, row in enumerate(data):
row_str = []
skip_formatting = rownum == 0 and self.output_params.output_header
for i, col in enumerate(row):
if formatting_dict is not None and str(i + 1) in formatting_dict.keys():
if formatting_dict is not None and str(i + 1) in formatting_dict.keys() and not skip_formatting:
fmt_str = formatting_dict[str(i + 1)]
else:
if self.output_params.beautify:
Expand All @@ -1371,6 +1372,9 @@ class QOutputPrinter(object):
except (UnicodeEncodeError, UnicodeError), e:
print >>sys.stderr, "Cannot encode data. Error:%s" % e
sys.exit(3)
except TypeError,e:
print >>sys.stderr, "Error while formatting output: %s" % e
sys.exit(4)
except IOError, e:
if e.errno == 32:
# broken pipe, that's ok
Expand Down
15 changes: 14 additions & 1 deletion test/test-suite
Original file line number Diff line number Diff line change
Expand Up @@ -1582,15 +1582,28 @@ class ParsingModeTests(AbstractQTestCase):
class FormattingTests(AbstractQTestCase):

def test_column_formatting(self):
cmd = 'seq 1 10 | ../bin/q -f 1=%4.3f,2=%4.3f "select sum(c1),avg(c1) from -"'
cmd = 'seq 1 10 | ../bin/q -f 1=%4.3f,2=%4.3f "select sum(c1),avg(c1) from -" -c 1'

retcode, o, e = run_command(cmd)

self.assertEquals(retcode, 0)
self.assertEquals(len(o), 1)
self.assertEquals(len(e), 0)

self.assertEquals(o[0], '55.000 5.500')

def test_column_formatting_with_output_header(self):
cmd = 'seq 1 10 | sed "1i column_name" | ../bin/q -f 1=%4.3f,2=%4.3f "select sum(column_name) mysum,avg(column_name) myavg from -" -c 1 -H -O'

retcode, o, e = run_command(cmd)

self.assertEquals(retcode, 0)
self.assertEquals(len(o), 2)
self.assertEquals(len(e), 0)

self.assertEquals(o[0], 'mysum myavg')
self.assertEquals(o[1], '55.000 5.500')


class SqlTests(AbstractQTestCase):

Expand Down

0 comments on commit 2386a30

Please sign in to comment.