select5.test 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.6 2001/10/15 00:44:36 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Build some test data
  19. #
  20. set fd [open data1.txt w]
  21. for {set i 1} {$i<32} {incr i} {
  22. for {set j 0} {pow(2,$j)<$i} {incr j} {}
  23. puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
  24. }
  25. close $fd
  26. execsql {
  27. CREATE TABLE t1(x int, y int);
  28. COPY t1 FROM 'data1.txt'
  29. }
  30. file delete data1.txt
  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 {
  48. set v [catch {execsql {
  49. SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
  50. }} msg]
  51. lappend v $msg
  52. } {1 {no such column: z}}
  53. do_test select5-2.2 {
  54. set v [catch {execsql {
  55. SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
  56. }} msg]
  57. lappend v $msg
  58. } {1 {no such function: z}}
  59. do_test select5-2.3 {
  60. set v [catch {execsql {
  61. SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
  62. }} msg]
  63. lappend v $msg
  64. } {0 {8 2 9 1 10 1}}
  65. do_test select5-2.4 {
  66. set v [catch {execsql {
  67. SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
  68. }} msg]
  69. lappend v $msg
  70. } {1 {no such function: z}}
  71. do_test select5-2.5 {
  72. set v [catch {execsql {
  73. SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
  74. }} msg]
  75. lappend v $msg
  76. } {1 {no such column: z}}
  77. # Get the Agg function to rehash in vdbe.c
  78. #
  79. do_test select5-3.1 {
  80. execsql {
  81. SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
  82. }
  83. } {1 1 5 2 1 5 3 1 5}
  84. # Run various aggregate functions when the count is zero.
  85. #
  86. do_test select5-4.1 {
  87. execsql {
  88. SELECT avg(x) FROM t1 WHERE x>100
  89. }
  90. } {{}}
  91. do_test select5-4.2 {
  92. execsql {
  93. SELECT count(x) FROM t1 WHERE x>100
  94. }
  95. } {0}
  96. do_test select5-4.3 {
  97. execsql {
  98. SELECT min(x) FROM t1 WHERE x>100
  99. }
  100. } {{}}
  101. do_test select5-4.4 {
  102. execsql {
  103. SELECT max(x) FROM t1 WHERE x>100
  104. }
  105. } {{}}
  106. do_test select5-4.5 {
  107. execsql {
  108. SELECT sum(x) FROM t1 WHERE x>100
  109. }
  110. } {0}
  111. finish_test