Skip to content

Commit c604adb

Browse files
alexandreycwesm
authored andcommitted
ARROW-3672 & ARROW-3673: [Go] add support for time32 and time64 array
Hello everyone, My attempt at adding support for time32 and time64 array. Need review because I'm not sure I added all that is needed. Thanks, Alexandre Author: alexandreyc <alexandre.crayssac@gmail.com> Closes apache#2944 from alexandreyc/master and squashes the following commits: e686b19 <alexandreyc> update mu to u for microseconds dd2a5d0 <alexandreyc> add tests for time 64 array and fix bug ea68cd5 <alexandreyc> add tests for time 32 array 83eb131 <alexandreyc> add tests for time32 and time64 builders e79db33 <alexandreyc> add time32 and time64 array 3d6ddcf <alexandreyc> Fix missing import in numeric builder template
1 parent 281eb22 commit c604adb

10 files changed

Lines changed: 1008 additions & 6 deletions

go/arrow/array/array.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ func init() {
183183
arrow.DATE32: unsupportedArrayType,
184184
arrow.DATE64: unsupportedArrayType,
185185
arrow.TIMESTAMP: func(data *Data) Interface { return NewTimestampData(data) },
186-
arrow.TIME32: unsupportedArrayType,
187-
arrow.TIME64: unsupportedArrayType,
186+
arrow.TIME32: func(data *Data) Interface { return NewTime32Data(data) },
187+
arrow.TIME64: func(data *Data) Interface { return NewTime64Data(data) },
188188
arrow.INTERVAL: unsupportedArrayType,
189189
arrow.DECIMAL: unsupportedArrayType,
190190
arrow.LIST: func(data *Data) Interface { return NewListData(data) },

go/arrow/array/numeric.gen.go

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/arrow/array/numeric_test.go

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,263 @@ func TestFloat64SliceDataWithNull(t *testing.T) {
134134
t.Fatalf("got=%v, want=%v", got, want)
135135
}
136136
}
137+
138+
func TestNewTime32Data(t *testing.T) {
139+
data := []arrow.Time32{
140+
arrow.Time32(1),
141+
arrow.Time32(2),
142+
arrow.Time32(4),
143+
arrow.Time32(8),
144+
arrow.Time32(16),
145+
}
146+
147+
dtype := arrow.FixedWidthTypes.Time32s
148+
ad := array.NewData(dtype, len(data),
149+
[]*memory.Buffer{nil, memory.NewBufferBytes(arrow.Time32Traits.CastToBytes(data))},
150+
nil, 0, 0,
151+
)
152+
t32a := array.NewTime32Data(ad)
153+
154+
assert.Equal(t, len(data), t32a.Len(), "unexpected Len()")
155+
assert.Equal(t, data, t32a.Time32Values(), "unexpected Float64Values()")
156+
}
157+
158+
func TestTime32SliceData(t *testing.T) {
159+
pool := memory.NewCheckedAllocator(memory.NewGoAllocator())
160+
defer pool.AssertSize(t, 0)
161+
162+
const (
163+
beg = 2
164+
end = 4
165+
)
166+
167+
var (
168+
vs = []arrow.Time32{
169+
arrow.Time32(1),
170+
arrow.Time32(2),
171+
arrow.Time32(4),
172+
arrow.Time32(8),
173+
arrow.Time32(16),
174+
}
175+
sub = vs[beg:end]
176+
)
177+
178+
dtype := arrow.FixedWidthTypes.Time32s
179+
b := array.NewTime32Builder(pool, dtype.(*arrow.Time32Type))
180+
defer b.Release()
181+
182+
for _, v := range vs {
183+
b.Append(v)
184+
}
185+
186+
arr := b.NewArray().(*array.Time32)
187+
defer arr.Release()
188+
189+
if got, want := arr.Len(), len(vs); got != want {
190+
t.Fatalf("got=%d, want=%d", got, want)
191+
}
192+
193+
if got, want := arr.Time32Values(), vs; !reflect.DeepEqual(got, want) {
194+
t.Fatalf("got=%v, want=%v", got, want)
195+
}
196+
197+
slice := array.NewSlice(arr, beg, end).(*array.Time32)
198+
defer slice.Release()
199+
200+
if got, want := slice.Len(), len(sub); got != want {
201+
t.Fatalf("got=%d, want=%d", got, want)
202+
}
203+
204+
if got, want := slice.Time32Values(), sub; !reflect.DeepEqual(got, want) {
205+
t.Fatalf("got=%v, want=%v", got, want)
206+
}
207+
}
208+
209+
func TestTime32SliceDataWithNull(t *testing.T) {
210+
pool := memory.NewCheckedAllocator(memory.NewGoAllocator())
211+
defer pool.AssertSize(t, 0)
212+
213+
const (
214+
beg = 2
215+
end = 5
216+
)
217+
218+
var (
219+
valids = []bool{true, true, true, false, true, true}
220+
vs = []arrow.Time32{
221+
arrow.Time32(1),
222+
arrow.Time32(2),
223+
arrow.Time32(3),
224+
arrow.Time32(0),
225+
arrow.Time32(4),
226+
arrow.Time32(5),
227+
}
228+
sub = vs[beg:end]
229+
)
230+
231+
dtype := arrow.FixedWidthTypes.Time32s
232+
b := array.NewTime32Builder(pool, dtype.(*arrow.Time32Type))
233+
defer b.Release()
234+
235+
b.AppendValues(vs, valids)
236+
237+
arr := b.NewArray().(*array.Time32)
238+
defer arr.Release()
239+
240+
if got, want := arr.Len(), len(valids); got != want {
241+
t.Fatalf("got=%d, want=%d", got, want)
242+
}
243+
244+
if got, want := arr.NullN(), 1; got != want {
245+
t.Fatalf("got=%d, want=%d", got, want)
246+
}
247+
248+
if got, want := arr.Time32Values(), vs; !reflect.DeepEqual(got, want) {
249+
t.Fatalf("got=%v, want=%v", got, want)
250+
}
251+
252+
slice := array.NewSlice(arr, beg, end).(*array.Time32)
253+
defer slice.Release()
254+
255+
if got, want := slice.NullN(), 1; got != want {
256+
t.Errorf("got=%d, want=%d", got, want)
257+
}
258+
259+
if got, want := slice.Len(), len(sub); got != want {
260+
t.Fatalf("got=%d, want=%d", got, want)
261+
}
262+
263+
if got, want := slice.Time32Values(), sub; !reflect.DeepEqual(got, want) {
264+
t.Fatalf("got=%v, want=%v", got, want)
265+
}
266+
}
267+
268+
func TestNewTime64Data(t *testing.T) {
269+
data := []arrow.Time64{
270+
arrow.Time64(1),
271+
arrow.Time64(2),
272+
arrow.Time64(4),
273+
arrow.Time64(8),
274+
arrow.Time64(16),
275+
}
276+
277+
dtype := arrow.FixedWidthTypes.Time64us
278+
ad := array.NewData(dtype, len(data),
279+
[]*memory.Buffer{nil, memory.NewBufferBytes(arrow.Time64Traits.CastToBytes(data))},
280+
nil, 0, 0,
281+
)
282+
t64a := array.NewTime64Data(ad)
283+
284+
assert.Equal(t, len(data), t64a.Len(), "unexpected Len()")
285+
assert.Equal(t, data, t64a.Time64Values(), "unexpected Float64Values()")
286+
}
287+
288+
func TestTime64SliceData(t *testing.T) {
289+
pool := memory.NewCheckedAllocator(memory.NewGoAllocator())
290+
defer pool.AssertSize(t, 0)
291+
292+
const (
293+
beg = 2
294+
end = 4
295+
)
296+
297+
var (
298+
vs = []arrow.Time64{
299+
arrow.Time64(1),
300+
arrow.Time64(2),
301+
arrow.Time64(4),
302+
arrow.Time64(8),
303+
arrow.Time64(16),
304+
}
305+
sub = vs[beg:end]
306+
)
307+
308+
dtype := arrow.FixedWidthTypes.Time64us
309+
b := array.NewTime64Builder(pool, dtype.(*arrow.Time64Type))
310+
defer b.Release()
311+
312+
for _, v := range vs {
313+
b.Append(v)
314+
}
315+
316+
arr := b.NewArray().(*array.Time64)
317+
defer arr.Release()
318+
319+
if got, want := arr.Len(), len(vs); got != want {
320+
t.Fatalf("got=%d, want=%d", got, want)
321+
}
322+
323+
if got, want := arr.Time64Values(), vs; !reflect.DeepEqual(got, want) {
324+
t.Fatalf("got=%v, want=%v", got, want)
325+
}
326+
327+
slice := array.NewSlice(arr, beg, end).(*array.Time64)
328+
defer slice.Release()
329+
330+
if got, want := slice.Len(), len(sub); got != want {
331+
t.Fatalf("got=%d, want=%d", got, want)
332+
}
333+
334+
if got, want := slice.Time64Values(), sub; !reflect.DeepEqual(got, want) {
335+
t.Fatalf("got=%v, want=%v", got, want)
336+
}
337+
}
338+
339+
func TestTime64SliceDataWithNull(t *testing.T) {
340+
pool := memory.NewCheckedAllocator(memory.NewGoAllocator())
341+
defer pool.AssertSize(t, 0)
342+
343+
const (
344+
beg = 2
345+
end = 5
346+
)
347+
348+
var (
349+
valids = []bool{true, true, true, false, true, true}
350+
vs = []arrow.Time64{
351+
arrow.Time64(1),
352+
arrow.Time64(2),
353+
arrow.Time64(3),
354+
arrow.Time64(0),
355+
arrow.Time64(4),
356+
arrow.Time64(5),
357+
}
358+
sub = vs[beg:end]
359+
)
360+
361+
dtype := arrow.FixedWidthTypes.Time64us
362+
b := array.NewTime64Builder(pool, dtype.(*arrow.Time64Type))
363+
defer b.Release()
364+
365+
b.AppendValues(vs, valids)
366+
367+
arr := b.NewArray().(*array.Time64)
368+
defer arr.Release()
369+
370+
if got, want := arr.Len(), len(valids); got != want {
371+
t.Fatalf("got=%d, want=%d", got, want)
372+
}
373+
374+
if got, want := arr.NullN(), 1; got != want {
375+
t.Fatalf("got=%d, want=%d", got, want)
376+
}
377+
378+
if got, want := arr.Time64Values(), vs; !reflect.DeepEqual(got, want) {
379+
t.Fatalf("got=%v, want=%v", got, want)
380+
}
381+
382+
slice := array.NewSlice(arr, beg, end).(*array.Time64)
383+
defer slice.Release()
384+
385+
if got, want := slice.NullN(), 1; got != want {
386+
t.Errorf("got=%d, want=%d", got, want)
387+
}
388+
389+
if got, want := slice.Len(), len(sub); got != want {
390+
t.Fatalf("got=%d, want=%d", got, want)
391+
}
392+
393+
if got, want := slice.Time64Values(), sub; !reflect.DeepEqual(got, want) {
394+
t.Fatalf("got=%v, want=%v", got, want)
395+
}
396+
}

0 commit comments

Comments
 (0)