orderby2.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # 2012 Sept 27
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing that the optimizations that disable
  13. # ORDER BY clauses when the natural order of a query is correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. set ::testprefix orderby2
  18. # Generate test data for a join. Verify that the join gets the
  19. # correct answer.
  20. #
  21. do_test 1.0 {
  22. db eval {
  23. CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  24. INSERT INTO t1 VALUES(1,11), (2,22);
  25. CREATE TABLE t2(d, e, UNIQUE(d,e));
  26. INSERT INTO t2 VALUES(10, 'ten'), (11,'eleven'), (12,'twelve'),
  27. (11, 'oneteen');
  28. }
  29. } {}
  30. do_test 1.1a {
  31. db eval {
  32. SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY d, e;
  33. }
  34. } {eleven oneteen}
  35. do_test 1.1b {
  36. db eval {
  37. EXPLAIN QUERY PLAN
  38. SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY d, e;
  39. }
  40. } {~/ORDER BY/}
  41. do_test 1.2a {
  42. db eval {
  43. SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY e;
  44. }
  45. } {eleven oneteen}
  46. do_test 1.2b {
  47. db eval {
  48. EXPLAIN QUERY PLAN
  49. SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY e;
  50. }
  51. } {~/ORDER BY/}
  52. do_test 1.3a {
  53. db eval {
  54. SELECT e, b FROM t1, t2 WHERE a=1 ORDER BY d, e;
  55. }
  56. } {ten 11 eleven 11 oneteen 11 twelve 11}
  57. do_test 1.3b {
  58. db eval {
  59. EXPLAIN QUERY PLAN
  60. SELECT e, b FROM t1, t2 WHERE a=1 ORDER BY d, e;
  61. }
  62. } {~/ORDER BY/}
  63. # The following tests derived from TH3 test module cov1/where34.test
  64. #
  65. do_test 2.0 {
  66. db eval {
  67. CREATE TABLE t31(a,b); CREATE INDEX t31ab ON t31(a,b);
  68. CREATE TABLE t32(c,d); CREATE INDEX t32cd ON t32(c,d);
  69. CREATE TABLE t33(e,f); CREATE INDEX t33ef ON t33(e,f);
  70. CREATE TABLE t34(g,h); CREATE INDEX t34gh ON t34(g,h);
  71. INSERT INTO t31 VALUES(1,4), (2,3), (1,3);
  72. INSERT INTO t32 VALUES(4,5), (3,6), (3,7), (4,8);
  73. INSERT INTO t33 VALUES(5,9), (7,10), (6,11), (8,12), (8,13), (7,14);
  74. INSERT INTO t34 VALUES(11,20), (10,21), (12,22), (9,23), (13,24),
  75. (14,25), (12,26);
  76. SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
  77. WHERE c=b AND e=d AND g=f
  78. ORDER BY a ASC, c ASC, e DESC, g ASC;
  79. }
  80. } {1,3,7,10 1,3,7,14 1,3,6,11 1,4,8,12 1,4,8,12 1,4,8,13 1,4,5,9 2,3,7,10 2,3,7,14 2,3,6,11}
  81. do_test 2.1 {
  82. db eval {
  83. SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
  84. WHERE c=b AND e=d AND g=f
  85. ORDER BY +a ASC, +c ASC, +e DESC, +g ASC;
  86. }
  87. } {1,3,7,10 1,3,7,14 1,3,6,11 1,4,8,12 1,4,8,12 1,4,8,13 1,4,5,9 2,3,7,10 2,3,7,14 2,3,6,11}
  88. do_test 2.2 {
  89. db eval {
  90. SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
  91. WHERE c=b AND e=d AND g=f
  92. ORDER BY a ASC, c ASC, e ASC, g ASC;
  93. }
  94. } {1,3,6,11 1,3,7,10 1,3,7,14 1,4,5,9 1,4,8,12 1,4,8,12 1,4,8,13 2,3,6,11 2,3,7,10 2,3,7,14}
  95. do_test 2.3 {
  96. optimization_control db cover-idx-scan off
  97. db cache flush
  98. db eval {
  99. SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
  100. WHERE c=b AND e=d AND g=f
  101. ORDER BY a ASC, c ASC, e ASC, g ASC;
  102. }
  103. } {1,3,6,11 1,3,7,10 1,3,7,14 1,4,5,9 1,4,8,12 1,4,8,12 1,4,8,13 2,3,6,11 2,3,7,10 2,3,7,14}
  104. optimization_control db all on
  105. db cache flush
  106. finish_test