[
https://issues.apache.org/jira/browse/ARROW-17606?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17600759#comment-17600759
]
Joris Van den Bossche commented on ARROW-17606:
-----------------------------------------------
This might be too naive, but when casting float to int, AFAIK we check for
truncation by comparing the int back to float and comparing float equality with
the input value. Can we do something similar here? Get the float representation
of the decimal, and compare with the original float?
> [C++] Cast float to decimal truncates
> -------------------------------------
>
> Key: ARROW-17606
> URL: https://issues.apache.org/jira/browse/ARROW-17606
> Project: Apache Arrow
> Issue Type: Bug
> Components: C++
> Reporter: Neal Richardson
> Priority: Major
> Fix For: 10.0.0
>
>
> Even with all of the cast options to allow truncation and overflow set to
> False, cast from double to decimal truncates.
> Short reproducer in R:
> {code}
> library(arrow, warn.conflicts = FALSE)
> a <- Scalar$create(sqrt(2))
> a
> #> Scalar
> #> 1.4142135623730951
> a$cast(decimal(5, 1))
> #> Scalar
> #> 1.4
> {code}
> Longer version that shows the cast options:
> {code}
> library(arrow, warn.conflicts = FALSE)
> library(dplyr, warn.conflicts = FALSE)
> tab <- arrow_table(a = sqrt(2))
> tab
> #> Table
> #> 1 rows x 1 columns
> #> $a <double>
> tab$a
> #> ChunkedArray
> #> <double>
> #> [
> #> [
> #> 1.4142135623730951
> #> ]
> #> ]
> # See that all of the cast options are false:
> tab %>%
> mutate(b = cast(a, decimal(5, 1)))
> #> Table (query)
> #> a: double
> #> b: decimal128(5, 1) (cast(a, {to_type=decimal128(5, 1),
> allow_int_overflow=false, allow_time_truncate=false,
> allow_time_overflow=false, allow_decimal_truncate=false,
> allow_float_truncate=false, allow_invalid_utf8=false}))
> #>
> #> See $.data for the source Arrow object
> new_tab <- tab %>%
> mutate(b = cast(a, decimal(5, 1))) %>%
> compute()
> new_tab
> #> Table
> #> 1 rows x 2 columns
> #> $a <double>
> #> $b <decimal128(5, 1)>
> new_tab$b
> #> ChunkedArray
> #> <decimal128(5, 1)>
> #> [
> #> [
> #> 1.4
> #> ]
> #> ]
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)