-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatement_test.go
890 lines (875 loc) · 25.8 KB
/
statement_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
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
//
// 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 (
_ "embed"
"reflect"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/samber/lo"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
)
func TestBuildStatement(t *testing.T) {
timestamp, err := time.Parse(time.RFC3339Nano, "2020-03-30T22:54:44.834017+09:00")
if err != nil {
t.Fatalf("unexpected time parse error: %v", err)
}
// valid tests
for _, test := range []struct {
desc string
input string
want Statement
skipLowerCase bool
}{
{
desc: "SELECT statement",
input: "SELECT * FROM t1",
want: &SelectStatement{Query: "SELECT * FROM t1"},
},
{
desc: "SELECT statement in multiple lines",
input: "SELECT\n*\nFROM t1",
want: &SelectStatement{Query: "SELECT\n*\nFROM t1"},
},
{
desc: "SELECT statement with comment",
input: "SELECT 0x1/**/A",
want: &SelectStatement{Query: "SELECT 0x1/**/A"},
},
{
desc: "WITH statement",
input: "WITH sub AS (SELECT 1) SELECT * FROM sub",
want: &SelectStatement{Query: "WITH sub AS (SELECT 1) SELECT * FROM sub"},
},
{
// https://cloud.google.com/spanner/docs/query-syntax#statement-hints
desc: "SELECT statement with statement hint",
input: "@{USE_ADDITIONAL_PARALLELISM=TRUE} SELECT * FROM t1",
want: &SelectStatement{Query: "@{USE_ADDITIONAL_PARALLELISM=TRUE} SELECT * FROM t1"},
},
{
desc: "Parenthesized SELECT statement",
input: "(SELECT * FROM t1)",
want: &SelectStatement{Query: "(SELECT * FROM t1)"},
},
{
desc: "CREATE DATABASE statement",
input: "CREATE DATABASE d1",
want: &CreateDatabaseStatement{CreateStatement: "CREATE DATABASE d1"},
},
{
desc: "DROP DATABASE statement",
input: "DROP DATABASE d1",
want: &DropDatabaseStatement{DatabaseId: "d1"},
},
{
desc: "DROP DATABASE statement with escaped database name",
input: "DROP DATABASE `TABLE`",
want: &DropDatabaseStatement{DatabaseId: "TABLE"},
},
{
desc: "ALTER DATABASE statement",
input: "ALTER DATABASE d1 SET OPTIONS ( version_retention_period = '7d' )",
want: &DdlStatement{Ddl: `ALTER DATABASE d1 SET OPTIONS (version_retention_period = "7d")`},
},
{
desc: "CREATE TABLE statement",
input: "CREATE TABLE t1 (id INT64 NOT NULL) PRIMARY KEY (id)",
want: &DdlStatement{Ddl: "CREATE TABLE t1 (\n id INT64 NOT NULL\n) PRIMARY KEY (id)"},
},
{
desc: "RENAME TABLE statement",
input: "RENAME TABLE t1 TO t2, t3 TO t4",
want: &DdlStatement{Ddl: "RENAME TABLE t1 TO t2, t3 TO t4"},
},
{
desc: "ALTER TABLE statement",
input: "ALTER TABLE t1 ADD COLUMN name STRING(16) NOT NULL",
want: &DdlStatement{Ddl: "ALTER TABLE t1 ADD COLUMN name STRING(16) NOT NULL"},
},
{
desc: "DROP TABLE statement",
input: "DROP TABLE t1",
want: &DdlStatement{Ddl: "DROP TABLE t1"},
},
{
desc: "CREATE INDEX statement",
input: "CREATE INDEX idx_name ON t1 (name DESC)",
want: &DdlStatement{Ddl: "CREATE INDEX idx_name ON t1(name DESC)"},
},
{
desc: "DROP INDEX statement",
input: "DROP INDEX idx_name",
want: &DdlStatement{Ddl: "DROP INDEX idx_name"},
},
{
desc: "TRUNCATE TABLE statement",
input: "TRUNCATE TABLE t1",
want: &TruncateTableStatement{Table: "t1"},
},
{
desc: "CREATE VIEW statement",
input: "CREATE VIEW t1view SQL SECURITY INVOKER AS SELECT t1.Id FROM t1",
want: &DdlStatement{Ddl: "CREATE VIEW t1view SQL SECURITY INVOKER AS SELECT t1.Id FROM t1"},
},
{
desc: "CREATE OR REPLACE VIEW statement",
input: "CREATE OR REPLACE VIEW t1view SQL SECURITY INVOKER AS SELECT t1.Id FROM t1",
want: &DdlStatement{Ddl: "CREATE OR REPLACE VIEW t1view SQL SECURITY INVOKER AS SELECT t1.Id FROM t1"},
},
{
desc: "DROP VIEW statement",
input: "DROP VIEW t1view",
want: &DdlStatement{Ddl: "DROP VIEW t1view"},
},
{
desc: "CREATE CHANGE STREAM FOR ALL statement",
input: "CREATE CHANGE STREAM EverythingStream FOR ALL",
want: &DdlStatement{Ddl: "CREATE CHANGE STREAM EverythingStream FOR ALL"},
},
{
desc: "CREATE CHANGE STREAM FOR specific columns statement",
input: "CREATE CHANGE STREAM NamesAndTitles FOR Singers(FirstName, LastName), Albums(Title)",
want: &DdlStatement{Ddl: "CREATE CHANGE STREAM NamesAndTitles FOR Singers(FirstName, LastName), Albums(Title)"},
},
{
desc: "ALTER CHANGE STREAM SET FOR statement",
input: "ALTER CHANGE STREAM NamesAndAlbums SET FOR Singers(FirstName, LastName), Albums, Songs",
want: &DdlStatement{Ddl: "ALTER CHANGE STREAM NamesAndAlbums SET FOR Singers(FirstName, LastName), Albums, Songs"},
},
{
desc: "ALTER CHANGE STREAM SET OPTIONS statement",
input: "ALTER CHANGE STREAM NamesAndAlbums SET OPTIONS( retention_period = '36h' )",
want: &DdlStatement{Ddl: `ALTER CHANGE STREAM NamesAndAlbums SET OPTIONS (retention_period = "36h")`},
},
{
desc: "ALTER CHANGE STREAM DROP FOR ALL statement",
input: "ALTER CHANGE STREAM MyStream DROP FOR ALL",
want: &DdlStatement{Ddl: "ALTER CHANGE STREAM MyStream DROP FOR ALL"},
},
{
desc: "DROP CHANGE STREAM statement",
input: "DROP CHANGE STREAM NamesAndAlbums",
want: &DdlStatement{Ddl: "DROP CHANGE STREAM NamesAndAlbums"},
},
{
desc: "GRANT statement",
input: "GRANT SELECT ON TABLE employees TO ROLE hr_rep",
want: &DdlStatement{Ddl: "GRANT SELECT ON TABLE employees TO ROLE hr_rep"},
},
{
desc: "REVOKE statement",
input: "REVOKE SELECT ON TABLE employees FROM ROLE hr_rep",
want: &DdlStatement{Ddl: "REVOKE SELECT ON TABLE employees FROM ROLE hr_rep"},
},
{
desc: "ALTER STATISTICS statement",
input: "ALTER STATISTICS package SET OPTIONS (allow_gc = false)",
want: &DdlStatement{Ddl: "ALTER STATISTICS package SET OPTIONS (allow_gc = false)"},
},
{
desc: "ANALYZE statement",
input: "ANALYZE",
want: &DdlStatement{Ddl: "ANALYZE"},
},
{
desc: "INSERT statement",
input: "INSERT INTO t1 (id, name) VALUES (1, 'yuki')",
want: &DmlStatement{Dml: "INSERT INTO t1 (id, name) VALUES (1, 'yuki')"},
},
{
desc: "UPDATE statement",
input: "UPDATE t1 SET name = hello WHERE id = 1",
want: &DmlStatement{Dml: "UPDATE t1 SET name = hello WHERE id = 1"},
},
{
desc: "DELETE statement",
input: "DELETE FROM t1 WHERE id = 1",
want: &DmlStatement{Dml: "DELETE FROM t1 WHERE id = 1"},
},
{
desc: "PARTITIONED UPDATE statement",
input: "PARTITIONED UPDATE t1 SET name = hello WHERE id > 1",
want: &PartitionedDmlStatement{Dml: "UPDATE t1 SET name = hello WHERE id > 1"},
},
{
desc: "PARTITIONED UPDATE statement with statement hint",
input: "PARTITIONED @{PDML_MAX_PARALLELISM=100} UPDATE t1 SET name = hello WHERE id > 1",
want: &PartitionedDmlStatement{Dml: "@{PDML_MAX_PARALLELISM=100} UPDATE t1 SET name = hello WHERE id > 1"},
},
{
desc: "PARTITIONED DELETE statement",
input: "PARTITIONED DELETE FROM t1 WHERE id > 1",
want: &PartitionedDmlStatement{Dml: "DELETE FROM t1 WHERE id > 1"},
},
{
desc: "PARTITIONED DELETE statement with statement hint",
input: "PARTITIONED @{PDML_MAX_PARALLELISM=100} DELETE FROM t1 WHERE id > 1",
want: &PartitionedDmlStatement{Dml: "@{PDML_MAX_PARALLELISM=100} DELETE FROM t1 WHERE id > 1"},
},
{
desc: "EXPLAIN INSERT statement",
input: "EXPLAIN INSERT INTO t1 (id, name) VALUES (1, 'yuki')",
want: &ExplainStatement{Explain: "INSERT INTO t1 (id, name) VALUES (1, 'yuki')", IsDML: true},
},
{
desc: "EXPLAIN UPDATE statement",
input: "EXPLAIN UPDATE t1 SET name = hello WHERE id = 1",
want: &ExplainStatement{Explain: "UPDATE t1 SET name = hello WHERE id = 1", IsDML: true},
},
{
desc: "EXPLAIN DELETE statement",
input: "EXPLAIN DELETE FROM t1 WHERE id = 1",
want: &ExplainStatement{Explain: "DELETE FROM t1 WHERE id = 1", IsDML: true},
},
{
desc: "DESCRIBE DELETE statement",
input: "DESCRIBE DELETE FROM t1 WHERE id = 1",
want: &DescribeStatement{Statement: "DELETE FROM t1 WHERE id = 1", IsDML: true},
},
{
desc: "EXPLAIN ANALYZE INSERT statement",
input: "EXPLAIN ANALYZE INSERT INTO t1 (id, name) VALUES (1, 'yuki')",
want: &ExplainAnalyzeDmlStatement{Dml: "INSERT INTO t1 (id, name) VALUES (1, 'yuki')"},
},
{
desc: "EXPLAIN ANALYZE UPDATE statement",
input: "EXPLAIN ANALYZE UPDATE t1 SET name = hello WHERE id = 1",
want: &ExplainAnalyzeDmlStatement{Dml: "UPDATE t1 SET name = hello WHERE id = 1"},
},
{
desc: "EXPLAIN ANALYZE DELETE statement",
input: "EXPLAIN ANALYZE DELETE FROM t1 WHERE id = 1",
want: &ExplainAnalyzeDmlStatement{Dml: "DELETE FROM t1 WHERE id = 1"},
},
{
desc: "BEGIN statement",
input: "BEGIN",
want: &BeginStatement{},
},
{
desc: "BEGIN TRANSACTION statement",
input: "BEGIN TRANSACTION",
want: &BeginStatement{},
},
{
desc: "BEGIN RW statement",
input: "BEGIN RW",
want: &BeginRwStatement{},
},
{
desc: "BEGIN PRIORITY statement",
input: "BEGIN PRIORITY MEDIUM",
want: &BeginStatement{
Priority: sppb.RequestOptions_PRIORITY_MEDIUM,
},
},
{
desc: "BEGIN RW PRIORITY statement",
input: "BEGIN RW PRIORITY LOW",
want: &BeginRwStatement{
Priority: sppb.RequestOptions_PRIORITY_LOW,
},
},
{
desc: "BEGIN RO statement",
input: "BEGIN RO",
want: &BeginRoStatement{TimestampBoundType: timestampBoundUnspecified},
},
{
desc: "BEGIN RO staleness statement",
input: "BEGIN RO 10",
want: &BeginRoStatement{Staleness: time.Duration(10 * time.Second), TimestampBoundType: exactStaleness},
},
{
desc: "BEGIN RO read timestamp statement",
input: "BEGIN RO 2020-03-30T22:54:44.834017+09:00",
want: &BeginRoStatement{Timestamp: timestamp, TimestampBoundType: readTimestamp},
skipLowerCase: true,
},
{
desc: "BEGIN RO PRIORITY statement",
input: "BEGIN RO PRIORITY LOW",
want: &BeginRoStatement{TimestampBoundType: timestampBoundUnspecified, Priority: sppb.RequestOptions_PRIORITY_LOW},
},
{
desc: "BEGIN RO staleness with PRIORITY statement",
input: "BEGIN RO 10 PRIORITY HIGH",
want: &BeginRoStatement{
Staleness: time.Duration(10 * time.Second),
TimestampBoundType: exactStaleness,
Priority: sppb.RequestOptions_PRIORITY_HIGH,
},
},
{
desc: "SET TRANSACTION READ ONLY statement",
input: "SET TRANSACTION READ ONLY",
want: &SetTransactionStatement{IsReadOnly: true},
},
{
desc: "SET TRANSACTION READ WRITE statement",
input: "SET TRANSACTION READ WRITE",
want: &SetTransactionStatement{IsReadOnly: false},
},
{
desc: "COMMIT statement",
input: "COMMIT",
want: &CommitStatement{},
},
{
desc: "COMMIT TRANSACTION statement",
input: "COMMIT TRANSACTION",
want: &CommitStatement{},
},
{
desc: "ROLLBACK statement",
input: "ROLLBACK",
want: &RollbackStatement{},
},
{
desc: "ROLLBACK TRANSACTION statement",
input: "ROLLBACK TRANSACTION",
want: &RollbackStatement{},
},
{
desc: "CLOSE statement",
input: "CLOSE",
want: &RollbackStatement{},
},
{
desc: "CLOSE TRANSACTION statement",
input: "CLOSE TRANSACTION",
want: &RollbackStatement{},
},
{
desc: "EXIT statement",
input: "EXIT",
want: &ExitStatement{},
},
{
desc: "USE statement",
input: "USE database2",
want: &UseStatement{Database: "database2"},
},
{
desc: "USE statement with quoted identifier",
input: "USE `my-database`",
want: &UseStatement{Database: "my-database"},
},
{
desc: "USE statement with role",
input: "USE database2 ROLE role2",
want: &UseStatement{Database: "database2", Role: "role2"},
},
{
desc: "USE statement with quoted identifier",
input: "USE `my-database` ROLE `my-role`",
want: &UseStatement{Database: "my-database", Role: "my-role"},
},
{
desc: "SHOW DATABASES statement",
input: "SHOW DATABASES",
want: &ShowDatabasesStatement{},
},
{
desc: "SHOW CREATE TABLE statement",
input: "SHOW CREATE TABLE t1",
want: &ShowCreateTableStatement{Table: "t1"},
},
{
desc: "SHOW CREATE TABLE statement with a named schema",
input: "SHOW CREATE TABLE sch1.t1",
want: &ShowCreateTableStatement{Schema: "sch1", Table: "t1"},
},
{
desc: "SHOW CREATE TABLE statement with quoted identifier",
input: "SHOW CREATE TABLE `TABLE`",
want: &ShowCreateTableStatement{Table: "TABLE"},
},
{
desc: "SHOW TABLES statement",
input: "SHOW TABLES",
want: &ShowTablesStatement{},
},
{
desc: "SHOW TABLES statement with schema",
input: "SHOW TABLES sch1",
want: &ShowTablesStatement{Schema: "sch1"},
},
{
desc: "SHOW TABLES statement with quoted schema",
input: "SHOW TABLES `sch1`",
want: &ShowTablesStatement{Schema: "sch1"},
},
{
desc: "SHOW INDEX statement",
input: "SHOW INDEX FROM t1",
want: &ShowIndexStatement{Table: "t1"},
},
{
desc: "SHOW INDEX statement with a named schema",
input: "SHOW INDEX FROM sch1.t1",
want: &ShowIndexStatement{Schema: "sch1", Table: "t1"},
},
{
desc: "SHOW INDEXES statement",
input: "SHOW INDEXES FROM t1",
want: &ShowIndexStatement{Table: "t1"},
},
{
desc: "SHOW INDEX statement with quoted identifier",
input: "SHOW INDEX FROM `TABLE`",
want: &ShowIndexStatement{Table: "TABLE"},
},
{
desc: "SHOW KEYS statement",
input: "SHOW KEYS FROM t1",
want: &ShowIndexStatement{Table: "t1"},
},
{
desc: "SHOW COLUMNS statement",
input: "SHOW COLUMNS FROM t1",
want: &ShowColumnsStatement{Table: "t1"},
},
{
desc: "SHOW COLUMNS statement with a named schema",
input: "SHOW COLUMNS FROM sch1.t1",
want: &ShowColumnsStatement{Schema: "sch1", Table: "t1"},
},
{
desc: "SHOW COLUMNS statement with quoted identifier",
input: "SHOW COLUMNS FROM `TABLE`",
want: &ShowColumnsStatement{Table: "TABLE"},
},
{
desc: "EXPLAIN SELECT statement",
input: "EXPLAIN SELECT * FROM t1",
want: &ExplainStatement{Explain: "SELECT * FROM t1"},
},
{
desc: "EXPLAIN SELECT statement with statement hint",
input: "EXPLAIN @{OPTIMIZER_VERSION=latest} SELECT * FROM t1",
want: &ExplainStatement{Explain: "@{OPTIMIZER_VERSION=latest} SELECT * FROM t1"},
},
{
desc: "EXPLAIN SELECT statement with WITH",
input: "EXPLAIN WITH t1 AS (SELECT 1) SELECT * FROM t1",
want: &ExplainStatement{Explain: "WITH t1 AS (SELECT 1) SELECT * FROM t1"},
},
{
desc: "GRAPH statement",
input: "GRAPH FinGraph MATCH (n) RETURN LABELS(n) AS label, n.id",
want: &SelectStatement{Query: "GRAPH FinGraph MATCH (n) RETURN LABELS(n) AS label, n.id"},
},
{
desc: "EXPLAIN GRAPH statement",
input: "EXPLAIN GRAPH FinGraph MATCH (n) RETURN LABELS(n) AS label, n.id",
want: &ExplainStatement{Explain: "GRAPH FinGraph MATCH (n) RETURN LABELS(n) AS label, n.id"},
},
{
desc: "EXPLAIN ANALYZE GRAPH statement",
input: "EXPLAIN ANALYZE GRAPH FinGraph MATCH (n) RETURN LABELS(n) AS label, n.id",
want: &ExplainAnalyzeStatement{Query: "GRAPH FinGraph MATCH (n) RETURN LABELS(n) AS label, n.id"},
},
{
desc: "DESCRIBE SELECT statement",
input: "DESCRIBE SELECT * FROM t1",
want: &DescribeStatement{Statement: "SELECT * FROM t1"},
},
{
desc: "Stored system procedures",
input: `CALL cancel_query("1234567890123456789")`,
want: &SelectStatement{Query: `CALL cancel_query("1234567890123456789")`},
},
{
desc: "EXPLAIN Stored system procedures",
input: `EXPLAIN CALL cancel_query("1234567890123456789")`,
want: &ExplainStatement{Explain: `CALL cancel_query("1234567890123456789")`},
},
{
desc: "EXPLAIN ANALYZE Stored system procedures",
input: `EXPLAIN ANALYZE CALL cancel_query("1234567890123456789")`,
want: &ExplainAnalyzeStatement{Query: `CALL cancel_query("1234567890123456789")`},
},
{
desc: "PARTITION statement",
input: `PARTITION SELECT * FROM Singers`,
want: &PartitionStatement{SQL: `SELECT * FROM Singers`},
},
{
desc: "TRY PARTITIONED QUERY statement",
input: `TRY PARTITIONED QUERY SELECT * FROM Singers`,
want: &TryPartitionedQueryStatement{SQL: `SELECT * FROM Singers`},
},
{
desc: "RUN PARTITIONED QUERY statement",
input: `RUN PARTITIONED QUERY SELECT * FROM Singers`,
want: &RunPartitionedQueryStatement{SQL: `SELECT * FROM Singers`},
},
{
desc: "RUN PARTITION statement",
input: `RUN PARTITION '123456789'`,
want: &RunPartitionStatement{Token: `123456789`},
},
{
desc: "SHOW LOCAL PROTO statement",
input: `SHOW LOCAL PROTO`,
want: &ShowLocalProtoStatement{},
},
{
desc: "SHOW REMOTE PROTO statement",
input: `SHOW REMOTE PROTO`,
want: &ShowRemoteProtoStatement{},
},
{
desc: "SYNC PROTO BUNDLE UPSERT statement",
input: "SYNC PROTO BUNDLE UPSERT (examples.ProtoType, examples.`EscapedType`, `examples.EscapedPath`)",
want: &SyncProtoStatement{UpsertPaths: sliceOf("examples.ProtoType", "examples.EscapedType", `examples.EscapedPath`)},
},
{
desc: "SYNC PROTO BUNDLE DELETE statement",
input: "SYNC PROTO BUNDLE DELETE (examples.ProtoType, examples.`EscapedType`, `examples.EscapedPath`)",
want: &SyncProtoStatement{DeletePaths: sliceOf("examples.ProtoType", "examples.EscapedType", `examples.EscapedPath`)},
},
{
desc: "SET statement",
input: `SET OPTIMIZER_VERSION = "3"`,
want: &SetStatement{VarName: "OPTIMIZER_VERSION", Value: `"3"`},
},
{
desc: "SET += statement",
input: `SET CLI_PROTO_DESCRIPTOR_FILE += "./message_descriptors.pb"`,
want: &SetAddStatement{VarName: "CLI_PROTO_DESCRIPTOR_FILE", Value: `"./message_descriptors.pb"`},
},
{
desc: "SHOW VARIABLE statement",
input: `SHOW VARIABLE OPTIMIZER_VERSION`,
want: &ShowVariableStatement{VarName: "OPTIMIZER_VERSION"},
},
{
desc: "SHOW VARIABLES statement",
input: `SHOW VARIABLES`,
want: &ShowVariablesStatement{},
},
{
desc: "SET PARAM type statement",
input: `SET PARAM string_type STRING`,
want: &SetParamTypeStatement{Name: "string_type", Type: "STRING"},
},
{
desc: "SET PARAM value statement",
input: `SET PARAM bytes_value = b"foo"`,
want: &SetParamValueStatement{Name: "bytes_value", Value: `b"foo"`},
},
{
desc: "SHOW PARAMS statement",
input: `SHOW PARAMS`,
want: &ShowParamsStatement{},
},
{
desc: "SHOW DDLS statement",
input: `SHOW DDLS`,
want: &ShowDdlsStatement{},
},
{
desc: "GEMINI statement",
input: `GEMINI "Show all tables"`,
want: &GeminiStatement{Text: "Show all tables"},
},
{
desc: "START BATCH DDL statement",
input: `START BATCH DDL`,
want: &StartBatchStatement{Mode: batchModeDDL},
},
{
desc: "START BATCH DML statement",
input: `START BATCH DML`,
want: &StartBatchStatement{Mode: batchModeDML},
},
{
desc: "ABORT BATCH statement",
input: `ABORT BATCH`,
want: &AbortBatchStatement{},
},
{
desc: "ABORT BATCH statement",
input: `ABORT BATCH TRANSACTION`,
want: &AbortBatchStatement{},
},
{
desc: "RUN BATCH statement",
input: `RUN BATCH`,
want: &RunBatchStatement{},
},
} {
t.Run(test.desc, func(t *testing.T) {
got, err := BuildStatement(test.input)
if err != nil {
t.Fatalf("BuildStatement(%q) got error: %v", test.input, err)
}
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("BuildStatement(%q) differ: %v", test.input, diff)
}
})
if !test.skipLowerCase {
input := strings.ToLower(test.input)
t.Run("Lower "+test.desc, func(t *testing.T) {
got, err := BuildStatement(input)
if err != nil {
t.Fatalf("BuildStatement(%q) got error: %v", input, err)
}
// check only type
gotType := reflect.TypeOf(got)
wantType := reflect.TypeOf(test.want)
if gotType != wantType {
t.Errorf("BuildStatement(%q) has invalid statement type: got = %q, but want = %q", input, gotType, wantType)
}
})
}
}
}
func TestBuildStatement_InvalidCase(t *testing.T) {
// invalid tests
for _, test := range []struct {
input string
}{
{"FOO BAR"},
{"SELEC T FROM t1"},
{"BEGIN PRIORITY CRITICAL"},
} {
t.Run(test.input, func(t *testing.T) {
got, err := BuildStatement(test.input)
if err == nil {
t.Errorf("BuildStatement(%q) = %#v, but want error", test.input, got)
}
})
}
}
func TestIsCreateTableDDL(t *testing.T) {
for _, tt := range []struct {
desc string
ddl string
schema string
table string
want bool
}{
{
desc: "exact match",
ddl: "CREATE TABLE t1 (\n",
table: "t1",
want: true,
},
{
desc: "given table is prefix of DDL's table",
ddl: "CREATE TABLE t12 (\n",
table: "t1",
want: false,
},
{
desc: "DDL's table is prefix of given table",
ddl: "CREATE TABLE t1 (\n",
table: "t12",
want: false,
},
{
desc: "given table has reserved word",
ddl: "CREATE TABLE `create` (\n",
table: "create",
want: true,
},
{
desc: "given table is regular expression",
ddl: "CREATE TABLE t1 (\n",
table: `..`,
want: false,
},
{
desc: "given table is invalid regular expression",
ddl: "CREATE TABLE t1 (\n",
table: `[\]`,
want: false,
},
} {
t.Run(tt.desc, func(t *testing.T) {
if got := isCreateTableDDL(tt.ddl, tt.schema, tt.table); got != tt.want {
t.Errorf("isCreateTableDDL(%q, %q) = %v, but want %v", tt.ddl, tt.table, got, tt.want)
}
})
}
}
func TestExtractSchemaAndTable(t *testing.T) {
for _, tt := range []struct {
desc string
input string
schema string
table string
}{
{
desc: "raw table",
input: "table",
schema: "",
table: "table",
},
{
desc: "quoted table",
input: "`table`",
schema: "",
table: "table",
},
{
desc: "FQN",
input: "schema.table",
schema: "schema",
table: "table",
},
{
desc: "FQN with spaces",
input: "schema . table",
schema: "schema",
table: "table",
},
{
desc: "FQN, both schema and table are quoted",
input: "`schema`.`table`",
schema: "schema",
table: "table",
},
{
desc: "FQN with spaces, both schema and table are quoted",
input: "`schema` . `table`",
schema: "schema",
table: "table",
},
{
desc: "FQN, only schema is quoted",
input: "`schema`.table",
schema: "schema",
table: "table",
},
{
desc: "FQN with spaces, only schema is quoted",
input: "`schema` . table",
schema: "schema",
table: "table",
},
{
desc: "FQN, only table is quoted",
input: "schema.`table`",
schema: "schema",
table: "table",
},
{
desc: "FQN with spaces, only table is quoted",
input: "schema . `table`",
schema: "schema",
table: "table",
},
{
desc: "whole quoted FQN",
input: "`schema.table`",
schema: "schema",
table: "table",
},
} {
t.Run(tt.desc, func(t *testing.T) {
if schema, table := extractSchemaAndTable(tt.input); schema != tt.schema || table != tt.table {
t.Errorf("extractSchemaAndTable(%q) = (%v, %v), but want (%v, %v)", tt.input, schema, table, tt.schema, tt.table)
}
})
}
}
//go:embed testdata/protos/order_descriptors.pb
var orderDescriptorsContent []byte
var orderFds = lo.Must(decodeMessage[descriptorpb.FileDescriptorSet, *descriptorpb.FileDescriptorSet](orderDescriptorsContent))
func decodeMessage[T any, PT interface {
*T
proto.Message
}](b []byte) (PT, error) {
var result T
var pt PT = &result
err := proto.Unmarshal(b, pt)
return pt, err
}
func TestComposeProtoBundleDDLs(t *testing.T) {
for _, tt := range []struct {
desc string
fds *descriptorpb.FileDescriptorSet
upsertPaths []string
deletePaths []string
want []string
}{
{
desc: "non-empty fds and empty input",
fds: orderFds,
upsertPaths: nil,
deletePaths: nil,
want: nil,
},
{
desc: "nil fds and empty input",
fds: nil,
upsertPaths: nil,
deletePaths: nil,
want: nil,
},
{
desc: "empty fds and empty input",
fds: &descriptorpb.FileDescriptorSet{},
want: nil,
},
{
desc: "UPSERT existing type",
fds: orderFds,
upsertPaths: sliceOf("examples.shipping.Order"),
want: sliceOf("ALTER PROTO BUNDLE UPDATE (examples.shipping.`Order`)"),
},
{
desc: "UPSERT non-existing type",
fds: orderFds,
upsertPaths: sliceOf("examples.UnknownType"),
want: sliceOf("ALTER PROTO BUNDLE INSERT (examples.UnknownType)"),
},
{
desc: "DELETE existing type",
fds: orderFds,
deletePaths: sliceOf("examples.shipping.Order"),
want: sliceOf("ALTER PROTO BUNDLE DELETE (examples.shipping.`Order`)"),
},
{
desc: "DELETE non-existing type",
fds: orderFds,
deletePaths: sliceOf("examples.UnknownType"),
want: nil,
},
// TODO: Support mixed SYNC PROTO BUNDLE in parseSyncProtoBundle
{
desc: "Mixed UPSERT, DELETE",
fds: orderFds,
upsertPaths: sliceOf("examples.shipping.Order", "examples.UnknownType"),
deletePaths: sliceOf("examples.shipping.OrderHistory"),
want: sliceOf("ALTER PROTO BUNDLE INSERT (examples.UnknownType) UPDATE (examples.shipping.`Order`) DELETE (examples.shipping.OrderHistory)"),
},
} {
got := composeProtoBundleDDLs(tt.fds, tt.upsertPaths, tt.deletePaths)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("composeProtoBundleDDLs() mismatch (-want +got):\n%s", diff)
}
}
}