forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
137 lines (125 loc) · 3.81 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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"
"github.com/wolffcm/flux/codes"
"github.com/wolffcm/flux/internal/errors"
"github.com/wolffcm/flux/semantic"
"github.com/wolffcm/flux/values"
)
// NewBuilder constructs a new builder for the given
// column type. The allocator passed in must be non-nil.
func NewBuilder(typ flux.ColType, mem memory.Allocator) array.Builder {
switch typ {
case flux.TInt, flux.TTime:
return array.NewInt64Builder(mem)
case flux.TUInt:
return array.NewUint64Builder(mem)
case flux.TFloat:
return array.NewFloat64Builder(mem)
case flux.TString:
return array.NewBinaryBuilder(mem, arrow.BinaryTypes.String)
case flux.TBool:
return array.NewBooleanBuilder(mem)
default:
panic(fmt.Errorf("unknown builder for type: %s", typ))
}
}
// AppendValue will append a value to the builder.
//
// Be aware when using this function that it will perform
// more slowly than type switching the builder to its
// appropriate type and appending multiple values in a row.
func AppendValue(b array.Builder, v values.Value) error {
if v.IsNull() {
b.AppendNull()
return nil
}
switch v.Type().Nature() {
case semantic.Int:
return AppendInt(b, v.Int())
case semantic.UInt:
return AppendUint(b, v.UInt())
case semantic.Float:
return AppendFloat(b, v.Float())
case semantic.String:
return AppendString(b, v.Str())
case semantic.Bool:
return AppendBool(b, v.Bool())
case semantic.Time:
return AppendTime(b, v.Time())
default:
panic(fmt.Errorf("unknown builder for type: %s", v.Type()))
}
}
// AppendInt will append an int64 to a compatible builder.
func AppendInt(b array.Builder, v int64) error {
vb, ok := b.(*array.Int64Builder)
if !ok {
return errors.Newf(codes.Internal, "incompatible builder for type %s", flux.TInt)
}
vb.Append(v)
return nil
}
// AppendUint will append a uint64 to a compatible builder.
func AppendUint(b array.Builder, v uint64) error {
vb, ok := b.(*array.Uint64Builder)
if !ok {
return errors.Newf(codes.Internal, "incompatible builder for type %s", flux.TUInt)
}
vb.Append(v)
return nil
}
// AppendFloat will append a float64 to a compatible builder.
func AppendFloat(b array.Builder, v float64) error {
vb, ok := b.(*array.Float64Builder)
if !ok {
return errors.Newf(codes.Internal, "incompatible builder for type %s", flux.TFloat)
}
vb.Append(v)
return nil
}
// AppendString will append a string to a compatible builder.
func AppendString(b array.Builder, v string) error {
vb, ok := b.(*array.BinaryBuilder)
if !ok {
return errors.Newf(codes.Internal, "incompatible builder for type %s", flux.TString)
}
vb.AppendString(v)
return nil
}
// AppendBool will append a bool to a compatible builder.
func AppendBool(b array.Builder, v bool) error {
vb, ok := b.(*array.BooleanBuilder)
if !ok {
return errors.Newf(codes.Internal, "incompatible builder for type %s", flux.TBool)
}
vb.Append(v)
return nil
}
// AppendTime will append a Time value to a compatible builder.
func AppendTime(b array.Builder, v values.Time) error {
vb, ok := b.(*array.Int64Builder)
if !ok {
return errors.Newf(codes.Internal, "incompatible builder for type %s", flux.TTime)
}
vb.Append(int64(v))
return nil
}
// Slice will construct a new slice of the array using the given
// start and stop index. The returned array must be released.
//
// This is functionally equivalent to using array.NewSlice,
// but array.NewSlice will construct an array.String when
// the data type is a string rather than an array.Binary.
func Slice(arr array.Interface, i, j int64) array.Interface {
data := array.NewSliceData(arr.Data(), i, j)
defer data.Release()
if _, ok := arr.(*array.Binary); ok {
return array.NewBinaryData(data)
}
return array.MakeFromData(data)
}