summaryrefslogtreecommitdiffstats
path: root/test/selectB.test
blob: 05ec9c6bdb0f5fc96b13444b612354f80086a3eb (plain)
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
# 2008 June 24
#
# 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 SQLite library. 
#
# $Id: selectB.test,v 1.10 2009/04/02 16:59:47 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

ifcapable !compound {
  finish_test
  return
}

proc test_transform {testname sql1 sql2 results} {
  set ::vdbe1 [list]
  set ::vdbe2 [list]
  db eval "explain $sql1" { lappend ::vdbe1 $opcode }
  db eval "explain $sql2" { lappend ::vdbe2 $opcode }

  do_test $testname.transform {
    set ::vdbe1
  } $::vdbe2

  set ::sql1 $sql1
  do_test $testname.sql1 {
    execsql $::sql1
  } $results

  set ::sql2 $sql2
  do_test $testname.sql2 {
    execsql $::sql2
  } $results
}

do_test selectB-1.1 {
  execsql {
    CREATE TABLE t1(a, b, c);
    CREATE TABLE t2(d, e, f);

    INSERT INTO t1 VALUES( 2,  4,  6);
    INSERT INTO t1 VALUES( 8, 10, 12);
    INSERT INTO t1 VALUES(14, 16, 18);

    INSERT INTO t2 VALUES(3,   6,  9);
    INSERT INTO t2 VALUES(12, 15, 18);
    INSERT INTO t2 VALUES(21, 24, 27);
  }
} {}

for {set ii 1} {$ii <= 2} {incr ii} {

  if {$ii == 2} {
    do_test selectB-2.1 {
      execsql {
        CREATE INDEX i1 ON t1(a);
        CREATE INDEX i2 ON t2(d);
      }
    } {}
  }

  test_transform selectB-$ii.2 {
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2
  } {2 8 14 3 12 21}
  
  test_transform selectB-$ii.3 {
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1
  } {2 3 8 12 14 21}
  
  test_transform selectB-$ii.4 {
    SELECT * FROM 
      (SELECT a FROM t1 UNION ALL SELECT d FROM t2) 
    WHERE a>10 ORDER BY 1
  } {
    SELECT a FROM t1 WHERE a>10 UNION ALL SELECT d FROM t2 WHERE d>10 ORDER BY 1
  } {12 14 21}
  
  test_transform selectB-$ii.5 {
    SELECT * FROM 
      (SELECT a FROM t1 UNION ALL SELECT d FROM t2) 
    WHERE a>10 ORDER BY a
  } {
    SELECT a FROM t1 WHERE a>10 
      UNION ALL 
    SELECT d FROM t2 WHERE d>10 
    ORDER BY a
  } {12 14 21}
  
  test_transform selectB-$ii.6 {
    SELECT * FROM 
      (SELECT a FROM t1 UNION ALL SELECT d FROM t2 WHERE d > 12) 
    WHERE a>10 ORDER BY a
  } {
    SELECT a FROM t1 WHERE a>10
      UNION ALL 
    SELECT d FROM t2 WHERE d>12 AND d>10
    ORDER BY a
  } {14 21}
  
  test_transform selectB-$ii.7 {
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1 
    LIMIT 2
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 2
  } {2 3}
  
  test_transform selectB-$ii.8 {
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1 
    LIMIT 2 OFFSET 3
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 2 OFFSET 3
  } {12 14}

  test_transform selectB-$ii.9 {
    SELECT * FROM (
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
    ) 
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
  } {2 8 14 3 12 21 6 12 18}
  
  test_transform selectB-$ii.10 {
    SELECT * FROM (
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
    ) ORDER BY 1
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
    ORDER BY 1
  } {2 3 6 8 12 12 14 18 21}
  
  test_transform selectB-$ii.11 {
    SELECT * FROM (
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
    ) WHERE a>=10 ORDER BY 1 LIMIT 3
  } {
    SELECT a FROM t1 WHERE a>=10 UNION ALL SELECT d FROM t2 WHERE d>=10
    UNION ALL SELECT c FROM t1 WHERE c>=10
    ORDER BY 1 LIMIT 3
  } {12 12 14}

  test_transform selectB-$ii.12 {
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 2)
  } {
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 2
  } {2 8}

  # An ORDER BY in a compound subqueries defeats flattening.  Ticket #3773
  # test_transform selectB-$ii.13 {
  #   SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a ASC)
  # } {
  #   SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 ASC
  # } {2 3 8 12 14 21}
  # 
  # test_transform selectB-$ii.14 {
  #  SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a DESC)
  # } {
  #  SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 DESC
  # } {21 14 12 8 3 2}
  #
  # test_transform selectB-$ii.14 {
  #   SELECT * FROM (
  #     SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a DESC
  #   ) LIMIT 2 OFFSET 2
  # } {
  #   SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 DESC
  #    LIMIT 2 OFFSET 2
  # } {12 8}
  #
  # test_transform selectB-$ii.15 {
  #   SELECT * FROM (
  #     SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY a ASC, e DESC
  #  )
  # } {
  #   SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY a ASC, e DESC
  # } {2 4 3 6 8 10 12 15 14 16 21 24}
}

