-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstatement.go
1192 lines (1016 loc) · 34.6 KB
/
statement.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2020 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
//
// 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.
//
package main
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"cloud.google.com/go/spanner"
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
pb "cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
)
// Partitioned DML tends to take long time to be finished.
// See: https://github.com/cloudspannerecosystem/spanner-cli/issues/102
const pdmlTimeout = time.Hour * 24
type Statement interface {
Execute(ctx context.Context, session *Session) (*Result, error)
}
// rowCountType is type of modified rows count by DML.
type rowCountType int
const (
// rowCountTypeExact is exact count type for DML result.
rowCountTypeExact rowCountType = iota
// rowCountTypeLowerBound is lower bound type for Partitioned DML result.
rowCountTypeLowerBound
)
type Result struct {
ColumnNames []string
Rows []Row
Predicates []string
AffectedRows int
AffectedRowsType rowCountType
Stats QueryStats
IsMutation bool
Timestamp time.Time
ForceVerbose bool
CommitStats *pb.CommitResponse_CommitStats
// ColumnTypes will be printed in `--verbose` mode if it is not empty
ColumnTypes []*pb.StructType_Field
}
type Row struct {
Columns []string
}
// QueryStats contains query statistics.
// Some fields may not have a valid value depending on the environment.
// For example, only ElapsedTime and RowsReturned has valid value for Cloud Spanner Emulator.
type QueryStats struct {
ElapsedTime string
CPUTime string
RowsReturned string
RowsScanned string
DeletedRowsScanned string
OptimizerVersion string
OptimizerStatisticsPackage string
}
var (
// Query
selectRe = regexp.MustCompile(`(?is)^(@{[^}]+}\s*)?(\(\s*)?(?:WITH|SELECT)\s.+$`)
graphRe = regexp.MustCompile(`(?is)^GRAPH\s.+$`)
callRe = regexp.MustCompile(`(?is)^CALL\s.+$`)
// DDL
createDatabaseRe = regexp.MustCompile(`(?is)^CREATE\s+DATABASE\s.+$`)
dropDatabaseRe = regexp.MustCompile(`(?is)^DROP\s+DATABASE\s+(.+)$`)
createRe = regexp.MustCompile(`(?is)^CREATE\s.+$`)
dropRe = regexp.MustCompile(`(?is)^DROP\s.+$`)
grantRe = regexp.MustCompile(`(?is)^GRANT\s.+$`)
revokeRe = regexp.MustCompile(`(?is)^REVOKE\s.+$`)
alterRe = regexp.MustCompile(`(?is)^ALTER\s.+$`)
renameRe = regexp.MustCompile(`(?is)^RENAME\s.+$`)
truncateTableRe = regexp.MustCompile(`(?is)^TRUNCATE\s+TABLE\s+(.+)$`)
analyzeRe = regexp.MustCompile(`(?is)^ANALYZE$`)
// DML
dmlRe = regexp.MustCompile(`(?is)^(INSERT|UPDATE|DELETE)\s+.+$`)
// Partitioned DML
// In fact, INSERT is not supported in a Partitioned DML, but accept it for showing better error message.
// https://cloud.google.com/spanner/docs/dml-partitioned#features_that_arent_supported
pdmlRe = regexp.MustCompile(`(?is)^PARTITIONED\s+((?:INSERT|UPDATE|DELETE)\s+.+$)`)
// Transaction
beginRwRe = regexp.MustCompile(`(?is)^BEGIN(?:\s+RW)?(?:\s+PRIORITY\s+(HIGH|MEDIUM|LOW))?(?:\s+TAG\s+(.+))?$`)
beginRoRe = regexp.MustCompile(`(?is)^BEGIN\s+RO(?:\s+([^\s]+))?(?:\s+PRIORITY\s+(HIGH|MEDIUM|LOW))?(?:\s+TAG\s+(.+))?$`)
commitRe = regexp.MustCompile(`(?is)^COMMIT$`)
rollbackRe = regexp.MustCompile(`(?is)^ROLLBACK$`)
closeRe = regexp.MustCompile(`(?is)^CLOSE$`)
// Other
exitRe = regexp.MustCompile(`(?is)^EXIT$`)
useRe = regexp.MustCompile(`(?is)^USE\s+([^\s]+)(?:\s+ROLE\s+(.+))?$`)
showDatabasesRe = regexp.MustCompile(`(?is)^SHOW\s+DATABASES$`)
showCreateTableRe = regexp.MustCompile(`(?is)^SHOW\s+CREATE\s+TABLE\s+(.+)$`)
showTablesRe = regexp.MustCompile(`(?is)^SHOW\s+TABLES(?:\s+(.+))?$`)
showColumnsRe = regexp.MustCompile(`(?is)^(?:SHOW\s+COLUMNS\s+FROM)\s+(.+)$`)
showIndexRe = regexp.MustCompile(`(?is)^SHOW\s+(?:INDEX|INDEXES|KEYS)\s+FROM\s+(.+)$`)
explainRe = regexp.MustCompile(`(?is)^EXPLAIN\s+(ANALYZE\s+)?(.+)$`)
describeRe = regexp.MustCompile(`(?is)^DESCRIBE\s+(.+)$`)
)
var (
explainColumnNames = []string{"ID", "Query_Execution_Plan"}
explainAnalyzeColumnNames = []string{"ID", "Query_Execution_Plan", "Rows_Returned", "Executions", "Total_Latency"}
describeColumnNames = []string{"Column_Name", "Column_Type"}
)
func BuildStatement(input string) (Statement, error) {
return BuildStatementWithComments(input, input)
}
func BuildStatementWithComments(stripped, raw string) (Statement, error) {
switch {
case exitRe.MatchString(stripped):
return &ExitStatement{}, nil
case useRe.MatchString(stripped):
matched := useRe.FindStringSubmatch(stripped)
return &UseStatement{Database: unquoteIdentifier(matched[1]), Role: unquoteIdentifier(matched[2])}, nil
case selectRe.MatchString(stripped), graphRe.MatchString(stripped), callRe.MatchString(stripped):
return &SelectStatement{Query: raw}, nil
case createDatabaseRe.MatchString(stripped):
return &CreateDatabaseStatement{CreateStatement: stripped}, nil
case createRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case dropDatabaseRe.MatchString(stripped):
matched := dropDatabaseRe.FindStringSubmatch(stripped)
return &DropDatabaseStatement{DatabaseId: unquoteIdentifier(matched[1])}, nil
case dropRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case alterRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case renameRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case grantRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case revokeRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case truncateTableRe.MatchString(stripped):
matched := truncateTableRe.FindStringSubmatch(stripped)
return &TruncateTableStatement{Table: unquoteIdentifier(matched[1])}, nil
case analyzeRe.MatchString(stripped):
return &DdlStatement{Ddl: stripped}, nil
case showDatabasesRe.MatchString(stripped):
return &ShowDatabasesStatement{}, nil
case showCreateTableRe.MatchString(stripped):
matched := showCreateTableRe.FindStringSubmatch(stripped)
schema, table := extractSchemaAndTable(unquoteIdentifier(matched[1]))
return &ShowCreateTableStatement{Schema: schema, Table: table}, nil
case showTablesRe.MatchString(stripped):
matched := showTablesRe.FindStringSubmatch(stripped)
return &ShowTablesStatement{Schema: unquoteIdentifier(matched[1])}, nil
case describeRe.MatchString(stripped):
matched := describeRe.FindStringSubmatch(stripped)
isDML := dmlRe.MatchString(matched[1])
switch {
case isDML:
return &DescribeStatement{Statement: matched[1], IsDML: true}, nil
default:
return &DescribeStatement{Statement: matched[1]}, nil
}
case explainRe.MatchString(stripped):
matched := explainRe.FindStringSubmatch(stripped)
isAnalyze := matched[1] != ""
isDML := dmlRe.MatchString(matched[2])
switch {
case isAnalyze && isDML:
return &ExplainAnalyzeDmlStatement{Dml: matched[2]}, nil
case isAnalyze:
return &ExplainAnalyzeStatement{Query: matched[2]}, nil
default:
return &ExplainStatement{Explain: matched[2], IsDML: isDML}, nil
}
case showColumnsRe.MatchString(stripped):
matched := showColumnsRe.FindStringSubmatch(stripped)
schema, table := extractSchemaAndTable(unquoteIdentifier(matched[1]))
return &ShowColumnsStatement{Schema: schema, Table: table}, nil
case showIndexRe.MatchString(stripped):
matched := showIndexRe.FindStringSubmatch(stripped)
schema, table := extractSchemaAndTable(unquoteIdentifier(matched[1]))
return &ShowIndexStatement{Schema: schema, Table: table}, nil
case dmlRe.MatchString(stripped):
return &DmlStatement{Dml: raw}, nil
case pdmlRe.MatchString(stripped):
matched := pdmlRe.FindStringSubmatch(stripped)
return &PartitionedDmlStatement{Dml: matched[1]}, nil
case beginRwRe.MatchString(stripped):
return newBeginRwStatement(stripped)
case beginRoRe.MatchString(stripped):
return newBeginRoStatement(stripped)
case commitRe.MatchString(stripped):
return &CommitStatement{}, nil
case rollbackRe.MatchString(stripped):
return &RollbackStatement{}, nil
case closeRe.MatchString(stripped):
return &CloseStatement{}, nil
}
return nil, errors.New("invalid statement")
}
func unquoteIdentifier(input string) string {
return strings.Trim(strings.TrimSpace(input), "`")
}
type SelectStatement struct {
Query string
}
func (s *SelectStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
stmt := spanner.NewStatement(s.Query)
iter, roTxn := session.RunQueryWithStats(ctx, stmt)
defer iter.Stop()
rows, columnNames, err := parseQueryResult(iter)
if err != nil {
if session.InReadWriteTransaction() && spanner.ErrCode(err) == codes.Aborted {
// Need to call rollback to free the acquired session in underlying google-cloud-go/spanner.
rollback := &RollbackStatement{}
rollback.Execute(ctx, session)
}
return nil, err
}
result := &Result{
ColumnNames: columnNames,
Rows: rows,
}
result.ColumnTypes = iter.Metadata.GetRowType().GetFields()
queryStats := parseQueryStats(iter.QueryStats)
rowsReturned, err := strconv.Atoi(queryStats.RowsReturned)
if err != nil {
return nil, fmt.Errorf("rowsReturned is invalid: %v", err)
}
result.AffectedRows = rowsReturned
result.Stats = queryStats
// ReadOnlyTransaction.Timestamp() is invalid until read.
if roTxn != nil {
result.Timestamp, _ = roTxn.Timestamp()
}
return result, nil
}
// extractColumnNames extract column names from ResultSetMetadata.RowType.Fields.
func extractColumnNames(fields []*pb.StructType_Field) []string {
var names []string
for _, field := range fields {
names = append(names, field.GetName())
}
return names
}
// parseQueryResult parses rows and columnNames from spanner.RowIterator.
// A caller is responsible for calling iterator.Stop().
func parseQueryResult(iter *spanner.RowIterator) ([]Row, []string, error) {
var rows []Row
for {
row, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, nil, err
}
columns, err := DecodeRow(row)
if err != nil {
return nil, nil, err
}
rows = append(rows, Row{
Columns: columns,
})
}
return rows, extractColumnNames(iter.Metadata.GetRowType().GetFields()), nil
}
// parseQueryStats parses spanner.RowIterator.QueryStats.
func parseQueryStats(stats map[string]interface{}) QueryStats {
var queryStats QueryStats
if v, ok := stats["elapsed_time"]; ok {
if elapsed, ok := v.(string); ok {
queryStats.ElapsedTime = elapsed
}
}
if v, ok := stats["rows_returned"]; ok {
if returned, ok := v.(string); ok {
queryStats.RowsReturned = returned
}
}
if v, ok := stats["rows_scanned"]; ok {
if scanned, ok := v.(string); ok {
queryStats.RowsScanned = scanned
}
}
if v, ok := stats["deleted_rows_scanned"]; ok {
if deletedRowsScanned, ok := v.(string); ok {
queryStats.DeletedRowsScanned = deletedRowsScanned
}
}
if v, ok := stats["cpu_time"]; ok {
if cpu, ok := v.(string); ok {
queryStats.CPUTime = cpu
}
}
if v, ok := stats["optimizer_version"]; ok {
if version, ok := v.(string); ok {
queryStats.OptimizerVersion = version
}
}
if v, ok := stats["optimizer_statistics_package"]; ok {
if pkg, ok := v.(string); ok {
queryStats.OptimizerStatisticsPackage = pkg
}
}
return queryStats
}
type CreateDatabaseStatement struct {
CreateStatement string
}
func (s *CreateDatabaseStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
op, err := session.adminClient.CreateDatabase(ctx, &adminpb.CreateDatabaseRequest{
Parent: session.InstancePath(),
CreateStatement: s.CreateStatement,
})
if err != nil {
return nil, err
}
if _, err := op.Wait(ctx); err != nil {
return nil, err
}
return &Result{IsMutation: true}, nil
}
type DropDatabaseStatement struct {
DatabaseId string
}
func (s *DropDatabaseStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
if err := session.adminClient.DropDatabase(ctx, &adminpb.DropDatabaseRequest{
Database: fmt.Sprintf("projects/%s/instances/%s/databases/%s", session.projectId, session.instanceId, s.DatabaseId),
}); err != nil {
return nil, err
}
return &Result{IsMutation: true}, nil
}
type DdlStatement struct {
Ddl string
}
func (s *DdlStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
return executeDdlStatements(ctx, session, []string{s.Ddl})
}
type BulkDdlStatement struct {
Ddls []string
}
func (s *BulkDdlStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
return executeDdlStatements(ctx, session, s.Ddls)
}
func executeDdlStatements(ctx context.Context, session *Session, ddls []string) (*Result, error) {
op, err := session.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: session.DatabasePath(),
Statements: ddls,
// There is no problem to send ProtoDescriptors with any DDL statements
ProtoDescriptors: session.protoDescriptor,
})
if err != nil {
return nil, err
}
if err := op.Wait(ctx); err != nil {
return nil, err
}
return &Result{IsMutation: true}, nil
}
type ShowDatabasesStatement struct {
}
func (s *ShowDatabasesStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
result := &Result{ColumnNames: []string{"Database"}}
dbIter := session.adminClient.ListDatabases(ctx, &adminpb.ListDatabasesRequest{
Parent: session.InstancePath(),
})
for {
database, err := dbIter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
re := regexp.MustCompile(`projects/[^/]+/instances/[^/]+/databases/(.+)`)
matched := re.FindStringSubmatch(database.GetName())
dbname := matched[1]
resultRow := Row{
Columns: []string{dbname},
}
result.Rows = append(result.Rows, resultRow)
}
result.AffectedRows = len(result.Rows)
return result, nil
}
type ShowCreateTableStatement struct {
Schema string
Table string
}
func (s *ShowCreateTableStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
result := &Result{ColumnNames: []string{"Table", "Create Table"}}
ddlResponse, err := session.adminClient.GetDatabaseDdl(ctx, &adminpb.GetDatabaseDdlRequest{
Database: session.DatabasePath(),
})
if err != nil {
return nil, err
}
for _, stmt := range ddlResponse.Statements {
if isCreateTableDDL(stmt, s.Schema, s.Table) {
var fqn string
if s.Schema == "" {
fqn = s.Table
} else {
fqn = fmt.Sprintf("%s.%s", s.Schema, s.Table)
}
resultRow := Row{
Columns: []string{fqn, stmt},
}
result.Rows = append(result.Rows, resultRow)
break
}
}
if len(result.Rows) == 0 {
return nil, fmt.Errorf("table %q doesn't exist in schema %q", s.Table, s.Schema)
}
result.AffectedRows = len(result.Rows)
return result, nil
}
func isCreateTableDDL(ddl string, schema string, table string) bool {
table = regexp.QuoteMeta(table)
var re string
if schema == "" {
re = fmt.Sprintf("(?i)^CREATE TABLE (%s|`%s`)\\s*\\(", table, table)
} else {
re = fmt.Sprintf("(?i)^CREATE TABLE (%s|`%s`)\\.(%s|`%s`)\\s*\\(", schema, schema, table, table)
}
return regexp.MustCompile(re).MatchString(ddl)
}
type ShowTablesStatement struct {
Schema string
}
func (s *ShowTablesStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
if session.InReadWriteTransaction() {
// INFORMATION_SCHEMA can not be used in read-write transaction.
// https://cloud.google.com/spanner/docs/information-schema
return nil, errors.New(`"SHOW TABLES" can not be used in a read-write transaction`)
}
alias := fmt.Sprintf("Tables_in_%s", session.databaseId)
stmt := spanner.NewStatement(fmt.Sprintf("SELECT t.TABLE_NAME AS `%s` FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_CATALOG = '' and t.TABLE_SCHEMA = @schema", alias))
stmt.Params["schema"] = s.Schema
iter, _ := session.RunQuery(ctx, stmt)
defer iter.Stop()
rows, columnNames, err := parseQueryResult(iter)
if err != nil {
return nil, err
}
return &Result{
ColumnNames: columnNames,
Rows: rows,
AffectedRows: len(rows),
}, nil
}
type ExplainStatement struct {
Explain string
IsDML bool
}
// Execute processes `EXPLAIN` statement for queries and DMLs.
func (s *ExplainStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
queryPlan, timestamp, _, err := runAnalyzeQuery(ctx, session, spanner.NewStatement(s.Explain), s.IsDML)
if err != nil {
return nil, err
}
if queryPlan == nil {
return nil, errors.New("EXPLAIN statement is not supported for Cloud Spanner Emulator.")
}
rows, predicates, err := processPlanWithoutStats(queryPlan)
if err != nil {
return nil, err
}
result := &Result{
ColumnNames: explainColumnNames,
AffectedRows: len(rows),
Rows: rows,
Timestamp: timestamp,
Predicates: predicates,
}
return result, nil
}
type DescribeStatement struct {
Statement string
IsDML bool
}
// Execute processes `DESCRIBE` statement for queries and DMLs.
func (s *DescribeStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
_, timestamp, metadata, err := runAnalyzeQuery(ctx, session, spanner.NewStatement(s.Statement), s.IsDML)
if err != nil {
return nil, err
}
var rows []Row
for _, field := range metadata.GetRowType().GetFields() {
rows = append(rows, Row{Columns: []string{field.GetName(), formatTypeVerbose(field.GetType())}})
}
result := &Result{
AffectedRows: len(rows),
ColumnNames: describeColumnNames,
Timestamp: timestamp,
Rows: rows,
}
return result, nil
}
func runAnalyzeQuery(ctx context.Context, session *Session, stmt spanner.Statement, isDML bool) (queryPlan *pb.QueryPlan, commitTimestamp time.Time, metadata *pb.ResultSetMetadata, err error) {
if !isDML {
queryPlan, metadata, err := session.RunAnalyzeQuery(ctx, stmt)
return queryPlan, time.Time{}, metadata, err
}
_, timestamp, queryPlan, metadata, err := runInNewOrExistRwTxForExplain(ctx, session, func() (int64, *pb.QueryPlan, *pb.ResultSetMetadata, error) {
plan, metadata, err := session.RunAnalyzeQuery(ctx, stmt)
return 0, plan, metadata, err
})
return queryPlan, timestamp, metadata, err
}
type ExplainAnalyzeStatement struct {
Query string
}
func (s *ExplainAnalyzeStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
stmt := spanner.NewStatement(s.Query)
iter, roTxn := session.RunQueryWithStats(ctx, stmt)
// consume iter
err := iter.Do(func(*spanner.Row) error {
return nil
})
if err != nil {
return nil, err
}
queryStats := parseQueryStats(iter.QueryStats)
rowsReturned, err := strconv.Atoi(queryStats.RowsReturned)
if err != nil {
return nil, fmt.Errorf("rowsReturned is invalid: %v", err)
}
// Cloud Spanner Emulator doesn't set query plan nodes to the result.
// See: https://github.com/GoogleCloudPlatform/cloud-spanner-emulator/blob/77188b228e7757cd56ecffb5bc3ee85dce5d6ae1/frontend/handlers/queries.cc#L224-L230
if iter.QueryPlan == nil {
return nil, errors.New("EXPLAIN ANALYZE statement is not supported for Cloud Spanner Emulator.")
}
rows, predicates, err := processPlanWithStats(iter.QueryPlan)
if err != nil {
return nil, err
}
// ReadOnlyTransaction.Timestamp() is invalid until read.
var timestamp time.Time
if roTxn != nil {
timestamp, _ = roTxn.Timestamp()
}
result := &Result{
ColumnNames: explainAnalyzeColumnNames,
ForceVerbose: true,
AffectedRows: rowsReturned,
Stats: queryStats,
Timestamp: timestamp,
Rows: rows,
Predicates: predicates,
}
return result, nil
}
func processPlanWithStats(plan *pb.QueryPlan) (rows []Row, predicates []string, err error) {
return processPlanImpl(plan, true)
}
func processPlanWithoutStats(plan *pb.QueryPlan) (rows []Row, predicates []string, err error) {
return processPlanImpl(plan, false)
}
func processPlanImpl(plan *pb.QueryPlan, withStats bool) (rows []Row, predicates []string, err error) {
planNodes := plan.GetPlanNodes()
maxWidthOfNodeID := len(fmt.Sprint(getMaxRelationalNodeID(plan)))
widthOfNodeIDWithIndicator := maxWidthOfNodeID + 1
tree := BuildQueryPlanTree(plan, 0)
treeRows, err := tree.RenderTreeWithStats(planNodes)
if err != nil {
return nil, nil, err
}
for _, row := range treeRows {
var formattedID string
if len(row.Predicates) > 0 {
formattedID = fmt.Sprintf("%*s", widthOfNodeIDWithIndicator, "*"+fmt.Sprint(row.ID))
} else {
formattedID = fmt.Sprintf("%*d", widthOfNodeIDWithIndicator, row.ID)
}
if withStats {
rows = append(rows, Row{[]string{formattedID, row.Text, row.RowsTotal, row.Execution, row.LatencyTotal}})
} else {
rows = append(rows, Row{[]string{formattedID, row.Text}})
}
for i, predicate := range row.Predicates {
var prefix string
if i == 0 {
prefix = fmt.Sprintf("%*d:", maxWidthOfNodeID, row.ID)
} else {
prefix = strings.Repeat(" ", maxWidthOfNodeID+1)
}
predicates = append(predicates, fmt.Sprintf("%s %s", prefix, predicate))
}
}
return rows, predicates, nil
}
type ShowColumnsStatement struct {
Schema string
Table string
}
func (s *ShowColumnsStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
if session.InReadWriteTransaction() {
// INFORMATION_SCHEMA can not be used in read-write transaction.
// https://cloud.google.com/spanner/docs/information-schema
return nil, errors.New(`"SHOW COLUMNS" can not be used in a read-write transaction`)
}
stmt := spanner.Statement{SQL: `SELECT
C.COLUMN_NAME as Field,
C.SPANNER_TYPE as Type,
C.IS_NULLABLE as ` + "`NULL`" + `,
I.INDEX_TYPE as Key,
IC.COLUMN_ORDERING as Key_Order,
CONCAT(CO.OPTION_NAME, "=", CO.OPTION_VALUE) as Options
FROM
INFORMATION_SCHEMA.COLUMNS C
LEFT JOIN
INFORMATION_SCHEMA.INDEX_COLUMNS IC USING(TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME)
LEFT JOIN
INFORMATION_SCHEMA.INDEXES I USING(TABLE_SCHEMA, TABLE_NAME, INDEX_NAME)
LEFT JOIN
INFORMATION_SCHEMA.COLUMN_OPTIONS CO USING(TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME)
WHERE
LOWER(C.TABLE_SCHEMA) = LOWER(@table_schema) AND LOWER(C.TABLE_NAME) = LOWER(@table_name)
ORDER BY
C.ORDINAL_POSITION ASC`,
Params: map[string]interface{}{"table_name": s.Table, "table_schema": s.Schema}}
iter, _ := session.RunQuery(ctx, stmt)
defer iter.Stop()
rows, columnNames, err := parseQueryResult(iter)
if err != nil {
return nil, err
}
if len(rows) == 0 {
return nil, fmt.Errorf("table %q doesn't exist in schema %q", s.Table, s.Schema)
}
return &Result{
ColumnNames: columnNames,
Rows: rows,
AffectedRows: len(rows),
}, nil
}
func extractSchemaAndTable(s string) (string, string) {
schema, table, found := strings.Cut(s, ".")
if !found {
return "", unquoteIdentifier(s)
}
return unquoteIdentifier(schema), unquoteIdentifier(table)
}
type ShowIndexStatement struct {
Schema string
Table string
}
func (s *ShowIndexStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
if session.InReadWriteTransaction() {
// INFORMATION_SCHEMA can not be used in read-write transaction.
// https://cloud.google.com/spanner/docs/information-schema
return nil, errors.New(`"SHOW INDEX" can not be used in a read-write transaction`)
}
stmt := spanner.Statement{
SQL: `SELECT
TABLE_NAME as Table,
PARENT_TABLE_NAME as Parent_table,
INDEX_NAME as Index_name,
INDEX_TYPE as Index_type,
IS_UNIQUE as Is_unique,
IS_NULL_FILTERED as Is_null_filtered,
INDEX_STATE as Index_state
FROM
INFORMATION_SCHEMA.INDEXES I
WHERE
LOWER(I.TABLE_SCHEMA) = @table_schema AND LOWER(TABLE_NAME) = LOWER(@table_name)`,
Params: map[string]interface{}{"table_name": s.Table, "table_schema": s.Schema}}
iter, _ := session.RunQuery(ctx, stmt)
defer iter.Stop()
rows, columnNames, err := parseQueryResult(iter)
if err != nil {
return nil, err
}
if len(rows) == 0 {
return nil, fmt.Errorf("table %q doesn't exist in schema %q", s.Table, s.Schema)
}
return &Result{
ColumnNames: columnNames,
Rows: rows,
AffectedRows: len(rows),
}, nil
}
type TruncateTableStatement struct {
Table string
}
func (s *TruncateTableStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
if session.InReadWriteTransaction() {
// PartitionedUpdate creates a new transaction and it could cause dead lock with the current running transaction.
return nil, errors.New(`"TRUNCATE TABLE" can not be used in a read-write transaction`)
}
if session.InReadOnlyTransaction() {
// Just for user-friendly.
return nil, errors.New(`"TRUNCATE TABLE" can not be used in a read-only transaction`)
}
stmt := spanner.NewStatement(fmt.Sprintf("DELETE FROM `%s` WHERE true", s.Table))
ctx, cancel := context.WithTimeout(ctx, pdmlTimeout)
defer cancel()
count, err := session.client.PartitionedUpdate(ctx, stmt)
if err != nil {
return nil, err
}
return &Result{
IsMutation: true,
AffectedRows: int(count),
}, nil
}
type DmlStatement struct {
Dml string
}
func (s *DmlStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
stmt := spanner.NewStatement(s.Dml)
result := &Result{IsMutation: true}
var rows []Row
var columnNames []string
var numRows int64
var metadata *pb.ResultSetMetadata
var err error
if session.InReadWriteTransaction() {
rows, columnNames, numRows, metadata, err = session.RunUpdate(ctx, stmt, false)
if err != nil {
// Need to call rollback to free the acquired session in underlying google-cloud-go/spanner.
rollback := &RollbackStatement{}
rollback.Execute(ctx, session)
return nil, fmt.Errorf("transaction was aborted: %v", err)
}
} else {
// Start implicit transaction.
begin := BeginRwStatement{}
if _, err = begin.Execute(ctx, session); err != nil {
return nil, err
}
rows, columnNames, numRows, metadata, err = session.RunUpdate(ctx, stmt, false)
if err != nil {
// once error has happened, escape from implicit transaction
rollback := &RollbackStatement{}
rollback.Execute(ctx, session)
return nil, err
}
commit := CommitStatement{}
txnResult, err := commit.Execute(ctx, session)
if err != nil {
return nil, err
}
result.Timestamp = txnResult.Timestamp
result.CommitStats = txnResult.CommitStats
}
result.ColumnTypes = metadata.GetRowType().GetFields()
result.Rows = rows
result.ColumnNames = columnNames
result.AffectedRows = int(numRows)
return result, nil
}
type PartitionedDmlStatement struct {
Dml string
}
func (s *PartitionedDmlStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
if session.InReadWriteTransaction() {
// PartitionedUpdate creates a new transaction and it could cause dead lock with the current running transaction.
return nil, errors.New(`Partitioned DML statement can not be run in a read-write transaction`)
}
if session.InReadOnlyTransaction() {
// Just for user-friendly.
return nil, errors.New(`Partitioned DML statement can not be run in a read-only transaction`)
}
stmt := spanner.NewStatement(s.Dml)
ctx, cancel := context.WithTimeout(ctx, pdmlTimeout)
defer cancel()
count, err := session.client.PartitionedUpdate(ctx, stmt)
if err != nil {
return nil, err
}
return &Result{
IsMutation: true,
AffectedRows: int(count),
AffectedRowsType: rowCountTypeLowerBound,
}, nil
}
type ExplainAnalyzeDmlStatement struct {
Dml string
}
func (s *ExplainAnalyzeDmlStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
stmt := spanner.NewStatement(s.Dml)
affectedRows, timestamp, queryPlan, _, err := runInNewOrExistRwTxForExplain(ctx, session, func() (int64, *pb.QueryPlan, *pb.ResultSetMetadata, error) {
iter, _ := session.RunQueryWithStats(ctx, stmt)
defer iter.Stop()
err := iter.Do(func(r *spanner.Row) error { return nil })
if err != nil {
return 0, nil, nil, err
}
return iter.RowCount, iter.QueryPlan, iter.Metadata, nil
})
if err != nil {
return nil, err
}
rows, predicates, err := processPlanWithStats(queryPlan)
if err != nil {
return nil, err
}
result := &Result{
IsMutation: true,
ColumnNames: explainAnalyzeColumnNames,
ForceVerbose: true,
AffectedRows: int(affectedRows),
Rows: rows,
Predicates: predicates,
Timestamp: timestamp,
}
return result, nil
}
// runInNewOrExistRwTxForExplain is a helper function for runAnalyzeQuery and ExplainAnalyzeDmlStatement.
// It execute a function in the current RW transaction or an implicit RW transaction.
func runInNewOrExistRwTxForExplain(ctx context.Context, session *Session, f func() (affected int64, plan *pb.QueryPlan, metadata *pb.ResultSetMetadata, err error)) (affected int64, ts time.Time, plan *pb.QueryPlan, metadata *pb.ResultSetMetadata, err error) {
if session.InReadWriteTransaction() {
affected, plan, metadata, err := f()
if err != nil {
// Need to call rollback to free the acquired session in underlying google-cloud-go/spanner.
rollback := &RollbackStatement{}
rollback.Execute(ctx, session)
return 0, time.Time{}, nil, nil, fmt.Errorf("transaction was aborted: %v", err)
}
return affected, time.Time{}, plan, metadata, nil
} else {
// Start implicit transaction.
begin := BeginRwStatement{}
if _, err := begin.Execute(ctx, session); err != nil {
return 0, time.Time{}, nil, nil, err
}
affected, plan, metadata, err := f()
if err != nil {
// once error has happened, escape from implicit transaction
rollback := &RollbackStatement{}
rollback.Execute(ctx, session)
return 0, time.Time{}, nil, nil, err
}
commit := CommitStatement{}
txnResult, err := commit.Execute(ctx, session)
if err != nil {
return 0, time.Time{}, nil, nil, err
}
return affected, txnResult.Timestamp, plan, metadata, nil
}
}
type BeginRwStatement struct {
Priority pb.RequestOptions_Priority
Tag string
}
func newBeginRwStatement(input string) (*BeginRwStatement, error) {
matched := beginRwRe.FindStringSubmatch(input)
stmt := &BeginRwStatement{}