os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/selectB.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2008 June 24
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library. 
sl@0
    12
#
sl@0
    13
# $Id: selectB.test,v 1.9 2008/08/04 03:51:24 danielk1977 Exp $
sl@0
    14
sl@0
    15
set testdir [file dirname $argv0]
sl@0
    16
source $testdir/tester.tcl
sl@0
    17
sl@0
    18
ifcapable !compound {
sl@0
    19
  finish_test
sl@0
    20
  return
sl@0
    21
}
sl@0
    22
sl@0
    23
proc test_transform {testname sql1 sql2 results} {
sl@0
    24
  set ::vdbe1 [list]
sl@0
    25
  set ::vdbe2 [list]
sl@0
    26
  db eval "explain $sql1" { lappend ::vdbe1 $opcode }
sl@0
    27
  db eval "explain $sql2" { lappend ::vdbe2 $opcode }
sl@0
    28
sl@0
    29
  do_test $testname.transform {
sl@0
    30
    set ::vdbe1
sl@0
    31
  } $::vdbe2
sl@0
    32
sl@0
    33
  set ::sql1 $sql1
sl@0
    34
  do_test $testname.sql1 {
sl@0
    35
    execsql $::sql1
sl@0
    36
  } $results
sl@0
    37
sl@0
    38
  set ::sql2 $sql2
sl@0
    39
  do_test $testname.sql2 {
sl@0
    40
    execsql $::sql2
sl@0
    41
  } $results
sl@0
    42
}
sl@0
    43
sl@0
    44
do_test selectB-1.1 {
sl@0
    45
  execsql {
sl@0
    46
    CREATE TABLE t1(a, b, c);
sl@0
    47
    CREATE TABLE t2(d, e, f);
sl@0
    48
sl@0
    49
    INSERT INTO t1 VALUES( 2,  4,  6);
sl@0
    50
    INSERT INTO t1 VALUES( 8, 10, 12);
sl@0
    51
    INSERT INTO t1 VALUES(14, 16, 18);
sl@0
    52
sl@0
    53
    INSERT INTO t2 VALUES(3,   6,  9);
sl@0
    54
    INSERT INTO t2 VALUES(12, 15, 18);
sl@0
    55
    INSERT INTO t2 VALUES(21, 24, 27);
sl@0
    56
  }
sl@0
    57
} {}
sl@0
    58
sl@0
    59
