Debian Codegraph @

query/query_test.go

raw symbols SCIP
1 /*
2 Copyright (c) 2019, Percona LLC.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 package query_test
32
33 import (
34 "testing"
35
36 "github.com/stretchr/testify/assert"
37
38 "github.com/percona/go-mysql/query"
39 _ "github.com/percona/go-mysql/test"
40 )
41
42 func TestFingerprintBasic(t *testing.T) {
43 type testCase struct {
44 name string
45 query string
46 expected string
47 }
48 // Test cases for fingerprinting SQL queries
49 testCases := []testCase{
50 {
51 name: "Basic SELECT",
52 query: "SELECT c FROM t WHERE id=1",
53 expected: "select c from t where id=?",
54 },
55 {
56 name: "UPDATE with value similar to one-line-comment",
57 query: `UPDATE groups_search SET charter = ' -------3\'\' XXXXXXXXX.\n \n -----------------------------------------------------', show_in_list = 'Y' WHERE group_id='aaaaaaaa'`,
58 expected: "update groups_search set charter = ?, show_in_list = ? where group_id=?",
59 },
60 {
61 // PT treats this as "mysqldump", but we don't do any special fingerprints.
62 name: "no special fingerprint",
63 query: "SELECT /*!40001 SQL_NO_CACHE */ * FROM `film`",
64 expected: "select /*!40001 sql_no_cache */ * from `film`",
65 },
66 {
67 name: "stored procedure calls",
68 query: "CALL foo(1, 2, 3)",
69 expected: "call foo",
70 },
71 {
72 name: "fingerprint admin commands as themselves",
73 query: "administrator command: Init DB",
74 expected: "administrator command: Init DB",
75 },
76 {
77 name: "remove identifier from USE",
78 query: "use `foo`",
79 expected: "use ?",
80 },
81 {
82 name: "handle bug from perlmonks thread 728718",
83 query: "select null, 5.001, 5001. from foo",
84 expected: "select ?, ?, ? from foo",
85 },
86 {
87 name: "quoted strings",
88 query: "select 'hello', '\nhello\n', \"hello\", '\\'' from foo",
89 expected: "select ?, ?, ?, ? from foo",
90 },
91 {
92 name: "handles trailing newline",
93 query: "select 'hello'\n",
94 expected: "select ?",
95 },
96 {
97 name: "handle backslashes in values",
98 query: "select '\\\\' from foo", // interpreted as "select '\\' from foo"
99 expected: "select ? from foo",
100 },
101 {
102 name: "collapse whitespace",
103 query: "select foo",
104 expected: "select foo",
105 },
106 {
107 name: "lowercases, replaces integer",
108 query: "SELECT * from foo where a = 5",
109 expected: "select * from foo where a = ?",
110 },
111 {
112 name: "handles floating point numbers",
113 query: "select 0e0, +6e-30, -6.00 from foo where a = 5.5 or b=0.5 or c=.5",
114 expected: "select ?, ?, ? from foo where a = ? or b=? or c=?",
115 },
116 {
117 name: "hex/bit",
118 query: "select 0x0, x'123', 0b1010, b'10101' from foo",
119 expected: "select ?, ?, ?, ? from foo",
120 },
121 {
122 name: "collapses whitespaces",
123 query: " select * from\nfoo where a = 5",
124 expected: "select * from foo where a = ?",
125 },
126 {
127 name: "IN lists with spaces",
128 query: "select * from foo where a in (5) and b in (5, 8,9 ,9 , 10)",
129 expected: "select * from foo where a in(?+) and b in(?+)",
130 },
131 {
132 // Numeric table names. By default, PT will return foo_n, etc. because
133 // match_embedded_numbers is false by default for speed.
134 name: "numeric table names",
135 query: "select foo_1 from foo_2_3",
136 expected: "select foo_1 from foo_2_3",
137 },
138 {
139 name: "numeric table name prefixes",
140 query: "select 123foo from 123foo",
141 expected: "select 123foo from 123foo",
142 },
143 {
144 name: "numeric table name prefixes with underscores",
145 query: "select 123_foo from 123_foo",
146 expected: "select 123_foo from 123_foo",
147 },
148 {
149 name: "string that needs no changes",
150 query: "insert into abtemp.coxed select foo.bar from foo",
151 expected: "insert into abtemp.coxed select foo.bar from foo",
152 },
153 {
154 name: "limit alone",
155 query: "select * from foo limit 5",
156 expected: "select * from foo limit ?",
157 },
158 {
159 name: "limit with comma-offset",
160 query: "select * from foo limit 5, 10",
161 expected: "select * from foo limit ?, ?",
162 },
163 {
164 name: "limit with offset",
165 query: "select * from foo limit 5 offset 10",
166 expected: "select * from foo limit ? offset ?",
167 },
168 {
169 name: "insert with quoted keyword as column",
170 query: "INSERT INTO test (ID, `Value`) VALUES (1 ,1)",
171 expected: "insert into test (id, `value`) values(?+)",
172 },
173 {
174 name: "insert with non-quoted keyword as column",
175 query: "INSERT INTO test (ID, Value) VALUES (1 ,1)",
176 expected: "insert into test (id, value) values(?+)",
177 },
178 {
179 name: "insert with non-quoted keyword and space as column",
180 query: "INSERT INTO test (ID, Value ) VALUES (1 ,1)",
181 expected: "insert into test (id, value ) values(?+)",
182 },
183 {
184 name: "insert with keyword as first column name",
185 query: "INSERT INTO test ( In , ID) VALUES (1,1)",
186 expected: "insert into test ( in , id) values(?+)",
187 },
188 {
189 name: "insert with value keyword as first column name",
190 query: "INSERT INTO test ( In , ID) VALUES (1,1)",
191 expected: "insert into test ( in , id) values(?+)",
192 },
193 {
194 name: "insert duplicate with keyword as column name",
195 query: "INSERT INTO test (id, value) VALUES (1, 10) ON DUPLICATE KEY UPDATE value = VALUES(value) + 5",
196 expected: "insert into test (id, value) values(?+) on duplicate key update value = values(value) + ?",
197 },
198 {
199 name: "insert duplicate with keyword and space as column name",
200 query: "INSERT INTO test (id, value ) VALUES (1, 10) ON DUPLICATE KEY UPDATE value = VALUES(value ) + 5",
201 expected: "insert into test (id, value ) values(?+) on duplicate key update value = values(value ) + ?",
202 },
203 {
204 name: "fingerprint load data infile",
205 query: "LOAD DATA INFILE '/tmp/foo.txt' INTO db.tbl",
206 expected: "load data infile ? into db.tbl",
207 },
208 {
209 name: "fingerprint db.tbl<number>name (preserve number()",
210 query: "SELECT * FROM prices.rt_5min where id=1",
211 expected: "select * from prices.rt_5min where id=?",
212 },
213 {
214 name: "fingerprint /* -- comment */ SELECT", // bug 1174956
215 query: "/* -- S++ SU ABORTABLE -- spd_user: rspadim */SELECT SQL_SMALL_RESULT SQL_CACHE DISTINCT centro_atividade FROM est_dia WHERE unidade_id=1001 AND item_id=67 AND item_id_red=573",
216 expected: "select sql_small_result sql_cache distinct centro_atividade from est_dia where unidade_id=? and item_id=? and item_id_red=?",
217 },
218 {
219 name: "fingerprint INSERT INTO t (ts) VALUES (NOW())",
220 query: "INSERT INTO t (ts) VALUES (NOW())",
221 expected: "insert into t (ts) values(?+)",
222 },
223 {
224 name: "fingerprint INSERT INTO t (ts) VALUES ('()', '\\(', '\\)')",
225 query: "INSERT INTO t (ts) VALUES ('()', '\\(', '\\)')",
226 expected: "insert into t (ts) values(?+)",
227 },
228 {
229 name: "select with backticks",
230 query: "SELECT `col` FROM `table-1` WHERE `id` = 5",
231 expected: "select `col` from `table-1` where `id` = ?",
232 },
233 }
234
235 for _, tc := range testCases {
236 t.Run(tc.name, func(t *testing.T) {
237 assert.Equal(t, tc.expected, query.Fingerprint(tc.query))
238 })
239 }
240 }
241
242 func TestFingerprintValueList(t *testing.T) {
243 var q string
244
245 // VALUES lists
246 q = "insert into foo(a, b, c) values(2, 4, 5)"
247 assert.Equal(
248 t,
249 "insert into foo(a, b, c) values(?+)",
250 query.Fingerprint(q),
251 )
252
253 // VALUES lists with multiple ()
254 q = "insert into foo(a, b, c) values(2, 4, 5) , (2,4,5)"
255 assert.Equal(
256 t,
257 "insert into foo(a, b, c) values(?+)",
258 query.Fingerprint(q),
259 )
260
261 // VALUES lists with VALUE()
262 q = "insert into foo(a, b, c) value(2, 4, 5)"
263 assert.Equal(
264 t,
265 "insert into foo(a, b, c) value(?+)",
266 query.Fingerprint(q),
267 )
268
269 q = "insert into foo values (1, '(2)', 'This is a trick: ). More values.', 4)"
270 assert.Equal(
271 t,
272 "insert into foo values(?+)",
273 query.Fingerprint(q),
274 )
275 }
276
277 func TestFingerprintInList(t *testing.T) {
278 var q string
279
280 q = "select * from t where (base.nid IN ('1412', '1410', '1411'))"
281 assert.Equal(
282 t,
283 "select * from t where (base.nid in(?+))",
284 query.Fingerprint(q),
285 )
286
287 q = "SELECT ID, name, parent, type FROM posts WHERE _name IN ('perf','caching') AND (type = 'page' OR type = 'attachment')"
288 assert.Equal(
289 t,
290 "select id, name, parent, type from posts where _name in(?+) and (type = ? or type = ?)",
291 query.Fingerprint(q),
292 )
293
294 q = "SELECT t FROM field WHERE (entity_type = 'node') AND (entity_id IN ('609')) AND (language IN ('und')) AND (deleted = '0') ORDER BY delta ASC"
295 assert.Equal(
296 t,
297 "select t from field where (entity_type = ?) and (entity_id in(?+)) and (language in(?+)) and (deleted = ?) order by delta",
298 query.Fingerprint(q),
299 )
300 }
301
302 func TestFingerprintOrderBy(t *testing.T) {
303 var q string
304
305 // Remove ASC from ORDER BY
306 // Issue 1030: Fingerprint can remove ORDER BY ASC
307 q = "select c from t where i=1 order by c asc"
308 assert.Equal(
309 t,
310 "select c from t where i=? order by c",
311 query.Fingerprint(q),
312 )
313
314 // Remove only ASC from ORDER BY
315 q = "select * from t where i=1 order by a, b ASC, d DESC, e asc"
316 assert.Equal(
317 t,
318 "select * from t where i=? order by a, b, d desc, e",
319 query.Fingerprint(q),
320 )
321
322 // Remove ASC from spacey ORDER BY
323 q = `select * from t where i=1 order by
324 a, b ASC, d DESC,
325
326 e asc`
327 assert.Equal(
328 t,
329 "select * from t where i=? order by a, b, d desc, e",
330 query.Fingerprint(q),
331 )
332 }
333
334 func TestFingerprintOneLineComments(t *testing.T) {
335 var q string
336
337 // Removes one-line comments in fingerprints
338 q = "select \n-- bar\n foo"
339 assert.Equal(
340 t,
341 "select foo",
342 query.Fingerprint(q),
343 )
344
345 // Removes one-line comments in fingerprint without mushing things together
346 q = "select foo-- bar\n,foo"
347 assert.Equal(
348 t,
349 "select foo,foo",
350 query.Fingerprint(q),
351 )
352
353 // Removes multi-line comment followed by 'space' and '/'
354 q = "/* /e */ select * from table\n"
355 assert.Equal(
356 t,
357 "select * from table",
358 query.Fingerprint(q),
359 )
360
361 // Remove multi-line comment immediately followed by '/'
362 q = "/*/this/is/also/a/comment*/ select * from table\n"
363 assert.Equal(
364 t,
365 "select * from table",
366 query.Fingerprint(q),
367 )
368
369 // Removes one-line EOL comments in fingerprints
370 q = "select foo -- bar\n"
371 assert.Equal(
372 t,
373 "select foo",
374 query.Fingerprint(q),
375 )
376
377 // Removes one-line # hash comments
378 q = "### Channels ###\n\u0009\u0009\u0009\u0009\u0009SELECT sourcetable, IF(f.lastcontent = 0, f.lastupdate, f.lastcontent) AS lastactivity,\n\u0009\u0009\u0009\u0009\u0009f.totalcount AS activity, type.class AS type,\n\u0009\u0009\u0009\u0009\u0009(f.nodeoptions \u0026 512) AS noUnsubscribe\n\u0009\u0009\u0009\u0009\u0009FROM node AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN contenttype AS type ON type.contenttypeid = f.contenttypeid \n\n\u0009\u0009\u0009\u0009\u0009INNER JOIN subscribed AS sd ON sd.did = f.nodeid AND sd.userid = 15965\n UNION ALL \n\n\u0009\u0009\u0009\u0009\u0009### Users ###\n\u0009\u0009\u0009\u0009\u0009SELECT f.name AS title, f.userid AS keyval, 'user' AS sourcetable, IFNULL(f.lastpost, f.joindate) AS lastactivity,\n\u0009\u0009\u0009\u0009\u0009f.posts as activity, 'Member' AS type,\n\u0009\u0009\u0009\u0009\u00090 AS noUnsubscribe\n\u0009\u0009\u0009\u0009\u0009FROM user AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN userlist AS ul ON ul.relationid = f.userid AND ul.userid = 15965\n\u0009\u0009\u0009\u0009\u0009WHERE ul.type = 'f' AND ul.aq = 'yes'\n ORDER BY title ASC LIMIT 100"
379 assert.Equal(
380 t,
381 "select sourcetable, if(f.lastcontent = ?, f.lastupdate, f.lastcontent) as lastactivity, f.totalcount as activity, type.class as type, (f.nodeoptions & ?) as nounsubscribe from node as f inner join contenttype as type on type.contenttypeid = f.contenttypeid inner join subscribed as sd on sd.did = f.nodeid and sd.userid = ? union all select f.name as title, f.userid as keyval, ? as sourcetable, ifnull(f.lastpost, f.joindate) as lastactivity, f.posts as activity, ? as type, ? as nounsubscribe from user as f inner join userlist as ul on ul.relationid = f.userid and ul.userid = ? where ul.type = ? and ul.aq = ? order by title limit ?",
382 query.Fingerprint(q),
383 )
384 }
385
386 func TestFingerprintTricky(t *testing.T) {
387 var q string
388
389 // Full hex can look like an ident if not for the leading 0x.
390 q = "SELECT c FROM t WHERE id=0xdeadbeaf"
391 assert.Equal(
392 t,
393 "select c from t where id=?",
394 query.Fingerprint(q),
395 )
396
397 // Caused a crash.
398 q = "SELECT * FROM t WHERE 1=1 AND id=1"
399 assert.Equal(
400 t,
401 "select * from t where ?=? and id=?",
402 query.Fingerprint(q),
403 )
404
405 // Caused a crash.
406 q = "SELECT `db`.*, (CASE WHEN (`date_start` <= '2014-09-10 09:17:59' AND `date_end` >= '2014-09-10 09:17:59') THEN 'open' WHEN (`date_start` > '2014-09-10 09:17:59' AND `date_end` > '2014-09-10 09:17:59') THEN 'tbd' ELSE 'none' END) AS `status` FROM `foo` AS `db` WHERE (a_b in ('1', '10101'))"
407 assert.Equal(
408 t,
409 "select `db`.*, (case when (`date_start` <= ? and `date_end` >= ?) then ? when (`date_start` > ? and `date_end` > ?) then ? else ? end) as `status` from `foo` as `db` where (a_b in(?+))",
410 query.Fingerprint(q),
411 )
412
413 // VALUES() after ON DUPE KEY is not the same as VALUES() for INSERT.
414 q = "insert into t values (1) on duplicate key update query_count=COALESCE(query_count, 0) + VALUES(query_count)"
415 assert.Equal(
416 t,
417 "insert into t values(?+) on duplicate key update query_count=coalesce(query_count, ?) + values(query_count)",
418 query.Fingerprint(q),
419 )
420
421 q = "insert into t values (1), (2), (3)\n\n\ton duplicate key update query_count=1"
422 assert.Equal(
423 t,
424 "insert into t values(?+) on duplicate key update query_count=?",
425 query.Fingerprint(q),
426 )
427
428 q = "select t.table_schema,t.table_name,engine from information_schema.tables t inner join information_schema.columns c on t.table_schema=c.table_schema and t.table_name=c.table_name group by t.table_schema,t.table_name having sum(if(column_key in ('PRI','UNI'),1,0))=0"
429 assert.Equal(
430 t,
431 "select t.table_schema,t.table_name,engine from information_schema.tables t inner join information_schema.columns c on t.table_schema=c.table_schema and t.table_name=c.table_name group by t.table_schema,t.table_name having sum(if(column_key in(?+),?,?))=?",
432 query.Fingerprint(q),
433 )
434
435 // Empty value list is valid SQL.
436 q = "INSERT INTO t () VALUES ()"
437 assert.Equal(
438 t,
439 "insert into t () values()",
440 query.Fingerprint(q),
441 )
442
443 q = "SELECT * FROM table WHERE field = 'value' /*arbitrary/31*/ "
444 assert.Equal(
445 t,
446 "select * from table where field = ?",
447 query.Fingerprint(q),
448 )
449
450 q = "SELECT * FROM table WHERE field = 'value' /*arbitrary31*/ "
451 assert.Equal(
452 t,
453 "select * from table where field = ?",
454 query.Fingerprint(q),
455 )
456 }
457
458 func TestNumbersInFunctions(t *testing.T) {
459 var q string
460
461 // Full hex can look like an ident if not for the leading 0x.
462 q = "select sleep(2) from test.n"
463 assert.Equal(
464 t,
465 "select sleep(?) from test.n",
466 query.Fingerprint(q),
467 )
468 }
469
470 func TestId(t *testing.T) {
471 var f string
472
473 f = "hello world"
474 assert.Equal(
475 t,
476 "93CB22BB8F5ACDC3",
477 query.Id(f),
478 )
479
480 f = "select sourcetable, if(f.lastcontent = ?, f.lastupdate, f.lastcontent) as lastactivity, f.totalcount as activity, type.class as type, (f.nodeoptions & ?) as nounsubscribe from node as f inner join contenttype as type on type.contenttypeid = f.contenttypeid inner join subscribed as sd on sd.did = f.nodeid and sd.userid = ? union all select f.name as title, f.userid as keyval, ? as sourcetable, ifnull(f.lastpost, f.joindate) as lastactivity, f.posts as activity, ? as type, ? as nounsubscribe from user as f inner join userlist as ul on ul.relationid = f.userid and ul.userid = ? where ul.type = ? and ul.aq = ? order by title limit ?"
481 assert.Equal(
482 t,
483 "DB9EF18846547B8C",
484 query.Id(f),
485 )
486
487 f = "select sleep(?) from n"
488 assert.Equal(
489 t,
490 "7F7D57ACDD8A346E",
491 query.Id(f),
492 )
493 }
494
495 func TestFingerprintPanicChallenge1(t *testing.T) {
496 q := "SELECT '' '' ''"
497 assert.Equal(
498 t,
499 "select ? ? ?",
500 query.Fingerprint(q),
501 )
502
503 q = "SELECT '' '' '' FROM kamil"
504 assert.Equal(
505 t,
506 "select ? ? ? from kamil",
507 query.Fingerprint(q),
508 )
509 }
510
511 func TestFingerprintPanicChallenge2(t *testing.T) {
512 q := "SELECT 'a' 'b' 'c' 'd'"
513 assert.Equal(
514 t,
515 "select ? ? ? ?",
516 query.Fingerprint(q),
517 )
518
519 q = "SELECT 'a' 'b' 'c' 'd' FROM kamil"
520 assert.Equal(
521 t,
522 "select ? ? ? ? from kamil",
523 query.Fingerprint(q),
524 )
525 }
526
527 func TestFingerprintDashesInNames(t *testing.T) {
528 q := "select field from `master-db-1`.`table-1` order by id, ?;"
529 assert.Equal(
530 t,
531 "select field from `master-db-1`.`table-1` order by id, ?;",
532 query.Fingerprint(q),
533 )
534
535 q = "select field from `-master-db-1`.`-table-1-` order by id, ?;"
536 assert.Equal(
537 t,
538 "select field from `-master-db-1`.`-table-1-` order by id, ?;",
539 query.Fingerprint(q),
540 )
541
542 q = "SELECT BENCHMARK(100000000, pow(rand(), rand())), 1 FROM `-hj-7d6-shdj5-7jd-kf-g988h-`.`-aaahj-7d6-shdj5-7&^%$jd-kf-g988h-9+4-5*6ab-`"
543 assert.Equal(
544 t,
545 "select benchmark(?, pow(rand(), rand())), ? from `-hj-7d6-shdj5-7jd-kf-g988h-`.`-aaahj-7d6-shdj5-7&^%$jd-kf-g988h-9+4-5*6ab-`",
546 query.Fingerprint(q),
547 )
548 }
549
550 func TestFingerprintKeywords(t *testing.T) {
551 var q string
552
553 // values is a keyword but value is not. :-\
554 q = "SELECT name, value FROM variable"
555 assert.Equal(
556 t,
557 "select name, value from variable",
558 query.Fingerprint(q),
559 )
560 }
561
562 func TestFingerprintUseIndex(t *testing.T) {
563 var q string
564
565 q = `SELECT 1 AS one FROM calls USE INDEX(index_name)`
566 assert.Equal(
567 t,
568 "select ? as one from calls use index(index_name)",
569 query.Fingerprint(q),
570 )
571 }
572
573 func TestFingerprintWithNumberInDbName(t *testing.T) {
574 var q string
575 defaultReplaceNumbersInWords := query.ReplaceNumbersInWords
576 query.ReplaceNumbersInWords = true
577 defer func() {
578 // Restore default value for other tests
579 query.ReplaceNumbersInWords = defaultReplaceNumbersInWords
580 }()
581
582 q = "SELECT c FROM org235.t WHERE id=0xdeadbeaf"
583 assert.Equal(
584 t,
585 "select c from org?.t where id=?",
586 query.Fingerprint(q),
587 )
588
589 q = "CREATE DATABASE org235_percona345 COLLATE 'utf8_general_ci'"
590 assert.Equal(
591 t,
592 "create database org?_percona? collate ?",
593 query.Fingerprint(q),
594 )
595
596 q = "select foo_1 from foo_2_3"
597 assert.Equal(
598 t,
599 "select foo_? from foo_?_?",
600 query.Fingerprint(q),
601 )
602
603 q = "SELECT * FROM prices.rt_5min where id=1"
604 assert.Equal(
605 t,
606 "select * from prices.rt_?min where id=?",
607 query.Fingerprint(q),
608 )
609
610 // @todo prefixes are not supported, requires more hacks
611 q = "select 123foo from 123foo"
612 assert.Equal(
613 t,
614 "select 123foo from 123foo",
615 query.Fingerprint(q),
616 )
617 }
618
619 func TestFingerprintMaxExecTimeWithBackticks(t *testing.T) {
620 q := "/* test-test-5775b87c5d-fczwg|@test-test|test-test|internal|/internal/test|test-test */\n\n SELECT /*+ MAX_EXECUTION_TIME(7995) */ `id`, `domain`, `city_id`, `name`, `polygon`, `state`, `translations` AS `translations`\n FROM `area`\n WHERE `state` IN ('active', 'disabled', 'removed') AND `domain` = 'test'"
621
622 assert.Equal(
623 t,
624 "select `id`, `domain`, `city_id`, `name`, `polygon`, `state`, `translations` as `translations` from `area` where `state` in(?+) and `domain` = ?",
625 query.Fingerprint(q),
626 )
627 }
628
629 func TestFingerprintMaxExecTimeNoBackticks(t *testing.T) {
630 q := "/* test-test-5775b87c5d-fczwg|@test-test|test-test|internal|/internal/test|test-test */\n\n SELECT /*+ MAX_EXECUTION_TIME(7995) */ id, domain, city_id, name, polygon, state, translations AS translations\n FROM area\n WHERE state IN ('active', 'disabled', 'removed') AND domain = 'test'"
631
632 assert.Equal(t,
633 "select id, domain, city_id, name, polygon, state, translations as translations from area where state in(?+) and domain = ?",
634 query.Fingerprint(q),
635 )
636 }
637
638 func TestFingerprintSubqueries(t *testing.T) {
639 type testCase struct {
640 name string
641 query string
642 expected string
643 }
644 // Test cases for subqueries in IN clauses
645 testCases := []testCase{
646 {
647 name: "Simple subquery",
648 query: "select * from a where a.id in (select b from c)",
649 expected: "select * from a where a.id in(select b from c)",
650 },
651 {
652 name: "multi subquery",
653 query: "select a, b from t1,t2 where t1.c in (select a from t where b in (select b from c where d like 'foo%') and e > (select 3 * sum(a) from h where b = c and e = d and d >= '1994-01-01' and f < date_add ('1994-01-01',interval '1' year) and a = b and c = 'abc' order by e LIMIT 0,200))",
654 expected: "select a, b from t1,t2 where t1.c in(select a from t where b in(select b from c where d like ?) and e > (select ? * sum(a) from h where b = c and e = d and d >= ? and f < date_add (?,interval ? year) and a = b and c = ? order by e limit ?,?))",
655 },
656
657 {
658 name: "Subquery with different table",
659 query: "select * from a where a.id in (select d from e)",
660 expected: "select * from a where a.id in(select d from e)",
661 },
662 {
663 name: "Subquery with different column",
664 query: "select * from a where a.id in (select f from c)",
665 expected: "select * from a where a.id in(select f from c)",
666 },
667 {
668 name: "Subquery with WHERE clause",
669 query: "select * from a where a.id in (select b from c where c.status = 'active')",
670 expected: "select * from a where a.id in(select b from c where c.status = ?)",
671 },
672 {
673 name: "Value list should still use (?+)",
674 query: "select * from a where a.id in (1, 2, 3)",
675 expected: "select * from a where a.id in(?+)",
676 },
677 {
678 name: "Mixed case SELECT",
679 query: "select * from a where a.id in (SELECT b FROM c)",
680 expected: "select * from a where a.id in(select b from c)",
681 },
682 {
683 name: "Subquery with spaces",
684 query: "select * from a where a.id in ( select b from c )",
685 expected: "select * from a where a.id in(select b from c)",
686 },
687 }
688
689 for _, tc := range testCases {
690 t.Run(tc.name, func(t *testing.T) {
691 actual := query.Fingerprint(tc.query)
692 assert.Equal(t, tc.expected, actual, "Query: %s", tc.query)
693 })
694 }
695 }
696
697 func TestFingerprintSystemVariableQueries(t *testing.T) {
698 type testCase struct {
699 name string
700 query string
701 expected string
702 }
703 // Test cases for system variable queries
704 testCases := []testCase{
705 {
706 name: "global read_only",
707 query: "set long_query_time = 0; SELECT @@GLOBAL.read_only AS Value",
708 expected: "set long_query_time = ?; select @@global.read_only as value",
709 },
710 }
711 for _, tc := range testCases {
712 t.Run(tc.name, func(t *testing.T) {
713 actual := query.Fingerprint(tc.query)
714 assert.Equal(t, tc.expected, actual, "Query: %s", tc.query)
715 })
716 }
717 }