select7.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # The author disclaims copyright to this source code. In place of
  2. # a legal notice, here is a blessing:
  3. #
  4. # May you do good and not evil.
  5. # May you find forgiveness for yourself and forgive others.
  6. # May you share freely, never taking more than you give.
  7. #
  8. #***********************************************************************
  9. # This file implements regression tests for SQLite library. The
  10. # focus of this file is testing compute SELECT statements and nested
  11. # views.
  12. #
  13. # $Id: select7.test,v 1.11 2007/09/12 17:01:45 danielk1977 Exp $
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. ifcapable compound {
  17. # A 3-way INTERSECT. Ticket #875
  18. ifcapable tempdb {
  19. do_test select7-1.1 {
  20. execsql {
  21. create temp table t1(x);
  22. insert into t1 values('amx');
  23. insert into t1 values('anx');
  24. insert into t1 values('amy');
  25. insert into t1 values('bmy');
  26. select * from t1 where x like 'a__'
  27. intersect select * from t1 where x like '_m_'
  28. intersect select * from t1 where x like '__x';
  29. }
  30. } {amx}
  31. }
  32. # Nested views do not handle * properly. Ticket #826.
  33. #
  34. ifcapable view {
  35. do_test select7-2.1 {
  36. execsql {
  37. CREATE TABLE x(id integer primary key, a TEXT NULL);
  38. INSERT INTO x (a) VALUES ('first');
  39. CREATE TABLE tempx(id integer primary key, a TEXT NULL);
  40. INSERT INTO tempx (a) VALUES ('t-first');
  41. CREATE VIEW tv1 AS SELECT x.id, tx.id FROM x JOIN tempx tx ON tx.id=x.id;
  42. CREATE VIEW tv1b AS SELECT x.id, tx.id FROM x JOIN tempx tx on tx.id=x.id;
  43. CREATE VIEW tv2 AS SELECT * FROM tv1 UNION SELECT * FROM tv1b;
  44. SELECT * FROM tv2;
  45. }
  46. } {1 1}
  47. } ;# ifcapable view
  48. } ;# ifcapable compound
  49. # Do not allow GROUP BY without an aggregate. Ticket #1039.
  50. #
  51. # Change: force any query with a GROUP BY clause to be processed as
  52. # an aggregate query, whether it contains aggregates or not.
  53. #
  54. ifcapable subquery {
  55. # do_test select7-3.1 {
  56. # catchsql {
  57. # SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
  58. # }
  59. # } {1 {GROUP BY may only be used on aggregate queries}}
  60. do_test select7-3.1 {
  61. catchsql {
  62. SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
  63. }
  64. } [list 0 [execsql {SELECT * FROM sqlite_master ORDER BY name}]]
  65. }
  66. # Ticket #2018 - Make sure names are resolved correctly on all
  67. # SELECT statements of a compound subquery.
  68. #
  69. ifcapable {subquery && compound} {
  70. do_test select7-4.1 {
  71. execsql {
  72. CREATE TABLE IF NOT EXISTS photo(pk integer primary key, x);
  73. CREATE TABLE IF NOT EXISTS tag(pk integer primary key, fk int, name);
  74. SELECT P.pk from PHOTO P WHERE NOT EXISTS (
  75. SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk
  76. EXCEPT
  77. SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
  78. );
  79. }
  80. } {}
  81. do_test select7-4.2 {
  82. execsql {
  83. INSERT INTO photo VALUES(1,1);
  84. INSERT INTO photo VALUES(2,2);
  85. INSERT INTO photo VALUES(3,3);
  86. INSERT INTO tag VALUES(11,1,'one');
  87. INSERT INTO tag VALUES(12,1,'two');
  88. INSERT INTO tag VALUES(21,1,'one-b');
  89. SELECT P.pk from PHOTO P WHERE NOT EXISTS (
  90. SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk
  91. EXCEPT
  92. SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
  93. );
  94. }
  95. } {2 3}
  96. }
  97. # ticket #2347
  98. #
  99. ifcapable {subquery && compound} {
  100. do_test select7-5.1 {
  101. catchsql {
  102. CREATE TABLE t2(a,b);
  103. SELECT 5 IN (SELECT a,b FROM t2);
  104. }
  105. } [list 1 \
  106. {only a single result allowed for a SELECT that is part of an expression}]
  107. do_test select7-5.2 {
  108. catchsql {
  109. SELECT 5 IN (SELECT * FROM t2);
  110. }
  111. } [list 1 \
  112. {only a single result allowed for a SELECT that is part of an expression}]
  113. do_test select7-5.3 {
  114. catchsql {
  115. SELECT 5 IN (SELECT a,b FROM t2 UNION SELECT b,a FROM t2);
  116. }
  117. } [list 1 \
  118. {only a single result allowed for a SELECT that is part of an expression}]
  119. do_test select7-5.4 {
  120. catchsql {
  121. SELECT 5 IN (SELECT * FROM t2 UNION SELECT * FROM t2);
  122. }
  123. } [list 1 \
  124. {only a single result allowed for a SELECT that is part of an expression}]
  125. }
  126. # Verify that an error occurs if you have too many terms on a
  127. # compound select statement.
  128. #
  129. ifcapable compound {
  130. if {$SQLITE_MAX_COMPOUND_SELECT>0} {
  131. set sql {SELECT 0}
  132. set result 0
  133. for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} {
  134. append sql " UNION ALL SELECT $i"
  135. lappend result $i
  136. }
  137. do_test select7-6.1 {
  138. catchsql $sql
  139. } [list 0 $result]
  140. append sql { UNION ALL SELECT 99999999}
  141. do_test select7-6.2 {
  142. catchsql $sql
  143. } {1 {too many terms in compound SELECT}}
  144. }
  145. }
  146. # This block of tests verifies that bug aa92c76cd4 is fixed.
  147. #
  148. do_test select7-7.1 {
  149. execsql {
  150. CREATE TABLE t3(a REAL);
  151. INSERT INTO t3 VALUES(44.0);
  152. INSERT INTO t3 VALUES(56.0);
  153. }
  154. } {}
  155. do_test select7-7.2 {
  156. execsql {
  157. pragma vdbe_trace = 0;
  158. SELECT (CASE WHEN a=0 THEN 0 ELSE (a + 25) / 50 END) AS categ, count(*)
  159. FROM t3 GROUP BY categ
  160. }
  161. } {1.38 1 1.62 1}
  162. do_test select7-7.3 {
  163. execsql {
  164. CREATE TABLE t4(a REAL);
  165. INSERT INTO t4 VALUES( 2.0 );
  166. INSERT INTO t4 VALUES( 3.0 );
  167. }
  168. } {}
  169. do_test select7-7.4 {
  170. execsql {
  171. SELECT (CASE WHEN a=0 THEN 'zero' ELSE a/2 END) AS t FROM t4 GROUP BY t;
  172. }
  173. } {1.0 1.5}
  174. do_test select7-7.5 {
  175. execsql { SELECT a=0, typeof(a) FROM t4 }
  176. } {0 real 0 real}
  177. do_test select7-7.6 {
  178. execsql { SELECT a=0, typeof(a) FROM t4 GROUP BY a }
  179. } {0 real 0 real}
  180. do_test select7-7.7 {
  181. execsql {
  182. CREATE TABLE t5(a TEXT, b INT);
  183. INSERT INTO t5 VALUES(123, 456);
  184. SELECT typeof(a), a FROM t5 GROUP BY a HAVING a<b;
  185. }
  186. } {text 123}
  187. finish_test