os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/where2.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
# 2005 July 28
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.  The
sl@0
    12
# focus of this file is testing the use of indices in WHERE clauses
sl@0
    13
# based on recent changes to the optimizer.
sl@0
    14
#
sl@0
    15
# $Id: where2.test,v 1.13 2007/12/10 05:03:48 danielk1977 Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
# Build some test data
sl@0
    21
#
sl@0
    22
do_test where2-1.0 {
sl@0
    23
  execsql {
sl@0
    24
    BEGIN;
sl@0
    25
    CREATE TABLE t1(w int, x int, y int, z int);
sl@0
    26
  }
sl@0
    27
  for {set i 1} {$i<=100} {incr i} {
sl@0
    28
    set w $i
sl@0
    29
    set x [expr {int(log($i)/log(2))}]
sl@0
    30
    set y [expr {$i*$i + 2*$i + 1}]
sl@0
    31
    set z [expr {$x+$y}]
sl@0
    32
    ifcapable tclvar {
sl@0
    33
      execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
sl@0
    34
    } else {
sl@0
    35
      execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
sl@0
    36
    }
sl@0
    37
  }
sl@0
    38
  execsql {
sl@0
    39
    CREATE UNIQUE INDEX i1w ON t1(w);
sl@0
    40
    CREATE INDEX i1xy ON t1(x,y);
sl@0
    41
    CREATE INDEX i1zyx ON t1(z,y,x);
sl@0
    42
    COMMIT;
sl@0
    43
  }
sl@0
    44
} {}
sl@0
    45
sl@0
    46
# Do an SQL statement.  Append the search count to the end of the result.
sl@0
    47
#
sl@0
    48
proc count sql {
sl@0
    49
  set ::sqlite_search_count 0
sl@0
    50
  return [concat [execsql $sql] $::sqlite_search_count]
sl@0
    51
}
sl@0
    52
sl@0
    53
# This procedure executes the SQL.  Then it checks to see if the OP_Sort
sl@0
    54
# opcode was executed.  If an OP_Sort did occur, then "sort" is appended
sl@0
    55
# to the result.  If no OP_Sort happened, then "nosort" is appended.
sl@0
    56
#
sl@0
    57
# This procedure is used to check to make sure sorting is or is not
sl@0
    58
# occurring as expected.
sl@0
    59
#
sl@0
    60
proc cksort {sql} {
sl@0
    61
  set ::sqlite_sort_count 0
sl@0
    62
  set data [execsql $sql]
sl@0
    63
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
    64
  lappend data $x
sl@0
    65
  return $data
sl@0
    66
}
sl@0
    67
sl@0
    68
# This procedure executes the SQL.  Then it appends to the result the
sl@0
    69
# "sort" or "nosort" keyword (as in the cksort procedure above) then
sl@0
    70
# it appends the ::sqlite_query_plan variable.
sl@0
    71
#
sl@0
    72
proc queryplan {sql} {
sl@0
    73
  set ::sqlite_sort_count 0
sl@0
    74
  set data [execsql $sql]
sl@0
    75
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
    76
  lappend data $x
sl@0
    77
  return [concat $data $::sqlite_query_plan]
sl@0
    78
}
sl@0
    79
sl@0
    80
sl@0
    81
# Prefer a UNIQUE index over another index.
sl@0
    82
#
sl@0
    83
do_test where2-1.1 {
sl@0
    84
  queryplan {
sl@0
    85
    SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396
sl@0
    86
  }
sl@0
    87
} {85 6 7396 7402 nosort t1 i1w}
sl@0
    88
sl@0
    89
# Always prefer a rowid== constraint over any other index.
sl@0
    90
#
sl@0
    91
do_test where2-1.3 {
sl@0
    92
  queryplan {
sl@0
    93
    SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 AND rowid=85
sl@0
    94
  }
sl@0
    95
} {85 6 7396 7402 nosort t1 *}
sl@0
    96
sl@0
    97
# When constrained by a UNIQUE index, the ORDER BY clause is always ignored.
sl@0
    98
