alias.test 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # 2008 August 28
  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. # This file implements regression tests for SQLite library. The
  13. # focus of this script is correct code generation of aliased result-set
  14. # values. See ticket #3343.
  15. #
  16. # $Id: alias.test,v 1.3 2009/04/23 13:22:44 drh Exp $
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # Aliases are currently evaluated twice. We might try to change this
  21. # in the future. But not now.
  22. return
  23. # A procedure to return a sequence of increasing integers.
  24. #
  25. namespace eval ::seq {
  26. variable counter 0
  27. proc value {args} {
  28. variable counter
  29. incr counter
  30. return $counter
  31. }
  32. proc reset {} {
  33. variable counter
  34. set counter 0
  35. }
  36. }
  37. do_test alias-1.1 {
  38. db function sequence ::seq::value
  39. db eval {
  40. CREATE TABLE t1(x);
  41. INSERT INTO t1 VALUES(9);
  42. INSERT INTO t1 VALUES(8);
  43. INSERT INTO t1 VALUES(7);
  44. SELECT x, sequence() FROM t1;
  45. }
  46. } {9 1 8 2 7 3}
  47. do_test alias-1.2 {
  48. ::seq::reset
  49. db eval {
  50. SELECT x, sequence() AS y FROM t1 WHERE y>0
  51. }
  52. } {9 1 8 2 7 3}
  53. do_test alias-1.3 {
  54. ::seq::reset
  55. db eval {
  56. SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99
  57. }
  58. } {9 1 8 2 7 3}
  59. do_test alias-1.4 {
  60. ::seq::reset
  61. db eval {
  62. SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99 AND y!=55
  63. }
  64. } {9 1 8 2 7 3}
  65. do_test alias-1.5 {
  66. ::seq::reset
  67. db eval {
  68. SELECT x, sequence() AS y FROM t1
  69. WHERE y>0 AND y<99 AND y!=55 AND y NOT IN (56,57,58)
  70. AND y NOT LIKE 'abc%' AND y%10==2
  71. }
  72. } {8 2}
  73. do_test alias-1.6 {
  74. ::seq::reset
  75. db eval {
  76. SELECT x, sequence() AS y FROM t1 WHERE y BETWEEN 0 AND 99
  77. }
  78. } {9 1 8 2 7 3}
  79. #do_test alias-1.7 {
  80. # ::seq::reset
  81. # db eval {
  82. # SELECT x, sequence() AS y FROM t1 WHERE y IN (55,66,3)
  83. # }
  84. #} {7 3}
  85. do_test alias-1.8 {
  86. ::seq::reset
  87. db eval {
  88. SELECT x, 1-sequence() AS y FROM t1 ORDER BY y
  89. }
  90. } {7 -2 8 -1 9 0}
  91. do_test alias-1.9 {
  92. ::seq::reset
  93. db eval {
  94. SELECT x, sequence() AS y FROM t1 ORDER BY -y
  95. }
  96. } {7 3 8 2 9 1}
  97. do_test alias-1.10 {
  98. ::seq::reset
  99. db eval {
  100. SELECT x, sequence() AS y FROM t1 ORDER BY x%2, y
  101. }
  102. } {8 2 9 1 7 3}
  103. unset -nocomplain random_int_list
  104. set random_int_list [db eval {
  105. SELECT random()&2147483647 AS r FROM t1, t1, t1, t1 ORDER BY r
  106. }]
  107. do_test alias-1.11 {
  108. lsort -integer $::random_int_list
  109. } $random_int_list
  110. do_test alias-2.1 {
  111. db eval {
  112. SELECT 4 UNION SELECT 1 ORDER BY 1
  113. }
  114. } {1 4}
  115. do_test alias-2.2 {
  116. db eval {
  117. SELECT 4 UNION SELECT 1 UNION SELECT 9 ORDER BY 1
  118. }
  119. } {1 4 9}
  120. if 0 {
  121. # Aliases in the GROUP BY clause cause the expression to be evaluated
  122. # twice in the current implementation. This might change in the future.
  123. #
  124. do_test alias-3.1 {
  125. ::seq::reset
  126. db eval {
  127. SELECT sequence(*) AS y, count(*) AS z FROM t1 GROUP BY y ORDER BY z, y
  128. }
  129. } {1 1 2 1 3 1}
  130. }
  131. finish_test