-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.go
300 lines (252 loc) · 8.72 KB
/
queries.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
292
293
294
295
296
297
298
299
300
// Copyright 2022-2023, Matthew Winter
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"context"
"encoding/csv"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
)
type QueryDetails struct {
SQL string
InputFile string
OutputFile string
Number int
Error error
QueryStartTime time.Time
QueryEndTime time.Time
FirstRowReturnedTime time.Time
AllRowsReturnedTime time.Time
TotalRowsReturned int64
}
type Queries struct {
Query []QueryDetails
ExecutionOrder []int
}
//---------------------------------------------------------------------------------------
// Walk the provided Input Path and load all SQL Query Files
func (queries *Queries) LoadQueries(inputPath string, outputPath string) error {
// Calculate the Absolute Output Path and add a results sub directory
outputPath, err := filepath.Abs(filepath.Join(outputPath, time.Now().Format("2006-01-02.150405")))
if err != nil {
return fmt.Errorf("[LoadQueries] Failed To Get Absolute Output Path: %w", err)
}
// Execute a Glob to return all files matching the provided pattern
matches, err := filepath.Glob(inputPath)
if err != nil {
return fmt.Errorf("[LoadQueries] Glob Failed: %w", err)
}
// Load all matching files returned from the Glob
queryCount := 1
for _, filename := range matches {
fileInfo, err := os.Stat(filename)
if err != nil {
return fmt.Errorf("[LoadQueries] Failed To Get File Info: %w", err)
}
// Skip Directories
if fileInfo.IsDir() {
continue
}
inputFile, _ := filepath.Abs(filename)
outputFile, _ := filepath.Abs(filepath.Join(outputPath, fmt.Sprintf("results-query-%06d.output", queryCount)))
buf, err := os.ReadFile(inputFile)
if err != nil {
return fmt.Errorf("[LoadQueries] Read Input File Failed: %w", err)
}
sql := QueryDetails{
SQL: string(buf),
InputFile: inputFile,
OutputFile: outputFile,
Number: queryCount,
}
logger.Debug().Int("Query Number", sql.Number).Msg("Query Details")
logger.Debug().Str("Input File", sql.InputFile).Msg(indent)
logger.Debug().Str("Output File", sql.OutputFile).Msg(indent)
logger.Debug().Str("SQL", sql.SQL).Msg(indent)
queries.Query = append(queries.Query, sql)
queries.ExecutionOrder = append(queries.ExecutionOrder, queryCount-1)
queryCount++
}
return nil
}
//---------------------------------------------------------------------------------------
// Shuffle the Query Execution Order
func (queries *Queries) ShuffleExecutionOrder(shuffle bool) {
if shuffle {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(queries.ExecutionOrder), func(i, j int) {
queries.ExecutionOrder[i], queries.ExecutionOrder[j] = queries.ExecutionOrder[j], queries.ExecutionOrder[i]
})
logger.Info().Msg("Query Execution Order Shuffle Complete")
}
}
//---------------------------------------------------------------------------------------
// Execute the Queries in BigQuery
func (queries *Queries) ExecuteQueries(project string, dataset string, location string, disableQueryCache bool, dryRun bool, delimiter string) error {
// Establish a BigQuery Client Connection
logger.Info().Msg("Establishing BigQuery Client Connection")
ctx := context.Background()
client, err := bigquery.NewClient(ctx, project)
if err != nil {
return fmt.Errorf("Failed Establishing BigQuery Client Connection: %w", err)
}
defer client.Close()
// BigQuery Client Configuration
client.Location = location
// Execut SQL Queries
errorCount := 0
for _, index := range queries.ExecutionOrder {
qd := queries.Query[index]
if dryRun {
qd.ExecuteDryRun(ctx, client, project, dataset, location, disableQueryCache)
qd.LogExecuteDryRun()
} else {
qd.ExecuteQuery(ctx, client, project, dataset, location, disableQueryCache, delimiter)
qd.LogExecuteQuery()
}
// Count the Errors
if qd.Error != nil {
errorCount++
}
}
// Raise an Error if one of the queries executed failed
if errorCount > 0 {
return fmt.Errorf("One or More Queries Failed")
}
return nil
}
//---------------------------------------------------------------------------------------
// Execute Query
func (qd *QueryDetails) ExecuteQuery(ctx context.Context, client *bigquery.Client, project string, dataset string, location string, disableQueryCache bool, delimiter string) {
// Make Sure the Output File Path Exists
path := filepath.Dir(qd.OutputFile)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err = os.MkdirAll(path, 0700); err != nil {
qd.Error = fmt.Errorf("Failed to Create the Output File Path")
return
}
}
// Create and Configure Query
q := client.Query(qd.SQL)
q.DefaultProjectID = project
q.DefaultDatasetID = dataset
q.Location = location
q.DisableQueryCache = disableQueryCache
q.DryRun = false
// Initiate the Query Job
qd.QueryStartTime = time.Now()
it, err := q.Read(ctx)
qd.QueryEndTime = time.Now()
if err != nil {
qd.Error = err
return
}
// Open the Output File
f, err := os.Create(qd.OutputFile)
if err != nil {
qd.Error = fmt.Errorf("Failed to Open the Output File")
return
}
defer f.Close()
// Ready the CSV Writer and use a buffered io writer
w := csv.NewWriter(bufio.NewWriter(f))
w.Comma = rune(delimiter[0])
defer w.Flush()
var rl RowLoader
var rowCount int64
for {
err := it.Next(&rl)
if rowCount == 0 {
qd.FirstRowReturnedTime = time.Now()
}
if err == iterator.Done {
qd.AllRowsReturnedTime = time.Now()
qd.TotalRowsReturned = rowCount
break
}
if err != nil {
qd.Error = err
return
}
if err := w.Write(rl.Row); err != nil {
qd.Error = fmt.Errorf("Failed Writing to the Output File")
return
}
rowCount++
}
}
//---------------------------------------------------------------------------------------
// Execute Dry Run Query
func (qd *QueryDetails) ExecuteDryRun(ctx context.Context, client *bigquery.Client, project string, dataset string, location string, disableQueryCache bool) {
// Create and Configure Query
q := client.Query(qd.SQL)
q.DefaultProjectID = project
q.DefaultDatasetID = dataset
q.Location = location
q.DisableQueryCache = disableQueryCache
q.DryRun = true
// Initiate the Query Job
qd.QueryStartTime = time.Now()
job, err := q.Run(ctx)
if err != nil {
qd.Error = err
return
}
// Check the Last Status for Errors
status := job.LastStatus()
if err = status.Err(); err != nil {
qd.Error = err
return
}
qd.QueryEndTime = time.Now()
}
//---------------------------------------------------------------------------------------
// Output the Query Execution Statistics to the Log
func (qd *QueryDetails) LogExecuteQuery() {
logger.Info().Int("Query Number", qd.Number).Msg("Query Execution")
logger.Info().Str("Input File", qd.InputFile).Msg(indent)
// Output Error Message if one exists, but nothing else
if qd.Error != nil {
logger.Error().Err(qd.Error).Msg(indent)
return
}
logger.Info().Str("Output File", qd.OutputFile).Msg(indent)
logger.Info().Time("Query Execution Start", qd.QueryStartTime).Msg(indent)
logger.Info().Time("Query Execution End", qd.QueryEndTime).Msg(indent)
logger.Info().TimeDiff("Execution Time (ms)", qd.QueryEndTime, qd.QueryStartTime).Msg(indent)
logger.Info().Time("First Row Returned", qd.FirstRowReturnedTime).Msg(indent)
logger.Info().Time("All Rows Returned", qd.AllRowsReturnedTime).Msg(indent)
logger.Info().TimeDiff("Return Time (ms)", qd.AllRowsReturnedTime, qd.QueryEndTime).Msg(indent)
logger.Info().Int64("Total Rows Returned", qd.TotalRowsReturned).Msg(indent)
}
//---------------------------------------------------------------------------------------
// Output the Query Dry Run Statistics to the Log
func (qd *QueryDetails) LogExecuteDryRun() {
logger.Info().Int("Query Number", qd.Number).Msg("Query Dry Run")
logger.Info().Str("Input File", qd.InputFile).Msg(indent)
// Output Error Message if one exists, but nothing else
if qd.Error != nil {
logger.Error().Err(qd.Error).Msg(indent)
return
}
logger.Info().Time("Query Execution Start", qd.QueryStartTime).Msg(indent)
logger.Info().Time("Query Execution End", qd.QueryEndTime).Msg(indent)
logger.Info().TimeDiff("Execution Time (ms)", qd.QueryEndTime, qd.QueryStartTime).Msg(indent)
}