#
sl@0
    99
do_test where2-2.1 {
sl@0
   100
  queryplan {
sl@0
   101
    SELECT * FROM t1 WHERE w=85 ORDER BY random(5);
sl@0
   102
  }
sl@0
   103
} {85 6 7396 7402 nosort t1 i1w}
sl@0
   104
do_test where2-2.2 {
sl@0
   105
  queryplan {
sl@0
   106
    SELECT * FROM t1 WHERE x=6 AND y=7396 ORDER BY random(5);
sl@0
   107
  }
sl@0
   108
} {85 6 7396 7402 sort t1 i1xy}
sl@0
   109
do_test where2-2.3 {
sl@0
   110
  queryplan {
sl@0
   111
    SELECT * FROM t1 WHERE rowid=85 AND x=6 AND y=7396 ORDER BY random(5);
sl@0
   112
  }
sl@0
   113
} {85 6 7396 7402 nosort t1 *}
sl@0
   114
sl@0
   115
sl@0
   116
# Efficient handling of forward and reverse table scans.
sl@0
   117
#
sl@0
   118
do_test where2-3.1 {
sl@0
   119
  queryplan {
sl@0
   120
    SELECT * FROM t1 ORDER BY rowid LIMIT 2
sl@0
   121
  }
sl@0
   122
} {1 0 4 4 2 1 9 10 nosort t1 *}
sl@0
   123
do_test where2-3.2 {
sl@0
   124
  queryplan {
sl@0
   125
    SELECT * FROM t1 ORDER BY rowid DESC LIMIT 2
sl@0
   126
  }
sl@0
   127
} {100 6 10201 10207 99 6 10000 10006 nosort t1 *}
sl@0
   128
sl@0
   129
# The IN operator can be used by indices at multiple layers
sl@0
   130
#
sl@0
   131
ifcapable subquery {
sl@0
   132
  do_test where2-4.1 {
sl@0
   133
    queryplan {
sl@0
   134
      SELECT * FROM t1 WHERE z IN (10207,10006) AND y IN (10000,10201)
sl@0
   135
                       AND x>0 AND x<10
sl@0
   136
      ORDER BY w
sl@0
   137
    }
sl@0
   138
  } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
sl@0
   139
  do_test where2-4.2 {
sl@0
   140
    queryplan {
sl@0
   141
      SELECT * FROM t1 WHERE z IN (10207,10006) AND y=10000
sl@0
   142
                       AND x>0 AND x<10
sl@0
   143
      ORDER BY w
sl@0
   144
    }
sl@0
   145
  } {99 6 10000 10006 sort t1 i1zyx}
sl@0
   146
  do_test where2-4.3 {
sl@0
   147
    queryplan {
sl@0
   148
      SELECT * FROM t1 WHERE z=10006 AND y IN (10000,10201)
sl@0
   149
                       AND x>0 AND x<10
sl@0
   150
      ORDER BY w
sl@0
   151
    }
sl@0
   152
  } {99 6 10000 10006 sort t1 i1zyx}
sl@0
   153
  ifcapable compound {
sl@0
   154
    do_test where2-4.4 {
sl@0
   155
      queryplan {
sl@0
   156
        SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
sl@0
   157
                         AND y IN (10000,10201)
sl@0
   158
                         AND x>0 AND x<10
sl@0
   159
        ORDER BY w
sl@0
   160
      }
sl@0
   161
    } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
sl@0
   162
    do_test where2-4.5 {
sl@0
   163
      queryplan {
sl@0
   164
        SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
sl@0
   165
                         AND y IN (SELECT 10000 UNION SELECT 10201)
sl@0
   166
                         AND x>0 AND x<10
sl@0
   167
        ORDER BY w
sl@0
   168
      }
sl@0
   169
    } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
sl@0
   170
  }
sl@0
   171
  do_test where2-4.6 {
sl@0
   172
    queryplan {
sl@0
   173
      SELECT * FROM t1
sl@0
   174
       WHERE x IN (1,2,3,4,5,6,7,8)
sl@0
   175
         AND y IN (10000,10001,10002,10003,10004,10005)
sl@0
   176
       ORDER BY 2
sl@0
   177
    }
sl@0
   178
  } {99 6 10000 10006 sort t1 i1xy}
sl@0
   179
sl@0
   180
  # Duplicate entires on the RHS of an IN operator do not cause duplicate
sl@0
   181
  # output rows.
sl@0
   182
  #
sl@0
   183
  do_test where2-4.6 {
sl@0
   184
    queryplan {
sl@0
   185
      SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
sl@0
   186
      ORDER BY w
sl@0
   187
    }
sl@0
   188
  } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
sl@0
   189
  ifcapable compound {
sl@0
   190
    do_test where2-4.7 {
sl@0
   191
      queryplan {
sl@0
   192
        SELECT * FROM t1 WHERE z IN (
sl@0
   193
           SELECT 10207 UNION ALL SELECT 10006
sl@0
   194
           UNION ALL SELECT 10006 UNION ALL SELECT 10207)
sl@0
   195
        ORDER BY w
sl@0
   196
      }
sl@0
   197
    } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
sl@0
   198
  }
sl@0
   199
sl@0
   200
} ;# ifcapable subquery
sl@0
   201
