-
Notifications
You must be signed in to change notification settings - Fork 451
/
expression.go
181 lines (147 loc) · 5.53 KB
/
expression.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package native
import (
"fmt"
"reflect"
"time"
"github.com/m3db/m3/src/query/graphite/common"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
)
var (
errTopLevelFunctionMustReturnTimeSeries = errors.NewInvalidParamsError(errors.New("top-level functions must return timeseries data"))
)
// An Expression is a metric query expression
type Expression interface {
CallASTNode
// Executes the expression against the given context, and returns the resulting time series data
Execute(ctx *common.Context) (ts.SeriesList, error)
}
// CallASTNode is an interface to help with printing the AST.
type CallASTNode interface {
// Name returns the name of the call.
Name() string
// Arguments describe each argument that the call has, some
// arguments can be casted to an Call themselves.
Arguments() []ArgumentASTNode
}
// ArgumentASTNode is an interface to help with printing the AST.
type ArgumentASTNode interface {
String() string
}
// A fetchExpression is an expression that fetches a bunch of data from storage based on a path expression
type fetchExpression struct {
// The path expression to fetch
pathArg fetchExpressionPathArg
}
type fetchExpressionPathArg struct {
path string
}
func (a fetchExpressionPathArg) String() string {
return a.path
}
// newFetchExpression creates a new fetch expression for a single path
func newFetchExpression(path string) *fetchExpression {
return &fetchExpression{pathArg: fetchExpressionPathArg{path: path}}
}
func (f *fetchExpression) Name() string {
return "fetch"
}
func (f *fetchExpression) Arguments() []ArgumentASTNode {
return []ArgumentASTNode{f.pathArg}
}
// Execute fetches results from storage
func (f *fetchExpression) Execute(ctx *common.Context) (ts.SeriesList, error) {
begin := time.Now()
result, err := ctx.Engine.FetchByQuery(ctx, f.pathArg.path, ctx.StartTime,
ctx.EndTime, ctx.Timeout)
if err != nil {
return ts.SeriesList{}, err
}
if ctx.TracingEnabled() {
ctx.Trace(common.Trace{
ActivityName: fmt.Sprintf("fetch %s", f.pathArg.path),
Duration: time.Since(begin),
Outputs: common.TraceStats{NumSeries: len(result.SeriesList)},
})
}
for _, r := range result.SeriesList {
r.Specification = f.pathArg.path
}
return ts.SeriesList{Values: result.SeriesList}, nil
}
// Evaluate evaluates the fetch and returns its results as a reflection value, allowing it to be used
// as an input argument to a function that takes a time series
func (f *fetchExpression) Evaluate(ctx *common.Context) (reflect.Value, error) {
timeseries, err := f.Execute(ctx)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(timeseries), nil
}
// CompatibleWith returns true if the reflected type is a time series or a generic interface.
func (f *fetchExpression) CompatibleWith(reflectType reflect.Type) bool {
return reflectType == singlePathSpecType || reflectType == multiplePathSpecsType || reflectType == interfaceType
}
func (f *fetchExpression) String() string {
return fmt.Sprintf("fetch(%s)", f.pathArg.path)
}
// A funcExpression is an expression that evaluates a function returning a timeseries
type funcExpression struct {
call *functionCall
}
// newFuncExpression creates a new expressioon based on the given function call
func newFuncExpression(call *functionCall) (Expression, error) {
if !(call.f.out == seriesListType || call.f.out == unaryContextShifterPtrType || call.f.out == binaryContextShifterPtrType) {
return nil, errTopLevelFunctionMustReturnTimeSeries
}
return &funcExpression{call: call}, nil
}
func (f *funcExpression) Name() string {
return f.call.Name()
}
func (f *funcExpression) Arguments() []ArgumentASTNode {
return f.call.Arguments()
}
// Execute evaluates the function and returns the result as a timeseries
func (f *funcExpression) Execute(ctx *common.Context) (ts.SeriesList, error) {
out, err := f.call.Evaluate(ctx)
if err != nil {
return ts.SeriesList{}, err
}
return out.Interface().(ts.SeriesList), nil
}
func (f *funcExpression) String() string { return f.call.String() }
// A noopExpression is an empty expression that returns nothing
type noopExpression struct{}
// Execute returns nothing
func (noop noopExpression) Execute(ctx *common.Context) (ts.SeriesList, error) {
return ts.SeriesList{}, nil
}
func (noop noopExpression) Name() string {
return "noop"
}
func (noop noopExpression) Arguments() []ArgumentASTNode {
return nil
}
func (noop noopExpression) String() string {
return noop.Name()
}