Debian Codegraph @

parse_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqlparser
18
19 import (
20 "bytes"
21 "fmt"
22 "strings"
23 "testing"
24 )
25
26 var (
27 validSQL = []struct {
28 input string
29 output string
30 }{{
31 input: "select 1",
32 output: "select 1 from dual",
33 }, {
34 input: "select 1 from t",
35 }, {
36 input: "select .1 from t",
37 }, {
38 input: "select 1.2e1 from t",
39 }, {
40 input: "select 1.2e+1 from t",
41 }, {
42 input: "select 1.2e-1 from t",
43 }, {
44 input: "select 08.3 from t",
45 }, {
46 input: "select -1 from t where b = -2",
47 }, {
48 input: "select - -1 from t",
49 output: "select 1 from t",
50 }, {
51 input: "select 1 from t // aa\n",
52 output: "select 1 from t",
53 }, {
54 input: "select 1 from t -- aa\n",
55 output: "select 1 from t",
56 }, {
57 input: "select 1 from t # aa\n",
58 output: "select 1 from t",
59 }, {
60 input: "select 1 --aa\nfrom t",
61 output: "select 1 from t",
62 }, {
63 input: "select 1 #aa\nfrom t",
64 output: "select 1 from t",
65 }, {
66 input: "select /* simplest */ 1 from t",
67 }, {
68 input: "select /* double star **/ 1 from t",
69 }, {
70 input: "select /* double */ /* comment */ 1 from t",
71 }, {
72 input: "select /* back-quote keyword */ `By` from t",
73 }, {
74 input: "select /* back-quote num */ `2a` from t",
75 }, {
76 input: "select /* back-quote . */ `a.b` from t",
77 }, {
78 input: "select /* back-quote back-quote */ `a``b` from t",
79 }, {
80 input: "select /* back-quote unnecessary */ 1 from `t`",
81 output: "select /* back-quote unnecessary */ 1 from t",
82 }, {
83 input: "select /* back-quote idnum */ 1 from `a1`",
84 output: "select /* back-quote idnum */ 1 from a1",
85 }, {
86 input: "select /* @ */ @@a from b",
87 }, {
88 input: "select /* \\0 */ '\\0' from a",
89 }, {
90 input: "select 1 /* drop this comment */ from t",
91 output: "select 1 from t",
92 }, {
93 input: "select /* union */ 1 from t union select 1 from t",
94 }, {
95 input: "select /* double union */ 1 from t union select 1 from t union select 1 from t",
96 }, {
97 input: "select /* union all */ 1 from t union all select 1 from t",
98 }, {
99 input: "select /* union distinct */ 1 from t union distinct select 1 from t",
100 }, {
101 input: "(select /* union parenthesized select */ 1 from t order by a) union select 1 from t",
102 output: "(select /* union parenthesized select */ 1 from t order by a asc) union select 1 from t",
103 }, {
104 input: "select /* union parenthesized select 2 */ 1 from t union (select 1 from t)",
105 }, {
106 input: "select /* union order by */ 1 from t union select 1 from t order by a",
107 output: "select /* union order by */ 1 from t union select 1 from t order by a asc",
108 }, {
109 input: "select /* union order by limit lock */ 1 from t union select 1 from t order by a limit 1 for update",
110 output: "select /* union order by limit lock */ 1 from t union select 1 from t order by a asc limit 1 for update",
111 }, {
112 input: "select /* union with limit on lhs */ 1 from t limit 1 union select 1 from t",
113 }, {
114 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",
115 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",
116 }, {
117 input: "select a from (select 1 as a from tbl1 union select 2 from tbl2) as t",
118 }, {
119 input: "select * from t1 join (select * from t2 union select * from t3) as t",
120 }, {
121 // Ensure this doesn't generate: ""select * from t1 join t2 on a = b join t3 on a = b".
122 input: "select * from t1 join t2 on a = b join t3",
123 }, {
124 input: "select * from t1 where col in (select 1 from dual union select 2 from dual)",
125 }, {
126 input: "select * from t1 where exists (select a from t2 union select b from t3)",
127 }, {
128 input: "select /* distinct */ distinct 1 from t",
129 }, {
130 input: "select /* straight_join */ straight_join 1 from t",
131 }, {
132 input: "select /* for update */ 1 from t for update",
133 }, {
134 input: "select /* lock in share mode */ 1 from t lock in share mode",
135 }, {
136 input: "select /* select list */ 1, 2 from t",
137 }, {
138 input: "select /* * */ * from t",
139 }, {
140 input: "select /* a.* */ a.* from t",
141 }, {
142 input: "select /* a.b.* */ a.b.* from t",
143 }, {
144 input: "select /* column alias */ a b from t",
145 output: "select /* column alias */ a as b from t",
146 }, {
147 input: "select /* column alias with as */ a as b from t",
148 }, {
149 input: "select /* keyword column alias */ a as `By` from t",
150 }, {
151 input: "select /* column alias as string */ a as \"b\" from t",
152 output: "select /* column alias as string */ a as b from t",
153 }, {
154 input: "select /* column alias as string without as */ a \"b\" from t",
155 output: "select /* column alias as string without as */ a as b from t",
156 }, {
157 input: "select /* a.* */ a.* from t",
158 }, {
159 input: "select next value for t",
160 output: "select next 1 values from t",
161 }, {
162 input: "select next value from t",
163 output: "select next 1 values from t",
164 }, {
165 input: "select next 10 values from t",
166 }, {
167 input: "select next :a values from t",
168 }, {
169 input: "select /* `By`.* */ `By`.* from t",
170 }, {
171 input: "select /* select with bool expr */ a = b from t",
172 }, {
173 input: "select /* case_when */ case when a = b then c end from t",
174 }, {
175 input: "select /* case_when_else */ case when a = b then c else d end from t",
176 }, {
177 input: "select /* case_when_when_else */ case when a = b then c when b = d then d else d end from t",
178 }, {
179 input: "select /* case */ case aa when a = b then c end from t",
180 }, {
181 input: "select /* parenthesis */ 1 from (t)",
182 }, {
183 input: "select /* parenthesis multi-table */ 1 from (t1, t2)",
184 }, {
185 input: "select /* table list */ 1 from t1, t2",
186 }, {
187 input: "select /* parenthessis in table list 1 */ 1 from (t1), t2",
188 }, {
189 input: "select /* parenthessis in table list 2 */ 1 from t1, (t2)",
190 }, {
191 input: "select /* use */ 1 from t1 use index (a) where b = 1",
192 }, {
193 input: "select /* keyword index */ 1 from t1 use index (`By`) where b = 1",
194 }, {
195 input: "select /* ignore */ 1 from t1 as t2 ignore index (a), t3 use index (b) where b = 1",
196 }, {
197 input: "select /* use */ 1 from t1 as t2 use index (a), t3 use index (b) where b = 1",
198 }, {
199 input: "select /* force */ 1 from t1 as t2 force index (a), t3 force index (b) where b = 1",
200 }, {
201 input: "select /* table alias */ 1 from t t1",
202 output: "select /* table alias */ 1 from t as t1",
203 }, {
204 input: "select /* table alias with as */ 1 from t as t1",
205 }, {
206 input: "select /* string table alias */ 1 from t as 't1'",
207 output: "select /* string table alias */ 1 from t as t1",
208 }, {
209 input: "select /* string table alias without as */ 1 from t 't1'",
210 output: "select /* string table alias without as */ 1 from t as t1",
211 }, {
212 input: "select /* keyword table alias */ 1 from t as `By`",
213 }, {
214 input: "select /* join */ 1 from t1 join t2",
215 }, {
216 input: "select /* join on */ 1 from t1 join t2 on a = b",
217 }, {
218 input: "select /* join on */ 1 from t1 join t2 using (a)",
219 }, {
220 input: "select /* inner join */ 1 from t1 inner join t2",
221 output: "select /* inner join */ 1 from t1 join t2",
222 }, {
223 input: "select /* cross join */ 1 from t1 cross join t2",
224 output: "select /* cross join */ 1 from t1 join t2",
225 }, {
226 input: "select /* straight_join */ 1 from t1 straight_join t2",
227 }, {
228 input: "select /* straight_join on */ 1 from t1 straight_join t2 on a = b",
229 }, {
230 input: "select /* left join */ 1 from t1 left join t2 on a = b",
231 }, {
232 input: "select /* left join */ 1 from t1 left join t2 using (a)",
233 }, {
234 input: "select /* left outer join */ 1 from t1 left outer join t2 on a = b",
235 output: "select /* left outer join */ 1 from t1 left join t2 on a = b",
236 }, {
237 input: "select /* left outer join */ 1 from t1 left outer join t2 using (a)",
238 output: "select /* left outer join */ 1 from t1 left join t2 using (a)",
239 }, {
240 input: "select /* right join */ 1 from t1 right join t2 on a = b",
241 }, {
242 input: "select /* right join */ 1 from t1 right join t2 using (a)",
243 }, {
244 input: "select /* right outer join */ 1 from t1 right outer join t2 on a = b",
245 output: "select /* right outer join */ 1 from t1 right join t2 on a = b",
246 }, {
247 input: "select /* right outer join */ 1 from t1 right outer join t2 using (a)",
248 output: "select /* right outer join */ 1 from t1 right join t2 using (a)",
249 }, {
250 input: "select /* natural join */ 1 from t1 natural join t2",
251 }, {
252 input: "select /* natural left join */ 1 from t1 natural left join t2",
253 }, {
254 input: "select /* natural left outer join */ 1 from t1 natural left join t2",
255 output: "select /* natural left outer join */ 1 from t1 natural left join t2",
256 }, {
257 input: "select /* natural right join */ 1 from t1 natural right join t2",
258 }, {
259 input: "select /* natural right outer join */ 1 from t1 natural right join t2",
260 output: "select /* natural right outer join */ 1 from t1 natural right join t2",
261 }, {
262 input: "select /* join on */ 1 from t1 join t2 on a = b",
263 }, {
264 input: "select /* join using */ 1 from t1 join t2 using (a)",
265 }, {
266 input: "select /* join using (a, b, c) */ 1 from t1 join t2 using (a, b, c)",
267 }, {
268 input: "select /* s.t */ 1 from s.t",
269 }, {
270 input: "select /* keyword schema & table name */ 1 from `By`.`bY`",
271 }, {
272 input: "select /* select in from */ 1 from (select 1 from t) as a",
273 }, {
274 input: "select /* select in from with no as */ 1 from (select 1 from t) a",
275 output: "select /* select in from with no as */ 1 from (select 1 from t) as a",
276 }, {
277 input: "select /* where */ 1 from t where a = b",
278 }, {
279 input: "select /* and */ 1 from t where a = b and a = c",
280 }, {
281 input: "select /* && */ 1 from t where a = b && a = c",
282 output: "select /* && */ 1 from t where a = b and a = c",
283 }, {
284 input: "select /* or */ 1 from t where a = b or a = c",
285 }, {
286 input: "select /* || */ 1 from t where a = b || a = c",
287 output: "select /* || */ 1 from t where a = b or a = c",
288 }, {
289 input: "select /* not */ 1 from t where not a = b",
290 }, {
291 input: "select /* ! */ 1 from t where a = !1",
292 }, {
293 input: "select /* bool is */ 1 from t where a = b is null",
294 }, {
295 input: "select /* bool is not */ 1 from t where a = b is not false",
296 }, {
297 input: "select /* true */ 1 from t where true",
298 }, {
299 input: "select /* false */ 1 from t where false",
300 }, {
301 input: "select /* false on left */ 1 from t where false = 0",
302 }, {
303 input: "select /* exists */ 1 from t where exists (select 1 from t)",
304 }, {
305 input: "select /* (boolean) */ 1 from t where not (a = b)",
306 }, {
307 input: "select /* in value list */ 1 from t where a in (b, c)",
308 }, {
309 input: "select /* in select */ 1 from t where a in (select 1 from t)",
310 }, {
311 input: "select /* not in */ 1 from t where a not in (b, c)",
312 }, {
313 input: "select /* like */ 1 from t where a like b",
314 }, {
315 input: "select /* like escape */ 1 from t where a like b escape '!'",
316 }, {
317 input: "select /* not like */ 1 from t where a not like b",
318 }, {
319 input: "select /* not like escape */ 1 from t where a not like b escape '$'",
320 }, {
321 input: "select /* regexp */ 1 from t where a regexp b",
322 }, {
323 input: "select /* not regexp */ 1 from t where a not regexp b",
324 }, {
325 input: "select /* rlike */ 1 from t where a rlike b",
326 output: "select /* rlike */ 1 from t where a regexp b",
327 }, {
328 input: "select /* not rlike */ 1 from t where a not rlike b",
329 output: "select /* not rlike */ 1 from t where a not regexp b",
330 }, {
331 input: "select /* between */ 1 from t where a between b and c",
332 }, {
333 input: "select /* not between */ 1 from t where a not between b and c",
334 }, {
335 input: "select /* is null */ 1 from t where a is null",
336 }, {
337 input: "select /* is not null */ 1 from t where a is not null",
338 }, {
339 input: "select /* is true */ 1 from t where a is true",
340 }, {
341 input: "select /* is not true */ 1 from t where a is not true",
342 }, {
343 input: "select /* is false */ 1 from t where a is false",
344 }, {
345 input: "select /* is not false */ 1 from t where a is not false",
346 }, {
347 input: "select /* < */ 1 from t where a < b",
348 }, {
349 input: "select /* <= */ 1 from t where a <= b",
350 }, {
351 input: "select /* >= */ 1 from t where a >= b",
352 }, {
353 input: "select /* > */ 1 from t where a > b",
354 }, {
355 input: "select /* != */ 1 from t where a != b",
356 }, {
357 input: "select /* <> */ 1 from t where a <> b",
358 output: "select /* <> */ 1 from t where a != b",
359 }, {
360 input: "select /* <=> */ 1 from t where a <=> b",
361 }, {
362 input: "select /* != */ 1 from t where a != b",
363 }, {
364 input: "select /* single value expre list */ 1 from t where a in (b)",
365 }, {
366 input: "select /* select as a value expression */ 1 from t where a = (select a from t)",
367 }, {
368 input: "select /* parenthesised value */ 1 from t where a = (b)",
369 }, {
370 input: "select /* over-parenthesize */ ((1)) from t where ((a)) in (((1))) and ((a, b)) in ((((1, 1))), ((2, 2)))",
371 }, {
372 input: "select /* dot-parenthesize */ (a.b) from t where (b.c) = 2",
373 }, {
374 input: "select /* & */ 1 from t where a = b & c",
375 }, {
376 input: "select /* & */ 1 from t where a = b & c",
377 }, {
378 input: "select /* | */ 1 from t where a = b | c",
379 }, {
380 input: "select /* ^ */ 1 from t where a = b ^ c",
381 }, {
382 input: "select /* + */ 1 from t where a = b + c",
383 }, {
384 input: "select /* - */ 1 from t where a = b - c",
385 }, {
386 input: "select /* * */ 1 from t where a = b * c",
387 }, {
388 input: "select /* / */ 1 from t where a = b / c",
389 }, {
390 input: "select /* % */ 1 from t where a = b % c",
391 }, {
392 input: "select /* div */ 1 from t where a = b div c",
393 }, {
394 input: "select /* MOD */ 1 from t where a = b MOD c",
395 output: "select /* MOD */ 1 from t where a = b % c",
396 }, {
397 input: "select /* << */ 1 from t where a = b << c",
398 }, {
399 input: "select /* >> */ 1 from t where a = b >> c",
400 }, {
401 input: "select /* % no space */ 1 from t where a = b%c",
402 output: "select /* % no space */ 1 from t where a = b % c",
403 }, {
404 input: "select /* u+ */ 1 from t where a = +b",
405 }, {
406 input: "select /* u- */ 1 from t where a = -b",
407 }, {
408 input: "select /* u~ */ 1 from t where a = ~b",
409 }, {
410 input: "select /* -> */ a.b -> 'ab' from t",
411 }, {
412 input: "select /* -> */ a.b ->> 'ab' from t",
413 }, {
414 input: "select /* empty function */ 1 from t where a = b()",
415 }, {
416 input: "select /* function with 1 param */ 1 from t where a = b(c)",
417 }, {
418 input: "select /* function with many params */ 1 from t where a = b(c, d)",
419 }, {
420 input: "select /* function with distinct */ count(distinct a) from t",
421 }, {
422 input: "select /* if as func */ 1 from t where a = if(b)",
423 }, {
424 input: "select /* current_timestamp as func */ current_timestamp() from t",
425 }, {
426 input: "select /* mod as func */ a from tab where mod(b, 2) = 0",
427 }, {
428 input: "select /* database as func no param */ database() from t",
429 }, {
430 input: "select /* database as func 1 param */ database(1) from t",
431 }, {
432 input: "select /* a */ a from t",
433 }, {
434 input: "select /* a.b */ a.b from t",
435 }, {
436 input: "select /* a.b.c */ a.b.c from t",
437 }, {
438 input: "select /* keyword a.b */ `By`.`bY` from t",
439 }, {
440 input: "select /* string */ 'a' from t",
441 }, {
442 input: "select /* double quoted string */ \"a\" from t",
443 output: "select /* double quoted string */ 'a' from t",
444 }, {
445 input: "select /* quote quote in string */ 'a''a' from t",
446 output: "select /* quote quote in string */ 'a\\'a' from t",
447 }, {
448 input: "select /* double quote quote in string */ \"a\"\"a\" from t",
449 output: "select /* double quote quote in string */ 'a\\\"a' from t",
450 }, {
451 input: "select /* quote in double quoted string */ \"a'a\" from t",
452 output: "select /* quote in double quoted string */ 'a\\'a' from t",
453 }, {
454 input: "select /* backslash quote in string */ 'a\\'a' from t",
455 }, {
456 input: "select /* literal backslash in string */ 'a\\\\na' from t",
457 }, {
458 input: "select /* all escapes */ '\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\' from t",
459 }, {
460 input: "select /* non-escape */ '\\x' from t",
461 output: "select /* non-escape */ 'x' from t",
462 }, {
463 input: "select /* unescaped backslash */ '\\n' from t",
464 }, {
465 input: "select /* value argument */ :a from t",
466 }, {
467 input: "select /* value argument with digit */ :a1 from t",
468 }, {
469 input: "select /* value argument with dot */ :a.b from t",
470 }, {
471 input: "select /* positional argument */ ? from t",
472 output: "select /* positional argument */ :v1 from t",
473 }, {
474 input: "select /* multiple positional arguments */ ?, ? from t",
475 output: "select /* multiple positional arguments */ :v1, :v2 from t",
476 }, {
477 input: "select /* list arg */ * from t where a in ::list",
478 }, {
479 input: "select /* list arg not in */ * from t where a not in ::list",
480 }, {
481 input: "select /* null */ null from t",
482 }, {
483 input: "select /* octal */ 010 from t",
484 }, {
485 input: "select /* hex */ x'f0A1' from t",
486 output: "select /* hex */ X'f0A1' from t",
487 }, {
488 input: "select /* hex caps */ X'F0a1' from t",
489 }, {
490 input: "select /* bit literal */ b'0101' from t",
491 output: "select /* bit literal */ B'0101' from t",
492 }, {
493 input: "select /* bit literal caps */ B'010011011010' from t",
494 }, {
495 input: "select /* 0x */ 0xf0 from t",
496 }, {
497 input: "select /* float */ 0.1 from t",
498 }, {
499 input: "select /* group by */ 1 from t group by a",
500 }, {
501 input: "select /* having */ 1 from t having a = b",
502 }, {
503 input: "select /* simple order by */ 1 from t order by a",
504 output: "select /* simple order by */ 1 from t order by a asc",
505 }, {
506 input: "select /* order by asc */ 1 from t order by a asc",
507 }, {
508 input: "select /* order by desc */ 1 from t order by a desc",
509 }, {
510 input: "select /* order by null */ 1 from t order by null",
511 }, {
512 input: "select /* limit a */ 1 from t limit a",
513 }, {
514 input: "select /* limit a,b */ 1 from t limit a, b",
515 }, {
516 input: "select /* binary unary */ a- -b from t",
517 output: "select /* binary unary */ a - -b from t",
518 }, {
519 input: "select /* - - */ - -b from t",
520 }, {
521 input: "select /* binary binary */ binary binary b from t",
522 }, {
523 input: "select /* binary ~ */ binary ~b from t",
524 }, {
525 input: "select /* ~ binary */ ~ binary b from t",
526 }, {
527 input: "select /* interval */ adddate('2008-01-02', interval 31 day) from t",
528 }, {
529 input: "select /* interval keyword */ adddate('2008-01-02', interval 1 year) from t",
530 }, {
531 input: "select /* dual */ 1 from dual",
532 }, {
533 input: "select /* Dual */ 1 from Dual",
534 output: "select /* Dual */ 1 from dual",
535 }, {
536 input: "select /* DUAL */ 1 from Dual",
537 output: "select /* DUAL */ 1 from dual",
538 }, {
539 input: "select /* column as bool in where */ a from t where b",
540 }, {
541 input: "select /* OR of columns in where */ * from t where a or b",
542 }, {
543 input: "select /* OR of mixed columns in where */ * from t where a = 5 or b and c is not null",
544 }, {
545 input: "select /* OR in select columns */ (a or b) from t where c = 5",
546 }, {
547 input: "select /* bool as select value */ a, true from t",
548 }, {
549 input: "select /* bool column in ON clause */ * from t join s on t.id = s.id and s.foo where t.bar",
550 }, {
551 input: "select /* bool in order by */ * from t order by a is null or b asc",
552 }, {
553 input: "select /* string in case statement */ if(max(case a when 'foo' then 1 else 0 end) = 1, 'foo', 'bar') as foobar from t",
554 }, {
555 input: "/*!show databases*/",
556 output: "show databases",
557 }, {
558 input: "select /*!40101 * from*/ t",
559 output: "select * from t",
560 }, {
561 input: "select /*! * from*/ t",
562 output: "select * from t",
563 }, {
564 input: "select /*!* from*/ t",
565 output: "select * from t",
566 }, {
567 input: "select /*!401011 from*/ t",
568 output: "select 1 from t",
569 }, {
570 input: "select /* dual */ 1 from dual",
571 }, {
572 input: "insert /* simple */ into a values (1)",
573 }, {
574 input: "insert /* a.b */ into a.b values (1)",
575 }, {
576 input: "insert /* multi-value */ into a values (1, 2)",
577 }, {
578 input: "insert /* multi-value list */ into a values (1, 2), (3, 4)",
579 }, {
580 input: "insert /* no values */ into a values ()",
581 }, {
582 input: "insert /* set */ into a set a = 1, b = 2",
583 output: "insert /* set */ into a(a, b) values (1, 2)",
584 }, {
585 input: "insert /* set default */ into a set a = default, b = 2",
586 output: "insert /* set default */ into a(a, b) values (default, 2)",
587 }, {
588 input: "insert /* value expression list */ into a values (a + 1, 2 * 3)",
589 }, {
590 input: "insert /* default */ into a values (default, 2 * 3)",
591 }, {
592 input: "insert /* column list */ into a(a, b) values (1, 2)",
593 }, {
594 input: "insert into a(a, b) values (1, ifnull(null, default(b)))",
595 }, {
596 input: "insert /* qualified column list */ into a(a, b) values (1, 2)",
597 }, {
598 input: "insert /* qualified columns */ into t (t.a, t.b) values (1, 2)",
599 output: "insert /* qualified columns */ into t(a, b) values (1, 2)",
600 }, {
601 input: "insert /* select */ into a select b, c from d",
602 }, {
603 input: "insert /* no cols & paren select */ into a(select * from t)",
604 output: "insert /* no cols & paren select */ into a select * from t",
605 }, {
606 input: "insert /* cols & paren select */ into a(a,b,c) (select * from t)",
607 output: "insert /* cols & paren select */ into a(a, b, c) select * from t",
608 }, {
609 input: "insert /* cols & union with paren select */ into a(b, c) (select d, e from f) union (select g from h)",
610 }, {
611 input: "insert /* on duplicate */ into a values (1, 2) on duplicate key update b = func(a), c = d",
612 }, {
613 input: "insert /* bool in insert value */ into a values (1, true, false)",
614 }, {
615 input: "insert /* bool in on duplicate */ into a values (1, 2) on duplicate key update b = false, c = d",
616 }, {
617 input: "insert /* bool in on duplicate */ into a values (1, 2, 3) on duplicate key update b = values(b), c = d",
618 }, {
619 input: "insert /* bool in on duplicate */ into a values (1, 2, 3) on duplicate key update b = values(a.b), c = d",
620 }, {
621 input: "insert /* bool expression on duplicate */ into a values (1, 2) on duplicate key update b = func(a), c = a > d",
622 }, {
623 input: "update /* simple */ a set b = 3",
624 }, {
625 input: "update /* a.b */ a.b set b = 3",
626 }, {
627 input: "update /* list */ a set b = 3, c = 4",
628 }, {
629 input: "update /* expression */ a set b = 3 + 4",
630 }, {
631 input: "update /* where */ a set b = 3 where a = b",
632 }, {
633 input: "update /* order */ a set b = 3 order by c desc",
634 }, {
635 input: "update /* limit */ a set b = 3 limit c",
636 }, {
637 input: "update /* bool in update */ a set b = true",
638 }, {
639 input: "update /* bool expr in update */ a set b = 5 > 2",
640 }, {
641 input: "update /* bool in update where */ a set b = 5 where c",
642 }, {
643 input: "update /* table qualifier */ a set a.b = 3",
644 }, {
645 input: "update /* table qualifier */ a set t.a.b = 3",
646 }, {
647 input: "update /* table alias */ tt aa set aa.cc = 3",
648 output: "update /* table alias */ tt as aa set aa.cc = 3",
649 }, {
650 input: "update (select id from foo) subqalias set id = 4",
651 output: "update (select id from foo) as subqalias set id = 4",
652 }, {
653 input: "update foo f, bar b set f.id = b.id where b.name = 'test'",
654 output: "update foo as f, bar as b set f.id = b.id where b.name = 'test'",
655 }, {
656 input: "update foo f join bar b on f.name = b.name set f.id = b.id where b.name = 'test'",
657 output: "update foo as f join bar as b on f.name = b.name set f.id = b.id where b.name = 'test'",
658 }, {
659 input: "delete /* simple */ from a",
660 }, {
661 input: "delete /* a.b */ from a.b",
662 }, {
663 input: "delete /* where */ from a where a = b",
664 }, {
665 input: "delete /* order */ from a order by b desc",
666 }, {
667 input: "delete /* limit */ from a limit b",
668 }, {
669 input: "delete a from a join b on a.id = b.id where b.name = 'test'",
670 }, {
671 input: "delete a, b from a, b where a.id = b.id and b.name = 'test'",
672 }, {
673 input: "delete from a1, a2 using t1 as a1 inner join t2 as a2 where a1.id=a2.id",
674 output: "delete a1, a2 from t1 as a1 join t2 as a2 where a1.id = a2.id",
675 }, {
676 input: "set /* simple */ a = 3",
677 }, {
678 input: "set #simple\n b = 4",
679 }, {
680 input: "set character_set_results = utf8",
681 }, {
682 input: "set @@session.autocommit = true",
683 }, {
684 input: "set @@session.`autocommit` = true",
685 }, {
686 input: "set @@session.'autocommit' = true",
687 }, {
688 input: "set @@session.\"autocommit\" = true",
689 }, {
690 input: "set names utf8 collate foo",
691 output: "set names 'utf8'",
692 }, {
693 input: "set character set utf8",
694 output: "set charset 'utf8'",
695 }, {
696 input: "set character set 'utf8'",
697 output: "set charset 'utf8'",
698 }, {
699 input: "set character set \"utf8\"",
700 output: "set charset 'utf8'",
701 }, {
702 input: "set charset default",
703 output: "set charset default",
704 }, {
705 input: "set session wait_timeout = 3600",
706 output: "set session wait_timeout = 3600",
707 }, {
708 input: "set /* list */ a = 3, b = 4",
709 }, {
710 input: "set /* mixed list */ a = 3, names 'utf8', charset 'ascii', b = 4",
711 }, {
712 input: "set session transaction isolation level repeatable read",
713 output: "set session tx_isolation = 'repeatable read'",
714 }, {
715 input: "set global transaction isolation level repeatable read",
716 output: "set global tx_isolation = 'repeatable read'",
717 }, {
718 input: "set transaction isolation level repeatable read",
719 output: "set tx_isolation = 'repeatable read'",
720 }, {
721 input: "set transaction isolation level read committed",
722 output: "set tx_isolation = 'read committed'",
723 }, {
724 input: "set transaction isolation level read uncommitted",
725 output: "set tx_isolation = 'read uncommitted'",
726 }, {
727 input: "set transaction isolation level serializable",
728 output: "set tx_isolation = 'serializable'",
729 }, {
730 input: "set transaction read write",
731 output: "set tx_read_only = 0",
732 }, {
733 input: "set transaction read only",
734 output: "set tx_read_only = 1",
735 }, {
736 input: "set tx_read_only = 1",
737 }, {
738 input: "set tx_read_only = 0",
739 }, {
740 input: "set tx_isolation = 'repeatable read'",
741 }, {
742 input: "set tx_isolation = 'read committed'",
743 }, {
744 input: "set tx_isolation = 'read uncommitted'",
745 }, {
746 input: "set tx_isolation = 'serializable'",
747 }, {
748 input: "set sql_safe_updates = 0",
749 }, {
750 input: "set sql_safe_updates = 1",
751 }, {
752 input: "alter ignore table a add foo",
753 output: "alter table a",
754 }, {
755 input: "alter table a add foo",
756 output: "alter table a",
757 }, {
758 input: "alter table a add spatial key foo (column1)",
759 output: "alter table a",
760 }, {
761 input: "alter table a add unique key foo (column1)",
762 output: "alter table a",
763 }, {
764 input: "alter table `By` add foo",
765 output: "alter table `By`",
766 }, {
767 input: "alter table a alter foo",
768 output: "alter table a",
769 }, {
770 input: "alter table a change foo",
771 output: "alter table a",
772 }, {
773 input: "alter table a modify foo",
774 output: "alter table a",
775 }, {
776 input: "alter table a drop foo",
777 output: "alter table a",
778 }, {
779 input: "alter table a disable foo",
780 output: "alter table a",
781 }, {
782 input: "alter table a enable foo",
783 output: "alter table a",
784 }, {
785 input: "alter table a order foo",
786 output: "alter table a",
787 }, {
788 input: "alter table a default foo",
789 output: "alter table a",
790 }, {
791 input: "alter table a discard foo",
792 output: "alter table a",
793 }, {
794 input: "alter table a import foo",
795 output: "alter table a",
796 }, {
797 input: "alter table a rename b",
798 output: "rename table a to b",
799 }, {
800 input: "alter table `By` rename `bY`",
801 output: "rename table `By` to `bY`",
802 }, {
803 input: "alter table a rename to b",
804 output: "rename table a to b",
805 }, {
806 input: "alter table a rename as b",
807 output: "rename table a to b",
808 }, {
809 input: "alter table a rename index foo to bar",
810 output: "alter table a",
811 }, {
812 input: "alter table a rename key foo to bar",
813 output: "alter table a",
814 }, {
815 input: "alter table e auto_increment = 20",
816 output: "alter table e",
817 }, {
818 input: "alter table e character set = 'ascii'",
819 output: "alter table e",
820 }, {
821 input: "alter table e default character set = 'ascii'",
822 output: "alter table e",
823 }, {
824 input: "alter table e comment = 'hello'",
825 output: "alter table e",
826 }, {
827 input: "alter table a reorganize partition b into (partition c values less than (?), partition d values less than (maxvalue))",
828 output: "alter table a reorganize partition b into (partition c values less than (:v1), partition d values less than (maxvalue))",
829 }, {
830 input: "alter table a partition by range (id) (partition p0 values less than (10), partition p1 values less than (maxvalue))",
831 output: "alter table a",
832 }, {
833 input: "alter table a add column id int",
834 output: "alter table a",
835 }, {
836 input: "alter table a add index idx (id)",
837 output: "alter table a",
838 }, {
839 input: "alter table a add fulltext index idx (id)",
840 output: "alter table a",
841 }, {
842 input: "alter table a add spatial index idx (id)",
843 output: "alter table a",
844 }, {
845 input: "alter table a add foreign key",
846 output: "alter table a",
847 }, {
848 input: "alter table a add primary key",
849 output: "alter table a",
850 }, {
851 input: "alter table a add constraint",
852 output: "alter table a",
853 }, {
854 input: "alter table a add id",
855 output: "alter table a",
856 }, {
857 input: "alter table a drop column id int",
858 output: "alter table a",
859 }, {
860 input: "alter table a drop partition p2712",
861 output: "alter table a",
862 }, {
863 input: "alter table a drop index idx (id)",
864 output: "alter table a",
865 }, {
866 input: "alter table a drop fulltext index idx (id)",
867 output: "alter table a",
868 }, {
869 input: "alter table a drop spatial index idx (id)",
870 output: "alter table a",
871 }, {
872 input: "alter table a drop foreign key",
873 output: "alter table a",
874 }, {
875 input: "alter table a drop primary key",
876 output: "alter table a",
877 }, {
878 input: "alter table a drop constraint",
879 output: "alter table a",
880 }, {
881 input: "alter table a drop id",
882 output: "alter table a",
883 }, {
884 input: "alter table a add vindex hash (id)",
885 }, {
886 input: "alter table a add vindex `hash` (`id`)",
887 output: "alter table a add vindex hash (id)",
888 }, {
889 input: "alter table a add vindex hash (id) using `hash`",
890 output: "alter table a add vindex hash (id) using hash",
891 }, {
892 input: "alter table a add vindex `add` (`add`)",
893 }, {
894 input: "alter table a add vindex hash (id) using hash",
895 }, {
896 input: "alter table a add vindex hash (id) using `hash`",
897 output: "alter table a add vindex hash (id) using hash",
898 }, {
899 input: "alter table user add vindex name_lookup_vdx (name) using lookup_hash with owner=user, table=name_user_idx, from=name, to=user_id",
900 }, {
901 input: "alter table user2 add vindex name_lastname_lookup_vdx (name,lastname) using lookup with owner=`user`, table=`name_lastname_keyspace_id_map`, from=`name,lastname`, to=`keyspace_id`",
902 output: "alter table user2 add vindex name_lastname_lookup_vdx (name, lastname) using lookup with owner=user, table=name_lastname_keyspace_id_map, from=name,lastname, to=keyspace_id",
903 }, {
904 input: "alter table a drop vindex hash",
905 }, {
906 input: "alter table a drop vindex `hash`",
907 output: "alter table a drop vindex hash",
908 }, {
909 input: "alter table a drop vindex hash",
910 output: "alter table a drop vindex hash",
911 }, {
912 input: "alter table a drop vindex `add`",
913 output: "alter table a drop vindex `add`",
914 }, {
915 input: "create table a",
916 }, {
917 input: "create table a (\n\t`a` int\n)",
918 output: "create table a (\n\ta int\n)",
919 }, {
920 input: "create table `by` (\n\t`by` char\n)",
921 }, {
922 input: "create table if not exists a (\n\t`a` int\n)",
923 output: "create table a (\n\ta int\n)",
924 }, {
925 input: "create table a ignore me this is garbage",
926 output: "create table a",
927 }, {
928 input: "create table a (a int, b char, c garbage)",
929 output: "create table a",
930 }, {
931 input: "create vindex hash_vdx using hash",
932 }, {
933 input: "create vindex lookup_vdx using lookup with owner=user, table=name_user_idx, from=name, to=user_id",
934 }, {
935 input: "create vindex xyz_vdx using xyz with param1=hello, param2='world', param3=123",
936 }, {
937 input: "create index a on b",
938 output: "alter table b",
939 }, {
940 input: "create unique index a on b",
941 output: "alter table b",
942 }, {
943 input: "create unique index a using foo on b",
944 output: "alter table b",
945 }, {
946 input: "create fulltext index a using foo on b",
947 output: "alter table b",
948 }, {
949 input: "create spatial index a using foo on b",
950 output: "alter table b",
951 }, {
952 input: "create view a",
953 output: "create table a",
954 }, {
955 input: "create or replace view a",
956 output: "create table a",
957 }, {
958 input: "alter view a",
959 output: "alter table a",
960 }, {
961 input: "drop view a",
962 output: "drop table a",
963 }, {
964 input: "drop table a",
965 output: "drop table a",
966 }, {
967 input: "drop table if exists a",
968 output: "drop table if exists a",
969 }, {
970 input: "drop view if exists a",
971 output: "drop table if exists a",
972 }, {
973 input: "drop index b on a",
974 output: "alter table a",
975 }, {
976 input: "analyze table a",
977 output: "alter table a",
978 }, {
979 input: "show binary logs",
980 output: "show binary logs",
981 }, {
982 input: "show binlog events",
983 output: "show binlog",
984 }, {
985 input: "show character set",
986 output: "show character set",
987 }, {
988 input: "show character set like '%foo'",
989 output: "show character set",
990 }, {
991 input: "show collation",
992 output: "show collation",
993 }, {
994 input: "show create database d",
995 output: "show create database",
996 }, {
997 input: "show create event e",
998 output: "show create event",
999 }, {
1000 input: "show create function f",
1001 output: "show create function",
1002 }, {
1003 input: "show create procedure p",
1004 output: "show create procedure",
1005 }, {
1006 input: "show create table t",
1007 output: "show create table",
1008 }, {
1009 input: "show create trigger t",
1010 output: "show create trigger",
1011 }, {
1012 input: "show create user u",
1013 output: "show create user",
1014 }, {
1015 input: "show create view v",
1016 output: "show create view",
1017 }, {
1018 input: "show databases",
1019 output: "show databases",
1020 }, {
1021 input: "show engine INNODB",
1022 output: "show engine",
1023 }, {
1024 input: "show engines",
1025 output: "show engines",
1026 }, {
1027 input: "show storage engines",
1028 output: "show storage",
1029 }, {
1030 input: "show errors",
1031 output: "show errors",
1032 }, {
1033 input: "show events",
1034 output: "show events",
1035 }, {
1036 input: "show function code func",
1037 output: "show function",
1038 }, {
1039 input: "show function status",
1040 output: "show function",
1041 }, {
1042 input: "show grants for 'root@localhost'",
1043 output: "show grants",
1044 }, {
1045 input: "show index from table",
1046 output: "show index",
1047 }, {
1048 input: "show indexes from table",
1049 output: "show indexes",
1050 }, {
1051 input: "show keys from table",
1052 output: "show keys",
1053 }, {
1054 input: "show master status",
1055 output: "show master",
1056 }, {
1057 input: "show open tables",
1058 output: "show open",
1059 }, {
1060 input: "show plugins",
1061 output: "show plugins",
1062 }, {
1063 input: "show privileges",
1064 output: "show privileges",
1065 }, {
1066 input: "show procedure code p",
1067 output: "show procedure",
1068 }, {
1069 input: "show procedure status",
1070 output: "show procedure",
1071 }, {
1072 input: "show processlist",
1073 output: "show processlist",
1074 }, {
1075 input: "show full processlist",
1076 output: "show processlist",
1077 }, {
1078 input: "show profile cpu for query 1",
1079 output: "show profile",
1080 }, {
1081 input: "show profiles",
1082 output: "show profiles",
1083 }, {
1084 input: "show relaylog events",
1085 output: "show relaylog",
1086 }, {
1087 input: "show slave hosts",
1088 output: "show slave",
1089 }, {
1090 input: "show slave status",
1091 output: "show slave",
1092 }, {
1093 input: "show status",
1094 output: "show status",
1095 }, {
1096 input: "show global status",
1097 output: "show global status",
1098 }, {
1099 input: "show session status",
1100 output: "show session status",
1101 }, {
1102 input: "show table status",
1103 output: "show table",
1104 }, {
1105 input: "show tables",
1106 }, {
1107 input: "show tables like '%keyspace%'",
1108 }, {
1109 input: "show tables where 1 = 0",
1110 }, {
1111 input: "show tables from a",
1112 }, {
1113 input: "show tables from a where 1 = 0",
1114 }, {
1115 input: "show tables from a like '%keyspace%'",
1116 }, {
1117 input: "show full tables",
1118 }, {
1119 input: "show full tables from a",
1120 }, {
1121 input: "show full tables in a",
1122 output: "show full tables from a",
1123 }, {
1124 input: "show full tables from a like '%keyspace%'",
1125 }, {
1126 input: "show full tables from a where 1 = 0",
1127 }, {
1128 input: "show full tables like '%keyspace%'",
1129 }, {
1130 input: "show full tables where 1 = 0",
1131 }, {
1132 input: "show triggers",
1133 output: "show triggers",
1134 }, {
1135 input: "show variables",
1136 output: "show variables",
1137 }, {
1138 input: "show global variables",
1139 output: "show global variables",
1140 }, {
1141 input: "show session variables",
1142 output: "show session variables",
1143 }, {
1144 input: "show vindexes",
1145 output: "show vindexes",
1146 }, {
1147 input: "show vindexes on t",
1148 output: "show vindexes on t",
1149 }, {
1150 input: "show vitess_keyspaces",
1151 }, {
1152 input: "show vitess_shards",
1153 }, {
1154 input: "show vitess_tablets",
1155 }, {
1156 input: "show vschema_tables",
1157 }, {
1158 input: "show warnings",
1159 output: "show warnings",
1160 }, {
1161 input: "show foobar",
1162 output: "show foobar",
1163 }, {
1164 input: "show foobar like select * from table where syntax is 'ignored'",
1165 output: "show foobar",
1166 }, {
1167 input: "use db",
1168 output: "use db",
1169 }, {
1170 input: "use duplicate",
1171 output: "use `duplicate`",
1172 }, {
1173 input: "use `ks:-80@master`",
1174 output: "use `ks:-80@master`",
1175 }, {
1176 input: "describe foobar",
1177 output: "otherread",
1178 }, {
1179 input: "desc foobar",
1180 output: "otherread",
1181 }, {
1182 input: "explain foobar",
1183 output: "otherread",
1184 }, {
1185 input: "truncate table foo",
1186 output: "truncate table foo",
1187 }, {
1188 input: "truncate foo",
1189 output: "truncate table foo",
1190 }, {
1191 input: "repair foo",
1192 output: "otheradmin",
1193 }, {
1194 input: "optimize foo",
1195 output: "otheradmin",
1196 }, {
1197 input: "select /* EQ true */ 1 from t where a = true",
1198 }, {
1199 input: "select /* EQ false */ 1 from t where a = false",
1200 }, {
1201 input: "select /* NE true */ 1 from t where a != true",
1202 }, {
1203 input: "select /* NE false */ 1 from t where a != false",
1204 }, {
1205 input: "select /* LT true */ 1 from t where a < true",
1206 }, {
1207 input: "select /* LT false */ 1 from t where a < false",
1208 }, {
1209 input: "select /* GT true */ 1 from t where a > true",
1210 }, {
1211 input: "select /* GT false */ 1 from t where a > false",
1212 }, {
1213 input: "select /* LE true */ 1 from t where a <= true",
1214 }, {
1215 input: "select /* LE false */ 1 from t where a <= false",
1216 }, {
1217 input: "select /* GE true */ 1 from t where a >= true",
1218 }, {
1219 input: "select /* GE false */ 1 from t where a >= false",
1220 }, {
1221 input: "select * from t order by a collate utf8_general_ci",
1222 output: "select * from t order by a collate utf8_general_ci asc",
1223 }, {
1224 input: "select k collate latin1_german2_ci as k1 from t1 order by k1 asc",
1225 }, {
1226 input: "select * from t group by a collate utf8_general_ci",
1227 }, {
1228 input: "select MAX(k collate latin1_german2_ci) from t1",
1229 }, {
1230 input: "select distinct k collate latin1_german2_ci from t1",
1231 }, {
1232 input: "select * from t1 where 'Müller' collate latin1_german2_ci = k",
1233 }, {
1234 input: "select * from t1 where k like 'Müller' collate latin1_german2_ci",
1235 }, {
1236 input: "select k from t1 group by k having k = 'Müller' collate latin1_german2_ci",
1237 }, {
1238 input: "select k from t1 join t2 order by a collate latin1_german2_ci asc, b collate latin1_german2_ci asc",
1239 }, {
1240 input: "select k collate 'latin1_german2_ci' as k1 from t1 order by k1 asc",
1241 output: "select k collate latin1_german2_ci as k1 from t1 order by k1 asc",
1242 }, {
1243 input: "select /* drop trailing semicolon */ 1 from dual;",
1244 output: "select /* drop trailing semicolon */ 1 from dual",
1245 }, {
1246 input: "select /* cache directive */ sql_no_cache 'foo' from t",
1247 }, {
1248 input: "select binary 'a' = 'A' from t",
1249 }, {
1250 input: "select 1 from t where foo = _binary 'bar'",
1251 }, {
1252 input: "select 1 from t where foo = _binary'bar'",
1253 output: "select 1 from t where foo = _binary 'bar'",
1254 }, {
1255 input: "select match(a) against ('foo') from t",
1256 }, {
1257 input: "select match(a1, a2) against ('foo' in natural language mode with query expansion) from t",
1258 }, {
1259 input: "select title from video as v where match(v.title, v.tag) against ('DEMO' in boolean mode)",
1260 }, {
1261 input: "select name, group_concat(score) from t group by name",
1262 }, {
1263 input: "select name, group_concat(distinct id, score order by id desc separator ':') from t group by name",
1264 }, {
1265 input: "select * from t partition (p0)",
1266 }, {
1267 input: "select * from t partition (p0, p1)",
1268 }, {
1269 input: "select e.id, s.city from employees as e join stores partition (p1) as s on e.store_id = s.id",
1270 }, {
1271 input: "select truncate(120.3333, 2) from dual",
1272 }, {
1273 input: "update t partition (p0) set a = 1",
1274 }, {
1275 input: "insert into t partition (p0) values (1, 'asdf')",
1276 }, {
1277 input: "insert into t1 select * from t2 partition (p0)",
1278 }, {
1279 input: "replace into t partition (p0) values (1, 'asdf')",
1280 }, {
1281 input: "delete from t partition (p0) where a = 1",
1282 }, {
1283 input: "stream * from t",
1284 }, {
1285 input: "stream /* comment */ * from t",
1286 }, {
1287 input: "begin",
1288 }, {
1289 input: "start transaction",
1290 output: "begin",
1291 }, {
1292 input: "commit",
1293 }, {
1294 input: "rollback",
1295 }, {
1296 input: "create database test_db",
1297 }, {
1298 input: "create schema test_db",
1299 output: "create database test_db",
1300 }, {
1301 input: "create database if not exists test_db",
1302 output: "create database test_db",
1303 }, {
1304 input: "drop database test_db",
1305 }, {
1306 input: "drop schema test_db",
1307 output: "drop database test_db",
1308 }, {
1309 input: "drop database if exists test_db",
1310 output: "drop database test_db",
1311 }}
1312 )
1313
1314 func TestValid(t *testing.T) {
1315 for _, tcase := range validSQL {
1316 if tcase.output == "" {
1317 tcase.output = tcase.input
1318 }
1319 tree, err := Parse(tcase.input)
1320 if err != nil {
1321 t.Errorf("Parse(%q) err: %v, want nil", tcase.input, err)
1322 continue
1323 }
1324 out := String(tree)
1325 if out != tcase.output {
1326 t.Errorf("Parse(%q) = %q, want: %q", tcase.input, out, tcase.output)
1327 }
1328 // This test just exercises the tree walking functionality.
1329 // There's no way automated way to verify that a node calls
1330 // all its children. But we can examine code coverage and
1331 // ensure that all walkSubtree functions were called.
1332 Walk(func(node SQLNode) (bool, error) {
1333 return true, nil
1334 }, tree)
1335 }
1336 }
1337
1338 func TestCaseSensitivity(t *testing.T) {
1339 validSQL := []struct {
1340 input string
1341 output string
1342 }{{
1343 input: "create table A (\n\t`B` int\n)",
1344 output: "create table A (\n\tB int\n)",
1345 }, {
1346 input: "create index b on A",
1347 output: "alter table A",
1348 }, {
1349 input: "alter table A foo",
1350 output: "alter table A",
1351 }, {
1352 input: "alter table A convert",
1353 output: "alter table A",
1354 }, {
1355 // View names get lower-cased.
1356 input: "alter view A foo",
1357 output: "alter table a",
1358 }, {
1359 input: "alter table A rename to B",
1360 output: "rename table A to B",
1361 }, {
1362 input: "rename table A to B",
1363 }, {
1364 input: "drop table B",
1365 output: "drop table B",
1366 }, {
1367 input: "drop table if exists B",
1368 output: "drop table if exists B",
1369 }, {
1370 input: "drop index b on A",
1371 output: "alter table A",
1372 }, {
1373 input: "select a from B",
1374 }, {
1375 input: "select A as B from C",
1376 }, {
1377 input: "select B.* from c",
1378 }, {
1379 input: "select B.A from c",
1380 }, {
1381 input: "select * from B as C",
1382 }, {
1383 input: "select * from A.B",
1384 }, {
1385 input: "update A set b = 1",
1386 }, {
1387 input: "update A.B set b = 1",
1388 }, {
1389 input: "select A() from b",
1390 }, {
1391 input: "select A(B, C) from b",
1392 }, {
1393 input: "select A(distinct B, C) from b",
1394 }, {
1395 // IF is an exception. It's always lower-cased.
1396 input: "select IF(B, C) from b",
1397 output: "select if(B, C) from b",
1398 }, {
1399 input: "select * from b use index (A)",
1400 }, {
1401 input: "insert into A(A, B) values (1, 2)",
1402 }, {
1403 input: "CREATE TABLE A (\n\t`A` int\n)",
1404 output: "create table A (\n\tA int\n)",
1405 }, {
1406 input: "create view A",
1407 output: "create table a",
1408 }, {
1409 input: "alter view A",
1410 output: "alter table a",
1411 }, {
1412 input: "drop view A",
1413 output: "drop table a",
1414 }, {
1415 input: "drop view if exists A",
1416 output: "drop table if exists a",
1417 }, {
1418 input: "select /* lock in SHARE MODE */ 1 from t lock in SHARE MODE",
1419 output: "select /* lock in SHARE MODE */ 1 from t lock in share mode",
1420 }, {
1421 input: "select next VALUE from t",
1422 output: "select next 1 values from t",
1423 }, {
1424 input: "select /* use */ 1 from t1 use index (A) where b = 1",
1425 }}
1426 for _, tcase := range validSQL {
1427 if tcase.output == "" {
1428 tcase.output = tcase.input
1429 }
1430 tree, err := Parse(tcase.input)
1431 if err != nil {
1432 t.Errorf("input: %s, err: %v", tcase.input, err)
1433 continue
1434 }
1435 out := String(tree)
1436 if out != tcase.output {
1437 t.Errorf("out: %s, want %s", out, tcase.output)
1438 }
1439 }
1440 }
1441
1442 func TestKeywords(t *testing.T) {
1443 validSQL := []struct {
1444 input string
1445 output string
1446 }{{
1447 input: "select current_timestamp",
1448 output: "select current_timestamp() from dual",
1449 }, {
1450 input: "update t set a = current_timestamp()",
1451 }, {
1452 input: "select a, current_date from t",
1453 output: "select a, current_date() from t",
1454 }, {
1455 input: "insert into t(a, b) values (current_date, current_date())",
1456 output: "insert into t(a, b) values (current_date(), current_date())",
1457 }, {
1458 input: "select * from t where a > utc_timestmp()",
1459 }, {
1460 input: "update t set b = utc_timestamp + 5",
1461 output: "update t set b = utc_timestamp() + 5",
1462 }, {
1463 input: "select utc_time, utc_date",
1464 output: "select utc_time(), utc_date() from dual",
1465 }, {
1466 input: "select 1 from dual where localtime > utc_time",
1467 output: "select 1 from dual where localtime() > utc_time()",
1468 }, {
1469 input: "update t set a = localtimestamp(), b = utc_timestamp",
1470 output: "update t set a = localtimestamp(), b = utc_timestamp()",
1471 }, {
1472 input: "insert into t(a) values (unix_timestamp)",
1473 }, {
1474 input: "select replace(a, 'foo', 'bar') from t",
1475 }, {
1476 input: "update t set a = replace('1234', '2', '1')",
1477 }, {
1478 input: "insert into t(a, b) values ('foo', 'bar') on duplicate key update a = replace(hex('foo'), 'f', 'b')",
1479 }, {
1480 input: "update t set a = left('1234', 3)",
1481 }, {
1482 input: "select left(a, 5) from t",
1483 }, {
1484 input: "update t set d = adddate(date('2003-12-31 01:02:03'), interval 5 days)",
1485 }, {
1486 input: "insert into t(a, b) values (left('foo', 1), 'b')",
1487 }, {
1488 input: "insert /* qualified function */ into t(a, b) values (test.PI(), 'b')",
1489 }, {
1490 input: "select /* keyword in qualified id */ * from t join z on t.key = z.key",
1491 output: "select /* keyword in qualified id */ * from t join z on t.`key` = z.`key`",
1492 }, {
1493 input: "select /* non-reserved keywords as unqualified cols */ date, view, offset from t",
1494 output: "select /* non-reserved keywords as unqualified cols */ `date`, `view`, `offset` from t",
1495 }, {
1496 input: "select /* share and mode as cols */ share, mode from t where share = 'foo'",
1497 output: "select /* share and mode as cols */ `share`, `mode` from t where `share` = 'foo'",
1498 }, {
1499 input: "select /* unused keywords as cols */ write, varying from t where trailing = 'foo'",
1500 output: "select /* unused keywords as cols */ `write`, `varying` from t where `trailing` = 'foo'",
1501 }, {
1502 input: "select status from t",
1503 output: "select `status` from t",
1504 }, {
1505 input: "select variables from t",
1506 output: "select `variables` from t",
1507 }}
1508
1509 for _, tcase := range validSQL {
1510 if tcase.output == "" {
1511 tcase.output = tcase.input
1512 }
1513 tree, err := Parse(tcase.input)
1514 if err != nil {
1515 t.Errorf("input: %s, err: %v", tcase.input, err)
1516 continue
1517 }
1518 out := String(tree)
1519 if out != tcase.output {
1520 t.Errorf("out: %s, want %s", out, tcase.output)
1521 }
1522 }
1523 }
1524
1525 func TestConvert(t *testing.T) {
1526 validSQL := []struct {
1527 input string
1528 output string
1529 }{{
1530 input: "select cast('abc' as date) from t",
1531 output: "select convert('abc', date) from t",
1532 }, {
1533 input: "select convert('abc', binary(4)) from t",
1534 }, {
1535 input: "select convert('abc', binary) from t",
1536 }, {
1537 input: "select convert('abc', char character set binary) from t",
1538 }, {
1539 input: "select convert('abc', char(4) ascii) from t",
1540 }, {
1541 input: "select convert('abc', char unicode) from t",
1542 }, {
1543 input: "select convert('abc', char(4)) from t",
1544 }, {
1545 input: "select convert('abc', char) from t",
1546 }, {
1547 input: "select convert('abc', nchar(4)) from t",
1548 }, {
1549 input: "select convert('abc', nchar) from t",
1550 }, {
1551 input: "select convert('abc', signed) from t",
1552 }, {
1553 input: "select convert('abc', signed integer) from t",
1554 output: "select convert('abc', signed) from t",
1555 }, {
1556 input: "select convert('abc', unsigned) from t",
1557 }, {
1558 input: "select convert('abc', unsigned integer) from t",
1559 output: "select convert('abc', unsigned) from t",
1560 }, {
1561 input: "select convert('abc', decimal(3, 4)) from t",
1562 }, {
1563 input: "select convert('abc', decimal(4)) from t",
1564 }, {
1565 input: "select convert('abc', decimal) from t",
1566 }, {
1567 input: "select convert('abc', date) from t",
1568 }, {
1569 input: "select convert('abc', time(4)) from t",
1570 }, {
1571 input: "select convert('abc', time) from t",
1572 }, {
1573 input: "select convert('abc', datetime(9)) from t",
1574 }, {
1575 input: "select convert('abc', datetime) from t",
1576 }, {
1577 input: "select convert('abc', json) from t",
1578 }, {
1579 input: "select convert('abc' using ascii) from t",
1580 }}
1581
1582 for _, tcase := range validSQL {
1583 if tcase.output == "" {
1584 tcase.output = tcase.input
1585 }
1586 tree, err := Parse(tcase.input)
1587 if err != nil {
1588 t.Errorf("input: %s, err: %v", tcase.input, err)
1589 continue
1590 }
1591 out := String(tree)
1592 if out != tcase.output {
1593 t.Errorf("out: %s, want %s", out, tcase.output)
1594 }
1595 }
1596
1597 invalidSQL := []struct {
1598 input string
1599 output string
1600 }{{
1601 input: "select convert('abc' as date) from t",
1602 output: "syntax error at position 24 near 'as'",
1603 }, {
1604 input: "select convert from t",
1605 output: "syntax error at position 20 near 'from'",
1606 }, {
1607 input: "select cast('foo', decimal) from t",
1608 output: "syntax error at position 19",
1609 }, {
1610 input: "select convert('abc', datetime(4+9)) from t",
1611 output: "syntax error at position 34",
1612 }, {
1613 input: "select convert('abc', decimal(4+9)) from t",
1614 output: "syntax error at position 33",
1615 }}
1616
1617 for _, tcase := range invalidSQL {
1618 _, err := Parse(tcase.input)
1619 if err == nil || err.Error() != tcase.output {
1620 t.Errorf("%s: %v, want %s", tcase.input, err, tcase.output)
1621 }
1622 }
1623 }
1624
1625 func TestSubStr(t *testing.T) {
1626
1627 validSQL := []struct {
1628 input string
1629 output string
1630 }{{
1631 input: "select substr(a, 1) from t",
1632 }, {
1633 input: "select substr(a, 1, 6) from t",
1634 }, {
1635 input: "select substring(a, 1) from t",
1636 output: "select substr(a, 1) from t",
1637 }, {
1638 input: "select substring(a, 1, 6) from t",
1639 output: "select substr(a, 1, 6) from t",
1640 }, {
1641 input: "select substr(a from 1 for 6) from t",
1642 output: "select substr(a, 1, 6) from t",
1643 }, {
1644 input: "select substring(a from 1 for 6) from t",
1645 output: "select substr(a, 1, 6) from t",
1646 }}
1647
1648 for _, tcase := range validSQL {
1649 if tcase.output == "" {
1650 tcase.output = tcase.input
1651 }
1652 tree, err := Parse(tcase.input)
1653 if err != nil {
1654 t.Errorf("input: %s, err: %v", tcase.input, err)
1655 continue
1656 }
1657 out := String(tree)
1658 if out != tcase.output {
1659 t.Errorf("out: %s, want %s", out, tcase.output)
1660 }
1661 }
1662 }
1663
1664 func TestCreateTable(t *testing.T) {
1665 validSQL := []string{
1666 // test all the data types and options
1667 "create table t (\n" +
1668 " col_bit bit,\n" +
1669 " col_tinyint tinyint auto_increment,\n" +
1670 " col_tinyint3 tinyint(3) unsigned,\n" +
1671 " col_smallint smallint,\n" +
1672 " col_smallint4 smallint(4) zerofill,\n" +
1673 " col_mediumint mediumint,\n" +
1674 " col_mediumint5 mediumint(5) unsigned not null,\n" +
1675 " col_int int,\n" +
1676 " col_int10 int(10) not null,\n" +
1677 " col_integer integer comment 'this is an integer',\n" +
1678 " col_bigint bigint,\n" +
1679 " col_bigint10 bigint(10) zerofill not null default 10,\n" +
1680 " col_real real,\n" +
1681 " col_real2 real(1,2) not null default 1.23,\n" +
1682 " col_double double,\n" +
1683 " col_double2 double(3,4) not null default 1.23,\n" +
1684 " col_float float,\n" +
1685 " col_float2 float(3,4) not null default 1.23,\n" +
1686 " col_decimal decimal,\n" +
1687 " col_decimal2 decimal(2),\n" +
1688 " col_decimal3 decimal(2,3),\n" +
1689 " col_numeric numeric,\n" +
1690 " col_numeric2 numeric(2),\n" +
1691 " col_numeric3 numeric(2,3),\n" +
1692 " col_date date,\n" +
1693 " col_time time,\n" +
1694 " col_timestamp timestamp,\n" +
1695 " col_datetime datetime,\n" +
1696 " col_year year,\n" +
1697 " col_char char,\n" +
1698 " col_char2 char(2),\n" +
1699 " col_char3 char(3) character set ascii,\n" +
1700 " col_char4 char(4) character set ascii collate ascii_bin,\n" +
1701 " col_varchar varchar,\n" +
1702 " col_varchar2 varchar(2),\n" +
1703 " col_varchar3 varchar(3) character set ascii,\n" +
1704 " col_varchar4 varchar(4) character set ascii collate ascii_bin,\n" +
1705 " col_binary binary,\n" +
1706 " col_varbinary varbinary(10),\n" +
1707 " col_tinyblob tinyblob,\n" +
1708 " col_blob blob,\n" +
1709 " col_mediumblob mediumblob,\n" +
1710 " col_longblob longblob,\n" +
1711 " col_tinytext tinytext,\n" +
1712 " col_text text,\n" +
1713 " col_mediumtext mediumtext,\n" +
1714 " col_longtext longtext,\n" +
1715 " col_text text character set ascii collate ascii_bin,\n" +
1716 " col_json json,\n" +
1717 " col_enum enum('a', 'b', 'c', 'd'),\n" +
1718 " col_enum2 enum('a', 'b', 'c', 'd') character set ascii,\n" +
1719 " col_enum3 enum('a', 'b', 'c', 'd') collate ascii_bin,\n" +
1720 " col_enum4 enum('a', 'b', 'c', 'd') character set ascii collate ascii_bin,\n" +
1721 " col_set set('a', 'b', 'c', 'd'),\n" +
1722 " col_set2 set('a', 'b', 'c', 'd') character set ascii,\n" +
1723 " col_set3 set('a', 'b', 'c', 'd') collate ascii_bin,\n" +
1724 " col_set4 set('a', 'b', 'c', 'd') character set ascii collate ascii_bin,\n" +
1725 " col_geometry1 geometry,\n" +
1726 " col_geometry2 geometry not null,\n" +
1727 " col_point1 point,\n" +
1728 " col_point2 point not null,\n" +
1729 " col_linestring1 linestring,\n" +
1730 " col_linestring2 linestring not null,\n" +
1731 " col_polygon1 polygon,\n" +
1732 " col_polygon2 polygon not null,\n" +
1733 " col_geometrycollection1 geometrycollection,\n" +
1734 " col_geometrycollection2 geometrycollection not null,\n" +
1735 " col_multipoint1 multipoint,\n" +
1736 " col_multipoint2 multipoint not null,\n" +
1737 " col_multilinestring1 multilinestring,\n" +
1738 " col_multilinestring2 multilinestring not null,\n" +
1739 " col_multipolygon1 multipolygon,\n" +
1740 " col_multipolygon2 multipolygon not null\n" +
1741 ")",
1742
1743 // test defaults
1744 "create table t (\n" +
1745 " i1 int default 1,\n" +
1746 " i2 int default null,\n" +
1747 " f1 float default 1.23,\n" +
1748 " s1 varchar default 'c',\n" +
1749 " s2 varchar default 'this is a string',\n" +
1750 " s3 varchar default null,\n" +
1751 " s4 timestamp default current_timestamp,\n" +
1752 " s5 bit(1) default B'0'\n" +
1753 ")",
1754
1755 // test key field options
1756 "create table t (\n" +
1757 " id int auto_increment primary key,\n" +
1758 " username varchar unique key,\n" +
1759 " email varchar unique,\n" +
1760 " full_name varchar key,\n" +
1761 " time1 timestamp on update current_timestamp,\n" +
1762 " time2 timestamp default current_timestamp on update current_timestamp\n" +
1763 ")",
1764
1765 // test defining indexes separately
1766 "create table t (\n" +
1767 " id int auto_increment,\n" +
1768 " username varchar,\n" +
1769 " email varchar,\n" +
1770 " full_name varchar,\n" +
1771 " geom point not null,\n" +
1772 " status_nonkeyword varchar,\n" +
1773 " primary key (id),\n" +
1774 " spatial key geom (geom),\n" +
1775 " unique key by_username (username),\n" +
1776 " unique by_username2 (username),\n" +
1777 " unique index by_username3 (username),\n" +
1778 " index by_status (status_nonkeyword),\n" +
1779 " key by_full_name (full_name)\n" +
1780 ")",
1781
1782 // test that indexes support USING <id>
1783 "create table t (\n" +
1784 " id int auto_increment,\n" +
1785 " username varchar,\n" +
1786 " email varchar,\n" +
1787 " full_name varchar,\n" +
1788 " status_nonkeyword varchar,\n" +
1789 " primary key (id) using BTREE,\n" +
1790 " unique key by_username (username) using HASH,\n" +
1791 " unique by_username2 (username) using OTHER,\n" +
1792 " unique index by_username3 (username) using XYZ,\n" +
1793 " index by_status (status_nonkeyword) using PDQ,\n" +
1794 " key by_full_name (full_name) using OTHER\n" +
1795 ")",
1796 // test other index options
1797 "create table t (\n" +
1798 " id int auto_increment,\n" +
1799 " username varchar,\n" +
1800 " email varchar,\n" +
1801 " primary key (id) comment 'hi',\n" +
1802 " unique key by_username (username) key_block_size 8,\n" +
1803 " unique index by_username4 (username) comment 'hi' using BTREE,\n" +
1804 " unique index by_username4 (username) using BTREE key_block_size 4 comment 'hi'\n" +
1805 ")",
1806
1807 // multi-column indexes
1808 "create table t (\n" +
1809 " id int auto_increment,\n" +
1810 " username varchar,\n" +
1811 " email varchar,\n" +
1812 " full_name varchar,\n" +
1813 " a int,\n" +
1814 " b int,\n" +
1815 " c int,\n" +
1816 " primary key (id, username),\n" +
1817 " unique key by_abc (a, b, c),\n" +
1818 " key by_email (email(10), username)\n" +
1819 ")",
1820
1821 // table options
1822 "create table t (\n" +
1823 " id int auto_increment\n" +
1824 ") engine InnoDB,\n" +
1825 " auto_increment 123,\n" +
1826 " avg_row_length 1,\n" +
1827 " default character set utf8mb4,\n" +
1828 " character set latin1,\n" +
1829 " checksum 0,\n" +
1830 " default collate binary,\n" +
1831 " collate ascii_bin,\n" +
1832 " comment 'this is a comment',\n" +
1833 " compression 'zlib',\n" +
1834 " connection 'connect_string',\n" +
1835 " data directory 'absolute path to directory',\n" +
1836 " delay_key_write 1,\n" +
1837 " encryption 'n',\n" +
1838 " index directory 'absolute path to directory',\n" +
1839 " insert_method no,\n" +
1840 " key_block_size 1024,\n" +
1841 " max_rows 100,\n" +
1842 " min_rows 10,\n" +
1843 " pack_keys 0,\n" +
1844 " password 'sekret',\n" +
1845 " row_format default,\n" +
1846 " stats_auto_recalc default,\n" +
1847 " stats_persistent 0,\n" +
1848 " stats_sample_pages 1,\n" +
1849 " tablespace tablespace_name storage disk,\n" +
1850 " tablespace tablespace_name\n",
1851 }
1852 for _, sql := range validSQL {
1853 sql = strings.TrimSpace(sql)
1854 tree, err := ParseStrictDDL(sql)
1855 if err != nil {
1856 t.Errorf("input: %s, err: %v", sql, err)
1857 continue
1858 }
1859 got := String(tree.(*DDL))
1860
1861 if sql != got {
1862 t.Errorf("want:\n%s\ngot:\n%s", sql, got)
1863 }
1864 }
1865
1866 sql := "create table t garbage"
1867 tree, err := Parse(sql)
1868 if err != nil {
1869 t.Errorf("input: %s, err: %v", sql, err)
1870 }
1871
1872 tree, err = ParseStrictDDL(sql)
1873 if tree != nil || err == nil {
1874 t.Errorf("ParseStrictDDL unexpectedly accepted input %s", sql)
1875 }
1876
1877 testCases := []struct {
1878 input string
1879 output string
1880 }{{
1881 // test key_block_size
1882 input: "create table t (\n" +
1883 " id int auto_increment,\n" +
1884 " username varchar,\n" +
1885 " unique key by_username (username) key_block_size 8,\n" +
1886 " unique key by_username2 (username) key_block_size=8,\n" +
1887 " unique by_username3 (username) key_block_size = 4\n" +
1888 ")",
1889 output: "create table t (\n" +
1890 " id int auto_increment,\n" +
1891 " username varchar,\n" +
1892 " unique key by_username (username) key_block_size 8,\n" +
1893 " unique key by_username2 (username) key_block_size 8,\n" +
1894 " unique by_username3 (username) key_block_size 4\n" +
1895 ")",
1896 },
1897 }
1898 for _, tcase := range testCases {
1899 tree, err := ParseStrictDDL(tcase.input)
1900 if err != nil {
1901 t.Errorf("input: %s, err: %v", tcase.input, err)
1902 continue
1903 }
1904 if got, want := String(tree.(*DDL)), tcase.output; got != want {
1905 t.Errorf("Parse(%s):\n%s, want\n%s", tcase.input, got, want)
1906 }
1907 }
1908 }
1909
1910 func TestCreateTableEscaped(t *testing.T) {
1911 testCases := []struct {
1912 input string
1913 output string
1914 }{{
1915 input: "create table `a`(`id` int, primary key(`id`))",
1916 output: "create table a (\n" +
1917 "\tid int,\n" +
1918 "\tprimary key (id)\n" +
1919 ")",
1920 }, {
1921 input: "create table `insert`(`update` int, primary key(`delete`))",
1922 output: "create table `insert` (\n" +
1923 "\t`update` int,\n" +
1924 "\tprimary key (`delete`)\n" +
1925 ")",
1926 }}
1927 for _, tcase := range testCases {
1928 tree, err := ParseStrictDDL(tcase.input)
1929 if err != nil {
1930 t.Errorf("input: %s, err: %v", tcase.input, err)
1931 continue
1932 }
1933 if got, want := String(tree.(*DDL)), tcase.output; got != want {
1934 t.Errorf("Parse(%s):\n%s, want\n%s", tcase.input, got, want)
1935 }
1936 }
1937 }
1938
1939 var (
1940 invalidSQL = []struct {
1941 input string
1942 output string
1943 excludeMulti bool // Don't use in the ParseNext multi-statement parsing tests.
1944 }{{
1945 input: "select $ from t",
1946 output: "syntax error at position 9 near '$'",
1947 }, {
1948 input: "select : from t",
1949 output: "syntax error at position 9 near ':'",
1950 }, {
1951 input: "select 0xH from t",
1952 output: "syntax error at position 10 near '0x'",
1953 }, {
1954 input: "select x'78 from t",
1955 output: "syntax error at position 12 near '78'",
1956 }, {
1957 input: "select x'777' from t",
1958 output: "syntax error at position 14 near '777'",
1959 }, {
1960 input: "select * from t where :1 = 2",
1961 output: "syntax error at position 24 near ':'",
1962 }, {
1963 input: "select * from t where :. = 2",
1964 output: "syntax error at position 24 near ':'",
1965 }, {
1966 input: "select * from t where ::1 = 2",
1967 output: "syntax error at position 25 near '::'",
1968 }, {
1969 input: "select * from t where ::. = 2",
1970 output: "syntax error at position 25 near '::'",
1971 }, {
1972 input: "update a set c = values(1)",
1973 output: "syntax error at position 26 near '1'",
1974 }, {
1975 input: "select(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1976 "(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(" +
1977 "F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1978 "(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(" +
1979 "F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1980 "(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(" +
1981 "F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1982 "(F(F(F(F(F(F(F(F(F(F(F(F(",
1983 output: "max nesting level reached at position 406",
1984 }, {
1985 input: "select(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1986 "(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(" +
1987 "F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1988 "(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(" +
1989 "F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1990 "(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(" +
1991 "F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F" +
1992 "(F(F(F(F(F(F(F(F(F(F(F(",
1993 output: "syntax error at position 404",
1994 }, {
1995 // This construct is considered invalid due to a grammar conflict.
1996 input: "insert into a select * from b join c on duplicate key update d=e",
1997 output: "syntax error at position 54 near 'key'",
1998 }, {
1999 input: "select * from a left join b",
2000 output: "syntax error at position 28",
2001 }, {
2002 input: "select * from a natural join b on c = d",
2003 output: "syntax error at position 34 near 'on'",
2004 }, {
2005 input: "select * from a natural join b using (c)",
2006 output: "syntax error at position 37 near 'using'",
2007 }, {
2008 input: "select next id from a",
2009 output: "expecting value after next at position 15 near 'id'",
2010 }, {
2011 input: "select next 1+1 values from a",
2012 output: "syntax error at position 15",
2013 }, {
2014 input: "insert into a values (select * from b)",
2015 output: "syntax error at position 29 near 'select'",
2016 }, {
2017 input: "select database",
2018 output: "syntax error at position 16",
2019 }, {
2020 input: "select mod from t",
2021 output: "syntax error at position 16 near 'from'",
2022 }, {
2023 input: "select 1 from t where div 5",
2024 output: "syntax error at position 26 near 'div'",
2025 }, {
2026 input: "select 1 from t where binary",
2027 output: "syntax error at position 29",
2028 }, {
2029 input: "select match(a1, a2) against ('foo' in boolean mode with query expansion) from t",
2030 output: "syntax error at position 57 near 'with'",
2031 }, {
2032 input: "select /* reserved keyword as unqualified column */ * from t where key = 'test'",
2033 output: "syntax error at position 71 near 'key'",
2034 }, {
2035 input: "select /* vitess-reserved keyword as unqualified column */ * from t where escape = 'test'",
2036 output: "syntax error at position 81 near 'escape'",
2037 }, {
2038 input: "(select /* parenthesized select */ * from t)",
2039 output: "syntax error at position 45",
2040 }, {
2041 input: "select * from t where id = ((select a from t1 union select b from t2) order by a limit 1)",
2042 output: "syntax error at position 76 near 'order'",
2043 }, {
2044 input: "select /* straight_join using */ 1 from t1 straight_join t2 using (a)",
2045 output: "syntax error at position 66 near 'using'",
2046 }, {
2047 input: "select 'aa",
2048 output: "syntax error at position 11 near 'aa'",
2049 excludeMulti: true,
2050 }, {
2051 input: "select 'aa\\",
2052 output: "syntax error at position 12 near 'aa'",
2053 excludeMulti: true,
2054 }, {
2055 input: "select /* aa",
2056 output: "syntax error at position 13 near '/* aa'",
2057 excludeMulti: true,
2058 }}
2059 )
2060
2061 func TestErrors(t *testing.T) {
2062 for _, tcase := range invalidSQL {
2063 _, err := Parse(tcase.input)
2064 if err == nil || err.Error() != tcase.output {
2065 t.Errorf("%s: %v, want %s", tcase.input, err, tcase.output)
2066 }
2067 }
2068 }
2069
2070 // Benchmark run on 6/23/17, prior to improvements:
2071 // BenchmarkParse1-4 100000 16334 ns/op
2072 // BenchmarkParse2-4 30000 44121 ns/op
2073
2074 func BenchmarkParse1(b *testing.B) {
2075 sql := "select 'abcd', 20, 30.0, eid from a where 1=eid and name='3'"
2076 for i := 0; i < b.N; i++ {
2077 ast, err := Parse(sql)
2078 if err != nil {
2079 b.Fatal(err)
2080 }
2081 _ = String(ast)
2082 }
2083 }
2084
2085 func BenchmarkParse2(b *testing.B) {
2086 sql := "select aaaa, bbb, ccc, ddd, eeee, ffff, gggg, hhhh, iiii from tttt, ttt1, ttt3 where aaaa = bbbb and bbbb = cccc and dddd+1 = eeee group by fff, gggg having hhhh = iiii and iiii = jjjj order by kkkk, llll limit 3, 4"
2087 for i := 0; i < b.N; i++ {
2088 ast, err := Parse(sql)
2089 if err != nil {
2090 b.Fatal(err)
2091 }
2092 _ = String(ast)
2093 }
2094 }
2095
2096 var benchQuery string
2097
2098 func init() {
2099 // benchQuerySize is the approximate size of the query.
2100 benchQuerySize := 1000000
2101
2102 // Size of value is 1/10 size of query. Then we add
2103 // 10 such values to the where clause.
2104 var baseval bytes.Buffer
2105 for i := 0; i < benchQuerySize/100; i++ {
2106 // Add an escape character: This will force the upcoming
2107 // tokenizer improvement to still create a copy of the string.
2108 // Then we can see if avoiding the copy will be worth it.
2109 baseval.WriteString("\\'123456789")
2110 }
2111
2112 var buf bytes.Buffer
2113 buf.WriteString("select a from t1 where v = 1")
2114 for i := 0; i < 10; i++ {
2115 fmt.Fprintf(&buf, " and v%d = \"%d%s\"", i, i, baseval.String())
2116 }
2117 benchQuery = buf.String()
2118 }
2119
2120 func BenchmarkParse3(b *testing.B) {
2121 for i := 0; i < b.N; i++ {
2122 if _, err := Parse(benchQuery); err != nil {
2123 b.Fatal(err)
2124 }
2125 }
2126 }