/
shift.go
206 lines (177 loc) · 5.33 KB
/
shift.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package universe
import (
"time"
"github.com/apache/arrow/go/v7/arrow/memory"
"github.com/influxdata/flux"
"github.com/influxdata/flux/array"
"github.com/influxdata/flux/arrow"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/execute/table"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const ShiftKind = "timeShift"
type ShiftOpSpec struct {
Shift flux.Duration `json:"duration"`
Columns []string `json:"columns"`
}
func init() {
shiftSignature := runtime.MustLookupBuiltinType("universe", "timeShift")
runtime.RegisterPackageValue("universe", ShiftKind, flux.MustValue(flux.FunctionValue(ShiftKind, createShiftOpSpec, shiftSignature)))
plan.RegisterProcedureSpec(ShiftKind, newShiftProcedure, ShiftKind)
execute.RegisterTransformation(ShiftKind, createShiftTransformation)
}
func createShiftOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(ShiftOpSpec)
if shift, err := args.GetRequiredDuration("duration"); err != nil {
return nil, err
} else {
spec.Shift = shift
}
if cols, ok, err := args.GetArray("columns", semantic.String); err != nil {
return nil, err
} else if ok {
columns, err := interpreter.ToStringArray(cols)
if err != nil {
return nil, err
}
spec.Columns = columns
} else {
spec.Columns = []string{
execute.DefaultTimeColLabel,
execute.DefaultStopColLabel,
execute.DefaultStartColLabel,
}
}
return spec, nil
}
func (s *ShiftOpSpec) Kind() flux.OperationKind {
return ShiftKind
}
type ShiftProcedureSpec struct {
plan.DefaultCost
Shift flux.Duration
Columns []string
Now time.Time
}
// TimeBounds implements plan.BoundsAwareProcedureSpec
func (s *ShiftProcedureSpec) TimeBounds(predecessorBounds *plan.Bounds) *plan.Bounds {
if predecessorBounds != nil {
return predecessorBounds.Shift(values.Duration(s.Shift))
}
return nil
}
func newShiftProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*ShiftOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &ShiftProcedureSpec{
Shift: spec.Shift,
Columns: spec.Columns,
Now: pa.Now(),
}, nil
}
func (s *ShiftProcedureSpec) Kind() plan.ProcedureKind {
return ShiftKind
}
func (s *ShiftProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(ShiftProcedureSpec)
*ns = *s
if s.Columns != nil {
ns.Columns = make([]string, len(s.Columns))
copy(ns.Columns, s.Columns)
}
return ns
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *ShiftProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
func createShiftTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*ShiftProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
return NewShiftTransformation(id, s, a.Allocator())
}
type shiftTransformation struct {
columns []string
shift execute.Duration
}
func NewShiftTransformation(id execute.DatasetID, spec *ShiftProcedureSpec, mem memory.Allocator) (execute.Transformation, execute.Dataset, error) {
tr := &shiftTransformation{
columns: spec.Columns,
shift: spec.Shift,
}
return execute.NewNarrowTransformation(id, tr, mem)
}
func (s *shiftTransformation) Process(chunk table.Chunk, d *execute.TransportDataset, mem memory.Allocator) error {
key := chunk.Key()
for _, c := range key.Cols() {
if execute.ContainsStr(s.columns, c.Label) {
k, err := s.regenerateKey(key)
if err != nil {
return err
}
key = k
break
}
}
buffer := arrow.TableBuffer{
GroupKey: key,
Columns: chunk.Cols(),
Values: make([]array.Array, chunk.NCols()),
}
for j, c := range chunk.Cols() {
vs := chunk.Values(j)
if execute.ContainsStr(s.columns, c.Label) {
if c.Type != flux.TTime {
return errors.Newf(codes.FailedPrecondition, "column %q is not of type time", c.Label)
}
buffer.Values[j] = s.shiftTimes(vs.(*array.Int), mem)
} else {
vs.Retain()
buffer.Values[j] = vs
}
}
out := table.ChunkFromBuffer(buffer)
return d.Process(out)
}
func (s *shiftTransformation) regenerateKey(key flux.GroupKey) (flux.GroupKey, error) {
cols := key.Cols()
vals := make([]values.Value, len(cols))
for j, c := range cols {
if execute.ContainsStr(s.columns, c.Label) {
if c.Type != flux.TTime {
return nil, errors.Newf(codes.FailedPrecondition, "column %q is not of type time", c.Label)
}
vals[j] = values.NewTime(key.ValueTime(j).Add(s.shift))
} else {
vals[j] = key.Value(j)
}
}
return execute.NewGroupKey(cols, vals), nil
}
func (s *shiftTransformation) shiftTimes(vs *array.Int, mem memory.Allocator) *array.Int {
b := array.NewIntBuilder(mem)
b.Resize(vs.Len())
for i, n := 0, vs.Len(); i < n; i++ {
if vs.IsNull(i) {
b.AppendNull()
continue
}
ts := execute.Time(vs.Value(i)).Add(s.shift)
b.Append(int64(ts))
}
return b.NewIntArray()
}
func (s *shiftTransformation) Close() error { return nil }