File: tkt-31338dca7e.test.lua

package info (click to toggle)
tarantool 2.6.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,396 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,176; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (261 lines) | stat: -rwxr-xr-x 7,841 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(11)

--!./tcltestrunner.lua
-- 2009 December 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 regression tests for sql library.
--
-- This file implements tests to verify that ticket [31338dca7e] has been
-- fixed.  Ticket [31338dca7e] demonstrates problems with the OR-clause
-- optimization in joins where the WHERE clause is of the form
--
--     (x AND y) OR z
--
-- And the x and y subterms from from different tables of the join.
--
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
test:do_test(
    "tkt-31338-1.1",
    function()
        return test:execsql [[
            CREATE TABLE t1(x  INT primary key);
            CREATE TABLE t2(y  INT primary key);
            INSERT INTO t1 VALUES(111);
            INSERT INTO t1 VALUES(222);
            INSERT INTO t2 VALUES(333);
            INSERT INTO t2 VALUES(444);
            SELECT * FROM t1, t2
             WHERE (x=111 AND y!=444) OR x=222
             ORDER BY x, y;
        ]]
    end, {
        -- <tkt-31338-1.1>
        111, 333, 222, 333, 222, 444
        -- </tkt-31338-1.1>
    })

test:do_test(
    "tkt-31338-1.2",
    function()
        return test:execsql [[
            CREATE INDEX t1x ON t1(x);
            SELECT * FROM t1, t2
             WHERE (x=111 AND y!=444) OR x=222
             ORDER BY x, y;
        ]]
    end, {
        -- <tkt-31338-1.2>
        111, 333, 222, 333, 222, 444
        -- </tkt-31338-1.2>
    })

test:do_test(
    "tkt-31338-2.1",
    function()
        return test:execsql [[
            CREATE TABLE t3(v  INT primary key,w INT );
            CREATE TABLE t4(x  INT primary key,y INT );
            CREATE TABLE t5(z  INT primary key);
            INSERT INTO t3 VALUES(111,222);
            INSERT INTO t3 VALUES(333,444);
            INSERT INTO t4 VALUES(222,333);
            INSERT INTO t4 VALUES(444,555);
            INSERT INTO t5 VALUES(888);
            INSERT INTO t5 VALUES(999);

            SELECT * FROM t3, t4, t5
             WHERE (v=111 AND x=w AND z!=999) OR (v=333 AND x=444)
             ORDER BY v, w, x, y, z;
        ]]
    end, {
        -- <tkt-31338-2.1>
        111, 222, 222, 333, 888, 333, 444, 444, 555, 888, 333, 444, 444, 555, 999
        -- </tkt-31338-2.1>
    })

test:do_test(
    "tkt-31338-2.2",
    function()
        return test:execsql [[
            CREATE INDEX t3v ON t3(v);
            CREATE INDEX t4x ON t4(x);
             SELECT * FROM t3, t4, t5
              WHERE (v=111 AND x=w AND z!=999) OR (v=333 AND x=444)
              ORDER BY v, w, x, y, z;
        ]]
    end, {
        -- <tkt-31338-2.2>
        111, 222, 222, 333, 888, 333, 444, 444, 555, 888, 333, 444, 444, 555, 999
        -- </tkt-31338-2.2>
    })


