select5.test 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # 2001 September 15
  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 aggregate functions and the
  13. # GROUP BY and HAVING clauses of SELECT statements.
  14. #
  15. # $Id: select5.test,v 1.20 2008/08/21 14:15:59 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Build some test data
  19. #
  20. execsql {
  21. CREATE TABLE t1(x int, y int);
  22. BEGIN;
  23. }
  24. for {set i 1} {$i<32} {incr i} {
  25. for {set j 0} {(1<<$j)<$i} {incr j} {}
  26. execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])"
  27. }
  28. execsql {
  29. COMMIT
  30. }
  31. do_test select5-1.0 {
  32. execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
  33. } {5 6 7 8 9 10}
  34. # Sort by an aggregate function.
  35. #
  36. do_test select5-1.1 {
  37. execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
  38. } {5 15 6 8 7 4 8 2 9 1 10 1}
  39. do_test select5-1.2 {
  40. execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
  41. } {9 1 10 1 8 2 7 4 6 8 5 15}
  42. do_test select5-1.3 {
  43. execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
  44. } {1 9 1 10 2 8 4 7 8 6 15 5}
  45. # Some error messages associated with aggregates and GROUP BY
  46. #
  47. do_test select5-2.1.1 {
  48. catchsql {
  49. SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
  50. }
  51. } {1 {no such column: z}}
  52. do_test select5-2.1.2 {
  53. catchsql {
  54. SELECT y, count(*) FROM t1 GROUP BY temp.t1.y ORDER BY y
  55. }
  56. } {1 {no such column: temp.t1.y}}
  57. do_test select5-2.2 {
  58. set v [catch {execsql {
  59. SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
  60. }} msg]
  61. lappend v $msg
  62. } {1 {no such function: z}}
  63. do_test select5-2.3 {
  64. set v [catch {execsql {
  65. SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
  66. }} msg]
  67. lappend v $msg
  68. } {0 {8 2 9 1 10 1}}
  69. do_test select5-2.4 {
  70. set v [catch {execsql {
  71. SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
  72. }} msg]
  73. lappend v $msg
  74. } {1 {no such function: z}}
  75. do_test select5-2.5 {
  76. set v [catch {execsql {
  77. SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
  78. }} msg]
  79. lappend v $msg
  80. } {1 {no such column: z}}
  81. # Get the Agg function to rehash in vdbe.c
  82. #
  83. do_test select5-3.1 {
  84. execsql {
  85. SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
  86. }
  87. } {1 1 5.0 2 1 5.0 3 1 5.0}
  88. # Run various aggregate functions when the count is zero.
  89. #
  90. do_test select5-4.1 {
  91. execsql {
  92. SELECT avg(x) FROM t1 WHERE x>100
  93. }
  94. } {{}}
  95. do_test select5-4.2 {
  96. execsql {
  97. SELECT count(x) FROM t1 WHERE x>100
  98. }
  99. } {0}
  100. do_test select5-4.3 {
  101. execsql {
  102. SELECT min(x) FROM t1 WHERE x>100
  103. }
  104. } {{}}
  105. do_test select5-4.4 {
  106. execsql {
  107. SELECT max(x) FROM t1 WHERE x>100
  108. }
  109. } {{}}
  110. do_test select5-4.5 {
  111. execsql {
  112. SELECT sum(x) FROM t1 WHERE x>100
  113. }
  114. } {{}}
  115. # Some tests for queries with a GROUP BY clause but no aggregate functions.
  116. #
  117. # Note: The query in test cases 5.1 through 5.5 are not legal SQL. So if the
  118. # implementation changes in the future and it returns different results,
  119. # this is not such a big deal.
  120. #
  121. do_test select5-5.1 {
  122. execsql {
  123. CREATE TABLE t2(a, b, c);
  124. INSERT INTO t2 VALUES(1, 2, 3);
  125. INSERT INTO t2 VALUES(1, 4, 5);
  126. INSERT INTO t2 VALUES(6, 4, 7);
  127. CREATE INDEX t2_idx ON t2(a);
  128. }
  129. } {}
  130. do_test select5-5.2 {
  131. execsql {
  132. SELECT a FROM t2 GROUP BY a;
  133. }
  134. } {1 6}
  135. do_test select5-5.3 {
  136. execsql {
  137. SELECT a FROM t2 WHERE a>2 GROUP BY a;
  138. }
  139. } {6}
  140. do_test select5-5.4 {
  141. execsql {
  142. SELECT a, b FROM t2 GROUP BY a, b;
  143. }
  144. } {1 2 1 4 6 4}
  145. do_test select5-5.5 {
  146. execsql {
  147. SELECT a, b FROM t2 GROUP BY a;
  148. }
  149. } {1 4 6 4}
  150. # Test rendering of columns for the GROUP BY clause.
  151. #
  152. do_test select5-5.11 {
  153. execsql {
  154. SELECT max(c), b*a, b, a FROM t2 GROUP BY b*a, b, a
  155. }
  156. } {3 2 2 1 5 4 4 1 7 24 4 6}
  157. # NULL compare equal to each other for the purposes of processing
  158. # the GROUP BY clause.
  159. #
  160. do_test select5-6.1 {
  161. execsql {
  162. CREATE TABLE t3(x,y);
  163. INSERT INTO t3 VALUES(1,NULL);
  164. INSERT INTO t3 VALUES(2,NULL);
  165. INSERT INTO t3 VALUES(3,4);
  166. SELECT count(x), y FROM t3 GROUP BY y ORDER BY 1
  167. }
  168. } {1 4 2 {}}
  169. do_test select5-6.2 {
  170. execsql {
  171. CREATE TABLE t4(x,y,z);
  172. INSERT INTO t4 VALUES(1,2,NULL);
  173. INSERT INTO t4 VALUES(2,3,NULL);
  174. INSERT INTO t4 VALUES(3,NULL,5);
  175. INSERT INTO t4 VALUES(4,NULL,6);
  176. INSERT INTO t4 VALUES(4,NULL,6);
  177. INSERT INTO t4 VALUES(5,NULL,NULL);
  178. INSERT INTO t4 VALUES(5,NULL,NULL);
  179. INSERT INTO t4 VALUES(6,7,8);
  180. SELECT max(x), count(x), y, z FROM t4 GROUP BY y, z ORDER BY 1
  181. }
  182. } {1 1 2 {} 2 1 3 {} 3 1 {} 5 4 2 {} 6 5 2 {} {} 6 1 7 8}
  183. do_test select5-7.2 {
  184. execsql {
  185. SELECT count(*), count(x) as cnt FROM t4 GROUP BY y ORDER BY cnt;
  186. }
  187. } {1 1 1 1 1 1 5 5}
  188. # See ticket #3324.
  189. #
  190. do_test select5-8.1 {
  191. execsql {
  192. CREATE TABLE t8a(a,b);
  193. CREATE TABLE t8b(x);
  194. INSERT INTO t8a VALUES('one', 1);
  195. INSERT INTO t8a VALUES('one', 2);
  196. INSERT INTO t8a VALUES('two', 3);
  197. INSERT INTO t8a VALUES('one', NULL);
  198. INSERT INTO t8b(rowid,x) VALUES(1,111);
  199. INSERT INTO t8b(rowid,x) VALUES(2,222);
  200. INSERT INTO t8b(rowid,x) VALUES(3,333);
  201. SELECT a, count(b) FROM t8a, t8b WHERE b=t8b.rowid GROUP BY a ORDER BY a;
  202. }
  203. } {one 2 two 1}
  204. do_test select5-8.2 {
  205. execsql {
  206. SELECT a, count(b) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a;
  207. }
  208. } {one 2 two 1}
  209. do_test select5-8.3 {
  210. execsql {
  211. SELECT t8a.a, count(t8a.b) FROM t8a, t8b WHERE t8a.b=t8b.rowid
  212. GROUP BY 1 ORDER BY 1;
  213. }
  214. } {one 2 two 1}
  215. do_test select5-8.4 {
  216. execsql {
  217. SELECT a, count(*) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a;
  218. }
  219. } {one 2 two 1}
  220. do_test select5-8.5 {
  221. execsql {
  222. SELECT a, count(b) FROM t8a, t8b WHERE b<x GROUP BY a ORDER BY a;
  223. }
  224. } {one 6 two 3}
  225. do_test select5-8.6 {
  226. execsql {
  227. SELECT a, count(t8a.b) FROM t8a, t8b WHERE b=t8b.rowid
  228. GROUP BY a ORDER BY 2;
  229. }
  230. } {two 1 one 2}
  231. do_test select5-8.7 {
  232. execsql {
  233. SELECT a, count(b) FROM t8a, t8b GROUP BY a ORDER BY 2;
  234. }
  235. } {two 3 one 6}
  236. do_test select5-8.8 {
  237. execsql {
  238. SELECT a, count(*) FROM t8a, t8b GROUP BY a ORDER BY 2;
  239. }
  240. } {two 3 one 9}
  241. finish_test