/
unpivot.go
161 lines (128 loc) · 4.33 KB
/
unpivot.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
package experimental
import (
"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/internal/errors"
"github.com/influxdata/flux/internal/execute/groupkey"
"github.com/influxdata/flux/internal/execute/table"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
const UnpivotKind = "experimental.unpivot"
type UnpivotOpSpec struct{}
func init() {
unpivotSig := runtime.MustLookupBuiltinType("experimental", "unpivot")
runtime.RegisterPackageValue("experimental", "unpivot", flux.MustValue(flux.FunctionValue(UnpivotKind, createUnpivotOpSpec, unpivotSig)))
flux.RegisterOpSpec(UnpivotKind, newUnpivotOp)
plan.RegisterProcedureSpec(UnpivotKind, newUnpivotProcedure, UnpivotKind)
execute.RegisterTransformation(UnpivotKind, createUnpivotTransformation)
}
func newUnpivotOp() flux.OperationSpec {
return &UnpivotOpSpec{}
}
func createUnpivotOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
return new(UnpivotOpSpec), nil
}
func (s *UnpivotOpSpec) Kind() flux.OperationKind {
return UnpivotKind
}
func newUnpivotProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
_, ok := qs.(*UnpivotOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &UnpivotProcedureSpec{}, nil
}
func createUnpivotTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*UnpivotProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
return NewUnpivotTransformation(s, id, a.Allocator())
}
type UnpivotProcedureSpec struct {
plan.DefaultCost
}
func (s *UnpivotProcedureSpec) Kind() plan.ProcedureKind {
return UnpivotKind
}
func (s *UnpivotProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(UnpivotProcedureSpec)
*ns = *s
return ns
}
func NewUnpivotTransformation(spec *UnpivotProcedureSpec, id execute.DatasetID, alloc memory.Allocator) (execute.Transformation, execute.Dataset, error) {
t := &unpivotTransformation{}
return execute.NewNarrowTransformation(id, t, alloc)
}
type unpivotTransformation struct {
execute.ExecutionNode
}
func (t *unpivotTransformation) Close() error { return nil }
func (t *unpivotTransformation) Process(chunk table.Chunk, d *execute.TransportDataset, mem memory.Allocator) error {
timeColumn := -1
for j, c := range chunk.Cols() {
if c.Label == "_time" {
timeColumn = j
break
}
}
if timeColumn == -1 || chunk.Cols()[timeColumn].Type != flux.TTime {
return errors.Newf(codes.Internal, "Expected a `_time` column in the input")
}
for i, c := range chunk.Cols() {
if chunk.Key().HasCol(c.Label) || c.Label == "_time" {
continue
}
groupKey := chunk.Key()
columns := groupKey.Cols()
columns = append(columns,
flux.ColMeta{Label: "_field", Type: flux.TString},
flux.ColMeta{Label: "_time", Type: flux.TTime},
flux.ColMeta{Label: "_value", Type: c.Type},
)
groupCols := []flux.ColMeta{{Label: "_field", Type: flux.TString}}
groupValues := []values.Value{values.NewString(c.Label)}
buffer := arrow.TableBuffer{
GroupKey: groupkey.New(
append(groupCols, groupKey.Cols()...),
append(groupValues, groupKey.Values()...),
),
Columns: columns,
Values: make([]array.Array, len(columns)),
}
// Copy group key columns
for toColumn, groupColumn := range groupKey.Cols() {
fromColumn := -1
for j, c := range chunk.Cols() {
if c.Label == groupColumn.Label {
fromColumn = j
break
}
}
values := chunk.Values(fromColumn)
values.Retain()
buffer.Values[toColumn] = values
}
buffer.Values[len(buffer.Values)-3] = array.StringRepeat(c.Label, chunk.Len(), mem)
times := chunk.Values(timeColumn)
times.Retain()
buffer.Values[len(buffer.Values)-2] = times
values := chunk.Values(i)
values.Retain()
buffer.Values[len(buffer.Values)-1] = values
out := table.ChunkFromBuffer(buffer)
if err := d.Process(out); err != nil {
return err
}
}
return nil
}