~ubuntu-branches/ubuntu/maverick/sqlite3/maverick-updates

« back to all changes in this revision

Viewing changes to test/where2.test

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2008-10-01 20:16:18 UTC
  • mfrom: (3.1.20 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081001201618-yfvqqj1qs29wdtcc
Tags: 3.5.9-5
Backport fix for distinct on indexes (closes: #500792).

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# focus of this file is testing the use of indices in WHERE clauses
13
13
# based on recent changes to the optimizer.
14
14
#
15
 
# $Id: where2.test,v 1.9 2006/05/11 13:26:26 drh Exp $
 
15
# $Id: where2.test,v 1.13 2007/12/10 05:03:48 danielk1977 Exp $
16
16
 
17
17
set testdir [file dirname $argv0]
18
18
source $testdir/tester.tcl
219
219
#
220
220
set ::idx {}
221
221
ifcapable subquery {set ::idx i1w}
222
 
do_test where2-6.1 {
 
222
do_test where2-6.1.1 {
223
223
  queryplan {
224
224
    SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w
225
225
  }
226
226
} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
 
227
do_test where2-6.1.2 {
 
228
  queryplan {
 
229
    SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w
 
230
  }
 
231
} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
227
232
do_test where2-6.2 {
228
233
  queryplan {
229
234
    SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w
258
263
  }
259
264
} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
260
265
 
 
266
# Ticket #2249.  Make sure the OR optimization is not attempted if
 
267
# comparisons between columns of different affinities are needed.
 
268
#
 
269
do_test where2-6.7 {
 
270
  execsql {
 
271
    CREATE TABLE t2249a(a TEXT UNIQUE);
 
272
    CREATE TABLE t2249b(b INTEGER);
 
273
    INSERT INTO t2249a VALUES('0123');
 
274
    INSERT INTO t2249b VALUES(123);
 
275
  }
 
276
  queryplan {
 
277
    -- Because a is type TEXT and b is type INTEGER, both a and b
 
278
    -- will attempt to convert to NUMERIC before the comparison.
 
279
    -- They will thus compare equal.
 
280
    --
 
281
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b;
 
282
  }
 
283
} {123 0123 nosort t2249b {} t2249a {}}
 
284
do_test where2-6.9 {
 
285
  queryplan {
 
286
    -- The + operator removes affinity from the rhs.  No conversions
 
287
    -- occur and the comparison is false.  The result is an empty set.
 
288
    --
 
289
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b;
 
290
  }
 
291
} {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
 
292
do_test where2-6.9.2 {
 
293
  # The same thing but with the expression flipped around.
 
294
  queryplan {
 
295
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a
 
296
  }
 
297
} {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
 
298
do_test where2-6.10 {
 
299
  queryplan {
 
300
    -- Use + on both sides of the comparison to disable indices
 
301
    -- completely.  Make sure we get the same result.
 
302
    --
 
303
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE +a=+b;
 
304
  }
 
305
} {nosort t2249b {} t2249a {}}
 
306
do_test where2-6.11 {
 
307
  # This will not attempt the OR optimization because of the a=b
 
308
  # comparison.
 
309
  queryplan {
 
310
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello';
 
311
  }
 
312
} {123 0123 nosort t2249b {} t2249a {}}
 
313
do_test where2-6.11.2 {
 
314
  # Permutations of the expression terms.
 
315
  queryplan {
 
316
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello';
 
317
  }
 
318
} {123 0123 nosort t2249b {} t2249a {}}
 
319
do_test where2-6.11.3 {
 
320
  # Permutations of the expression terms.
 
321
  queryplan {
 
322
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a;
 
323
  }
 
324
} {123 0123 nosort t2249b {} t2249a {}}
 
325
do_test where2-6.11.4 {
 
326
  # Permutations of the expression terms.
 
327
  queryplan {
 
328
    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a;
 
329
  }
 
330
} {123 0123 nosort t2249b {} t2249a {}}
 
331
ifcapable explain&&subquery {
 
332
  # These tests are not run if subquery support is not included in the
 
333
  # build. This is because these tests test the "a = 1 OR a = 2" to
 
334
  # "a IN (1, 2)" optimisation transformation, which is not enabled if
 
335
  # subqueries and the IN operator is not available.
 
336
  #
 
337
  do_test where2-6.12 {
 
338
    # In this case, the +b disables the affinity conflict and allows
 
339
    # the OR optimization to be used again.  The result is now an empty
 
340
    # set, the same as in where2-6.9.
 
341
    queryplan {
 
342
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello';
 
343
    }
 
344
  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
 
345
  do_test where2-6.12.2 {
 
346
    # In this case, the +b disables the affinity conflict and allows
 
347
    # the OR optimization to be used again.  The result is now an empty
 
348
    # set, the same as in where2-6.9.
 
349
    queryplan {
 
350
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a;
 
351
    }
 
352
  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
 
353
  do_test where2-6.12.3 {
 
354
    # In this case, the +b disables the affinity conflict and allows
 
355
    # the OR optimization to be used again.  The result is now an empty
 
356
    # set, the same as in where2-6.9.
 
357
    queryplan {
 
358
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello';
 
359
    }
 
360
  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
 
361
  do_test where2-6.13 {
 
362
    # The addition of +a on the second term disabled the OR optimization.
 
363
    # But we should still get the same empty-set result as in where2-6.9.
 
364
    queryplan {
 
365
      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello';
 
366
    }
 
367
  } {nosort t2249b {} t2249a {}}
 
368
}
 
369
 
 
370
# Variations on the order of terms in a WHERE clause in order
 
371
# to make sure the OR optimizer can recognize them all.
 
372
do_test where2-6.20 {
 
373
  queryplan {
 
374
    SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a
 
375
  }
 
376
} {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
 
377
ifcapable explain&&subquery {
 
378
  # These tests are not run if subquery support is not included in the
 
379
  # build. This is because these tests test the "a = 1 OR a = 2" to
 
380
  # "a IN (1, 2)" optimisation transformation, which is not enabled if
 
381
  # subqueries and the IN operator is not available.
 
382
  #
 
383
  do_test where2-6.21 {
 
384
    queryplan {
 
385
      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a OR y.a='hello'
 
386
    }
 
387
  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
 
388
  do_test where2-6.22 {
 
389
    queryplan {
 
390
      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a=x.a OR y.a='hello'
 
391
    }
 
392
  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
 
393
  do_test where2-6.23 {
 
394
    queryplan {
 
395
      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a='hello' OR x.a=y.a
 
396
    }
 
397
  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
 
398
}
 
399
 
261
400
# Unique queries (queries that are guaranteed to return only a single
262
401
# row of result) do not call the sorter.  But all tables must give
263
402
# a unique result.  If any one table in the join does not give a unique
451
590
    }
452
591
  } {}
453
592
}  
 
593
 
 
594
# Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized
 
595
# when we have an index on A and B.
 
596
#
 
597
ifcapable or_opt&&tclvar {
 
598
  do_test where2-9.1 {
 
599
    execsql {
 
600
      BEGIN;
 
601
      CREATE TABLE t10(a,b,c);
 
602
      INSERT INTO t10 VALUES(1,1,1);
 
603
      INSERT INTO t10 VALUES(1,2,2);
 
604
      INSERT INTO t10 VALUES(1,3,3);
 
605
    }
 
606
    for {set i 4} {$i<=1000} {incr i} {
 
607
      execsql {INSERT INTO t10 VALUES(1,$i,$i)}
 
608
    }
 
609
    execsql {
 
610
      CREATE INDEX i10 ON t10(a,b);
 
611
      COMMIT;
 
612
      SELECT count(*) FROM t10;
 
613
    }
 
614
  } 1000
 
615
  ifcapable subquery {
 
616
    do_test where2-9.2 {
 
617
      count {
 
618
        SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3)
 
619
      }
 
620
    } {1 2 2 1 3 3 7}
 
621
  }
 
622
}
 
623
 
454
624
finish_test