Backport of Fix TopN with BySegment=true (#845) for 0.6.x

See https://github.com/metamx/druid/pull/856 for more details
Does not include unit tests