cse.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # 2008 April 1
  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. #
  12. # Test cases designed to exercise and verify the logic for
  13. # factoring constant expressions out of loops and for
  14. # common subexpression eliminations.
  15. #
  16. # $Id: cse.test,v 1.6 2008/08/04 03:51:24 danielk1977 Exp $
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. do_test cse-1.1 {
  21. execsql {
  22. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d, e, f);
  23. INSERT INTO t1 VALUES(1,11,12,13,14,15);
  24. INSERT INTO t1 VALUES(2,21,22,23,24,25);
  25. }
  26. execsql {
  27. SELECT b, -b, ~b, NOT b, NOT NOT b, b-b, b+b, b*b, b/b, b FROM t1
  28. }
  29. } {11 -11 -12 0 1 0 22 121 1 11 21 -21 -22 0 1 0 42 441 1 21}
  30. do_test cse-1.2 {
  31. execsql {
  32. SELECT b, b%b, b==b, b!=b, b<b, b<=b, b IS NULL, b NOT NULL, b FROM t1
  33. }
  34. } {11 0 1 0 0 1 0 1 11 21 0 1 0 0 1 0 1 21}
  35. do_test cse-1.3 {
  36. execsql {
  37. SELECT b, abs(b), coalesce(b,-b,NOT b,c,NOT c), c, -c FROM t1;
  38. }
  39. } {11 11 11 12 -12 21 21 21 22 -22}
  40. do_test cse-1.4 {
  41. execsql {
  42. SELECT CASE WHEN a==1 THEN b ELSE c END, b, c FROM t1
  43. }
  44. } {11 11 12 22 21 22}
  45. do_test cse-1.5 {
  46. execsql {
  47. SELECT CASE a WHEN 1 THEN b WHEN 2 THEN c ELSE d END, b, c, d FROM t1
  48. }
  49. } {11 11 12 13 22 21 22 23}
  50. do_test cse-1.6.1 {
  51. execsql {
  52. SELECT CASE b WHEN 11 THEN -b WHEN 21 THEN -c ELSE -d END, b, c, d FROM t1
  53. }
  54. } {-11 11 12 13 -22 21 22 23}
  55. do_test cse-1.6.2 {
  56. execsql {
  57. SELECT CASE b+1 WHEN c THEN d WHEN e THEN f ELSE 999 END, b, c, d FROM t1
  58. }
  59. } {13 11 12 13 23 21 22 23}
  60. do_test cse-1.6.3 {
  61. execsql {
  62. SELECT CASE WHEN b THEN d WHEN e THEN f ELSE 999 END, b, c, d FROM t1
  63. }
  64. } {13 11 12 13 23 21 22 23}
  65. do_test cse-1.6.4 {
  66. execsql {
  67. SELECT b, c, d, CASE WHEN b THEN d WHEN e THEN f ELSE 999 END FROM t1
  68. }
  69. } {11 12 13 13 21 22 23 23}
  70. do_test cse-1.6.5 {
  71. execsql {
  72. SELECT b, c, d, CASE WHEN 0 THEN d WHEN e THEN f ELSE 999 END FROM t1
  73. }
  74. } {11 12 13 15 21 22 23 25}
  75. do_test cse-1.7 {
  76. execsql {
  77. SELECT a, -a, ~a, NOT a, NOT NOT a, a-a, a+a, a*a, a/a, a FROM t1
  78. }
  79. } {1 -1 -2 0 1 0 2 1 1 1 2 -2 -3 0 1 0 4 4 1 2}
  80. do_test cse-1.8 {
  81. execsql {
  82. SELECT a, a%a, a==a, a!=a, a<a, a<=a, a IS NULL, a NOT NULL, a FROM t1
  83. }
  84. } {1 0 1 0 0 1 0 1 1 2 0 1 0 0 1 0 1 2}
  85. do_test cse-1.9 {
  86. execsql {
  87. SELECT NOT b, ~b, NOT NOT b, b FROM t1
  88. }
  89. } {0 -12 1 11 0 -22 1 21}
  90. do_test cse-1.10 {
  91. execsql {
  92. SELECT CAST(b AS integer), typeof(b), CAST(b AS text), typeof(b) FROM t1
  93. }
  94. } {11 integer 11 integer 21 integer 21 integer}
  95. ifcapable compound {
  96. do_test cse-1.11 {
  97. execsql {
  98. SELECT *,* FROM t1 WHERE a=2
  99. UNION ALL
  100. SELECT *,* FROM t1 WHERE a=1
  101. }
  102. } {2 21 22 23 24 25 2 21 22 23 24 25 1 11 12 13 14 15 1 11 12 13 14 15}
  103. do_test cse-1.12 {
  104. execsql {
  105. SELECT coalesce(b,c,d,e), a, b, c, d, e FROM t1 WHERE a=2
  106. UNION ALL
  107. SELECT coalesce(e,d,c,b), e, d, c, b, a FROM t1 WHERE a=1
  108. }
  109. } {21 2 21 22 23 24 14 14 13 12 11 1}
  110. }
  111. do_test cse-1.13 {
  112. execsql {
  113. SELECT upper(b), typeof(b), b FROM t1
  114. }
  115. } {11 integer 11 21 integer 21}
  116. do_test cse-1.14 {
  117. execsql {
  118. SELECT b, typeof(b), upper(b), typeof(b), b FROM t1
  119. }
  120. } {11 integer 11 integer 11 21 integer 21 integer 21}
  121. # Overflow the column cache. Create queries involving more and more
  122. # columns until the cache overflows. Verify correct operation throughout.
  123. #
  124. do_test cse-2.1 {
  125. execsql {
  126. CREATE TABLE t2(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,
  127. a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,
  128. a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,
  129. a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,
  130. a40,a41,a42,a43,a44,a45,a46,a47,a48,a49);
  131. INSERT INTO t2 VALUES(0,1,2,3,4,5,6,7,8,9,
  132. 10,11,12,13,14,15,16,17,18,19,
  133. 20,21,22,23,24,25,26,27,28,29,
  134. 30,31,32,33,34,35,36,37,38,39,
  135. 40,41,42,43,44,45,46,47,48,49);
  136. SELECT * FROM t2;
  137. }
  138. } {0 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}
  139. for {set i 1} {$i<100} {incr i} {
  140. set n [expr {int(rand()*44)+5}]
  141. set colset {}
  142. set answer {}
  143. for {set j 0} {$j<$n} {incr j} {
  144. set r [expr {$j+int(rand()*5)}]
  145. if {$r>49} {set r [expr {99-$r}]}
  146. lappend colset a$j a$r
  147. lappend answer $j $r
  148. }
  149. set sql "SELECT [join $colset ,] FROM t2"
  150. do_test cse-2.2.$i {
  151. # explain $::sql
  152. execsql $::sql
  153. } $answer
  154. }
  155. finish_test