Skip to content

Commit 4ec231b

Browse files
authored
GH-35911: [Go] Fix method CastToBytes of decimal256Traits (#35912)
### Rationale for this change ### What changes are included in this PR? ### Are these changes tested? Yes ### Are there any user-facing changes? Yes * Closes: #35911 Authored-by: izveigor <izveigor@gmail.com> Signed-off-by: Matt Topol <zotthewizard@gmail.com>
1 parent 3d0172d commit 4ec231b

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

go/arrow/type_traits_decimal256.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (decimal256Traits) CastToBytes(b []decimal256.Num) []byte {
5959
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
6060

6161
var res []byte
62-
s := (*reflect.SliceHeader)(unsafe.Pointer(&b))
62+
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
6363
s.Data = h.Data
6464
s.Len = h.Len * Decimal256SizeBytes
6565
s.Cap = h.Cap * Decimal256SizeBytes

go/arrow/type_traits_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/apache/arrow/go/v13/arrow"
2626
"github.com/apache/arrow/go/v13/arrow/decimal128"
27+
"github.com/apache/arrow/go/v13/arrow/decimal256"
2728
"github.com/apache/arrow/go/v13/arrow/float16"
2829
)
2930

@@ -133,6 +134,50 @@ func TestDecimal128Traits(t *testing.T) {
133134
}
134135
}
135136

137+
func TestDecimal256Traits(t *testing.T) {
138+
const N = 10
139+
nbytes := arrow.Decimal256Traits.BytesRequired(N)
140+
b1 := arrow.Decimal256Traits.CastToBytes([]decimal256.Num{
141+
decimal256.New(0, 0, 0, 10),
142+
decimal256.New(1, 1, 1, 10),
143+
decimal256.New(2, 2, 2, 10),
144+
decimal256.New(3, 3, 3, 10),
145+
decimal256.New(4, 4, 4, 10),
146+
decimal256.New(5, 5, 5, 10),
147+
decimal256.New(6, 6, 6, 10),
148+
decimal256.New(7, 7, 7, 10),
149+
decimal256.New(8, 8, 8, 10),
150+
decimal256.New(9, 9, 9, 10),
151+
})
152+
153+
b2 := make([]byte, nbytes)
154+
for i := 0; i < N; i++ {
155+
beg := i * arrow.Decimal256SizeBytes
156+
end := (i + 1) * arrow.Decimal256SizeBytes
157+
arrow.Decimal256Traits.PutValue(b2[beg:end], decimal256.New(uint64(i), uint64(i), uint64(i), 10))
158+
}
159+
160+
if !reflect.DeepEqual(b1, b2) {
161+
v1 := arrow.Decimal256Traits.CastFromBytes(b1)
162+
v2 := arrow.Decimal256Traits.CastFromBytes(b2)
163+
t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
164+
}
165+
166+
v1 := arrow.Decimal256Traits.CastFromBytes(b1)
167+
for i, v := range v1 {
168+
if got, want := v, decimal256.New(uint64(i), uint64(i), uint64(i), 10); got != want {
169+
t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
170+
}
171+
}
172+
173+
v2 := make([]decimal256.Num, N)
174+
arrow.Decimal256Traits.Copy(v2, v1)
175+
176+
if !reflect.DeepEqual(v1, v2) {
177+
t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
178+
}
179+
}
180+
136181
func TestMonthIntervalTraits(t *testing.T) {
137182
const N = 10
138183
b1 := arrow.MonthIntervalTraits.CastToBytes([]arrow.MonthInterval{

0 commit comments

Comments
 (0)