Clickhouse: order by with formatReadableSize orders alphabetically instead of numerically

Created on 17 Jul 2019  Â·  4Comments  Â·  Source: ClickHouse/ClickHouse

This is probably due to the formatReadableSize being applied before the results get displayed, but it would be super nice if order by still ordered by the original values.

question usability

Most helpful comment

this is contradicts SQL.

you can use
select formatReadableSize(size)
...
order by size

All 4 comments

this is contradicts SQL.

you can use
select formatReadableSize(size)
...
order by size

It is not a bug. formatReadableSize return strings, so they are ordered as strings.

Use the approach shown in @den-crane 's answer,

@abraithwaite

Consider more simple example:

SELECT toString(number) AS x FROM numbers(100) ORDER BY x

It's obvious that this query sort data alphabetically (by String value). And it is the only way it should work.

formatReadableSize is similar. This function returns String data type, not some String but sort numerically magic.

Thanks! I was aware that it was being converted to a string but didn't realize I could order by the original field. Works great!

Was this page helpful?
0 / 5 - 0 ratings