for {set ii 1} {$ii <= 2} {incr ii} {
sl@0
    60
sl@0
    61
  if {$ii == 2} {
sl@0
    62
    do_test selectB-2.1 {
sl@0
    63
      execsql {
sl@0
    64
        CREATE INDEX i1 ON t1(a);
sl@0
    65
        CREATE INDEX i2 ON t2(d);
sl@0
    66
      }
sl@0
    67
    } {}
sl@0
    68
  }
sl@0
    69
sl@0
    70
  test_transform selectB-$ii.2 {
sl@0
    71
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
sl@0
    72
  } {
sl@0
    73
    SELECT a FROM t1 UNION ALL SELECT d FROM t2
sl@0
    74
  } {2 8 14 3 12 21}
sl@0
    75
  
sl@0
    76
  test_transform selectB-$ii.3 {
sl@0
    77
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1
sl@0
    78
  } {
sl@0
    79
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1
sl@0
    80
  } {2 3 8 12 14 21}
sl@0
    81
  
sl@0
    82
  test_transform selectB-$ii.4 {
sl@0
    83
    SELECT * FROM 
sl@0
    84
      (SELECT a FROM t1 UNION ALL SELECT d FROM t2) 
sl@0
    85
    WHERE a>10 ORDER BY 1
sl@0
    86
  } {
sl@0
    87
    SELECT a FROM t1 WHERE a>10 UNION ALL SELECT d FROM t2 WHERE d>10 ORDER BY 1
sl@0
    88
  } {12 14 21}
sl@0
    89
  
sl@0
    90
  test_transform selectB-$ii.5 {
sl@0
    91
    SELECT * FROM 
sl@0
    92
      (SELECT a FROM t1 UNION ALL SELECT d FROM t2) 
sl@0
    93
    WHERE a>10 ORDER BY a
sl@0
    94
  } {
sl@0
    95
    SELECT a FROM t1 WHERE a>10 
sl@0
    96
      UNION ALL 
sl@0
    97
    SELECT d FROM t2 WHERE d>10 
sl@0
    98
    ORDER BY a
sl@0
    99
  } {12 14 21}
sl@0
   100
  
sl@0
   101
  test_transform selectB-$ii.6 {
sl@0
   102
    SELECT * FROM 
sl@0
   103
      (SELECT a FROM t1 UNION ALL SELECT d FROM t2 WHERE d > 12) 
sl@0
   104
    WHERE a>10 ORDER BY a
sl@0
   105
  } {
sl@0
   106
    SELECT a FROM t1 WHERE a>10
sl@0
   107
      UNION ALL 
sl@0
   108
    SELECT d FROM t2 WHERE d>12 AND d>10
sl@0
   109
    ORDER BY a
sl@0
   110
  } {14 21}
sl@0
   111
  
sl@0
   112
  test_transform selectB-$ii.7 {
sl@0
   113
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1 
sl@0
   114
    LIMIT 2
sl@0
   115
  } {
sl@0
   116
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 2
sl@0
   117
  } {2 3}
sl@0
   118
  
sl@0
   119
  test_transform selectB-$ii.8 {
sl@0
   120
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1 
sl@0
   121
    LIMIT 2 OFFSET 3
sl@0
   122
  } {
sl@0
   123
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 2 OFFSET 3
sl@0
   124
  } {12 14}
sl@0
   125
  
sl@0
   126
  test_transform selectB-$ii.9 {
sl@0
   127
    SELECT * FROM (
sl@0
   128
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
sl@0
   129
    ) 
sl@0
   130
  } {
sl@0
   131
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
sl@0
   132
  } {2 8 14 3 12 21 6 12 18}
sl@0
   133
  
sl@0
   134
  test_transform selectB-$ii.10 {
sl@0
   135
    SELECT * FROM (
sl@0
   136
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
sl@0
   137
    ) ORDER BY 1
sl@0
   138
  } {
sl@0
   139
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
sl@0
   140
    ORDER BY 1
sl@0
   141
  } {2 3 6 8 12 12 14 18 21}
sl@0
   142
  
sl@0
   143
  test_transform selectB-$ii.11 {
sl@0
   144
    SELECT * FROM (
sl@0
   145
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
sl@0
   146
    ) WHERE a>=10 ORDER BY 1 LIMIT 3
sl@0
   147
  } {
sl@0
   148
    SELECT a FROM t1 WHERE a>=10 UNION ALL SELECT d FROM t2 WHERE d>=10
sl@0
   149
    UNION ALL SELECT c FROM t1 WHERE c>=10
sl@0
   150
    ORDER BY 1 LIMIT 3
sl@0
   151
  } {12 12 14}
sl@0
   152
sl@0
   153
  test_transform selectB-$ii.12 {
sl@0
   154
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 2)
sl@0
   155
  } {
sl@0
   156
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 2
sl@0
   157
  } {2 8}
sl@0
   158
sl@0
   159
  test_transform selectB-$ii.13 {
sl@0
   160
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a ASC)
sl@0
   161
  } {
sl@0
   162
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 ASC
sl@0
   163
  } {2 3 8 12 14 21}
sl@0
   164
sl@0
   165
  test_transform selectB-$ii.14 {
sl@0
   166
    SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a DESC)
sl@0
   167
  } {
sl@0
   168
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 DESC
sl@0
   169
  } {21 14 12 8 3 2}
sl@0
   170
sl@0
   171
  test_transform selectB-$ii.14 {
sl@0
   172
    SELECT * FROM (
sl@0
   173
      SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a DESC
sl@0
   174
    ) LIMIT 2 OFFSET 2
sl@0
   175
  } {
sl@0
   176
    SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 2
sl@0
   177
  } {12 8}
sl@0
   178
sl@0
   179
  test_transform selectB-$ii.15 {
sl@0
   180
    SELECT * FROM (
sl@0
   181
      SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY a ASC, e DESC
sl@0
   182
    )
sl@0
   183
  } {
sl@0
   184
    SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY a ASC, e DESC
sl@0
   185
  } {2 4 3 6 8 10 12 15 14 16 21 24}
sl@0
   186
}
sl@0
   187
sl@0
   188
