/
e_select1.test.lua
executable file
·2320 lines (2113 loc) · 95.1 KB
/
e_select1.test.lua
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
#!/usr/bin/env tarantool
local test = require("sqltester")
test:plan(509)
--!./tcltestrunner.lua
-- 2010 July 16
--
-- The author disclaims copyright to this source code. In place of
-- a legal notice, here is a blessing:
--
-- May you do good and not evil.
-- May you find forgiveness for yourself and forgive others.
-- May you share freely, never taking more than you give.
--
-------------------------------------------------------------------------
--
-- This file implements tests to verify that the "testable statements" in
-- the lang_select.html document are correct.
--
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
test:do_execsql_test(
"e_select-1.0",
[[
CREATE TABLE t1(a TEXT PRIMARY KEY, b TEXT);
INSERT INTO t1 VALUES('a', 'one');
INSERT INTO t1 VALUES('b', 'two');
INSERT INTO t1 VALUES('c', 'three');
CREATE TABLE t2(a TEXT PRIMARY KEY, b TEXT);
INSERT INTO t2 VALUES('a', 'I');
INSERT INTO t2 VALUES('b', 'II');
INSERT INTO t2 VALUES('c', 'III');
CREATE TABLE t3(a TEXT PRIMARY KEY, c INT);
INSERT INTO t3 VALUES('a', 1);
INSERT INTO t3 VALUES('b', 2);
CREATE TABLE t4(a TEXT PRIMARY KEY, c INT);
INSERT INTO t4 VALUES('a', NULL);
INSERT INTO t4 VALUES('b', 2);
]], {
-- <e_select-1.0>
-- </e_select-1.0>
})
local t1_cross_t2 = { "a", "one", "a", "I", "a", "one", "b", "II", "a", "one", "c", "III", "b", "two", "a", "I", "b", "two", "b", "II", "b", "two", "c", "III", "c", "three", "a", "I", "c", "three", "b", "II", "c", "three", "c", "III" }
local t1_cross_t1 = { "a", "one", "a", "one", "a", "one", "b", "two", "a", "one", "c", "three", "b", "two", "a", "one", "b", "two", "b", "two", "b", "two", "c", "three", "c", "three", "a", "one", "c", "three", "b", "two", "c", "three", "c", "three" }
-- This proc is a specialized version of [do_execsql_test].
-- The second argument to this proc must be a SELECT statement that
-- features a cross join of some time. Instead of the usual ",",
-- "CROSS JOIN" or "INNER JOIN" join-op, the string JOIN_PATTERN must be
-- substituted.
--
-- This test runs the SELECT three times - once with:
--
-- * s/JOIN_PATTERN/,/
-- * s/JOIN_PATTERN/JOIN/
-- * s/JOIN_PATTERN/INNER JOIN/
-- * s/JOIN_PATTERN/CROSS JOIN/
--
-- and checks that each time the results of the SELECT are $res.
--
local function do_join_test(tn, select, res)
for tn2, joinop in ipairs({",", "CROSS JOIN", "INNER JOIN"}) do
local S = string.gsub(select, "JOIN_PATTERN", joinop)
test:do_execsql_test(
tn.."."..tn2,
S,
res)
end
end
---------------------------------------------------------------------------
-- The following tests check that all paths on the syntax diagrams on
-- the lang_select.html page may be taken.
--
-- -- syntax diagram join-constraint
--
do_join_test("e_select-0.1.1", [[
SELECT count(*) FROM t1 JOIN_PATTERN t2 ON (t1.a=t2.a)
]], {3})
do_join_test("e_select-0.1.2", [[
SELECT count(*) FROM t1 JOIN_PATTERN t2 USING (a)
]], {3})
do_join_test("e_select-0.1.3", [[
SELECT count(*) FROM t1 JOIN_PATTERN t2
]], {9})
test:do_catchsql_test(
"e_select-0.1.4",
[[
SELECT count(*) FROM t1, t2 ON (t1.a=t2.a) USING (a)
]], {
-- <e_select-0.1.4>
1, "cannot have both ON and USING clauses in the same join"
-- </e_select-0.1.4>
})
test:do_catchsql_test(
"e_select-0.1.5",
[[
SELECT count(*) FROM t1, t2 USING (a) ON (t1.a=t2.a)
]], {
-- <e_select-0.1.5>
1, [[At line 1 at or near position 47: keyword 'ON' is reserved. Please use double quotes if 'ON' is an identifier.]]
-- </e_select-0.1.5>
})
-- -- syntax diagram select-core
--
-- 0: SELECT ...
-- 1: SELECT DISTINCT ...
-- 2: SELECT ALL ...
--
-- 0: No FROM clause
-- 1: Has FROM clause
--
-- 0: No WHERE clause
-- 1: Has WHERE clause
--
-- 0: No GROUP BY clause
-- 1: Has GROUP BY clause
-- 2: Has GROUP BY and HAVING clauses
--
test:do_select_tests(
"e_select-0.2",
{
{"0000.1", "SELECT 1, 2, 3 ", {1, 2, 3}},
{"1000.1", "SELECT DISTINCT 1, 2, 3 ", {1, 2, 3}},
{"2000.1", "SELECT ALL 1, 2, 3 ", {1, 2, 3}},
{"0100.1", "SELECT a, b, a||b FROM t1 ", {"a", "one", "aone", "b", "two", "btwo", "c", "three", "cthree"}},
{"1100.1", "SELECT DISTINCT a, b, a||b FROM t1 ", {"a", "one", "aone", "b", "two", "btwo", "c", "three", "cthree"}},
{"1200.1", "SELECT ALL a, b, a||b FROM t1 ", {"a", "one", "aone", "b", "two", "btwo", "c", "three", "cthree"}},
{"0010.1", "SELECT 1, 2, 3 WHERE true ", {1, 2, 3}},
{"0010.2", "SELECT 1, 2, 3 WHERE false ", {}},
{"0010.3", "SELECT 1, 2, 3 WHERE NULL ", {}},
{"1010.1", "SELECT DISTINCT 1, 2, 3 WHERE true ", {1, 2, 3}},
{"2010.1", "SELECT ALL 1, 2, 3 WHERE true ", {1, 2, 3}},
{"0110.1", "SELECT a, b, a||b FROM t1 WHERE a!='x' ", {"a", "one", "aone", "b", "two", "btwo", "c", "three", "cthree"}},
{"0110.2", "SELECT a, b, a||b FROM t1 WHERE a=='x'", {}},
{"1110.1", "SELECT DISTINCT a, b, a||b FROM t1 WHERE a!='x' ", {"a", "one", "aone", "b", "two", "btwo", "c", "three", "cthree"}},
{"2110.0", "SELECT ALL a, b, a||b FROM t1 WHERE a=='x'", {}},
{"0001.1", "SELECT 1, 2, 3 GROUP BY 2", {1, 2, 3}},
{"0002.1", "SELECT 1, 2, 3 GROUP BY 2 HAVING count(*)=1", {1, 2, 3}},
{"0002.2", "SELECT 1, 2, 3 GROUP BY 2 HAVING count(*)>1", {}},
{"1001.1", "SELECT DISTINCT 1, 2, 3 GROUP BY 2", {1, 2, 3}},
{"1002.1", "SELECT DISTINCT 1, 2, 3 GROUP BY 2 HAVING count(*)=1", {1, 2, 3}},
{"1002.2", "SELECT DISTINCT 1, 2, 3 GROUP BY 2 HAVING count(*)>1", {}},
{"2001.1", "SELECT ALL 1, 2, 3 GROUP BY 2", {1, 2, 3}},
{"2002.1", "SELECT ALL 1, 2, 3 GROUP BY 2 HAVING count(*)=1", {1, 2, 3}},
{"2002.2", "SELECT ALL 1, 2, 3 GROUP BY 2 HAVING count(*)>1", {}},
{"0101.1", "SELECT count(*), max(a) FROM t1 GROUP BY b", {1, "a", 1, "c", 1, "b"}},
{"0102.1", "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=1", {1, "a", 1, "c", 1, "b"}},
{"0102.2", "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2", { }},
{"1101.1", "SELECT DISTINCT count(*), max(a) FROM t1 GROUP BY b", {1, "a", 1, "c", 1, "b"}},
{"1102.1", "SELECT DISTINCT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=1", {1, "a", 1, "c", 1, "b"}},
{"1102.2", "SELECT DISTINCT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2", { }},
{"2101.1", "SELECT ALL count(*), max(a) FROM t1 GROUP BY b", {1, "a", 1, "c", 1, "b"}},
{"2102.1", "SELECT ALL count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=1", {1, "a", 1, "c", 1, "b"}},
{"2102.2", "SELECT ALL count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2", {}},
{"0011.1", "SELECT 1, 2, 3 WHERE true GROUP BY 2", {1, 2, 3}},
{"0012.1", "SELECT 1, 2, 3 WHERE false GROUP BY 2 HAVING count(*)=1", {}},
{"0012.2", "SELECT 1, 2, 3 WHERE false GROUP BY 2 HAVING count(*)>1", {}},
{"1011.1", "SELECT DISTINCT 1, 2, 3 WHERE false GROUP BY 2", {}},
{"1012.1", "SELECT DISTINCT 1, 2, 3 WHERE true GROUP BY 2 HAVING count(*)=1", {1, 2, 3}},
{"1012.2", "SELECT DISTINCT 1, 2, 3 WHERE NULL GROUP BY 2 HAVING count(*)>1", {}},
{"2011.1", "SELECT ALL 1, 2, 3 WHERE true GROUP BY 2", {1, 2, 3}},
{"2012.1", "SELECT ALL 1, 2, 3 WHERE false GROUP BY 2 HAVING count(*)=1", {}},
{"2012.2", "SELECT ALL 1, 2, 3 WHERE true GROUP BY 2 HAVING count(*)>1", {}},
{"0111.1", "SELECT count(*), max(a) FROM t1 WHERE a='a' GROUP BY b", {1, "a"}},
{"0112.1", "SELECT count(*), max(a) FROM t1 WHERE a='c' GROUP BY b HAVING count(*)=1", {1, "c"}},
{"0112.2", "SELECT count(*), max(a) FROM t1 WHERE false GROUP BY b HAVING count(*)=2", { }},
{"1111.1", "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a<'c' GROUP BY b", {1, "a", 1, "b"}},
{"1112.1", "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a>'a' GROUP BY b HAVING count(*)=1", {1, "c", 1, "b"}},
{"1112.2", "SELECT DISTINCT count(*), max(a) FROM t1 WHERE false GROUP BY b HAVING count(*)=2", { }},
{"2111.1", "SELECT ALL count(*), max(a) FROM t1 WHERE b>'one' GROUP BY b", {1, "c", 1, "b"}},
{"2112.1", "SELECT ALL count(*), max(a) FROM t1 WHERE a!='b' GROUP BY b HAVING count(*)=1", {1, "a", 1, "c"}},
{"2112.2", "SELECT ALL count(*), max(a) FROM t1 WHERE false GROUP BY b HAVING count(*)=2", { }},
})
-- -- syntax diagram result-column
--
test:do_select_tests(
"e_select-0.3",
{
{"1", "SELECT * FROM t1", {"a", "one", "b", "two", "c", "three"}},
{"2", "SELECT t1.* FROM t1", {"a", "one", "b", "two", "c", "three"}},
{"3", "SELECT 'x'||a||'x' FROM t1", {"xax", "xbx", "xcx"}},
{"4", "SELECT 'x'||a||'x' alias FROM t1", {"xax", "xbx", "xcx"}},
{"5", "SELECT 'x'||a||'x' AS alias FROM t1", {"xax", "xbx", "xcx"}},
})
-- # -- syntax diagram join-source
-- #
-- # -- syntax diagram join-op
-- #
-- do_select_tests e_select-0.4 {
-- 1 "SELECT t1.rowid FROM t1" {1, 2, 3}
-- 2 "SELECT t1.rowid FROM t1,t2" {1, 1, 1, 2, 2, 2, 3, 3, 3}
-- 3 "SELECT t1.rowid FROM t1,t2,t3" {1 1 1 1 1 1 2 2 2 2, 2, 2, 3, 3, 3, 3, 3, 3}
-- 4 "SELECT t1.rowid FROM t1" {1, 2, 3}
-- 5 "SELECT t1.rowid FROM t1 JOIN t2" {1, 1, 1, 2, 2, 2, 3, 3, 3}
-- 6 "SELECT t1.rowid FROM t1 JOIN t2 JOIN t3"-- {1 1 1 1 1 1 2 2 2 2, 2, 2, 3, 3, 3, 3, 3, 3}
-- 7 "SELECT t1.rowid FROM t1 NATURAL JOIN t3" {1, 2}
-- 8 "SELECT t1.rowid FROM t1 NATURAL LEFT OUTER JOIN t3" {1, 2, 3}
-- 9 "SELECT t1.rowid FROM t1 NATURAL LEFT JOIN t3" {1, 2, 3}
-- 10 "SELECT t1.rowid FROM t1 NATURAL INNER JOIN t3" {1, 2}
-- 11 "SELECT t1.rowid FROM t1 NATURAL CROSS JOIN t3" {1, 2}
-- 12 "SELECT t1.rowid FROM t1 JOIN t3" {1, 1, 2, 2, 3, 3}
-- 13 "SELECT t1.rowid FROM t1 LEFT OUTER JOIN t3" {1, 1, 2, 2, 3, 3}
-- 14 "SELECT t1.rowid FROM t1 LEFT JOIN t3" {1, 1, 2, 2, 3, 3}
-- 15 "SELECT t1.rowid FROM t1 INNER JOIN t3" {1, 1, 2, 2, 3, 3}
-- 16 "SELECT t1.rowid FROM t1 CROSS JOIN t3" {1, 1, 2, 2, 3, 3}
-- }
-- # -- syntax diagram compound-operator
-- #
-- do_select_tests e_select-0.5 {
-- 1 "SELECT rowid FROM t1 UNION ALL SELECT rowid+2 FROM t4" {1, 2, 3, 3, 4}
-- 2 "SELECT rowid FROM t1 UNION SELECT rowid+2 FROM t4" {1, 2, 3, 4}
-- 3 "SELECT rowid FROM t1 INTERSECT SELECT rowid+2 FROM t4" {3}
-- 4 "SELECT rowid FROM t1 EXCEPT SELECT rowid+2 FROM t4" {1, 2}
-- }
-- -- syntax diagram ordering-term
--
test:do_select_tests(
"e_select-0.6",
{
{"1", "SELECT b||a FROM t1 ORDER BY b||a", {"onea", "threec", "twob"}},
{"2", "SELECT b||a FROM t1 ORDER BY (b||a) COLLATE \"unicode_ci\"", {"onea", "threec", "twob"}},
{"3", "SELECT b||a FROM t1 ORDER BY (b||a) ASC", {"onea", "threec", "twob"}},
{"4", "SELECT b||a FROM t1 ORDER BY (b||a) DESC", {"twob", "threec", "onea"}},
})
-- -- syntax diagram select-stmt
--
test:do_select_tests(
"e_select-0.7",
{
{"1", "SELECT * FROM t1", {"a", "one", "b", "two", "c", "three"}},
{"2", "SELECT * FROM t1 ORDER BY b", {"a", "one", "c", "three", "b", "two"}},
{"3", "SELECT * FROM t1 ORDER BY b, a", {"a", "one", "c", "three", "b", "two"}},
{"4", "SELECT * FROM t1 LIMIT 10", {"a", "one", "b", "two", "c", "three"}},
{"5", "SELECT * FROM t1 LIMIT 10 OFFSET 5", {}},
{"6", "SELECT * FROM t1 LIMIT 10, 5", {}},
{"7", "SELECT * FROM t1 ORDER BY a LIMIT 10", {"a", "one", "b", "two", "c", "three"}},
{"8", "SELECT * FROM t1 ORDER BY b LIMIT 10 OFFSET 5", {}},
{"9", "SELECT * FROM t1 ORDER BY a,b LIMIT 10, 5", {}},
{"10", "SELECT * FROM t1 UNION SELECT b, a FROM t1", {"a", "one", "b", "two", "c", "three", "one", "a", "three", "c", "two", "b"}},
{"11", "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b", {"one", "a", "two", "b", "three", "c", "a", "one", "c", "three", "b", "two"}},
{"12", "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b, a", {"one", "a", "two", "b", "three", "c", "a", "one", "c", "three", "b", "two"}},
{"13", "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10", {"a", "one", "b", "two", "c", "three", "one", "a", "three", "c", "two", "b"}},
{"14", "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10 OFFSET 5", {"two", "b"}},
{"15", "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10, 5", {}},
{"16", "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY a LIMIT 10", {"a", "one", "b", "two", "c", "three", "one", "a", "three", "c", "two", "b"}},
{"17", "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b LIMIT 10 OFFSET 5", {"b", "two"}},
{"18", "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY a,b LIMIT 10, 5", {}},
})
---------------------------------------------------------------------------
-- The following tests focus on FROM clause (join) processing.
--
-- EVIDENCE-OF: R-16074-54196 If the FROM clause is omitted from a simple
-- SELECT statement, then the input data is implicitly a single row zero
-- columns wide
--
test:do_select_tests(
"e_select-1.1",
{
{"1", "SELECT 'abc'", {"abc"}},
{"2", "SELECT 'abc' WHERE NULL", {}},
{"3", "SELECT NULL", {""}},
{"4", "SELECT count(*)", {1}},
{"5", "SELECT count(*) WHERE false", {0}},
{"6", "SELECT count(*) WHERE true", {1}},
})
--
-- The following block of tests - e_select-1.4.* - test that the description
-- of cartesian joins in the SELECT documentation is consistent with sql.
-- In doing so, we test the following three requirements as a side-effect:
--
-- EVIDENCE-OF: R-49872-03192 If the join-operator is "CROSS JOIN",
-- "INNER JOIN", "JOIN" or a comma (",") and there is no ON or USING
-- clause, then the result of the join is simply the cartesian product of
-- the left and right-hand datasets.
--
-- The tests are built on this assertion. NUMBERly, they test that the output
-- of a CROSS JOIN, JOIN, INNER JOIN or "," join matches the expected result
-- of calculating the cartesian product of the left and right-hand datasets.
--
-- EVIDENCE-OF: R-46256-57243 There is no difference between the "INNER
-- JOIN", "JOIN" and "," join operators.
--
-- EVIDENCE-OF: R-25071-21202 The "CROSS JOIN" join operator produces the
-- same result as the "INNER JOIN", "JOIN" and "," operators
--
-- All tests are run 4 times, with the only difference in each run being
-- which of the 4 equivalent cartesian product join operators are used.
-- Since the output data is the same in all cases, we consider that this
-- qualifies as testing the two statements above.
--
test:do_execsql_test(
"e_select-1.4.0",
[[
CREATE TABLE x1(id INT primary key, a TEXT , b TEXT );
CREATE TABLE x2(id INT primary key, c NUMBER , d NUMBER , e NUMBER );
CREATE TABLE x3(id INT primary key, f TEXT , g TEXT , h TEXT , i TEXT );
-- x1: 3 rows, 2 columns
INSERT INTO x1 VALUES(1,'24', 'converging');
INSERT INTO x1 VALUES(2, NULL, CAST(X'CB71' as TEXT));
INSERT INTO x1 VALUES(3,'blonds', 'proprietary');
-- x2: 2 rows, 3 columns
INSERT INTO x2 VALUES(1,-60.06, NULL, NULL);
INSERT INTO x2 VALUES(2,-58, NULL, 1.21);
-- x3: 5 rows, 4 columns
INSERT INTO x3 VALUES(1,'-39.24', NULL, 'encompass', '-1');
INSERT INTO x3 VALUES(2,'presenting', '51', 'reformation', 'dignified');
INSERT INTO x3 VALUES(3,'conducting', '-87.24', '37.56', NULL);
INSERT INTO x3 VALUES(4,'coldest', '-96', 'dramatists', '82.3');
INSERT INTO x3 VALUES(5,'alerting', NULL, '-93.79', NULL);
]], {
-- <e_select-1.4.0>
-- </e_select-1.4.0>
})
-- EVIDENCE-OF: R-59089-25828 The columns of the cartesian product
-- dataset are, in order, all the columns of the left-hand dataset
-- followed by all the columns of the right-hand dataset.
--
do_join_test("e_select-1.4.1.1", [[
SELECT a,b,c,d,e FROM x1 JOIN_PATTERN x2 LIMIT 1
]], {"24", "converging", -60.06, "", ""})
do_join_test("e_select-1.4.1.2", [[
SELECT c,d,e,a,b FROM x2 JOIN_PATTERN x1 LIMIT 1
]], {-60.06, "", "", "24", "converging"})
do_join_test("e_select-1.4.1.3", [[
SELECT f,g,h,i,c,d,e FROM x3 JOIN_PATTERN x2 LIMIT 1
]], {'-39.24', "", "encompass", '-1', -60.06, "", ""})
do_join_test("e_select-1.4.1.4", [[
SELECT c,d,e,f,g,h,i FROM x2 JOIN_PATTERN x3 LIMIT 1
]], {-60.06, "", "", '-39.24', "", "encompass", '-1'})
-- EVIDENCE-OF: R-44414-54710 There is a row in the cartesian product
-- dataset formed by combining each unique combination of a row from the
-- left-hand and right-hand datasets.
--
do_join_test("e_select-1.4.2.1", [[
SELECT c,d,e,f,g,h,i FROM x2 JOIN_PATTERN x3 ORDER BY +c, +f
]], { -60.06,"","","-39.24","","encompass","-1",-60.06,"","","alerting","","-93.79","",-60.06,"","","coldest","-96","dramatists","82.3",-60.06,"","","conducting","-87.24","37.56","",-60.06,"","","presenting","51","reformation","dignified",-58,"",1.21,"-39.24","","encompass","-1",-58,"",1.21,"alerting","","-93.79","",-58,"",1.21,"coldest","-96","dramatists","82.3",-58,"",1.21,"conducting","-87.24","37.56","",-58,"",1.21,"presenting","51","reformation","dignified" })
-- TODO: Come back and add a few more like the above.
-- EVIDENCE-OF: R-18439-38548 In other words, if the left-hand dataset
-- consists of Nleft rows of Mleft columns, and the right-hand dataset of
-- Nright rows of Mright columns, then the cartesian product is a dataset
-- of Nleft×Nright rows, each containing Mleft+Mright columns.
--
-- x1, x2 (Nlhs=3, Nrhs=2) (Mlhs=2, Mrhs=3)
do_join_test("e_select-1.4.3.1", [[
SELECT count(*) FROM x1 JOIN_PATTERN x2
]], {3 * 2})
test:do_test(
"e_select-1.4.3.2",
function()
return #test:execsql("SELECT a,b,c,d,e FROM x1, x2") / 6
end, 2 + 3)
-- x2, x3 (Nlhs=2, Nrhs=5) (Mlhs=3, Mrhs=4)
do_join_test("e_select-1.4.3.3", [[
SELECT count(*) FROM x2 JOIN_PATTERN x3
]], {2 * 5})
test:do_test(
"e_select-1.4.3.4",
function()
return #test:execsql("SELECT c,d,e,f,g,h,i FROM x2 JOIN x3") / 10
end,(3 + 4))
-- x3, x1 (Nlhs=5, Nrhs=3) (Mlhs=4, Mrhs=2)
do_join_test("e_select-1.4.3.5", [[
SELECT count(*) FROM x3 JOIN_PATTERN x1
]], {5 * 3})
test:do_test(
"e_select-1.4.3.6",
function()
return #test:execsql("SELECT f,g,h,i,a,b FROM x3 CROSS JOIN x1") / 15
end, (4 + 2))
-- x3, x3 (Nlhs=5, Nrhs=5) (Mlhs=4, Mrhs=4)
do_join_test("e_select-1.4.3.7", [[
SELECT count(*) FROM x3 JOIN_PATTERN x3
]], {5 * 5})
test:do_test(
"e_select-1.4.3.8",
function()
return #test:execsql("SELECT x3.f,x3.g,x3.h,x3.i,x4.f,x4.g,x4.h,x4.i FROM x3 INNER JOIN x3 AS x4") / 25
end, (4 + 4))
-- Some extra cartesian product tests using tables t1 and t2.
--
test:do_execsql_test(
"e_select-1.4.4.1",
[[
SELECT * FROM t1, t2
]], t1_cross_t2)
test:do_execsql_test(
"e_select-1.4.4.2",
[[
SELECT * FROM t1 AS x, t1 AS y
]], t1_cross_t1)
test:do_select_tests(
"e_select-1.4.5",{
{1, " SELECT * FROM t1 CROSS JOIN t2 ", t1_cross_t2},
{2, " SELECT * FROM t1 AS y CROSS JOIN t1 AS x ", t1_cross_t1},
{3, " SELECT * FROM t1 INNER JOIN t2 ", t1_cross_t2},
{4, " SELECT * FROM t1 AS y INNER JOIN t1 AS x ", t1_cross_t1 }})
-- EVIDENCE-OF: R-38465-03616 If there is an ON clause then the ON
-- expression is evaluated for each row of the cartesian product as a
-- boolean expression. Only rows for which the expression evaluates to
-- true are included from the dataset.
--
local data ={
{"1"," SELECT * FROM t1 JOIN_PATTERN t2 ON (true) ",t1_cross_t2},
{"2"," SELECT * FROM t1 JOIN_PATTERN t2 ON (false) ",{}},
{"3"," SELECT * FROM t1 JOIN_PATTERN t2 ON (NULL) ",{}},
{"6"," SELECT * FROM t1 JOIN_PATTERN t2 ON (true) ",t1_cross_t2},
{"9"," SELECT t1.b, t2.b FROM t1 JOIN_PATTERN t2 ON (t1.a = t2.a) ",{"one", "I", "two", "II", "three", "III"}},
{"10"," SELECT t1.b, t2.b FROM t1 JOIN_PATTERN t2 ON (t1.a = 'a') ",{"one", "I", "one", "II", "one", "III"}},
{"11"," SELECT t1.b, t2.b FROM t1 JOIN_PATTERN t2 ON (CASE WHEN t1.a = 'a' THEN NULL ELSE true END)",
{"two", "I", "two", "II", "two", "III", "three", "I", "three", "II", "three", "III"}},
}
for _, val in ipairs(data) do
local tn = val[1]
local select = val[2]
local res = val[3]
do_join_test("e_select-1.3."..tn, select, res)
end
-- EVIDENCE-OF: R-49933-05137 If there is a USING clause then each of the
-- column names specified must exist in the datasets to both the left and
-- right of the join-operator.
--
test:do_catchsql_test(
"e_select-1.4.1",
"SELECT * FROM t1, t3 USING (b)",
{1, "/cannot join using column B -- column not present in both tables/"})
test:do_catchsql_test(
"e_select-1.4.2",
"SELECT * FROM t3, t1 USING (c)",
{1, "/cannot join using column C -- column not present in both tables/"})
test:do_catchsql_test(
"e_select-1.4.3",
"SELECT * FROM t3, (SELECT a AS b, b AS c FROM t1) USING (a)",
{1, "/cannot join using column A -- column not present in both tables/"})
-- EVIDENCE-OF: R-22776-52830 For each pair of named columns, the
-- expression "lhs.X = rhs.X" is evaluated for each row of the cartesian
-- product as a boolean expression. Only rows for which all such
-- expressions evaluates to true are included from the result set.
--
test:do_select_tests(
"e_select-1.5",
{
{1, "SELECT * FROM t1, t3 USING (a)", {"a", "one", 1, "b", "two", 2}},
{2, "SELECT * FROM t3, t4 USING (a,c)", {"b", 2}}
})
-- MUST_WORK_TEST
if (0 > 0)
then
-- EVIDENCE-OF: R-54046-48600 When comparing values as a result of a
-- USING clause, the normal rules for handling affinities, collation
-- sequences and NULL values in comparisons apply.
--
-- EVIDENCE-OF: R-38422-04402 The column from the dataset on the
-- left-hand side of the join-operator is considered to be on the
-- left-hand side of the comparison operator (=) for the purposes of
-- collation sequence and affinity precedence.
--
-- Legacy from the original code. Must be replaced with analogue
-- functions from box.
local X = nil
test:do_execsql_test(
"e_select-1.6.0",
[[
CREATE TABLE t5(a TEXT COLLATE "unicode_ci", b TEXT COLLATE "binary");
INSERT INTO t5 VALUES('AA', 'cc');
INSERT INTO t5 VALUES('BB', 'dd');
INSERT INTO t5 VALUES(NULL, NULL);
CREATE TABLE t6(a TEXT COLLATE "binary", b TEXT COLLATE "unicode_ci");
INSERT INTO t6 VALUES('aa', 'cc');
INSERT INTO t6 VALUES('bb', 'DD');
INSERT INTO t6 VALUES(NULL, NULL);
]], {
-- <e_select-1.6.0>
-- </e_select-1.6.0>
})
-- Legacy from the original code. Must be replaced with analogue
-- functions from box.
local res = nil
local tn = nil
for _ in X(0, "X!foreach", [=[["tn select res","\n 1 { SELECT * FROM t5 JOIN_PATTERN t6 USING (a) } {AA cc cc BB dd DD}\n 2 { SELECT * FROM t6 JOIN_PATTERN t5 USING (a) } {}\n 3 { SELECT * FROM (SELECT a COLLATE "unicode_ci", b FROM t6) JOIN_PATTERN t5 USING (a) } \n {aa cc cc bb DD dd}\n 4 { SELECT * FROM t5 JOIN_PATTERN t6 USING (a,b) } {AA cc}\n 5 { SELECT * FROM t6 JOIN_PATTERN t5 USING (a,b) } {}\n "]]=]) do
do_join_test("e_select-1.6."..tn, select, res)
end
-- EVIDENCE-OF: R-57047-10461 For each pair of columns identified by a
-- USING clause, the column from the right-hand dataset is omitted from
-- the joined dataset.
--
-- EVIDENCE-OF: R-56132-15700 This is the only difference between a USING
-- clause and its equivalent ON constraint.
--
for _ in X(0, "X!foreach", [=[["tn select res","\n 1a { SELECT * FROM t1 JOIN_PATTERN t2 USING (a) } \n {a one I b two II c three III}\n 1b { SELECT * FROM t1 JOIN_PATTERN t2 ON (t1.a=t2.a) }\n {a one a I b two b II c three c III}\n\n 2a { SELECT * FROM t3 JOIN_PATTERN t4 USING (a) } \n {a 1 {} b 2 2}\n 2b { SELECT * FROM t3 JOIN_PATTERN t4 ON (t3.a=t4.a) } \n {a 1 a {} b 2 b 2}\n\n 3a { SELECT * FROM t3 JOIN_PATTERN t4 USING (a,c) } {b 2}\n 3b { SELECT * FROM t3 JOIN_PATTERN t4 ON (t3.a=t4.a AND t3.c=t4.c) } {b 2 b 2}\n\n 4a { SELECT * FROM (SELECT a COLLATE "unicode_ci", b FROM t6) AS x \n JOIN_PATTERN t5 USING (a) } \n {aa cc cc bb DD dd}\n 4b { SELECT * FROM (SELECT a COLLATE "unicode_ci", b FROM t6) AS x\n JOIN_PATTERN t5 ON (x.a=t5.a) } \n {aa cc AA cc bb DD BB dd}\n "]]=]) do
do_join_test("e_select-1.7."..tn, select, res)
end
X(630, "X!cmd", [=[["EVIDENCE-OF:","R-42531-52874","If","the","join-operator","is","a","LEFT JOIN","or"]]=])
X(632, "X!cmd", [=[["been","applied,","an","extra","row","is","added","to","the","output","for","each","row","in","the"]]=])
X(633, "X!cmd", [=[["original","left-hand","input","dataset","that","corresponds","to","no","rows","at","all","in"]]=])
X(634, "X!cmd", [=[["the","composite","dataset","(if","any)."]]=])
end
test:do_execsql_test(
"e_select-1.8.0",
[[
CREATE TABLE t7(a TEXT PRIMARY KEY, b TEXT, c INT );
CREATE TABLE t8(a TEXT PRIMARY KEY, d TEXT, e INT );
INSERT INTO t7 VALUES('x', 'ex', 24);
INSERT INTO t7 VALUES('y', 'why', 25);
INSERT INTO t8 VALUES('x', 'abc', 24);
INSERT INTO t8 VALUES('z', 'ghi', 26);
]], {
-- <e_select-1.8.0>
-- </e_select-1.8.0>
})
test:do_select_tests(
"e_select-1.8",
{
{"1a", "SELECT count(*) FROM t7 JOIN t8 ON (t7.a=t8.a)", {1}},
{"1b", "SELECT count(*) FROM t7 LEFT JOIN t8 ON (t7.a=t8.a)", {2}},
{"2a", "SELECT count(*) FROM t7 JOIN t8 USING (a)", {1}},
{"2b", "SELECT count(*) FROM t7 LEFT JOIN t8 USING (a)", {2}},
})
-- EVIDENCE-OF: R-15607-52988 The added rows contain NULL values in the
-- columns that would normally contain values copied from the right-hand
-- input dataset.
--
test:do_select_tests(
"e_select-1.9",
{
{"1a", "SELECT * FROM t7 JOIN t8 ON (t7.a=t8.a)", {"x", "ex", 24, "x", "abc", 24}},
{"1b", "SELECT * FROM t7 LEFT JOIN t8 ON (t7.a=t8.a)", {"x", "ex", 24, "x", "abc", 24, "y", "why", 25, "", "", ""}},
{"2a", "SELECT * FROM t7 JOIN t8 USING (a)", {"x", "ex", 24, "abc", 24}},
{"2b", "SELECT * FROM t7 LEFT JOIN t8 USING (a)", {"x", "ex", 24, "abc", 24, "y", "why", 25, "", ""}},
})
-- EVIDENCE-OF: R-04932-55942 If the NATURAL keyword is in the
-- join-operator then an implicit USING clause is added to the
-- join-constraints. The implicit USING clause contains each of the
-- column names that appear in both the left and right-hand input
-- datasets.
--
test:do_select_tests(
"e_select-1-10",
{
{"1a", "SELECT * FROM t7 JOIN t8 USING (a)", {"x", "ex", 24, "abc", 24}},
{"1b", "SELECT * FROM t7 NATURAL JOIN t8", {"x", "ex", 24, "abc", 24}},
{"2a", "SELECT * FROM t8 JOIN t7 USING (a)", {"x", "abc", 24, "ex", 24}},
{"2b", "SELECT * FROM t8 NATURAL JOIN t7", {"x", "abc", 24, "ex", 24}},
{"3a", "SELECT * FROM t7 LEFT JOIN t8 USING (a)", {"x", "ex", 24, "abc", 24, "y", "why", 25, "", ""}},
{"3b", "SELECT * FROM t7 NATURAL LEFT JOIN t8", {"x", "ex", 24, "abc", 24, "y", "why", 25, "", ""}},
{"4a", "SELECT * FROM t8 LEFT JOIN t7 USING (a)", {"x", "abc", 24, "ex", 24, "z", "ghi", 26, "", ""}},
{"4b", "SELECT * FROM t8 NATURAL LEFT JOIN t7", {"x", "abc", 24, "ex", 24, "z", "ghi", 26, "", ""}},
{"5a", "SELECT * FROM t3 JOIN t4 USING (a,c)", {"b", 2}},
{"5b", "SELECT * FROM t3 NATURAL JOIN t4", {"b", 2}},
{"6a", "SELECT * FROM t3 LEFT JOIN t4 USING (a,c)", {"a", 1, "b", 2}},
{"6b", "SELECT * FROM t3 NATURAL LEFT JOIN t4", {"a", 1, "b", 2}},
})
-- EVIDENCE-OF: R-49566-01570 If the left and right-hand input datasets
-- feature no common column names, then the NATURAL keyword has no effect
-- on the results of the join.
--
test:do_execsql_test(
"e_select-1.11.0",
[[
CREATE TABLE t10(id INT primary key, x INT , y TEXT);
INSERT INTO t10 VALUES(1, 1, 'true');
INSERT INTO t10 VALUES(2, 0, 'false');
]], {
-- <e_select-1.11.0>
-- </e_select-1.11.0>
})
test:do_select_tests(
"e_select-1-11",
{
{"1a", "SELECT a, x FROM t1 CROSS JOIN t10", {"a", 1, "a", 0, "b", 1, "b", 0, "c", 1, "c", 0}},
{"1b", "SELECT a, x FROM t1 NATURAL CROSS JOIN t10", {"a", 1, "a", 0, "b", 1, "b", 0, "c", 1, "c", 0}},
})
-- EVIDENCE-OF: R-39625-59133 A USING or ON clause may not be added to a
-- join that specifies the NATURAL keyword.
--
data = {
"SELECT * FROM t1 NATURAL LEFT JOIN t2 USING (a)",
"SELECT * FROM t1 NATURAL LEFT JOIN t2 ON (t1.a=t2.a)",
"SELECT * FROM t1 NATURAL LEFT JOIN t2 ON (45)",
}
for tn, sql in ipairs(data) do
test:do_catchsql_test(
"e_select-1.12."..tn,
string.format([[
%s
]], sql), {
1, "a NATURAL join may not have an ON or USING clause"
})
end
---------------------------------------------------------------------------
-- The next block of tests - e_select-3.* - concentrate on verifying
-- statements made regarding WHERE clause processing.
--
test:drop_all_tables()
test:do_execsql_test(
"e_select-3.0",
[[
CREATE TABLE x1(id INT PRIMARY KEY, k INT , x TEXT , y TEXT , z TEXT );
INSERT INTO x1 VALUES(1, 1, 'relinquished', 'aphasia', '78.43');
INSERT INTO x1 VALUES(2, 2, 'A8E8D66F', '07CF', '-81');
INSERT INTO x1 VALUES(3, 3, '-22', '-27.57', NULL);
INSERT INTO x1 VALUES(4, 4, NULL, 'bygone', 'picky');
INSERT INTO x1 VALUES(5, 5, NULL, '96.28', NULL);
INSERT INTO x1 VALUES(6, 6, '0', '1', '2');
CREATE TABLE x2(id INT primary key, k INT , x TEXT , y2 TEXT );
INSERT INTO x2 VALUES(1, 1, '50', 'B82838');
INSERT INTO x2 VALUES(2, 5, '84.79', '65.88');
INSERT INTO x2 VALUES(3, 3, '-22', '0E1BE452A393');
INSERT INTO x2 VALUES(4, 7, 'mistrusted', 'standardized');
]], {
-- <e_select-3.0>
-- </e_select-3.0>
})
-- EVIDENCE-OF: R-60775-64916 If a WHERE clause is specified, the WHERE
-- expression is evaluated for each row in the input data as a boolean
-- expression. Only rows for which the WHERE clause expression evaluates
-- to true are included from the dataset before continuing.
--
test:do_execsql_test(
"e_select-3.1.5",
[[
SELECT k FROM x1 WHERE x IS NULL
]], {
-- <e_select-3.1.5>
4, 5
-- </e_select-3.1.5>
})
test:do_execsql_test(
"e_select-3.2.1a",
[[
SELECT k FROM x1 LEFT JOIN x2 USING(k)
]], {
-- <e_select-3.2.1a>
1, 2, 3, 4, 5, 6
-- </e_select-3.2.1a>
})
test:do_execsql_test(
"e_select-3.2.1b",
[[
SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k <> 0
]], {
-- <e_select-3.2.1b>
1, 3, 5
-- </e_select-3.2.1b>
})
test:do_execsql_test(
"e_select-3.2.2",
[[
SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k IS NULL
]], {
-- <e_select-3.2.2>
2, 4, 6
-- </e_select-3.2.2>
})
test:do_execsql_test(
"e_select-3.2.3",
[[
SELECT k FROM x1 NATURAL JOIN x2 WHERE x2.k <> 0
]], {
-- <e_select-3.2.3>
3
-- </e_select-3.2.3>
})
test:do_execsql_test(
"e_select-3.2.4",
[[
SELECT k FROM x1 NATURAL JOIN x2 WHERE x2.k-3 <> 0
]], {
-- <e_select-3.2.4>
-- </e_select-3.2.4>
})
---------------------------------------------------------------------------
-- Tests below this point are focused on verifying the testable statements
-- related to caculating the result rows of a simple SELECT statement.
--
test:drop_all_tables()
test:do_execsql_test(
"e_select-4.0",
[[
CREATE TABLE z1(id INT primary key, a NUMBER, b NUMBER, c TEXT);
CREATE TABLE z2(id INT primary key, d NUMBER, e NUMBER);
CREATE TABLE z3(id INT primary key, a NUMBER, b NUMBER);
INSERT INTO z1 VALUES(1, 51.65, -59.58, 'belfries');
INSERT INTO z1 VALUES(2, -5, NULL, '75');
INSERT INTO z1 VALUES(3, -2.2, -23.18, 'suiters');
INSERT INTO z1 VALUES(4, NULL, 67, 'quartets');
INSERT INTO z1 VALUES(5, -1.04, -32.3, 'aspen');
INSERT INTO z1 VALUES(6, 63, 0, '-26');
INSERT INTO z2 VALUES(1, NULL, 21);
INSERT INTO z2 VALUES(2, 36.0, 6.0);
INSERT INTO z3 VALUES(1, 123.21, 123.12);
INSERT INTO z3 VALUES(2, 49.17, -67);
]], {
-- <e_select-4.0>
-- </e_select-4.0>
})
-- EVIDENCE-OF: R-36327-17224 If a result expression is the special
-- expression "*" then all columns in the input data are substituted for
-- that one expression.
--
-- EVIDENCE-OF: R-43693-30522 If the expression is the alias of a table
-- or subquery in the FROM clause followed by ".*" then all columns from
-- the named table or subquery are substituted for the single expression.
--
test:do_select_tests(
"e_select-4.1",
{
{"1", "SELECT a,b,c FROM z1 LIMIT 1", {51.65, -59.58, "belfries"}},
{"2", "SELECT a,b,c,d,e FROM z1,z2 LIMIT 1", {51.65, -59.58, "belfries", "", 21}},
{"3", "SELECT z1.a, z1.b, z1.c FROM z1,z2 LIMIT 1", {51.65, -59.58, "belfries"}},
{"4", "SELECT z2.d, z2.e FROM z1,z2 LIMIT 1", {"", 21}},
{"5", "SELECT z2.d, z2.e, z1.a, z1.b, z1.c FROM z1,z2 LIMIT 1", {"", 21, 51.65, -59.58, "belfries"}},
{"6", "SELECT count(*), a,b,c FROM z1", {6, 63, 0, "-26"}},
{"7", "SELECT max(a), a,b,c FROM z1", {63, 63, 0, "-26"}},
{"8", "SELECT a,b,c, min(a) FROM z1", {-5, "", "75", -5}},
{"9", "SELECT a,b,c,d,e,a,b,c,d,e FROM z1,z2 LIMIT 1", {
51.65, -59.58, "belfries", "", 21, 51.65, -59.58, "belfries", "", 21}},
{"10", "SELECT z1.a, z1.b, z1.c,z1.a, z1.b, z1.c FROM z2,z1 LIMIT 1", {
51.65, -59.58, "belfries", 51.65, -59.58, "belfries"}},
})
-- EVIDENCE-OF: R-38023-18396 It is an error to use a "*" or "alias.*"
-- expression in any context other than a result expression list.
--
-- EVIDENCE-OF: R-44324-41166 It is also an error to use a "*" or
-- "alias.*" expression in a simple SELECT query that does not have a
-- FROM clause.
--
data = {
{"1.1", "SELECT a, b, c FROM z1 WHERE *", "Syntax error at line 1 near '*'"},
{"1.2", "SELECT a, b, c FROM z1 GROUP BY *", "Syntax error at line 1 near '*'"},
{"1.3", "SELECT 1 + * FROM z1", "Syntax error at line 1 near '*'"},
{"1.4", "SELECT * + 1 FROM z1", "Failed to expand '*' in SELECT statement without FROM clause"},
{"2.1", "SELECT *", "Failed to expand '*' in SELECT statement without FROM clause"},
{"2.2", "SELECT * WHERE 1", "Failed to expand '*' in SELECT statement without FROM clause"},
{"2.3", "SELECT * WHERE 0", "Failed to expand '*' in SELECT statement without FROM clause"},
{"2.4", "SELECT count(*), *", "Failed to expand '*' in SELECT statement without FROM clause"}
}
for _, val in ipairs(data) do
local tn = val[1]
local select = val[2]
local err = val[3]
test:do_catchsql_test(
"e_select-4.2."..tn,
select, {
1, err
})
end
-- EVIDENCE-OF: R-08669-22397 The number of columns in the rows returned
-- by a simple SELECT statement is equal to the number of expressions in
-- the result expression list after substitution of * and alias.*
-- expressions.
--
-- MUST_WORK_TEST prepared statement
if 0>0 then
-- Legacy from the original code. Must be replaced with analogue
-- functions from box.
local X = nil
local sql_finalize = nil
local sql_prepare_v2 = nil
local nCol = nil
local tn = nil
for _ in X(0, "X!foreach", [=[["tn select nCol","\n 1 \"SELECT a,b,c FROM z1\" 3\n 2 \"SELECT a,b,c FROM z1 NATURAL JOIN z3\" 3\n 3 \"SELECT z1.a,z1.b,z1.c FROM z1 NATURAL JOIN z3\" 3\n 4 \"SELECT z3.a,z3.b FROM z1 NATURAL JOIN z3\" 2\n 5 \"SELECT z1.a,z1.b,z1.c, z3.a,z3.b FROM z1 NATURAL JOIN z3\" 5\n 6 \"SELECT 1, 2, z1.a,z1.b,z1.c FROM z1\" 5\n 7 \"SELECT a, a,b,c, b, c FROM z1\" 6\n"]]=]) do
local stmt = sql_prepare_v2("db", select, -1, "DUMMY")
test:do_sql_column_count_test(
"e_select-4.3."..tn,
stmt, {
nCol
})
sql_finalize(stmt)
end
end
-- In lang_select.html, a non-aggregate query is defined as any simple SELECT
-- that has no GROUP BY clause and no aggregate expressions in the result
-- expression list. Other queries are aggregate queries. Test cases
-- e_select-4.4.* through e_select-4.12.*, inclusive, which test the part of
-- simple SELECT that is different for aggregate and non-aggregate queries
-- verify (in a way) that these definitions are consistent:
--
-- EVIDENCE-OF: R-20637-43463 A simple SELECT statement is an aggregate
-- query if it contains either a GROUP BY clause or one or more aggregate
-- functions in the result-set.
--
-- EVIDENCE-OF: R-23155-55597 Otherwise, if a simple SELECT contains no
-- aggregate functions or a GROUP BY clause, it is a non-aggregate query.
--
-- EVIDENCE-OF: R-44050-47362 If the SELECT statement is a non-aggregate
-- query, then each expression in the result expression list is evaluated
-- for each row in the dataset filtered by the WHERE clause.
--
test:do_select_tests(
"e_select-4.4",
{
{"1", "SELECT a, b FROM z1", {51.65, -59.58, -5, "", -2.2, -23.18, "", 67, -1.04, -32.3, 63, 0}},
{"2", "SELECT a IS NULL, b+1, a,b,c FROM z1",
{false, -58.58, 51.65, -59.58, "belfries", false, "", -5, "", "75",
false, -22.18, -2.2, -23.18, "suiters", true, 68, "", 67, "quartets", false, -31.3,
-1.04, -32.3, "aspen", false, 1, 63, 0, "-26"}},
{"3", "SELECT 32*32, CAST(d AS TEXT) || CAST(e AS TEXT) FROM z2", {1024, "", 1024, "36.06.0"}},
})
-- Test cases e_select-4.5.* and e_select-4.6.* together show that:
--
-- EVIDENCE-OF: R-51988-01124 The single row of result-set data created
-- by evaluating the aggregate and non-aggregate expressions in the
-- result-set forms the result of an aggregate query without a GROUP BY
-- clause.
--
-- EVIDENCE-OF: R-57629-25253 If the SELECT statement is an aggregate
-- query without a GROUP BY clause, then each aggregate expression in the
-- result-set is evaluated once across the entire dataset.
--
test:do_select_tests(
"e_select-4.5",
{
{"1", "SELECT count(a), max(a), count(b), max(b) FROM z1", {5, 63, 5, 67}},
{"2", "SELECT count(*), max(1)", {1, 1}},
{"3", "SELECT sum(b+1) FROM z1 NATURAL LEFT JOIN z3", {-43.06}},
{"4", "SELECT sum(b+2) FROM z1 NATURAL LEFT JOIN z3", {-38.06}},
{"5", "SELECT sum(CAST(b IS NOT NULL AS INTEGER)) FROM z1 NATURAL LEFT JOIN z3", {5}},
})
-- EVIDENCE-OF: R-26684-40576 Each non-aggregate expression in the
-- result-set is evaluated once for an arbitrarily selected row of the
-- dataset.
--
-- EVIDENCE-OF: R-27994-60376 The same arbitrarily selected row is used
-- for each non-aggregate expression.
--
-- Note: The results of many of the queries in this block of tests are
-- technically undefined, as the documentation does not specify which row
-- sql will arbitrarily select to use for the evaluation of the
-- non-aggregate expressions.
--
test:drop_all_tables()
test:do_execsql_test(
"e_select-4.6.0",
[[
CREATE TABLE a1(one INT PRIMARY KEY, two INT );
INSERT INTO a1 VALUES(1, 1);
INSERT INTO a1 VALUES(2, 3);
INSERT INTO a1 VALUES(3, 6);
INSERT INTO a1 VALUES(4, 10);
CREATE TABLE a2(one INT PRIMARY KEY, three INT );
INSERT INTO a2 VALUES(1, 1);
INSERT INTO a2 VALUES(3, 2);
INSERT INTO a2 VALUES(6, 3);
INSERT INTO a2 VALUES(10, 4);
]], {
-- <e_select-4.6.0>
-- </e_select-4.6.0>
})
test:do_select_tests(
"e_select-4.6",
{
{"1", "SELECT one, two, count(*) FROM a1", {4, 10, 4}},
{"2", "SELECT one, two, count(*) FROM a1 WHERE one<3", {2, 3, 2}},
{"3", "SELECT one, two, count(*) FROM a1 WHERE one>3", {4, 10, 1}},
{"4", "SELECT *, count(*) FROM a1 JOIN a2", {4, 10, 10, 4, 16}},
{"5", "SELECT *, sum(three) FROM a1 NATURAL JOIN a2", {3, 6, 2, 3}},
{"6", "SELECT *, sum(three) FROM a1 NATURAL JOIN a2", {3, 6, 2, 3}},
{"7", "SELECT group_concat(three, ''), a1.* FROM a1 NATURAL JOIN a2", {"12", 3, 6}},
})
-- EVIDENCE-OF: R-04486-07266 Or, if the dataset contains zero rows, then
-- each non-aggregate expression is evaluated against a row consisting
-- entirely of NULL values.
--
test:do_select_tests(
"e_select-4.7",
{
{"1", "SELECT one, two, count(*) FROM a1 WHERE false", {"", "", 0}},
{"2", "SELECT sum(two), * FROM a1, a2 WHERE three>5", {"", "", "", "", ""}},
{"3", "SELECT max(one) IS NULL, one IS NULL, two IS NULL FROM a1 WHERE two=7", {true, true, true}},
})
-- EVIDENCE-OF: R-64138-28774 An aggregate query without a GROUP BY
-- clause always returns exactly one row of data, even if there are zero
-- rows of input data.
--
-- MUST_WORK_TEST prepared statements
if 0>0 then
-- Legacy from the original code. Must be replaced with analogue
-- functions from box.
local X = nil
local sql_finalize = nil
local sql_prepare_v2 = nil
for _ in X(0, "X!foreach", [=[["tn select","\n 8.1 \"SELECT count(*) FROM a1\"\n 8.2 \"SELECT count(*) FROM a1 WHERE 0\"\n 8.3 \"SELECT count(*) FROM a1 WHERE 1\"\n 8.4 \"SELECT max(a1.one)+min(two), a1.one, two, * FROM a1, a2 WHERE 1\"\n 8.5 \"SELECT max(a1.one)+min(two), a1.one, two, * FROM a1, a2 WHERE 0\"\n"]]=]) do
-- Set $nRow to the number of rows returned by $select:
local stmt, nRow
stmt = sql_prepare_v2("db", select, -1, "DUMMY")
nRow = 0
while X(979, "X!cmd", [=[["expr","\"sql_ROW\" == [sql_step $::stmt]"]]=])
do
nRow = nRow + 1
end
local rc = sql_finalize(stmt)
-- Test that $nRow==1 and that statement execution was successful
-- (rc==sql_OK).
-- Legacy from the original code. Must be replaced with analogue
-- functions from box.
local X = nil
X(983, "X!cmd", [=[["do_test",["e_select-4.",["tn"]],[["list","list",["rc"],["nRow"]]],"sql_OK 1"]]=])