Skip to content

Commit 17e85bf

Browse files
committed
ARROW-5387: [Go] properly handle sub-slice of List
Author: Sebastien Binet <binet@cern.ch> Closes apache#4360 from sbinet/issue-5387 and squashes the following commits: bbc8a83 <Sebastien Binet> ARROW-5387: properly handle sub-slice of List
1 parent 65ae609 commit 17e85bf

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

go/arrow/array/list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ func (a *List) String() string {
5555
o.WriteString("(null)")
5656
continue
5757
}
58-
beg := int64(a.offsets[i])
59-
end := int64(a.offsets[i+1])
58+
j := i + a.array.data.offset
59+
beg := int64(a.offsets[j])
60+
end := int64(a.offsets[j+1])
6061
sub := NewSlice(a.values, beg, end)
6162
fmt.Fprintf(o, "%v", sub)
6263
sub.Release()

go/arrow/array/list_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,65 @@ func TestListArrayBulkAppend(t *testing.T) {
149149
t.Fatalf("got=%v, want=%v", got, want)
150150
}
151151
}
152+
153+
func TestListArraySlice(t *testing.T) {
154+
pool := memory.NewCheckedAllocator(memory.NewGoAllocator())
155+
defer pool.AssertSize(t, 0)
156+
157+
var (
158+
vs = []int32{0, 1, 2, 3, 4, 5, 6}
159+
lengths = []int{3, 0, 4}
160+
isValid = []bool{true, false, true}
161+
offsets = []int32{0, 3, 3, 7}
162+
)
163+
164+
lb := array.NewListBuilder(pool, arrow.PrimitiveTypes.Int32)
165+
defer lb.Release()
166+
vb := lb.ValueBuilder().(*array.Int32Builder)
167+
vb.Reserve(len(vs))
168+
169+
lb.AppendValues(offsets, isValid)
170+
for _, v := range vs {
171+
vb.Append(v)
172+
}
173+
174+
arr := lb.NewArray().(*array.List)
175+
defer arr.Release()
176+
177+
if got, want := arr.DataType().ID(), arrow.LIST; got != want {
178+
t.Fatalf("got=%v, want=%v", got, want)
179+
}
180+
181+
if got, want := arr.Len(), len(isValid); got != want {
182+
t.Fatalf("got=%d, want=%d", got, want)
183+
}
184+
185+
for i := range lengths {
186+
if got, want := arr.IsValid(i), isValid[i]; got != want {
187+
t.Fatalf("got[%d]=%v, want[%d]=%v", i, got, i, want)
188+
}
189+
if got, want := arr.IsNull(i), lengths[i] == 0; got != want {
190+
t.Fatalf("got[%d]=%v, want[%d]=%v", i, got, i, want)
191+
}
192+
}
193+
194+
if got, want := arr.Offsets(), offsets; !reflect.DeepEqual(got, want) {
195+
t.Fatalf("got=%v, want=%v", got, want)
196+
}
197+
198+
varr := arr.ListValues().(*array.Int32)
199+
if got, want := varr.Int32Values(), vs; !reflect.DeepEqual(got, want) {
200+
t.Fatalf("got=%v, want=%v", got, want)
201+
}
202+
203+
if got, want := arr.String(), `[[0 1 2] (null) [3 4 5 6]]`; got != want {
204+
t.Fatalf("got=%q, want=%q", got, want)
205+
}
206+
207+
sub := array.NewSlice(arr, 1, 3).(*array.List)
208+
defer sub.Release()
209+
210+
if got, want := sub.String(), `[(null) [3 4 5 6]]`; got != want {
211+
t.Fatalf("got=%q, want=%q", got, want)
212+
}
213+
}

0 commit comments

Comments
 (0)