test:execsql([[
    DROP TABLE t1;
    DROP TABLE t2;
    DROP TABLE t3;
    DROP TABLE t4;
    DROP TABLE t5;
]])
-- Ticket [2c2de252666662f5459904fc33a9f2956cbff23c]
--
test:do_test(
    "tkt-31338-3.1",
    function()
        -- foreach x [db eval {SELECT name FROM sql_master WHERE type='table'}] {
        --    db eval "DROP TABLE $x"
        -- }
        return test:execsql [[
            CREATE TABLE t1(a  INT primary key,b INT ,c INT ,d INT );
            CREATE TABLE t2(e  INT primary key,f INT );
            INSERT INTO t1 VALUES(1,2,3,4);
            INSERT INTO t2 VALUES(10,-8);
            CREATE INDEX t1a ON t1(a);
            CREATE INDEX t1b ON t1(b);
            CREATE TABLE t3(g  INT primary key);
            INSERT INTO t3 VALUES(4);
            CREATE TABLE t4(h  INT primary key);
            INSERT INTO t4 VALUES(5);

            SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
             WHERE (a=1 AND h=4)
                 OR (b IN (
                       SELECT x FROM (SELECT e+f AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                       GROUP BY e
                    ));
        ]]
    end, {
        -- <tkt-31338-3.1>
        4, 1, 2, 3, 4, ""
        -- </tkt-31338-3.1>
    })

test:do_test(
    "tkt-31338-3.2",
    function()
        return test:execsql [[
            SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
             WHERE (a=1 AND h=4)
                 OR (b=2 AND b NOT IN (
                       SELECT x+1 FROM (SELECT e+f AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                       GROUP BY e
                    ));
        ]]
    end, {
        -- <tkt-31338-3.2>
        4, 1, 2, 3, 4, ""
        -- </tkt-31338-3.2>
    })

test:do_test(
    "tkt-31338-3.3",
    function()
        return test:execsql [[
            SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
             WHERE (+a=1 AND h=4)
                 OR (b IN (
                       SELECT x FROM (SELECT e+f AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                       GROUP BY e
                    ));
        ]]
    end, {
        -- <tkt-31338-3.3>
        4, 1, 2, 3, 4, ""
        -- </tkt-31338-3.3>
    })

test:do_test(
    "tkt-31338-3.4",
    function()
        return test:execsql [[
            SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
             WHERE (a=1 AND h=4)
                 OR (+b IN (
                       SELECT x FROM (SELECT e+f AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                       GROUP BY e
                    ));
        ]]
    end, {
        -- <tkt-31338-3.4>
        4, 1, 2, 3, 4, ""
        -- </tkt-31338-3.4>
    })

-- MUST_WORK_TEST
if (1 > 0)
 then
    test:do_test(
        "tkt-31338-3.5",
        function()
            return test:execsql [[
                CREATE TABLE t5(a  INT primary key,b INT ,c INT ,d INT ,e INT ,f INT );
                CREATE TABLE t6(g  INT primary key,h INT );
                CREATE TRIGGER t6r AFTER INSERT ON t6 FOR EACH ROW BEGIN
                  INSERT INTO t5    
                    SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
                     WHERE (a=1 AND h=4)
                        OR (b IN (
                           SELECT x FROM (SELECT e+f AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                           GROUP BY e
                        ));
                END;
                INSERT INTO t6 VALUES(88,99);
                SELECT * FROM t5;
            ]]
        end, {
            -- <tkt-31338-3.5>
            4, 1, 2, 3, 4, ""
            -- </tkt-31338-3.5>
        })

end
test:do_test(
    "tkt-31338-3.6",
    function()
        return test:execsql [[
            INSERT INTO t1 VALUES(2,4,3,4);
            INSERT INTO t1 VALUES(99,101,3,4);
            INSERT INTO t1 VALUES(98,97,3,4);
            SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
             WHERE (a=1 AND h=4)
                 OR (b IN (
                       SELECT x+a FROM (SELECT e+f AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                       GROUP BY e
                    ));
        ]]
    end, {
        -- <tkt-31338-3.6>
        4, 2, 4, 3, 4, "", 4, 99, 101, 3, 4, ""
        -- </tkt-31338-3.6>
    })

test:do_test(
    "tkt-31338-3.7",
    function()
        return test:execsql [[
            SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
             WHERE (a=1 AND h=4)
                 OR (b IN (
                       SELECT x FROM (SELECT e+f+a AS x, e FROM t2 ORDER BY 1 LIMIT 2)
                       GROUP BY e
                    ));
        ]]
    end, {
        -- <tkt-31338-3.7>
        4, 2, 4, 3, 4, "", 4, 99, 101, 3, 4, ""
        -- </tkt-31338-3.7>
    })

test:finish_test()