Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions arrow/compute/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,22 @@ func (c *CastSuite) TestDecimal128ToInt() {
opts.AllowDecimalTruncate = true
c.checkCastArr(negScale, arrow.PrimitiveTypes.Int64, `[1234567890000, -120000]`, *opts)
})

c.Run("int64 bounds inclusive", func() {
opts.AllowIntOverflow = false
opts.AllowDecimalTruncate = false

atBounds, _, _ := array.FromJSON(c.mem, &arrow.Decimal128Type{Precision: 38, Scale: 0},
strings.NewReader(`["9223372036854775807", "-9223372036854775808", null]`))
defer atBounds.Release()
c.checkCastArr(atBounds, arrow.PrimitiveTypes.Int64,
`[9223372036854775807, -9223372036854775808, null]`, *opts)

beyondBounds, _, _ := array.FromJSON(c.mem, &arrow.Decimal128Type{Precision: 38, Scale: 0},
strings.NewReader(`["9223372036854775808", "-9223372036854775809"]`))
defer beyondBounds.Release()
checkCastFails(c.T(), beyondBounds, *opts)
})
}

func (c *CastSuite) TestDecimal256ToInt() {
Expand Down Expand Up @@ -828,6 +844,22 @@ func (c *CastSuite) TestDecimal256ToInt() {
opts.AllowDecimalTruncate = true
c.checkCastArr(negScale, arrow.PrimitiveTypes.Int64, `[1234567890000, -120000]`, *opts)
})

c.Run("int64 bounds inclusive", func() {
opts.AllowIntOverflow = false
opts.AllowDecimalTruncate = false

atBounds, _, _ := array.FromJSON(c.mem, &arrow.Decimal256Type{Precision: 40, Scale: 0},
strings.NewReader(`["9223372036854775807", "-9223372036854775808", null]`))
defer atBounds.Release()
c.checkCastArr(atBounds, arrow.PrimitiveTypes.Int64,
`[9223372036854775807, -9223372036854775808, null]`, *opts)

beyondBounds, _, _ := array.FromJSON(c.mem, &arrow.Decimal256Type{Precision: 40, Scale: 0},
strings.NewReader(`["9223372036854775808", "-9223372036854775809"]`))
defer beyondBounds.Release()
checkCastFails(c.T(), beyondBounds, *opts)
})
}

func (c *CastSuite) TestIntegerToDecimal() {
Expand Down
7 changes: 5 additions & 2 deletions arrow/compute/internal/kernels/numeric_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ func CastIntegerToFloating(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.

type decimal[T decimal128.Num | decimal256.Num] interface {
Less(T) bool
GreaterEqual(T) bool
Greater(T) bool
LowBits() uint64
}

func decimalToIntImpl[InT decimal128.Num | decimal256.Num, OutT arrow.IntType | arrow.UintType](allowOverflow bool, min, max InT, v decimal[InT], err *error) OutT {
if !allowOverflow && (v.Less(min) || v.GreaterEqual(max)) {
// min and max are the inclusive bounds of the output integer type, so a
// value equal to max (e.g. math.MaxInt64) is in range. Use a strict
// greater-than check rather than >= so the endpoints are not rejected.
if !allowOverflow && (v.Less(min) || v.Greater(max)) {
debug.Log("integer value out of bounds from decimal")
*err = fmt.Errorf("%w: integer value out of bounds", arrow.ErrInvalid)
return OutT(0)
Expand Down
Loading