/
query.go
291 lines (245 loc) · 8.51 KB
/
query.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
291
package lint
import (
"context"
"fmt"
"sync"
"time"
"github.com/bruin-data/bruin/pkg/pipeline"
"github.com/bruin-data/bruin/pkg/query"
"go.uber.org/zap"
)
type queryValidator interface {
IsValid(ctx context.Context, query *query.Query) (bool, error)
}
type connectionManager interface {
GetConnection(conn string) (interface{}, error)
}
type queryExtractor interface {
ExtractQueriesFromString(content string) ([]*query.Query, error)
}
type QueryValidatorRule struct {
Identifier string
TaskType pipeline.AssetType
Connections connectionManager
Extractor queryExtractor
WorkerCount int
Logger *zap.SugaredLogger
}
func (q *QueryValidatorRule) Name() string {
return q.Identifier
}
func (q *QueryValidatorRule) GetApplicableLevels() []Level {
// we need to implement this rule for asset level as well
return []Level{LevelPipeline, LevelAsset}
}
func (q *QueryValidatorRule) ValidateAsset(ctx context.Context, p *pipeline.Pipeline, asset *pipeline.Asset) ([]*Issue, error) {
issues := make([]*Issue, 0)
queries, err := q.Extractor.ExtractQueriesFromString(asset.ExecutableFile.Content)
if err != nil {
q.Logger.Debugf("failed to extract the queries from pipeline '%s' task '%s'", p.Name, asset.Name)
issues = append(issues, &Issue{
Task: asset,
Description: fmt.Sprintf("Cannot read executable file '%s'", asset.ExecutableFile.Path),
Context: []string{
err.Error(),
},
})
return issues, nil //nolint:nilerr
}
if len(queries) == 0 {
issues = append(issues, &Issue{
Task: asset,
Description: fmt.Sprintf("No queries found in executable file '%s'", asset.ExecutableFile.Path),
})
return issues, nil
} else if len(queries) > 1 {
// this has to be the case until we find a nicer solution for no-dry-run databases
// remember that BigQuery or others that support dry-run already return a single query as part of the queryExtractor implementations
// which means the only scenario where there are multiple queries returned is the no-dry-run databases
issues = append(issues, &Issue{
Task: asset,
Description: fmt.Sprintf("Validating multiple queries in a single asset is not supported: '%s'", asset.ExecutableFile.Path),
})
return issues, nil
}
foundQuery := queries[0]
assetConnectionName := p.GetConnectionNameForAsset(asset)
q.Logger.Debugw("The connection will be used for asset", "asset", asset.Name, "connection", assetConnectionName)
validator, err := q.Connections.GetConnection(assetConnectionName)
if err != nil {
issues = append(issues, &Issue{
Task: asset,
Description: fmt.Sprintf("Cannot get connection for task '%s': %+v", asset.Name, err),
})
return issues, nil
}
validatorInstance, ok := validator.(queryValidator)
if !ok {
issues = append(issues, &Issue{
Task: asset,
Description: fmt.Sprintf("Query validator '%s' is not a valid instance", assetConnectionName),
})
return issues, nil
}
valid, err := validatorInstance.IsValid(ctx, foundQuery)
if err != nil {
issues = append(issues, &Issue{
Task: asset,
Description: fmt.Sprintf("Failed to validate query: %s", err),
Context: []string{
"The failing query is as follows:",
foundQuery.Query,
},
})
} else if !valid {
issues = append(issues, &Issue{
Task: asset,
Description: "Query is invalid: " + foundQuery.Query,
Context: []string{
"The failing query is as follows:",
foundQuery.Query,
},
})
}
return issues, nil
}
func (q *QueryValidatorRule) validateTask(p *pipeline.Pipeline, task *pipeline.Asset, done chan<- []*Issue) {
issues := make([]*Issue, 0)
q.Logger.Debugf("validating pipeline '%s' task '%s'", p.Name, task.Name)
queries, err := q.Extractor.ExtractQueriesFromString(task.ExecutableFile.Content)
q.Logger.Debugf("got the query extract results from file for pipeline '%s' task '%s'", p.Name, task.Name)
if err != nil {
q.Logger.Debugf("failed to extract the queries from pipeline '%s' task '%s'", p.Name, task.Name)
issues = append(issues, &Issue{
Task: task,
Description: fmt.Sprintf("Cannot read executable file '%s'", task.ExecutableFile.Path),
Context: []string{
err.Error(),
},
})
done <- issues
q.Logger.Debugf("pushed the error for failed to extract the queries from pipeline '%s' task '%s'", p.Name, task.Name)
return
}
q.Logger.Debugf("Found %d queries in file '%s'", len(queries), task.ExecutableFile.Path)
if len(queries) == 0 {
issues = append(issues, &Issue{
Task: task,
Description: fmt.Sprintf("No queries found in executable file '%s'", task.ExecutableFile.Path),
})
done <- issues
return
} else if len(queries) > 1 {
// this has to be the case until we find a nicer solution for no-dry-run databases
q.Logger.Warnf("Found %d queries in file '%s', we cannot validate script assets, skipping query validation", len(queries), task.ExecutableFile.Path)
done <- issues
return
}
var mu sync.Mutex
var wg sync.WaitGroup
q.Logger.Debugf("Going into the query loop for asset '%s'", task.Name)
for index, foundQuery := range queries {
wg.Add(1)
q.Logger.Debugf("Spawning the %dth index validation for asset '%s'", index, task.Name)
go func(index int, foundQuery *query.Query) {
defer wg.Done()
q.Logger.Debugw("Checking if a query is valid", "path", task.ExecutableFile.Path)
start := time.Now()
assetConnectionName := p.GetConnectionNameForAsset(task)
q.Logger.Debugw("The connection will be used for asset", "asset", task.Name, "connection", assetConnectionName)
validator, err := q.Connections.GetConnection(assetConnectionName)
if err != nil {
mu.Lock()
issues = append(issues, &Issue{
Task: task,
Description: fmt.Sprintf("Cannot get connection for task '%s': %+v", task.Name, err),
})
mu.Unlock()
return
}
valll, ok := validator.(queryValidator)
if !ok {
mu.Lock()
issues = append(issues, &Issue{
Task: task,
Description: fmt.Sprintf("Query validator '%s' is not a valid instance", assetConnectionName),
})
mu.Unlock()
return
}
valid, err := valll.IsValid(context.Background(), foundQuery)
if err != nil {
mu.Lock()
issues = append(issues, &Issue{
Task: task,
Description: fmt.Sprintf("Invalid query found at index %d: %s", index, err),
Context: []string{
"The failing query is as follows:",
foundQuery.Query,
},
})
mu.Unlock()
} else if !valid {
mu.Lock()
issues = append(issues, &Issue{
Task: task,
Description: fmt.Sprintf("Query '%s' is invalid", foundQuery.Query),
Context: []string{
"The failing query is as follows:",
foundQuery.Query,
},
})
mu.Unlock()
}
duration := time.Since(start)
q.Logger.Debugw("Finished with query checking", "path", task.ExecutableFile.Path, "duration", duration)
}(index, foundQuery)
}
q.Logger.Debugf("Waiting for all workers to finish for task '%s'", task.Name)
wg.Wait()
q.Logger.Debugf("Workers finished for task '%s'", task.Name)
done <- issues
q.Logger.Debugf("Pushed issues to the done channel for task '%s'", task.Name)
}
func (q *QueryValidatorRule) bufferSize() int {
return 256
}
func (q *QueryValidatorRule) Validate(p *pipeline.Pipeline) ([]*Issue, error) {
issues := make([]*Issue, 0)
// skip if there are no workers defined
if q.WorkerCount == 0 {
return issues, nil
}
q.Logger.Debugf("Starting validation with %d workers for task type '%s'", q.WorkerCount, q.TaskType)
taskChannel := make(chan *pipeline.Asset, q.bufferSize())
results := make(chan []*Issue, q.bufferSize())
// start the workers
for i := 0; i < q.WorkerCount; i++ {
go func(taskChannel <-chan *pipeline.Asset, results chan<- []*Issue) {
for task := range taskChannel {
q.validateTask(p, task, results)
}
}(taskChannel, results)
}
processedTaskCount := 0
for _, task := range p.Assets {
if task.Type != q.TaskType {
q.Logger.Debug("Skipping task, task type not matched")
continue
}
q.Logger.Debugf("Processing task type: %s", task.Type)
processedTaskCount++
taskChannel <- task
q.Logger.Debugf("Pushed a task to the taskChannel")
}
q.Logger.Debugf("Processed %d tasks at path '%s', closing channel", processedTaskCount, p.DefinitionFile.Path)
for i := 0; i < processedTaskCount; i++ {
q.Logger.Debugf("Waiting for results for i = %d", i)
foundIssues := <-results
q.Logger.Debugf("Received issues: %d/%d", i+1, processedTaskCount)
issues = append(issues, foundIssues...)
}
close(taskChannel)
q.Logger.Debugf("Closed the channel")
return issues, nil
}