sl@0
   202
# The use of an IN operator disables the index as a sorter.
sl@0
   203
#
sl@0
   204
do_test where2-5.1 {
sl@0
   205
  queryplan {
sl@0
   206
    SELECT * FROM t1 WHERE w=99 ORDER BY w
sl@0
   207
  }
sl@0
   208
} {99 6 10000 10006 nosort t1 i1w}
sl@0
   209
sl@0
   210
ifcapable subquery {
sl@0
   211
  do_test where2-5.2 {
sl@0
   212
    queryplan {
sl@0
   213
      SELECT * FROM t1 WHERE w IN (99) ORDER BY w
sl@0
   214
    }
sl@0
   215
  } {99 6 10000 10006 sort t1 i1w}
sl@0
   216
}
sl@0
   217
sl@0
   218
# Verify that OR clauses get translated into IN operators.
sl@0
   219
#
sl@0
   220
set ::idx {}
sl@0
   221
ifcapable subquery {set ::idx i1w}
sl@0
   222
do_test where2-6.1.1 {
sl@0
   223
  queryplan {
sl@0
   224
    SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w
sl@0
   225
  }
sl@0
   226
} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
sl@0
   227
do_test where2-6.1.2 {
sl@0
   228
  queryplan {
sl@0
   229
    SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w
sl@0
   230
  }
sl@0
   231
} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
sl@0
   232
do_test where2-6.2 {
sl@0
   233
  queryplan {
sl@0
   234
    SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w
sl@0
   235
  }
sl@0
   236
} [list 6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
sl@0
   237
sl@0
   238
do_test where2-6.3 {
sl@0
   239
  queryplan {
sl@0
   240
    SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=+w ORDER BY +w
sl@0
   241
  }
sl@0
   242
} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}}
sl@0
   243
do_test where2-6.4 {
sl@0
   244
  queryplan {
sl@0
   245
    SELECT * FROM t1 WHERE w=99 OR +w=100 OR 6=w ORDER BY +w
sl@0
   246
  }
sl@0
   247
} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}}
sl@0
   248
sl@0
   249
set ::idx {}
sl@0
   250
ifcapable subquery {set ::idx i1zyx}
sl@0
   251
do_test where2-6.5 {
sl@0
   252
  queryplan {
sl@0
   253
    SELECT b.* FROM t1 a, t1 b
sl@0
   254
     WHERE a.w=1 AND (a.y=b.z OR b.z=10)
sl@0
   255
     ORDER BY +b.w
sl@0
   256
  }
sl@0
   257
} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
sl@0
   258
do_test where2-6.6 {
sl@0
   259
  queryplan {
sl@0
   260
    SELECT b.* FROM t1 a, t1 b
sl@0
   261
     WHERE a.w=1 AND (b.z=10 OR a.y=b.z OR b.z=10)
sl@0
   262
     ORDER BY +b.w
sl@0
   263
  }
sl@0
   264
} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
sl@0
   265
