There's a logical error here

https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/cast.cc#L239

The cast

constexpr in_type kMax =
static_cast<in_type>(std::numeric_limits<out_type>::max());

is overflowing int32_t

This code path was only written for the unsigned-to-signed case. The
functors need to be reworked to accommodate the signed-to-unsigned
cast
On Wed, Nov 14, 2018 at 8:36 AM Francois Saint-Jacques
<fsaintjacq...@networkdump.com> wrote:
>
> Seems like the type combination you're using (int32 -> uint32) and (int32
> -> uint64) don't match the following pattern-matching
>
> https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/cast.cc#L191-L192
>
> which avoid using "safe" cast and revert to the following cast
> implementation (with no explicit check):
>
> https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/cast.cc#L316-L318
>
> My take on this is that downcasting concept should only apply to same sign
> integers. I'd propose the following refactor:
>
> 1. Rename `allow_int_overflow` to `allow_int_truncate`. Since truncation is
> still applicable even if there's no downcasting.
> 2. Refactor code such that the pattern matching is done on possible
> truncation and not downcasting. The fast casting is only applied to
> same-sign and size(O) >= size(I). The (possible according to
> allow_int_truncate) slow and bound checking is done when signed-ness is not
> matching or there's downcasting.
>
> Can you open a JIRA ticket?
>
> François
>
> On Wed, Nov 14, 2018 at 6:10 AM Romain Francois <rom...@purrple.cat> wrote:
>
> > I'm implementing Array$cast() in the R package, i.e.
> > https://issues.apache.org/jira/browse/ARROW-3741?filter=12344983
> >
> > I'm seeing some weird results when casting from int32 to uint32 and uint64
> > (I'm expecting errors here as it's supposed to be safe
> >
> > https://github.com/apache/arrow/pull/2959#issuecomment-438606974
> >
> > a <- array(-(1:3))
> > a$cast(uint16())
> > #> Error in Array__cast(self, target_type, options): Invalid: Integer
> > value out of bounds
> > a$cast(uint16())
> > #> Error in Array__cast(self, target_type, options): Invalid: Integer
> > value out of bounds
> > a$cast(uint32())
> > #> arrow::Array
> > #> [
> > #>   4294967295,
> > #>   4294967294,
> > #>   4294967293
> > #> ]
> > a$cast(uint32())$type()
> > #> arrow::UInt32
> > #> uint32
> >
> > a$cast(uint64())
> > #> arrow::Array
> > #> [
> > #>   -1,
> > #>   -2,
> > #>   -3
> > #> ]
> > a$cast(uint64())$type()
> > #> arrow::UInt64
> > #> uint64
> >
> > I don't think this is an R issue.
> >
> > Can someone using other front ends to the C++ library check if they get
> > the same problem ?
> >
> >
> >
> >
>
> --
> Sent from my jetpack.

Reply via email to