minmax.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 SELECT statements that contain
  13. # aggregate min() and max() functions and which are handled as
  14. # as a special case.
  15. #
  16. # $Id: minmax.test,v 1.4 2002/05/31 15:51:26 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. do_test minmax-1.0 {
  20. execsql {
  21. BEGIN;
  22. CREATE TABLE t1(x, y);
  23. INSERT INTO t1 VALUES(1,1);
  24. INSERT INTO t1 VALUES(2,2);
  25. INSERT INTO t1 VALUES(3,2);
  26. INSERT INTO t1 VALUES(4,3);
  27. INSERT INTO t1 VALUES(5,3);
  28. INSERT INTO t1 VALUES(6,3);
  29. INSERT INTO t1 VALUES(7,3);
  30. INSERT INTO t1 VALUES(8,4);
  31. INSERT INTO t1 VALUES(9,4);
  32. INSERT INTO t1 VALUES(10,4);
  33. INSERT INTO t1 VALUES(11,4);
  34. INSERT INTO t1 VALUES(12,4);
  35. INSERT INTO t1 VALUES(13,4);
  36. INSERT INTO t1 VALUES(14,4);
  37. INSERT INTO t1 VALUES(15,4);
  38. INSERT INTO t1 VALUES(16,5);
  39. INSERT INTO t1 VALUES(17,5);
  40. INSERT INTO t1 VALUES(18,5);
  41. INSERT INTO t1 VALUES(19,5);
  42. INSERT INTO t1 VALUES(20,5);
  43. COMMIT;
  44. SELECT DISTINCT y FROM t1 ORDER BY y;
  45. }
  46. } {1 2 3 4 5}
  47. do_test minmax-1.1 {
  48. set sqlite_search_count 0
  49. execsql {SELECT min(x) FROM t1}
  50. } {1}
  51. do_test minmax-1.2 {
  52. set sqlite_search_count
  53. } {19}
  54. do_test minmax-1.3 {
  55. set sqlite_search_count 0
  56. execsql {SELECT max(x) FROM t1}
  57. } {20}
  58. do_test minmax-1.4 {
  59. set sqlite_search_count
  60. } {19}
  61. do_test minmax-1.5 {
  62. execsql {CREATE INDEX t1i1 ON t1(x)}
  63. set sqlite_search_count 0
  64. execsql {SELECT min(x) FROM t1}
  65. } {1}
  66. do_test minmax-1.6 {
  67. set sqlite_search_count
  68. } {1}
  69. do_test minmax-1.7 {
  70. set sqlite_search_count 0
  71. execsql {SELECT max(x) FROM t1}
  72. } {20}
  73. do_test minmax-1.8 {
  74. set sqlite_search_count
  75. } {1}
  76. do_test minmax-1.9 {
  77. set sqlite_search_count 0
  78. execsql {SELECT max(y) FROM t1}
  79. } {5}
  80. do_test minmax-1.10 {
  81. set sqlite_search_count
  82. } {19}
  83. do_test minmax-2.0 {
  84. execsql {
  85. CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
  86. INSERT INTO t2 SELECT * FROM t1;
  87. }
  88. set sqlite_search_count 0
  89. execsql {SELECT min(a) FROM t2}
  90. } {1}
  91. do_test minmax-2.1 {
  92. set sqlite_search_count
  93. } {0}
  94. do_test minmax-2.2 {
  95. set sqlite_search_count 0
  96. execsql {SELECT max(a) FROM t2}
  97. } {20}
  98. do_test minmax-2.3 {
  99. set sqlite_search_count
  100. } {0}
  101. do_test minmax-3.0 {
  102. execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
  103. set sqlite_search_count 0
  104. execsql {SELECT max(a) FROM t2}
  105. } {21}
  106. do_test minmax-3.1 {
  107. set sqlite_search_count
  108. } {0}
  109. do_test minmax-3.2 {
  110. execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
  111. set sqlite_search_count 0
  112. execsql {
  113. SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2)
  114. }
  115. } {999}
  116. do_test minmax-3.3 {
  117. set sqlite_search_count
  118. } {0}
  119. do_test minmax-4.1 {
  120. execsql {
  121. SELECT coalesce(min(x),-1), coalesce(max(x),-1) FROM
  122. (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
  123. }
  124. } {1 20}
  125. do_test minmax-4.2 {
  126. execsql {
  127. SELECT y, sum(x) FROM
  128. (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
  129. GROUP BY y ORDER BY y;
  130. }
  131. } {1 1 2 5 3 22 4 92 5 90 6 0}
  132. do_test minmax-4.3 {
  133. execsql {
  134. SELECT y, count(x), count(*) FROM
  135. (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
  136. GROUP BY y ORDER BY y;
  137. }
  138. } {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
  139. finish_test