sl@0
   266
# Ticket #2249.  Make sure the OR optimization is not attempted if
sl@0
   267
# comparisons between columns of different affinities are needed.
sl@0
   268
#
sl@0
   269
do_test where2-6.7 {
sl@0
   270
  execsql {
sl@0
   271
    CREATE TABLE t2249a(a TEXT UNIQUE);
sl@0
   272
    CREATE TABLE t2249b(b INTEGER);
sl@0
   273
    INSERT INTO t2249a VALUES('0123');
sl@0
   274
    INSERT INTO t2249b VALUES(123);
sl@0
   275
  }
sl@0
   276
  queryplan {
sl@0
   277
    -- Because a is type TEXT and b is type INTEGER, both a and b
sl@0
   278
    -- will attempt to convert to NUMERIC before the comparison.
sl@0
   279
    -- They will thus compare equal.
sl@0
   280
    --
sl@0
   281
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b;
sl@0
   282
  }
sl@0
   283
} {123 0123 nosort t2249b {} t2249a {}}
sl@0
   284
do_test where2-6.9 {
sl@0
   285
  queryplan {
sl@0
   286
    -- The + operator removes affinity from the rhs.  No conversions
sl@0
   287
    -- occur and the comparison is false.  The result is an empty set.
sl@0
   288
    --
sl@0
   289
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b;
sl@0
   290
  }
sl@0
   291
} {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
sl@0
   292
do_test where2-6.9.2 {
sl@0
   293
  # The same thing but with the expression flipped around.
sl@0
   294
  queryplan {
sl@0
   295
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a
sl@0
   296
  }
sl@0
   297
} {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
sl@0
   298
do_test where2-6.10 {
sl@0
   299
  queryplan {
sl@0
   300
    -- Use + on both sides of the comparison to disable indices
sl@0
   301
    -- completely.  Make sure we get the same result.
sl@0
   302
    --
sl@0
   303
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE +a=+b;
sl@0
   304
  }
sl@0
   305
} {nosort t2249b {} t2249a {}}
sl@0
   306
do_test where2-6.11 {
sl@0
   307
  # This will not attempt the OR optimization because of the a=b
sl@0
   308
  # comparison.
sl@0
   309
  queryplan {
sl@0
   310
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello';
sl@0
   311
  }
sl@0
   312
} {123 0123 nosort t2249b {} t2249a {}}
sl@0
   313
do_test where2-6.11.2 {
sl@0
   314
  # Permutations of the expression terms.
sl@0
   315
  queryplan {
sl@0
   316
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello';
sl@0
   317
  }
sl@0
   318
} {123 0123 nosort t2249b {} t2249a {}}
sl@0
   319
do_test where2-6.11.3 {
sl@0
   320
  # Permutations of the expression terms.
sl@0
   321
  queryplan {
sl@0
   322
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a;
sl@0
   323
  }
sl@0
   324
} {123 0123 nosort t2249b {} t2249a {}}
sl@0
   325
do_test where2-6.11.4 {
sl@0
   326
  # Permutations of the expression terms.
sl@0
   327
  queryplan {
sl@0
   328
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a;
sl@0
   329
  }
sl@0
   330
} {123 0123 nosort t2249b {} t2249a {}}
sl@0
   331
ifcapable explain&&subquery {
sl@0
   332
  # These tests are not run if subquery support is not included in the
sl@0
   333
  # build. This is because these tests test the "a = 1 OR a = 2" to
sl@0
   334
  # "a IN (1, 2)" optimisation transformation, which is not enabled if
sl@0
   335
  # subqueries and the IN operator is not available.
sl@0
   336
  #
sl@0
   337
  do_test where2-6.12 {
sl@0
   338
    # In this case, the +b disables the affinity conflict and allows
sl@0
   339
    # the OR optimization to be used again.  The result is now an empty
sl@0
   340
    # set, the same as in where2-6.9.
sl@0
   341
    queryplan {
sl@0
   342
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello';
sl@0
   343
    }
sl@0
   344
  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
sl@0
   345
  do_test where2-6.12.2 {
sl@0
   346
    # In this case, the +b disables the affinity conflict and allows
sl@0
   347
    # the OR optimization to be used again.  The result is now an empty
sl@0
   348
    # set, the same as in where2-6.9.
sl@0
   349
    queryplan {
sl@0
   350
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a;
sl@0
   351
    }
sl@0
   352
  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
sl@0
   353
  do_test where2-6.12.3 {
sl@0
   354
    # In this case, the +b disables the affinity conflict and allows
sl@0
   355
    # the OR optimization to be used again.  The result is now an empty
sl@0
   356
    # set, the same as in where2-6.9.
sl@0
   357
    queryplan {
sl@0
   358
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello';
sl@0
   359
    }
sl@0
   360
  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
sl@0
   361
  do_test where2-6.13 {
sl@0
   362
    # The addition of +a on the second term disabled the OR optimization.
sl@0
   363
    # But we should still get the same empty-set result as in where2-6.9.
sl@0
   364
    queryplan {
sl@0
   365
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello';
sl@0
   366
    }
sl@0
   367
  } {nosort t2249b {} t2249a {}}
sl@0
   368
}
sl@0
   369
sl@0
   370
# Variations on the order of terms in a WHERE clause in order
sl@0
   371
# to make sure the OR optimizer can recognize them all.
sl@0
   372
do_test where2-6.20 {
sl@0
   373
  queryplan {
sl@0
   374
    SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a
sl@0
   375
  }
sl@0
   376
} {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
sl@0
   377
ifcapable explain&&subquery {
sl@0
   378
  # These tests are not run if subquery support is not included in the
sl@0
   379
  # build. This is because these tests test the "a = 1 OR a = 2" to
sl@0
   380
  # "a IN (1, 2)" optimisation transformation, which is not enabled if
sl@0
   381
  # subqueries and the IN operator is not available.
sl@0
   382
  #
sl@0
   383
  do_test where2-6.21 {
sl@0
   384
    queryplan {
sl@0
   385
      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a OR y.a='hello'
sl@0
   386
    }
sl@0
   387
  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
sl@0
   388
  do_test where2-6.22 {
sl@0
   389
    queryplan {
sl@0
   390
      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a=x.a OR y.a='hello'
sl@0
   391
    }
sl@0
   392
  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
sl@0
   393
  do_test where2-6.23 {
sl@0
   394
    queryplan {
sl@0
   395
      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a='hello' OR x.a=y.a
sl@0
   396
    }
sl@0
   397
  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
sl@0
   398
}
sl@0
   399
sl@0
   400
# Unique queries (queries that are guaranteed to return only a single
sl@0
   401
# row of result) do not call the sorter.  But all tables must give
sl@0
   402
# a unique result.  If any one table in the join does not give a unique
sl@0
   403
# result then sorting is necessary.
sl@0
   404
#
sl@0
   405
do_test where2-7.1 {
sl@0
   406
  cksort {
sl@0
   407
    create table t8(a unique, b, c);
sl@0
   408
    insert into t8 values(1,2,3);
sl@0
   409
    insert into t8 values(2,3,4);
sl@0
   410
    create table t9(x,y);
sl@0
   411
    insert into t9 values(2,4);
sl@0
   412
    insert into t9 values(2,3);
sl@0
   413
    select y from t8, t9 where a=1 order by a, y;
sl@0
   414
  }
sl@0
   415
} {3 4 sort}
sl@0
   416
do_test where2-7.2 {
sl@0
   417
  cksort {
sl@0
   418
    select * from t8 where a=1 order by b, c
sl@0
   419
  }
sl@0
   420
} {1 2 3 nosort}
sl@0
   421
do_test where2-7.3 {
sl@0
   422
  cksort {
sl@0
   423
    select * from t8, t9 where a=1 and y=3 order by b, x
sl@0
   424
  }
sl@0
   425
} {1 2 3 2 3 sort}
sl@0
   426
do_test where2-7.4 {
sl@0
   427
  cksort {
sl@0
   428
    create unique index i9y on t9(y);
sl@0
   429
    select * from t8, t9 where a=1 and y=3 order by b, x
sl@0
   430
  }
sl@0
   431
} {1 2 3 2 3 nosort}
sl@0
   432
sl@0
   433
# Ticket #1807.  Using IN constrains on multiple columns of
sl@0
   434
# a multi-column index.
sl@0
   435
#
sl@0
   436
ifcapable subquery {
sl@0
   437
  do_test where2-8.1 {
sl@0
   438
    execsql {
sl@0
   439
      SELECT * FROM t1 WHERE x IN (20,21) AND y IN (1,2)
sl@0
   440
    }
sl@0
   441
  } {}
sl@0
   442
  do_test where2-8.2 {
sl@0
   443
    execsql {
sl@0
   444
      SELECT * FROM t1 WHERE x IN (1,2) AND y IN (-5,-6)
sl@0
   445
    }
sl@0
   446
  } {}
sl@0
   447
  execsql {CREATE TABLE tx AS SELECT * FROM t1}
sl@0
   448
  do_test where2-8.3 {
sl@0
   449
    execsql {
sl@0
   450
      SELECT w FROM t1
sl@0
   451
       WHERE x IN (SELECT x FROM tx WHERE rowid<0)
sl@0
   452
         AND +y IN (SELECT y FROM tx WHERE rowid=1)
sl@0
   453
    }
sl@0
   454
  } {}
sl@0
   455
  do_test where2-8.4 {
sl@0
   456
    execsql {
sl@0
   457
      SELECT w FROM t1
sl@0
   458
       WHERE x IN (SELECT x FROM tx WHERE rowid=1)
sl@0
   459
         AND y IN (SELECT y FROM tx WHERE rowid<0)
sl@0
   460
    }
sl@0
   461
  } {}
sl@0
   462
  #set sqlite_where_trace 1
sl@0
   463
  do_test where2-8.5 {
sl@0
   464
    execsql {
sl@0
   465
      CREATE INDEX tx_xyz ON tx(x, y, z, w);
sl@0
   466
      SELECT w FROM tx
sl@0
   467
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   468
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   469
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 12 AND 14)
sl@0
   470
    }
sl@0
   471
  } {12 13 14}
sl@0
   472
  do_test where2-8.6 {
sl@0
   473
    execsql {
sl@0
   474
      SELECT w FROM tx
sl@0
   475
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   476
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 12 AND 14)
sl@0
   477
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   478
    }
sl@0
   479
  } {12 13 14}
sl@0
   480
  do_test where2-8.7 {
sl@0
   481
    execsql {
sl@0
   482
      SELECT w FROM tx
sl@0
   483
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 12 AND 14)
sl@0
   484
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   485
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   486
    }
sl@0
   487
  } {10 11 12 13 14 15}
sl@0
   488
  do_test where2-8.8 {
sl@0
   489
    execsql {
sl@0
   490
      SELECT w FROM tx
sl@0
   491
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   492
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   493
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   494
    }
sl@0
   495
  } {10 11 12 13 14 15 16 17 18 19 20}
sl@0
   496
  do_test where2-8.9 {
sl@0
   497
    execsql {
sl@0
   498
      SELECT w FROM tx
sl@0
   499
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   500
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   501
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 2 AND 4)
sl@0
   502
    }
sl@0
   503
  } {}
sl@0
   504
  do_test where2-8.10 {
sl@0
   505
    execsql {
sl@0
   506
      SELECT w FROM tx
sl@0
   507
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   508
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 2 AND 4)
sl@0
   509
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   510
    }
sl@0
   511
  } {}
sl@0
   512
  do_test where2-8.11 {
sl@0
   513
    execsql {
sl@0
   514
      SELECT w FROM tx
sl@0
   515
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 2 AND 4)
sl@0
   516
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   517
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   518
    }
sl@0
   519
  } {}
sl@0
   520
  do_test where2-8.12 {
sl@0
   521
    execsql {
sl@0
   522
      SELECT w FROM tx
sl@0
   523
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   524
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   525
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN -4 AND -2)
sl@0
   526
    }
sl@0
   527
  } {}
sl@0
   528
  do_test where2-8.13 {
sl@0
   529
    execsql {
sl@0
   530
      SELECT w FROM tx
sl@0
   531
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   532
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN -4 AND -2)
sl@0
   533
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   534
    }
sl@0
   535
  } {}
sl@0
   536
  do_test where2-8.14 {
sl@0
   537
    execsql {
sl@0
   538
      SELECT w FROM tx
sl@0
   539
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN -4 AND -2)
sl@0
   540
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   541
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   542
    }
sl@0
   543
  } {}
sl@0
   544
  do_test where2-8.15 {
sl@0
   545
    execsql {
sl@0
   546
      SELECT w FROM tx
sl@0
   547
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   548
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   549
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 200 AND 300)
sl@0
   550
    }
sl@0
   551
  } {}
