Make double/real->decimal cast compatible with cast to varchar#9595
Make double/real->decimal cast compatible with cast to varchar#9595losipiuk merged 2 commits intoprestodb:masterfrom
Conversation
There was a problem hiding this comment.
cmt msg
Make double/real->decimal cast compatible with cast to varchar ...
s/discourage/&d
s/as s/as the s
s/change/&,
s/naturally prints out/is printed
s/out/as
s/when/and &
There was a problem hiding this comment.
Is catch (ArithmeticException below still reachable?
There was a problem hiding this comment.
Yeah - encodeScaledValue can throw it.
There was a problem hiding this comment.
Is catch (ArithmeticException below still reachable?
There was a problem hiding this comment.
Is this a valid double value? Does it fit? You could try using hex floating point literals to represent test doubles: https://stackoverflow.com/questions/25712348/hexadecimal-floating-point-literals.
According to standard we want to make sure that Mantissa is not truncated right? What is the implicit precision of double?
There was a problem hiding this comment.
After offline discussion. It would be nice to have round trip tests to ensure mantissa information is not truncated.
There was a problem hiding this comment.
It is string which can be interpreted as a valid double (this one: 1234567890123456824475648). By valid hear I mean the value which is exactly representable by IEEE 754-1985 double precision.
There was a problem hiding this comment.
I am not sure we should give up such easily on optimized conversion. Is there a code we could reuse to implement conversion correctly? What does Hive use?
There was a problem hiding this comment.
It does not use this implementation (as results are different). I don't know if their implementation uses intermediate String object.
|
@martint regarding your comment: #9422 (comment) The 2003 standard says: So it would appear that we shouldn't truncate significant binary digits of an approximate numeric mantissa. Assuming that implicitly double has infinite precision, then our previous implementation is actually conforming to the standard although it is less intuitive. @losipiuk could you check what other databases do? EDIT: it could be that both implementations are correct as they choose different textual representation of double, but both translate to the same double value. |
I checked PSQL and it behaves as code from this PR: Oracle does some weird sh***
I think so. I did not read it anywhere but my guess is that the double to decimal convertion Hive/PSQL uses (which is compatible with |
e1b4771 to
3f174a8
Compare
|
AC |
There was a problem hiding this comment.
add TODO for faster implementation
There was a problem hiding this comment.
I don't think this be revisited unless benchmarks show we should do it. We could place such TODOs in quite a few places in the code, would that be useful?
There was a problem hiding this comment.
I added them - they are no harm. Maybe it will motivate sb to do the benchmarks in the future.
23be055 to
a3edf71
Compare
Casting approximate numerics (DOUBLE and REAL) to DECIMAL used encoding which exposed all digits from decimal representation of values stored by floating point number (for digits before decimal point). This resulted in somewhat unexpected results. E.g. cast (double '100000000000000000000000000000000' as decimal(38)) returned 100000000000000005366162204393472. If you look at BigDecimal(double) constructor (which is discourage to be used), it has the same behaviour. After this change, the encoding of double will be compatible with casting double to varchar, and also how it naturally is printed out in CLI. The double '100000000000000000000000000000000' is printed as '1.0E32' and cast to decimal(38) the value will match that.
a3edf71 to
7422703
Compare
fixes #9575
cc: @martint