File: orderby6.test.lua

package info (click to toggle)
tarantool 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 85,364 kB
  • sloc: ansic: 513,760; cpp: 69,489; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,173; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (332 lines) | stat: -rwxr-xr-x 11,914 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
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
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(34)

--!./tcltestrunner.lua
-- 2014-03-21
--
-- 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.  The
-- focus of this file is testing that the block-sort optimization.
--
-- NOTE: tests with ORDER BY + LIMIT + (ASC + DESC, i.e
-- different sorting orders) are commented in this file as they
-- are failing because we don't have multi-directional iterators.
-- Details and full list of commented tests can be found here:
-- https://github.com/tarantool/tarantool/issues/3309
-- Please, uncomment the tests when multi-directional iterators
-- are introduced.
--
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
testprefix = "orderby6"
-- Run all tests twice.  Once with a normal table and a second time
-- with a WITHOUT ROWID table
--
-- Tarantool: no rowid support. So run the case once
-- for _ in X(0, "X!foreach", [=[["tn rowidclause","1 {} 2 {WITHOUT ROWID}"]]=]) do
    -- Construct a table with 1000 rows and a split primary key
    --
    -- MUST_WORK_TEST
    --reset_db
    test:execsql "DROP TABLE IF EXISTS t1; DROP TABLE IF EXISTS t2;"
    test:do_test(
        "1.1",
        function()
            test:execsql "CREATE TABLE t1(a INT ,b INT ,c INT ,PRIMARY KEY(b,c));"
            return test:execsql [[
                WITH RECURSIVE
                 cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<1000)
                INSERT INTO t1 SELECT x, x%40, x/40 FROM cnt;
            ]]
        end, {
            
        })

    -- Run various ORDER BY queries that can benefit from block-sort.
    -- Compare the output to the same output using a full-sort enforced
    -- by adding + to each term of the ORDER BY clause.
    --
    test:do_execsql_test(
        "1.2", 
        [[
            SELECT b,a,c FROM t1 ORDER BY b,a,c;
        ]], 
        test:execsql "SELECT b,a,c FROM t1 ORDER BY +b,+a,+c"
        )

    test:do_execsql_test(
        "1.3",
        [[
            SELECT b,a,c FROM t1 ORDER BY b,c DESC,a;
        ]], 
        test:execsql "SELECT b,a,c FROM t1 ORDER BY +b,+c DESC,+a"
        )

    test:do_execsql_test(
        "1.4",
        [[
            SELECT b,a,c FROM t1 ORDER BY b DESC,c,a;
        ]], 
        test:execsql "SELECT b,a,c FROM t1 ORDER BY +b DESC,+c,+a"
        )

    test:do_execsql_test(
        "1.5",
        [[
            SELECT b,a,c FROM t1 ORDER BY b DESC,a,c;
        ]], 
        test:execsql "SELECT b,a,c FROM t1 ORDER BY +b DESC,+a,+c"
        )

    -- LIMIT and OFFSET clauses on block-sort queries.
    --
    test:do_execsql_test(
        "1.11",
        [[
            SELECT a FROM t1 ORDER BY b, a LIMIT 10 OFFSET 20;
        ]], {
            840, 880, 920, 960, 1000, 1, 41, 81, 121, 161
        })

    test:do_execsql_test(
        "1.11x",
        [[
            SELECT a FROM t1 ORDER BY +b, a LIMIT 10 OFFSET 20;
        ]], {
            840, 880, 920, 960, 1000, 1, 41, 81, 121, 161
        })
   -- Tests below are disabled due to incapability of sorting two or more
   -- key columns with different orders (DESC/ASC). As soon as Tarantool
   -- supports this feature, these tests will be uncommented.
   -- #3016
   -- test:do_execsql_test(
   --     "1.12",
   --     [[
   --         SELECT a FROM t1 ORDER BY b DESC, a LIMIT 10 OFFSET 20;
   --     ]], {
   --         839, 879, 919, 959, 999, 38, 78, 118, 158, 198
   --     })

   -- test:do_execsql_test(
   --     "1.12",
   --     [[
   --         SELECT a FROM t1 ORDER BY +b DESC, a LIMIT 10 OFFSET 20;
   --     ]], {
   --         839, 879, 919, 959, 999, 38, 78, 118, 158, 198
   --     })

   -- test:do_execsql_test(
   --     "1.13",
   --     [[
   --         SELECT a FROM t1 ORDER BY b, a DESC LIMIT 10 OFFSET 45;
   --     ]], {
   --         161, 121, 81, 41, 1, 962, 922, 882, 842, 802
   --     })

   -- test:do_execsql_test(
   --     "1.13x",
   --     [[
   --         SELECT a FROM t1 ORDER BY +b, a DESC LIMIT 10 OFFSET 45;
   --     ]], {
   --         161, 121, 81, 41, 1, 962, 922, 882, 842, 802
   --     })

   -- test:do_execsql_test(
   --     "1.14",
   --     [[
   --         SELECT a FROM t1 ORDER BY b DESC, a LIMIT 10 OFFSET 45;
   --     ]], {
   --         838, 878, 918, 958, 998, 37, 77, 117, 157, 197
   --     })

   -- test:do_execsql_test(
   --     "1.14x",
   --     [[
   --         SELECT a FROM t1 ORDER BY +b DESC, a LIMIT 10 OFFSET 45;
   --     ]], {
   --         838, 878, 918, 958, 998, 37, 77, 117, 157, 197
   --     })

    -- Many test cases where the LIMIT+OFFSET window is in various
    -- alignments with block-sort boundaries.
    --
    local data = {
        {limit=0, offset=4, orderby="+b,+a"},
        {limit=0, offset=5, orderby="+b,+a"},
        {limit=0, offset=6, orderby="+b,+a"},
        {limit=0, offset=9, orderby="+b,+a"},
        {limit=0, offset=0, orderby="+b,+a"},
        {limit=0, offset=1, orderby="+b,+a"},
        {limit=7, offset=4, orderby="+b,+a"},
        {limit=7, offset=9, orderby="+b,+a"},
        -- Tests with different sorting orders are commented
        -- because of the reason described in NOTE at the
        -- beginning of the file.
        -- <begin>
        --{limit=0, offset=4, orderby="+b DESC,+a"},
        --{limit=0, offset=5, orderby="+b DESC,+a"},
        --{limit=0, offset=6, orderby="+b DESC,+a"},
        --{limit=0, offset=9, orderby="+b DESC,+a"},
        --{limit=0, offset=0, orderby="+b DESC,+a"},
        --{limit=0, offset=1, orderby="+b DESC,+a"},
        --{limit=7, offset=4, orderby="+b DESC,+a"},
        --{limit=7, offset=9, orderby="+b DESC,+a"},
        --{limit=0, offset=4, orderby="+b,+a DESC"},
        --{limit=0, offset=5, orderby="+b,+a DESC"},
        --{limit=0, offset=6, orderby="+b,+a DESC"},
        --{limit=0, offset=9, orderby="+b,+a DESC"},
        --{limit=0, offset=0, orderby="+b,+a DESC"},
        --{limit=0, offset=1, orderby="+b,+a DESC"},
        --{limit=7, offset=4, orderby="+b,+a DESC"},
        --{limit=7, offset=9, orderby="+b,+a DESC"},
        -- <end>
        {limit=0, offset=4, orderby="+b DESC,+a DESC"},
        {limit=0, offset=5, orderby="+b DESC,+a DESC"},
        {limit=0, offset=6, orderby="+b DESC,+a DESC"},
        {limit=0, offset=9, orderby="+b DESC,+a DESC"},
        {limit=0, offset=0, orderby="+b DESC,+a DESC"},
        {limit=0, offset=1, orderby="+b DESC,+a DESC"},
        {limit=7, offset=4, orderby="+b DESC,+a DESC"},
        {limit=7, offset=9, orderby="+b DESC,+a DESC"}}
    for i, v in ipairs(data) do
        local sql1 = "SELECT a FROM t1 ORDER BY "..v.orderby.." LIMIT "..v.limit.." OFFSET "..v.offset..";"
        local sql2 = "SELECT a FROM t1 ORDER BY "..string.gsub(v.orderby, "+", "").." LIMIT "..v.limit.." OFFSET "..v.offset..";"
        test:do_execsql_test("1.21."..i, sql1, test:execsql(sql2))
    end

