forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeat.go
69 lines (66 loc) · 1.44 KB
/
repeat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package arrow
import (
"fmt"
"github.com/apache/arrow/go/arrow"
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
"github.com/wolffcm/flux/semantic"
"github.com/wolffcm/flux/values"
)
// Repeat will construct an arrow array that repeats
// the value n times.
func Repeat(v values.Value, n int, mem memory.Allocator) array.Interface {
switch v.Type().Nature() {
case semantic.Int:
b := array.NewInt64Builder(mem)
b.Resize(n)
v := v.Int()
for i := 0; i < n; i++ {
b.Append(v)
}
return b.NewArray()
case semantic.UInt:
b := array.NewUint64Builder(mem)
b.Resize(n)
v := v.UInt()
for i := 0; i < n; i++ {
b.Append(v)
}
return b.NewArray()
case semantic.Float:
b := array.NewFloat64Builder(mem)
b.Resize(n)
v := v.Float()
for i := 0; i < n; i++ {
b.Append(v)
}
return b.NewArray()
case semantic.String:
b := array.NewBinaryBuilder(mem, arrow.BinaryTypes.String)
b.Resize(n)
b.ReserveData(n * len(v.Str()))
v := v.Str()
for i := 0; i < n; i++ {
b.AppendString(v)
}
return b.NewArray()
case semantic.Bool:
b := array.NewBooleanBuilder(mem)
b.Resize(n)
v := v.Bool()
for i := 0; i < n; i++ {
b.Append(v)
}
return b.NewArray()
case semantic.Time:
b := array.NewInt64Builder(mem)
b.Resize(n)
v := int64(v.Time())
for i := 0; i < n; i++ {
b.Append(v)
}
return b.NewArray()
default:
panic(fmt.Errorf("unknown builder for type: %s", v.Type()))
}
}