-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
1967 lines (1541 loc) · 57.3 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Reverse Snowflake Joins
Copyright (c) 2008, Alexandru Toth
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Alexandru Toth ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
"""
Do not run this file!
Unintuitive as it is, this is "included" verbatim into revj's namespace
Run the tests with:
python revj.py
"""
#common for many tests
basicTests = ['select ((((a)))) from table;',
'select (((a))),(b+(c+(d+3))) from table;',
"select b || '((1))2))34' from table;",
"""select a,'"',b,'"' from table; """,
"""select "x", '"', "y" ,'"' from table; """,
"""select "table"."a", "table"."b" from table;"""]
simplifyTests = """select sin(cos(a+b+sin(table."c"))) from table;
select 2*a*2 + b.b-c/3, 2*x ||'xx' from table ttt where (m *n = 0) or (ww|| qq='mmmmmmmm');
select a, (2*a.a) + ((b+c)+3) from table;
select a*(-1) from table;
select instr(a,'d') || 'dd' || nvl(b.b,'c') from table;
select nvl(a,'d'||'cc') || 'dd' || nvl(b,2+1+3) from table;
select nvl(a.a,b) from table;
select nvl(a.a, (2*a.a) + ((b+c.c)+3)) from table;
select substr('aa', 3, b) from table;
select nvl (a), nvl ( a ), nvl (a ) from table;
select x+y+substr(a+b+upper(d)) from table;
select sin(cos(q + upper(lower(u)))) from table;
select sin(cos(tan(x) + y)*2) + substr(a||b.b||upper(lower("d"))) from table;
select 2*a + b.b+c+3, x ||'xx' from table;
select * from table ttt where (m +cos(n) = 0) or (ww || concat(qq,'xx')='mmmmmm')
select "table2"."column name" from "schema"."table";
select table.a_a01+99, table.a_a02*99,'*', table.b_b02+'not defined', table.c_c02+'cc' + 'dd' from table where '*'='*' or table.aa04='d';
select 2*q*4, table.*, 2*xx from table;
select count(*)*3 from table;
select a + 2 + pi()*3 from table where b=random();
select * from table order by 10*sin(cos(c33)+d44)+3;
select * from table where a=1 or sin(b)=0.5;
select * from table where a<>'lala'; """\
.splitlines()
outerJoinTests = ['select * from t1, t2 where t1.id1 (+) = t2.id1;',
'select * from t1, t2 where t1.id2 (+)= t2.id2;',
'select * from t1, t2 where t1.id3 = (+) t2.id3;',
'select * from t1, t2 where t1.id4 =(+) t2.id4;',
'select * from t1, t2 where t1.id5 = ( + ) t2.id5;']
class SanityCheckerTestCase(unittest.TestCase):
def setUp(self):
self.sc = SanityChecker()
def testCheckParens(self):
self.assertRaises(BadParensException, self.sc.checkParens,
'select ((((a))))) from table')
assert self.sc.checkParens('select (((a))),(b+(c+(d+3))) from t;')
class QuoteRemoverTestCase(unittest.TestCase):
def setUp(self):
self.qr = QuoteRemover()
def testRemoveUnknownMS(self):
s = 'select * from table as t1 WITH( nolock) where x=1'
res = self.qr.process(s)
assert 'with' not in res
assert 'nolock' not in res
def testRemoveUnknownMS(self):
s = 'select * from table as t1 WITH( nolock) where x=1'
res = self.qr.process(s)
assert 'with' not in res
assert 'nolock' not in res
def testRemoveUnknownPG(self):
s = 'select t.aa::varchar( 20), bb[3], cc[4:5], dd[6][7] as [ee] from table t'
res = self.qr.process(s)
assert '::' not in res
assert 'varchar' not in res
assert '20' not in res
assert 'aa' in res
assert 'bb' in res
assert 'cc' in res
assert 'dd as ee' in res
assert ':' not in res
assert '[3]' not in res
assert '[4:5]' not in res
assert '[6]' not in res
def testRemoveCast(self):
s = """select cast(T2.a as integer)
from T2
group by cast(T2.a as integer)"""
res = self.qr.process(s)
assert 'integer' not in res
assert ' as ' not in res
def testKeepQuotedUnknownPG(self):
s = "select * from table where x='t.aa::varchar( 20)';"
res = self.qr.process(s)
assert '::' not in res
assert 'varchar' not in res
assert '20' not in res
assert 'aa' not in res
def testQuoteEscapes(self):
res = [""" "my table"."my column\" is "" long" """,
""" 'a''b\'c\'d' """]
res = map(self.qr.removeQuoteEscapes, res)
for r in res:
for q in ESCAPEDQUOTES:
assert q not in res
def testRemoveDoubleQuotes(self):
res = [""" "my table"."my column\" is "" long '' """,
""" 'a''b\'c\'d' """]
res = list(map(self.qr.removeQuoteEscapes, res))
res = list(map(self.qr.removeQuotedIdent, res))
for r in res:
for q in ESCAPEDQUOTES:
assert q not in res + [' ']
def removeSpaces(self, x):
return re.sub('\s+', '', x)
def testRemoveConst(self):
res = ["select a * 2.3E4, b * 2e4, c * .3e4 + 3.14 from table;",
"select a || 'sin(cos(x))', '*', '1234' from table;",
"select a || '(((' || 'dd'||'select ' from table where b='))';"]
bads = ['sin', 'cos', '2.3', 'e4', 'E4', '2.3', '3.14',
'((', '))', 's4']
for r in res:
self.qr.reset()
x = self.qr.removeConst(r)
for b in bads:
try:
assert b not in x
except AssertionError:
print >>sys.stderr, '[%s] in [%s]' %(b, x)
assert b not in x
assert x.islower()
assert self.removeSpaces(self.qr.restoreConst(r)) == \
self.removeSpaces(r)
def testResetInProcess(self):
self.qr.quotedConsts = {1:1, 2:2}
x = self.qr.process('select a from table')
assert self.qr.quotedConsts == {}
def testSanityInProcess(self):
bads = basicTests
for r in bads:
bads = self.qr.process(r)
#bad SQL
bads = ['select ((((a))))) from table;',
'(select a+(b+(c+(d+3) from table;',
"select a || '((1))2))34' from table;)",
"select [a from table;",
"select a] from table;",
"select {a from table;",
"select a} from table;",
"select a' from table;",
'select a" from table;']
for r in bads:
self.assertRaises(BadParensException,
self.qr.process, r)
#bad sql
bads = ["""select "table"."a'", "table"."b'" from table; """,
"""select "table"."a)" from table; """ ,
"""select "table"."a+" from table; """]
for r in bads:
self.assertRaises(BadIdentException,
self.qr.process, r)
def testSanityOk(self):
goods = ["select * from t where x='((((('",
"select * from t where x=']]]]]]'",
"select * from t where x='}}}}'",
"""select * from t where x='"' """]
for r in goods:
bads = self.qr.process(r)
def testReplacedConsts(self):
s = "select m99 from t where a11 = 'aa' and t.b22 = 2e34 and 3.14 = c33;"
res = self.qr.process(s)
assert "'aa'" in self.qr.quotedConsts
assert "2e34" in self.qr.quotedConsts
assert "3.14" in self.qr.quotedConsts
def testInClause(self):
s = ("select * from table t where t.a in (1,2,3) "
"and t.b in ('x', 'y') and t.c='a'")
res = self.qr.process(s)
assert '(' not in res
assert ')' not in res
assert 'in (1,2,3)' in self.qr.quotedConsts
assert "in ('x','y')" in self.qr.quotedConsts
def testNotInClause(self):
s = ("select * from table t where t.a not in (1,2,3) "
"and t.b not in ('x', 'y') and t.c='a'")
res = self.qr.process(s)
ok = False
for i in compar.searchString(res):
if i[0] != 'not_in_equal':
ok = True
if not ok:
raise Exception('NOT BETWEEN is not an operator')
assert '(' not in res
assert ')' not in res
assert 'not in (1,2,3)' in self.qr.quotedConsts
assert "not in ('x','y')" in self.qr.quotedConsts
def testEmptyStringConst(self):
s = "SELECT IF (cu.active, 'active','') from cu"
res = self.qr.process(s)
assert "'active'" in self.qr.quotedConsts
assert "''" in self.qr.quotedConsts
def testBetween(self):
s = ("select table.* from table where table.a11 between 3 and 4 and table.b22 =5 " )
res = self.qr.process(s)
assert "table.b22" in res
assert "5" not in res
assert "table.b22 = '" in res
assert "table.a11" in res
assert "between_equal" in res
def testNotBetween(self):
s = ("select table.* from table where table.a11 not between 3 and 4 and table.b22 =5 " )
res = self.qr.process(s)
ok = False
for i in compar.searchString(res):
if i[0] != 'not_between_equal':
ok = True
if not ok:
raise Exception('NOT BETWEEN is not an operator')
assert "table.b22" in res
assert "5" not in res
assert "table.b22 = '" in res
assert "table.a11" in res
assert "not_between_equal" in res
def testSelectDistinct(self):
s = ("Select distinct a.id from a " )
res = self.qr.process(s)
assert "distinct" not in res.lower()
class SimplifierTestCase(unittest.TestCase):
def setUp(self):
self.si = Simplifier()
self.qr = QuoteRemover()
def process(self, s):
self.qr.reset()
self.si.reset()
return self.si.process(self.qr.process(s))
def testSelectStar(self):
s = "select table.*, *, a * 2 * 3 from table"
res = self.process(s)
try:
assert len(res.split('*')) == 3 #that's 2 occurences of *
except AssertionError:
print >>sys.stderr, res
raise Exception("* is not a binop in [%s]" % s)
def testCountStar(self):
s = "select count(*)*3 from table;"
res = self.process(s)
assert "_agg" in res
assert "(*)" in res
assert "3" not in res
def testNumbersInIdent(self):
s = 'select table.a000 * 2 + b_12-3 + "c23" + d34 from table'
res = self.process(s)
goods = ['a000','b_12', 'c23', 'd34']
for g in goods:
assert g in res
def testProcess(self):
s = basicTests + simplifyTests
for x in s:
res = self.si.process(
self.qr.process(x))
assert "select" in res.lower()
assert res.islower()
def testReduceLongOperands(self):
s = "select a||b || c || '||||||||' from table"
res = self.process(s)
assert '|' not in res
def testReduceConstBinop(self):
s = "select 2*a, 0.2/b, (222-c), 'ss'||d from table"
res = self.process(s)
assert '2' not in res #remove constant binop
assert "'s" not in res #remove constant binop
def testFuncsWithComma(self):
s = "select nvl(a111 + 2222, 3333, 4444) from table;"
res = self.process(s)
assert 'a111' in res
assert '2222' not in res
assert '3333' not in res
assert '4444' not in res
assert 'nvl' not in res
def testFuncsWithCommaJoins(self):
s = "select * from t1, t2 where t1.a=t2.a and nvl(t1.x,0) =(+) nvl(t2.y, 0) and t1.b = t2.b;"
res = self.process(s)
assert 'nvl' not in res
def testFuncsNoParam(self):
s = "select a + 2 + pi()*3 from table where b=random();"
res = self.process(s)
assert 'pi' not in res #pi is redundant
assert "random" in res #don' remove == constant func()
def testFuncsAndAggregs(self):
s = "select x, sum(t.y) from t group by x having nvl(sum(z)) > 0;"
res = self.process(s)
assert '_agg(z)' in res
assert 'nvl' not in res
def testDistinctAggregs(self):
s = "select count(a), count(distinct b), count(DISTINCT c) from t "
res = self.process(s)
cntPos = aggregatesAsList.index('count')
assert '_%s_agg(a)' % cntPos in res
#one space left after removing "(DISTINCT b)"
assert '_%s_agg( b)' % (cntPos + AGG_DISTINCT) in res
assert '_%s_agg( b)' % (cntPos + AGG_DISTINCT) in res
assert 'distinct' not in res
def testKeepSpacing(self):
s = "select a, (2*m) from table;"
res = self.process(s)
assert ('a,' in res) or ('a ,' in res) #keep spacing
def testWhere(self):
s = "select a from table where (sin(a)+pi()/2)=3+random()+4;"
res = self.process(s)
assert ('pi' not in res) or ('2' not in res)
assert '/' not in res
assert '*' not in res
assert ('3' not in res) or ('4' not in res) or ('random' not in res)
def testAnsiJoin(self):
s = """select a from t1 inner join t2 on (sin(t1.a)+pi()/3)
=4+random()+5+cos(t2.b);"""
res = self.process(s)
assert 't1.a' in res
assert 't2.b' in res
assert ('pi' not in res) or ('3' not in res)
assert '/' not in res
assert '*' not in res
assert ('4' not in res) or ('5' not in res) or ('random' not in res)
def testOuterJoin(self):
s = outerJoinTests
for x in s:
res = self.process(x)
try:
assert "#" in res.lower()
except AssertionError:
raise Exception("# not in [%s]" % res)
def testMultipleSelects(self):
s = "select * from table where table.a in (select id from country)"
res = self.process(s)
assert "(" in res
assert len(s.split('select')) == 3 # that's 2 selects
def testColAliases(self):
s = "select a as aa, 3 as bb, 2*3*sin(c)*4 as cc from table;"
res = self.process(s)
assert "aa" in res
assert "bb" in res
assert "cc" in res
def testSquareBrackets(self):
s = "select aa as [bb] from table;"
res = self.process(s)
assert "aa" in res
def testDollarCurlyBraces(self):
s = "select a,b,c from ${table};"
res = self.process(s)
assert "$table" in res
def testTableAliases(self):
s = "select * from table1 aa, table 2 bb;"
res = self.process(s)
assert "aa" in res
assert "bb" in res
def testAnsiJoinAliases(self):
s = "select * from table1 aa inner join table2 bb on x=y;"
res = self.process(s)
assert "aa" in res
assert "bb" in res
def testGroupBy(self):
s = 'select * from table t group by "a11", t.b22, ' + \
'10*sin(cos(c33)+d44)+3'
res = self.process(s)
assert "a11" in res
assert "t.b22" in res
assert "c33" in res
assert "d44" in res
assert 'group_by' in res
def testOrderBy(self):
s = 'select * from table t order by "a11", t.b22, ' + \
'10*sin(cos(c33)+d44)+3'
res = self.process(s)
assert "a11" in res
assert "t.b22" in res
assert "c33" in res
assert "d44" in res
assert 'order_by' in res
def testStarComparisons(self):
#some reportign tools generate 'select ALL' syntax
s = "select * from t where a=1 and ('*'='*' or nvl(b,'n')='n')" + \
" and c=0;"
res = self.process(s)
assert len(res.split('=')) == 4 # 3 equals, one removed :-)
def testGROUP_CONCAT(self):
#this query is on the frontpage; embarrasing
s = """SELECT GROUP_CONCAT(film.name, SEPARATOR ', ') AS actors
FROM film"""
res = self.process(s)
assert "separator" not in res.lower()
class SingleSelectTestCase(unittest.TestCase):
def setUp(self):
self.si = Simplifier()
self.qr = QuoteRemover()
#param is for quoted consts; those change after each process()
self.ss = SingleSelect(self.qr)
def process(self, s):
self.qr.reset()
self.si.reset()
self.ss.reset()
return self.ss.process(
self.si.process(
self.qr.process(s)))
def testMultipleSelects(self):
s = "select * from table where table.a in (select id from country)"
self.assertRaises(MultipleSelectsException, self.ss.process, s)
def testColAliases(self):
s = "select a as aa, 2*t.b+4 as bb, sin(c) as cc, d, 3 as e from t;"
res = self.process(s)
assert 'aa' in self.ss.colAliases['t.a']
assert 'aa' not in self.ss.colAliases['t.b']
assert 'bb' in self.ss.colAliases['t.b']
assert 'cc' in self.ss.colAliases['t.c']
assert 't.d' not in self.ss.colAliases
assert 'd' not in self.ss.colAliases
assert 'as' not in self.ss.colAliases
assert 't.e' not in self.ss.colAliases
assert 'e' not in self.ss.colAliases
assert 't.a' in self.ss.columns
assert 't.b' in self.ss.columns
assert 't.c' in self.ss.columns
assert 't.d' in self.ss.columns
assert 't.e' in self.ss.columns
def testColAliasExpressions(self):
s = 'select a, 2*(m+"n"+3) as oo ,b as bb from table;'
res = self.process(s)
assert 'bb' in self.ss.colAliases['table.b']
assert 'm' not in self.ss.colAliases
assert 'n' not in self.ss.colAliases
assert 'table.a' in self.ss.columns
assert 'table.b' in self.ss.columns
assert 'table.m' in self.ss.columns
assert 'table.n' in self.ss.columns
assert '2' not in self.ss.columns
assert '3' not in self.ss.columns
assert 'table.2' not in self.ss.columns
assert 'table.3' not in self.ss.columns
#oo is no longer expression alias; it is a column
assert 'table.oo' in self.ss.columns
def testColAliasExpressionsWithJoins(self):
s = 'select 2*(t1.m+"t2.n"+3) as oo ,t1.b as bb from t1, t2;'
res = self.process(s)
assert 'bb' in self.ss.colAliases['t1.b']
assert 'oo' in self.ss.exprAliases
def testStarColAlias(self):
s = "select a, table.* from table;"
res = self.process(s)
assert 'table.*' in self.ss.columns
assert '*' not in self.ss.columns
assert 'table.a' in self.ss.columns
assert self.ss.selectStar == False
def testSelectStar_X(self):
s = "select t2.a, * from table1, table2 t2, table2 t2again;"
res = self.process(s)
assert '*' not in self.ss.columns
assert self.ss.selectStar == True
assert 'table1.*' in self.ss.columns
assert 't2.*' in self.ss.columns
assert 't2again.*' in self.ss.columns
def testNaturalJoin(self):
s = "select student_name, class_name from student natural join class"
self.assertRaises(NaturalJoinException, self.ss.process, s)
def testOldJoin(self):
s = "select t1.m99 from table1 t1, table2, table3 t3 where t1.a11=t3.b22"
res = self.process(s)
assert 'a11' not in self.ss.tableAliases
assert 't1.a11' not in self.ss.tableAliases
assert 'b22' not in self.ss.tableAliases
assert 't3.b22' not in self.ss.tableAliases
assert 'm99' not in self.ss.tableAliases
assert 't1.m99' not in self.ss.tableAliases
assert 'table1' in self.ss.tableAliases
assert 'table2' in self.ss.tableAliases
assert 'table3' in self.ss.tableAliases
assert 't1' in self.ss.tableAliases['table1']
assert 't2' not in self.ss.tableAliases['table1']
assert 't3' not in self.ss.tableAliases['table1']
assert 't3' in self.ss.tableAliases['table3']
assert 't1.a11' in self.ss.joins
assert 't3.b22' in self.ss.joins['t1.a11']
def testAnsiJoin(self):
s = """select * from (table1 t1 inner join table2 t2 on
"t1"."a" = t2.m and sin(t1.b) = t2.n+2)
inner join table3 t3 on t1.a = 10*t3.x and t1.b || 't1.xx' = t3.y
where t1.xxx=1;"""
res = self.process(s)
assert 't1.a' in self.ss.columns
assert 't1.b' in self.ss.columns
assert 't2.m' in self.ss.columns
assert 't2.n' in self.ss.columns
assert 't3.x' in self.ss.columns
assert 't3.y' in self.ss.columns
assert 't1.xxx' in self.ss.columns
assert 't1.a' in self.ss.joins
assert 't2.m' in self.ss.joins['t1.a']
assert 't3.x' in self.ss.joins['t1.a']
assert 't1.b' in self.ss.joins
assert 't2.n' in self.ss.joins['t1.b']
assert 't3.y' in self.ss.joins['t1.b']
assert 'table1' in self.ss.tableAliases
assert 'table2' in self.ss.tableAliases
assert 'table3' in self.ss.tableAliases
def testLeftJoinAnsi(self):
s = """SELECT * FROM t1 LEFT JOIN t2 on t1.a=t2.a"""
res = self.process(s)
assert 'T1.A' in self.ss.joins
assert 't2.a' in self.ss.joins
def testRightJoinAnsi(self):
s = """SELECT * FROM t1 right JOIN t2 on t1.a=t2.a"""
res = self.process(s)
assert 't1.a' in self.ss.joins
assert 'T2.A' in self.ss.joins
def testFullJoinAnsi(self):
s = """SELECT * FROM t1 FULL JOIN t2 on t1.a=t2.a"""
res = self.process(s)
assert 'T1.A' in self.ss.joins
assert 'T2.A' in self.ss.joins
#def testCrossAnsi(self):
# s = """SELECT * FROM t1 CROSS JOIN t2 where t1.a=3 and t2.b=5"""
# res = self.process(s)
# assert 't1.a' in self.ss.columns
# assert 't2.b' in self.ss.columns
# assert len(self.ss.joins) == 0
# assert '--' not in res
def testCrossJoinAlias(self):
s = """SELECT * FROM t1 a1 CROSS JOIN t2 a2;"""
res = self.process(s)
assert 't1' in self.ss.tableAliases
assert 't2' in self.ss.tableAliases
#this needs to be fixed properly
#assert 'a1' not in self.ss.tableAliases
assert 'a2' not in self.ss.tableAliases
assert 'a1' in self.ss.tableAliases['t1']
assert 'a2' in self.ss.tableAliases['t2']
assert len(self.ss.joins) == 0
assert '--' not in res
def testJoinUsing(self):
s = """SELECT * FROM t1 JOIN t2 USING (a,b,c,d)"""
res = self.process(s)
assert 't1.a' in self.ss.joins
assert 't1.b' in self.ss.joins
assert 't1.c' in self.ss.joins
assert 't1.d' in self.ss.joins
assert 't2.a' in self.ss.joins
assert 't2.b' in self.ss.joins
assert 't2.c' in self.ss.joins
assert 't2.d' in self.ss.joins
assert 't2.a' in self.ss.joins['t1.a']
assert 't2.b' in self.ss.joins['t1.b']
assert 't2.c' in self.ss.joins['t1.c']
assert 't2.d' in self.ss.joins['t1.d']
assert len(self.ss.tableAliases) == 2
assert 't1' in self.ss.tableAliases
assert 't2' in self.ss.tableAliases
def testLeftJoinUsing(self):
s = """SELECT * FROM t1 LEFT JOIN t2 USING (a,b,c,d)"""
res = self.process(s)
assert 'T1.A' in self.ss.joins
assert 't2.a' in self.ss.joins
def testRightJoinUsing(self):
s = """SELECT * FROM t1 right JOIN t2 USING (a,b,c,d)"""
res = self.process(s)
assert 't1.a' in self.ss.joins
assert 'T2.A' in self.ss.joins
def testFullJoinUsing(self):
s = """SELECT * FROM t1 FULL JOIN t2 USING (a,b,c,d)"""
res = self.process(s)
assert 'T1.A' in self.ss.joins
assert 'T2.A' in self.ss.joins
def testMax2TablesUsing(self):
s = """SELECT * FROM t1 JOIN t2 USING (a,b) join t3 using (a,b)"""
self.assertRaises(AmbiguousColumnException, self.process, s)
def testOldJoinWithWhere(self):
s = """select * from t1, t2 where t1.id1 = t2.id2;"""
res = self.process(s)
assert 't2.id2' not in self.ss.filters
def testAnsiMixedJoin(self):
s = """select * from table1 t1, table2 t2 inner join table3 t3 on
t2.id2 = t3.id3
where t1.id1 = t2.id2"""
res = self.process(s)
assert 't2.id2' not in self.ss.tableAliases
assert 't3.id3' not in self.ss.tableAliases
assert 'T2.ID2' not in self.ss.tableAliases
assert 'T3.ID3' not in self.ss.tableAliases
assert 't1.id1' in self.ss.columns
assert 't2.id2' in self.ss.columns
assert 't3.id3' in self.ss.columns
assert 't1.id1' in self.ss.joins
assert 't2.id2' in self.ss.joins
assert 't3.id3' in self.ss.joins
assert 'table1' in self.ss.tableAliases
assert 't1' in self.ss.tableAliases['table1']
assert 'table2' in self.ss.tableAliases
assert 't2' in self.ss.tableAliases['table2']
assert 'table3' in self.ss.tableAliases
assert 't3' in self.ss.tableAliases['table3']
def testAnsiMixedJoinNotOrdered(self):
s = """select * from table1 t1, table2 t2 inner join table3 t3 on
t2.id2 = t3.id3, table4, table5 t5, table6
where t1.id1 = t2.id2"""
res = self.process(s)
#same as previously
assert 't2.id2' not in self.ss.tableAliases
assert 't3.id3' not in self.ss.tableAliases
assert 'T2.ID2' not in self.ss.tableAliases
assert 'T3.ID3' not in self.ss.tableAliases
assert 't1.id1' in self.ss.columns
assert 't2.id2' in self.ss.columns
assert 't3.id3' in self.ss.columns
assert 't1.id1' in self.ss.joins
assert 't2.id2' in self.ss.joins
assert 't3.id3' in self.ss.joins
assert 'table1' in self.ss.tableAliases
assert 't1' in self.ss.tableAliases['table1']
assert 'table2' in self.ss.tableAliases
assert 't2' in self.ss.tableAliases['table2']
assert 'table3' in self.ss.tableAliases
assert 't3' in self.ss.tableAliases['table3']
#new tests
assert 'table4' in self.ss.tableAliases
assert 'table5' in self.ss.tableAliases
assert 't5' in self.ss.tableAliases['table5']
assert 'table4' in self.ss.tableAliases
def testAnsiJoinWithWhere(self):
# from SQLite test suite
s = """select * from t1 left join t2 on t1.b=t2.x and t1.c=1
left join t3 on t1.b=t3.p where t1.c=2"""
res = self.process(s)
assert 't1' in self.ss.tableAliases
assert 't2' in self.ss.tableAliases
assert 't3' in self.ss.tableAliases
def testTableAsAlias(self):
#MySQL speciffic ?
s = """select * from table1 as t1 inner join table2 as t2 on t1.x=t2.y;"""
res = self.process(s)
assert 'table1' in self.ss.tableAliases
assert 't1' in self.ss.tableAliases['table1']
assert 'table2' in self.ss.tableAliases
assert 't2' in self.ss.tableAliases['table2']
def testAnsiJoinsNoAlias(self):
s = """select * from category left join film_category on category.category_id = film_category.category_id;"""
res = self.process(s)
assert 'category' in self.ss.tableAliases
assert 'film_category' in self.ss.tableAliases
def testGroupBy(self):
s = 'select m99 from table t group by "a11", t.b22, ' + \
'10* sin(cos(c33)+d44)+3'
res = self.process(s)
assert 't.a11' in self.ss.columns
assert 't.b22' in self.ss.columns
assert 't.c33' in self.ss.columns
assert 't.d44' in self.ss.columns
assert 't.a11' in self.ss.groups
assert 't.b22' in self.ss.groups
assert 't.c33' in self.ss.groups
assert 't.d44' in self.ss.groups
assert 'm99' not in self.ss.groups
assert 't.m99' not in self.ss.groups
def testOrderBy(self):
s = 'select m99 from t order by "a11", t.b22, 10*sin(cos(c33)+d44)+3'
res = self.process(s)
assert 't.a11' in self.ss.columns
assert 't.b22' in self.ss.columns
assert 't.c33' in self.ss.columns
assert 't.d44' in self.ss.columns
assert 't.a11' in self.ss.orders
assert 't.b22' in self.ss.orders
assert 'm99' not in self.ss.orders
assert 't.m99' not in self.ss.orders
def testOrderByAlias(self):
s = 'select t1.m99 as zz from t1, t2 order by zz'
res = self.process(s)
assert 't1.m99' in self.ss.columns
assert 'm99' not in self.ss.orders
assert 't1.m99' not in self.ss.orders
assert 'zz' in self.ss.orders
def testSumGroupHaving(self):
s = 'select c33, d44, sum(a11), min(t.b22) from t ' + \
'group by c33, d44 where m99=0 having 0<sum(e55)'
res = self.process(s)
assert 't.a11' in self.ss.columns
assert 't.a11' in self.ss.aggregs
assert 'sum' in self.ss.aggregs['t.a11']
assert 't.a11' not in self.ss.groups
assert 't.b22' in self.ss.columns
assert 't.b22' in self.ss.aggregs
assert 'min' in self.ss.aggregs['t.b22']
assert 't.b22' not in self.ss.groups
assert 't.c33' in self.ss.columns
assert 't.c33' not in self.ss.aggregs
assert 't.c33' in self.ss.groups
assert 't.d44' in self.ss.columns
assert 't.d44' not in self.ss.aggregs
assert 't.d44' in self.ss.groups
assert 't.e55' in self.ss.columns
assert 't.e55' in self.ss.havings
#need to reverse comparison operator
assert "sum(e55)> 0" in self.ss.havings['t.e55']
assert 't.e55' not in self.ss.aggregs
assert 't.e55' not in self.ss.groups
assert 't.m99' in self.ss.columns
assert 't.m99' not in self.ss.aggregs
assert 't.m99' not in self.ss.groups
def testSumGroupHaving(self):
s = 'select sum(a11) from t;'
res = self.process(s)
assert 't.a11' in self.ss.columns
assert 't.a11' in self.ss.aggregs
assert 'SUM(a11)' in self.ss.aggregs['t.a11']
def testOrderBy(self):
s = 'select m99 from t order by "a11", t.b22, 10*sin(cos(c33)+d44)+3'
res = self.process(s)
assert 't.a11' in self.ss.columns
assert 't.a11' in self.ss.orders
assert 't.b22' in self.ss.columns
assert 't.b22' in self.ss.orders
assert 't.c33' in self.ss.columns
assert 't.c33' in self.ss.orders
assert 't.d44' in self.ss.columns
assert 't.d44' in self.ss.orders
def testOrderBy(self):
s = 'select m99 from t order by "a11", t.b22 DESC, 10*sin(cos(c33)+d44)+3 DESC'
res = self.process(s)
assert 't.a11' in self.ss.columns
assert 't.a11' in self.ss.orders
assert 't.b22' in self.ss.columns
assert 't.b22' + '_'*10 in self.ss.orders
assert 't.c33' in self.ss.columns
assert 't.c33' in self.ss.orders
assert 't.d44' in self.ss.columns
assert 't.d44' in self.ss.orders
def testStarOrderBy(self):
s = 'SELECT * from t order by y'
res = self.process(s)
assert 't.y' in self.ss.columns
assert 't.y' in self.ss.orders
def testFilter(self):
s = "select m99 from t where (a11 = 0 or a11 = 1 ) and " + \
" 'qqq' =t.b22 and c33=pi();"
res = self.process(s)
assert 't.a11' in self.ss.filters
assert '= 0' in self.ss.filters['t.a11']
assert '= 1' in self.ss.filters['t.a11']
assert 't.b22' in self.ss.filters
assert "= 'qqq'" in self.ss.filters['t.b22']
assert 't.c33' in self.ss.filters
assert '= pi()' in repr(self.ss.filters['t.c33'])
assert 'm99' not in self.ss.filters
assert 't.m99' not in self.ss.filters
def testReverseOrderFilter(self):
s = "select * from t where 0 < x;"
res = self.process(s)
assert 't.x' in self.ss.filters
#need to reverse comparison operator
assert '> 0' in self.ss.filters['t.x']
def testFuncOverSum(self):
#func is not reduced
s = "select t.x, sum(t.y) from t group by x having nvl(sum(t.z),0) > 0;"
res = self.process(s)
assert 't.z' in self.ss.havings
assert "SUM(t.z)> 0" in self.ss.havings['t.z']
def testSanityCheckTables(self):
s = "select m99 from unknown_table where (a11 = 0 or a11 = 1 ) and " + \