forked from DataDog/go-sqllexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobfuscate_and_normalize_test.go
More file actions
678 lines (670 loc) · 22.1 KB
/
obfuscate_and_normalize_test.go
File metadata and controls
678 lines (670 loc) · 22.1 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
package sqllexer
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestObfuscationAndNormalization(t *testing.T) {
tests := []struct {
input string
expected string
statementMetadata StatementMetadata
lexerOpts []lexerOption
}{
{
input: `DELETE FROM [discount] WHERE [description]=@1`,
expected: `DELETE FROM discount WHERE description = @1`,
statementMetadata: StatementMetadata{
Tables: []string{"discount"},
Comments: []string{},
Commands: []string{"DELETE"},
Procedures: []string{},
Size: 14,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
{
input: "SELECT 1",
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 = 1`,
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 (1, 2) and name IN ARRAY[3, 4]",
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 = 1 AND ha.name = ANY ('3', '4')
`,
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 = '2'",
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 = 'XXX'
-- 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 = 1",
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: "SELECT TRUNC(SYSDATE@!) from dual",
expected: "SELECT TRUNC ( SYSDATE@! ) from dual",
statementMetadata: StatementMetadata{
Tables: []string{"dual"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 10,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSOracle),
},
},
{
input: `
select sql_fulltext from v$sql where force_matching_signature = 1033183797897134935
GROUP BY c.name, force_matching_signature, plan_hash_value
HAVING MAX(last_active_time) > sysdate - :seconds/24/60/60
FETCH FIRST :limit ROWS ONLY`,
expected: "select sql_fulltext from v$sql where force_matching_signature = ? GROUP BY c.name, force_matching_signature, plan_hash_value HAVING MAX ( last_active_time ) > sysdate - :seconds / ? / ? / ? FETCH FIRST :limit ROWS ONLY",
statementMetadata: StatementMetadata{
Tables: []string{"v$sql"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSOracle),
},
},
{
input: "SELECT TABLESPACE_NAME, USED_SPACE, TABLESPACE_SIZE, USED_PERCENT FROM SYS.DBA_TABLESPACE_USAGE_METRICS K WHERE USED_PERCENT > 85",
expected: `SELECT TABLESPACE_NAME, USED_SPACE, TABLESPACE_SIZE, USED_PERCENT FROM SYS.DBA_TABLESPACE_USAGE_METRICS K WHERE USED_PERCENT > ?`,
statementMetadata: StatementMetadata{
Tables: []string{"SYS.DBA_TABLESPACE_USAGE_METRICS"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 38,
},
},
{
input: "SELECT dbms_lob.substr(sql_fulltext, 4000, 1) sql_fulltext FROM sys.dd_session",
expected: "SELECT dbms_lob.substr ( sql_fulltext, ?, ? ) sql_fulltext FROM sys.dd_session",
statementMetadata: StatementMetadata{
Tables: []string{"sys.dd_session"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 20,
},
},
{
input: "begin execute immediate 'alter session set sql_trace=true'; end;",
expected: "begin execute immediate ?; end",
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"BEGIN", "EXECUTE"},
Procedures: []string{},
Size: 12,
},
},
{
// double quoted table name
input: `SELECT * FROM "public"."users" WHERE id = 1`,
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"public.users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
},
{
input: "SELECT * FROM `database`.`table`",
expected: "SELECT * FROM database.table",
statementMetadata: StatementMetadata{
Tables: []string{"database.table"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 20,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSMySQL),
},
},
{
// double quoted table name with non-ascii characters
input: `SELECT * FROM "fóo"."users" WHERE id = 1`,
expected: `SELECT * FROM fóo.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"fóo.users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 16,
},
},
{
// double quoted table name with truncated quotes
input: `SELECT * FROM "fóo"."us`,
expected: `SELECT * FROM "fóo"."us`,
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 6,
},
},
{
// double quoted table name with truncated quotes
input: `SELECT "fun" FROM "`,
expected: `SELECT fun FROM "`,
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 6,
},
},
{
// [] quoted table name
input: `SELECT * FROM [public].[users] WHERE id = 1`,
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"public.users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
// test for .Net tracer
{
// [] quoted table name
input: `SELECT * FROM [public].[users] WHERE id = 1`,
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"public.users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServerAlias1),
},
},
// test for Java tracer
{
// [] quoted table name
input: `SELECT * FROM [public].[users] WHERE id = 1`,
expected: `SELECT * FROM public.users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"public.users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServerAlias2),
},
},
{
input: `CREATE PROCEDURE TestProc AS SELECT * FROM users`,
expected: `CREATE PROCEDURE TestProc AS SELECT * FROM users`,
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"CREATE", "SELECT"},
Procedures: []string{"TestProc"},
Size: 25,
},
},
{
input: "SELECT $func$SELECT * FROM table WHERE ID in ('a', 1, 2)$func$ FROM users",
expected: "SELECT $func$SELECT * FROM table WHERE ID in ( ? )$func$ FROM users",
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: "SELECT $func$INSERT INTO table VALUES ('a', 1, 2)$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: `select "user_id" from "public"."users"`,
expected: `select user_id from public.users`,
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 18,
},
},
{
// boolean and null
input: `SELECT * FROM users where active = true and deleted is FALSE and age is not null and test is NULL`,
expected: `SELECT * FROM users where active = ? and deleted is ? and age is not ? and test is ?`,
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: "SELECT file#, name, bytes, status FROM V$DATAFILE",
expected: "SELECT file#, name, bytes, status FROM V$DATAFILE",
statementMetadata: StatementMetadata{
Tables: []string{"V$DATAFILE"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 16,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSOracle),
},
},
{
input: `SELECT * FROM users WHERE id = 1 # this is a comment`,
expected: `SELECT * FROM users WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{"# this is a comment"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 30,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSMySQL),
},
},
{
input: `SELECT * FROM [世界].[测试] WHERE id = 1`,
expected: `SELECT * FROM 世界.测试 WHERE id = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"世界.测试"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 19,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
{
input: `SET NOCOUNT ON
IF @@OPTIONS & 512 > 0
RAISERROR ('Current user has SET NOCOUNT turned on.', 1, 1)`,
expected: `SET NOCOUNT ON IF @@OPTIONS & ? > ? RAISERROR ( ? )`,
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
Size: 0,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
{
input: `
WITH SILENCES AS (
SELECT LOWER(BASE_TABLE_NAME), CREATED_DT, SILENCE_UNTIL_DT, REASON
,ROW_NUMBER() OVER (PARTITION BY LOWER(BASE_TABLE_NAME) ORDER BY CREATED_DT DESC) AS ROW_NUMBER
FROM REPORTING.GENERAL.SOME_TABLE
WHERE CONTAINS('us1', LOWER(DATACENTER_LABEL))
)
SELECT * FROM SILENCES WHERE ROW_NUMBER = 1;`,
expected: `WITH SILENCES AS ( SELECT LOWER ( BASE_TABLE_NAME ), CREATED_DT, SILENCE_UNTIL_DT, REASON, ROW_NUMBER ( ) OVER ( PARTITION BY LOWER ( BASE_TABLE_NAME ) ORDER BY CREATED_DT DESC ) FROM REPORTING.GENERAL.SOME_TABLE WHERE CONTAINS ( ?, LOWER ( DATACENTER_LABEL ) ) ) SELECT * FROM SILENCES WHERE ROW_NUMBER = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"REPORTING.GENERAL.SOME_TABLE"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 34,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSnowflake),
},
},
{
input: `USE WAREHOUSE "SOME_WAREHOUSE";`,
expected: `USE WAREHOUSE SOME_WAREHOUSE`, // double quoted identifier are not replaced
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"USE"},
Procedures: []string{},
Size: 3,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSnowflake),
},
},
{
input: `SELECT 1 FROM REPORTING.GENERAL.SOME_RANDOM_TABLE
WHERE BASE_TABLE_NAME='xxx_ttt_zzz_v1'
AND DATACENTER_LABEL='us3'
AND CENSUS_ELEMENT_ID='bef52c3f-788f-4fb3-b116-a05a1c4a9792';`,
expected: `SELECT ? FROM REPORTING.GENERAL.SOME_RANDOM_TABLE WHERE BASE_TABLE_NAME = ? AND DATACENTER_LABEL = ? AND CENSUS_ELEMENT_ID = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"REPORTING.GENERAL.SOME_RANDOM_TABLE"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 41,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSnowflake),
},
},
{
input: `COPY INTO REPORTING.GENERAL.MY_TABLE
(FEATURE,DESCRIPTION,COVERAGE,DATE_PARTITION)
FROM (SELECT $1,$2,$3,TO_TIMESTAMP('2023-12-14 00:00:00') FROM @REPORTING.GENERAL.SOME_DESCRIPTIONS/external_data/)
file_format=(type=CSV SKIP_HEADER=1 FIELD_OPTIONALLY_ENCLOSED_BY='\"' ESCAPE_UNENCLOSED_FIELD='\\' FIELD_DELIMITER=',' )
;`,
expected: `COPY INTO REPORTING.GENERAL.MY_TABLE ( FEATURE, DESCRIPTION, COVERAGE, DATE_PARTITION ) FROM ( SELECT $1, $2, $3, TO_TIMESTAMP ( ? ) FROM @REPORTING.GENERAL.SOME_DESCRIPTIONS/external_data/ ) file_format = ( type = CSV SKIP_HEADER = ? FIELD_OPTIONALLY_ENCLOSED_BY = ? ESCAPE_UNENCLOSED_FIELD = ? FIELD_DELIMITER = ? )`,
statementMetadata: StatementMetadata{
Tables: []string{"REPORTING.GENERAL.MY_TABLE", "@REPORTING.GENERAL.SOME_DESCRIPTIONS/external_data/"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 83,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSnowflake),
},
},
{
input: `SELECT EXISTS(
SELECT * FROM REPORTING.INFORMATION_SCHEMA.TABLES
WHERE table_schema='XXX_YYY'
AND table_name='ABC'
AND table_type='EXTERNAL TABLE'
);`,
expected: `SELECT EXISTS ( SELECT * FROM REPORTING.INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ? AND table_type = ? )`,
statementMetadata: StatementMetadata{
Tables: []string{"REPORTING.INFORMATION_SCHEMA.TABLES"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 41,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSnowflake),
},
},
{
input: `ALTER EXTERNAL TABLE REPORTING.TEST.MY_TABLE REFRESH '2024_01_15';`,
expected: `ALTER EXTERNAL TABLE REPORTING.TEST.MY_TABLE REFRESH ?`,
statementMetadata: StatementMetadata{
Tables: []string{"REPORTING.TEST.MY_TABLE"},
Comments: []string{},
Commands: []string{"ALTER"},
Procedures: []string{},
Size: 28,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSnowflake),
},
},
{
input: `SELECT * FROM users where '{"a": 1, "b": 2}'::jsonb <@ '{"a": 1, "b": 2}'::jsonb`,
expected: `SELECT * FROM users where ? :: jsonb <@ '{"a": 1, "b": 2}' :: jsonb`,
statementMetadata: StatementMetadata{
Tables: []string{"users"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 11,
},
},
{
input: `DELETE FROM [discount] WHERE [description]=@1`,
expected: `DELETE FROM discount WHERE description = @1`,
statementMetadata: StatementMetadata{
Tables: []string{"discount"},
Comments: []string{},
Commands: []string{"DELETE"},
Procedures: []string{},
Size: 14,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
{
input: `/*dddbs='mydb',ddpv='1.2.3'*/ ( @p1 bigint ) SELECT * from dbm_user WHERE id = @p1`,
expected: `SELECT * from dbm_user WHERE id = @p1`,
statementMetadata: StatementMetadata{
Tables: []string{"dbm_user"},
Comments: []string{"/*dddbs='mydb',ddpv='1.2.3'*/"},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 43,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
{
input: `( @p1 varchar(50) ) SELECT * from dbm_user WHERE name = @p1`,
expected: `SELECT * from dbm_user WHERE name = @p1`,
statementMetadata: StatementMetadata{
Tables: []string{"dbm_user"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 14,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSSQLServer),
},
},
{
input: `SELECT pk, updatedAt, createdAt, name, description, isAutoCreated, autoCreatedFeaturePk FROM FeatureStrategyGroup WHERE FeatureStrategyGroup.autoCreatedFeaturePk IN ( ? )`,
expected: `SELECT pk, updatedAt, createdAt, name, description, isAutoCreated, autoCreatedFeaturePk FROM FeatureStrategyGroup WHERE FeatureStrategyGroup.autoCreatedFeaturePk IN ( ? )`,
statementMetadata: StatementMetadata{
Tables: []string{"FeatureStrategyGroup"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 26,
},
lexerOpts: []lexerOption{
WithDBMS("postgres"),
},
},
{
// Test for parallel range-partitioned UNION ALL query
input: `(SELECT * FROM dbm_user WHERE (id >= 0 AND id < 10000)) UNION ALL (SELECT * FROM dbm_user WHERE (id >= 10000 AND id < 20000)) UNION ALL (SELECT * FROM dbm_user WHERE (id >= 20000 AND id < 30000)) UNION ALL (SELECT * FROM dbm_user WHERE (id >= 30000 AND id < 40000)) UNION ALL (SELECT * FROM dbm_user WHERE (id >= 40000 AND id < 50000)) ORDER BY id;`,
expected: `( SELECT * FROM dbm_user WHERE ( id >= ? AND id < ? ) ) UNION ALL ( SELECT * FROM dbm_user WHERE ( id >= ? AND id < ? ) ) UNION ALL ( SELECT * FROM dbm_user WHERE ( id >= ? AND id < ? ) ) UNION ALL ( SELECT * FROM dbm_user WHERE ( id >= ? AND id < ? ) ) UNION ALL ( SELECT * FROM dbm_user WHERE ( id >= ? AND id < ? ) ) ORDER BY id`,
statementMetadata: StatementMetadata{
Tables: []string{"dbm_user"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 14,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSMySQL),
},
},
{
input: `select * from dbmorders.dbm_user where nickname = "foo";`,
expected: `select * from dbmorders.dbm_user where nickname = ?`,
statementMetadata: StatementMetadata{
Tables: []string{"dbmorders.dbm_user"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 24,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSMySQL),
},
},
{
input: `SELECT * FROM t WHERE a = :p1 AND b IN (1, 2) OR c = :p2`,
expected: `SELECT * FROM t WHERE a = :p? AND b IN ( ? ) OR c = :p?`,
statementMetadata: StatementMetadata{
Tables: []string{"t"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 7,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSPostgres),
},
},
{
input: `select foo from bar where baz = :"SYS_B_1"`,
expected: `select foo from bar where baz = :SYS_B_?`,
statementMetadata: StatementMetadata{
Tables: []string{"bar"},
Comments: []string{},
Commands: []string{"SELECT"},
Procedures: []string{},
Size: 9,
},
lexerOpts: []lexerOption{
WithDBMS(DBMSOracle),
},
},
}
obfuscator := NewObfuscator(
WithReplaceDigits(true),
WithReplaceBoolean(true),
WithReplaceNull(true),
WithDollarQuotedFunc(true),
WithKeepJsonPath(true),
)
normalizer := NewNormalizer(
WithCollectComments(true),
WithCollectCommands(true),
WithCollectTables(true),
WithCollectProcedures(true),
WithKeepSQLAlias(false),
)
for _, test := range tests {
t.Run("", func(t *testing.T) {
got, statementMetadata, err := ObfuscateAndNormalize(test.input, obfuscator, normalizer, test.lexerOpts...)
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
assertStatementMetadataEqual(t, &test.statementMetadata, statementMetadata)
})
}
}
// TestObfuscateAndNormalizeDoesNotPinLargeBackingArrays verifies that the ObfuscateAndNormalize
// function returns strings that don't hold references to excessively large backing arrays.
func TestObfuscateAndNormalizeDoesNotPinLargeBackingArrays(t *testing.T) {
obfuscator := NewObfuscator()
normalizer := NewNormalizer(
WithCollectComments(true),
WithCollectCommands(true),
WithCollectTables(true),
WithCollectProcedures(true),
)
RunBackingArrayTests(t, "ObfuscateAndNormalize", func(input string) (BackingArrayTestResult, error) {
sql, metadata, err := ObfuscateAndNormalize(input, obfuscator, normalizer)
return BackingArrayTestResult{SQL: sql, Metadata: metadata}, err
})
}