-
Notifications
You must be signed in to change notification settings - Fork 152
/
cmp.go
179 lines (146 loc) · 4.97 KB
/
cmp.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
package plantest
import (
"fmt"
"reflect"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/flux"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic/semantictest"
"github.com/influxdata/flux/stdlib/kafka"
"github.com/influxdata/flux/stdlib/universe"
"github.com/pkg/errors"
)
// CmpOptions are the options needed to compare plan.ProcedureSpecs inside plan.Spec.
var CmpOptions = append(
semantictest.CmpOptions,
cmp.AllowUnexported(flux.Spec{}),
cmp.AllowUnexported(universe.JoinOpSpec{}),
cmpopts.IgnoreUnexported(flux.Spec{}),
cmpopts.IgnoreUnexported(universe.JoinOpSpec{}),
cmp.AllowUnexported(kafka.ToKafkaProcedureSpec{}),
cmpopts.IgnoreUnexported(kafka.ToKafkaProcedureSpec{}),
)
// ComparePlans compares two query plans using an arbitrary comparator function f
func ComparePlans(p, q *plan.Spec, f func(p, q plan.Node) error) error {
err := compareMetadata(p, q)
if err != nil {
return err
}
var w, v []plan.Node
_ = p.TopDownWalk(func(node plan.Node) error {
w = append(w, node)
return nil
})
_ = q.TopDownWalk(func(node plan.Node) error {
v = append(v, node)
return nil
})
if len(w) != len(v) {
return fmt.Errorf("plans have %d and %d nodes respectively", len(w), len(v))
}
for i := 0; i < len(w); i++ {
if err := f(w[i], v[i]); err != nil {
return err
}
}
return nil
}
// ComparePlansShallow Compares the two specs, but only compares the
// metadata and the types of each node. Individual fields of procedure specs
// are not compared.
func ComparePlansShallow(p, q *plan.Spec) error {
if err := ComparePlans(p, q, cmpPlanNodeShallow); err != nil {
return err
}
return nil
}
func compareMetadata(p, q *plan.Spec) error {
opts := cmpopts.IgnoreFields(plan.Spec{}, "Roots")
if diff := cmp.Diff(p, q, opts); diff != "" {
return errors.Errorf("plan metadata not equal; -want/+got:\n%v", diff)
}
return nil
}
// CompareLogicalPlans compares two logical plans.
func CompareLogicalPlans(p, q *plan.Spec) error {
return ComparePlans(p, q, CompareLogicalPlanNodes)
}
// CompareLogicalPlanNodes is a comparator function for LogicalPlanNodes
func CompareLogicalPlanNodes(p, q plan.Node) error {
if _, ok := p.(*plan.LogicalNode); !ok {
return fmt.Errorf("expected %s to be a LogicalNode", p.ID())
}
if _, ok := q.(*plan.LogicalNode); !ok {
return fmt.Errorf("expected %s to be a LogicalNode", q.ID())
}
return cmpPlanNode(p, q)
}
// ComparePhysicalPlanNodes is a comparator function for PhysicalPlanNodes
func ComparePhysicalPlanNodes(p, q plan.Node) error {
var pp, qq *plan.PhysicalPlanNode
var ok bool
if pp, ok = p.(*plan.PhysicalPlanNode); !ok {
return fmt.Errorf("expected %s to be a PhysicalPlanNode", p.ID())
}
if qq, ok = q.(*plan.PhysicalPlanNode); !ok {
return fmt.Errorf("expected %s to be a PhysicalPlanNode", q.ID())
}
if err := cmpPlanNode(p, q); err != nil {
return err
}
// Both nodes must consume the same required attributes
if !cmp.Equal(pp.RequiredAttrs, qq.RequiredAttrs) {
return fmt.Errorf("required attributes not equal -want(%s)/+got(%s) %s",
pp.ID(), qq.ID(), cmp.Diff(pp.RequiredAttrs, qq.RequiredAttrs))
}
// Both nodes must produce the same physical attributes
if !cmp.Equal(pp.OutputAttrs, qq.OutputAttrs) {
return fmt.Errorf("output attributes not equal -want(%s)/+got(%s) %s",
pp.ID(), qq.ID(), cmp.Diff(pp.OutputAttrs, qq.OutputAttrs))
}
return nil
}
func cmpPlanNode(p, q plan.Node) error {
// Both nodes must have the same ID
if p.ID() != q.ID() {
return fmt.Errorf("wanted %s, but got %s", p.ID(), q.ID())
}
// Both nodes must be the same kind of procedure
if p.Kind() != q.Kind() {
return fmt.Errorf("wanted %s, but got %s", p.Kind(), q.Kind())
}
// Both nodes must have the same time bounds
if !cmp.Equal(p.Bounds(), q.Bounds()) {
return fmt.Errorf("plan nodes have different bounds -want(%s)/+got(%s) %s",
p.ID(), q.ID(), cmp.Diff(p.Bounds(), q.Bounds()))
}
// The specifications of both procedures must be the same
if !cmp.Equal(p.ProcedureSpec(), q.ProcedureSpec(), CmpOptions...) {
return fmt.Errorf("procedure specs not equal -want(%s)/+got(%s) %s",
p.ID(), q.ID(), cmp.Diff(p.ProcedureSpec(), q.ProcedureSpec(), CmpOptions...))
}
return nil
}
func cmpPlanNodeShallow(p, q plan.Node) error {
// Just make sure that they have the same type
pt, qt := reflect.TypeOf(p.ProcedureSpec()), reflect.TypeOf(q.ProcedureSpec())
_, pIsYield := p.ProcedureSpec().(plan.YieldProcedureSpec)
_, qIsYield := q.ProcedureSpec().(plan.YieldProcedureSpec)
if pIsYield && qIsYield {
// generated yields are produced by the planner but their specs
// are not public. So consider any types that implement yield to be equal.
return nil
}
if pIsYield != qIsYield {
if pIsYield {
return fmt.Errorf("wanted a yield, but got a %v", qt)
} else {
return fmt.Errorf("wanted a %v, but got a yield", pt)
}
}
if pt != qt {
return fmt.Errorf("plan nodes have different types; -want/+got: -%v/+%v", pt, qt)
}
return nil
}