-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmulti_statement_rows.go
More file actions
451 lines (416 loc) · 15.6 KB
/
multi_statement_rows.go
File metadata and controls
451 lines (416 loc) · 15.6 KB
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
// Copyright 2025 Google LLC
//
// 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 spannerdriver
import (
"context"
"database/sql/driver"
"errors"
"io"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/googleapis/go-sql-spanner/parser"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var _ driver.RowsNextResultSet = &multiStatementRows{}
var errRowsClosed = status.Error(codes.FailedPrecondition, "rows already closed")
var errExpectedRowsNextResultSet = status.Error(codes.Internal, "expected RowsNextResultSet")
// multiStatementRows is an implementation of the driver.RowsNextResultSet interface that is used by database/sql
// for multi-statement SQL strings that return multiple results. This implementation is a wrapper on top of the
// existing driver.Rows implementation in the Spanner database/sql driver, and it is only used when the driver
// detects that a SQL string contains more than one statement.
type multiStatementRows struct {
ctx context.Context
closed bool
closeErr error
currentIndex int
results []driver.RowsNextResultSet
errorIndex int
err error
}
type statementKey struct {
index int
}
// queryMultiple executes a multi-statement SQL string and returns a multiStatementRows.
// The function tries to execute all statements on Spanner. It stops execution at the first error (if any).
// Results are returned for all successful statements. If any error occurs, then execution stops at that statement
// and the error is returned as the result of that statement.
//
// The above means that this function returns an error if the first statement fails. It returns a nil error if one
// of the later statements fail.
//
// The function uses similar transaction semantics as PostgreSQL for multi-statement SQL strings. That means:
// 1. The statements are wrapped in an implicit transaction block. The implicit transaction is committed
// if all statements executed successfully, and rolled back if any of the statements fail.
// 2. If the connection already has a transaction, then that transaction is used instead.
// 3. If the SQL string contains a BEGIN statement, then the transaction is changed from implicit to explicit.
// An explicit transaction does not automatically commit or roll back at the end of the block.
// 4. If the SQL string contains a COMMIT statement, then the current transaction is committed. The next statement
// starts a new implicit transaction block.
// 5. DDL statements commit the current implicit transaction, as DDL is not supported in transactions. A new
// implicit transaction block is started after the DDL statement(s). This is different from how PostgreSQL
// behaves, as PostgreSQL supports DDL in transactions.
func queryMultiple(ctx context.Context, conn *conn, statements []string, args []driver.NamedValue) (res *multiStatementRows, returnedErr error) {
var currentTransaction *delegatingTransaction
defer func() {
if currentTransaction != nil && currentTransaction.implicit {
if returnedErr != nil || res.err != nil {
_ = currentTransaction.Rollback()
} else {
returnedErr = currentTransaction.Commit()
}
}
}()
result := &multiStatementRows{
ctx: ctx,
errorIndex: -1,
results: make([]driver.RowsNextResultSet, 0, len(statements)),
}
var err error
statementInfo := determineStatementTypes(conn, statements)
for index := 0; index < len(statements); {
statement := statements[index]
// Start an implicit transaction block if the connection currently does not have a transaction and needs one,
// or commit the current implicit transaction if the next statement is a DDL statement.
currentTransaction, err = maybeStartOrCommitImplicitTransaction(ctx, conn, index, statements, statementInfo)
if err != nil {
return nil, err
}
// Create a derived context for each query to prevent the main context from being linked to each query.
// The underlying Spanner RowIterator cancels the context that it is given when it is closed. The RowIterator
// is automatically closed when all data has been read, meaning that if we were to use the same context for
// each query, the context will be cancelled once all the data of the first result set have been read. That
// again can cause reading data from the following result sets to fail.
queryCtx := context.WithValue(ctx, statementKey{index: index}, statement)
// TODO: Implement a look-ahead to see if the following statements are all queries, and whether we can execute
// those in parallel (should be an opt-in).
numExecuted := 0
if canUseDdlBatch(index, statementInfo) {
numExecuted, err = queryDdlBatch(queryCtx, conn, index, result, statements, statementInfo, args)
} else if canUseDmlBatch(index, statementInfo) {
numExecuted, err = queryDmlBatch(queryCtx, conn, index, result, statements, statementInfo, args)
} else {
numExecuted, err = querySingle(queryCtx, conn, index, result, statement, args)
}
index += numExecuted
if err != nil {
if index == 0 {
return nil, err
}
return result, nil
}
}
return result, nil
}
// Execute a statement from a multi-statement SQL string as a single statement.
// The result is added to the multiStatementRows instance that is passed in to this function.
func querySingle(ctx context.Context, conn *conn, index int, result *multiStatementRows, statement string, args []driver.NamedValue) (int, error) {
r, err := conn.querySingle(ctx, statement /* isPartOfMultiStatementString = */, true, args)
if err != nil {
result.errorIndex = index
result.err = err
return 0, err
}
if rn, ok := r.(driver.RowsNextResultSet); ok {
result.results = append(result.results, rn)
} else {
// This should not happen.
return 0, registerUnexpectedResultSetError(r, index, result)
}
return 1, nil
}
// Execute a sequence of DDL statements from a multi-statement SQL string as a DDL batch.
func queryDdlBatch(ctx context.Context, conn *conn, index int, result *multiStatementRows, statements []string, statementInfo []*parser.StatementInfo, args []driver.NamedValue) (int, error) {
startIndex := index
endIndex := len(statements)
for i := startIndex + 1; i < len(statementInfo); i++ {
if statementInfo[i].StatementType != parser.StatementTypeDdl {
endIndex = i
break
}
}
if err := conn.StartBatchDDL(); err != nil {
return 0, registerError(index, result, err)
}
for i := startIndex; i < endIndex; i++ {
if _, err := querySingle(ctx, conn, i, result, statements[i], args); err != nil {
_ = conn.AbortBatch()
return index, err
}
}
_, err := conn.runBatch(ctx)
if err != nil {
var be *BatchError
if errors.As(err, &be) {
return len(be.BatchUpdateCounts), registerBatchError(index, result, be)
}
return 0, registerError(index, result, err)
}
return endIndex - startIndex, nil
}
// Execute a sequence of DML statements from a multi-statement SQL string as a DML batch.
func queryDmlBatch(ctx context.Context, conn *conn, index int, result *multiStatementRows, statements []string, statementInfo []*parser.StatementInfo, args []driver.NamedValue) (int, error) {
startIndex := index
endIndex := len(statements)
for i := startIndex + 1; i < len(statementInfo); i++ {
if statementInfo[i].StatementType != parser.StatementTypeDml {
endIndex = i
break
}
}
if err := conn.StartBatchDML(); err != nil {
return 0, registerError(index, result, err)
}
execOpts := conn.tempExecOptions
if execOpts == nil {
execOpts = &ExecOptions{}
}
for i := startIndex; i < endIndex; i++ {
if res, err := conn.execSingle(ctx, statements[i], args); err != nil {
_ = conn.AbortBatch()
return startIndex, err
} else {
r := createDriverResultRows(res /* isPartitionedDml = */, false, func() {}, execOpts)
result.results = append(result.results, r)
}
}
res, err := conn.RunDmlBatch(ctx)
if err != nil {
var be *BatchError
if errors.As(err, &be) {
return len(be.BatchUpdateCounts), registerBatchError(index, result, be)
}
return 0, registerError(index, result, err)
}
modified, err := res.BatchRowsAffected()
if err != nil {
return 0, registerError(index, result, err)
}
for i, m := range modified {
if noRows, ok := result.results[startIndex+i].(*emptyRows); ok {
noRows.stats = &sppb.ResultSetStats{RowCount: &sppb.ResultSetStats_RowCountExact{RowCountExact: m}}
}
}
return endIndex - startIndex, nil
}
func registerError(index int, result *multiStatementRows, err error) error {
result.errorIndex = index
result.err = err
return err
}
func registerBatchError(index int, result *multiStatementRows, err *BatchError) error {
result.errorIndex = index + len(err.BatchUpdateCounts)
result.err = err
return err
}
func registerUnexpectedResultSetError(r driver.Rows, index int, result *multiStatementRows) error {
_ = r.Close()
result.errorIndex = index
result.err = errExpectedRowsNextResultSet
return result.err
}
func maybeStartOrCommitImplicitTransaction(ctx context.Context, conn *conn, index int, statements []string, statementInfo []*parser.StatementInfo) (*delegatingTransaction, error) {
if shouldStartImplicitTransaction(conn, index, statements, statementInfo) && !conn.inTransaction() {
var tx driver.Tx
var err error
if canUseReadOnlyTransaction(conn, index, statements, statementInfo) {
tx, err = conn.BeginReadOnlyTransaction(ctx, &ReadOnlyTransactionOptions{
baseTransactionOptions: baseTransactionOptions{implicit: true},
}, func() {})
} else {
tx, err = conn.BeginReadWriteTransaction(ctx, &ReadWriteTransactionOptions{
baseTransactionOptions: baseTransactionOptions{implicit: true},
}, func() {})
}
if err != nil {
return nil, err
}
if delegatingTx, ok := tx.(*delegatingTransaction); !ok {
// This should never happen.
return nil, status.Error(codes.Internal, "expected a Spanner transaction")
} else {
return delegatingTx, nil
}
} else if conn.inImplicitTransaction() && statementInfo[index].StatementType == parser.StatementTypeDdl {
if _, err := conn.Commit(ctx); err != nil {
return nil, err
}
}
return conn.tx, nil
}
func shouldStartImplicitTransaction(conn *conn, index int, statements []string, statementInfo []*parser.StatementInfo) bool {
// Do not start a transaction if this is the last statement.
if index == len(statements)-1 {
return false
}
info := statementInfo[index]
statement := statements[index]
if info.StatementType == parser.StatementTypeQuery || info.StatementType == parser.StatementTypeDml || info.StatementType == parser.StatementTypeUnknown {
// Do not start a transaction if the statement that follows directly is a DDL statement, and that just means
// that the transaction will be committed right after anyway.
if statementInfo[index+1].StatementType == parser.StatementTypeDdl {
return false
}
return true
}
if info.StatementType == parser.StatementTypeDdl {
return false
}
if info.StatementType == parser.StatementTypeClientSide {
// It's a client-side statement. Only start an implicit transaction if the client-side statement is not a
// transaction control statement.
cs, err := conn.parser.ParseClientSideStatement(statement)
if err != nil {
return true
}
if _, ok := cs.(*parser.ParsedBeginStatement); ok {
return false
}
if _, ok := cs.(*parser.ParsedCommitStatement); ok {
return false
}
if _, ok := cs.(*parser.ParsedRollbackStatement); ok {
return false
}
}
// Start an implicit transaction by default.
return true
}
func determineStatementTypes(conn *conn, statements []string) []*parser.StatementInfo {
p := conn.parser
res := make([]*parser.StatementInfo, len(statements))
for i, statement := range statements {
res[i] = p.DetectStatementType(statement)
}
return res
}
func canUseReadOnlyTransaction(conn *conn, index int, statements []string, statementInfo []*parser.StatementInfo) bool {
if statementInfo[index].StatementType != parser.StatementTypeQuery {
return false
}
insideExplicitTx := false
for i := index + 1; i < len(statementInfo); i++ {
if isCommitOrRollback(conn, statements[i], statementInfo[i]) {
return true
}
if isBegin(conn, statements[i], statementInfo[i]) {
insideExplicitTx = true
}
if statementInfo[i].StatementType == parser.StatementTypeDml || statementInfo[i].StatementType == parser.StatementTypeUnknown {
return false
}
}
// We cannot use a read-only transaction if we are inside an explicit transaction block, as the transaction will
// still be active when all the statements have been executed.
return !insideExplicitTx
}
func canUseDdlBatch(index int, statementInfo []*parser.StatementInfo) bool {
if index == len(statementInfo)-1 || statementInfo[index].StatementType != parser.StatementTypeDdl {
return false
}
return statementInfo[index+1].StatementType == parser.StatementTypeDdl
}
func canUseDmlBatch(index int, statementInfo []*parser.StatementInfo) bool {
if index == len(statementInfo)-1 || statementInfo[index].StatementType != parser.StatementTypeDml || statementInfo[index].HasThenReturn {
return false
}
return statementInfo[index+1].StatementType == parser.StatementTypeDml && !statementInfo[index+1].HasThenReturn
}
func isBegin(conn *conn, query string, statementInfo *parser.StatementInfo) bool {
if statementInfo.StatementType == parser.StatementTypeClientSide {
cs, err := conn.parser.ParseClientSideStatement(query)
if err != nil {
return false
}
if _, ok := cs.(*parser.ParsedBeginStatement); ok {
return true
}
}
return false
}
func isCommitOrRollback(conn *conn, query string, statementInfo *parser.StatementInfo) bool {
if statementInfo.StatementType == parser.StatementTypeDdl {
return true
}
if statementInfo.StatementType == parser.StatementTypeClientSide {
cs, err := conn.parser.ParseClientSideStatement(query)
if err != nil {
return false
}
if _, ok := cs.(*parser.ParsedCommitStatement); ok {
return true
}
if _, ok := cs.(*parser.ParsedRollbackStatement); ok {
return true
}
}
return false
}
func (m *multiStatementRows) HasNextResultSet() bool {
if m.closed {
return false
}
if m.currentHasNextResultSet() {
return true
}
if m.errorIndex > -1 {
return m.currentIndex < m.errorIndex
}
return m.currentIndex < len(m.results)-1
}
func (m *multiStatementRows) currentHasNextResultSet() bool {
return m.currentIndex < len(m.results) && m.results[m.currentIndex].HasNextResultSet()
}
func (m *multiStatementRows) NextResultSet() error {
if m.closed {
return errRowsClosed
}
if !m.HasNextResultSet() {
return io.EOF
}
if m.currentHasNextResultSet() {
return m.results[m.currentIndex].NextResultSet()
}
if err := m.results[m.currentIndex].Close(); err != nil {
return err
}
m.currentIndex++
if m.currentIndex == m.errorIndex {
return m.err
}
return nil
}
func (m *multiStatementRows) Columns() []string {
if m.currentIndex == m.errorIndex {
return nil
}
return m.results[m.currentIndex].Columns()
}
func (m *multiStatementRows) Close() error {
if m.closed {
return m.closeErr
}
m.closed = true
for _, result := range m.results {
if err := result.Close(); err != nil && m.closeErr == nil {
m.closeErr = err
}
}
return m.closeErr
}
func (m *multiStatementRows) Next(dest []driver.Value) error {
if m.currentIndex == m.errorIndex {
return m.err
}
return m.results[m.currentIndex].Next(dest)
}