sl@0
   552
  do_test where2-8.16 {
sl@0
   553
    execsql {
sl@0
   554
      SELECT w FROM tx
sl@0
   555
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   556
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 200 AND 300)
sl@0
   557
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   558
    }
sl@0
   559
  } {}
sl@0
   560
  do_test where2-8.17 {
sl@0
   561
    execsql {
sl@0
   562
      SELECT w FROM tx
sl@0
   563
       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 200 AND 300)
sl@0
   564
         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   565
         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
sl@0
   566
    }
sl@0
   567
  } {}
sl@0
   568
  do_test where2-8.18 {
sl@0
   569
    execsql {
sl@0
   570
      SELECT w FROM tx
sl@0
   571
       WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
sl@0
   572
         AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
sl@0
   573
         AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 200 AND 300)
sl@0
   574
    }
sl@0
   575
  } {}
sl@0
   576
  do_test where2-8.19 {
sl@0
   577
    execsql {
sl@0
   578
      SELECT w FROM tx
sl@0
   579
       WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
sl@0
   580
         AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 200 AND 300)
sl@0
   581
         AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
sl@0
   582
    }
sl@0
   583
  } {}
sl@0
   584
  do_test where2-8.20 {
sl@0
   585
    execsql {
sl@0
   586
      SELECT w FROM tx
sl@0
   587
       WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 200 AND 300)
sl@0
   588
         AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
sl@0
   589
         AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
sl@0
   590
    }
