count.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # 2009 February 24
  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 "SELECT count(*)" statements.
  13. #
  14. # $Id: count.test,v 1.6 2009/06/05 17:09:12 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Test plan:
  18. #
  19. # count-0.*: Make sure count(*) works on an empty database. (Ticket #3774)
  20. #
  21. # count-1.*: Test that the OP_Count instruction appears to work on both
  22. # tables and indexes. Test both when they contain 0 entries,
  23. # when all entries are on the root page, and when the b-tree
  24. # forms a structure 2 and 3 levels deep.
  25. #
  26. # count-2.*: Test that
  27. #
  28. #
  29. do_test count-0.1 {
  30. db eval {
  31. SELECT count(*) FROM sqlite_master;
  32. }
  33. } {0}
  34. set iTest 0
  35. foreach zIndex [list {
  36. /* no-op */
  37. } {
  38. CREATE INDEX i1 ON t1(a);
  39. }] {
  40. incr iTest
  41. do_test count-1.$iTest.1 {
  42. execsql {
  43. DROP TABLE IF EXISTS t1;
  44. CREATE TABLE t1(a, b);
  45. }
  46. execsql $zIndex
  47. execsql { SELECT count(*) FROM t1 }
  48. } {0}
  49. do_test count-1.$iTest.2 {
  50. execsql {
  51. INSERT INTO t1 VALUES(1, 2);
  52. INSERT INTO t1 VALUES(3, 4);
  53. SELECT count(*) FROM t1;
  54. }
  55. } {2}
  56. do_test count-1.$iTest.3 {
  57. execsql {
  58. INSERT INTO t1 SELECT * FROM t1; -- 4
  59. INSERT INTO t1 SELECT * FROM t1; -- 8
  60. INSERT INTO t1 SELECT * FROM t1; -- 16
  61. INSERT INTO t1 SELECT * FROM t1; -- 32
  62. INSERT INTO t1 SELECT * FROM t1; -- 64
  63. INSERT INTO t1 SELECT * FROM t1; -- 128
  64. INSERT INTO t1 SELECT * FROM t1; -- 256
  65. SELECT count(*) FROM t1;
  66. }
  67. } {256}
  68. do_test count-1.$iTest.4 {
  69. execsql {
  70. INSERT INTO t1 SELECT * FROM t1; -- 512
  71. INSERT INTO t1 SELECT * FROM t1; -- 1024
  72. INSERT INTO t1 SELECT * FROM t1; -- 2048
  73. INSERT INTO t1 SELECT * FROM t1; -- 4096
  74. SELECT count(*) FROM t1;
  75. }
  76. } {4096}
  77. do_test count-1.$iTest.5 {
  78. execsql {
  79. BEGIN;
  80. INSERT INTO t1 SELECT * FROM t1; -- 8192
  81. INSERT INTO t1 SELECT * FROM t1; -- 16384
  82. INSERT INTO t1 SELECT * FROM t1; -- 32768
  83. INSERT INTO t1 SELECT * FROM t1; -- 65536
  84. COMMIT;
  85. SELECT count(*) FROM t1;
  86. }
  87. } {65536}
  88. }
  89. proc uses_op_count {sql} {
  90. if {[lsearch [execsql "EXPLAIN $sql"] Count]>=0} {
  91. return 1;
  92. }
  93. return 0
  94. }
  95. do_test count-2.1 {
  96. execsql {
  97. CREATE TABLE t2(a, b);
  98. }
  99. uses_op_count {SELECT count(*) FROM t2}
  100. } {1}
  101. do_test count-2.2 {
  102. catchsql {SELECT count(DISTINCT *) FROM t2}
  103. } {1 {near "*": syntax error}}
  104. do_test count-2.3 {
  105. uses_op_count {SELECT count(DISTINCT a) FROM t2}
  106. } {0}
  107. do_test count-2.4 {
  108. uses_op_count {SELECT count(a) FROM t2}
  109. } {0}
  110. do_test count-2.5 {
  111. uses_op_count {SELECT count() FROM t2}
  112. } {1}
  113. do_test count-2.6 {
  114. catchsql {SELECT count(DISTINCT) FROM t2}
  115. } {1 {DISTINCT aggregates must have exactly one argument}}
  116. do_test count-2.7 {
  117. uses_op_count {SELECT count(*)+1 FROM t2}
  118. } {0}
  119. do_test count-2.8 {
  120. uses_op_count {SELECT count(*) FROM t2 WHERE a IS NOT NULL}
  121. } {0}
  122. do_test count-2.9 {
  123. catchsql {SELECT count(*) FROM t2 HAVING count(*)>1}
  124. } {1 {a GROUP BY clause is required before HAVING}}
  125. do_test count-2.10 {
  126. uses_op_count {SELECT count(*) FROM (SELECT 1)}
  127. } {0}
  128. do_test count-2.11 {
  129. execsql { CREATE VIEW v1 AS SELECT 1 AS a }
  130. uses_op_count {SELECT count(*) FROM v1}
  131. } {0}
  132. do_test count-2.12 {
  133. uses_op_count {SELECT count(*), max(a) FROM t2}
  134. } {0}
  135. do_test count-2.13 {
  136. uses_op_count {SELECT count(*) FROM t1, t2}
  137. } {0}
  138. ifcapable vtab {
  139. register_echo_module [sqlite3_connection_pointer db]
  140. do_test count-2.14 {
  141. execsql { CREATE VIRTUAL TABLE techo USING echo(t1); }
  142. uses_op_count {SELECT count(*) FROM techo}
  143. } {0}
  144. }
  145. do_test count-3.1 {
  146. execsql {
  147. CREATE TABLE t3(a, b);
  148. SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==0;
  149. }
  150. } {0}
  151. do_test count-3.2 {
  152. execsql {
  153. SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==1;
  154. }
  155. } {}
  156. do_test count-4.1 {
  157. execsql {
  158. CREATE TABLE t4(a, b);
  159. INSERT INTO t4 VALUES('a', 'b');
  160. CREATE INDEX t4i1 ON t4(b, a);
  161. SELECT count(*) FROM t4;
  162. }
  163. } {1}
  164. do_test count-4.2 {
  165. execsql {
  166. CREATE INDEX t4i2 ON t4(b);
  167. SELECT count(*) FROM t4;
  168. }
  169. } {1}
  170. do_test count-4.3 {
  171. execsql {
  172. DROP INDEX t4i1;
  173. CREATE INDEX t4i1 ON t4(b, a);
  174. SELECT count(*) FROM t4;
  175. }
  176. } {1}
  177. finish_test