forked from cockroachdb/cockroach
/
scan_test.go
188 lines (168 loc) · 5.09 KB
/
scan_test.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
// Copyright 2016 The Cockroach Authors.
//
// 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
//
// http://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.
//
// Author: Radu Berinde (radu@cockroachlabs.com)
package sql_test
import (
"bytes"
gosql "database/sql"
"fmt"
"math/rand"
"sort"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
)
// genAs returns num random distinct ordered values in [0, valRange).
func genValues(num, valRange int) []int {
// Generate num _distinct_ values. We do this by generating a partial permutation.
perm := make([]int, valRange)
for i := 0; i < valRange; i++ {
perm[i] = i
}
for i := 0; i < num; i++ {
// Choose a random element starting at i.
pos := rand.Int() % (num - i)
perm[i], perm[i+pos] = perm[i+pos], perm[i]
}
perm = perm[:num]
// Sort the values. These distinct values will be the starts of our ranges.
sort.Ints(perm)
return perm
}
// testScanBatchQuery runs a query of the form
// SELECT a,B FROM test.scan WHERE a IN (1,5,3..) AND b >= 5 AND b <= 10
// numSpans controls the number of possible values for a.
func testScanBatchQuery(t *testing.T, db *gosql.DB, numSpans, numAs, numBs int, reverse bool) {
// Generate numSpans values for A
aVals := genValues(numSpans, numAs)
// Generate a random range for B
bStart := rand.Int() % numBs
bEnd := bStart + rand.Int()%(numBs-bStart)
var expected [][2]int
for _, a := range aVals {
for b := bStart; b <= bEnd; b++ {
expected = append(expected, [2]int{a, b})
}
}
if len(aVals) == 0 {
// No filter on a.
for a := 0; a < numAs; a++ {
for b := bStart; b <= bEnd; b++ {
expected = append(expected, [2]int{a, b})
}
}
}
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("SELECT a,b FROM test.scan WHERE b >= %d AND b <= %d", bStart, bEnd))
for i, a := range aVals {
if i == 0 {
buf.WriteString(fmt.Sprintf(" AND a IN (%d", a))
} else {
buf.WriteString(fmt.Sprintf(",%d", a))
}
}
if len(aVals) > 0 {
buf.WriteString(")")
}
if reverse {
buf.WriteString(" ORDER BY a DESC, b DESC")
for i, j := 0, len(expected)-1; i < j; i, j = i+1, j-1 {
expected[i], expected[j] = expected[j], expected[i]
}
}
rows, err := db.Query(buf.String())
if err != nil {
t.Fatal(err)
}
n := 0
for rows.Next() {
if n >= len(expected) {
t.Fatalf("too many rows (expected %d)", len(expected))
}
var a, b int
err = rows.Scan(&a, &b)
if err != nil {
t.Fatal(err)
}
if a != expected[n][0] || b != expected[n][1] {
t.Errorf("row %d: invalid values %d,%d (expected %d,%d)",
n, a, b, expected[n][0], expected[n][1])
}
n++
}
if n != len(expected) {
t.Fatalf("too few rows %d (expected %d)", n, len(expected))
}
rows.Close()
if err := rows.Err(); err != nil {
t.Fatal(err)
}
}
// TestScanBatches tests the scan-in-batches code by artificially setting the batch size to
// particular values and performing queries.
func TestScanBatches(t *testing.T) {
defer leaktest.AfterTest(t)()
s, db, _ := serverutils.StartServer(
t, base.TestServerArgs{UseDatabase: "test"})
defer s.Stopper().Stop()
if _, err := db.Exec(`CREATE DATABASE IF NOT EXISTS test`); err != nil {
t.Fatal(err)
}
// The test will screw around with KVBatchSize; make sure to restore it at the end.
restore := sqlbase.SetKVBatchSize(10)
defer restore()
numAs := 5
numBs := 20
if _, err := db.Exec(`DROP TABLE IF EXISTS test.scan`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE test.scan (a INT, b INT, v STRING, PRIMARY KEY (a, b))`); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
buf.WriteString(`INSERT INTO test.scan VALUES `)
for a := 0; a < numAs; a++ {
for b := 0; b < numBs; b++ {
if a+b > 0 {
buf.WriteString(", ")
}
if (a+b)%2 == 0 {
fmt.Fprintf(&buf, "(%d, %d, 'str%d%d')", a, b, a, b)
} else {
// Every other row doesn't get the string value (to have NULLs).
fmt.Fprintf(&buf, "(%d, %d, NULL)", a, b)
}
}
}
if _, err := db.Exec(buf.String()); err != nil {
t.Fatal(err)
}
// The table will have one key for the even rows, and two keys for the odd rows.
numKeys := 3 * numAs * numBs / 2
batchSizes := []int{1, 2, 3, 5, 10, 13, 100, numKeys - 1, numKeys, numKeys + 1}
numSpanValues := []int{0, 1, 2, 3}
for _, batch := range batchSizes {
sqlbase.SetKVBatchSize(int64(batch))
for _, numSpans := range numSpanValues {
testScanBatchQuery(t, db, numSpans, numAs, numBs, false)
testScanBatchQuery(t, db, numSpans, numAs, numBs, true)
}
}
if _, err := db.Exec(`DROP TABLE test.scan`); err != nil {
t.Fatal(err)
}
}