-
Notifications
You must be signed in to change notification settings - Fork 787
/
queryParser.go
290 lines (261 loc) · 8.45 KB
/
queryParser.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2020 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.
//go:generate mockgen -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
package gcloud
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/xwb1989/sqlparser"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/types"
)
type (
// QueryParser parses a limited SQL where clause into a struct
QueryParser interface {
Parse(query string) (*parsedQuery, error)
}
queryParser struct{}
parsedQuery struct {
workflowID *string
workflowType *string
startTime int64
closeTime int64
searchPrecision *string
runID *string
emptyResult bool
}
)
// All allowed fields for filtering
const (
WorkflowID = "WorkflowID"
RunID = "RunID"
WorkflowType = "WorkflowType"
CloseTime = "CloseTime"
StartTime = "StartTime"
CloseStatus = "CloseStatus"
SearchPrecision = "SearchPrecision"
)
// Precision specific values
const (
PrecisionDay = "Day"
PrecisionHour = "Hour"
PrecisionMinute = "Minute"
PrecisionSecond = "Second"
)
const (
queryTemplate = "select * from dummy where %s"
defaultDateTimeFormat = time.RFC3339
)
// NewQueryParser creates a new query parser for filestore
func NewQueryParser() QueryParser {
return &queryParser{}
}
func (p *queryParser) Parse(query string) (*parsedQuery, error) {
stmt, err := sqlparser.Parse(fmt.Sprintf(queryTemplate, query))
if err != nil {
return nil, err
}
whereExpr := stmt.(*sqlparser.Select).Where.Expr
parsedQuery := &parsedQuery{}
if err := p.convertWhereExpr(whereExpr, parsedQuery); err != nil {
return nil, err
}
if (parsedQuery.closeTime == 0 && parsedQuery.startTime == 0) || (parsedQuery.closeTime != 0 && parsedQuery.startTime != 0) {
return nil, errors.New("Requires a StartTime or CloseTime")
}
if parsedQuery.searchPrecision == nil {
return nil, errors.New("SearchPrecision is required when searching for a StartTime or CloseTime")
}
return parsedQuery, nil
}
func (p *queryParser) convertWhereExpr(expr sqlparser.Expr, parsedQuery *parsedQuery) error {
if expr == nil {
return errors.New("where expression is nil")
}
switch expr.(type) {
case *sqlparser.ComparisonExpr:
return p.convertComparisonExpr(expr.(*sqlparser.ComparisonExpr), parsedQuery)
case *sqlparser.AndExpr:
return p.convertAndExpr(expr.(*sqlparser.AndExpr), parsedQuery)
case *sqlparser.ParenExpr:
return p.convertParenExpr(expr.(*sqlparser.ParenExpr), parsedQuery)
default:
return errors.New("only comparison and \"and\" expression is supported")
}
}
func (p *queryParser) convertParenExpr(parenExpr *sqlparser.ParenExpr, parsedQuery *parsedQuery) error {
return p.convertWhereExpr(parenExpr.Expr, parsedQuery)
}
func (p *queryParser) convertAndExpr(andExpr *sqlparser.AndExpr, parsedQuery *parsedQuery) error {
if err := p.convertWhereExpr(andExpr.Left, parsedQuery); err != nil {
return err
}
return p.convertWhereExpr(andExpr.Right, parsedQuery)
}
func (p *queryParser) convertComparisonExpr(compExpr *sqlparser.ComparisonExpr, parsedQuery *parsedQuery) error {
colName, ok := compExpr.Left.(*sqlparser.ColName)
if !ok {
return fmt.Errorf("invalid filter name: %s", sqlparser.String(compExpr.Left))
}
colNameStr := sqlparser.String(colName)
op := compExpr.Operator
valExpr, ok := compExpr.Right.(*sqlparser.SQLVal)
if !ok {
return fmt.Errorf("invalid value: %s", sqlparser.String(compExpr.Right))
}
valStr := sqlparser.String(valExpr)
switch colNameStr {
case WorkflowID:
val, err := extractStringValue(valStr)
if err != nil {
return err
}
if op != "=" {
return fmt.Errorf("only operator = is supported for %s with Google Cloud Storage", WorkflowID)
}
if parsedQuery.workflowID != nil && *parsedQuery.workflowID != val {
parsedQuery.emptyResult = true
return nil
}
parsedQuery.workflowID = common.StringPtr(val)
case RunID:
val, err := extractStringValue(valStr)
if err != nil {
return err
}
if op != "=" {
return fmt.Errorf("only operator = is supported for %s with Google Cloud Storage", RunID)
}
if parsedQuery.runID != nil && *parsedQuery.runID != val {
parsedQuery.emptyResult = true
return nil
}
parsedQuery.runID = common.StringPtr(val)
case CloseTime:
timestamp, err := convertToTimestamp(valStr)
if err != nil {
return err
}
if op != "=" {
return fmt.Errorf("only operator = is supported for %s with Google Cloud Storage", CloseTime)
}
parsedQuery.closeTime = timestamp
case StartTime:
timestamp, err := convertToTimestamp(valStr)
if err != nil {
return err
}
if op != "=" {
return fmt.Errorf("only operator = is supported for %s with Google Cloud Storage", StartTime)
}
parsedQuery.startTime = timestamp
case WorkflowType:
val, err := extractStringValue(valStr)
if err != nil {
return err
}
if op != "=" {
return fmt.Errorf("only operator = is supported for %s with Google Cloud Storage", WorkflowType)
}
if parsedQuery.workflowType != nil && *parsedQuery.workflowType != val {
parsedQuery.emptyResult = true
return nil
}
parsedQuery.workflowType = common.StringPtr(val)
case SearchPrecision:
val, err := extractStringValue(valStr)
if err != nil {
return err
}
if op != "=" {
return fmt.Errorf("only operator = is supported for %s with Google Cloud Storage", SearchPrecision)
}
if parsedQuery.searchPrecision != nil && *parsedQuery.searchPrecision != val {
return fmt.Errorf("only one expression is allowed for %s", SearchPrecision)
}
switch val {
case PrecisionDay:
case PrecisionHour:
case PrecisionMinute:
case PrecisionSecond:
default:
return fmt.Errorf("invalid value for %s: %s", SearchPrecision, val)
}
parsedQuery.searchPrecision = common.StringPtr(val)
default:
return fmt.Errorf("unknown filter name: %s", colNameStr)
}
return nil
}
func (p *queryParser) convertCloseTime(timestamp int64, op string, parsedQuery *parsedQuery) error {
switch op {
case "=":
if err := p.convertCloseTime(timestamp, ">=", parsedQuery); err != nil {
return err
}
if err := p.convertCloseTime(timestamp, "<=", parsedQuery); err != nil {
return err
}
default:
return fmt.Errorf("operator %s is not supported for close time", op)
}
return nil
}
func convertToTimestamp(timeStr string) (int64, error) {
timestamp, err := strconv.ParseInt(timeStr, 10, 64)
if err == nil {
return timestamp, nil
}
timestampStr, err := extractStringValue(timeStr)
if err != nil {
return 0, err
}
parsedTime, err := time.Parse(defaultDateTimeFormat, timestampStr)
if err != nil {
return 0, err
}
return parsedTime.UnixNano(), nil
}
func convertStatusStr(statusStr string) (types.WorkflowExecutionCloseStatus, error) {
statusStr = strings.ToLower(statusStr)
switch statusStr {
case "completed":
return types.WorkflowExecutionCloseStatusCompleted, nil
case "failed":
return types.WorkflowExecutionCloseStatusFailed, nil
case "canceled":
return types.WorkflowExecutionCloseStatusCanceled, nil
case "continuedasnew":
return types.WorkflowExecutionCloseStatusContinuedAsNew, nil
case "timedout":
return types.WorkflowExecutionCloseStatusTimedOut, nil
default:
return 0, fmt.Errorf("unknown workflow close status: %s", statusStr)
}
}
func extractStringValue(s string) (string, error) {
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
return s[1 : len(s)-1], nil
}
return "", fmt.Errorf("value %s is not a string value", s)
}