-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
ttljob_query_builder.go
331 lines (306 loc) · 9.09 KB
/
ttljob_query_builder.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
// 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 ttljob
import (
"bytes"
"context"
"fmt"
"time"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/errors"
)
// selectQueryBuilder is responsible for maintaining state around the
// SELECT portion of the TTL job.
type selectQueryBuilder struct {
tableID descpb.ID
pkColumns []string
selectOpName string
rangeToProcess rangeToProcess
selectBatchSize int64
aost time.Time
ttlExpr catpb.Expression
// isFirst is true if we have not invoked a query using the builder yet.
isFirst bool
// cachedQuery is the cached query, which stays the same from the second
// iteration onwards.
cachedQuery string
// cachedArgs keeps a cache of args to use in the run query.
// The cache is of form [cutoff, <endFilterClause...>, <startFilterClause..>].
cachedArgs []interface{}
// pkColumnNamesSQL caches the column names of the PK.
pkColumnNamesSQL string
// endPKColumnNamesSQL caches the column names of the ending PK.
endPKColumnNamesSQL string
}
type rangeToProcess struct {
startPK, endPK tree.Datums
}
func makeSelectQueryBuilder(
tableID descpb.ID,
cutoff time.Time,
pkColumns []string,
relationName string,
rangeToProcess rangeToProcess,
aost time.Time,
selectBatchSize int64,
ttlExpr catpb.Expression,
) selectQueryBuilder {
// We will have a maximum of 1 + len(pkColumns)*2 columns, where one
// is reserved for AOST, and len(pkColumns) for both start and end key.
cachedArgs := make([]interface{}, 0, 1+len(pkColumns)*2)
cachedArgs = append(cachedArgs, cutoff)
endPK := rangeToProcess.endPK
for _, d := range endPK {
cachedArgs = append(cachedArgs, d)
}
for _, d := range rangeToProcess.startPK {
cachedArgs = append(cachedArgs, d)
}
return selectQueryBuilder{
tableID: tableID,
pkColumns: pkColumns,
selectOpName: fmt.Sprintf("ttl select %s", relationName),
rangeToProcess: rangeToProcess,
aost: aost,
selectBatchSize: selectBatchSize,
ttlExpr: ttlExpr,
cachedArgs: cachedArgs,
isFirst: true,
pkColumnNamesSQL: makeColumnNamesSQL(pkColumns),
endPKColumnNamesSQL: makeColumnNamesSQL(pkColumns[:len(endPK)]),
}
}
func (b *selectQueryBuilder) buildQuery() string {
// Generate the end key clause for SELECT, which always stays the same.
// Start from $2 as $1 is for the now clause.
// The end key of a range is exclusive, so use <.
var endFilterClause string
endPK := b.rangeToProcess.endPK
if len(endPK) > 0 {
endFilterClause = fmt.Sprintf(" AND (%s) < (", b.endPKColumnNamesSQL)
for i := range endPK {
if i > 0 {
endFilterClause += ", "
}
endFilterClause += fmt.Sprintf("$%d", i+2)
}
endFilterClause += ")"
}
startPK := b.rangeToProcess.startPK
var filterClause string
if !b.isFirst {
// After the first query, we always want (col1, ...) > (cursor_col_1, ...)
filterClause = fmt.Sprintf(" AND (%s) > (", b.pkColumnNamesSQL)
for i := range b.pkColumns {
if i > 0 {
filterClause += ", "
}
// We start from 2 if we don't have an endPK clause, but add len(b.endPK)
// if there is.
filterClause += fmt.Sprintf("$%d", 2+len(endPK)+i)
}
filterClause += ")"
} else if len(startPK) > 0 {
// For the the first query, we want (col1, ...) >= (cursor_col_1, ...)
filterClause = fmt.Sprintf(" AND (%s) >= (", makeColumnNamesSQL(b.pkColumns[:len(startPK)]))
for i := range startPK {
if i > 0 {
filterClause += ", "
}
// We start from 2 if we don't have an endPK clause, but add len(b.endPK)
// if there is.
filterClause += fmt.Sprintf("$%d", 2+len(endPK)+i)
}
filterClause += ")"
}
return fmt.Sprintf(
`SELECT %[1]s FROM [%[2]d AS tbl_name]
AS OF SYSTEM TIME %[3]s
WHERE %[4]s <= $1%[5]s%[6]s
ORDER BY %[1]s
LIMIT %[7]d`,
b.pkColumnNamesSQL,
b.tableID,
tree.MustMakeDTimestampTZ(b.aost, time.Microsecond),
b.ttlExpr,
filterClause,
endFilterClause,
b.selectBatchSize,
)
}
func (b *selectQueryBuilder) nextQuery() (string, []interface{}) {
if b.isFirst {
q := b.buildQuery()
b.isFirst = false
return q, b.cachedArgs
}
// All subsequent query strings are the same.
// Populate the cache once, and then maintain it for all subsequent calls.
if b.cachedQuery == "" {
b.cachedQuery = b.buildQuery()
}
return b.cachedQuery, b.cachedArgs
}
func (b *selectQueryBuilder) run(
ctx context.Context, ie sqlutil.InternalExecutor,
) ([]tree.Datums, error) {
q, args := b.nextQuery()
// Use a nil txn so that the AOST clause is handled correctly. Currently,
// the internal executor will treat a passed-in txn as an explicit txn, so
// the AOST clause on the SELECT query would not be interpreted correctly.
qosLevel := sessiondatapb.TTLLow
ret, err := ie.QueryBufferedEx(
ctx,
b.selectOpName,
nil, /* txn */
sessiondata.InternalExecutorOverride{
User: username.RootUserName(),
QualityOfService: &qosLevel,
},
q,
args...,
)
if err != nil {
return nil, err
}
if err := b.moveCursor(ret); err != nil {
return nil, err
}
return ret, nil
}
func (b *selectQueryBuilder) moveCursor(rows []tree.Datums) error {
// Move the cursor forward.
if len(rows) > 0 {
lastRow := rows[len(rows)-1]
b.cachedArgs = b.cachedArgs[:1+len(b.rangeToProcess.endPK)]
if len(lastRow) != len(b.pkColumns) {
return errors.AssertionFailedf("expected %d columns for last row, got %d", len(b.pkColumns), len(lastRow))
}
for _, d := range lastRow {
b.cachedArgs = append(b.cachedArgs, d)
}
}
return nil
}
// deleteQueryBuilder is responsible for maintaining state around the
// SELECT portion of the TTL job.
type deleteQueryBuilder struct {
tableID descpb.ID
pkColumns []string
deleteBatchSize int64
deleteOpName string
ttlExpr catpb.Expression
// cachedQuery is the cached query, which stays the same as long as we are
// deleting up to deleteBatchSize elements.
cachedQuery string
// cachedArgs keeps a cache of args to use in the run query.
// The cache is of form [cutoff, flattened PKs...].
cachedArgs []interface{}
}
func makeDeleteQueryBuilder(
tableID descpb.ID,
cutoff time.Time,
pkColumns []string,
relationName string,
deleteBatchSize int64,
ttlExpr catpb.Expression,
) deleteQueryBuilder {
cachedArgs := make([]interface{}, 0, 1+int64(len(pkColumns))*deleteBatchSize)
cachedArgs = append(cachedArgs, cutoff)
return deleteQueryBuilder{
tableID: tableID,
pkColumns: pkColumns,
deleteBatchSize: deleteBatchSize,
deleteOpName: fmt.Sprintf("ttl delete %s", relationName),
ttlExpr: ttlExpr,
cachedArgs: cachedArgs,
}
}
func (b *deleteQueryBuilder) buildQuery(numRows int) string {
columnNamesSQL := makeColumnNamesSQL(b.pkColumns)
var placeholderStr string
for i := 0; i < numRows; i++ {
if i > 0 {
placeholderStr += ", "
}
placeholderStr += "("
for j := 0; j < len(b.pkColumns); j++ {
if j > 0 {
placeholderStr += ", "
}
placeholderStr += fmt.Sprintf("$%d", 2+i*len(b.pkColumns)+j)
}
placeholderStr += ")"
}
return fmt.Sprintf(
`DELETE FROM [%d AS tbl_name] WHERE %s <= $1 AND (%s) IN (%s)`,
b.tableID,
b.ttlExpr,
columnNamesSQL,
placeholderStr,
)
}
func (b *deleteQueryBuilder) buildQueryAndArgs(rows []tree.Datums) (string, []interface{}) {
var q string
if int64(len(rows)) == b.deleteBatchSize {
if b.cachedQuery == "" {
b.cachedQuery = b.buildQuery(len(rows))
}
q = b.cachedQuery
} else {
q = b.buildQuery(len(rows))
}
deleteArgs := b.cachedArgs[:1]
for _, row := range rows {
for _, col := range row {
deleteArgs = append(deleteArgs, col)
}
}
return q, deleteArgs
}
func (b *deleteQueryBuilder) run(
ctx context.Context, ie sqlutil.InternalExecutor, txn *kv.Txn, rows []tree.Datums,
) (int, error) {
q, deleteArgs := b.buildQueryAndArgs(rows)
qosLevel := sessiondatapb.TTLLow
return ie.ExecEx(
ctx,
b.deleteOpName,
txn,
sessiondata.InternalExecutorOverride{
User: username.RootUserName(),
QualityOfService: &qosLevel,
},
q,
deleteArgs...,
)
}
// makeColumnNamesSQL converts columns into an escape string
// for an order by clause, e.g.:
// {"a", "b"} => a, b
// {"escape-me", "b"} => "escape-me", b
func makeColumnNamesSQL(columns []string) string {
var b bytes.Buffer
for i, pkColumn := range columns {
if i > 0 {
b.WriteString(", ")
}
lexbase.EncodeRestrictedSQLIdent(&b, pkColumn, lexbase.EncNoFlags)
}
return b.String()
}