PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/test/subselect.test

https://bitbucket.org/aware/sqlite
Unknown | 210 lines | 197 code | 13 blank | 0 comment | 0 complexity | 8b1e3e5af389d7e01061d2329c1f3869 MD5 | raw file
  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 are part of
  13. # expressions.
  14. #
  15. # $Id:$
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Omit this whole file if the library is build without subquery support.
  19. ifcapable !subquery {
  20. finish_test
  21. return
  22. }
  23. # Basic sanity checking. Try a simple subselect.
  24. #
  25. do_test subselect-1.1 {
  26. execsql {
  27. CREATE TABLE t1(a int, b int);
  28. INSERT INTO t1 VALUES(1,2);
  29. INSERT INTO t1 VALUES(3,4);
  30. INSERT INTO t1 VALUES(5,6);
  31. }
  32. execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
  33. } {3 4}
  34. # Try a select with more than one result column.
  35. #
  36. do_test subselect-1.2 {
  37. set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
  38. lappend v $msg
  39. } {1 {only a single result allowed for a SELECT that is part of an expression}}
  40. # A subselect without an aggregate.
  41. #
  42. do_test subselect-1.3a {
  43. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
  44. } {2}
  45. do_test subselect-1.3b {
  46. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
  47. } {4}
  48. do_test subselect-1.3c {
  49. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
  50. } {6}
  51. do_test subselect-1.3d {
  52. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
  53. } {}
  54. ifcapable compound {
  55. do_test subselect-1.3e {
  56. execsql {
  57. SELECT b FROM t1
  58. WHERE a = (SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY 1);
  59. }
  60. } {2}
  61. }
  62. # What if the subselect doesn't return any value. We should get
  63. # NULL as the result. Check it out.
  64. #
  65. do_test subselect-1.4 {
  66. execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
  67. } {2}
  68. # Try multiple subselects within a single expression.
  69. #
  70. do_test subselect-1.5 {
  71. execsql {
  72. CREATE TABLE t2(x int, y int);
  73. INSERT INTO t2 VALUES(1,2);
  74. INSERT INTO t2 VALUES(2,4);
  75. INSERT INTO t2 VALUES(3,8);
  76. INSERT INTO t2 VALUES(4,16);
  77. }
  78. execsql {
  79. SELECT y from t2
  80. WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
  81. }
  82. } {8}
  83. # Try something useful. Delete every entry from t2 where the
  84. # x value is less than half of the maximum.
  85. #
  86. do_test subselect-1.6 {
  87. execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
  88. execsql {SELECT x FROM t2 ORDER BY x}
  89. } {2 3 4}
  90. # Make sure sorting works for SELECTs there used as a scalar expression.
  91. #
  92. do_test subselect-2.1 {
  93. execsql {
  94. SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
  95. }
  96. } {1 5}
  97. do_test subselect-2.2 {
  98. execsql {
  99. SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
  100. }
  101. } {1}
  102. do_test subselect-2.3 {
  103. execsql {
  104. SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
  105. }
  106. } {0}
  107. # Verify that the ORDER BY clause is honored in a subquery.
  108. #
  109. ifcapable compound {
  110. do_test subselect-3.1 {
  111. execsql {
  112. CREATE TABLE t3(x int);
  113. INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
  114. SELECT * FROM t3 ORDER BY x;
  115. }
  116. } {1 2 3 4 5 6}
  117. } ;# ifcapable compound
  118. ifcapable !compound {
  119. do_test subselect-3.1 {
  120. execsql {
  121. CREATE TABLE t3(x int);
  122. INSERT INTO t3 SELECT a FROM t1;
  123. INSERT INTO t3 SELECT b FROM t1;
  124. SELECT * FROM t3 ORDER BY x;
  125. }
  126. } {1 2 3 4 5 6}
  127. } ;# ifcapable !compound
  128. do_test subselect-3.2 {
  129. execsql {
  130. SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
  131. }
  132. } {3}
  133. do_test subselect-3.3 {
  134. execsql {
  135. SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
  136. }
  137. } {11}
  138. do_test subselect-3.4 {
  139. execsql {
  140. SELECT (SELECT x FROM t3 ORDER BY x);
  141. }
  142. } {1}
  143. do_test subselect-3.5 {
  144. execsql {
  145. SELECT (SELECT x FROM t3 ORDER BY x DESC);
  146. }
  147. } {6}
  148. do_test subselect-3.6 {
  149. execsql {
  150. SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
  151. }
  152. } {1}
  153. do_test subselect-3.7 {
  154. execsql {
  155. SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
  156. }
  157. } {6}
  158. do_test subselect-3.8 {
  159. execsql {
  160. SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
  161. }
  162. } {3}
  163. do_test subselect-3.9 {
  164. execsql {
  165. SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
  166. }
  167. } {4}
  168. do_test subselect-3.10 {
  169. execsql {
  170. SELECT x FROM t3 WHERE x IN
  171. (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
  172. }
  173. } {4}
  174. # Ticket #2295.
  175. # Make sure type affinities work correctly on subqueries with
  176. # an ORDER BY clause.
  177. #
  178. do_test subselect-4.1 {
  179. execsql {
  180. CREATE TABLE t4(a TEXT, b TEXT);
  181. INSERT INTO t4 VALUES('a','1');
  182. INSERT INTO t4 VALUES('b','2');
  183. INSERT INTO t4 VALUES('c','3');
  184. SELECT a FROM t4 WHERE b IN (SELECT b FROM t4 ORDER BY b);
  185. }
  186. } {a b c}
  187. do_test subselect-4.2 {
  188. execsql {
  189. SELECT a FROM t4 WHERE b IN (SELECT b FROM t4 ORDER BY b LIMIT 1);
  190. }
  191. } {a}
  192. do_test subselect-4.3 {
  193. execsql {
  194. SELECT a FROM t4 WHERE b IN (SELECT b FROM t4 ORDER BY b DESC LIMIT 1);
  195. }
  196. } {c}
  197. finish_test