Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Won't Fix
-
Impala 2.5.0
-
None
Description
Impala has several versions of round: round(double a), round(double a, int d), round(decimal a, int_type d)
round(double a) returns a BIGINT, which makes sense because it rounds to the nearest int.
round(decimal a, int_type d) returns a DECIMAL, which makes sense because it rounds to a decimal digit.
round(double a, int d) predates DECIMAL support, so it returns a DOUBLE. It is specified to return the nearest double value.
E.g. round(cast(1 as DOUBLE) / 10, 1) returns the binary floating point value closest to 0.1. This number has no exact decimal representation. Both 0.100000000000000000000000000 and 0.10000000000000000555 are valid decimal representations of this floating point number. I.e. if you convert them back to float, you will get the same number.
This is correct according to floating point conversion rules and the Impala documentation, but it is confusing for two reasons:
- round() returning a double is a little surprising, because it can't precisely represent the result
- Impala clients can display the floating-point result in multiple valid ways. Different clients have different algorithms for converting floating-point to decimal for display, so even if Impala returns the same result it may appear as 0.1 in one client and 0.10000000000000000555. We don't specify that clients have to use a particular algorithm, so it's valid as long as it converts back to the same float as part of a round-trip.
We should consider changing the spec of round() in Impala to always return a decimal to avoid this confusion.
Attachments
Issue Links
- blocks
-
IMPALA-4924 Make DECIMAL_V2 the default mode
- Resolved
- is superceded by
-
IMPALA-6230 The output type of a round() function should match the input type
- Resolved