-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathquery_comparison_util.go
489 lines (445 loc) · 15 KB
/
query_comparison_util.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package tests
import (
"context"
gosql "database/sql"
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/test"
"github.com/cockroachdb/cockroach/pkg/internal/sqlsmith"
"github.com/cockroachdb/cockroach/pkg/roachprod/install"
"github.com/cockroachdb/cockroach/pkg/testutils/floatcmp"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/google/go-cmp/cmp"
)
type queryComparisonTest struct {
name string
setupName string
run func(*sqlsmith.Smither, *rand.Rand, queryComparisonHelper) error
}
func runQueryComparison(
ctx context.Context, t test.Test, c cluster.Cluster, qct *queryComparisonTest,
) {
roundTimeout := 10 * time.Minute
// A cluster context with a slightly longer timeout than the test timeout is
// used so cluster creation or cleanup commands should never time out and
// result in "context deadline exceeded" Github issues being created.
var clusterCancel context.CancelFunc
var clusterCtx context.Context
clusterCtx, clusterCancel = context.WithTimeout(ctx, t.Spec().(*registry.TestSpec).Timeout-2*time.Minute)
defer clusterCancel()
// Run 10 minute iterations of query comparison in a loop for about the entire
// test, giving 5 minutes at the end to allow the test to shut down cleanly.
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(clusterCtx, t.Spec().(*registry.TestSpec).Timeout-5*time.Minute)
defer cancel()
done := ctx.Done()
shouldExit := func() bool {
select {
case <-done:
return true
default:
return false
}
}
c.Put(clusterCtx, t.Cockroach(), "./cockroach")
for i := 0; ; i++ {
if shouldExit() {
return
}
c.Start(clusterCtx, t.L(), option.DefaultStartOpts(), install.MakeClusterSettings())
runOneRoundQueryComparison(ctx, i, roundTimeout, t, c, qct)
// If this iteration was interrupted because the timeout of ctx has been
// reached, we want to cleanly exit from the test, without wiping out the
// cluster (if we tried that, we'd get an error because ctx is canceled).
if shouldExit() {
return
}
c.Stop(clusterCtx, t.L(), option.DefaultStopOpts())
c.Wipe(clusterCtx)
}
}
// runOneRoundQueryComparison creates a random schema, inserts random data, and
// then executes queries until the roundTimeout is reached.
func runOneRoundQueryComparison(
ctx context.Context,
iter int,
roundTimeout time.Duration,
t test.Test,
c cluster.Cluster,
qct *queryComparisonTest,
) {
// Set up a statement logger for easy reproduction. We only
// want to log successful statements and statements that
// produced a final error or panic.
logPath := filepath.Join(t.ArtifactsDir(), fmt.Sprintf("%s%03d.log", qct.name, iter))
log, err := os.Create(logPath)
if err != nil {
t.Fatalf("could not create %s%03d.log: %v", qct.name, iter, err)
}
defer log.Close()
logStmt := func(stmt string) {
stmt = strings.TrimSpace(stmt)
if stmt == "" {
return
}
fmt.Fprint(log, stmt)
if !strings.HasSuffix(stmt, ";") {
fmt.Fprint(log, ";")
}
// Blank lines are necessary for reduce -costfuzz to function correctly.
fmt.Fprint(log, "\n\n")
}
printStmt := func(stmt string) {
stmt = strings.TrimSpace(stmt)
if stmt == "" {
return
}
t.L().Printf(stmt)
if !strings.HasSuffix(stmt, ";") {
t.L().Printf(";")
}
t.L().Printf("\n\n")
}
failureLogPath := filepath.Join(
t.ArtifactsDir(), fmt.Sprintf("%s%03d.failure.log", qct.name, iter),
)
failureLog, err := os.Create(failureLogPath)
if err != nil {
t.Fatalf("could not create %s%03d.failure.log: %v", qct.name, iter, err)
}
defer failureLog.Close()
logFailure := func(stmt string, rows [][]string) {
fmt.Fprint(failureLog, stmt)
fmt.Fprint(failureLog, "\n----\n")
fmt.Fprint(failureLog, sqlutils.MatrixToStr(rows))
fmt.Fprint(failureLog, "\n")
}
conn := c.Conn(ctx, t.L(), 1)
rnd, seed := randutil.NewTestRand()
t.L().Printf("seed: %d", seed)
t.L().Printf("setupName: %s", qct.setupName)
setup := sqlsmith.Setups[qct.setupName](rnd)
t.Status("executing setup")
t.L().Printf("setup:\n%s", strings.Join(setup, "\n"))
for _, stmt := range setup {
if _, err := conn.Exec(stmt); err != nil {
t.Fatal(err)
} else {
logStmt(stmt)
}
}
setStmtTimeout := fmt.Sprintf("SET statement_timeout='%s';", statementTimeout.String())
t.Status("setting statement_timeout")
t.L().Printf("statement timeout:\n%s", setStmtTimeout)
if _, err := conn.Exec(setStmtTimeout); err != nil {
t.Fatal(err)
}
logStmt(setStmtTimeout)
setUnconstrainedStmt := "SET unconstrained_non_covering_index_scan_enabled = true;"
t.Status("setting unconstrained_non_covering_index_scan_enabled")
t.L().Printf("\n%s", setUnconstrainedStmt)
if _, err := conn.Exec(setUnconstrainedStmt); err != nil {
logStmt(setUnconstrainedStmt)
t.Fatal(err)
}
logStmt(setUnconstrainedStmt)
isMultiRegion := qct.setupName == sqlsmith.SeedMultiRegionSetupName
if isMultiRegion {
setupMultiRegionDatabase(t, conn, logStmt)
}
// Initialize a smither that generates only INSERT and UPDATE statements with
// the InsUpdOnly option.
mutatingSmither := newMutatingSmither(conn, rnd, t, true /* disableDelete */, isMultiRegion)
defer mutatingSmither.Close()
// Initialize a smither that generates only deterministic SELECT statements.
smither, err := sqlsmith.NewSmither(conn, rnd,
sqlsmith.DisableMutations(), sqlsmith.DisableNondeterministicFns(), sqlsmith.DisableLimits(),
sqlsmith.UnlikelyConstantPredicate(), sqlsmith.FavorCommonData(),
sqlsmith.UnlikelyRandomNulls(), sqlsmith.DisableCrossJoins(),
sqlsmith.DisableIndexHints(), sqlsmith.DisableWith(), sqlsmith.DisableDecimals(),
sqlsmith.LowProbabilityWhereClauseWithJoinTables(),
sqlsmith.DisableUDFs(),
sqlsmith.SetComplexity(.3),
sqlsmith.SetScalarComplexity(.1),
)
if err != nil {
t.Fatal(err)
}
defer smither.Close()
t.Status("running ", qct.name)
until := time.After(roundTimeout)
done := ctx.Done()
for i := 1; ; i++ {
select {
case <-until:
return
case <-done:
return
default:
}
const numInitialMutations = 1000
if i == numInitialMutations {
t.Status("running ", qct.name, ": ", i, " initial mutations completed")
// Initialize a new mutating smither that generates INSERT, UPDATE and
// DELETE statements with the MutationsOnly option.
mutatingSmither = newMutatingSmither(conn, rnd, t, false /* disableDelete */, isMultiRegion)
defer mutatingSmither.Close()
}
if i%1000 == 0 {
if i != numInitialMutations {
t.Status("running ", qct.name, ": ", i, " statements completed")
}
}
// Run `numInitialMutations` mutations first so that the tables have rows.
// Run a mutation every 25th iteration afterwards to continually change the
// state of the database.
if i < numInitialMutations || i%25 == 0 {
runMutationStatement(conn, mutatingSmither, logStmt)
continue
}
h := queryComparisonHelper{
conn: conn,
logStmt: logStmt,
logFailure: logFailure,
printStmt: printStmt,
stmtNo: i,
}
if err := qct.run(smither, rnd, h); err != nil {
t.Fatal(err)
}
}
}
func newMutatingSmither(
conn *gosql.DB, rnd *rand.Rand, t test.Test, disableDelete bool, isMultiRegion bool,
) (mutatingSmither *sqlsmith.Smither) {
var smitherOpts []sqlsmith.SmitherOption
smitherOpts = append(smitherOpts,
sqlsmith.FavorCommonData(), sqlsmith.UnlikelyRandomNulls(),
sqlsmith.DisableInsertSelect(), sqlsmith.DisableCrossJoins(),
sqlsmith.SetComplexity(.05),
sqlsmith.SetScalarComplexity(.01))
if disableDelete {
smitherOpts = append(smitherOpts, sqlsmith.InsUpdOnly())
} else {
smitherOpts = append(smitherOpts, sqlsmith.MutationsOnly())
}
if isMultiRegion {
smitherOpts = append(smitherOpts, sqlsmith.EnableAlters())
smitherOpts = append(smitherOpts, sqlsmith.MultiRegionDDLs())
}
var err error
mutatingSmither, err = sqlsmith.NewSmither(conn, rnd, smitherOpts...)
if err != nil {
t.Fatal(err)
}
return mutatingSmither
}
// sqlAndOutput holds a SQL statement and its output.
type sqlAndOutput struct {
sql string
output [][]string
}
// queryComparisonHelper is used to execute statements in query comparison
// tests. It keeps track of each statement that is executed so they can be
// logged in case of failure.
type queryComparisonHelper struct {
conn *gosql.DB
logStmt func(string)
logFailure func(string, [][]string)
printStmt func(string)
stmtNo int
statements []string
statementsAndExplains []sqlAndOutput
colTypes []string
}
// runQuery runs the given query and returns the output. If the stmt doesn't
// result in an error, as a side effect, it also saves the query, the query
// plan, and the output of running the query so they can be logged in case of
// failure.
func (h *queryComparisonHelper) runQuery(stmt string) ([][]string, error) {
// Log this statement with a timestamp but commented out. This will help in
// cases when the stmt will get stuck and the whole test will time out (in
// such a scenario, since the stmt didn't execute successfully, it won't get
// logged by the caller).
h.logStmt(fmt.Sprintf("-- %s: %s", timeutil.Now(),
// Remove all newline symbols to log this stmt as a single line. This
// way this auxiliary logging takes up less space (if the stmt executes
// successfully, it'll still get logged with the nice formatting).
strings.ReplaceAll(stmt, "\n", "")),
)
runQueryImpl := func(stmt string) ([][]string, error) {
rows, err := h.conn.Query(stmt)
if err != nil {
return nil, err
}
defer rows.Close()
cts, err := rows.ColumnTypes()
if err != nil {
return nil, err
}
h.colTypes = make([]string, len(cts))
for i, ct := range cts {
h.colTypes[i] = ct.DatabaseTypeName()
}
return sqlutils.RowsToStrMatrix(rows)
}
// First use EXPLAIN to try to get the query plan. This is best-effort, and
// only for the purpose of debugging, so ignore any errors.
explainStmt := "EXPLAIN " + stmt
explainRows, err := runQueryImpl(explainStmt)
if err == nil {
h.statementsAndExplains = append(
h.statementsAndExplains, sqlAndOutput{sql: explainStmt, output: explainRows},
)
}
// Now run the query and save the output.
rows, err := runQueryImpl(stmt)
if err != nil {
return nil, err
}
// Only save the stmt on success - this makes it easier to reproduce the
// log. The caller still can include it into the statements later if
// necessary.
h.statements = append(h.statements, stmt)
h.statementsAndExplains = append(h.statementsAndExplains, sqlAndOutput{sql: stmt, output: rows})
return rows, nil
}
// execStmt executes the given statement. As a side effect, it also saves the
// statement so it can be logged in case of failure.
func (h *queryComparisonHelper) execStmt(stmt string) error {
h.statements = append(h.statements, stmt)
h.statementsAndExplains = append(h.statementsAndExplains, sqlAndOutput{sql: stmt})
_, err := h.conn.Exec(stmt)
return err
}
// logStatements logs all the queries and statements that were executed.
func (h *queryComparisonHelper) logStatements() {
for _, stmt := range h.statements {
h.logStmt(stmt)
h.printStmt(stmt)
}
}
// logVerboseOutput logs all the queries and statements that were executed,
// as well as the output rows and EXPLAIN output.
func (h *queryComparisonHelper) logVerboseOutput() {
for _, stmtOrExplain := range h.statementsAndExplains {
h.logFailure(stmtOrExplain.sql, stmtOrExplain.output)
}
}
// makeError wraps the error with the given message and includes the number of
// statements run so far.
func (h *queryComparisonHelper) makeError(err error, msg string) error {
return errors.Wrapf(err, "%s. %d statements run", msg, h.stmtNo)
}
// unsortedMatricesDiffWithFloatComp sorts and compares the rows in rowMatrix1
// to rowMatrix2 and outputs a diff or message related to the comparison. If a
// string comparison of the rows fails, and they contain floats or decimals, it
// performs an approximate comparison of the values.
func unsortedMatricesDiffWithFloatComp(
rowMatrix1, rowMatrix2 [][]string, colTypes []string,
) (string, error) {
// Use an unlikely string as a separator when joining rows for initial
// comparison, so that we can split the sorted rows if we need to make
// additional comparisons.
sep := ",unsortedMatricesDiffWithFloatComp separator,"
var rows1 []string
for _, row := range rowMatrix1 {
rows1 = append(rows1, strings.Join(row[:], sep))
}
var rows2 []string
for _, row := range rowMatrix2 {
rows2 = append(rows2, strings.Join(row[:], sep))
}
sort.Strings(rows1)
sort.Strings(rows2)
result := cmp.Diff(rows1, rows2)
if result == "" {
return result, nil
}
if len(rows1) != len(rows2) {
return fmt.Sprintf("row counts are not the same (%d vs %d)", len(rows1), len(rows2)), nil
}
var decimalIdxs []int
var floatIdxs []int
for i := range colTypes {
// On s390x, check that values for both float and decimal coltypes are
// approximately equal to take into account platform differences in floating
// point calculations. On other architectures, check float values only.
if runtime.GOARCH == "s390x" && colTypes[i] == "DECIMAL" {
decimalIdxs = append(decimalIdxs, i)
}
if colTypes[i] == "FLOAT4" || colTypes[i] == "FLOAT8" {
floatIdxs = append(floatIdxs, i)
}
}
if len(decimalIdxs) == 0 && len(floatIdxs) == 0 {
return result, nil
}
for i := range rows1 {
// Split the sorted rows.
row1 := strings.Split(rows1[i], sep)
row2 := strings.Split(rows2[i], sep)
// Check that decimal values are approximately equal.
for j := range decimalIdxs {
match, err := floatcmp.FloatsMatchApprox(row1[j], row2[j])
if err != nil {
return "", err
}
if !match {
return result, nil
}
}
// Check that float values are approximately equal.
for j := range floatIdxs {
var err error
var match bool
if runtime.GOARCH == "s390x" {
match, err = floatcmp.FloatsMatchApprox(row1[j], row2[j])
} else {
match, err = floatcmp.FloatsMatch(row1[j], row2[j])
}
if err != nil {
return "", err
}
if !match {
return result, nil
}
}
}
return "", nil
}
// unsortedMatricesDiff sorts and compares rows of data.
func unsortedMatricesDiff(rowMatrix1, rowMatrix2 [][]string) string {
var rows1 []string
for _, row := range rowMatrix1 {
rows1 = append(rows1, strings.Join(row[:], ","))
}
var rows2 []string
for _, row := range rowMatrix2 {
rows2 = append(rows2, strings.Join(row[:], ","))
}
sort.Strings(rows1)
sort.Strings(rows2)
return cmp.Diff(rows1, rows2)
}