--     for _ in X(0, "X!foreach", [=[["tx limit offset orderby","\n     1  10 24 {+b,+a}\n     2  10 25 {+b,+a}\n     3  10 26 {+b,+a}\n     4  10 39 {+b,+a}\n     5  10 40 {+b,+a}\n     6  10 41 {+b,+a}\n     7  27 24 {+b,+a}\n     8  27 49 {+b,+a}\n     11 10 24 {+b DESC,+a}\n     12 10 25 {+b DESC,+a}\n     13 10 26 {+b DESC,+a}\n     14 10 39 {+b DESC,+a}\n     15 10 40 {+b DESC,+a}\n     16 10 41 {+b DESC,+a}\n     17 27 24 {+b DESC,+a}\n     18 27 49 {+b DESC,+a}\n     21 10 24 {+b,+a DESC}\n     22 10 25 {+b,+a DESC}\n     23 10 26 {+b,+a DESC}\n     24 10 39 {+b,+a DESC}\n     25 10 40 {+b,+a DESC}\n     26 10 41 {+b,+a DESC}\n     27 27 24 {+b,+a DESC}\n     28 27 49 {+b,+a DESC}\n     31 10 24 {+b DESC,+a DESC}\n     32 10 25 {+b DESC,+a DESC}\n     33 10 26 {+b DESC,+a DESC}\n     34 10 39 {+b DESC,+a DESC}\n     35 10 40 {+b DESC,+a DESC}\n     36 10 41 {+b DESC,+a DESC}\n     37 27 24 {+b DESC,+a DESC}\n     38 27 49 {+b DESC,+a DESC}\n  "]]=]) do
--         sql1 = string.format("SELECT a FROM t1 ORDER BY %s LIMIT %s OFFSET %s;", orderby, limit, offset)
--         sql2 = X(96, "X!cmd", [=[["string","map","+ {}",["sql1"]]]=])
--         -- puts $sql2\n$sql1\n[db eval $sql2]
--         test:do_test(
-- string.format("%s.21.%s", tn, tx),
--             function()
--                 return test:execsql sql2
--             end, {
--                 test:execsql sql1
--             })