do_test selectB-3.0 {
sl@0
   189
  execsql {
sl@0
   190
    DROP INDEX i1;
sl@0
   191
    DROP INDEX i2;
sl@0
   192
  }
sl@0
   193
} {}
sl@0
   194
sl@0
   195
for {set ii 3} {$ii <= 4} {incr ii} {
sl@0
   196
sl@0
   197
  if {$ii == 4} {
sl@0
   198
    do_test selectB-4.0 {
sl@0
   199
      execsql {
sl@0
   200
        CREATE INDEX i1 ON t1(a);
sl@0
   201
        CREATE INDEX i2 ON t1(b);
sl@0
   202
        CREATE INDEX i3 ON t1(c);
sl@0
   203
        CREATE INDEX i4 ON t2(d);
sl@0
   204
        CREATE INDEX i5 ON t2(e);
sl@0
   205
        CREATE INDEX i6 ON t2(f);
sl@0
   206
      }
sl@0
   207
    } {}
sl@0
   208
  }
sl@0
   209
sl@0
   210
  do_test selectB-$ii.1 {
sl@0
   211
    execsql {
sl@0
   212
      SELECT DISTINCT * FROM 
sl@0
   213
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) 
sl@0
   214
      ORDER BY 1;
sl@0
   215
    }
sl@0
   216
  } {6 12 15 18 24}
sl@0
   217
  
sl@0
   218
  do_test selectB-$ii.2 {
sl@0
   219
    execsql {
sl@0
   220
      SELECT c, count(*) FROM 
sl@0
   221
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) 
sl@0
   222
      GROUP BY c ORDER BY 1;
sl@0
   223
    }
sl@0
   224
  } {6 2 12 1 15 1 18 1 24 1}
sl@0
   225
  do_test selectB-$ii.3 {
sl@0
   226
    execsql {
sl@0
   227
      SELECT c, count(*) FROM 
sl@0
   228
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) 
sl@0
   229
      GROUP BY c HAVING count(*)>1;
sl@0
   230
    }
sl@0
   231
  } {6 2}
sl@0
   232
  do_test selectB-$ii.4 {
sl@0
   233
    execsql {
sl@0
   234
      SELECT t4.c, t3.a FROM 
sl@0
   235
        (SELECT c FROM t1 UNION ALL SELECT e FROM t2) AS t4, t1 AS t3
sl@0
   236
      WHERE t3.a=14
sl@0
   237
      ORDER BY 1
sl@0
   238
    }
sl@0
   239
  } {6 14 6 14 12 14 15 14 18 14 24 14}
sl@0
   240
  
sl@0
   241
  do_test selectB-$ii.5 {
sl@0
   242
    execsql {
sl@0
   243
      SELECT d FROM t2 
sl@0
   244
      EXCEPT 
sl@0
   245
      SELECT a FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
sl@0
   246
    }
sl@0
   247
  } {}
sl@0
   248
  do_test selectB-$ii.6 {
sl@0
   249
    execsql {
sl@0
   250
      SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
sl@0
   251
      EXCEPT 
sl@0
   252
      SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
sl@0
   253
    }
sl@0
   254
  } {}
sl@0
   255
  do_test selectB-$ii.7 {
sl@0
   256
    execsql {
sl@0
   257
      SELECT c FROM t1
sl@0
   258
      EXCEPT 
sl@0
   259
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   260
    }
sl@0
   261
  } {12}
sl@0
   262
  do_test selectB-$ii.8 {
sl@0
   263
    execsql {
sl@0
   264
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   265
      EXCEPT 
sl@0
   266
      SELECT c FROM t1
sl@0
   267
    }
sl@0
   268
  } {9 15 24 27}
sl@0
   269
  do_test selectB-$ii.9 {
sl@0
   270
    execsql {
sl@0
   271
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   272
      EXCEPT 
sl@0
   273
      SELECT c FROM t1
sl@0
   274
      ORDER BY c DESC
sl@0
   275
    }
sl@0
   276
  } {27 24 15 9}
sl@0
   277
  
sl@0
   278
  do_test selectB-$ii.10 {
sl@0
   279
    execsql {
sl@0
   280
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   281
      UNION 
sl@0
   282
      SELECT c FROM t1
sl@0
   283
      ORDER BY c DESC
sl@0
   284
    }
sl@0
   285
  } {27 24 18 15 12 9 6}
