zeroshade commented on code in PR #1691:
URL: https://github.com/apache/arrow-go/pull/1691#discussion_r4721736011


##########
arrow/compute/cast_test.go:
##########
@@ -733,6 +733,46 @@ func (c *CastSuite) TestFloatingToInt() {
        }
 }
 
+func (c *CastSuite) TestFloatingToIntOutOfRange() {
+       // tooLarge is the smallest power of two the target can't hold. 
Out-of-range
+       // conversions saturate on arm64, and for 32- and 64-bit targets the 
saturated
+       // maximum rounds back to that power of two. The narrower targets are 
controls:
+       // their maximum is exact in the input type.
+       tests := []struct {
+               from     arrow.DataType
+               to       arrow.DataType
+               tooLarge string
+               inRange  string
+               expected string
+       }{
+               {arrow.PrimitiveTypes.Float64, arrow.PrimitiveTypes.Int8, 
`[128]`, `[127, -128]`, `[127, -128]`},
+               {arrow.PrimitiveTypes.Float64, arrow.PrimitiveTypes.Uint8, 
`[256]`, `[255]`, `[255]`},
+               {arrow.PrimitiveTypes.Float32, arrow.PrimitiveTypes.Int32, 
`[2147483648]`,
+                       `[2147483520, -2147483648]`, `[2147483520, 
-2147483648]`},
+               {arrow.PrimitiveTypes.Float32, arrow.PrimitiveTypes.Uint32, 
`[4294967296]`, `[4294967040]`, `[4294967040]`},
+               {arrow.PrimitiveTypes.Float32, arrow.PrimitiveTypes.Int64, 
`[9223372036854775808]`,
+                       `[9223371487098961920, -9223372036854775808]`, 
`[9223371487098961920, -9223372036854775808]`},
+               {arrow.PrimitiveTypes.Float64, arrow.PrimitiveTypes.Int64, 
`[9223372036854775808]`,
+                       `[9223372036854774784, -9223372036854775808]`, 
`[9223372036854774784, -9223372036854775808]`},
+               {arrow.PrimitiveTypes.Float32, arrow.PrimitiveTypes.Uint64, 
`[18446744073709551616]`,
+                       `[18446742974197923840]`, `[18446742974197923840]`},
+               {arrow.PrimitiveTypes.Float64, arrow.PrimitiveTypes.Uint64, 
`[18446744073709551616]`,
+                       `[18446744073709549568]`, `[18446744073709549568]`},
+               {arrow.FixedWidthTypes.Float16, arrow.PrimitiveTypes.Int16, 
`[32768]`, `[32752, -32768]`, `[32752, -32768]`},
+       }
+
+       for _, tt := range tests {
+               c.Run(tt.from.String()+" to "+tt.to.String(), func() {
+                       c.checkCastFails(tt.from, tt.tooLarge, 
compute.SafeCastOptions(tt.to))
+                       c.checkCastFails(tt.from, `[1, null, `+tt.tooLarge[1:], 
compute.SafeCastOptions(tt.to))
+                       c.checkCast(tt.from, tt.to, tt.inRange, tt.expected)

Review Comment:
   nit: slicing the JSON literal (`tt.tooLarge[1:]`) to splice in the null case 
is a bit opaque. Consider a separate field for the null-containing input, or 
storing the bare number and wrapping it in both places.



##########
arrow/compute/internal/kernels/numeric_cast.go:
##########
@@ -584,30 +585,34 @@ func boolToFloat16(_ *exec.KernelCtx, in []byte, out 
[]float16.Num) error {
        return nil
 }
 
-func wasTrunc[InT constraints.Float | float16.Num, OutT arrow.IntType | 
arrow.UintType](out OutT, in InT) bool {
+func wasTrunc[InT constraints.Float | float16.Num, OutT arrow.IntType | 
arrow.UintType](out OutT, in InT, upper float64) bool {
        switch v := any(in).(type) {
        case float16.Num:

Review Comment:
   nit: the float16 `-0.0` fix is a separate user-visible behavior change from 
the upper-bound fix. It is mentioned in the description, but worth calling out 
in the commit body as well so it is easy to find when bisecting.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to