--     end
    -------------------------------------------------------------------------
    -- A second test table, t2, has many columns open to sorting.
    test:do_test(
        "1.31",
        function()
            test:execsql "CREATE TABLE t2(a INT ,b INT ,c INT ,d INT ,e INT ,f INT ,PRIMARY KEY(b,c,d,e,f));"
            return test:execsql [[
                WITH RECURSIVE
                 cnt(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM cnt WHERE x<242)
                INSERT INTO t2 SELECT x,  x%3, (x/3)%3, (x/9)%3, (x/27)%3, (x/81)%3
                                FROM cnt;
            ]]
        end, {
            
        })

    test:do_execsql_test(
        "1.32",
        [[
            SELECT a FROM t2 ORDER BY b,c,d,e,f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f;"
        )

    test:do_execsql_test(
        "1.33",
        [[
            SELECT a FROM t2 ORDER BY b,c,d,e,+f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f;"
        )

    test:do_execsql_test(
        "1.34",
        [[
            SELECT a FROM t2 ORDER BY b,c,d,+e,+f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f;"
        )

    test:do_execsql_test(
        "1.35",
        [[
            SELECT a FROM t2 ORDER BY b,c,+d,+e,+f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f;"
        )

    test:do_execsql_test(
        "1.36",
        [[
            SELECT a FROM t2 ORDER BY b,+c,+d,+e,+f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f;"
        )

    test:do_execsql_test(
        "1.37",
        [[
            SELECT a FROM t2 ORDER BY b,c,d,e,f DESC;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f DESC;"
        )

    test:do_execsql_test(
        "1.38",
        [[
            SELECT a FROM t2 ORDER BY b,c,d,e DESC,f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e DESC,+f;"
        )

    test:do_execsql_test(
        "1.39",
        [[
            SELECT a FROM t2 ORDER BY b,c,d DESC,e,f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d DESC,+e,+f;"
        )

    test:do_execsql_test(
        "1.40",
        [[
            SELECT a FROM t2 ORDER BY b,c DESC,d,e,f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b,+c DESC,+d,+e,+f;"
        )

    test:do_execsql_test(
        "1.41",
        [[
            SELECT a FROM t2 ORDER BY b DESC,c,d,e,f;
        ]], 
        test:execsql "SELECT a FROM t2 ORDER BY +b DESC,+c,+d,+e,+f;"
        )
    -- Tests are commented because of the reason described in
    -- NOTE at the beginning of the file.
    -- <begin>
    --test:do_execsql_test(
    --    "1.42",
    --    [[
    --        SELECT a FROM t2 ORDER BY b DESC,c DESC,d,e,f LIMIT 31;
    --    ]],
    --    test:execsql "SELECT a FROM t2 ORDER BY +b DESC,+c DESC,+d,+e,+f LIMIT 31"
    --    )
    --
    --test:do_execsql_test(
    --    "1.43",
    --    [[
    --        SELECT a FROM t2 ORDER BY b,c,d,e,f DESC LIMIT 8 OFFSET 7;
    --    ]],
    --    test:execsql "SELECT a FROM t2 ORDER BY +b,+c,+d,+e,+f DESC LIMIT 8 OFFSET 7"
    --    )
    -- <end>
test:finish_test()