-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnormalizer_test.go
More file actions
1577 lines (1540 loc) · 47.5 KB
/
normalizer_test.go
File metadata and controls
1577 lines (1540 loc) · 47.5 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
package sqllexer
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNormalizer(t *testing.T) {
tests := []struct {
input string
expected string
statementMetadata StatementMetadata
}{
{
input: "SELECT ?",
expected: "SELECT ?",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 6,
},
},
{
input: `
/*dddbs='orders-mysql',dde='dbm-agent-integration',ddps='orders-app',ddpv='7825a16',traceparent='00-000000000000000068e229d784ee697c-569d1b940c1fb3ac-00'*/
/* date='12%2F31',key='val' */
SELECT * FROM users WHERE id = ?`,
expected: "SELECT * FROM users WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{"/*dddbs='orders-mysql',dde='dbm-agent-integration',ddps='orders-app',ddpv='7825a16',traceparent='00-000000000000000068e229d784ee697c-569d1b940c1fb3ac-00'*/", "/* date='12%2F31',key='val' */"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 196,
},
},
{
input: "SELECT * FROM users WHERE id IN (?, ?) and name IN ARRAY[?, ?]",
expected: "SELECT * FROM users WHERE id IN ( ? ) and name IN ARRAY [ ? ]",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: `
SELECT h.id, h.org_id, h.name, ha.name as alias, h.created
FROM vs?.host h
JOIN vs?.host_alias ha on ha.host_id = h.id
WHERE ha.org_id = ? AND ha.name = ANY ( ?, ? )
`,
expected: "SELECT h.id, h.org_id, h.name, ha.name, h.created FROM vs?.host h JOIN vs?.host_alias ha on ha.host_id = h.id WHERE ha.org_id = ? AND ha.name = ANY ( ? )",
statementMetadata: StatementMetadata{
Tables: []string{"vs?.host", "vs?.host_alias"},
Comments: []string{},
Commands: []string{"SELECT", "JOIN"},
Procedures: []string{},
Size: 32,
},
},
{
input: "/* this is a comment */ SELECT * FROM users WHERE id = ?",
expected: "SELECT * FROM users WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{"/* this is a comment */"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 34,
},
},
{
input: `
/* this is a ` + `
multiline comment */
SELECT * FROM users /* comment comment */ WHERE id = ?
-- this is another comment
`,
expected: "SELECT * FROM users WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{"/* this is a \nmultiline comment */", "/* comment comment */", "-- this is another comment"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 92,
},
},
{
input: "SELECT u.id as ID, u.name as Name FROM users as u WHERE u.id = ?",
expected: "SELECT u.id, u.name FROM users WHERE u.id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: "UPDATE users SET name = (SELECT name FROM test_users WHERE id = ?) WHERE id = ?",
expected: "UPDATE users SET name = ( SELECT name FROM test_users WHERE id = ? ) WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users", "test_users"},
Comments: []string{},
Commands: []string{"UPDATE", "SELECT"},
Procedures: []string{},
Size: 27,
},
},
{
input: `
INSERT INTO order_status_change ( dbm_order_id, message, price, state )
VALUES ( (
SELECT id as dbm_order_id
FROM dbm_order
WHERE id = ?
) (
-- random comment
SELECT ( t.price * t.quantity * d.discount_percent ) AS price
FROM dbm_order o
JOIN order_item t ON o.id = t.dbm_order_id
JOIN discount d ON d.dbm_item_id = t.id
WHERE o.id = ?
LIMIT ?
) )`,
expected: "INSERT INTO order_status_change ( dbm_order_id, message, price, state ) VALUES ( ( SELECT id FROM dbm_order WHERE id = ? ) ( SELECT ( t.price * t.quantity * d.discount_percent ) FROM dbm_order o JOIN order_item t ON o.id = t.dbm_order_id JOIN discount d ON d.dbm_item_id = t.id WHERE o.id = ? LIMIT ? ) )",
statementMetadata: StatementMetadata{
Tables: []string{"order_status_change", "dbm_order", "order_item", "discount"},
Comments: []string{"-- random comment"},
Commands: []string{"INSERT", "SELECT", "JOIN"},
Procedures: []string{},
Size: 79,
},
},
{
input: "DELETE FROM users WHERE id IN (?, ?)",
expected: "DELETE FROM users WHERE id IN ( ? )",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"DELETE"},
Procedures: []string{},
Size: 11,
},
},
{
input: `
CREATE PROCEDURE test_procedure()
BEGIN
SELECT * FROM users WHERE id = ?;
Update test_users set name = ? WHERE id = ?;
Delete FROM user? WHERE id = ?;
END;
`,
expected: "CREATE PROCEDURE test_procedure ( ) BEGIN SELECT * FROM users WHERE id = ?; Update test_users set name = ? WHERE id = ?; Delete FROM user? WHERE id = ?; END",
statementMetadata: StatementMetadata{
Tables: []string{"users", "test_users", "user?"},
Comments: []string{},
Commands: []string{"CREATE", "BEGIN", "SELECT", "UPDATE", "DELETE"},
Procedures: []string{},
Size: 49,
},
},
{
input: `
SELECT org_id, resource_type, meta_key, meta_value
FROM public.schema_meta
WHERE org_id IN ( ? ) AND resource_type IN ( ? ) AND meta_key IN ( ? )
`,
expected: "SELECT org_id, resource_type, meta_key, meta_value FROM public.schema_meta WHERE org_id IN ( ? ) AND resource_type IN ( ? ) AND meta_key IN ( ? )",
statementMetadata: StatementMetadata{
Tables: []string{"public.schema_meta"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 24,
},
},
{
input: `
WITH cte AS (
SELECT id, name, age
FROM person
WHERE age > ?
)
UPDATE person
SET age = ?
WHERE id IN (SELECT id FROM cte);
INSERT INTO person (name, age)
SELECT name, ?
FROM cte
WHERE age <= ?;
`,
expected: "WITH cte AS ( SELECT id, name, age FROM person WHERE age > ? ) UPDATE person SET age = ? WHERE id IN ( SELECT id FROM cte ); INSERT INTO person ( name, age ) SELECT name, ? FROM cte WHERE age <= ?",
statementMetadata: StatementMetadata{
Tables: []string{"person"},
Comments: []string{},
Commands: []string{"SELECT", "UPDATE", "INSERT"},
Procedures: []string{},
Size: 24,
},
},
{
input: "WITH updates AS ( UPDATE metrics_metadata SET metric_type = ? updated = ? :: timestamp, interval = ? unit_id = ? per_unit_id = ? description = ? orientation = ? integration = ? short_name = ? WHERE metric_key = ? AND org_id = ? RETURNING ? ) INSERT INTO metrics_metadata ( org_id, metric_key, metric_type, interval, unit_id, per_unit_id, description, orientation, integration, short_name ) SELECT ? WHERE NOT EXISTS ( SELECT ? FROM updates )",
expected: "WITH updates AS ( UPDATE metrics_metadata SET metric_type = ? updated = ? :: timestamp, interval = ? unit_id = ? per_unit_id = ? description = ? orientation = ? integration = ? short_name = ? WHERE metric_key = ? AND org_id = ? RETURNING ? ) INSERT INTO metrics_metadata ( org_id, metric_key, metric_type, interval, unit_id, per_unit_id, description, orientation, integration, short_name ) SELECT ? WHERE NOT EXISTS ( SELECT ? FROM updates )",
statementMetadata: StatementMetadata{
Tables: []string{"metrics_metadata"},
Comments: []string{},
Commands: []string{"UPDATE", "INSERT", "SELECT"},
Procedures: []string{},
Size: 34,
},
},
{
input: `
/* Multi-line comment */
SELECT * FROM clients WHERE (clients.first_name = ?) LIMIT ? BEGIN INSERT INTO owners (created_at, first_name, locked, orders_count, updated_at) VALUES (?, ?, ?, ?, ?) COMMIT`,
expected: "SELECT * FROM clients WHERE ( clients.first_name = ? ) LIMIT ? BEGIN INSERT INTO owners ( created_at, first_name, locked, orders_count, updated_at ) VALUES ( ? ) COMMIT",
statementMetadata: StatementMetadata{
Tables: []string{"clients", "owners"},
Comments: []string{"/* Multi-line comment */"},
Commands: []string{"SELECT", "BEGIN", "INSERT", "COMMIT"},
Procedures: []string{},
Size: 60,
},
},
{
input: `-- Single line comment
-- Another single line comment
-- Another another single line comment
GRANT USAGE, DELETE ON SCHEMA datadog TO datadog`,
expected: "GRANT USAGE, DELETE ON SCHEMA datadog TO datadog",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{"-- Single line comment", "-- Another single line comment", "-- Another another single line comment"},
Commands: []string{"GRANT", "DELETE"},
Procedures: []string{},
Size: 101,
},
},
{
input: `-- Testing table value constructor SQL expression
SELECT * FROM (VALUES (?, ?)) AS d (id, animal)`,
expected: "SELECT * FROM ( VALUES ( ? ) ) ( id, animal )",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{"-- Testing table value constructor SQL expression"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 55,
},
},
{
input: `ALTER TABLE tabletest DROP COLUMN columna`,
expected: "ALTER TABLE tabletest DROP COLUMN columna",
statementMetadata: StatementMetadata{
Tables: []string{"tabletest"},
Comments: []string{},
Commands: []string{"ALTER", "DROP"},
Procedures: []string{},
Size: 18,
},
},
{
input: `REVOKE ALL ON SCHEMA datadog FROM datadog`,
expected: "REVOKE ALL ON SCHEMA datadog FROM datadog",
statementMetadata: StatementMetadata{
Tables: []string{"datadog"},
Comments: []string{},
Commands: []string{"REVOKE"},
Procedures: []string{},
Size: 13,
},
},
{
input: "/* Testing explicit table SQL expression */ WITH T1 AS (SELECT PNO , PNAME , COLOR , WEIGHT , CITY FROM P WHERE CITY = ?), T2 AS (SELECT PNO, PNAME, COLOR, WEIGHT, CITY, ? * WEIGHT AS NEW_WEIGHT, ? AS NEW_CITY FROM T1), T3 AS ( SELECT PNO , PNAME, COLOR, NEW_WEIGHT AS WEIGHT, NEW_CITY AS CITY FROM T2), T4 AS ( TABLE P EXCEPT CORRESPONDING TABLE T1) TABLE T4 UNION CORRESPONDING TABLE T3",
expected: "WITH T1 AS ( SELECT PNO, PNAME, COLOR, WEIGHT, CITY FROM P WHERE CITY = ? ), T2 AS ( SELECT PNO, PNAME, COLOR, WEIGHT, CITY, ? * WEIGHT, ? FROM T1 ), T3 AS ( SELECT PNO, PNAME, COLOR, NEW_WEIGHT, NEW_CITY FROM T2 ), T4 AS ( TABLE P EXCEPT CORRESPONDING TABLE T1 ) TABLE T4 UNION CORRESPONDING TABLE T3",
statementMetadata: StatementMetadata{
Tables: []string{"P", "T2", "T4", "T3"},
Comments: []string{"/* Testing explicit table SQL expression */"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 56,
},
},
{
// truncated
input: "SELECT * FROM users WHERE id =",
expected: "SELECT * FROM users WHERE id =",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: "SELECT d.id, d.uuid, d.org_id, d.creator_id, d.updater_id, d.monitor_id, d.parent_id, d.original_parent_id, d.scope, d.start_dt, d.end_dt, d.canceled_dt, d.active, d.disabled, d.created, d.modified, d.message, d.monitor_tags, d.recurrence, d.mute_first_recovery_notification, d.scope_v2_query, d.scope_v2 FROM monitor_downtime d, org o WHERE o.id = d.org_id AND d.modified >= ? AND o.partition_num = ANY (?, ?, ?)",
expected: "SELECT d.id, d.uuid, d.org_id, d.creator_id, d.updater_id, d.monitor_id, d.parent_id, d.original_parent_id, d.scope, d.start_dt, d.end_dt, d.canceled_dt, d.active, d.disabled, d.created, d.modified, d.message, d.monitor_tags, d.recurrence, d.mute_first_recovery_notification, d.scope_v2_query, d.scope_v2 FROM monitor_downtime d, org o WHERE o.id = d.org_id AND d.modified >= ? AND o.partition_num = ANY ( ? )",
statementMetadata: StatementMetadata{
Tables: []string{"monitor_downtime", "org"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 25,
},
},
{
input: "SELECT set_host_tags_bigint (? ARRAY[?, ?, ?])",
expected: "SELECT set_host_tags_bigint ( ? ARRAY [ ? ] )",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 6,
},
},
{
input: "/* ok comment */ UPDATE /*foo comment*/ table_name SET column_name = ? WHERE column_name = ?",
expected: "UPDATE table_name SET column_name = ? WHERE column_name = ?",
statementMetadata: StatementMetadata{
Tables: []string{"table_name"},
Comments: []string{"/* ok comment */", "/*foo comment*/"},
Commands: []string{"UPDATE"},
Procedures: []string{},
Size: 47,
},
},
{
input: "SELECT * FROM users WHERE id IN (?, ?)",
expected: `SELECT * FROM users WHERE id IN ( ? )`,
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: "SELECT $func$INSERT INTO table VALUES (?,?,?)$func$ FROM users",
expected: "SELECT $func$INSERT INTO table VALUES ( ? )$func$ FROM users",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: `DROP TABLE IF EXISTS users`,
expected: `DROP TABLE IF EXISTS users`,
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"DROP"},
Procedures: []string{},
Size: 9,
},
},
{
input: "SELECT * FROM ONLY users WHERE id = ?",
expected: "SELECT * FROM ONLY users WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
// Multi-byte UTF-8 characters should be preserved, not split into bytes.
// This is a regression test for scanUnknown() byte-splitting bug.
{
input: "SELECT a, b FROM t", // Full-width comma U+FF0C
expected: "SELECT a , b FROM t",
statementMetadata: StatementMetadata{
Tables: []string{"t"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 7,
},
},
{
input: "SELECT a, b, c FROM t", // Mixed ASCII and full-width comma
expected: "SELECT a, b , c FROM t",
statementMetadata: StatementMetadata{
Tables: []string{"t"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 7,
},
},
}
normalizer := NewNormalizer(
WithCollectComments(true),
WithCollectCommands(true),
WithCollectTables(true),
WithKeepSQLAlias(false),
)
for _, test := range tests {
t.Run("", func(t *testing.T) {
got, statementMetadata, err := normalizer.Normalize(test.input)
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
assertStatementMetadataEqual(t, &test.statementMetadata, statementMetadata)
})
}
}
func TestNormalizerNotCollectMetadata(t *testing.T) {
tests := []struct {
input string
expected string
statementMetadata StatementMetadata
}{
{
input: "SELECT ?",
expected: "SELECT ?",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
Size: 0,
},
},
{
input: "SELECT * FROM users WHERE id = ?",
expected: "SELECT * FROM users WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
Size: 0,
},
},
{
input: "SELECT id as ID, name as Name FROM users WHERE id IN (?, ?)",
expected: "SELECT id as ID, name as Name FROM users WHERE id IN ( ? )",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
Size: 0,
},
},
{
input: `TRUNCATE TABLE datadog`,
expected: "TRUNCATE TABLE datadog",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
Size: 0,
},
},
}
normalizer := NewNormalizer(
WithCollectComments(false),
WithCollectCommands(false),
WithCollectTables(false),
WithKeepSQLAlias(true),
)
for _, test := range tests {
t.Run("", func(t *testing.T) {
got, statementMetadata, err := normalizer.Normalize(test.input)
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
assertStatementMetadataEqual(t, &test.statementMetadata, statementMetadata)
})
}
}
func TestNormalizerFormatting(t *testing.T) {
tests := []struct {
queries []string
expected string
uppercaseKeywords bool
}{
{
queries: []string{
"SELECT id,name, address FROM users where id = ?",
"SELECT id, name, address FROM users where id = ?",
"SELECT id,name, address FROM users where id =?",
"SELECT id as ID, name as Name, address FROM users where id = ?",
},
expected: "SELECT id, name, address FROM users where id = ?",
},
{
queries: []string{
"SELECT id,name, address FROM users where id IN (?, ?,?, ?)",
"SELECT id, name, address FROM users where id IN ( ? )",
"SELECT id, name, address FROM users where id IN ( ? )",
"SELECT id, name, address FROM users where id IN (?,?,?)",
},
expected: "SELECT id, name, address FROM users where id IN ( ? )",
},
{
queries: []string{
"SELECT * FROM discount where description LIKE ?",
"select * from discount where description LIKE ?",
"select * from discount where description like ?",
},
expected: "SELECT * FROM discount WHERE description LIKE ?",
uppercaseKeywords: true,
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
normalizer := NewNormalizer(
WithCollectComments(false),
WithUppercaseKeywords(test.uppercaseKeywords),
)
for _, query := range test.queries {
got, _, err := normalizer.Normalize(query)
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
}
})
}
}
func TestNormalizerRepeatedExecution(t *testing.T) {
// This test is to ensure that repeated executions of the normalizer
// should always produce the same normalized SQL.
// This is also a good test to normalize a previous normalized SQL.
tests := []struct {
input string
expected string
statementMetadata StatementMetadata
}{
{
input: "SELECT * FROM users WHERE id = ?",
expected: "SELECT * FROM users WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: "SELECT * FROM users WHERE id IN (?, ?)",
expected: "SELECT * FROM users WHERE id IN ( ? )",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: `
/* this is a comment */
SELECT h.id, h.org_id, h.name, ha.name as alias, h.created`,
expected: "SELECT h.id, h.org_id, h.name, ha.name, h.created",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{"/* this is a comment */"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 29,
},
},
{
input: "SELECT u.id as ID, u.name as Name FROM users as u WHERE u.id = ?",
expected: "SELECT u.id, u.name FROM users WHERE u.id = ?",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
}
normalizer := NewNormalizer(
WithCollectCommands(true),
WithCollectTables(true),
WithCollectComments(true),
)
for _, test := range tests {
t.Run("", func(t *testing.T) {
input := test.input
for i := 0; i < 10; i++ {
got, statementMetadata, err := normalizer.Normalize(input)
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
assert.Equal(t, test.statementMetadata.Commands, statementMetadata.Commands)
assert.Equal(t, test.statementMetadata.Tables, statementMetadata.Tables)
// comments are stripped after the first execution
if i == 0 {
assert.Equal(t, test.statementMetadata.Comments, statementMetadata.Comments)
} else {
assert.Equal(t, []string{}, statementMetadata.Comments)
}
input = got
}
})
}
}
func TestNormalizeDeobfuscatedSQL(t *testing.T) {
// This test is to ensure that normalizer works with deobfuscated SQL.
// This is important to allow users to opt out of obfuscation.
tests := []struct {
input string
expected string
statementMetadata StatementMetadata
normalizationConfig *normalizerConfig
lexerOptions []lexerOption
}{
{
input: "SELECT id,name, address FROM users where id = 1",
expected: "SELECT id, name, address FROM users where id = 1",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
},
},
{
input: "SELECT id,name, address FROM users where id IN (1, 2, 3, 4)",
expected: "SELECT id, name, address FROM users where id IN ( 1, 2, 3, 4 )",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
},
},
{
input: `
/* test comment */
SELECT id,name, address FROM users where id IN (1, 2, 3, 4)`,
expected: "SELECT id, name, address FROM users where id IN ( 1, 2, 3, 4 )",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{"/* test comment */"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 29,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
},
},
{
// comments should be stripped even when CollectComments is false
input: `
/* test comment */
SELECT id,name, address FROM users where id IN (1, 2, 3, 4)`,
expected: "SELECT id, name, address FROM users where id IN ( 1, 2, 3, 4 )",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: false,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
},
},
{
input: `
/* this is a comment */
SELECT h.id, h.org_id, h.name, ha.name as alias, h.created`,
expected: "SELECT h.id, h.org_id, h.name, ha.name, h.created",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{"/* this is a comment */"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 29,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
},
},
{
input: "SELECT u.id as ID, u.name as Name FROM users as u WHERE u.id = '123'",
expected: "SELECT u.id, u.name FROM users WHERE u.id = '123'",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
},
},
{
input: "SELECT u.id as ID, u.name as Name FROM users as u WHERE u.id = '123'",
expected: "SELECT u.id as ID, u.name as Name FROM users as u WHERE u.id = '123'",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
},
},
{
input: `SELECT * FROM "users" WHERE id = ?`,
expected: `SELECT * FROM users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
},
},
{
input: `SELECT * FROM "users" WHERE id = ?`,
expected: `SELECT * FROM "users" WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
KeepIdentifierQuotation: true,
},
},
{
input: `SELECT * FROM public."users" WHERE id = ?`,
expected: `SELECT * FROM public."users" WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`public."users"`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 20,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
KeepIdentifierQuotation: true,
},
},
{
input: `SELECT * FROM "users" WHERE id = ?`,
expected: `SELECT * FROM users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
Size: 0,
},
normalizationConfig: &normalizerConfig{
CollectComments: false,
CollectCommands: false,
CollectTables: false,
KeepSQLAlias: false,
KeepIdentifierQuotation: false,
},
},
// Multi-byte UTF-8 quoted identifiers should have quotes stripped correctly.
// This is a regression test for scanDoubleQuotedIdentifier() byte-splitting bug.
{
input: `SELECT * FROM "über" WHERE id = ?`,
expected: `SELECT * FROM über WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`über`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11, // über(5 bytes) + SELECT(6 bytes)
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
KeepIdentifierQuotation: false,
},
},
{
input: `SELECT * FROM "世界" WHERE id = ?`,
expected: `SELECT * FROM 世界 WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`世界`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 12, // 世界(6 bytes) + SELECT(6 bytes)
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: false,
KeepIdentifierQuotation: false,
},
},
{
input: `SELECT a AS "café" FROM t`,
expected: `SELECT a AS café FROM t`,
statementMetadata: StatementMetadata{
Tables: []string{`t`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 7, // t(1 byte) + SELECT(6 bytes)
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
KeepIdentifierQuotation: false,
},
},
{
input: `SELECT mytable.mycol AS "mytable.mycol" FROM mytable`,
expected: `SELECT mytable.mycol AS "mytable.mycol" FROM mytable`,
statementMetadata: StatementMetadata{
Tables: []string{`mytable`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 13,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
KeepIdentifierQuotation: false,
},
},
{
input: `SELECT * FROM "public"."users" WHERE id = ?`,
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
},
},
{
input: `SELECT * FROM "public"."users" WHERE id = ?`,
expected: `SELECT * FROM "public"."users" WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
KeepIdentifierQuotation: true,
},
},
{
input: "SELECT * FROM `public`.`users` WHERE id = ?",
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
},
lexerOptions: []lexerOption{
WithDBMS(DBMSMySQL),
},
},
{
input: "SELECT * FROM `public`.`users` WHERE id = ?",
expected: "SELECT * FROM `public`.`users` WHERE id = ?",
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
KeepIdentifierQuotation: true,
},
lexerOptions: []lexerOption{
WithDBMS(DBMSMySQL),
},
},
{
input: `SELECT * FROM [public].[users] WHERE id = ?`,
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
normalizationConfig: &normalizerConfig{
CollectComments: true,
CollectCommands: true,
CollectTables: true,
KeepSQLAlias: true,
},
lexerOptions: []lexerOption{
WithDBMS(DBMSSQLServer),