forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_test.go
More file actions
1283 lines (1245 loc) · 39.4 KB
/
parse_test.go
File metadata and controls
1283 lines (1245 loc) · 39.4 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
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 2017 Google Inc.
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 sqlparser
import (
"bytes"
"fmt"
"strings"
"testing"
)
var (
validSQL = []struct {
input string
output string
}{{
input: "select 1",
}, {
input: "insert /* simple */ into a values (1)",
}, {
input: "select 1 from t",
}, {
input: "select .1 from t",
}, {
input: "select 1.2e1 from t",
}, {
input: "select 1.2e+1 from t",
}, {
input: "select 1.2e-1 from t",
}, {
input: "select 08.3 from t",
}, {
input: "select -1 from t where b = -2",
}, {
input: "select - -1 from t",
output: "select 1 from t",
}, {
input: "select 1 from t // aa\n",
output: "select 1 from t",
}, {
input: "select 1 from t -- aa\n",
output: "select 1 from t",
}, {
input: "select 1 from t # aa\n",
output: "select 1 from t",
}, {
input: "select 1 --aa\nfrom t",
output: "select 1 from t",
}, {
input: "select 1 #aa\nfrom t",
output: "select 1 from t",
}, {
input: "select /* simplest */ 1 from t",
}, {
input: "select /* double star **/ 1 from t",
}, {
input: "select /* double */ /* comment */ 1 from t",
}, {
input: "select /* back-quote keyword */ `By` from t",
}, {
input: "select /* back-quote num */ `2a` from t",
}, {
input: "select /* back-quote . */ `a.b` from t",
}, {
input: "select /* back-quote back-quote */ `a``b` from t",
}, {
input: "select /* back-quote unnecessary */ 1 from `t`",
output: "select /* back-quote unnecessary */ 1 from t",
}, {
input: "select /* back-quote idnum */ 1 from `a1`",
output: "select /* back-quote idnum */ 1 from a1",
}, {
input: "select /* @ */ @@a from b",
}, {
input: "select /* \\0 */ '\\0' from a",
}, {
input: "select 1 /* drop this comment */ from t",
output: "select 1 from t",
}, {
input: "select /* union */ 1 from t union select 1 from t",
}, {
input: "select /* double union */ 1 from t union select 1 from t union select 1 from t",
}, {
input: "select /* union all */ 1 from t union all select 1 from t",
}, {
input: "(select /* union parenthesized select */ 1 from t order by a) union select 1 from t",
output: "(select /* union parenthesized select */ 1 from t order by a asc) union select 1 from t",
}, {
input: "select /* union parenthesized select 2 */ 1 from t union (select 1 from t)",
}, {
input: "select /* union order by */ 1 from t union select 1 from t order by a",
output: "select /* union order by */ 1 from t union select 1 from t order by a asc",
}, {
input: "select /* union with limit on lhs */ 1 from t limit 1 union select 1 from t",
}, {
input: "(select id, a from t order by id limit 1) union (select id, b as a from s order by id limit 1) order by a limit 1",
output: "(select id, a from t order by id asc limit 1) union (select id, b as a from s order by id asc limit 1) order by a asc limit 1",
}, {
input: "select a from (select 1 as a from tbl1 union select 2 from tbl2) as t",
}, {
input: "select * from t1 join (select * from t2 union select * from t3) as t",
}, {
// Ensure this doesn't generate: ""select * from t1 join t2 on a = b join t3 on a = b".
input: "select * from t1 join t2 on a = b join t3",
}, {
input: "select * from t1 where col in (select 1 from dual union select 2 from dual)",
}, {
input: "select * from t1 where exists (select a from t2 union select b from t3)",
}, {
input: "select /* distinct */ distinct 1 from t",
}, {
input: "select /* select list */ 1, 2 from t",
}, {
input: "select /* * */ * from t",
}, {
input: "select /* a.* */ a.* from t",
}, {
input: "select /* a.b.* */ a.b.* from t",
}, {
input: "select /* column alias */ a b from t",
output: "select /* column alias */ a as b from t",
}, {
input: "select /* column alias with as */ a as b from t",
}, {
input: "select /* keyword column alias */ a as `By` from t",
}, {
input: "select /* column alias as string */ a as \"b\" from t",
output: "select /* column alias as string */ a as b from t",
}, {
input: "select /* column alias as string without as */ a \"b\" from t",
output: "select /* column alias as string without as */ a as b from t",
}, {
input: "select /* a.* */ a.* from t",
}, {
input: "select /* `By`.* */ `By`.* from t",
}, {
input: "select /* select with bool expr */ a = b from t",
}, {
input: "select /* case_when */ case when a = b then c end from t",
}, {
input: "select /* case_when_else */ case when a = b then c else d end from t",
}, {
input: "select /* case_when_when_else */ case when a = b then c when b = d then d else d end from t",
}, {
input: "select /* case */ case aa when a = b then c end from t",
}, {
input: "select /* parenthesis */ 1 from (t)",
}, {
input: "select /* parenthesis multi-table */ 1 from (t1, t2)",
}, {
input: "select /* table list */ 1 from t1, t2",
}, {
input: "select /* parenthessis in table list 1 */ 1 from (t1), t2",
}, {
input: "select /* parenthessis in table list 2 */ 1 from t1, (t2)",
}, {
input: "select /* table alias */ 1 from t t1",
output: "select /* table alias */ 1 from t as t1",
}, {
input: "select /* table alias with as */ 1 from t as t1",
}, {
input: "select /* string table alias */ 1 from t as 't1'",
output: "select /* string table alias */ 1 from t as t1",
}, {
input: "select /* string table alias without as */ 1 from t 't1'",
output: "select /* string table alias without as */ 1 from t as t1",
}, {
input: "select /* keyword table alias */ 1 from t as `By`",
}, {
input: "select /* join */ 1 from t1 join t2",
}, {
input: "select /* join on */ 1 from t1 join t2 on a = b",
}, {
input: "select /* join on */ 1 from t1 join t2 using (a)",
}, {
input: "select /* inner join */ 1 from t1 join t2",
}, {
input: "select /* inner join */ 1 from t1 inner join t2",
}, {
input: "select /* cross join */ 1 from t1 cross join t2",
}, {
input: "select /* left join */ 1 from t1 left join t2 on a = b",
}, {
input: "select /* left join */ 1 from t1 left join t2 using (a)",
}, {
input: "select /* left outer join */ 1 from t1 left outer join t2 on a = b",
output: "select /* left outer join */ 1 from t1 left join t2 on a = b",
}, {
input: "select /* left outer join */ 1 from t1 left outer join t2 using (a)",
output: "select /* left outer join */ 1 from t1 left join t2 using (a)",
}, {
input: "select /* natural join */ 1 from t1 natural join t2",
}, {
input: "select /* natural left join */ 1 from t1 natural left join t2",
}, {
input: "select /* natural left outer join */ 1 from t1 natural left join t2",
output: "select /* natural left outer join */ 1 from t1 natural left join t2",
}, {
input: "select /* join on */ 1 from t1 join t2 on a = b",
}, {
input: "select /* join using */ 1 from t1 join t2 using (a)",
}, {
input: "select /* join using (a, b, c) */ 1 from t1 join t2 using (a, b, c)",
}, {
input: "select /* s.t */ 1 from s.t",
}, {
input: "select /* keyword schema & table name */ 1 from `By`.`bY`",
}, {
input: "select /* select in from */ 1 from (select 1 from t) as a",
}, {
input: "select /* select in from with no as */ 1 from (select 1 from t) a",
output: "select /* select in from with no as */ 1 from (select 1 from t) as a",
}, {
input: "select /* where */ 1 from t where a = b",
}, {
input: "select /* and */ 1 from t where a = b and a = c",
}, {
input: "select /* && */ 1 from t where a = b && a = c",
output: "select /* && */ 1 from t where a = b and a = c",
}, {
input: "select /* or */ 1 from t where a = b or a = c",
}, {
input: "select /* || */ 1 from t where a = b || a = c",
output: "select /* || */ 1 from t where a = b or a = c",
}, {
input: "select /* not */ 1 from t where not a = b",
}, {
input: "select /* ! */ 1 from t where a = !1",
}, {
input: "select /* bool is */ 1 from t where a = b is null",
}, {
input: "select /* bool is not */ 1 from t where a = b is not false",
}, {
input: "select /* true */ 1 from t where true",
}, {
input: "select /* false */ 1 from t where false",
}, {
input: "select /* false on left */ 1 from t where false = 0",
}, {
input: "select /* exists */ 1 from t where exists (select 1 from t)",
}, {
input: "select /* (boolean) */ 1 from t where not (a = b)",
}, {
input: "select /* in value list */ 1 from t where a in (b, c)",
}, {
input: "select /* in select */ 1 from t where a in (select 1 from t)",
}, {
input: "select /* not in */ 1 from t where a not in (b, c)",
}, {
input: "select /* like */ 1 from t where a like b",
}, {
input: "select /* like escape */ 1 from t where a like b escape '!'",
}, {
input: "select /* not like */ 1 from t where a not like b",
}, {
input: "select /* not like escape */ 1 from t where a not like b escape '$'",
}, {
input: "select /* regexp */ 1 from t where a regexp b",
}, {
input: "select /* not regexp */ 1 from t where a not regexp b",
}, {
input: "select /* rlike */ 1 from t where a rlike b",
output: "select /* rlike */ 1 from t where a regexp b",
}, {
input: "select /* not rlike */ 1 from t where a not rlike b",
output: "select /* not rlike */ 1 from t where a not regexp b",
}, {
input: "select /* between */ 1 from t where a between b and c",
}, {
input: "select /* not between */ 1 from t where a not between b and c",
}, {
input: "select /* is null */ 1 from t where a is null",
}, {
input: "select /* is not null */ 1 from t where a is not null",
}, {
input: "select /* is true */ 1 from t where a is true",
}, {
input: "select /* is not true */ 1 from t where a is not true",
}, {
input: "select /* is false */ 1 from t where a is false",
}, {
input: "select /* is not false */ 1 from t where a is not false",
}, {
input: "select /* < */ 1 from t where a < b",
}, {
input: "select /* <= */ 1 from t where a <= b",
}, {
input: "select /* >= */ 1 from t where a >= b",
}, {
input: "select /* > */ 1 from t where a > b",
}, {
input: "select /* != */ 1 from t where a != b",
}, {
input: "select /* <> */ 1 from t where a <> b",
}, {
input: "select /* != */ 1 from t where a != b",
}, {
input: "select /* single value expression list */ 1 from t where a in (b)",
}, {
input: "select /* select as a value expression */ 1 from t where a = (select a from t)",
}, {
input: "select /* parenthesised value */ 1 from t where a = (b)",
}, {
input: "select /* over-parenthesize */ ((1)) from t where ((a)) in (((1))) and ((a, b)) in ((((1, 1))), ((2, 2)))",
}, {
input: "select /* dot-parenthesize */ (a.b) from t where (b.c) = 2",
}, {
input: "select /* & */ 1 from t where a = b & c",
}, {
input: "select /* & */ 1 from t where a = b & c",
}, {
input: "select /* | */ 1 from t where a = b | c",
}, {
input: "select /* ^ */ 1 from t where a = b ^ c",
}, {
input: "select /* + */ 1 from t where a = b + c",
}, {
input: "select /* - */ 1 from t where a = b - c",
}, {
input: "select /* * */ 1 from t where a = b * c",
}, {
input: "select /* / */ 1 from t where a = b / c",
}, {
input: "select /* % */ 1 from t where a = b % c",
}, {
input: "select /* div */ 1 from t where a = b div c",
}, {
input: "select /* MOD */ 1 from t where a = b MOD c",
output: "select /* MOD */ 1 from t where a = b % c",
}, {
input: "select /* << */ 1 from t where a = b << c",
}, {
input: "select /* >> */ 1 from t where a = b >> c",
}, {
input: "select /* % no space */ 1 from t where a = b%c",
output: "select /* % no space */ 1 from t where a = b % c",
}, {
input: "select /* u+ */ 1 from t where a = +b",
}, {
input: "select /* u- */ 1 from t where a = -b",
}, {
input: "select /* u~ */ 1 from t where a = ~b",
}, {
input: "select /* empty function */ 1 from t where a = b()",
}, {
input: "select /* function with 1 param */ 1 from t where a = b(c)",
}, {
input: "select /* function with many params */ 1 from t where a = b(c, d)",
}, {
input: "select /* function with distinct */ count(distinct a) from t",
}, {
input: "select /* if as func */ 1 from t where a = if(b)",
}, {
input: "select /* mod as func */ a from tab where mod(b, 2) = 0",
}, {
input: "select /* database as func no param */ database() from t",
}, {
input: "select /* database as func 1 param */ database(1) from t",
}, {
input: "select /* a */ a from t",
}, {
input: "select /* a.b */ a.b from t",
}, {
input: "select /* a.b.c */ a.b.c from t",
}, {
input: "select /* keyword a.b */ `By`.`bY` from t",
}, {
input: "select /* string */ 'a' from t",
}, {
input: "select /* double quoted string */ \"a\" from t",
output: "select /* double quoted string */ 'a' from t",
}, {
input: "select /* quote quote in string */ 'a''a' from t",
output: "select /* quote quote in string */ 'a''a' from t",
}, {
input: "select /* double quote quote in string */ \"a\"\"a\" from t",
output: "select /* double quote quote in string */ 'a\\\"a' from t",
}, {
input: "select /* quote in double quoted string */ \"a'a\" from t",
output: "select /* quote in double quoted string */ 'a''a' from t",
}, {
input: "select /* backslash quote in string */ 'a''a' from t",
output: "select /* backslash quote in string */ 'a''a' from t",
}, {
input: "select /* literal backslash in string */ 'a\\\\na' from t",
}, {
input: "select /* non-escape */ '\\x' from t",
}, {
input: "select /* unescaped backslash */ '\\n' from t",
}, {
input: "select /* value argument */ :a from t",
}, {
input: "select /* value argument with digit */ :a1 from t",
}, {
input: "select /* value argument with dot */ :a.b from t",
}, {
input: "select /* positional argument */ ? from t",
output: "select /* positional argument */ :v1 from t",
}, {
input: "select /* multiple positional arguments */ ?, ? from t",
output: "select /* multiple positional arguments */ :v1, :v2 from t",
}, {
input: "select /* list arg */ * from t where a in ::list",
}, {
input: "select /* list arg not in */ * from t where a not in ::list",
}, {
input: "select /* null */ null from t",
}, {
input: "select /* octal */ 010 from t",
}, {
input: "select /* hex */ x'f0A1' from t",
output: "select /* hex */ X'f0A1' from t",
}, {
input: "select /* hex caps */ X'F0a1' from t",
}, {
input: "select /* 0x */ 0xf0 from t",
}, {
input: "select /* float */ 0.1 from t",
}, {
input: "select /* group by */ 1 from t group by a",
}, {
input: "select /* having */ 1 from t having a = b",
}, {
input: "select /* simple order by */ 1 from t order by a",
output: "select /* simple order by */ 1 from t order by a asc",
}, {
input: "select /* order by asc */ 1 from t order by a asc",
}, {
input: "select /* order by desc */ 1 from t order by a desc",
}, {
input: "select /* order by null */ 1 from t order by null",
}, {
input: "select /* limit a */ 1 from t limit a",
}, {
input: "select /* limit a,b */ 1 from t limit a, b",
}, {
input: "select /* binary unary */ a- -b from t",
output: "select /* binary unary */ a - -b from t",
}, {
input: "select /* - - */ - -b from t",
}, {
input: "select /* interval */ adddate('2008-01-02', interval 31 day) from t",
}, {
input: "select /* interval keyword */ adddate('2008-01-02', interval 1 year) from t",
}, {
input: "select /* dual */ 1 from dual",
}, {
input: "select /* Dual */ 1 from Dual",
output: "select /* Dual */ 1 from dual",
}, {
input: "select /* DUAL */ 1 from Dual",
output: "select /* DUAL */ 1 from dual",
}, {
input: "select /* column as bool in where */ a from t where b",
}, {
input: "select /* OR of columns in where */ * from t where a or b",
}, {
input: "select /* OR of mixed columns in where */ * from t where a = 5 or b and c is not null",
}, {
input: "select /* OR in select columns */ (a or b) from t where c = 5",
}, {
input: "select /* bool as select value */ a, true from t",
}, {
input: "select /* bool column in ON clause */ * from t join s on t.id = s.id and s.foo where t.bar",
}, {
input: "select /* bool in order by */ * from t order by a is null or b asc",
}, {
input: "select /* string in case statement */ if(max(case a when 'foo' then 1 else 0 end) = 1, 'foo', 'bar') as foobar from t",
}, {
input: "select /*!40101 * from*/ t",
output: "select * from t",
}, {
input: "select /*! * from*/ t",
output: "select * from t",
}, {
input: "select /*!* from*/ t",
output: "select * from t",
}, {
input: "select /*!401011 from*/ t",
output: "select 1 from t",
}, {
input: "select /* dual */ 1 from dual",
}, {
input: "insert /* a.b */ into a.b values (1)",
}, {
input: "insert /* multi-value */ into a values (1, 2)",
}, {
input: "insert /* multi-value list */ into a values (1, 2), (3, 4)",
}, {
input: "insert /* no values */ into a values ()",
}, {
input: "insert /* set */ into a set a = 1, b = 2",
output: "insert /* set */ into a(a, b) values (1, 2)",
}, {
input: "insert /* set default */ into a set a = default, b = 2",
output: "insert /* set default */ into a(a, b) values (default, 2)",
}, {
input: "insert /* value expression list */ into a values (a + 1, 2 * 3)",
}, {
input: "insert /* default */ into a values (default, 2 * 3)",
}, {
input: "insert /* column list */ into a(a, b) values (1, 2)",
}, {
input: "insert into a(a, b) values (1, ifnull(null, default(b)))",
}, {
input: "insert /* qualified column list */ into a(a, b) values (1, 2)",
}, {
input: "insert /* qualified columns */ into t (t.a, t.b) values (1, 2)",
output: "insert /* qualified columns */ into t(a, b) values (1, 2)",
}, {
input: "insert /* select */ into a select b, c from d",
}, {
input: "insert /* no cols & paren select */ into a(select * from t)",
output: "insert /* no cols & paren select */ into a select * from t",
}, {
input: "insert /* cols & paren select */ into a(a,b,c) (select * from t)",
output: "insert /* cols & paren select */ into a(a, b, c) select * from t",
}, {
input: "insert /* cols & union with paren select */ into a(b, c) (select d, e from f) union (select g from h)",
}, {
input: "insert /* bool in insert value */ into a values (1, true, false)",
}, {
input: "update /* simple */ a set b = 3",
}, {
input: "update /* a.b */ a.b set b = 3",
}, {
input: "update /* list */ a set b = 3, c = 4",
}, {
input: "update /* expression */ a set b = 3 + 4",
}, {
input: "update /* where */ a set b = 3 where a = b",
}, {
input: "update /* order */ a set b = 3 order by c desc",
}, {
input: "update /* limit */ a set b = 3 limit c",
}, {
input: "update /* bool in update */ a set b = true",
}, {
input: "update /* bool expr in update */ a set b = 5 > 2",
}, {
input: "update /* bool in update where */ a set b = 5 where c",
}, {
input: "update /* table qualifier */ a set a.b = 3",
}, {
input: "update /* table qualifier */ a set t.a.b = 3",
}, {
input: "update /* table alias */ tt aa set aa.cc = 3",
output: "update /* table alias */ tt as aa set aa.cc = 3",
}, {
input: "update (select id from foo) subqalias set id = 4",
output: "update (select id from foo) as subqalias set id = 4",
}, {
input: "update foo f, bar b set f.id = b.id where b.name = 'test'",
output: "update foo as f, bar as b set f.id = b.id where b.name = 'test'",
}, {
input: "update foo f join bar b on f.name = b.name set f.id = b.id where b.name = 'test'",
output: "update foo as f join bar as b on f.name = b.name set f.id = b.id where b.name = 'test'",
}, {
input: "delete /* simple */ from a",
}, {
input: "delete /* a.b */ from a.b",
}, {
input: "delete /* where */ from a where a = b",
}, {
input: "delete /* order */ from a order by b desc",
}, {
input: "delete /* limit */ from a limit b",
}, {
input: "alter table a rename to b",
}, {
input: "alter table a add column id int",
output: "alter table a",
}, {
input: "alter table a add id int",
output: "alter table a",
}, {
input: "create table a",
}, {
input: "create table a (\n\t`a` int\n)",
output: "create table a (\n\ta int\n)",
}, {
input: "create table `by` (\n\t`by` char\n)",
}, {
input: "create table if not exists a (\n\t`a` int\n)",
output: "create table a (\n\ta int\n)",
}, {
input: "create table a ignore me this is garbage",
output: "create table a",
}, {
input: "create table a (a int, b char, c garbage)",
output: "create table a",
}, {
input: "create index a on b",
output: "create index on b",
}, {
input: "create unique index a on b",
output: "create index on b",
}, {
input: "drop table a",
}, {
input: "drop table if exists a",
}, {
input: "drop index if exists a.b",
}, {
input: "show create table t",
}, {
input: "show index from table t",
output: "show index t",
}, {
input: "show table status",
}, {
input: "show tables",
}, {
input: "describe foobar",
output: "show table foobar",
}, {
input: "desc foobar",
output: "show table foobar",
}, {
input: "select /* EQ true */ 1 from t where a = true",
}, {
input: "select /* EQ false */ 1 from t where a = false",
}, {
input: "select /* NE true */ 1 from t where a != true",
}, {
input: "select /* NE false */ 1 from t where a != false",
}, {
input: "select /* LT true */ 1 from t where a < true",
}, {
input: "select /* LT false */ 1 from t where a < false",
}, {
input: "select /* GT true */ 1 from t where a > true",
}, {
input: "select /* GT false */ 1 from t where a > false",
}, {
input: "select /* LE true */ 1 from t where a <= true",
}, {
input: "select /* LE false */ 1 from t where a <= false",
}, {
input: "select /* GE true */ 1 from t where a >= true",
}, {
input: "select /* GE false */ 1 from t where a >= false",
}, {
input: "select /* drop trailing semicolon */ 1 from dual;",
output: "select /* drop trailing semicolon */ 1 from dual",
}, {
input: "select name, group_concat(score) from t group by name",
}, {
input: "select name, group_concat(distinct id, score order by id desc separator ':') from t group by name",
}}
)
func TestValid(t *testing.T) {
for _, tcase := range validSQL {
if tcase.output == "" {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
if err != nil {
t.Errorf("Parse(%q) err: %v, want nil", tcase.input, err)
continue
}
out := String(tree)
if out != tcase.output {
t.Errorf("Parse(%q) = %q, want: %q", tcase.input, out, tcase.output)
}
// This test just exercises the tree walking functionality.
// There's no way automated way to verify that a node calls
// all its children. But we can examine code coverage and
// ensure that all walkSubtree functions were called.
Walk(func(node SQLNode) (bool, error) {
return true, nil
}, tree)
}
}
func TestCaseSensitivity(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "create table A (\n\t`B` int\n)",
output: "create table A (\n\tB int\n)",
}, {
input: "create index b on A",
output: "create index on A",
}, {
input: "alter table A rename to B",
}, {
input: "drop table B",
output: "drop table B",
}, {
input: "drop table if exists B",
}, {
input: "drop index if exists b.A",
}, {
input: "select a from B",
}, {
input: "select A as B from C",
}, {
input: "select B.* from c",
}, {
input: "select B.A from c",
}, {
input: "select * from B as C",
}, {
input: "select * from A.B",
}, {
input: "update A set b = 1",
}, {
input: "update A.B set b = 1",
}, {
input: "select A() from b",
}, {
input: "select A(B, C) from b",
}, {
input: "select A(distinct B, C) from b",
}, {
// IF is an exception. It's always lower-cased.
input: "select IF(B, C) from b",
output: "select if(B, C) from b",
}, {
input: "insert into A(A, B) values (1, 2)",
}, {
input: "CREATE TABLE A (\n\t`A` int\n)",
output: "create table A (\n\tA int\n)",
}}
for _, tcase := range validSQL {
if tcase.output == "" {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
if err != nil {
t.Errorf("input: %s, err: %v", tcase.input, err)
continue
}
out := String(tree)
if out != tcase.output {
t.Errorf("out: %s, want %s", out, tcase.output)
}
}
}
func TestKeywords(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "select a, current_date from t",
output: "select a, current_date from t",
}, {
input: "select replace(a, 'foo', 'bar') from t",
}, {
input: "update t set a = replace('1234', '2', '1')",
}, {
input: "update t set d = adddate(date('2003-12-31 01:02:03'), interval 5 days)",
}, {
input: "insert /* qualified function */ into t(a, b) values (test.PI(), 'b')",
}, {
input: "select /* keyword in qualified id */ * from t join z on t.key = z.key",
output: "select /* keyword in qualified id */ * from t join z on t.`key` = z.`key`",
}}
for _, tcase := range validSQL {
if tcase.output == "" {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
if err != nil {
t.Errorf("input: %s, err: %v", tcase.input, err)
continue
}
out := String(tree)
if out != tcase.output {
t.Errorf("out: %s, want %s", out, tcase.output)
}
}
}
func TestConvert(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "select cast('abc' as date) from t",
}, {
input: "select cast('abc' as char(4)) from t",
}, {
input: "select cast('abc' as char) from t",
}, {
input: "select cast('abc' as nchar(4)) from t",
}, {
input: "select cast('abc' as nchar) from t",
}, {
input: "select cast('abc' as signed) from t",
}, {
input: "select cast('abc' as signed integer) from t",
output: "select cast('abc' as signed) from t",
}, {
input: "select cast('abc' as unsigned) from t",
}, {
input: "select cast('abc' as unsigned integer) from t",
output: "select cast('abc' as unsigned) from t",
}, {
input: "select cast('abc' as decimal(3, 4)) from t",
}, {
input: "select cast('abc' as decimal(4)) from t",
}, {
input: "select cast('abc' as decimal) from t",
}, {
input: "select cast('abc' as date) from t",
}, {
input: "select cast('abc' as time(4)) from t",
}, {
input: "select cast('abc' as time) from t",
}, {
input: "select cast('abc' as datetime(9)) from t",
}, {
input: "select cast('abc' as datetime) from t",
}}
for _, tcase := range validSQL {
if tcase.output == "" {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
if err != nil {
t.Errorf("input: %s, err: %v", tcase.input, err)
continue
}
out := String(tree)
if out != tcase.output {
t.Errorf("out: %s, want %s", out, tcase.output)
}
}
invalidSQL := []struct {
input string
output string
}{{
input: "select cast from t",
output: "syntax error at position 17 near 'from'",
}, {
input: "select cast('foo', decimal) from t",
output: "syntax error at position 19",
}, {
input: "select cast('abc' as datetime(4+9)) from t",
output: "syntax error at position 33",
}, {
input: "select cast('abc', decimal(4+9)) from t",
output: "syntax error at position 19",
}}
for _, tcase := range invalidSQL {
_, err := Parse(tcase.input)
if err == nil || err.Error() != tcase.output {
t.Errorf("%s: %v, want %s", tcase.input, err, tcase.output)
}
}
}
func TestSubStr(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "select substr(a, 1) from t",
}, {
input: "select substr(a, 1, 6) from t",
}}
for _, tcase := range validSQL {
if tcase.output == "" {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
if err != nil {
t.Errorf("input: %s, err: %v", tcase.input, err)
continue
}
out := String(tree)
if out != tcase.output {
t.Errorf("out: %s, want %s", out, tcase.output)
}
}
}
func TestCreateTable(t *testing.T) {
validSQL := []string{
// test all the data types and options
"create table t (\n" +
" col_tinyint tinyint auto_increment,\n" +
" col_tinyint3 tinyint(3) unsigned,\n" +
" col_smallint smallint,\n" +
" col_smallint4 smallint(4) zerofill,\n" +
" col_mediumint mediumint,\n" +
" col_mediumint5 mediumint(5) unsigned not null,\n" +
" col_int int,\n" +
" col_int10 int(10) not null,\n" +
" col_integer integer,\n" +
" col_bigint bigint,\n" +
" col_bigint10 bigint(10) zerofill not null default 10,\n" +
" col_real real,\n" +
" col_real2 real(1,2) not null default 1.23,\n" +
" col_double double,\n" +
" col_double2 double(3,4) not null default 1.23,\n" +
" col_float float,\n" +
" col_float2 float(3,4) not null default 1.23,\n" +
" col_decimal decimal,\n" +
" col_decimal2 decimal(2),\n" +
" col_decimal3 decimal(2,3),\n" +
" col_numeric numeric,\n" +
" col_numeric2 numeric(2),\n" +
" col_numeric3 numeric(2,3),\n" +
" col_date date,\n" +
" col_time time,\n" +
" col_timestamp timestamp,\n" +
" col_datetime datetime,\n" +
" col_year year,\n" +
" col_char char,\n" +
" col_char2 char(2),\n" +
" col_char3 char(3),\n" +
" col_char4 char(4),\n" +
" col_varchar varchar,\n" +
" col_varchar2 varchar(2),\n" +
" col_varchar3 varchar(3),\n" +
" col_varchar4 varchar(4),\n" +
" col_tinyblob tinyblob,\n" +
" col_blob blob,\n" +
" col_mediumblob mediumblob,\n" +
" col_longblob longblob,\n" +
" col_tinytext tinytext,\n" +
" col_text text,\n" +
" col_mediumtext mediumtext,\n" +
" col_longtext longtext,\n" +
" col_text text\n" +
")",
// test defaults
"create table t (\n" +
" i1 int default 1,\n" +
" i2 int default null,\n" +
" f1 float default 1.23,\n" +
" s1 varchar default 'c',\n" +
" s2 varchar default 'this is a string',\n" +
" s3 varchar default null,\n" +
" s4 timestamp default current_timestamp\n" +
")",
// test key field options
"create table t (\n" +
" id int auto_increment primary key,\n" +
" username varchar unique key,\n" +
" email varchar unique,\n" +
" full_name varchar key,\n" +
" time1 timestamp,\n" +
" time2 timestamp default current_timestamp\n" +
")",
// test defining indexes separately
"create table t (\n" +
" id int auto_increment,\n" +
" username varchar,\n" +
" email varchar,\n" +
" full_name varchar,\n" +
" status_nonkeyword varchar,\n" +
" primary key (id),\n" +
" unique key by_username (username),\n" +
" unique by_username2 (username),\n" +
" unique index by_username3 (username),\n" +
" index by_status (status_nonkeyword),\n" +
" key by_full_name (full_name)\n" +
")",
// test that indexes support USING <id>
"create table t (\n" +
" id int auto_increment,\n" +
" username varchar,\n" +
" email varchar,\n" +
" full_name varchar,\n" +
" status_nonkeyword varchar,\n" +
" primary key (id),\n" +
" unique key by_username (username),\n" +
" unique by_username2 (username),\n" +
" unique index by_username3 (username),\n" +