sl@0
   591
  } {}
sl@0
   592
}  
sl@0
   593
sl@0
   594
# Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized
sl@0
   595
# when we have an index on A and B.
sl@0
   596
#
sl@0
   597
ifcapable or_opt&&tclvar {
sl@0
   598
  do_test where2-9.1 {
sl@0
   599
    execsql {
sl@0
   600
      BEGIN;
sl@0
   601
      CREATE TABLE t10(a,b,c);
sl@0
   602
      INSERT INTO t10 VALUES(1,1,1);
sl@0
   603
      INSERT INTO t10 VALUES(1,2,2);
sl@0
   604
      INSERT INTO t10 VALUES(1,3,3);
sl@0
   605
    }
sl@0
   606
    for {set i 4} {$i<=1000} {incr i} {
sl@0
   607
      execsql {INSERT INTO t10 VALUES(1,$i,$i)}
sl@0
   608
    }
sl@0
   609
    execsql {
sl@0
   610
      CREATE INDEX i10 ON t10(a,b);
sl@0
   611
      COMMIT;
sl@0
   612
      SELECT count(*) FROM t10;
sl@0
   613
    }
sl@0
   614
  } 1000
sl@0
   615
  ifcapable subquery {
sl@0
   616
    do_test where2-9.2 {
sl@0
   617
      count {
sl@0
   618
        SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3)
sl@0
   619
      }
sl@0
   620
    } {1 2 2 1 3 3 7}
sl@0
   621
  }
sl@0
   622
}
sl@0
   623
sl@0
   624
finish_test