do_test selectB-3.0 {
  execsql {
    DROP INDEX i1;
    DROP INDEX i2;
  }
} {}

for {set ii 3} {$ii <= 6} {incr ii} {

  switch $ii {
    4 {
      optimization_control db query-flattener off
    }
    5 {
      optimization_control db query-flattener on
      do_test selectB-5.0 {
        execsql {
          CREATE INDEX i1 ON t1(a);
          CREATE INDEX i2 ON t1(b);
          CREATE INDEX i3 ON t1(c);
          CREATE INDEX i4 ON t2(d);
          CREATE INDEX i5 ON t2(e);
          CREATE INDEX i6 ON t2(f);
        }
      } {}
    }
    6 {
      optimization_control db query-flattener off
    }
  }

  do_test selectB-$ii.1 {
    execsql {
      SELECT DISTINCT * FROM 
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) 
      ORDER BY 1;
    }
  } {6 12 15 18 24}
  
  do_test selectB-$ii.2 {
    execsql {
      SELECT c, count(*) FROM 
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) 
      GROUP BY c ORDER BY 1;
    }
  } {6 2 12 1 15 1 18 1 24 1}
  do_test selectB-$ii.3 {
    execsql {
      SELECT c, count(*) FROM 
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) 
      GROUP BY c HAVING count(*)>1;
    }
  } {6 2}
  do_test selectB-$ii.4 {
    execsql {
      SELECT t4.c, t3.a FROM 
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) AS t4, t1 AS t3
      WHERE t3.a=14
      ORDER BY 1
    }
  } {6 14 6 14 12 14 15 14 18 14 24 14}
  
  do_test selectB-$ii.5 {
    execsql {
      SELECT d FROM t2 
      EXCEPT 
      SELECT a FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
    }
  } {}
  do_test selectB-$ii.6 {
    execsql {
      SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
      EXCEPT 
      SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
    }
  } {}
  do_test selectB-$ii.7 {
    execsql {
      SELECT c FROM t1
      EXCEPT 
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
    }
  } {12}
  do_test selectB-$ii.8 {
    execsql {
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      EXCEPT 
      SELECT c FROM t1
    }
  } {9 15 24 27}
  do_test selectB-$ii.9 {
    execsql {
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      EXCEPT 
      SELECT c FROM t1
      ORDER BY c DESC
    }
  } {27 24 15 9}
  
  do_test selectB-$ii.10 {
    execsql {
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      UNION 
      SELECT c FROM t1
      ORDER BY c DESC
    }
  } {27 24 18 15 12 9 6}
  do_test selectB-$ii.11 {
    execsql {
      SELECT c FROM t1
      UNION 
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      ORDER BY c
    }
  } {6 9 12 15 18 24 27}
  do_test selectB-$ii.12 {
    execsql {
      SELECT c FROM t1 UNION SELECT e FROM t2 UNION ALL SELECT f FROM t2
      ORDER BY c
    }
  } {6 9 12 15 18 18 24 27}
  do_test selectB-$ii.13 {
    execsql {
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      UNION 
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      ORDER BY 1
    }
  } {6 9 15 18 24 27}
  
  do_test selectB-$ii.14 {
    execsql {
      SELECT c FROM t1
      INTERSECT 
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      ORDER BY 1
    }
  } {6 18}
  do_test selectB-$ii.15 {
    execsql {
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      INTERSECT 
      SELECT c FROM t1
      ORDER BY 1
    }
  } {6 18}
  do_test selectB-$ii.16 {
    execsql {
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      INTERSECT 
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
      ORDER BY 1
    }
  } {6 9 15 18 24 27}

  do_test selectB-$ii.17 {
    execsql {
      SELECT * FROM (
        SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 4
      ) LIMIT 2
    }
  } {2 8}

  do_test selectB-$ii.18 {
    execsql {
      SELECT * FROM (
        SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 4 OFFSET 2
      ) LIMIT 2
    }
  } {14 3}

  do_test selectB-$ii.19 {
    execsql {
      SELECT * FROM (
        SELECT DISTINCT (a/10) FROM t1 UNION ALL SELECT DISTINCT(d%2) FROM t2
      )
    }
  } {0 1 1 0}

  do_test selectB-$ii.20 {
    execsql {
      SELECT DISTINCT * FROM (
        SELECT DISTINCT (a/10) FROM t1 UNION ALL SELECT DISTINCT(d%2) FROM t2
      )
    }
  } {0 1}

  do_test selectB-$ii.21 {
    execsql {
      SELECT * FROM (SELECT * FROM t1 UNION ALL SELECT * FROM t2) ORDER BY a+b
    }
  } {2 4 6 3 6 9 8 10 12 12 15 18 14 16 18 21 24 27}

  do_test selectB-$ii.22 {
    execsql {
      SELECT * FROM (SELECT 345 UNION ALL SELECT d FROM t2) ORDER BY 1;
    }
  } {3 12 21 345}

  do_test selectB-$ii.23 {
    execsql {
      SELECT x, y FROM (
        SELECT a AS x, b AS y FROM t1
        UNION ALL
        SELECT a*10 + 0.1, f*10 + 0.1 FROM t1 JOIN t2 ON (c=d)
        UNION ALL
        SELECT a*100, b*100 FROM t1
      ) ORDER BY 1;
    }
  } {2 4 8 10 14 16 80.1 180.1 200 400 800 1000 1400 1600}

  do_test selectB-$ii.24 {
    execsql {
      SELECT x, y FROM (
        SELECT a AS x, b AS y FROM t1
        UNION ALL
        SELECT a*10 + 0.1, f*10 + 0.1 FROM t1 LEFT JOIN t2 ON (c=d)
        UNION ALL
        SELECT a*100, b*100 FROM t1
      ) ORDER BY 1;
    }
  } {2 4 8 10 14 16 20.1 {} 80.1 180.1 140.1 {} 200 400 800 1000 1400 1600}

  do_test selectB-$ii.25 {
    execsql {
      SELECT x+y FROM (
        SELECT a AS x, b AS y FROM t1
        UNION ALL
        SELECT a*10 + 0.1, f*10 + 0.1 FROM t1 LEFT JOIN t2 ON (c=d)
        UNION ALL
        SELECT a*100, b*100 FROM t1
      ) WHERE y+x NOT NULL ORDER BY 1;
    }
  } {6 18 30 260.2 600 1800 3000}
}

finish_test