-
Notifications
You must be signed in to change notification settings - Fork 153
/
limit.go
208 lines (179 loc) · 5.46 KB
/
limit.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
207
208
package universe
import (
arrowmem "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/table"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
)
const LimitKind = "limit"
// LimitOpSpec limits the number of rows returned per table.
type LimitOpSpec struct {
N int64 `json:"n"`
Offset int64 `json:"offset"`
}
func init() {
limitSignature := runtime.MustLookupBuiltinType("universe", "limit")
runtime.RegisterPackageValue("universe", LimitKind, flux.MustValue(flux.FunctionValue(LimitKind, createLimitOpSpec, limitSignature)))
plan.RegisterProcedureSpec(LimitKind, newLimitProcedure, LimitKind)
// TODO register a range transformation. Currently range is only supported if it is pushed down into a select procedure.
execute.RegisterTransformation(LimitKind, createLimitTransformation)
}
func createLimitOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(LimitOpSpec)
n, err := args.GetRequiredInt("n")
if err != nil {
return nil, err
}
spec.N = n
if offset, ok, err := args.GetInt("offset"); err != nil {
return nil, err
} else if ok {
spec.Offset = offset
}
return spec, nil
}
func (s *LimitOpSpec) Kind() flux.OperationKind {
return LimitKind
}
type LimitProcedureSpec struct {
plan.DefaultCost
N int64 `json:"n"`
Offset int64 `json:"offset"`
}
func newLimitProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*LimitOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &LimitProcedureSpec{
N: spec.N,
Offset: spec.Offset,
}, nil
}
func (s *LimitProcedureSpec) Kind() plan.ProcedureKind {
return LimitKind
}
func (s *LimitProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(LimitProcedureSpec)
*ns = *s
return ns
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *LimitProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
func createLimitTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*LimitProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
return NewLimitTransformation(s, id, a.Allocator())
}
type limitState struct {
n int
offset int
}
func NewLimitTransformation(
spec *LimitProcedureSpec,
id execute.DatasetID,
mem memory.Allocator,
) (execute.Transformation, execute.Dataset, error) {
t := &limitTransformation{
n: int(spec.N),
offset: int(spec.Offset),
}
return execute.NewNarrowStateTransformation[*limitState](id, t, mem)
}
type limitTransformation struct {
n, offset int
}
func (t *limitTransformation) Process(
chunk table.Chunk,
state *limitState,
dataset *execute.TransportDataset,
_ arrowmem.Allocator,
) (*limitState, bool, error) {
// `.Process` is reentrant, so to speak. The first invocation will not
// include a value for `state`. Initialization happens here then is passed
// in/out for the subsequent calls.
if state == nil {
state = &limitState{n: t.n, offset: t.offset}
}
return t.processChunk(chunk, state, dataset)
}
func (t *limitTransformation) processChunk(
chunk table.Chunk,
state *limitState,
dataset *execute.TransportDataset,
) (*limitState, bool, error) {
chunkLen := chunk.Len()
// Pass empty chunks along to downstream transformations for these cases.
if state.n <= 0 || chunkLen == 0 {
// TODO(onelson): seems like there should be a more simple way to produce an empty chunk
buf := chunk.Buffer()
buf.Values = make([]array.Array, chunk.NCols())
for idx := range buf.Values {
values := chunk.Values(idx)
if values.Len() == 0 {
values.Retain()
} else {
values = arrow.Slice(values, int64(0), int64(0))
}
buf.Values[idx] = values
}
out := table.ChunkFromBuffer(buf)
if err := dataset.Process(out); err != nil {
return nil, false, err
}
return state, true, nil
}
if chunkLen <= state.offset {
state.offset -= chunkLen
return state, true, nil
}
start := state.offset
stop := chunkLen
count := stop - start
if count > state.n {
count = state.n
stop = start + count
}
// Update state for the next iteration
state.n -= count
state.offset = 0
buf := chunk.Buffer()
// XXX(onelson): seems like we're building a 2D array where the outer is by
// column, and the inners are the column values per row?
buf.Values = make([]array.Array, chunk.NCols())
for idx := range buf.Values {
values := chunk.Values(idx)
// If there's no cruft at the end, just keep the original array,
// otherwise we need to truncate it to ensure all inners have the
// expected size.
// XXX(onelson): Could there be a 3rd case where we have less than the count?
if values.Len() == count {
values.Retain()
} else {
values = arrow.Slice(values, int64(start), int64(stop))
}
buf.Values[idx] = values
}
out := table.ChunkFromBuffer(buf)
if err := dataset.Process(out); err != nil {
return nil, false, err
}
return state, true, nil
}
func (*limitTransformation) Close() error {
return nil
}