/
test_bql.py
2007 lines (1957 loc) · 97.3 KB
/
test_bql.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
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2016, MIT Probabilistic Computing Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import StringIO
import apsw
import pytest
import bayeslite
import bayeslite.ast as ast
import bayeslite.compiler as compiler
import bayeslite.core as core
import bayeslite.guess as guess
import bayeslite.parse as parse
import bayeslite.metamodels.troll_rng as troll
from bayeslite.util import cursor_value
import test_core
import test_csv
def bql2sql(string, setup=None):
with test_core.t1() as (bdb, _table_id):
if setup is not None:
setup(bdb)
phrases = parse.parse_bql_string(string)
out = compiler.Output(0, {}, ())
for phrase in phrases:
assert ast.is_query(phrase)
compiler.compile_query(bdb, phrase, out)
out.write(';')
return out.getvalue()
# XXX Kludgey mess. Please reorganize.
def bql2sqlparam(string):
with test_core.t1() as (bdb, _table_id):
phrases = parse.parse_bql_string(string)
out0 = StringIO.StringIO()
for phrase in phrases:
out = None
if isinstance(phrase, ast.Parametrized):
bindings = (None,) * phrase.n_numpar
out = compiler.Output(phrase.n_numpar, phrase.nampar_map,
bindings)
phrase = phrase.phrase
else:
out = StringIO.StringIO()
assert ast.is_query(phrase)
compiler.compile_query(bdb, phrase, out)
# XXX Do something about the parameters.
out0.write(out.getvalue())
out0.write(';')
return out0.getvalue()
def bql_execute(bdb, string, bindings=()):
return map(tuple, bdb.execute(string, bindings))
def empty(cursor):
assert cursor is not None
assert cursor.description is not None
assert len(cursor.description) == 0
with pytest.raises(StopIteration):
cursor.next()
def test_conditional_probability():
with test_core.t1() as (bdb, _generator_id):
bdb.execute('initialize 1 model for t1_cc')
bdb.execute('analyze t1_cc for 1 iteration wait')
q0 = 'estimate probability of age = 8 by t1_cc'
q1 = 'estimate probability of age = 8 given () by t1_cc'
assert bdb.execute(q0).fetchvalue() == bdb.execute(q1).fetchvalue()
q2 = 'estimate probability of age = 8 given (weight = 16) by t1_cc'
assert bdb.execute(q0).fetchvalue() < bdb.execute(q2).fetchvalue()
bdb.execute('estimate probability of value 8'
' given (weight = 16) from columns of t1_cc').fetchall()
def test_joint_probability():
with test_core.t1() as (bdb, _generator_id):
bdb.execute('initialize 1 model for t1_cc')
bdb.execute('analyze t1_cc for 1 iteration wait')
q0 = 'estimate probability of age = 8 by t1_cc'
q1 = 'estimate probability of (age = 8) by t1_cc'
assert bdb.execute(q0).fetchvalue() == bdb.execute(q1).fetchvalue()
q1 = 'estimate probability of (age = 8) given () by t1_cc'
assert bdb.execute(q0).fetchvalue() == bdb.execute(q1).fetchvalue()
q2 = 'estimate probability of age = 8 given (weight = 16) by t1_cc'
assert bdb.execute(q0).fetchvalue() < bdb.execute(q2).fetchvalue()
q0 = 'estimate probability of age = 8 by t1_cc'
q1 = 'estimate probability of (age = 8, weight = 16) by t1_cc'
assert bdb.execute(q1).fetchvalue() < bdb.execute(q0).fetchvalue()
q2 = 'estimate probability of (age = 8, weight = 16)' \
" given (label = 'mumble') by t1_cc"
assert bdb.execute(q1).fetchvalue() < bdb.execute(q2).fetchvalue()
def test_badbql():
with test_core.t1() as (bdb, _generator_id):
with pytest.raises(ValueError):
bdb.execute('')
with pytest.raises(ValueError):
bdb.execute(';')
with pytest.raises(ValueError):
bdb.execute('select 0; select 1')
def test_select_trivial():
assert bql2sql('select null;') == 'SELECT NULL;'
assert bql2sql("select 'x';") == "SELECT 'x';"
assert bql2sql("select 'x''y';") == "SELECT 'x''y';"
assert bql2sql('select "x";') == 'SELECT "x";'
assert bql2sql('select "x""y";') == 'SELECT "x""y";'
assert bql2sql('select 0;') == 'SELECT 0;'
assert bql2sql('select 0.;') == 'SELECT 0.0;'
assert bql2sql('select .0;') == 'SELECT 0.0;'
assert bql2sql('select 0.0;') == 'SELECT 0.0;'
assert bql2sql('select 1e0;') == 'SELECT 1.0;'
assert bql2sql('select 1e+1;') == 'SELECT 10.0;'
assert bql2sql('select 1e-1;') == 'SELECT 0.1;'
assert bql2sql('select -1e+1;') == 'SELECT (- 10.0);'
assert bql2sql('select +1e-1;') == 'SELECT (+ 0.1);'
assert bql2sql('select SQRT(1-EXP(-2*value)) FROM bm_mi;') == \
'SELECT "SQRT"((1 - "EXP"(((- 2) * "value")))) FROM "bm_mi";'
assert bql2sql('select .1e0;') == 'SELECT 0.1;'
assert bql2sql('select 1.e10;') == 'SELECT 10000000000.0;'
assert bql2sql('select all 0;') == 'SELECT 0;'
assert bql2sql('select distinct 0;') == 'SELECT DISTINCT 0;'
assert bql2sql('select 0 as z;') == 'SELECT 0 AS "z";'
assert bql2sql('select * from t;') == 'SELECT * FROM "t";'
assert bql2sql('select t.* from t;') == 'SELECT "t".* FROM "t";'
assert bql2sql('select c from t;') == 'SELECT "c" FROM "t";'
assert bql2sql('select c as d from t;') == 'SELECT "c" AS "d" FROM "t";'
assert bql2sql('select t.c as d from t;') == \
'SELECT "t"."c" AS "d" FROM "t";'
assert bql2sql('select t.c as d, p as q, x from t;') == \
'SELECT "t"."c" AS "d", "p" AS "q", "x" FROM "t";'
assert bql2sql('select * from t, u;') == 'SELECT * FROM "t", "u";'
assert bql2sql('select * from t as u;') == 'SELECT * FROM "t" AS "u";'
assert bql2sql('select * from (select 0);') == 'SELECT * FROM (SELECT 0);'
assert bql2sql('select t.c from (select d as c from u) as t;') == \
'SELECT "t"."c" FROM (SELECT "d" AS "c" FROM "u") AS "t";'
assert bql2sql('select * where x;') == 'SELECT * WHERE "x";'
assert bql2sql('select * from t where x;') == \
'SELECT * FROM "t" WHERE "x";'
assert bql2sql('select * group by x;') == 'SELECT * GROUP BY "x";'
assert bql2sql('select * from t where x group by y;') == \
'SELECT * FROM "t" WHERE "x" GROUP BY "y";'
assert bql2sql('select * from t where x group by y, z;') == \
'SELECT * FROM "t" WHERE "x" GROUP BY "y", "z";'
assert bql2sql('select * from t where x group by y having sum(z) < 1') == \
'SELECT * FROM "t" WHERE "x" GROUP BY "y" HAVING ("sum"("z") < 1);'
assert bql2sql('select * order by x;') == 'SELECT * ORDER BY "x";'
assert bql2sql('select * order by x asc;') == 'SELECT * ORDER BY "x";'
assert bql2sql('select * order by x desc;') == \
'SELECT * ORDER BY "x" DESC;'
assert bql2sql('select * order by x, y;') == 'SELECT * ORDER BY "x", "y";'
assert bql2sql('select * order by x desc, y;') == \
'SELECT * ORDER BY "x" DESC, "y";'
assert bql2sql('select * order by x, y asc;') == \
'SELECT * ORDER BY "x", "y";'
assert bql2sql('select * limit 32;') == 'SELECT * LIMIT 32;'
assert bql2sql('select * limit 32 offset 16;') == \
'SELECT * LIMIT 32 OFFSET 16;'
assert bql2sql('select * limit 16, 32;') == 'SELECT * LIMIT 32 OFFSET 16;'
assert bql2sql('select (select0);') == 'SELECT "select0";'
assert bql2sql('select (select 0);') == 'SELECT (SELECT 0);'
assert bql2sql('select f(f(), f(x), y);') == \
'SELECT "f"("f"(), "f"("x"), "y");'
assert bql2sql('select a and b or c or not d is e is not f like j;') == \
'SELECT ((("a" AND "b") OR "c") OR' \
+ ' (NOT ((("d" IS "e") IS NOT "f") LIKE "j")));'
assert bql2sql('select a like b not like c like d escape e;') == \
'SELECT ((("a" LIKE "b") NOT LIKE "c") LIKE "d" ESCAPE "e");'
assert bql2sql('select a like b escape c glob d not glob e;') == \
'SELECT ((("a" LIKE "b" ESCAPE "c") GLOB "d") NOT GLOB "e");'
assert bql2sql('select a not glob b glob c escape d;') == \
'SELECT (("a" NOT GLOB "b") GLOB "c" ESCAPE "d");'
assert bql2sql('select a glob b escape c regexp e not regexp f;') == \
'SELECT ((("a" GLOB "b" ESCAPE "c") REGEXP "e") NOT REGEXP "f");'
assert bql2sql('select a not regexp b regexp c escape d;') == \
'SELECT (("a" NOT REGEXP "b") REGEXP "c" ESCAPE "d");'
assert bql2sql('select a regexp b escape c not regexp d escape e;') == \
'SELECT (("a" REGEXP "b" ESCAPE "c") NOT REGEXP "d" ESCAPE "e");'
assert bql2sql('select a not regexp b escape c match e not match f;') == \
'SELECT ((("a" NOT REGEXP "b" ESCAPE "c") MATCH "e") NOT MATCH "f");'
assert bql2sql('select a not match b match c escape d;') == \
'SELECT (("a" NOT MATCH "b") MATCH "c" ESCAPE "d");'
assert bql2sql('select a match b escape c not match d escape e;') == \
'SELECT (("a" MATCH "b" ESCAPE "c") NOT MATCH "d" ESCAPE "e");'
assert bql2sql('select a not match b escape c between d and e;') == \
'SELECT (("a" NOT MATCH "b" ESCAPE "c") BETWEEN "d" AND "e");'
assert bql2sql('select a between b and c and d;') == \
'SELECT (("a" BETWEEN "b" AND "c") AND "d");'
assert bql2sql('select a like b like c escape d between e and f;') == \
'SELECT ((("a" LIKE "b") LIKE "c" ESCAPE "d") BETWEEN "e" AND "f");'
assert bql2sql('select a between b and c not between d and e;') == \
'SELECT (("a" BETWEEN "b" AND "c") NOT BETWEEN "d" AND "e");'
assert bql2sql('select a not between b and c in (select f);') == \
'SELECT (("a" NOT BETWEEN "b" AND "c") IN (SELECT "f"));'
assert bql2sql('select a in (select b) and c not in (select d);') == \
'SELECT (("a" IN (SELECT "b")) AND ("c" NOT IN (SELECT "d")));'
assert bql2sql('select a in (select b) isnull notnull!=c<>d<e<=f>g;') == \
'SELECT ((((("a" IN (SELECT "b")) ISNULL) NOTNULL) != "c") !=' \
+ ' ((("d" < "e") <= "f") > "g"));'
assert bql2sql('select a>b>=c<<d>>e&f|g+h-i*j/k;') == \
'SELECT (("a" > "b") >= (((("c" << "d") >> "e") & "f") |' \
+ ' (("g" + "h") - (("i" * "j") / "k"))));'
assert bql2sql('select a/b%c||~~d collate e collate\'f\'||1;') == \
'SELECT (("a" / "b") % (("c" || (((~ (~ "d")) COLLATE "e")' \
+ ' COLLATE "f")) || 1));'
assert bql2sql('select cast(f(x) as binary blob);') == \
'SELECT CAST("f"("x") AS "binary" "blob");'
assert bql2sql('select cast(42 as varint(73));') == \
'SELECT CAST(42 AS "varint"(73));'
assert bql2sql('select cast(f(x, y, z) as varchar(12 ,34));') == \
'SELECT CAST("f"("x", "y", "z") AS "varchar"(12, 34));'
assert bql2sql('select exists (select a) and not exists (select b);') == \
'SELECT (EXISTS (SELECT "a") AND (NOT EXISTS (SELECT "b")));'
assert bql2sql('select case when a - b then c else d end from t;') == \
'SELECT CASE WHEN ("a" - "b") THEN "c" ELSE "d" END FROM "t";'
assert bql2sql('select case f(a) when b + c then d else e end from t;') \
== \
'SELECT CASE "f"("a") WHEN ("b" + "c") THEN "d" ELSE "e" END FROM "t";'
def test_estimate_bql():
assert bql2sql('estimate predictive probability of weight'
' from t1_cc;') == \
'SELECT bql_row_column_predictive_probability(1, NULL, _rowid_, 3)' \
' FROM "t1";'
assert bql2sql('estimate label, predictive probability of weight'
' from t1_cc;') \
== \
'SELECT "label",' \
' bql_row_column_predictive_probability(1, NULL, _rowid_, 3)' \
' FROM "t1";'
assert bql2sql('estimate predictive probability of weight, label'
' from t1_cc;') \
== \
'SELECT bql_row_column_predictive_probability(1, NULL, _rowid_, 3),' \
' "label"' \
' FROM "t1";'
assert bql2sql('estimate predictive probability of weight + 1'
' from t1_cc;') == \
'SELECT (bql_row_column_predictive_probability(1, NULL, _rowid_, 3)' \
' + 1)' \
' FROM "t1";'
with pytest.raises(parse.BQLParseError):
# Need a table.
bql2sql('estimate predictive probability of weight;')
with pytest.raises(parse.BQLParseError):
# Need at most one generator.
bql2sql('estimate predictive probability of weight from t1_cc, t1_cc;')
with pytest.raises(parse.BQLParseError):
# Need a generator name, not a subquery.
bql2sql('estimate predictive probability of weight from (select 0);')
with pytest.raises(parse.BQLParseError):
# Need a column.
bql2sql('estimate predictive probability from t1_cc;')
assert bql2sql('estimate probability of weight = 20 from t1_cc;') == \
'SELECT bql_pdf_joint(1, NULL, 3, 20) FROM "t1";'
assert bql2sql('estimate probability of weight = 20 given (age = 8)'
'from t1_cc;') == \
'SELECT bql_pdf_joint(1, NULL, 3, 20, -1, 2, 8) FROM "t1";'
assert bql2sql('estimate probability of (weight = 20, age = 8)'
' from t1_cc;') == \
'SELECT bql_pdf_joint(1, NULL, 3, 20, 2, 8) FROM "t1";'
assert bql2sql('estimate probability of (weight = 20, age = 8)'
" given (label = 'mumble') from t1_cc;") == \
"SELECT bql_pdf_joint(1, NULL, 3, 20, 2, 8, -1, 1, 'mumble')" \
' FROM "t1";'
assert bql2sql('estimate probability of weight = (c + 1) from t1_cc;') == \
'SELECT bql_pdf_joint(1, NULL, 3, ("c" + 1)) FROM "t1";'
assert bql2sql('estimate probability of weight = f(c) from t1_cc;') == \
'SELECT bql_pdf_joint(1, NULL, 3, "f"("c")) FROM "t1";'
assert bql2sql('estimate similarity to (rowid = 5) from t1_cc;') == \
'SELECT bql_row_similarity(1, NULL, _rowid_,' \
' (SELECT _rowid_ FROM "t1" WHERE ("rowid" = 5))) FROM "t1";'
assert bql2sql('estimate similarity to (rowid = 5) with respect to age'
' from t1_cc') == \
'SELECT bql_row_similarity(1, NULL, _rowid_,' \
' (SELECT _rowid_ FROM "t1" WHERE ("rowid" = 5)), 2) FROM "t1";'
assert bql2sql('estimate similarity to (rowid = 5)'
' with respect to (age, weight) from t1_cc;') == \
'SELECT bql_row_similarity(1, NULL, _rowid_,' \
' (SELECT _rowid_ FROM "t1" WHERE ("rowid" = 5)), 2, 3) FROM "t1";'
assert bql2sql('estimate similarity to (rowid = 5) with respect to (*)'
' from t1_cc;') == \
'SELECT bql_row_similarity(1, NULL, _rowid_,' \
' (SELECT _rowid_ FROM "t1" WHERE ("rowid" = 5))) FROM "t1";'
assert bql2sql('estimate similarity to (rowid = 5)'
' with respect to (age, weight) from t1_cc;') == \
'SELECT bql_row_similarity(1, NULL, _rowid_,' \
' (SELECT _rowid_ FROM "t1" WHERE ("rowid" = 5)), 2, 3) FROM "t1";'
assert bql2sql('estimate dependence probability of age with weight' +
' from t1_cc;') == \
'SELECT bql_column_dependence_probability(1, NULL, 2, 3) FROM "t1";'
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate dependence probability with age from t1_cc;')
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate dependence probability from t1_cc;')
assert bql2sql('estimate mutual information of age with weight' +
' from t1_cc;') == \
'SELECT bql_column_mutual_information(1, NULL, 2, 3, NULL) FROM "t1";'
assert bql2sql('estimate mutual information of age with weight' +
' using 42 samples from t1_cc;') == \
'SELECT bql_column_mutual_information(1, NULL, 2, 3, 42) FROM "t1";'
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate mutual information with age from t1_cc;')
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate mutual information from t1_cc;')
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate mutual information with age using 42 samples'
' from t1_cc;')
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate mutual information using 42 samples from t1_cc;')
# XXX Should be SELECT, not ESTIMATE, here?
assert bql2sql('estimate correlation of age with weight from t1_cc;') == \
'SELECT bql_column_correlation(1, 2, 3) FROM "t1";'
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate correlation with age from t1_cc;')
with pytest.raises(bayeslite.BQLError):
# Need both columns fixed.
bql2sql('estimate correlation from t1_cc;')
with pytest.raises(bayeslite.BQLError):
# No PREDICT outside INFER.
bql2sql('estimate predict age with confidence 0.9 from t1_cc;')
assert bql2sql('infer explicit predict age with confidence 0.9'
' from t1_cc;') == \
'SELECT bql_predict(1, NULL, 2, _rowid_, 0.9) FROM "t1";'
assert bql2sql('infer explicit rowid, age,'
' predict age confidence age_conf from t1_cc') == \
'SELECT c0 AS "rowid", c1 AS "age",' \
' bql_json_get(c2, \'value\') AS "age",' \
' bql_json_get(c2, \'confidence\') AS "age_conf"' \
' FROM (SELECT "rowid" AS c0, "age" AS c1,' \
' bql_predict_confidence(1, NULL, 2, _rowid_) AS c2' \
' FROM "t1");'
assert bql2sql('infer explicit rowid, age,'
' predict age as age_inf confidence age_conf from t1_cc') == \
'SELECT c0 AS "rowid", c1 AS "age",' \
' bql_json_get(c2, \'value\') AS "age_inf",' \
' bql_json_get(c2, \'confidence\') AS "age_conf"' \
' FROM (SELECT "rowid" AS c0, "age" AS c1,' \
' bql_predict_confidence(1, NULL, 2, _rowid_) AS c2' \
' FROM "t1");'
assert bql2sql('infer rowid, age, weight from t1_cc') \
== \
'SELECT "rowid" AS "rowid",' \
' "IFNULL"("age", bql_predict(1, NULL, 2, _rowid_, 0)) AS "age",' \
' "IFNULL"("weight", bql_predict(1, NULL, 3, _rowid_, 0))' \
' AS "weight"' \
' FROM "t1";'
assert bql2sql('infer rowid, age, weight with confidence 0.9 from t1_cc') \
== \
'SELECT "rowid" AS "rowid",' \
' "IFNULL"("age", bql_predict(1, NULL, 2, _rowid_, 0.9)) AS "age",' \
' "IFNULL"("weight", bql_predict(1, NULL, 3, _rowid_, 0.9))' \
' AS "weight"' \
' FROM "t1";'
assert bql2sql('infer rowid, age, weight with confidence 0.9 from t1_cc'
' where label = \'foo\'') \
== \
'SELECT "rowid" AS "rowid",' \
' "IFNULL"("age", bql_predict(1, NULL, 2, _rowid_, 0.9)) AS "age",' \
' "IFNULL"("weight", bql_predict(1, NULL, 3, _rowid_, 0.9))' \
' AS "weight"' \
' FROM "t1"' \
' WHERE ("label" = \'foo\');'
assert bql2sql('infer rowid, age, weight with confidence 0.9 from t1_cc'
' where ifnull(label, predict label with confidence 0.7)'
' = \'foo\'') \
== \
'SELECT "rowid" AS "rowid",' \
' "IFNULL"("age", bql_predict(1, NULL, 2, _rowid_, 0.9)) AS "age",' \
' "IFNULL"("weight", bql_predict(1, NULL, 3, _rowid_, 0.9))' \
' AS "weight"' \
' FROM "t1"' \
' WHERE ("ifnull"("label", bql_predict(1, NULL, 1, _rowid_, 0.7))' \
' = \'foo\');'
assert bql2sql('infer rowid, * from t1_cc') == \
'SELECT "rowid" AS "rowid", "id" AS "id",' \
' "IFNULL"("label", bql_predict(1, NULL, 1, _rowid_, 0)) AS "label",' \
' "IFNULL"("age", bql_predict(1, NULL, 2, _rowid_, 0)) AS "age",' \
' "IFNULL"("weight", bql_predict(1, NULL, 3, _rowid_, 0))' \
' AS "weight"' \
' FROM "t1";'
def test_estimate_columns_trivial():
prefix0 = 'SELECT c.name AS name'
prefix1 = ' FROM bayesdb_generator AS g,' \
' bayesdb_generator_column AS gc, bayesdb_column AS c' \
' WHERE g.id = 1 AND gc.generator_id = g.id' \
' AND c.tabname = g.tabname AND c.colno = gc.colno'
prefix = prefix0 + prefix1
assert bql2sql('estimate * from columns of t1_cc;') == \
prefix + ';'
assert bql2sql('estimate * from columns of t1_cc where' +
' (probability of value 42) > 0.5') == \
prefix + \
' AND (bql_column_value_probability(1, NULL, c.colno, 42) > 0.5);'
assert bql2sql('estimate * from columns of t1_cc'
' where (probability of value 8) > (probability of age = 16)') == \
prefix + \
' AND (bql_column_value_probability(1, NULL, c.colno, 8) >' \
' bql_pdf_joint(1, NULL, 2, 16));'
with pytest.raises(bayeslite.BQLError):
# PREDICTIVE PROBABILITY makes no sense without row.
bql2sql('estimate * from columns of t1_cc where' +
' predictive probability of x > 0;')
with pytest.raises(bayeslite.BQLError):
# SIMILARITY makes no sense without row.
bql2sql('estimate * from columns of t1_cc where' +
' similarity to (rowid = x) with respect to c > 0;')
assert bql2sql('estimate * from columns of t1_cc where' +
' dependence probability with age > 0.5;') == \
prefix + \
' AND (bql_column_dependence_probability(1, NULL, 2, c.colno) > 0.5);'
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc where' +
' dependence probability of age with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc'
' where dependence probability > 0.5;')
assert bql2sql('estimate * from columns of t1_cc order by' +
' mutual information with age;') == \
prefix + \
' ORDER BY bql_column_mutual_information(1, NULL, 2, c.colno, NULL);'
assert bql2sql('estimate * from columns of t1_cc order by' +
' mutual information with age using 42 samples;') == \
prefix + \
' ORDER BY bql_column_mutual_information(1, NULL, 2, c.colno, 42);'
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc order by' +
' mutual information of age with weight;')
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc'
' where mutual information > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc order by' +
' mutual information of age with weight using 42 samples;')
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc where' +
' mutual information using 42 samples > 0.5;')
assert bql2sql('estimate * from columns of t1_cc order by' +
' correlation with age desc;') == \
prefix + ' ORDER BY bql_column_correlation(1, 2, c.colno) DESC;'
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc order by' +
' correlation of age with weight;')
with pytest.raises(bayeslite.BQLError):
# Must omit exactly one column.
bql2sql('estimate * from columns of t1_cc where correlation > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Makes no sense.
bql2sql('estimate * from columns of t1_cc'
' where predict age with confidence 0.9 > 30;')
assert bql2sql('estimate'
' *, dependence probability with weight as depprob,'
' mutual information with weight as mutinf'
' from columns of t1_cc'
' where depprob > 0.5 order by mutinf desc') == \
prefix0 + \
', bql_column_dependence_probability(1, NULL, 3, c.colno)' \
' AS "depprob"' \
', bql_column_mutual_information(1, NULL, 3, c.colno, NULL)' \
' AS "mutinf"' + \
prefix1 + \
' AND ("depprob" > 0.5)' \
' ORDER BY "mutinf" DESC;'
def test_estimate_pairwise_trivial():
prefix = 'SELECT 1 AS generator_id, c0.name AS name0, c1.name AS name1, '
infix = ' AS value'
infix0 = ' FROM bayesdb_generator AS g,'
infix0 += ' bayesdb_generator_column AS gc0, bayesdb_column AS c0,'
infix0 += ' bayesdb_generator_column AS gc1, bayesdb_column AS c1'
infix0 += ' WHERE g.id = 1'
infix0 += ' AND gc0.generator_id = g.id AND gc1.generator_id = g.id'
infix0 += ' AND c0.tabname = g.tabname AND c0.colno = gc0.colno'
infix0 += ' AND c1.tabname = g.tabname AND c1.colno = gc1.colno'
infix += infix0
assert bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc;') == \
prefix + \
'bql_column_dependence_probability(1, NULL, c0.colno, c1.colno)' + \
infix + ';'
assert bql2sql('estimate mutual information'
' from pairwise columns of t1_cc where'
' (probability of age = 0) > 0.5;') == \
prefix + \
'bql_column_mutual_information(1, NULL, c0.colno, c1.colno, NULL)' + \
infix + \
' AND (bql_pdf_joint(1, NULL, 2, 0) > 0.5);'
with pytest.raises(bayeslite.BQLError):
# PROBABILITY OF VALUE is 1-column.
bql2sql('estimate correlation from pairwise columns of t1_cc where' +
' (probability of value 0) > 0.5;')
with pytest.raises(bayeslite.BQLError):
# PREDICTIVE PROBABILITY OF is a row function.
bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc' +
' where predictive probability of x > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc'
' where dependence probability of age with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate mutual information from pairwise columns of t1_cc'
' where dependence probability with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate mutual information using 42 samples'
' from pairwise columns of t1_cc'
' where dependence probability with weight > 0.5;')
assert bql2sql('estimate correlation from pairwise columns of t1_cc'
' where dependence probability > 0.5;') == \
prefix + 'bql_column_correlation(1, c0.colno, c1.colno)' + \
infix + ' AND' \
' (bql_column_dependence_probability(1, NULL, c0.colno, c1.colno)' \
' > 0.5);'
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc'
' where mutual information of age with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc'
' where mutual information of age with weight using 42 samples'
' > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate mutual information from pairwise columns of t1_cc'
' where mutual information with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate mutual information using 42 samples'
' from pairwise columns of t1_cc'
' where mutual information with weight using 42 samples > 0.5;')
assert bql2sql('estimate correlation from pairwise columns of t1_cc' +
' where mutual information > 0.5;') == \
prefix + 'bql_column_correlation(1, c0.colno, c1.colno)' + \
infix + ' AND' + \
' (bql_column_mutual_information(1, NULL, c0.colno, c1.colno, NULL)' \
' > 0.5);'
assert bql2sql('estimate correlation from pairwise columns of t1_cc' +
' where mutual information using 42 samples > 0.5;') == \
prefix + 'bql_column_correlation(1, c0.colno, c1.colno)' + \
infix + ' AND' + \
' (bql_column_mutual_information(1, NULL, c0.colno, c1.colno, 42)' \
' > 0.5);'
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc'
' where correlation of age with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate mutual information from pairwise columns of t1_cc'
' where correlation with weight > 0.5;')
with pytest.raises(bayeslite.BQLError):
# Must omit both columns.
bql2sql('estimate mutual information using 42 samples'
' from pairwise columns of t1_cc'
' where correlation with weight > 0.5;')
assert bql2sql('estimate correlation from pairwise columns of t1_cc'
' where correlation > 0.5;') == \
prefix + 'bql_column_correlation(1, c0.colno, c1.colno)' + \
infix + ' AND' + \
' (bql_column_correlation(1, c0.colno, c1.colno) > 0.5);'
with pytest.raises(bayeslite.BQLError):
# Makes no sense.
bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc'
' where predict age with confidence 0.9 > 30;')
assert bql2sql('estimate dependence probability as depprob,'
' mutual information as mutinf'
' from pairwise columns of t1_cc'
' where depprob > 0.5 order by mutinf desc') == \
prefix + \
'bql_column_dependence_probability(1, NULL, c0.colno, c1.colno)' \
' AS "depprob",' \
' bql_column_mutual_information(1, NULL, c0.colno, c1.colno, NULL)' \
' AS "mutinf"' + \
infix0 + \
' AND ("depprob" > 0.5)' \
' ORDER BY "mutinf" DESC;'
def test_estimate_pairwise_row():
prefix = 'SELECT r0._rowid_ AS rowid0, r1._rowid_ AS rowid1'
infix = ' AS value FROM "t1" AS r0, "t1" AS r1'
assert bql2sql('estimate similarity from pairwise t1_cc;') == \
prefix + ', bql_row_similarity(1, NULL, r0._rowid_, r1._rowid_)' + \
infix + ';'
assert bql2sql('estimate similarity with respect to age' +
' from pairwise t1_cc;') == \
prefix + ', bql_row_similarity(1, NULL, r0._rowid_, r1._rowid_, 2)' + \
infix + ';'
with pytest.raises(bayeslite.BQLError):
# PREDICT is a 1-row function.
bql2sql('estimate predict age with confidence 0.9 from pairwise t1;')
def test_estimate_pairwise_selected_columns():
assert bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc for label, age') == \
'SELECT 1 AS generator_id, c0.name AS name0, c1.name AS name1,' \
' bql_column_dependence_probability(1, NULL, c0.colno, c1.colno)' \
' AS value' \
' FROM bayesdb_generator AS g,' \
' bayesdb_generator_column AS gc0, bayesdb_column AS c0,' \
' bayesdb_generator_column AS gc1, bayesdb_column AS c1' \
' WHERE g.id = 1' \
' AND gc0.generator_id = g.id AND gc1.generator_id = g.id' \
' AND c0.tabname = g.tabname AND c0.colno = gc0.colno' \
' AND c1.tabname = g.tabname AND c1.colno = gc1.colno' \
' AND c0.colno IN (1, 2) AND c1.colno IN (1, 2);'
assert bql2sql('estimate dependence probability'
' from pairwise columns of t1_cc'
' for (ESTIMATE * FROM COLUMNS OF t1_cc'
' ORDER BY name DESC LIMIT 2)') == \
'SELECT 1 AS generator_id, c0.name AS name0, c1.name AS name1,' \
' bql_column_dependence_probability(1, NULL, c0.colno, c1.colno)' \
' AS value' \
' FROM bayesdb_generator AS g,' \
' bayesdb_generator_column AS gc0, bayesdb_column AS c0,' \
' bayesdb_generator_column AS gc1, bayesdb_column AS c1' \
' WHERE g.id = 1' \
' AND gc0.generator_id = g.id AND gc1.generator_id = g.id' \
' AND c0.tabname = g.tabname AND c0.colno = gc0.colno' \
' AND c1.tabname = g.tabname AND c1.colno = gc1.colno' \
' AND c0.colno IN (3, 1) AND c1.colno IN (3, 1);'
def test_select_columns_subquery():
assert bql2sql('select id, t1.(estimate * from columns of t1_cc'
' order by name asc limit 2) from t1') == \
'SELECT "id", "t1"."age", "t1"."label" FROM "t1";'
def test_trivial_commands():
with test_csv.bayesdb_csv_file(test_csv.csv_data) as (bdb, fname):
# XXX Query parameters!
with open(fname, 'rU') as f:
bayeslite.bayesdb_read_csv(bdb, 't', f, header=True, create=True)
with open(fname, 'rU') as f:
with pytest.raises(ValueError):
bayeslite.bayesdb_read_csv(bdb, 't', f, header=True,
create=True)
with open(fname, 'rU') as f:
bayeslite.bayesdb_read_csv(bdb, 't', f, header=True, create=True,
ifnotexists=True)
guess.bayesdb_guess_generator(bdb, 't_cc', 't', 'crosscat')
with pytest.raises(ValueError):
guess.bayesdb_guess_generator(bdb, 't_cc', 't', 'crosscat')
guess.bayesdb_guess_generator(bdb, 't_cc', 't', 'crosscat',
ifnotexists=True)
bdb.execute('initialize 2 models for t_cc')
with pytest.raises(bayeslite.BQLError):
bdb.execute('initialize 2 models for t_cc')
bdb.execute('drop models from t_cc')
bdb.execute('drop models from t_cc')
bdb.execute('initialize 2 models for t_cc')
with pytest.raises(bayeslite.BQLError):
bdb.execute('initialize 2 models for t_cc')
with pytest.raises(bayeslite.BQLError):
bdb.execute('drop models 0-2 from t_cc')
bdb.execute('drop models 0-1 from t_cc')
with bdb.savepoint():
bdb.execute('initialize 2 models for t_cc')
bdb.execute('drop models 0-1 from t_cc')
with pytest.raises(bayeslite.BQLError):
bdb.execute('drop models 0-1 from t_cc')
bdb.execute('initialize 2 models for t_cc')
bdb.execute('initialize 1 model if not exists for t_cc')
bdb.execute('initialize 2 models if not exists for t_cc')
generator_id = core.bayesdb_get_generator(bdb, 't_cc')
assert core.bayesdb_generator_table(bdb, generator_id) == 't'
bdb.execute('alter table t rename to t')
assert core.bayesdb_generator_table(bdb, generator_id) == 't'
bdb.execute('alter table t rename to T')
assert core.bayesdb_generator_table(bdb, generator_id) == 'T'
bdb.execute('estimate count(*) from t_cc').fetchall()
bdb.execute('alter table t rename to t')
assert core.bayesdb_generator_table(bdb, generator_id) == 't'
bdb.execute('alter generator t_cc rename to t0_cc')
assert core.bayesdb_generator_name(bdb, generator_id) == 't0_cc'
bdb.execute('alter generator t0_cc rename to zot, rename to T0_CC')
assert core.bayesdb_generator_name(bdb, generator_id) == 'T0_CC'
bdb.execute('alter generator T0_cc rename to T0_cc')
assert core.bayesdb_generator_name(bdb, generator_id) == 'T0_cc'
bdb.execute('alter generator t0_CC rename to t0_cc')
assert core.bayesdb_generator_name(bdb, generator_id) == 't0_cc'
bdb.execute('estimate count(*) from t0_cc').fetchall()
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate count(*) from t_cc')
bdb.execute('alter generator t0_cc rename to T0_cc')
bdb.execute('analyze t0_cc for 1 iteration wait')
colno = core.bayesdb_generator_column_number(bdb, generator_id,
'gender')
with pytest.raises(parse.BQLParseError):
# Rename the table's columns, not the generator's columns.
bdb.execute('alter generator t0_cc rename gender to sex')
with pytest.raises(NotImplementedError): # XXX
bdb.execute('alter table t rename to t0, rename gender to sex')
assert core.bayesdb_generator_column_number(bdb, generator_id,
'sex') \
== colno
bdb.execute('analyze t0_cc model 0 for 1 iteration wait')
bdb.execute('alter generator t0_cc rename to t_cc')
assert core.bayesdb_generator_column_number(bdb, generator_id,
'sex') \
== colno
bdb.execute('select sex from t0').fetchall()
with pytest.raises(AssertionError): # XXX
bdb.execute('select gender from t0')
assert False, 'Need to fix quoting of unknown columns!'
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate predict sex with confidence 0.9'
' from t_cc').fetchall()
bdb.execute('infer explicit predict sex with confidence 0.9'
' from t_cc').fetchall()
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate predict gender with confidence 0.9'
' from t_cc')
with pytest.raises(bayeslite.BQLError):
bdb.execute('infer explicit predict gender with confidence 0.9'
' from t_cc')
bdb.execute('alter table t0 rename sex to gender')
assert core.bayesdb_generator_column_number(bdb, generator_id,
'gender') \
== colno
bdb.execute('alter generator t0_cc rename to t_cc') # XXX
bdb.execute('alter table t rename to T0') # XXX
bdb.sql_execute('create table t0_temp(x)')
bdb.execute('alter table T0 rename to t0')
assert bdb.execute('select count(*) from t0_temp').fetchvalue() == 0
assert bdb.execute('select count(*) from t0').fetchvalue() > 0
bdb.execute('drop table T0_TEMP')
bdb.execute('analyze t_cc model 0 for 1 iteration wait')
bdb.execute('analyze t_cc model 1 for 1 iteration wait')
bdb.execute('analyze t_cc models 0-1 for 1 iteration wait')
bdb.execute('analyze t_cc models 0,1 for 1 iteration wait')
bdb.execute('analyze t_cc for 1 iteration wait')
bdb.execute('select * from t0').fetchall()
bdb.execute('select * from T0').fetchall()
bdb.execute('estimate * from t_cc').fetchall()
bdb.execute('estimate * from T_CC').fetchall()
bdb.execute('estimate similarity from pairwise t_cc').fetchall()
bdb.execute('select value from'
' (estimate correlation from pairwise columns of t_cc)').fetchall()
bdb.execute('infer explicit predict age with confidence 0.9'
' from t_cc').fetchall()
bdb.execute('infer explicit predict AGE with confidence 0.9'
' from T_cc').fetchall()
bdb.execute('infer explicit predict aGe with confidence 0.9'
' from T_cC').fetchall()
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate predict agee with confidence 0.9 from t_cc')
with pytest.raises(bayeslite.BQLError):
bdb.execute('infer explicit predict agee with confidence 0.9'
' from t_cc')
# Make sure it works with the table too if we create a default
# generator.
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from t0')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from columns of t0')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate correlation from pairwise columns of t0')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate similarity from pairwise t0')
bdb.execute('''
create default generator t_ccd for t0 using crosscat(
age numerical,
rank categorical
)
''')
bdb.execute('initialize 1 model if not exists for t_ccd')
bdb.execute('analyze t_ccd for 1 iteration wait')
bdb.execute('''
create generator t_cce for t0 using crosscat(
guess(*),
age numerical,
rank numerical
)
''')
with pytest.raises(bayeslite.BQLError):
# No models to analyze.
bdb.execute('analyze t_cce for 1 iteration wait')
bdb.execute('initialize 1 model if not exists for t_cce')
bdb.execute('analyze t_cce for 1 iteration wait')
bdb.execute('estimate correlation'
' from pairwise columns of t_cce').fetchall()
bdb.execute('initialize 2 models if not exists for t0')
bdb.execute('analyze t0 for 1 iteration wait')
bdb.execute('estimate * from t0').fetchall()
bdb.execute('estimate * from columns of t0').fetchall()
bdb.execute('estimate * from columns of t0'
' order by dependence probability with age').fetchall()
bdb.execute('estimate correlation'
' from pairwise columns of t0').fetchall()
bdb.execute('estimate similarity from pairwise t0').fetchall()
# XXX Distinguish the two generators somehow.
bdb.execute('alter table t0 set default generator to t_cc')
bdb.execute('estimate * from t0').fetchall()
bdb.execute('estimate * from columns of t0').fetchall()
bdb.execute('estimate correlation'
' from pairwise columns of t0').fetchall()
bdb.execute('estimate similarity from pairwise t0').fetchall()
bdb.execute('alter table t0 unset default generator')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from t0')
bdb.execute('alter table t0 rename to t')
bdb.execute('alter table t set default generator to t_ccd')
bdb.execute('estimate * from t').fetchall()
bdb.execute('estimate * from columns of t').fetchall()
bdb.execute('estimate correlation'
' from pairwise columns of t').fetchall()
bdb.execute('estimate similarity from pairwise t').fetchall()
bdb.execute('drop generator t_ccd')
with pytest.raises(bayeslite.BQLError):
bdb.execute('initialize 3 models if not exists for t_ccd')
with pytest.raises(bayeslite.BQLError):
bdb.execute('initialize 4 models if not exists for t')
with pytest.raises(bayeslite.BQLError):
bdb.execute('analyze t_ccd for 1 iteration wait')
with pytest.raises(bayeslite.BQLError):
bdb.execute('analyze t0 for 1 iteration wait')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from t_ccd')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from columns of t_ccd')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate correlation from pairwise columns of t_ccd')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate similarity from pairwise t_ccd')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from t')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate * from columns of t')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate correlation from pairwise columns of t')
with pytest.raises(bayeslite.BQLError):
bdb.execute('estimate similarity from pairwise t')
bdb.execute('alter table t set default generator to t_cc')
bdb.execute('initialize 6 models if not exists for t_cc')
bdb.execute('initialize 7 models if not exists for t')
bdb.execute('analyze t_cc for 1 iteration wait')
bdb.execute('analyze t for 1 iteration wait')
bdb.execute('estimate * from t').fetchall()
bdb.execute('estimate * from columns of t').fetchall()
bdb.execute('estimate correlation'
' from pairwise columns of t').fetchall()
bdb.execute('estimate similarity from pairwise t').fetchall()
def test_trivial_deadline():
with test_core.t1() as (bdb, _table_id):
bdb.execute('initialize 1 model for t1_cc')
bdb.execute('analyze t1_cc for 1 second wait')
def test_parametrized():
assert bql2sqlparam('select * from t where id = ?') == \
'SELECT * FROM "t" WHERE ("id" = ?1);'
assert bql2sqlparam('select * from t where id = :foo') == \
'SELECT * FROM "t" WHERE ("id" = ?1);'
assert bql2sqlparam('select * from t where id = $foo') == \
'SELECT * FROM "t" WHERE ("id" = ?1);'
assert bql2sqlparam('select * from t where id = @foo') == \
'SELECT * FROM "t" WHERE ("id" = ?1);'
assert bql2sqlparam('select * from t where id = ?123') == \
'SELECT * FROM "t" WHERE ("id" = ?1);'
assert bql2sqlparam('select * from t where a = $foo and b = ?1;') == \
'SELECT * FROM "t" WHERE (("a" = ?1) AND ("b" = ?1));'
assert bql2sqlparam('select * from t' +
' where a = ?123 and b = :foo and c = ?124') == \
'SELECT * FROM "t" WHERE' + \
' ((("a" = ?1) AND ("b" = ?2)) AND ("c" = ?2));'
with test_csv.bayesdb_csv_file(test_csv.csv_data) as (bdb, fname):
with open(fname, 'rU') as f:
bayeslite.bayesdb_read_csv(bdb, 't', f, header=True, create=True)
assert bql_execute(bdb, 'select count(*) from t') == [(7,)]
assert bql_execute(bdb, 'select count(distinct division) from t') == \
[(6,)]
assert bql_execute(bdb, 'select * from t where height > ?', (70,)) == \
[
(41, 'M', 65600, 72, 'marketing', 4),
(30, 'M', 70000, 73, 'sales', 4),
(30, 'F', 81000, 73, 'engineering', 3),
]
assert bql_execute(bdb, 'select * from t where height > ?123',
(0,)*122 + (70,)) == \
[
(41, 'M', 65600, 72, 'marketing', 4),
(30, 'M', 70000, 73, 'sales', 4),
(30, 'F', 81000, 73, 'engineering', 3),
]
assert bql_execute(bdb, 'select age from t where division = :division',
{':division': 'sales'}) == \
[(34,), (30,)]
assert bql_execute(bdb, 'select division from t' +
' where age < @age and rank > ?;',
(40, 4)) == \
[('accounting',)]
assert bql_execute(bdb, 'select division from t' +
' where age < @age and rank > :rank;',
{':RANK': 4, '@aGe': 40}) == \
[('accounting',)]
with pytest.raises(ValueError):
bdb.execute('select * from t where age < ? and rank > :r',
{':r': 4})
def traced_execute(query, *args):
bql = []
def trace(string, _bindings):
bql.append(' '.join(string.split()))
bdb.trace(trace)
with bdb.savepoint():
bdb.execute(query, *args)
bdb.untrace(trace)
return bql
def sqltraced_execute(query, *args):
sql = []
def trace(string, _bindings):
sql.append(' '.join(string.split()))
bdb.sql_trace(trace)
with bdb.savepoint():
bdb.execute(query, *args)
bdb.sql_untrace(trace)
return sql
bdb.execute('create generator t_cc for t using crosscat(guess(*))')
bdb.execute('initialize 1 model for t_cc;')
iters0 = bdb.sql_execute('select *'
' from bayesdb_generator_model').fetchall()
thetas0 = bdb.sql_execute('select *'
' from bayesdb_crosscat_theta').fetchall()
bdb.execute('analyze t_cc for 1 iteration wait;')
iters1 = bdb.sql_execute('select *'
' from bayesdb_generator_model').fetchall()
thetas1 = bdb.sql_execute('select *'
' from bayesdb_crosscat_theta').fetchall()
assert iters0 != iters1
assert thetas0 != thetas1
assert traced_execute('estimate similarity to (rowid = 1)'
' with respect to (estimate * from columns of t_cc limit 1)'
' from t_cc;') == [
'estimate similarity to (rowid = 1)' \
' with respect to (estimate * from columns of t_cc limit 1)' \
' from t_cc;',
]
assert sqltraced_execute('estimate similarity to (rowid = 1)'
' with respect to (estimate * from columns of t_cc limit 1)'
' from t_cc;') == [
'SELECT COUNT(*) FROM bayesdb_generator'
' WHERE name = :name OR (defaultp AND tabname = :name)',
'SELECT id FROM bayesdb_generator'
' WHERE name = :name OR (defaultp AND tabname = :name)',
'SELECT tabname FROM bayesdb_generator WHERE id = ?',
'SELECT COUNT(*) FROM bayesdb_generator'
' WHERE name = :name OR (defaultp AND tabname = :name)',
'SELECT id FROM bayesdb_generator'
' WHERE name = :name OR (defaultp AND tabname = :name)',
# ESTIMATE * FROM COLUMNS OF:
'SELECT c.name AS name'
' FROM bayesdb_generator AS g,'
' bayesdb_generator_column AS gc,'
' bayesdb_column AS c'
' WHERE g.id = 1 AND gc.generator_id = g.id'
' AND c.tabname = g.tabname AND c.colno = gc.colno'
' LIMIT 1',
'SELECT c.colno'
' FROM bayesdb_generator AS g,'
' bayesdb_generator_column AS gc,'
' bayesdb_column AS c'
' WHERE g.id = :generator_id AND c.name = :column_name'
' AND g.id = gc.generator_id'
' AND g.tabname = c.tabname AND gc.colno = c.colno',
# ESTIMATE SIMILARITY TO (rowid=1):