sl@0
   286
  do_test selectB-$ii.11 {
sl@0
   287
    execsql {
sl@0
   288
      SELECT c FROM t1
sl@0
   289
      UNION 
sl@0
   290
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   291
      ORDER BY c
sl@0
   292
    }
sl@0
   293
  } {6 9 12 15 18 24 27}
sl@0
   294
  do_test selectB-$ii.12 {
sl@0
   295
    execsql {
sl@0
   296
      SELECT c FROM t1 UNION SELECT e FROM t2 UNION ALL SELECT f FROM t2
sl@0
   297
      ORDER BY c
sl@0
   298
    }
sl@0
   299
  } {6 9 12 15 18 18 24 27}
sl@0
   300
  do_test selectB-$ii.13 {
sl@0
   301
    execsql {
sl@0
   302
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   303
      UNION 
sl@0
   304
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   305
      ORDER BY 1
sl@0
   306
    }
sl@0
   307
  } {6 9 15 18 24 27}
sl@0
   308
  
sl@0
   309
  do_test selectB-$ii.14 {
sl@0
   310
    execsql {
sl@0
   311
      SELECT c FROM t1
sl@0
   312
      INTERSECT 
sl@0
   313
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   314
      ORDER BY 1
sl@0
   315
    }
sl@0
   316
  } {6 18}
sl@0
   317
  do_test selectB-$ii.15 {
sl@0
   318
    execsql {
sl@0
   319
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   320
      INTERSECT 
sl@0
   321
      SELECT c FROM t1
sl@0
   322
      ORDER BY 1
sl@0
   323
    }
sl@0
   324
  } {6 18}
sl@0
   325
  do_test selectB-$ii.16 {
sl@0
   326
    execsql {
sl@0
   327
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   328
      INTERSECT 
sl@0
   329
      SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
sl@0
   330
      ORDER BY 1
sl@0
   331
    }
sl@0
   332
  } {6 9 15 18 24 27}
sl@0
   333
sl@0
   334
  do_test selectB-$ii.17 {
sl@0
   335
    execsql {
sl@0
   336
      SELECT * FROM (
sl@0
   337
        SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 4
sl@0
   338
      ) LIMIT 2
sl@0
   339
    }
sl@0
   340
  } {2 8}
sl@0
   341
sl@0
   342
  do_test selectB-$ii.18 {
sl@0
   343
    execsql {
sl@0
   344
      SELECT * FROM (
sl@0
   345
        SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 4 OFFSET 2
sl@0
   346
      ) LIMIT 2
sl@0
   347
    }
sl@0
   348
  } {14 3}
sl@0
   349
sl@0
   350
  do_test selectB-$ii.19 {
sl@0
   351
    execsql {
sl@0
   352
      SELECT * FROM (
sl@0
   353
        SELECT DISTINCT (a/10) FROM t1 UNION ALL SELECT DISTINCT(d%2) FROM t2
sl@0
   354
      )
sl@0
   355
    }
sl@0
   356
  } {0 1 0 1}
sl@0
   357
sl@0
   358
  do_test selectB-$ii.20 {
sl@0
   359
    execsql {
sl@0
   360
      SELECT DISTINCT * FROM (
sl@0
   361
        SELECT DISTINCT (a/10) FROM t1 UNION ALL SELECT DISTINCT(d%2) FROM t2
sl@0
   362
      )
sl@0
   363
    }
sl@0
   364
  } {0 1}
sl@0
   365
sl@0
   366
  do_test selectB-$ii.21 {
sl@0
   367
    execsql {
sl@0
   368
      SELECT * FROM (SELECT * FROM t1 UNION ALL SELECT * FROM t2) ORDER BY a+b
sl@0
   369
    }
sl@0
   370
  } {2 4 6 3 6 9 8 10 12 12 15 18 14 16 18 21 24 27}
sl@0
   371
sl@0
   372
  do_test selectB-$ii.21 {
sl@0
   373
    execsql {
sl@0
   374
      SELECT * FROM (SELECT 345 UNION ALL SELECT d FROM t2) ORDER BY 1;
sl@0
   375
    }
sl@0
   376
  } {3 12 21 345}
sl@0
   377
}
sl@0
   378
sl@0
   379
finish_test