Skip to content

Commit 32fe18d

Browse files
authored
refactor: move spec details into an internal package (#5116)
The spec was something that originally existed to accomodate transpilers. We later decided transpilers should just use the AST rather than something internal like the spec and the repl became the only thing that uses the spec. This moves the spec to an internal package so it stays as an internal detail and does not exist as part of the external Go API. This also removes the ider interface. It didn't appear to be used for anything other than an implementation inside of the spec, so this just removes it. The transformations that needed to know the ids of their parents didn't use the operation ids anyway so it didn't matter.
1 parent ac9b827 commit 32fe18d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+472
-459
lines changed

compile.go

+12-35
Original file line numberDiff line numberDiff line change
@@ -66,43 +66,23 @@ func functionValue(name string, c CreateOperationSpec, mt semantic.MonoType, sid
6666

6767
var _ = tableSpecKey // So that linter doesn't think tableSpecKey is unused, considering above TODO.
6868

69-
// IDer produces the mapping of table Objects to OperationIDs
70-
type IDer interface {
71-
ID(*TableObject) OperationID
72-
}
73-
74-
// IDerOpSpec is the interface any operation spec that needs
75-
// access to OperationIDs in the query spec must implement.
76-
type IDerOpSpec interface {
77-
IDer(ider IDer)
78-
}
79-
8069
// TableObject represents the value returned by a transformation.
8170
// As such, it holds the OperationSpec of the transformation it is associated with,
8271
// and it is a values.Value (and, also, a values.Object).
8372
// It can be compiled and executed as a flux.Program by using a lang.TableObjectCompiler.
8473
type TableObject struct {
8574
// TODO(Josh): Remove args once the
8675
// OperationSpec interface has an Equal method.
87-
t semantic.MonoType
88-
args Arguments
89-
Kind OperationKind
90-
Spec OperationSpec
91-
Source OperationSource
76+
t semantic.MonoType
77+
args Arguments
78+
Kind OperationKind
79+
Spec OperationSpec
80+
Source struct {
81+
Stack []interpreter.StackEntry
82+
}
9283
Parents []*TableObject
9384
}
9485

95-
func (t *TableObject) Operation(ider IDer) *Operation {
96-
if iderOpSpec, ok := t.Spec.(IDerOpSpec); ok {
97-
iderOpSpec.IDer(ider)
98-
}
99-
100-
return &Operation{
101-
ID: ider.ID(t),
102-
Spec: t.Spec,
103-
Source: t.Source,
104-
}
105-
}
10686
func (t *TableObject) IsNull() bool {
10787
return false
10888
}
@@ -326,17 +306,14 @@ func (f *function) call(ctx context.Context, args interpreter.Arguments) (values
326306
return nil, err
327307
}
328308

329-
stack := interpreter.Stack(ctx)
330309
t := &TableObject{
331-
t: returnType,
332-
args: arguments,
333-
Kind: spec.Kind(),
334-
Spec: spec,
335-
Source: OperationSource{
336-
Stack: stack,
337-
},
310+
t: returnType,
311+
args: arguments,
312+
Kind: spec.Kind(),
313+
Spec: spec,
338314
Parents: a.parents,
339315
}
316+
t.Source.Stack = interpreter.Stack(ctx)
340317
return t, nil
341318
}
342319
func (f *function) String() string {

execute/concurrency_quota_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/influxdata/flux/internal/operation"
89
"github.com/influxdata/flux/plan"
910
"go.uber.org/zap/zaptest"
1011

11-
"github.com/influxdata/flux"
1212
"github.com/influxdata/flux/dependencies/dependenciestest"
1313
"github.com/influxdata/flux/dependency"
1414
"github.com/influxdata/flux/execute"
@@ -108,7 +108,7 @@ func (rule parallelizeFromTo) Rewrite(ctx context.Context, pn plan.Node) (plan.N
108108

109109
type flagger map[string]interface{}
110110

111-
func compile(fluxText string, now time.Time) (context.Context, *flux.Spec, error) {
111+
func compile(fluxText string, now time.Time) (context.Context, *operation.Spec, error) {
112112
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
113113
defer deps.Finish()
114114
spec, err := spec.FromScript(ctx, runtime.Default, now, fluxText)

spec.go renamed to internal/operation/spec.go

+42-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
package flux
1+
package operation
22

33
import (
44
"time"
55

6+
"github.com/influxdata/flux"
67
"github.com/influxdata/flux/codes"
78
"github.com/influxdata/flux/internal/errors"
9+
"github.com/influxdata/flux/interpreter"
810
)
911

12+
// Node denotes a single operation in a query.
13+
type Node struct {
14+
ID NodeID `json:"id"`
15+
Spec flux.OperationSpec `json:"spec"`
16+
Source NodeSource `json:"source"`
17+
}
18+
19+
// NodeSource specifies the source location that created
20+
// an operation.
21+
type NodeSource struct {
22+
Stack []interpreter.StackEntry `json:"stack"`
23+
}
24+
25+
// NodeID is a unique ID within a query for the operation.
26+
type NodeID string
27+
1028
// Spec specifies a query.
1129
type Spec struct {
12-
Operations []*Operation `json:"operations"`
13-
Edges []Edge `json:"edges"`
14-
Resources ResourceManagement `json:"resources"`
15-
Now time.Time `json:"now"`
16-
17-
sorted []*Operation
18-
children map[OperationID][]*Operation
19-
parents map[OperationID][]*Operation
30+
Operations []*Node `json:"operations"`
31+
Edges []Edge `json:"edges"`
32+
Resources flux.ResourceManagement `json:"resources"`
33+
Now time.Time `json:"now"`
34+
35+
sorted []*Node
36+
children map[NodeID][]*Node
37+
parents map[NodeID][]*Node
2038
}
2139

2240
// Edge is a data flow relationship between a parent and a child
2341
type Edge struct {
24-
Parent OperationID `json:"parent"`
25-
Child OperationID `json:"child"`
42+
Parent NodeID `json:"parent"`
43+
Child NodeID `json:"child"`
2644
}
2745

2846
// Walk calls f on each operation exactly once.
2947
// The function f will be called on an operation only after
3048
// all of its parents have already been passed to f.
31-
func (q *Spec) Walk(f func(o *Operation) error) error {
49+
func (q *Spec) Walk(f func(o *Node) error) error {
3250
if len(q.sorted) == 0 {
3351
if err := q.prepare(); err != nil {
3452
return err
@@ -53,7 +71,7 @@ func (q *Spec) Validate() error {
5371

5472
// Children returns a list of children for a given operation.
5573
// If the query is invalid no children will be returned.
56-
func (q *Spec) Children(id OperationID) []*Operation {
74+
func (q *Spec) Children(id NodeID) []*Node {
5775
if q.children == nil {
5876
err := q.prepare()
5977
if err != nil {
@@ -65,7 +83,7 @@ func (q *Spec) Children(id OperationID) []*Operation {
6583

6684
// Parents returns a list of parents for a given operation.
6785
// If the query is invalid no parents will be returned.
68-
func (q *Spec) Parents(id OperationID) []*Operation {
86+
func (q *Spec) Parents(id NodeID) []*Node {
6987
if q.parents == nil {
7088
err := q.prepare()
7189
if err != nil {
@@ -91,23 +109,23 @@ func (q *Spec) prepare() error {
91109
q.parents = parents
92110
q.children = children
93111

94-
tMarks := make(map[OperationID]bool)
95-
pMarks := make(map[OperationID]bool)
112+
tMarks := make(map[NodeID]bool)
113+
pMarks := make(map[NodeID]bool)
96114

97115
for _, r := range roots {
98116
if err := q.visit(tMarks, pMarks, r); err != nil {
99117
return err
100118
}
101119
}
102-
//reverse q.sorted
120+
// reverse q.sorted
103121
for i, j := 0, len(q.sorted)-1; i < j; i, j = i+1, j-1 {
104122
q.sorted[i], q.sorted[j] = q.sorted[j], q.sorted[i]
105123
}
106124
return nil
107125
}
108126

109-
func (q *Spec) computeLookup() (map[OperationID]*Operation, error) {
110-
lookup := make(map[OperationID]*Operation, len(q.Operations))
127+
func (q *Spec) computeLookup() (map[NodeID]*Node, error) {
128+
lookup := make(map[NodeID]*Node, len(q.Operations))
111129
for _, o := range q.Operations {
112130
if _, ok := lookup[o.ID]; ok {
113131
return nil, errors.Newf(codes.Internal, "found duplicate operation ID %q", o.ID)
@@ -117,13 +135,13 @@ func (q *Spec) computeLookup() (map[OperationID]*Operation, error) {
117135
return lookup, nil
118136
}
119137

120-
func (q *Spec) determineParentsChildrenAndRoots() (parents, children map[OperationID][]*Operation, roots []*Operation, _ error) {
138+
func (q *Spec) determineParentsChildrenAndRoots() (parents, children map[NodeID][]*Node, roots []*Node, _ error) {
121139
lookup, err := q.computeLookup()
122140
if err != nil {
123141
return nil, nil, nil, err
124142
}
125-
children = make(map[OperationID][]*Operation, len(q.Operations))
126-
parents = make(map[OperationID][]*Operation, len(q.Operations))
143+
children = make(map[NodeID][]*Node, len(q.Operations))
144+
parents = make(map[NodeID][]*Node, len(q.Operations))
127145
for _, e := range q.Edges {
128146
// Build children map
129147
c, ok := lookup[e.Child]
@@ -150,7 +168,7 @@ func (q *Spec) determineParentsChildrenAndRoots() (parents, children map[Operati
150168

151169
// Depth first search topological sorting of a DAG.
152170
// https://en.wikipedia.org/wiki/Topological_sorting#Algorithms
153-
func (q *Spec) visit(tMarks, pMarks map[OperationID]bool, o *Operation) error {
171+
func (q *Spec) visit(tMarks, pMarks map[NodeID]bool, o *Node) error {
154172
id := o.ID
155173
if tMarks[id] {
156174
return errors.New(codes.Invalid, "found cycle in query")
@@ -173,7 +191,7 @@ func (q *Spec) visit(tMarks, pMarks map[OperationID]bool, o *Operation) error {
173191
// Functions return the names of all functions used in the plan
174192
func (q *Spec) Functions() ([]string, error) {
175193
funcs := []string{}
176-
err := q.Walk(func(o *Operation) error {
194+
err := q.Walk(func(o *Node) error {
177195
funcs = append(funcs, string(o.Spec.Kind()))
178196
return nil
179197
})

0 commit comments

Comments
 (0)